1 10 11 12 package org.enhydra.jawe.xml.panels; 13 14 import org.enhydra.jawe.xml.*; 15 16 import java.util.*; 17 import javax.swing.*; 18 import java.awt.*; 19 import java.awt.event.*; 20 21 24 public class XMLListControlPanel extends XMLControlPanel { 25 26 public XMLListControlPanel (XMLCollection myOwner, 28 String title, boolean isVertical,boolean hasBorder, 29 boolean editButton) { 30 31 super(myOwner,title,isVertical,hasBorder); 32 33 controlledPanel=((XMLCollection)myOwner).getControlledPanel(); 34 35 String a=XMLUtil.getLanguageDependentString("AddKey"); 36 String r=XMLUtil.getLanguageDependentString("RemoveKey"); 37 String e=XMLUtil.getLanguageDependentString("EditKey"); 38 39 Dimension dim; 40 if (editButton) { 41 dim=getPreferredDimension(new String []{a,r}); 42 } else { 43 dim=getPreferredDimension(new String []{a,r,e}); 44 } 45 46 final XMLButton buttonAdd=new XMLButton(a,"Add",dim); 47 final XMLButton buttonRemove=new XMLButton(r,"Remove",dim); 48 final XMLButton buttonEdit=new XMLButton(e,"EditCellSmall",dim); 49 50 buttonAdd.setEnabled(!myOwner.isReadOnly()); 51 buttonRemove.setEnabled(!myOwner.isReadOnly()); 52 add(Box.createRigidArea(new Dimension(0,10))); 53 add(buttonAdd); 54 add(Box.createRigidArea(new Dimension(0,5))); 55 add(buttonRemove); 56 if (editButton) { 57 add(Box.createRigidArea(new Dimension(0,5))); 58 add(buttonEdit); 59 } 60 61 buttonRemove.addActionListener(new ActionListener() { 62 public void actionPerformed(ActionEvent e) { 63 JList l=((XMLListPanel)controlledPanel).getList(); 64 Object obj=null; 65 int ind=0; 66 try { 67 obj=l.getSelectedValue(); 68 ind=l.getSelectedIndex(); 69 } catch (Exception ex) {} 70 71 if (obj!=null) { 72 if (((XMLCollection)getOwner()).canRemoveElement((XMLElement)obj)) { 73 int yn=JOptionPane.showConfirmDialog(getDialog(), 74 XMLUtil.getLanguageDependentString( 75 "MessageDoYouReallyWantToRemoveSelectedItem"), 76 ((XMLElement)obj).toLabel()+" - "+ 77 XMLUtil.getLanguageDependentString("DeletingKey"), 78 JOptionPane.YES_NO_OPTION); 79 if (yn==JOptionPane.YES_OPTION) { 80 DefaultListModel listModel=(DefaultListModel)l.getModel(); 81 listModel.removeElement(obj); 82 ((XMLCollection)getOwner()).remove(obj); 83 ((XMLCollection)getOwner()).onElementRemoved((XMLElement)obj); 84 } else { 85 l.requestFocus(); 86 return; 87 } 88 } else { 89 String message=""; 90 if (obj instanceof XMLComplexElement) { 91 message=XMLUtil.getLanguageDependentString(((XMLCollection)getOwner()). 92 getInUseMessageName((XMLComplexElement)obj)); 93 } 94 JOptionPane.showMessageDialog(getDialog(), 95 message,XMLUtil.getLanguageDependentString("Title"), 96 JOptionPane.ERROR_MESSAGE); 97 } 98 } else { 99 JOptionPane.showMessageDialog(getDialog(), 100 XMLUtil.getLanguageDependentString("WarningEmptySelectionToEditOrDelete"), 101 XMLUtil.getLanguageDependentString("Title"), 102 JOptionPane.WARNING_MESSAGE); 103 } 104 try { 105 if (ind==0) { 106 ind++; 107 } 108 l.setSelectedIndex(ind-1); 109 } catch (Exception ex) {} 110 l.requestFocus(); 111 } 112 }); 113 114 buttonAdd.addActionListener(new ActionListener() { 115 public void actionPerformed(ActionEvent e) { 116 JList l=((XMLListPanel)controlledPanel).getList(); 117 DefaultListModel listModel=(DefaultListModel)l.getModel(); 118 int size = listModel.getSize(); 119 120 XMLElement newElement=((XMLCollection)getOwner()).generateNewElement(); 121 String label=newElement.toLabel()+" - "+XMLUtil.getLanguageDependentString("SelectKey"); 122 XMLElementDialog de=new XMLElementDialog(getDialog(),label); 123 de.editXMLElement(newElement.getPanel(),true,false); 124 if (!de.isCanceled()) { 126 if (!((XMLCollection)getOwner()).canInsertElement(newElement)) { 127 return; 128 } 129 listModel.addElement(newElement); l.setSelectedIndex(size); 131 ((XMLCollection)getOwner()).add(newElement); 132 ((XMLCollection)getOwner()).onElementInserted(newElement); 133 } 134 l.requestFocus(); 135 } 136 }); 137 138 buttonEdit.addActionListener(new ActionListener() { 140 public void actionPerformed(ActionEvent e) { 141 JList l=((XMLListPanel)controlledPanel).getList(); 142 XMLElement editElement=null; 143 try { 144 editElement=(XMLElement)l.getSelectedValue(); 145 } catch (Exception ex) {} 146 147 if (editElement!=null) { 148 XMLElementDialog de=new XMLElementDialog(getDialog(), 149 editElement.toLabel()+" '"+editElement.toString()+"' - "+ 150 XMLUtil.getLanguageDependentString("EditingKey")); 151 de.editXMLElement(editElement.getPanel(),true,false); 152 if (!de.isCanceled()) { 153 ((XMLCollection)getOwner()).onElementModified(editElement); 154 controlledPanel.paintAll(controlledPanel.getGraphics()); 155 } 156 } else { 157 JOptionPane.showMessageDialog(getDialog(), 158 XMLUtil.getLanguageDependentString("WarningEmptySelectionToEditOrDelete"), 159 XMLUtil.getLanguageDependentString("Title"), 160 JOptionPane.WARNING_MESSAGE); 161 } 162 l.requestFocus(); 163 } 164 }); 165 166 } 167 168 } 169 170 | Popular Tags |