package diploma;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.sql.*; 
import java.util.*;
import java.io.*;


public class TabelModificat extends TabelNou {
	String numeTabel;
	protected JButton cpNouButton, modifCpButton, redenTabButton, stergeTabButton;
	private TabelMysqlObj tabObj;	//până la sfârșit păstrează vechile valori,
	//așa cum sunt citite din fișierul de pe disc
	public Atribute campSelectat, indexSelectat;	//păstrează vechile valori
	public static final int CAMP_NOU = 100;		
	public static final int CAMP_STERS = 200;
	public static final int CAMP_MODIF = 300;
	public static final int INDEX_NOU = 400;
	public static final int INDEX_STERS = 500;
	public static final int NUME_NOU_TAB = 600;
	public static final int TAB_STERS = 700;
	public int ultimaActiune = 0;	//public - pt a putea fi accesat din umpleIndx
	
	class AscultaLaModifTabel implements ActionListener, ListSelectionListener,
		ChangeListener, MouseListener {
		public void actionPerformed(ActionEvent e) {
			Component[] comp1 = {etNumeCp, numeCampFTxField,  tipCpComboBox,
				etLungCp, lungCampTxField, formatCpComboBox, nenulCheckBox,
				implicCheckBox, implicTxField, nenegCheckBox, zeroCheckBox, 
				autoIncCheckBox, binaryCheckBox, umpleEnSet.inputTxField, 
				umpleEnSet.eliminButton, enSet.lista1, enSet.buton1, 
				enSet.buton2, enSet.lista2, umpleEnSet.infoLabel1, 
				umpleEnSet.infoLabel2}; 
			Component[] comp2 = {indUnRadButton, indNUnRadButton, 
				chPrimRadButton, /*chStrRadButton,*/ ftextRadButton, indx.lista1, 
				indx.buton1, indx.lista2, umpleIndx.inputTxField, 
				umpleIndx.eliminButton, umpleIndx.infoLabel1, 
				umpleIndx.infoLabel2};
			if (e.getActionCommand() == "Câmp nou") {
				campList.clearSelection();		//deselectăm
				for (Component compon : comp1)
					compon.setEnabled(true);	
				resetAtrib();
				numeCampFTxField.requestFocusInWindow();
				ultimaActiune = CAMP_NOU;
			}
			else if (e.getActionCommand() == "Modifică câmp") {
				if (campList.getSelectedIndex() < 0)
					return;
				campSelectat = arrayCpAtribHash.get(numeCampFTxField.getText());
				for (Component compon : comp1)
					compon.setEnabled(true);	
				resetAtrib();
				completareControaleDinAtribute(campSelectat, true);
				ultimaActiune = CAMP_MODIF;
			}
			else if(e.getActionCommand() == "Index nou") {
				for (Component compon : comp2)
					compon.setEnabled(true);
				if (cheiePrimara)
					chPrimRadButton.setEnabled(false);
				if (((DefaultListModel)indxNumeList.getModel()).isEmpty())
					umpleIndx.eliminButton.setEnabled(false);	
				ultimaActiune = INDEX_NOU;
				if (!((DefaultListModel)indx.lista2.getModel()).isEmpty())
					((DefaultListModel)indx.lista2.getModel()).removeAllElements();
				if (indxNumeList.getSelectedIndex() > -1)	
					indxNumeList.clearSelection();
				umpleIndx.inputTxField.setText("");
				umpleIndx.inputTxField.requestFocusInWindow();
			}
			else if (e.getActionCommand() == "Redenumire tabel") {
				etNumeTabel.setEnabled(true);
				numeTabelFTxField.setToolTipText(
					"Introduceți noul nume și apăsați <enter>. " +
					"Maxim 64 caractere, primul literă, fără '?', ' ', '*'," +
					"'/', '\\', '.' sau '('");
				numeTabelFTxField.setEnabled(true);
				numeTabelFTxField.requestFocusInWindow();
				numeTabelFTxField.selectAll();
			}
			else if (e.getActionCommand().equals("Enter nume tabel")) {
				etNumeTabel.setEnabled(false);
				numeTabelFTxField.setEnabled(false);
				if (numeTabel.equals(numeTabelFTxField.getText()))
					return;
				ultimaActiune = NUME_NOU_TAB;
				adCpSauIdxLaCom(null, true);
				resetButton.requestFocusInWindow();
			}
			else if (e.getActionCommand() == "Ștergere tabel") {
				int answ = JOptionPane.showConfirmDialog(null, "Sunteți absolut"
					+ " sigur că vreți \nsă ștergeți tabelul " + numeTabel +
					" ?", "Alegeți", JOptionPane.YES_NO_OPTION);
				if (answ == JOptionPane.YES_OPTION) {
					File tabFis = new File(numeBazaDeDate, tabObj.getNumeTabel()
						+ ".txt");	//numele vechi, neschimbat eventual
					if (tabFis.exists()) tabFis.delete();
					ultimaActiune = TAB_STERS;
					adCpSauIdxLaCom(null, true);	
				}
			}
			else if (e.getActionCommand() == "Închidere") {
				if (tabObj != null) {
					File fisInit = new File(numeBazaDeDate, tabObj.getNumeTabel() + 
						".txt");
					File scrPeDisc = new File(numeBazaDeDate, numeTabel + ".txt");
					if (fisInit.exists() && ! tabObj.getNumeTabel().equals(
						numeTabel)) {	
						fisInit.delete();		//s-a schimbat numele tabelului, 
					}				//ștergem vechiul fișier
					tabObj = new TabelMysqlObj(numeBazaDeDate, numeTabel, 
						arrayCpAtribHash, arrayIdxAtribHash);
					ObjectOutputStream objOutStr;
					try {
						objOutStr = new ObjectOutputStream(new FileOutputStream(
							scrPeDisc));
						objOutStr.writeObject(tabObj);
						try {
							if (objOutStr != null)
								objOutStr.close();	
						}
						catch(IOException exc) {}
					}
					catch(IOException exc) {
						Toolkit.getDefaultToolkit().beep();
						JOptionPane.showMessageDialog(null, "Nu s-au putut scrie pe"
							+ " disc informațiile \ndespre tabelul cu numele " +
							numeTabel + ".\nInterogați baza de date MYSQL",
							"Atenție ! Eroare la scriere !", 
							JOptionPane.ERROR_MESSAGE);
							dispose();
					}
					dispose();
				}
				else dispose();
			}
		}
	
		public void stateChanged(ChangeEvent e) {
			if (e.getSource() == campSiIndexTabbedPane) {
				if (campSiIndexTabbedPane.getSelectedIndex() == 0) {	//e câmp
					cpNouButton.setLabel("Câmp nou");
					cpNouButton.setMnemonic(KeyEvent.VK_C);
					modifCpButton.setVisible(true);
				}
				else {			//e index
					cpNouButton.setLabel("Index nou");
					cpNouButton.setMnemonic(KeyEvent.VK_O);
					modifCpButton.setVisible(false);
				}
			}
		}
		
		public void mouseEntered(MouseEvent e) {
			if (e.getSource() == campList) {
				campList.addListSelectionListener(this);
			}
		}
		public void mouseExited(MouseEvent e) { 
			if (e.getSource() == campList) {
				campList.removeListSelectionListener(this);
			}
		}
		public void mouseClicked(MouseEvent e) {} 
		public void mousePressed(MouseEvent e) {} 
		public void mouseReleased(MouseEvent e) {} 
		
	
		public void valueChanged(ListSelectionEvent e) {
			if (e.getSource() == campList) {
				resetAtrib();
				campSiIndexTabbedPane.setSelectedIndex(0);
				String cpSelString = (String) campList.getSelectedValue();
				int indice = cpSelString.indexOf(" ");
				Atribute cpCrt = arrayCpAtribHash.get(
					cpSelString.substring(0, indice));
		//		comSqlEdPane.setText(cpCrt.afisAtrib(true) + "<br>");
				completareControaleDinAtribute(cpCrt, false);
			}
		}
	}
	
