001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.awt.BorderLayout;
017: import java.awt.Dialog;
018: import java.awt.GridLayout;
019: import java.awt.Insets;
020: import java.awt.LayoutManager;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.WindowAdapter;
025: import java.awt.event.WindowEvent;
026:
027: import javax.swing.JButton;
028: import javax.swing.JComponent;
029: import javax.swing.JDialog;
030: import javax.swing.JPanel;
031: import javax.swing.KeyStroke;
032: import javax.swing.WindowConstants;
033: import javax.swing.border.EmptyBorder;
034:
035: /**
036: * DialogSupport is factory based class for creating dialogs of certain
037: * behaviour. It is intended to be used whenever editor needs to popup a dialog.
038: * It presents a way for changing the implementation of the dialog depending on
039: * the enviroment the Editor is embeded in.
040: *
041: * @author pnejedly
042: * @version 1.0
043: */
044: public class DialogSupport {
045:
046: private static DialogFactory factory;
047:
048: /** Noone needs to instantiate the dialog support */
049: private DialogSupport() {
050: }
051:
052: /**
053: * The method for creating a dialog with specified properties.
054: *
055: * @param title
056: * The title of created dialog.
057: * @param panel
058: * The content of the dialog to be displayed.
059: * @param modal
060: * Whether the dialog should be modal.
061: * @param buttons
062: * The array of JButtons to be added to the dialog.
063: * @param sidebuttons
064: * The buttons could be placed under the panel (false), or on the
065: * right side of the panel (true).
066: * @param defaultIndex
067: * The index of default button in the buttons array, if <CODE>index <
068: * 0</CODE>, no default button is set.
069: * @param cancelIndex
070: * The index about cancel button - the button that will be
071: * <I>pressed</I> when closing the dialog.
072: * @param listener
073: * The listener which will be notified of all button events.
074: * @return newly created <CODE>Dialog</CODE>
075: */
076: public static Dialog createDialog(String title, JPanel panel,
077: boolean modal, JButton[] buttons, boolean sidebuttons,
078: int defaultIndex, int cancelIndex, ActionListener listener) {
079: if (factory == null) {
080: factory = new DefaultDialogFactory();
081: }
082: return factory.createDialog(title, panel, modal, buttons,
083: sidebuttons, defaultIndex, cancelIndex, listener);
084: }
085:
086: /**
087: * The method for setting custom factory for creating dialogs via the
088: * {@link #createDialog(java.lang.String, javax.swing.JPanel, boolean, javax.swing.JButton[], boolean, int, int, java.awt.event.ActionListener) createDialog}
089: * method. If no factory is set, the
090: * {@link DialogSupport.DefaultDialogFactory DefaultDialogFactory} will be
091: * used.
092: *
093: * @param factory
094: * the {@link DialogSupport.DialogFactory DialogFactory}
095: * implementation that will be responsible for providing dialogs.
096: *
097: * @see DialogSupport.DialogFactory
098: * @see DialogSupport.DefaultDialogFactory
099: */
100: public static void setDialogFactory(DialogFactory factory) {
101: DialogSupport.factory = factory;
102: }
103:
104: /**
105: * DialogFactory implementation is a class responsible for providing proper
106: * implementation of Dialog containing required widgets. It can provide the
107: * dialog itself or delegate the functionality to another piece of code, e.g
108: * some windowing system.
109: */
110: public static interface DialogFactory {
111:
112: /**
113: * The method for creating a dialog with specified properties.
114: *
115: * @param title
116: * The title of created dialog.
117: * @param panel
118: * The content of the dialog to be displayed.
119: * @param modal
120: * Whether the dialog should be modal.
121: * @param buttons
122: * The array of JButtons to be added to the dialog.
123: * @param sidebuttons
124: * The buttons could be placed under the panel (false), or on
125: * the right side of the panel (true).
126: * @param defaultIndex
127: * The index of default button in the buttons array, if
128: * <CODE>index < 0</CODE>, no default button is set.
129: * @param cancelIndex
130: * The index of cancel button - the button that will be
131: * <I>pressed</I> when closing the dialog.
132: * @param listener
133: * The listener which will be notified of all button events.
134: * @return newly created <CODE>Dialog</CODE>
135: */
136: public Dialog createDialog(String title, JPanel panel,
137: boolean modal, JButton[] buttons, boolean sidebuttons,
138: int defaultIndex, int cancelIndex,
139: ActionListener listener);
140: }
141:
142: /**
143: * The DialogFactory that will be used to create Dialogs if no other
144: * DialogFactory is set to DialogSupport.
145: */
146: private static class DefaultDialogFactory extends WindowAdapter
147: implements DialogFactory, ActionListener {
148:
149: private JButton cancelButton;
150:
151: /**
152: * Create a panel with buttons that will be placed according to the
153: * required alignment
154: */
155: JPanel createButtonPanel(JButton[] buttons, boolean sidebuttons) {
156: int count = buttons.length;
157:
158: JPanel outerPanel = new JPanel(new BorderLayout());
159: outerPanel.setBorder(new EmptyBorder(new Insets(
160: sidebuttons ? 5 : 0, sidebuttons ? 0 : 5, 5, 5)));
161:
162: LayoutManager lm = new GridLayout(
163: // GridLayout makes equal cells
164: sidebuttons ? count : 1, sidebuttons ? 1 : count,
165: 5, 5);
166:
167: JPanel innerPanel = new JPanel(lm);
168:
169: for (int i = 0; i < count; i++)
170: innerPanel.add(buttons[i]);
171:
172: outerPanel.add(innerPanel, sidebuttons ? BorderLayout.NORTH
173: : BorderLayout.EAST);
174: return outerPanel;
175: }
176:
177: public Dialog createDialog(String title, JPanel panel,
178: boolean modal, JButton[] buttons, boolean sidebuttons,
179: int defaultIndex, int cancelIndex,
180: ActionListener listener) {
181:
182: // create the dialog with given content
183: JDialog d = new JDialog((javax.swing.JFrame) null, title,
184: modal);
185: d
186: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
187: d.getContentPane().add(panel, BorderLayout.CENTER);
188:
189: // Add the buttons to it
190: JPanel buttonPanel = createButtonPanel(buttons, sidebuttons);
191: String buttonAlign = sidebuttons ? BorderLayout.EAST
192: : BorderLayout.SOUTH;
193: d.getContentPane().add(buttonPanel, buttonAlign);
194:
195: // add listener to buttons
196: if (listener != null) {
197: for (int i = 0; i < buttons.length; i++) {
198: buttons[i].addActionListener(listener);
199: }
200: }
201:
202: // register the default button, if available
203: if (defaultIndex >= 0) {
204: d.getRootPane().setDefaultButton(buttons[defaultIndex]);
205: }
206:
207: // register the cancel button helpers, if available
208: if (cancelIndex >= 0) {
209: cancelButton = buttons[cancelIndex];
210: // redirect the Esc key to Cancel button
211: d.getRootPane().registerKeyboardAction(
212: this ,
213: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0,
214: true),
215: JComponent.WHEN_IN_FOCUSED_WINDOW);
216:
217: // listen on windowClosing and redirect it to Cancel button
218: d.addWindowListener(this );
219: }
220:
221: d.pack();
222: return d;
223: }
224:
225: public void actionPerformed(ActionEvent evt) {
226: cancelButton.doClick(10);
227: }
228:
229: public void windowClosing(WindowEvent evt) {
230: cancelButton.doClick(10);
231: }
232: }
233:
234: }
|