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