	public TabelModificat(InterfataGrafica interfataGrafica) {
		super(interfataGrafica);
		this.numeTabel = (String)interfataGrafica.intGrafList.getSelectedValue();
		//creăm un obiect ce ascultă evenimentele :
		AscultaLaModifTabel ascModTab = new AscultaLaModifTabel();
		setTitle("Modificare a structurii tabelului " + numeTabel);
		cpNouButton = new JButton("Câmp nou");
		cpNouButton.setMnemonic(KeyEvent.VK_C);
		cpNouButton.addActionListener(ascModTab);
		modifCpButton = new JButton("Modifică câmp");
		modifCpButton.setMnemonic(KeyEvent.VK_F);
		modifCpButton.addActionListener(ascModTab);
		butCpPanel.add(modifCpButton, 1);
		butCpPanel.add(cpNouButton, 2);
		((FlowLayout) butCpPanel.getLayout()).setHgap(45);
		redenTabButton = new JButton("Redenumire tabel");
		redenTabButton.setMnemonic(KeyEvent.VK_M);
		redenTabButton.addActionListener(ascModTab);
		stergeTabButton = new JButton("Ștergere tabel");
		stergeTabButton.setMnemonic(KeyEvent.VK_A);
		stergeTabButton.addActionListener(ascModTab);
		butonPanel.add(redenTabButton, 0);
		butonPanel.add(stergeTabButton, 0);
		inregTabButton.setVisible(false);	//nu trebuie butonul de creare tabel
		anulTabButton.setLabel("Închidere");
		anulTabButton.setMnemonic(KeyEvent.VK_D);
		anulTabButton.addActionListener(ascModTab);
		campList.addMouseListener(ascModTab);
		campSiIndexTabbedPane.addChangeListener(ascModTab);
		Component[] compCpSiIndArray = {numeTabelFTxField, tempTabel,  
				tipCpComboBox, lungCampTxField, formatCpComboBox, nenulCheckBox, 
				implicCheckBox, implicTxField, nenegCheckBox, zeroCheckBox, 
				autoIncCheckBox, binaryCheckBox, umpleEnSet.inputTxField, 
				umpleEnSet.eliminButton, enSet.lista1, enSet.buton1, enSet.buton2, 
				enSet.lista2, numeCampFTxField, umpleIndx.inputTxField, 
				indUnRadButton, indNUnRadButton, chPrimRadButton,
				chStrRadButton, ftextRadButton, indx.lista1, indx.buton1, 
				indx.buton2, indx.lista2, etNumeTabel, etNumeCp, etLungCp,
				umpleEnSet.infoLabel1, umpleEnSet.infoLabel2, 
				umpleIndx.infoLabel1, umpleIndx.infoLabel2};
		for (Component compCrt : compCpSiIndArray)
			compCrt.setEnabled(false);	
		File tabFis = new File(numeBazaDeDate, numeTabel + ".txt");
		if (! tabFis.exists() || ! tabFis.isFile() || tabFis.length() == 0) {
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(null, "Nu s-au salvat local informații "
		 	+ "despre\ntabelul cu numele " + numeTabel +
		 	".\nApăsați butonul \'Vizualizare structură\'",
			"Atenție !", JOptionPane.WARNING_MESSAGE);
			return;
		}
		ObjectInputStream objInpStr;
		try {
			objInpStr = new ObjectInputStream(new FileInputStream(
				tabFis));
			tabObj = (TabelMysqlObj) objInpStr.readObject();
		}
		catch(IOException exc) {
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(null, "Nu s-au putut citi informațiile "
		 	+ "despre\ntabelul cu numele " + numeTabel +
		 	".\nApăsați butonul \'Vizualizare structură\' din aplicație și apoi reveniți",
			"Atenție ! Eroare la citire !", JOptionPane.ERROR_MESSAGE);
			return;
		}
		catch (ClassNotFoundException exc) {
			return;
		} 
		try {
			objInpStr.close();	
		}
		catch(IOException exc) {}
		numeTabelFTxField.setText(numeTabel);
		numeTabelFTxField.setActionCommand("Enter nume tabel");
		numeTabelFTxField.addActionListener(ascModTab);
		
		arrayCpAtribHash = tabObj.getArrayCpAtribHash();
		arrayIdxAtribHash = tabObj.getArrayIdxAtribHash();
		if (tabObj.isTemporary()) {
			tempTabel.setSelected(true);
		}
		//populăm campList cu câmpurile tabelului :
		for (Atribute cpCrt : arrayCpAtribHash.values()) {
			if (cpCrt.getProperty("auto_increment").equals("auto_increment"))
				auto_inc = true;
			String cpInList = cpCrt.afisAtrib(true);
			((DefaultListModel) campList.getModel()).addElement(cpInList);
			((DefaultListModel) indx.lista1.getModel()).addElement(
				cpCrt.getProperty("numeCamp"));
		}
		for (Atribute indCrt : arrayIdxAtribHash.values()) {
			if (indCrt.getProperty("numeIndex").equals("primary"))
				cheiePrimara = true;
			((DefaultListModel) indxNumeList.getModel()).addElement(
				indCrt.getProperty("numeIndex"));	
		}
		campList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		comandaSqlEdPane = new StringBuffer("<html><body text=\"green\"> " + 
			"</body></html>");
	//fereastra se închide doar din butonul Închidere, pt. a scrie inf. pe disc
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
			
		this.setFocusTraversalPolicy(new FocusTraversalPolicy() {
			private Component[] compFormNrArray = {numeTabelFTxField, tempTabel, 
				numeCampFTxField, tipCpComboBox, lungCampTxField, 
				formatCpComboBox, nenulCheckBox, implicCheckBox, implicTxField, 
				nenegCheckBox, zeroCheckBox, autoIncCheckBox, binaryCheckBox, 
				resetButton, modifCpButton, cpNouButton, inregCpButton, campList,  
				stergeCpButton, stergeTabButton, redenTabButton, anulTabButton, 
				comSqlButton};
			private Component[] compEnSetArray = {numeTabelFTxField, tempTabel, 
				numeCampFTxField, tipCpComboBox, umpleEnSet.inputTxField, 
				umpleEnSet.eliminButton, enSet.lista1, enSet.buton1, 
				enSet.buton2, enSet.lista2, resetButton, modifCpButton, 
				cpNouButton, inregCpButton, campList, stergeCpButton, 
				stergeTabButton, redenTabButton, anulTabButton, 
				comSqlButton}; 
			private Component[] compIndexArray = {numeTabelFTxField, tempTabel,
				umpleIndx.inputTxField, umpleIndx.eliminButton, indxNumeList,
				indUnRadButton, indNUnRadButton, chPrimRadButton, chStrRadButton, 
				ftextRadButton, indx.lista1, indx.buton1, indx.buton2, 
				indx.lista2, resetButton, cpNouButton, inregCpButton, campList, 
				stergeCpButton, stergeTabButton, redenTabButton, anulTabButton, 
				comSqlButton}; 
			
			public Component getComponentAfter(Container cont, Component comp) {
				int index = tipCpComboBox.getSelectedIndex();
				Component[] compon;
				if (campSiIndexTabbedPane.getSelectedIndex() == 1)
					compon = compIndexArray;
				else if (index > 23 && campSiIndexTabbedPane.getSelectedIndex() 
					== 0) 	//avem enum sau set
					compon = compEnSetArray;
				else compon = compFormNrArray;
				int i = compon.length;
				for (int j = 0; j < i; j++) {
					if (comp.equals(compon[j])) {
						while (	! compon[(j + 1) % i].isEnabled() || 
							! compon[(j + 1) % i].isVisible())
							j++;
						return compon[(j + 1) % i];
					}
				}
				return null;	
			}
			public Component getComponentBefore(Container cont, Component comp) {
				int index = tipCpComboBox.getSelectedIndex();
				Component[] compon;
				if (campSiIndexTabbedPane.getSelectedIndex() == 1)
					compon = compIndexArray;
				else if (index > 23 && campSiIndexTabbedPane.getSelectedIndex() == 0) 	//avem enum sau set
					compon = compEnSetArray;
				else compon = compFormNrArray;
				int i = compon.length;
				for (int j = i - 1; j >= 0; j--) {
					if (comp.equals(compon[j])) {
						while (	! compon[(j + i - 1) % i].isEnabled() || 
							! compon[(j + 1) % i].isVisible())
							j--;
						return compon[(j + i - 1) % i];
					}
				}
				return null;		
			}
			public Component getDefaultComponent(Container cont) {
            	if (campSiIndexTabbedPane.getSelectedIndex() == 0)
            		return numeTabelFTxField;
            	else return umpleIndx.inputTxField;
        	}
        	public Component getLastComponent(Container cont) {
            	return comSqlButton;
        	}
	        public Component getFirstComponent(Container cont) {
            	if (campSiIndexTabbedPane.getSelectedIndex() == 0)
            		return numeTabelFTxField;
            	else return umpleIndx.inputTxField;
	        }
		});
	}
	
