KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > discRack > presentation > DElementDialog


1 package discRack.presentation;
2
3 import discRack.*;
4 import discRack.presentation.delements.*;
5 import discRack.presentation.dpanels.*;
6 import discRack.business.person.*;
7
8 import javax.swing.*;
9 import java.awt.*;
10 import java.awt.event.*;
11
12 import java.util.*;
13 import java.net.URL JavaDoc;
14
15 /**
16  * The dialog for showing objects derived from {@link DPanel} classes. The
17  * given DPanel object must have it's owner, which type is a class
18  * derived from the {@link DSimpleElement} class.
19  * <p> The dialog enables editing of all editable fields contained
20  * within given panel and after user presses OK button, the new values
21  * contained within edited fields are set to corresponding members of
22  * panel's owner.
23  *
24  * @author Sasa Bojanic
25  * @version 1.0
26 */

27 public class DElementDialog extends JDialog {
28    private DPanel panelToEdit=new DPanel();
29    private JButton buttonOK;
30    private JButton buttonCancel;
31
32    private WindowListener wl=new WindowAdapter() {
33       public void windowClosing(WindowEvent e) {
34          isCanceled=true;
35          dispose();
36       }
37    };
38
39    /** Set to true if cancel hasn't been pressed */
40    private boolean isCanceled=false;
41
42    /**
43    * Constructs a new modal instance which parent is <code>parent</code> and
44    * proceeds a title of dialog.
45    *
46    * @param parent Parent frame.
47    * @param title Dialog title.
48    */

49    public DElementDialog (JFrame parent,String JavaDoc title) {
50       super(parent,title,true);
51       initDialog();
52    }
53
54    /**
55    * Constructs a new modal instance which parent is <code>parent</code> and
56    * proceeds a title of dialog.
57    *
58    * @param parent Parent dialog.
59    * @param title Dialog title.
60    */

61    public DElementDialog (JDialog parent,String JavaDoc title) {
62       super(parent,title,true);
63       initDialog();
64    }
65
66    /**
67    * @return <tt>true</tt> if cancel hasn't been pressed,
68    * <tt>false</tt> otherwise.
69    */

70    public boolean isCanceled () {
71       return isCanceled;
72    }
73
74    /**
75    * Displays dialog and adds specified panel to it, along
76    * with OK and/or Cancel button. The dialog allows user to edit
77    * the panel field(s) that represents corresponding member of
78    * it's owner class. After user presses OK, the content of
79    * panel field(s) is transfered to the corresponding owner
80    * class member.
81    *
82    * @param elementPanel The panel with visual presentation of it's
83    * owner members.
84    */

85    public void editDElement (DPanel elementPanel,boolean hasCancelButton) {
86
87       Container cp=getContentPane();
88       cp.remove(panelToEdit);
89       panelToEdit=elementPanel;
90       cp.add(panelToEdit,0);
91       pack();
92
93       if (hasCancelButton) {
94          buttonCancel.setVisible(true);
95       } else {
96          buttonCancel.setVisible(false);
97          buttonOK.requestFocus();
98       }
99
100       setLocationRelativeTo(getParent());
101       pack();
102       show();
103       try {
104          if (panelToEdit instanceof DGroupPanel) {
105             if (panelToEdit.getComponent(0).isEnabled()) {
106                panelToEdit.getComponent(0).requestFocus();
107             } else {
108                panelToEdit.getComponent(1).requestFocus();
109             }
110          }
111
112       } catch (Exception JavaDoc ex) {
113          panelToEdit.requestFocus();
114       }
115
116    }
117
118    private void initDialog () {
119       try {
120          JPanel buttonPanel=new JPanel();
121
122          buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
123          buttonPanel.setAlignmentY(Component.TOP_ALIGNMENT);
124
125          buttonOK=new JButton("OK");
126          URL JavaDoc u = ResourceManager.getResource("OKImage");
127          if (u!=null) {
128             buttonOK.setIcon(new ImageIcon(u));
129          }
130
131          buttonCancel=new JButton("Cancel");
132          u = ResourceManager.getResource("CancelImage");
133          if (u!=null) {
134             buttonCancel.setIcon(new ImageIcon(u));
135          }
136
137          buttonPanel.add(buttonOK);
138          buttonPanel.add(buttonCancel);
139
140          Container cp=getContentPane();
141          cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));
142
143          cp.add(panelToEdit);
144          cp.add(buttonPanel);
145
146          // action listener for confirming
147
buttonOK.addActionListener( new ActionListener(){
148             public void actionPerformed( ActionEvent ae ){
149                if (canApplyChanges()) {
150                   applyChanges();
151                   dispose();
152                }
153             }
154          });
155
156          // action listener for canceling
157
buttonCancel.addActionListener( new ActionListener(){
158             public void actionPerformed( ActionEvent ae ){
159                isCanceled=true;
160                dispose();
161             }
162          });
163
164
165          addWindowListener(wl);
166
167          getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
168             .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Cancel");
169          getRootPane().getActionMap().put("Cancel", new AbstractAction() {
170             public void actionPerformed(ActionEvent e) {
171                isCanceled=true;
172                dispose();
173             }
174          });
175
176       } catch (Exception JavaDoc e) {
177          e.printStackTrace();
178       }
179       setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
180       setResizable(false);
181       setLocationRelativeTo(getParent());
182       buttonOK.setDefaultCapable(true);
183       getRootPane().setDefaultButton(buttonOK);
184
185    }
186
187    public boolean canApplyChanges () {
188       if (panelToEdit.checkRequired()) {
189          if (!panelToEdit.getOwner().setDODSElements(panelToEdit)) {
190             return false;
191          } else {
192             return true;
193          }
194       } else {
195          return false;
196       }
197    }
198
199    public void applyChanges () {
200       panelToEdit.setElements();
201    }
202
203 }
204
205
206
Popular Tags