KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > XMLElementDialog


1 /* XMLElementDialog.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  * Harald Meister harald.meister@abacus.ch
9  *
10  */

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 JavaDoc;
23
24 /**
25  * The dialog for showing objects derived from {@link XMLPanel} classes. The
26  * given XMLPanel object must have it's owner, which type is a class
27  * derived from the {@link XMLElement} class, and serves as a representation
28  * of an XML tag.
29  * <p> The dialog enables editing of all editable fields contained
30  * within given panel and after user presses OK button, the new values
31  * contained within edited fields are set to corresponding members of
32  * panel's owner.
33  */

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    /** Set to true if cancel hasn't been pressed */
47    private boolean isCanceled=false;
48
49    /** The list of XML change listeners. */
50    private static ArrayList xmlChangeListeners = new ArrayList();
51
52    /**
53     * Constructs a new modal unparented instance that will define new element.
54     */

55    public XMLElementDialog () {
56       this((JFrame)null,"");
57    }
58
59    /**
60     * Constructs a new modal instance which parent is <code>parent</code> and
61     * proceeds a title of dialog.
62     *
63     * @param parent Parent frame.
64     * @param title Dialog title.
65     */

66    public XMLElementDialog (JFrame parent,String JavaDoc title) {
67       this(parent,title,true);
68    }
69
70    /**
71     * Constructs a new modal instance which parent is <code>parent</code> and
72     * proceeds a title of dialog.
73     *
74     * @param parent Parent dialog.
75     * @param title Dialog title.
76     */

77    public XMLElementDialog (JDialog parent,String JavaDoc title) {
78       this(parent,title,true);
79    }
80
81    /**
82     * Constructs a new modal instance which parent is <code>parent</code> and
83     * proceeds a title of dialog.
84     *
85     * @param parent Parent frame.
86     * @param title Dialog title.
87     * @param isModal true if dialog is modal.
88     */

89    public XMLElementDialog (JFrame parent,String JavaDoc title,boolean isModal) {
90       super(parent,title,isModal);
91       initDialog();
92    }
93
94    /**
95     * Constructs a new modal instance which parent is <code>parent</code> and
96     * proceeds a title of dialog.
97     *
98     * @param parent Parent dialog.
99     * @param title Dialog title.
100     * @param isModal true if dialog is modal.
101     */

102    public XMLElementDialog (JDialog parent,String JavaDoc 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    /**
116     * @return <tt>true</tt> if cancel hasn't been pressed,
117     * <tt>false</tt> otherwise.
118     */

119    public boolean isCanceled () {
120       return isCanceled;
121    }
122
123    public void setCanceled (boolean isCanceled) {
124       this.isCanceled=isCanceled;
125    }
126
127    /** Returns the panel that is currently beeing edited. */
128    public XMLPanel getEditingPanel () {
129       return panelToEdit;
130    }
131
132    /**
133     * Displays dialog and adds specified panel to it, along
134     * with OK and/or Cancel button. The dialog allows user to edit
135     * the panel field(s) that represents corresponding member of
136     * it's owner class. After user presses OK, the content of
137     * panel field(s) is transfered to the corresponding owner
138     * class member. The member(s) of owner class represent a
139     * program equivalent for real XML entity.
140     *
141     * @param elementPanel The panel with visual presentation of it's
142     * owner members.
143     */

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       // doNotClose is true only for dialog for entering
154
// unique package Id - when we start to edit new package
155
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 JavaDoc 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          // action listener for confirming
205
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();//do repaint
214
}
215                      }
216                   }
217                });
218
219          // action listener for canceling
220
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {
328          panelToEdit.requestFocus();
329       }
330    }
331
332 }
333
334
335 /* End of XMLElementDialog.java */
336
Popular Tags