	public void completareControaleDinAtribute(Atribute cpCrt, boolean 
		controaleActivate) {
			numeCampFTxField.setText(cpCrt.getProperty("numeCamp"));
			//pt enum sau set tipul e : enum(.....) sau set(.....)
			//int are 3 litere și substring(0, 4) îl blochează
			if (! cpCrt.getProperty("tipCamp").equals("int") &&
				(cpCrt.getProperty("tipCamp").substring(0, 3).equals("set") ||
				cpCrt.getProperty("tipCamp").substring(0, 4).equals("enum"))) {
				int indParDesch = cpCrt.getProperty("tipCamp").indexOf("(");
				Component[] comp1 = {umpleEnSet.inputTxField, 
					umpleEnSet.eliminButton, enSet.lista1, enSet.buton1, 
					enSet.buton2, enSet.lista2};
				if (cpCrt.getProperty("tipCamp").substring(
					0, indParDesch).equals("enum"))
					tipCpComboBox.setSelectedItem("enum");
				else 
					tipCpComboBox.setSelectedItem("set");
				int indParInch = cpCrt.getProperty("tipCamp").indexOf(")");
				String cpMultime = cpCrt.getProperty("tipCamp").substring(
					indParDesch + 1, indParInch);
				String[] cpMultArray = cpMultime.split(", ");
				for(String cpCrtMult : cpMultArray) {
					cpCrtMult = cpCrtMult.substring(1, 
						cpCrtMult.length() - 1);	//fără ' și '
					((DefaultListModel) enSet.lista1.getModel()).addElement(
						cpCrtMult);
				} 
			//default('ceas', 'carte',...) pt set și default 'da' pt enum
				String defStr = cpCrt.getProperty("default");
				if (! defStr.equals("no")) {
					cpMultime = defStr.substring(defStr.length() - 1,
						defStr.length()).equals(")") ? defStr.substring(8,
						defStr.length() - 1) : defStr.substring(8);
					cpMultArray = cpMultime.split(", ");
					for(String cpCrtMult : cpMultArray) {
						cpCrtMult = cpCrtMult.substring(1, 
							cpCrtMult.length() - 1);	//fără ' și '
						((DefaultListModel) enSet.lista2.getModel()).addElement(
							cpCrtMult);
					}
				}
				if (! controaleActivate) {
					for (Component compon : comp1)
						compon.setEnabled(false);
				}
			}
			else {		//tipul nu e enum sau set
				Component[] comp2 = {lungCampTxField, formatCpComboBox,
					nenulCheckBox, implicCheckBox, implicTxField, 
					nenegCheckBox, zeroCheckBox, autoIncCheckBox, 
					binaryCheckBox, etLungCp, etNumeCp, numeCampFTxField,
					tipCpComboBox}; 
				
				tipCpComboBox.setSelectedItem(cpCrt.getProperty("tipCamp"));
				String crtString;
				if (! cpCrt.getProperty("lungCamp").equals("no"))
					lungCampTxField.setText(cpCrt.getProperty("lungCamp"));
				if (! (crtString = cpCrt.getProperty("formatCamp")).equals(
					"no"))
					formatCpComboBox.setSelectedItem(cpCrt.getProperty(
						"formatCamp"));
				if (cpCrt.getProperty("not null").equals("not null"))
					nenulCheckBox.setSelected(true);
				if (! (crtString = cpCrt.getProperty("default")).equals(
					"no")) {	//ex: crtString = "default 'azi'"
						implicCheckBox.setSelected(true);
						if (crtString.indexOf("default '") == 0)
							implicTxField.setText(crtString.substring(9,
								crtString.length() - 1));
						else	//ex: crtStr = "default 100"
							implicTxField.setText(crtString.substring(8));
				}
				if (cpCrt.getProperty("unsigned").equals("unsigned"))
					nenegCheckBox.setSelected(true);
				if (cpCrt.getProperty("zerofill").equals("zerofill"))
					zeroCheckBox.setSelected(true);
				if (cpCrt.getProperty("auto_increment").equals(
					"auto_increment"))
					autoIncCheckBox.setSelected(true);
				if (cpCrt.getProperty("binary").equals("binary"))
					binaryCheckBox.setSelected(true);
				if (! controaleActivate) {
					for (Component compon : comp2)
						compon.setEnabled(false);
				}
			}
	}
	
