001: package discRack.presentation;
002:
003: import discRack.*;
004: import discRack.presentation.delements.*;
005: import discRack.presentation.dpanels.*;
006: import discRack.business.person.*;
007:
008: import javax.swing.*;
009: import java.awt.*;
010: import java.awt.event.*;
011:
012: import java.util.*;
013: import java.net.URL;
014:
015: /**
016: * The dialog for showing objects derived from {@link DPanel} classes. The
017: * given DPanel object must have it's owner, which type is a class
018: * derived from the {@link DSimpleElement} class.
019: * <p> The dialog enables editing of all editable fields contained
020: * within given panel and after user presses OK button, the new values
021: * contained within edited fields are set to corresponding members of
022: * panel's owner.
023: *
024: * @author Sasa Bojanic
025: * @version 1.0
026: */
027: public class DElementDialog extends JDialog {
028: private DPanel panelToEdit = new DPanel();
029: private JButton buttonOK;
030: private JButton buttonCancel;
031:
032: private WindowListener wl = new WindowAdapter() {
033: public void windowClosing(WindowEvent e) {
034: isCanceled = true;
035: dispose();
036: }
037: };
038:
039: /** Set to true if cancel hasn't been pressed */
040: private boolean isCanceled = false;
041:
042: /**
043: * Constructs a new modal instance which parent is <code>parent</code> and
044: * proceeds a title of dialog.
045: *
046: * @param parent Parent frame.
047: * @param title Dialog title.
048: */
049: public DElementDialog(JFrame parent, String title) {
050: super (parent, title, true);
051: initDialog();
052: }
053:
054: /**
055: * Constructs a new modal instance which parent is <code>parent</code> and
056: * proceeds a title of dialog.
057: *
058: * @param parent Parent dialog.
059: * @param title Dialog title.
060: */
061: public DElementDialog(JDialog parent, String title) {
062: super (parent, title, true);
063: initDialog();
064: }
065:
066: /**
067: * @return <tt>true</tt> if cancel hasn't been pressed,
068: * <tt>false</tt> otherwise.
069: */
070: public boolean isCanceled() {
071: return isCanceled;
072: }
073:
074: /**
075: * Displays dialog and adds specified panel to it, along
076: * with OK and/or Cancel button. The dialog allows user to edit
077: * the panel field(s) that represents corresponding member of
078: * it's owner class. After user presses OK, the content of
079: * panel field(s) is transfered to the corresponding owner
080: * class member.
081: *
082: * @param elementPanel The panel with visual presentation of it's
083: * owner members.
084: */
085: public void editDElement(DPanel elementPanel,
086: boolean hasCancelButton) {
087:
088: Container cp = getContentPane();
089: cp.remove(panelToEdit);
090: panelToEdit = elementPanel;
091: cp.add(panelToEdit, 0);
092: pack();
093:
094: if (hasCancelButton) {
095: buttonCancel.setVisible(true);
096: } else {
097: buttonCancel.setVisible(false);
098: buttonOK.requestFocus();
099: }
100:
101: setLocationRelativeTo(getParent());
102: pack();
103: show();
104: try {
105: if (panelToEdit instanceof DGroupPanel) {
106: if (panelToEdit.getComponent(0).isEnabled()) {
107: panelToEdit.getComponent(0).requestFocus();
108: } else {
109: panelToEdit.getComponent(1).requestFocus();
110: }
111: }
112:
113: } catch (Exception ex) {
114: panelToEdit.requestFocus();
115: }
116:
117: }
118:
119: private void initDialog() {
120: try {
121: JPanel buttonPanel = new JPanel();
122:
123: buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
124: buttonPanel.setAlignmentY(Component.TOP_ALIGNMENT);
125:
126: buttonOK = new JButton("OK");
127: URL u = ResourceManager.getResource("OKImage");
128: if (u != null) {
129: buttonOK.setIcon(new ImageIcon(u));
130: }
131:
132: buttonCancel = new JButton("Cancel");
133: u = ResourceManager.getResource("CancelImage");
134: if (u != null) {
135: buttonCancel.setIcon(new ImageIcon(u));
136: }
137:
138: buttonPanel.add(buttonOK);
139: buttonPanel.add(buttonCancel);
140:
141: Container cp = getContentPane();
142: cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
143:
144: cp.add(panelToEdit);
145: cp.add(buttonPanel);
146:
147: // action listener for confirming
148: buttonOK.addActionListener(new ActionListener() {
149: public void actionPerformed(ActionEvent ae) {
150: if (canApplyChanges()) {
151: applyChanges();
152: dispose();
153: }
154: }
155: });
156:
157: // action listener for canceling
158: buttonCancel.addActionListener(new ActionListener() {
159: public void actionPerformed(ActionEvent ae) {
160: isCanceled = true;
161: dispose();
162: }
163: });
164:
165: addWindowListener(wl);
166:
167: getRootPane()
168: .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
169: .put(
170: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,
171: 0, false), "Cancel");
172: getRootPane().getActionMap().put("Cancel",
173: new AbstractAction() {
174: public void actionPerformed(ActionEvent e) {
175: isCanceled = true;
176: dispose();
177: }
178: });
179:
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
184: setResizable(false);
185: setLocationRelativeTo(getParent());
186: buttonOK.setDefaultCapable(true);
187: getRootPane().setDefaultButton(buttonOK);
188:
189: }
190:
191: public boolean canApplyChanges() {
192: if (panelToEdit.checkRequired()) {
193: if (!panelToEdit.getOwner().setDODSElements(panelToEdit)) {
194: return false;
195: } else {
196: return true;
197: }
198: } else {
199: return false;
200: }
201: }
202:
203: public void applyChanges() {
204: panelToEdit.setElements();
205: }
206:
207: }
|