1 11 12 13 package org.enhydra.jawe.xml; 14 15 import org.enhydra.jawe.xml.panels.*; 16 17 import javax.swing.*; 18 import java.awt.*; 19 import java.awt.event.*; 20 21 import java.util.*; 22 import java.net.URL ; 23 24 34 public class XMLElementDialog extends JDialog { 35 private XMLPanel panelToEdit=new XMLPanel(); 36 private JButton buttonOK; 37 private JButton buttonCancel; 38 39 private WindowListener wl=new WindowAdapter() { 40 public void windowClosing(WindowEvent e) { 41 isCanceled=true; 42 dispose(); 43 } 44 }; 45 46 47 private boolean isCanceled=false; 48 49 50 private static ArrayList xmlChangeListeners = new ArrayList(); 51 52 55 public XMLElementDialog () { 56 this((JFrame)null,""); 57 } 58 59 66 public XMLElementDialog (JFrame parent,String title) { 67 this(parent,title,true); 68 } 69 70 77 public XMLElementDialog (JDialog parent,String title) { 78 this(parent,title,true); 79 } 80 81 89 public XMLElementDialog (JFrame parent,String title,boolean isModal) { 90 super(parent,title,isModal); 91 initDialog(); 92 } 93 94 102 public XMLElementDialog (JDialog parent,String title,boolean isModal) { 103 super(parent,title,isModal); 104 initDialog(); 105 } 106 107 public static void addXMLElementChangedListener (XMLElementChangeListener ecl) { 108 xmlChangeListeners.add(ecl); 109 } 110 111 public static void removeXMLElementChangedListener (XMLElementChangeListener ecl) { 112 xmlChangeListeners.remove(ecl); 113 } 114 115 119 public boolean isCanceled () { 120 return isCanceled; 121 } 122 123 public void setCanceled (boolean isCanceled) { 124 this.isCanceled=isCanceled; 125 } 126 127 128 public XMLPanel getEditingPanel () { 129 return panelToEdit; 130 } 131 132 144 public void editXMLElement (XMLPanel elementPanel, 145 boolean hasCancelButton,boolean doNotClose) { 146 147 Container cp=getContentPane(); 148 cp.remove(panelToEdit); 149 panelToEdit=elementPanel; 150 cp.add(panelToEdit,0); 151 pack(); 152 153 if (doNotClose) { 156 removeWindowListener(wl); 157 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). 158 remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false)); 159 } 160 161 if (hasCancelButton) { 162 buttonCancel.setVisible(true); 163 } else { 164 buttonCancel.setVisible(false); 165 if (!doNotClose) { 166 buttonOK.requestFocus(); 167 } 168 } 169 170 setLocationRelativeTo(getParent()); 171 pack(); 172 show(); 173 requestFocus(); 174 } 175 176 private void initDialog () { 177 try { 178 JPanel buttonPanel=new JPanel(); 179 180 buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT); 181 buttonPanel.setAlignmentY(Component.TOP_ALIGNMENT); 182 183 buttonOK=new JButton(XMLUtil.getLanguageDependentString("OKKey")); 184 URL u = XMLUtil.getResource("OKImage"); 185 if (u!=null) { 186 buttonOK.setIcon(new ImageIcon(u)); 187 } 188 189 buttonCancel=new JButton(XMLUtil.getLanguageDependentString("CancelKey")); 190 u = XMLUtil.getResource("CancelImage"); 191 if (u!=null) { 192 buttonCancel.setIcon(new ImageIcon(u)); 193 } 194 195 buttonPanel.add(buttonOK); 196 buttonPanel.add(buttonCancel); 197 198 Container cp=getContentPane(); 199 cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS)); 200 201 cp.add(panelToEdit); 202 cp.add(buttonPanel); 203 204 buttonOK.addActionListener( new ActionListener(){ 206 public void actionPerformed( ActionEvent ae ){ 207 if (panelToEdit.getOwner().isReadOnly()) { 208 dispose(); 209 } else if (canApplyChanges()) { 210 applyChanges(); 211 dispose(); 212 if (getParent() != null){ 213 getParent().repaint(); } 215 } 216 } 217 }); 218 219 buttonCancel.addActionListener( new ActionListener(){ 221 public void actionPerformed( ActionEvent ae ){ 222 isCanceled=true; 223 dispose(); 224 } 225 }); 226 227 addWindowListener(wl); 228 229 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 230 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Cancel"); 231 getRootPane().getActionMap().put("Cancel", new AbstractAction() { 232 public void actionPerformed(ActionEvent e) { 233 isCanceled=true; 234 dispose(); 235 } 236 }); 237 238 } catch (Exception e) { 239 e.printStackTrace(); 240 } 241 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 242 setResizable(true); 243 setLocationRelativeTo(getParent()); 244 buttonOK.setDefaultCapable(true); 245 getRootPane().setDefaultButton(buttonOK); 246 247 } 248 249 250 public void switchPanel (XMLPanel newPanel,String newTitle,boolean hasCancelButton) { 251 if (isModal()) return; 252 253 if (canApplyChanges()) { 254 applyChanges(); 255 } else { 256 return; 257 } 258 259 setTitle(newTitle); 260 261 Container cp=getContentPane(); 262 cp.remove(panelToEdit); 263 panelToEdit=newPanel; 264 cp.add(panelToEdit,0); 265 pack(); 266 267 try { 268 panelToEdit.getComponent(0).requestFocus(); 269 } catch (Exception ex) { 270 panelToEdit.requestFocus(); 271 } 272 273 if (hasCancelButton) { 274 buttonCancel.setVisible(true); 275 } else { 276 buttonCancel.setVisible(false); 277 buttonOK.requestFocus(); 278 } 279 280 pack(); 281 show(); 282 283 } 284 285 public boolean canApplyChanges () { 286 if (panelToEdit.checkRequired()) { 287 if (!panelToEdit.getOwner().isValidEnter(panelToEdit)) { 288 return false; 289 } 290 if (panelToEdit.getOwner() instanceof XMLCollectionElement) { 291 if (((XMLCollectionElement)panelToEdit.getOwner()). 292 isIDUniqueAndValid(panelToEdit)) { 293 return true; 294 } else { 295 return false; 296 } 297 } else { 298 return true; 299 } 300 } else { 301 return false; 302 } 303 } 304 305 public void applyChanges () { 306 panelToEdit.setElements(); 307 notifyListeners(panelToEdit.getOwner()); 308 } 309 310 public static void notifyListeners (XMLElement changedEl) { 311 Iterator it=xmlChangeListeners.iterator(); 312 while (it.hasNext()) { 313 XMLElementChangeListener cl=(XMLElementChangeListener)it.next(); 314 cl.xmlElementChanged(changedEl); 315 } 316 } 317 318 public void requestFocus() { 319 try { 320 if (panelToEdit instanceof XMLGroupPanel) { 321 if (panelToEdit.getComponent(0).isEnabled()) { 322 panelToEdit.getComponent(0).requestFocus(); 323 } else { 324 panelToEdit.getComponent(1).requestFocus(); 325 } 326 } 327 } catch (Exception ex) { 328 panelToEdit.requestFocus(); 329 } 330 } 331 332 } 333 334 335 336 | Popular Tags |