	protected void adCpSauIdxLaCom(Atribute atr, boolean eCamp) {
		comandaSql = new StringBuffer("");	//câte o comandă către MYSQL
		comandaSql = comandaSql.append("alter table " + numeTabel + " ");
		int offset;
		switch (ultimaActiune) {
			case CAMP_NOU : 
				StringBuffer cpNouStr = new StringBuffer(atr.afisAtrib(true));
				comandaSql = comandaSql.append("add " + cpNouStr.toString());
//nu s-a putut executa comanda, nu modificăm și nu scriem nimic :				
				if (! execComanda())
					break;	
				offset = cpNouStr.indexOf(" ");
				cpNouStr = cpNouStr.insert(offset, "</font>");
				cpNouStr = cpNouStr.insert(0, "<font color=\"red\">");
				cpNouStr = cpNouStr.insert(0, "alter table <font color=\"red\">"
					+ numeTabel + "</font> add ");
				cpNouStr = cpNouStr.append("<br>");
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, cpNouStr.toString());
				break;
			case CAMP_MODIF :
				StringBuffer cpModStr = new StringBuffer(atr.afisAtrib(true));
				comandaSql = comandaSql.append("change " + 
					campSelectat.getProperty("numeCamp") + " " + 
					cpModStr.toString());
				if (! execComanda())
					break;	
				offset = cpModStr.indexOf(" ");
				cpModStr = cpModStr.insert(offset, "</font>");
				cpModStr = cpModStr.insert(0, campSelectat.getProperty(
					"numeCamp") + " ");
				cpModStr = cpModStr.insert(0, "<font color=\"red\">");
				cpModStr = cpModStr.insert(0, "alter table <font color=\"red\">"
					+ numeTabel + "</font> change ");
				cpModStr = cpModStr.append("<br>");
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, cpModStr.toString());
				//dacă s-a schimbat numele câmpului, ștergem vechiul nume din
				//indx.list1 și din arrayCpAtribHash :
				if (! atr.getProperty("numeCamp").equals(
					campSelectat.getProperty("numeCamp"))) {
					((DefaultListModel)indx.lista1.getModel()).removeElement(
						campSelectat.getProperty("numeCamp"));
					arrayCpAtribHash.remove(campSelectat.getProperty(
						"numeCamp"));
					//înlocuim cu noul nume în listaCpIndexate ale indecșilor
					//ce conțin câmpul în cauză
	 				stergeCpDinListaDeCpAIdx(atr, false);
				}
				
