001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing;
018:
019: import com.l2fprod.common.util.ResourceManager;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dialog;
025: import java.awt.Frame;
026: import java.awt.GraphicsConfiguration;
027: import java.awt.HeadlessException;
028: import java.awt.Window;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.KeyEvent;
031: import java.awt.event.WindowAdapter;
032: import java.awt.event.WindowEvent;
033:
034: import javax.swing.AbstractAction;
035: import javax.swing.Action;
036: import javax.swing.JButton;
037: import javax.swing.JComponent;
038: import javax.swing.JDialog;
039: import javax.swing.JPanel;
040: import javax.swing.KeyStroke;
041: import javax.swing.SwingUtilities;
042:
043: /**
044: * An extension of the <code>JDialog</code> with built-in support for
045: * OK/CANCEL/CLOSE buttons.
046: *
047: * <code>BaseDialog</code> adds:
048: * <ul>
049: * <li>support for ESCAPE key to dispose the window</li>
050: * <li>ok, cancel methods</li>
051: * <li>simple method to check if ok or cancel was called ({@link #ask})
052: * </li>
053: * <li>button pane</li>
054: * <li>banner pane</li>
055: * </ul>
056: */
057: public class BaseDialog extends JDialog {
058:
059: /**
060: * Used to set the mode of the dialog to OK/CANCEL. When in this mode, OK and
061: * Cancel buttons are automatically added to the dialog.
062: */
063: public final static int OK_CANCEL_DIALOG = 0;
064:
065: /**
066: * Used to set the mode of the dialog to OK/CANCEL. When in this mode, a
067: * Close button is automatically added to the dialog.
068: */
069: public final static int CLOSE_DIALOG = 1;
070:
071: private BannerPanel banner;
072: private JPanel contentPane;
073: private JPanel buttonPane;
074: private boolean cancelClicked;
075:
076: private JButton okButton;
077: private JButton cancelOrCloseButton;
078: private int mode;
079:
080: private Action okAction = new AbstractAction() {
081: public void actionPerformed(ActionEvent e) {
082: ok();
083: }
084: };
085:
086: private Action cancelOrCloseAction = new AbstractAction() {
087: public void actionPerformed(ActionEvent e) {
088: cancel();
089: }
090: };
091:
092: public BaseDialog() throws HeadlessException {
093: super ();
094: buildUI();
095: }
096:
097: public BaseDialog(Dialog owner) throws HeadlessException {
098: super (owner);
099: buildUI();
100: }
101:
102: public static BaseDialog newBaseDialog(Component parent) {
103: Window window = parent instanceof Window ? (Window) parent
104: : (Window) SwingUtilities.getAncestorOfClass(
105: Window.class, parent);
106:
107: if (window instanceof Frame) {
108: return new BaseDialog((Frame) window);
109: } else if (window instanceof Dialog) {
110: return new BaseDialog((Dialog) window);
111: } else {
112: return new BaseDialog();
113: }
114: }
115:
116: public BaseDialog(Dialog owner, boolean modal)
117: throws HeadlessException {
118: super (owner, modal);
119: buildUI();
120: }
121:
122: public BaseDialog(Frame owner) throws HeadlessException {
123: super (owner);
124: buildUI();
125: }
126:
127: public BaseDialog(Frame owner, boolean modal)
128: throws HeadlessException {
129: super (owner, modal);
130: buildUI();
131: }
132:
133: public BaseDialog(Dialog owner, String title)
134: throws HeadlessException {
135: super (owner, title);
136: buildUI();
137: }
138:
139: public BaseDialog(Dialog owner, String title, boolean modal)
140: throws HeadlessException {
141: super (owner, title, modal);
142: buildUI();
143: }
144:
145: public BaseDialog(Frame owner, String title)
146: throws HeadlessException {
147: super (owner, title);
148: buildUI();
149: }
150:
151: public BaseDialog(Frame owner, String title, boolean modal)
152: throws HeadlessException {
153: super (owner, title, modal);
154: buildUI();
155: }
156:
157: public BaseDialog(Dialog owner, String title, boolean modal,
158: GraphicsConfiguration gc) throws HeadlessException {
159: super (owner, title, modal, gc);
160: buildUI();
161: }
162:
163: public BaseDialog(Frame owner, String title, boolean modal,
164: GraphicsConfiguration gc) {
165: super (owner, title, modal, gc);
166: buildUI();
167: }
168:
169: /**
170: * Gets the BannerPanel displayed in this dialog. The BannerPanel can be made
171: * invisible by calling <code>getBanner().setVisible(false);</code> if it
172: * is not needed.
173: *
174: * @see BannerPanel
175: */
176: public final BannerPanel getBanner() {
177: return banner;
178: }
179:
180: public final Container getContentPane() {
181: return contentPane;
182: }
183:
184: public final Container getButtonPane() {
185: return buttonPane;
186: }
187:
188: /**
189: * @return true if OK was clicked, false if CANCEL or CLOSE was clicked
190: */
191: public boolean ask() {
192: setVisible(true);
193: dispose();
194: return !cancelClicked;
195: }
196:
197: /**
198: * Called when the user clicks on the OK button.
199: */
200: public void ok() {
201: cancelClicked = false;
202: setVisible(false);
203: }
204:
205: /**
206: * Called when the user clicks on the CANCEL or CLOSE button
207: */
208: public void cancel() {
209: cancelClicked = true;
210: setVisible(false);
211: }
212:
213: /**
214: * Sets the mode of this dialog. Default mode is
215: * {@link BaseDialog#OK_CANCEL_DIALOG}
216: *
217: * @param mode {@link BaseDialog#OK_CANCEL_DIALOG}or
218: * {@link BaseDialog#CLOSE_DIALOG}
219: */
220: public void setDialogMode(int mode) {
221: if (!(mode == OK_CANCEL_DIALOG || mode == CLOSE_DIALOG)) {
222: throw new IllegalArgumentException("invalid dialog mode");
223: }
224:
225: if (okButton != null) {
226: buttonPane.remove(okButton);
227: okButton = null;
228: }
229: if (cancelOrCloseButton != null) {
230: buttonPane.remove(cancelOrCloseButton);
231: cancelOrCloseButton = null;
232: }
233:
234: switch (mode) {
235: case OK_CANCEL_DIALOG:
236: okButton = new JButton(ResourceManager
237: .all(BaseDialog.class).getString("ok"));
238: okButton.addActionListener(okAction);
239: buttonPane.add(okButton);
240: cancelOrCloseButton = new JButton(ResourceManager.all(
241: BaseDialog.class).getString("cancel"));
242: cancelOrCloseButton.addActionListener(cancelOrCloseAction);
243: buttonPane.add(cancelOrCloseButton);
244: getRootPane().setDefaultButton(okButton);
245: break;
246: case CLOSE_DIALOG:
247: cancelOrCloseButton = new JButton(ResourceManager.all(
248: BaseDialog.class).getString("close"));
249: cancelOrCloseButton.addActionListener(cancelOrCloseAction);
250: buttonPane.add(cancelOrCloseButton);
251: break;
252: }
253:
254: this .mode = mode;
255: }
256:
257: /**
258: * Gets this dialog mode
259: *
260: * @return this dialog mode
261: */
262: public int getDialogMode() {
263: return mode;
264: }
265:
266: /**
267: * Centers this dialog on screen.
268: */
269: public void centerOnScreen() {
270: UIUtilities.centerOnScreen(this );
271: }
272:
273: protected String paramString() {
274: return super .paramString()
275: + ",dialogMode="
276: + (mode == OK_CANCEL_DIALOG ? "OK_CANCEL_DIALOG"
277: : "CLOSE_DIALOG");
278: }
279:
280: private void buildUI() {
281: Container container = super .getContentPane();
282: container.setLayout(new BorderLayout(0, 0));
283:
284: banner = new BannerPanel();
285: container.add("North", banner);
286:
287: JPanel contentPaneAndButtons = new JPanel();
288: contentPaneAndButtons.setLayout(LookAndFeelTweaks
289: .createVerticalPercentLayout());
290: contentPaneAndButtons
291: .setBorder(LookAndFeelTweaks.WINDOW_BORDER);
292: container.add("Center", contentPaneAndButtons);
293:
294: contentPane = new JPanel();
295: LookAndFeelTweaks.setBorderLayout(contentPane);
296: LookAndFeelTweaks.setBorder(contentPane);
297: contentPaneAndButtons.add(contentPane, "*");
298:
299: buttonPane = new JPanel();
300: buttonPane
301: .setLayout(LookAndFeelTweaks.createButtonAreaLayout());
302: LookAndFeelTweaks.setBorder(buttonPane);
303: contentPaneAndButtons.add(buttonPane);
304:
305: // layout is done, now everything about "cancel", "escape key" and "click
306: // on close in window bar"
307: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
308:
309: ((JComponent) container).registerKeyboardAction(
310: cancelOrCloseAction, KeyStroke.getKeyStroke(
311: KeyEvent.VK_ESCAPE, 0),
312: JComponent.WHEN_IN_FOCUSED_WINDOW);
313:
314: addWindowListener(new WindowAdapter() {
315: public void windowClosing(WindowEvent e) {
316: cancel();
317: }
318: });
319:
320: // default mode is OK_CANCEL_DIALOG
321: setDialogMode(OK_CANCEL_DIALOG);
322: }
323:
324: }
|