KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > DialogSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.lib2;
21
22 import com.sun.org.apache.bcel.internal.generic.LOOKUPSWITCH;
23 import java.awt.Insets JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.event.*;
26 import java.awt.LayoutManager JavaDoc;
27 import java.awt.BorderLayout JavaDoc;
28 import java.awt.GridLayout JavaDoc;
29 import java.util.Collection JavaDoc;
30 import javax.swing.*;
31 import javax.swing.border.EmptyBorder JavaDoc;
32 import org.netbeans.spi.editor.DialogFactory;
33 import org.openide.util.Lookup;
34
35 /**
36  * DialogSupport is factory based class for creating dialogs of certain
37  * behaviour. It is intended to be used whenever editor needs to popup a dialog.
38  * It presents a way for changing the implementation of the dialog depending
39  * on the enviroment the Editor is embeded in.
40  *
41  * @author pnejedly
42  * @version 1.0
43  */

44 public final class DialogSupport {
45
46     private static DialogSupport instance;
47     
48     private DialogFactory externalFactory;
49     private Lookup.Result<DialogFactory> result;
50
51     public static synchronized DialogSupport getInstance() {
52         if (instance == null) {
53             instance = new DialogSupport();
54         }
55         return instance;
56     }
57     
58     /** Noone needs to instantiate the dialog support */
59     private DialogSupport() {
60         result = Lookup.getDefault().lookup(new Lookup.Template<DialogFactory>(DialogFactory.class));
61     }
62
63     /**
64      * The method for creating a dialog with specified properties.
65      * @param title The title of created dialog.
66      * @param panel The content of the dialog to be displayed.
67      * @param modal Whether the dialog should be modal.
68      * @param buttons The array of JButtons to be added to the dialog.
69      * @param sidebuttons The buttons could be placed under the panel (false),
70      * or on the right side of the panel (true).
71      * @param defaultIndex The index of default button in the buttons array,
72      * if <CODE>index < 0</CODE>, no default button is set.
73      * @param cancelIndex The index about cancel button - the button that will
74      * be <I>pressed</I> when closing the dialog.
75      * @param listener The listener which will be notified of all button
76      * events.
77      * @return newly created <CODE>Dialog</CODE>
78      */

79     public Dialog JavaDoc createDialog(
80         String JavaDoc title, JPanel panel, boolean modal,
81         JButton[] buttons, boolean sidebuttons, int defaultIndex, int cancelIndex,
82         ActionListener listener
83     ) {
84         DialogFactory factory = null;
85         
86         if( externalFactory != null ) {
87             factory = externalFactory;
88         } else {
89             Collection JavaDoc<? extends DialogFactory> factories = result.allInstances();
90             if (factories.isEmpty()) {
91                 factory = new DefaultDialogFactory();
92             } else {
93                 factory = factories.iterator().next();
94             }
95         }
96         
97         return factory.createDialog(title, panel, modal, buttons, sidebuttons,
98                 defaultIndex, cancelIndex, listener );
99     }
100     
101     /** The method for setting custom factory for creating dialogs via
102      * the {@link #createDialog(java.lang.String, javax.swing.JPanel, boolean, javax.swing.JButton[], boolean, int, int, java.awt.event.ActionListener) createDialog} method.
103      * If no factory is set, the {@link DialogSupport.DefaultDialogFactory DefaultDialogFactory}
104      * will be used.
105      *
106      * <p><b>IMPORTANT:</b> This method is here only for supporting the backwards
107      * compatibility of the {@link org.netbeans.editor.DialogSupport} class.
108      *
109      * @param factory the {@link DialogSupport.DialogFactory DialogFactory}
110      * implementation that will be responsible for providing dialogs.
111      */

112     public void setExternalDialogFactory( DialogFactory factory ) {
113         externalFactory = factory;
114     }
115     
116     /** The DialogFactory that will be used to create Dialogs if no other
117      * DialogFactory is set to DialogSupport.
118      */

119     private static class DefaultDialogFactory extends WindowAdapter implements DialogFactory, ActionListener {
120         
121         private JButton cancelButton;
122         
123         /** Create a panel with buttons that will be placed according
124          * to the required alignment */

125         private JPanel createButtonPanel( JButton[] buttons, boolean sidebuttons ) {
126             int count = buttons.length;
127             
128             JPanel outerPanel = new JPanel( new BorderLayout JavaDoc() );
129             outerPanel.setBorder( new EmptyBorder JavaDoc( new Insets JavaDoc(
130                     sidebuttons ? 5 : 0, sidebuttons ? 0 : 5, 5, 5 ) ) );
131
132             LayoutManager JavaDoc lm = new GridLayout JavaDoc( // GridLayout makes equal cells
133
sidebuttons ? count : 1, sidebuttons ? 1 : count, 5, 5 );
134                 
135             JPanel innerPanel = new JPanel( lm );
136             
137             for( int i = 0; i < count; i++ ) innerPanel.add( buttons[i] );
138             
139             outerPanel.add( innerPanel,
140                 sidebuttons ? BorderLayout.NORTH : BorderLayout.EAST ) ;
141             return outerPanel;
142         }
143         
144         public Dialog JavaDoc createDialog( String JavaDoc title, JPanel panel, boolean modal,
145                 JButton[] buttons, boolean sidebuttons, int defaultIndex,
146                 int cancelIndex, ActionListener listener ) {
147
148             // create the dialog with given content
149
JDialog d = new JDialog( (javax.swing.JFrame JavaDoc)null, title, modal );
150             d.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
151             d.getContentPane().add( panel, BorderLayout.CENTER);
152             
153             // Add the buttons to it
154
JPanel buttonPanel = createButtonPanel( buttons, sidebuttons );
155             String JavaDoc buttonAlign = sidebuttons ? BorderLayout.EAST : BorderLayout.SOUTH;
156             d.getContentPane().add( buttonPanel, buttonAlign );
157
158             // add listener to buttons
159
if( listener != null ) {
160                 for( int i = 0; i < buttons.length; i++ ) {
161                     buttons[i].addActionListener( listener );
162                 }
163             }
164
165             // register the default button, if available
166
if( defaultIndex >= 0 ) {
167                 d.getRootPane().setDefaultButton( buttons[defaultIndex] );
168             }
169             
170             // register the cancel button helpers, if available
171
if( cancelIndex >= 0 ) {
172                 cancelButton = buttons[cancelIndex];
173                 // redirect the Esc key to Cancel button
174
d.getRootPane().registerKeyboardAction(
175                     this,
176                     KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true),
177                     JComponent.WHEN_IN_FOCUSED_WINDOW
178                 );
179
180                 // listen on windowClosing and redirect it to Cancel button
181
d.addWindowListener( this );
182             }
183
184             d.pack();
185             return d;
186         }
187         
188         public void actionPerformed(ActionEvent evt) {
189             cancelButton.doClick( 10 );
190         }
191
192         public void windowClosing( WindowEvent evt ) {
193             cancelButton.doClick( 10 );
194         }
195     } // End of DefaultDialogFactory class
196
}
197
Popular Tags