KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > impl > NbDialogFactory


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.impl;
21
22 import java.awt.event.*;
23 import java.awt.Dialog JavaDoc;
24 import javax.swing.JButton JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JDialog JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.KeyStroke JavaDoc;
29 import org.openide.DialogDescriptor;
30 import org.openide.util.HelpCtx;
31 import java.util.HashMap JavaDoc;
32 import org.netbeans.spi.editor.DialogFactory;
33
34 /**
35  *
36  * @author Vita Stejskal
37  */

38 public class NbDialogFactory implements DialogFactory {
39
40     /**
41      * Hash map containing string (ClassNames) <-> string (HelpID).
42      */

43     private static HashMap JavaDoc helpIDs;
44     
45     private static final String JavaDoc HELP_ID_MacroSavePanel = "editing.macros.recording"; // !!! NOI18N
46
private static final String JavaDoc HELP_ID_FindPanel = "editing.find"; // !!! NOI18N
47
private static final String JavaDoc HELP_ID_JavaFastImportPanel = "editing.fastimport"; // !!! NOI18N
48
private static final String JavaDoc HELP_ID_ScrollCompletionPane = "editing.codecompletion"; // !!! NOI18N
49

50     public NbDialogFactory()
51     {
52         if (helpIDs == null)
53         {
54             helpIDs = new HashMap JavaDoc(7);
55             helpIDs.put("org.netbeans.editor.MacroSavePanel", HELP_ID_MacroSavePanel); // NOI18N
56
helpIDs.put("org.netbeans.editor.ext.FindDialogSupport$FindPanel", HELP_ID_FindPanel); // NOI18N
57
helpIDs.put("org.netbeans.editor.ext.ScrollCompletionPane", HELP_ID_ScrollCompletionPane); // NOI18N
58
helpIDs.put("org.netbeans.editor.ext.java.JavaFastImportPanel", HELP_ID_JavaFastImportPanel); // NOI18N
59
}
60     }
61     
62     /**
63      * The method for creating a dialog with specified properties.
64      * @param title The title of created dialog.
65      * @param panel The content of the dialog to be displayed.
66      * @param modal Whether the dialog should be modal.
67      * @param buttons The array of JButtons to be added to the dialog.
68      * @param sideButtons The buttons could be placed under the panel (false),
69      * or on the right side of the panel (true).
70      * @param defaultIndex The index of default button in the buttons array,
71      * if <CODE>index < 0</CODE>, no default button is set.
72      * @param cancelIndex The index of cancel button - the button that will
73      * be <I>pressed</I> when closing the dialog.\
74      * @param listener The listener which will be notified of all button
75      * events.
76      */

77     public Dialog JavaDoc createDialog(String JavaDoc title, JPanel JavaDoc panel,boolean modal,JButton JavaDoc[] buttons,boolean sideButtons,int defaultIndex,int cancelIndex,ActionListener listener) {
78         String JavaDoc helpID = (String JavaDoc)helpIDs.get(panel.getClass().getName());
79         Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(
80                 new DialogDescriptor( panel, title, modal, buttons,
81                 defaultIndex == -1 ? buttons[0] : buttons[defaultIndex],
82                     sideButtons ? DialogDescriptor.RIGHT_ALIGN : DialogDescriptor.BOTTOM_ALIGN,
83                     helpID != null ? new HelpCtx( helpID ) : null, listener
84                 )
85         );
86
87         // register the cancel button helpers
88
if( cancelIndex >= 0 && d instanceof JDialog JavaDoc ) {
89             final JButton JavaDoc cancelButton = buttons[cancelIndex];
90             // register the Esc key to simulate Cancel click
91
((JDialog JavaDoc)d).getRootPane().registerKeyboardAction(
92                 new ActionListener() {
93                     public void actionPerformed(ActionEvent evt) { // l.actionPerformed( new ActionEvent(buttons[cancelButtonIndex], 0, null));
94
cancelButton.doClick( 10 );
95                     }
96                 },
97                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true),
98                 //JComponent.WHEN_IN_FOCUSED_WINDOW
99
JComponent.WHEN_FOCUSED
100             );
101
102             //bugfix of #45552, 45555, 45556, 45558
103
((JDialog JavaDoc)d).getRootPane().setFocusable(false);
104                 
105             d.addWindowListener(
106                 new WindowAdapter() {
107                     public void windowClosing( WindowEvent evt ) {
108                         cancelButton.doClick( 10 );
109                     }
110                 }
111             );
112         }
113                     
114         return d;
115     }
116     
117 }
118
Popular Tags