				//ștergem vechiul câmp din campList :
				cpModStr = new StringBuffer(campSelectat.afisAtrib(true));
				if (((DefaultListModel)campList.getModel()).contains(
					cpModStr.toString()))
					((DefaultListModel)campList.getModel()).removeElement(
						cpModStr.toString());
				campList.setSelectedValue(atr.afisAtrib(true), true);
		 
				break;
			case CAMP_STERS :
				StringBuffer cpDelStr = new StringBuffer(
					atr.getProperty("numeCamp"));
				comandaSql = comandaSql.append("drop " + cpDelStr.toString());
				if (! execComanda())
					break;	
				cpDelStr = cpDelStr.append("</font").append("<br>");;
				cpDelStr = cpDelStr.insert(0, "<font color=\"red\">");
				cpDelStr = cpDelStr.insert(0, "alter table <font color=\"red\">"
					+ numeTabel + "</font> drop ");
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, cpDelStr.toString());
				break;
			case INDEX_NOU :
				StringBuffer indNouStr = new StringBuffer(atr.afisAtrib(false));
				comandaSql = comandaSql.append("add " + indNouStr.toString());
				if (! execComanda())
					break;	
				offset = indNouStr.indexOf(atr.getProperty("numeIndex"));
				indNouStr = indNouStr.insert(offset, "  <font color=\"red\">");
				offset = indNouStr.indexOf(atr.getProperty("numeIndex"));
				offset += atr.getProperty("numeIndex").length(); 
				indNouStr = indNouStr.insert(offset, "  </font>");
				indNouStr = indNouStr.insert(0, "alter table <font color=\"red\">"
					+ numeTabel + "</font> add ");
				indNouStr = indNouStr.append("<br>");
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, indNouStr.toString());
				break;
			case INDEX_STERS :
				StringBuffer indDelStr = atr.getProperty("tipIndex").equals(
					"primary key") ? new StringBuffer("primary key") :
					new StringBuffer("index " + atr.getProperty("numeIndex"));
				comandaSql = comandaSql.append("drop " + indDelStr.toString());
				if (! execComanda())
					break;	
				offset = indDelStr.indexOf(atr.getProperty("numeIndex"));
				indDelStr = indDelStr.insert(offset, "  <font color=\"red\">");
				offset = indDelStr.indexOf(atr.getProperty("numeIndex"));
				offset += atr.getProperty("numeIndex").length(); 
				indDelStr = indDelStr.insert(offset, "  </font>");
				indDelStr = indDelStr.insert(0, "alter table <font color=\"red\">"
					+ numeTabel + "</font> drop ");
				indDelStr = indDelStr.append("<br>");
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, indDelStr.toString());
				break;
			case NUME_NOU_TAB :
				comandaSql = new StringBuffer("alter table " + numeTabel +
					" rename " + numeTabelFTxField.getText());
				if (! execComanda())
					break;	
				String linCom = "alter table <font color=\"red\">" + 
					numeTabel + "</font> rename <font color=\"red\">" + 
					numeTabelFTxField.getText() + "</font> <br>";
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, linCom);
				comSqlEdPane.setText(comandaSqlEdPane.toString());
				interfataGrafica.dlm.removeElement(numeTabel);
				interfataGrafica.dlm.addElement(numeTabelFTxField.getText());
				numeTabel = numeTabelFTxField.getText();	//noul nume de tab
				interfataGrafica.intGrafList.setSelectedValue(numeTabel, true);
				break;
			case TAB_STERS :
				comandaSql = new StringBuffer("drop table " + numeTabel);
				if (! execComanda()) {
					int answ = JOptionPane.showConfirmDialog(null, "Ștergeți"
						+ " salvarea locală a \ntabelului " + numeTabel +
						" ?", "Alegeți", JOptionPane.YES_NO_OPTION);
					if (answ == JOptionPane.NO_OPTION) 
						break;
				}
				offset = comandaSqlEdPane.indexOf("</body>");
				comandaSqlEdPane.insert(offset, "drop table <font color=\"red\">"
					+ numeTabel + "</font> <br>");
				comSqlEdPane.setText(comandaSqlEdPane.toString());
				tabObj = null;
				dispose();
				break;
				 
		}
		Component[] compCpSiIndArray = {numeTabelFTxField, tempTabel,  
				tipCpComboBox, lungCampTxField, formatCpComboBox, nenulCheckBox, 
				implicCheckBox, implicTxField, nenegCheckBox, zeroCheckBox, 
				autoIncCheckBox, binaryCheckBox, umpleEnSet.inputTxField, 
				umpleEnSet.eliminButton, enSet.lista1, enSet.buton1, enSet.buton2, 
				enSet.lista2, numeCampFTxField, umpleIndx.inputTxField, 
				indUnRadButton, indNUnRadButton, chPrimRadButton,
				chStrRadButton, ftextRadButton, indx.lista1, indx.buton1, 
				indx.buton2, indx.lista2, etNumeTabel, etNumeCp, etLungCp,
				umpleEnSet.infoLabel1, umpleEnSet.infoLabel2, 
				umpleIndx.infoLabel1, umpleIndx.infoLabel2};
		for (Component compCrt : compCpSiIndArray)
			compCrt.setEnabled(false);
	}
	
	//Trimitem comanda la serverul MySQL:
	public boolean execComanda() {
		try {
			Statement s_ment = interfataGrafica.conex.createStatement();
			s_ment.executeUpdate(comandaSql.toString());
			return true;
		}
		catch (SQLException exc) {
			JOptionPane.showMessageDialog(null, "Nu s-a putut executa "
		 	+ " comanda MySQL ALTER TABLE\n" + exc.getMessage(),
			"Atenție ! Eroare !", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
//Dacă există un index ce indexează un singur câmp, acel câmp nu poate fi șters.
//Altfel, câmpul poate fi șters cu comanda alter. Definim două funcții :
	public String listIdxNumaiCuAcestCp(String nmCp) {
		String listIdx = "";	//indecșii ce au numai acest cp nmCp indexat
		for (Atribute idxAtr : arrayIdxAtribHash.values()) {
			String listCpStr = idxAtr.getProperty("listaCpIndexate");
			listCpStr = listCpStr.substring(1, listCpStr.length() - 1);
			String[] listCpArray = listCpStr.split(", ");
			if (listCpArray.length == 1) {
				int offs = listCpArray[0].indexOf("(");	
				if (offs > 0)	//câmp indexat pe o anumită lungime
					listCpArray[0] = listCpArray[0].substring(0, offs);
				if (listCpArray[0].equals(nmCp))	//un singur câmp
					listIdx += idxAtr.getProperty("numeIndex") + ", "; 
			}
		}
		if (listIdx != "")
			listIdx = listIdx.substring(0, listIdx.length() - 2);	//fără ultima virgulă
		return listIdx;
	}
	
	public void stergeCpDinListaDeCpAIdx(Atribute cpAtr, boolean stergeCp) {
		for (Atribute idxAtr : arrayIdxAtribHash.values()) {
			String listCpStr = idxAtr.getProperty("listaCpIndexate");
			listCpStr = listCpStr.substring(1, listCpStr.length() - 1);
			String[] listCpArray = listCpStr.split(", ");
			String nouListCp = "(";
			for (String cpCrtStr : listCpArray) {
				int offs = cpCrtStr.indexOf("(");
				if (stergeCp) {
					if (offs > 0)	//câmp indexat pe o anumită lungime
						cpCrtStr = cpCrtStr.substring(0, offs);
					if (! cpCrtStr.equals(cpAtr.getProperty("numeCamp"))) {
						nouListCp += nouListCp.substring(nouListCp.length() - 1,
							nouListCp.length()).equals("(") ? cpCrtStr : 
							", " + cpCrtStr; 
					}
				}
				else {		//modifică numele câmpului
					String vechiNumeCpStr = campSelectat.getProperty("numeCamp");
					if (cpCrtStr.indexOf(vechiNumeCpStr) == 0) {
						if (offs < 0)
							cpCrtStr = cpAtr.getProperty("numeCamp");
						else cpCrtStr = cpCrtStr.replaceFirst(vechiNumeCpStr,
							cpAtr.getProperty("numeCamp"));
					}
					nouListCp += nouListCp.substring(nouListCp.length() - 1,
							nouListCp.length()).equals("(") ? cpCrtStr : 
							", " + cpCrtStr;
				}
			}
			nouListCp += ")";
			if (! listCpStr.equals(nouListCp.substring(1, 
				nouListCp.length() - 1)) && ! nouListCp.equals("()")) {
				idxAtr.setProperty("listaCpIndexate", nouListCp);
			}
		}
	}
}