001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.lib2;
043:
044: import java.awt.Insets;
045: import java.awt.Dialog;
046: import java.awt.event.*;
047: import java.awt.LayoutManager;
048: import java.awt.BorderLayout;
049: import java.awt.GridLayout;
050: import java.util.Collection;
051: import javax.swing.*;
052: import javax.swing.border.EmptyBorder;
053: import org.netbeans.spi.editor.DialogFactory;
054: import org.openide.util.Lookup;
055:
056: /**
057: * DialogSupport is factory based class for creating dialogs of certain
058: * behaviour. It is intended to be used whenever editor needs to popup a dialog.
059: * It presents a way for changing the implementation of the dialog depending
060: * on the enviroment the Editor is embeded in.
061: *
062: * @author pnejedly
063: * @version 1.0
064: */
065: public final class DialogSupport {
066:
067: private static DialogSupport instance;
068:
069: private DialogFactory externalFactory;
070: private Lookup.Result<DialogFactory> result;
071:
072: public static synchronized DialogSupport getInstance() {
073: if (instance == null) {
074: instance = new DialogSupport();
075: }
076: return instance;
077: }
078:
079: /** Noone needs to instantiate the dialog support */
080: private DialogSupport() {
081: result = Lookup.getDefault()
082: .lookup(
083: new Lookup.Template<DialogFactory>(
084: DialogFactory.class));
085: }
086:
087: /**
088: * The method for creating a dialog with specified properties.
089: * @param title The title of created dialog.
090: * @param panel The content of the dialog to be displayed.
091: * @param modal Whether the dialog should be modal.
092: * @param buttons The array of JButtons to be added to the dialog.
093: * @param sidebuttons The buttons could be placed under the panel (false),
094: * or on the right side of the panel (true).
095: * @param defaultIndex The index of default button in the buttons array,
096: * if <CODE>index < 0</CODE>, no default button is set.
097: * @param cancelIndex The index about cancel button - the button that will
098: * be <I>pressed</I> when closing the dialog.
099: * @param listener The listener which will be notified of all button
100: * events.
101: * @return newly created <CODE>Dialog</CODE>
102: */
103: public Dialog createDialog(String title, JPanel panel,
104: boolean modal, JButton[] buttons, boolean sidebuttons,
105: int defaultIndex, int cancelIndex, ActionListener listener) {
106: DialogFactory factory = null;
107:
108: if (externalFactory != null) {
109: factory = externalFactory;
110: } else {
111: Collection<? extends DialogFactory> factories = result
112: .allInstances();
113: if (factories.isEmpty()) {
114: factory = new DefaultDialogFactory();
115: } else {
116: factory = factories.iterator().next();
117: }
118: }
119:
120: return factory.createDialog(title, panel, modal, buttons,
121: sidebuttons, defaultIndex, cancelIndex, listener);
122: }
123:
124: /** The method for setting custom factory for creating dialogs via
125: * the {@link #createDialog(java.lang.String, javax.swing.JPanel, boolean, javax.swing.JButton[], boolean, int, int, java.awt.event.ActionListener) createDialog} method.
126: * If no factory is set, the {@link DialogSupport.DefaultDialogFactory DefaultDialogFactory}
127: * will be used.
128: *
129: * <p><b>IMPORTANT:</b> This method is here only for supporting the backwards
130: * compatibility of the {@link org.netbeans.editor.DialogSupport} class.
131: *
132: * @param factory the {@link DialogSupport.DialogFactory DialogFactory}
133: * implementation that will be responsible for providing dialogs.
134: */
135: public void setExternalDialogFactory(DialogFactory factory) {
136: externalFactory = factory;
137: }
138:
139: /** The DialogFactory that will be used to create Dialogs if no other
140: * DialogFactory is set to DialogSupport.
141: */
142: private static class DefaultDialogFactory extends WindowAdapter
143: implements DialogFactory, ActionListener {
144:
145: private JButton cancelButton;
146:
147: /** Create a panel with buttons that will be placed according
148: * to the required alignment */
149: private JPanel createButtonPanel(JButton[] buttons,
150: boolean sidebuttons) {
151: int count = buttons.length;
152:
153: JPanel outerPanel = new JPanel(new BorderLayout());
154: outerPanel.setBorder(new EmptyBorder(new Insets(
155: sidebuttons ? 5 : 0, sidebuttons ? 0 : 5, 5, 5)));
156:
157: LayoutManager lm = new GridLayout(
158: // GridLayout makes equal cells
159: sidebuttons ? count : 1, sidebuttons ? 1 : count,
160: 5, 5);
161:
162: JPanel innerPanel = new JPanel(lm);
163:
164: for (int i = 0; i < count; i++)
165: innerPanel.add(buttons[i]);
166:
167: outerPanel.add(innerPanel, sidebuttons ? BorderLayout.NORTH
168: : BorderLayout.EAST);
169: return outerPanel;
170: }
171:
172: public Dialog createDialog(String title, JPanel panel,
173: boolean modal, JButton[] buttons, boolean sidebuttons,
174: int defaultIndex, int cancelIndex,
175: ActionListener listener) {
176:
177: // create the dialog with given content
178: JDialog d = new JDialog((javax.swing.JFrame) null, title,
179: modal);
180: d
181: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
182: d.getContentPane().add(panel, BorderLayout.CENTER);
183:
184: // Add the buttons to it
185: JPanel buttonPanel = createButtonPanel(buttons, sidebuttons);
186: String buttonAlign = sidebuttons ? BorderLayout.EAST
187: : BorderLayout.SOUTH;
188: d.getContentPane().add(buttonPanel, buttonAlign);
189:
190: // add listener to buttons
191: if (listener != null) {
192: for (int i = 0; i < buttons.length; i++) {
193: buttons[i].addActionListener(listener);
194: }
195: }
196:
197: // register the default button, if available
198: if (defaultIndex >= 0) {
199: d.getRootPane().setDefaultButton(buttons[defaultIndex]);
200: }
201:
202: // register the cancel button helpers, if available
203: if (cancelIndex >= 0) {
204: cancelButton = buttons[cancelIndex];
205: // redirect the Esc key to Cancel button
206: d.getRootPane().registerKeyboardAction(
207: this ,
208: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0,
209: true),
210: JComponent.WHEN_IN_FOCUSED_WINDOW);
211:
212: // listen on windowClosing and redirect it to Cancel button
213: d.addWindowListener(this );
214: }
215:
216: d.pack();
217: return d;
218: }
219:
220: public void actionPerformed(ActionEvent evt) {
221: cancelButton.doClick(10);
222: }
223:
224: public void windowClosing(WindowEvent evt) {
225: cancelButton.doClick(10);
226: }
227: } // End of DefaultDialogFactory class
228: }
|