KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > uiapi > CustomizerDialog


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.project.uiapi;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.WindowAdapter JavaDoc;
26 import java.awt.event.WindowEvent JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.swing.AbstractAction JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import org.netbeans.api.project.ProjectManager;
37 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
38 import org.openide.DialogDescriptor;
39 import org.openide.DialogDisplayer;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.Mutex;
42 import org.openide.util.NbBundle;
43
44 /** Implementation of standard customizer dialog.
45  *
46  * @author Petr Hrebejk
47  */

48 public class CustomizerDialog {
49
50     /** Factory class only
51      */

52     private CustomizerDialog() {}
53
54     // Option indexes
55
private static final int OPTION_OK = 0;
56     private static final int OPTION_CANCEL = OPTION_OK + 1;
57
58     // Option command names
59
private static final String JavaDoc COMMAND_OK = "OK"; // NOI18N
60
private static final String JavaDoc COMMAND_CANCEL = "CANCEL"; // NOI18N
61

62     public static Dialog JavaDoc createDialog( ActionListener JavaDoc okOptionListener, final CustomizerPane innerPane,
63             HelpCtx helpCtx, final ProjectCustomizer.Category[] categories ) {
64
65         ListeningButton okButton = new ListeningButton(
66                 NbBundle.getMessage(CustomizerDialog.class, "LBL_Customizer_Ok_Option"), // NOI18N
67
categories);
68         okButton.setEnabled(CustomizerDialog.checkValidity(categories));
69
70         // Create options
71
JButton JavaDoc options[] = {
72             okButton,
73             new JButton JavaDoc( NbBundle.getMessage( CustomizerDialog.class, "LBL_Customizer_Cancel_Option" ) ) , // NOI18N
74
};
75
76         // Set commands
77
options[ OPTION_OK ].setActionCommand( COMMAND_OK );
78         options[ OPTION_CANCEL ].setActionCommand( COMMAND_CANCEL );
79
80         //A11Y
81
options[ OPTION_OK ].getAccessibleContext().setAccessibleDescription ( NbBundle.getMessage( CustomizerDialog.class, "AD_Customizer_Ok_Option") ); // NOI18N
82
options[ OPTION_CANCEL ].getAccessibleContext().setAccessibleDescription ( NbBundle.getMessage( CustomizerDialog.class, "AD_Customizer_Cancel_Option") ); // NOI18N
83

84
85         // RegisterListener
86
ActionListener JavaDoc optionsListener = new OptionListener( okOptionListener, categories );
87         options[ OPTION_OK ].addActionListener( optionsListener );
88         options[ OPTION_CANCEL ].addActionListener( optionsListener );
89
90         innerPane.getAccessibleContext().setAccessibleName( NbBundle.getMessage( CustomizerDialog.class, "AN_ProjectCustomizer") ); //NOI18N
91
innerPane.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage( CustomizerDialog.class, "AD_ProjectCustomizer") ); //NOI18N
92

93         if ( helpCtx == null ) {
94             helpCtx = HelpCtx.DEFAULT_HELP;
95         }
96
97         DialogDescriptor dialogDescriptor = new DialogDescriptor(
98             innerPane, // innerPane
99
NbBundle.getMessage( CustomizerDialog.class, "LBL_Customizer_Title" ), // NOI18N // displayName
100
false, // modal
101
options, // options
102
options[OPTION_OK], // initial value
103
DialogDescriptor.BOTTOM_ALIGN, // options align
104
helpCtx, // helpCtx
105
null ); // listener
106

107         innerPane.addPropertyChangeListener( new HelpCtxChangeListener( dialogDescriptor, helpCtx ) );
108         if ( innerPane instanceof HelpCtx.Provider ) {
109             dialogDescriptor.setHelpCtx( ((HelpCtx.Provider)innerPane).getHelpCtx() );
110         }
111         dialogDescriptor.setClosingOptions( new Object JavaDoc[] { options[ OPTION_OK ], options[ OPTION_CANCEL ] } );
112
113         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
114
115         dialog.addWindowListener(new WindowAdapter JavaDoc() {
116             public void windowClosed(WindowEvent JavaDoc e) {
117                 innerPane.clearPanelComponentCache();
118                 List JavaDoc<ProjectCustomizer.Category> queue = new LinkedList JavaDoc<ProjectCustomizer.Category>(Arrays.asList(categories));
119
120                 while (!queue.isEmpty()) {
121                     ProjectCustomizer.Category category = queue.remove(0);
122
123                     Utilities.removeCategoryChangeSupport(category);
124
125                     if (category.getSubcategories() != null) {
126                         queue.addAll(Arrays.asList(category.getSubcategories()));
127                     }
128                 }
129             }
130         });
131
132         return dialog;
133
134     }
135
136     /** Returns whether all given categories are valid or not. */
137     private static boolean checkValidity(ProjectCustomizer.Category[] categories) {
138         for (ProjectCustomizer.Category c : categories) {
139             if (!c.isValid()) {
140                 return false;
141             }
142             ProjectCustomizer.Category[] subCategories = c.getSubcategories();
143             if (subCategories != null) {
144                 if (!checkValidity(subCategories)) {
145                     return false;
146                 }
147             }
148         }
149         return true;
150     }
151
152     /** Listens to the actions on the Customizer's option buttons */
153     private static class OptionListener implements ActionListener JavaDoc {
154
155         private ActionListener JavaDoc okOptionListener;
156         private ProjectCustomizer.Category[] categories;
157
158         OptionListener( ActionListener JavaDoc okOptionListener, ProjectCustomizer.Category[] categs) {
159             this.okOptionListener = okOptionListener;
160             categories = categs;
161         }
162         
163         public void actionPerformed( final ActionEvent JavaDoc e ) {
164             String JavaDoc command = e.getActionCommand();
165             
166             if ( COMMAND_OK.equals( command ) ) {
167                 // Call the OK option listener
168
ProjectManager.mutex().writeAccess(new Mutex.Action<Object JavaDoc>() {
169                     public Object JavaDoc run() {
170                         okOptionListener.actionPerformed( e ); // XXX maybe create new event
171
actionPerformed(e, categories);
172                         return null;
173                     }
174                 });
175             }
176         }
177         
178         private void actionPerformed(ActionEvent JavaDoc e, ProjectCustomizer.Category[] categs) {
179             for (int i = 0; i < categs.length; i++) {
180                 ActionListener JavaDoc list = categs[i].getOkButtonListener();
181                 if (list != null) {
182                     list.actionPerformed(e);// XXX maybe create new event
183
}
184                 if (categs[i].getSubcategories() != null) {
185                     actionPerformed(e, categs[i].getSubcategories());
186                 }
187             }
188         }
189
190     }
191
192     private static class HelpCtxChangeListener implements PropertyChangeListener JavaDoc {
193
194         DialogDescriptor dialogDescriptor;
195         HelpCtx defaultHelpCtx;
196
197         HelpCtxChangeListener( DialogDescriptor dialogDescriptor, HelpCtx defaultHelpCtx ) {
198             this.dialogDescriptor = dialogDescriptor;
199             this.defaultHelpCtx = defaultHelpCtx;
200         }
201
202         public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
203
204             if ( CustomizerPane.HELP_CTX_PROPERTY.equals( evt.getPropertyName() ) ) {
205                 HelpCtx newHelp = (HelpCtx)evt.getNewValue();
206                 dialogDescriptor.setHelpCtx( newHelp == null || newHelp == HelpCtx.DEFAULT_HELP ? defaultHelpCtx : newHelp );
207             }
208
209         }
210
211     }
212
213     private static class ListeningButton extends JButton JavaDoc implements PropertyChangeListener JavaDoc {
214
215         private ProjectCustomizer.Category[] categories;
216
217         public ListeningButton(String JavaDoc label, ProjectCustomizer.Category[] categories) {
218             super(label);
219             this.categories = categories;
220             for (ProjectCustomizer.Category c : categories) {
221                 Utilities.getCategoryChangeSupport(c).addPropertyChangeListener(this);
222             }
223
224         }
225
226         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
227             if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) {
228                 boolean valid = (Boolean JavaDoc) evt.getNewValue();
229                 // enable only if all categories are valid
230
setEnabled(valid && CustomizerDialog.checkValidity(categories));
231             }
232         }
233
234     }
235
236 }
237
Popular Tags