1 10 11 12 package org.enhydra.jawe.xml.panels; 13 14 import org.enhydra.jawe.xml.*; 15 import org.enhydra.jawe.xml.panels.tablesorting.*; 16 17 import java.util.*; 18 import javax.swing.*; 19 import javax.swing.table.*; 20 import java.awt.*; 21 import java.awt.event.*; 22 23 26 public class XMLTableControlPanel extends XMLControlPanel { 27 28 public XMLTableControlPanel (XMLCollection myOwner,String title, 29 boolean isVertical,boolean hasBorder) { 30 31 super(myOwner,title,isVertical,hasBorder); 32 33 controlledPanel=((XMLCollection)myOwner).getControlledPanel(); 34 35 String n=XMLUtil.getLanguageDependentString("NewLabel"); 36 String e=XMLUtil.getLanguageDependentString("EditKey"); 37 String d=XMLUtil.getLanguageDependentString("DeleteLabel"); 38 40 Dimension dim=getPreferredDimension(new String []{n,e,d}); 41 42 XMLButton buttonNew=new XMLButton(n,"NewSmall",dim); 43 XMLButton buttonEdit=new XMLButton(e,"EditCellSmall",dim); 44 XMLButton buttonDelete=new XMLButton(d,"DeleteSmall",dim); 45 47 if (isVertical) { 48 add(Box.createRigidArea(new Dimension(0,10))); 49 } 50 add(buttonNew); 51 add(Box.createRigidArea(new Dimension((isVertical)? 0:5,(isVertical)? 5:0))); 52 add(buttonEdit); 53 add(Box.createRigidArea(new Dimension((isVertical)? 0:10,(isVertical)? 10:0))); 54 add(buttonDelete); 55 add(Box.createVerticalGlue()); 58 59 buttonNew.setEnabled(!myOwner.isReadOnly()); 60 buttonDelete.setEnabled(!myOwner.isReadOnly()); 61 62 buttonNew.addActionListener(new ActionListener() { 64 public void actionPerformed(ActionEvent ae) { 65 XMLElement newElement=((XMLCollection)getOwner()).generateNewElement(); 66 XMLElementDialog de=new XMLElementDialog(getDialog(), 67 newElement.toLabel()+" - "+ 68 XMLUtil.getLanguageDependentString("DefiningKey")); 69 de.editXMLElement(newElement.getPanel(),true,false); 70 if (!de.isCanceled()) { 72 addElement(newElement); 73 ((XMLCollection)getOwner()).onElementCreated(newElement); 74 } 75 } 76 }); 77 78 buttonEdit.addActionListener(new ActionListener() { 80 public void actionPerformed(ActionEvent ae) { 81 XMLElement editElement=getSelectedElement(); 82 if (editElement != null) { XMLElementDialog de=new XMLElementDialog(getDialog(), 84 editElement.toLabel()+" '"+editElement.toString()+"' - "+ 85 XMLUtil.getLanguageDependentString("EditingKey")); 86 de.editXMLElement(editElement.getPanel(),true,false); 87 if (!de.isCanceled()) { 88 modifyElement(editElement); 89 ((XMLCollection)getOwner()).onElementModified(editElement); 90 } 91 } else { 92 complainLoudly("WarningEmptySelectionToEditOrDelete"); 93 } 94 ((XMLTablePanel)controlledPanel).getTable().requestFocus(); 95 } 96 }); 97 98 buttonDelete.addActionListener(new ActionListener() { 100 public void actionPerformed(ActionEvent ae) { 101 XMLElement deleteElement=getSelectedElement(); 102 if (deleteElement == null) { 103 complainLoudly("WarningEmptySelectionToEditOrDelete"); 104 } else if (deleteElement.isReadOnly()) { 105 complainLoudly(((XMLCollection)getOwner()). 106 getReadOnlyMessageName(deleteElement)); 107 } else if (!((XMLCollection)getOwner()).canRemoveElement(deleteElement)) { 108 complainLoudly(((XMLCollection)getOwner()). 109 getInUseMessageName(deleteElement)); 110 } else { 111 int yn=JOptionPane.showConfirmDialog(getDialog(), 112 XMLUtil.getLanguageDependentString( 113 "MessageDoYouReallyWantToRemoveSelectedItem"), 114 deleteElement.toLabel()+" - "+ 115 XMLUtil.getLanguageDependentString("DeletingKey"), 116 JOptionPane.YES_NO_OPTION); 117 if (yn==JOptionPane.YES_OPTION) { 118 removeElement(deleteElement); 119 ((XMLCollection)getOwner()).onElementDeleted(deleteElement); 120 XMLElementDialog.notifyListeners(new XMLElement()); 121 } 122 } 123 } 124 }); 125 126 142 143 } 144 145 public XMLElement getSelectedElement() { 146 return ((XMLTablePanel)controlledPanel).getSelectedElement(); 147 } 148 149 private void addElement(XMLElement e) { 150 XMLCollection owncol=(XMLCollection)getOwner(); 151 int rowpos=owncol.size(); 152 owncol.add(e); 153 JTable t=((XMLTablePanel)controlledPanel).getTable(); 154 DefaultTableModel dtm=(DefaultTableModel)t.getModel(); 155 Vector v=null; 156 if (e instanceof XMLComplexElement) { 157 v=new Vector(((XMLComplexElement)e).toComplexTypeValues()); 158 } else { 159 v=new Vector(); 160 v.add(e.toString()); 161 } 162 163 v.add(0,e); 165 dtm.insertRow(rowpos,v); 166 try { 168 t.setRowSelectionInterval(rowpos,rowpos); 170 } catch (Exception ex) {} 171 t.requestFocus(); 172 if (t instanceof SortingTable) { 173 ((SortingTable)t).performSorting(null); 174 } 175 } 176 177 private void modifyElement(XMLElement e) { 178 ((XMLTablePanel)controlledPanel).modifyElement(e); 179 } 180 181 private void removeElement(XMLElement e) { 182 ((XMLTablePanel)controlledPanel).removeElement(e); 183 } 184 185 188 public void complainLoudly(String message) { 189 JOptionPane.showMessageDialog(getDialog(), 190 XMLUtil.getLanguageDependentString(message), 191 XMLUtil.getLanguageDependentString("Title"), 192 JOptionPane.WARNING_MESSAGE); 193 } 194 } 195 196 | Popular Tags |