2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.project.uiapi;
44
import java.awt.Dialog;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.awt.event.WindowAdapter;
48
import java.awt.event.WindowEvent;
49
import java.beans.PropertyChangeEvent;
50
import java.beans.PropertyChangeListener;
51
import java.io.IOException;
52
import java.util.Arrays;
53
import java.util.LinkedList;
54
import java.util.List;
55
import javax.swing.AbstractAction;
56
import javax.swing.Action;
57
import javax.swing.JButton;
58
import javax.swing.JPanel;
59
import org.netbeans.api.project.Project;
60
import org.netbeans.api.project.ProjectManager;
61
import org.netbeans.spi.project.ui.support.ProjectCustomizer;
62
import org.openide.DialogDescriptor;
63
import org.openide.DialogDisplayer;
64
import org.openide.util.Exceptions;
65
import org.openide.util.HelpCtx;
66
import org.openide.util.Lookup;
67
import org.openide.util.Mutex;
68
import org.openide.util.NbBundle;
70
/** Implementation of standard customizer dialog.
72
* @author Petr Hrebejk
74
public class CustomizerDialog {
76
/** Factory class only
78
private CustomizerDialog() {}
81
private static final int OPTION_OK = 0;
82
private static final int OPTION_CANCEL = OPTION_OK + 1;
84
// Option command names
85
private static final String COMMAND_OK = "OK"; // NOI18N
86
private static final String COMMAND_CANCEL = "CANCEL"; // NOI18N
88
public static Dialog createDialog( ActionListener okOptionListener, final CustomizerPane innerPane,
89
HelpCtx helpCtx, final ProjectCustomizer.Category[] categories,
91
ProjectCustomizer.CategoryComponentProvider componentProvider ) {
93
ListeningButton okButton = new ListeningButton(
94
NbBundle.getMessage(CustomizerDialog.class, "LBL_Customizer_Ok_Option"), // NOI18N
96
okButton.setEnabled(CustomizerDialog.checkValidity(categories));
101
new JButton( NbBundle.getMessage( CustomizerDialog.class, "LBL_Customizer_Cancel_Option" ) ) , // NOI18N
105
options[ OPTION_OK ].setActionCommand( COMMAND_OK );
106
options[ OPTION_CANCEL ].setActionCommand( COMMAND_CANCEL );
109
options[ OPTION_OK ].getAccessibleContext().setAccessibleDescription ( NbBundle.getMessage( CustomizerDialog.class, "AD_Customizer_Ok_Option") ); // NOI18N
110
options[ OPTION_CANCEL ].getAccessibleContext().setAccessibleDescription ( NbBundle.getMessage( CustomizerDialog.class, "AD_Customizer_Cancel_Option") ); // NOI18N
114
ActionListener optionsListener = new OptionListener( okOptionListener, categories , componentProvider);
115
options[ OPTION_OK ].addActionListener( optionsListener );
116
options[ OPTION_CANCEL ].addActionListener( optionsListener );
118
innerPane.getAccessibleContext().setAccessibleName( NbBundle.getMessage( CustomizerDialog.class, "AN_ProjectCustomizer") ); //NOI18N
119
innerPane.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage( CustomizerDialog.class, "AD_ProjectCustomizer") ); //NOI18N
121
if ( helpCtx == null ) {
122
helpCtx = HelpCtx.DEFAULT_HELP;
125
DialogDescriptor dialogDescriptor = new DialogDescriptor(
126
innerPane, // innerPane
127
NbBundle.getMessage( CustomizerDialog.class, "LBL_Customizer_Title" ), // NOI18N // displayName
130
options[OPTION_OK], // initial value
131
DialogDescriptor.BOTTOM_ALIGN, // options align
135
innerPane.addPropertyChangeListener( new HelpCtxChangeListener( dialogDescriptor, helpCtx ) );
136
if ( innerPane instanceof HelpCtx.Provider ) {
137
dialogDescriptor.setHelpCtx( ((HelpCtx.Provider)innerPane).getHelpCtx() );
139
dialogDescriptor.setClosingOptions( new Object[] { options[ OPTION_OK ], options[ OPTION_CANCEL ] } );
141
Dialog dialog = DialogDisplayer.getDefault().createDialog( dialogDescriptor );
143
dialog.addWindowListener(new WindowAdapter() {
144
public void windowClosed(WindowEvent e) {
145
innerPane.clearPanelComponentCache();
146
List<ProjectCustomizer.Category> queue = new LinkedList<ProjectCustomizer.Category>(Arrays.asList(categories));
148
while (!queue.isEmpty()) {
149
ProjectCustomizer.Category category = queue.remove(0);
151
Utilities.removeCategoryChangeSupport(category);
153
if (category.getSubcategories() != null) {
154
queue.addAll(Arrays.asList(category.getSubcategories()));
164
/** Returns whether all given categories are valid or not. */
165
private static boolean checkValidity(ProjectCustomizer.Category[] categories) {
166
for (ProjectCustomizer.Category c : categories) {
170
ProjectCustomizer.Category[] subCategories = c.getSubcategories();
171
if (subCategories != null) {
172
if (!checkValidity(subCategories)) {
180
/** Listens to the actions on the Customizer's option buttons */
181
private static class OptionListener implements ActionListener {
183
private ActionListener okOptionListener;
184
private ProjectCustomizer.Category[] categories;
185
private Lookup.Provider prov;
187
OptionListener( ActionListener okOptionListener, ProjectCustomizer.Category[] categs,
188
ProjectCustomizer.CategoryComponentProvider componentProvider) {
189
this.okOptionListener = okOptionListener;
192
if (componentProvider instanceof Lookup.Provider) {
193
prov = (Lookup.Provider)componentProvider;
197
public void actionPerformed( final ActionEvent e ) {
198
String command = e.getActionCommand();
200
if ( COMMAND_OK.equals( command ) ) {
201
// Call the OK option listener
202
ProjectManager.mutex().writeAccess(new Mutex.Action<Object>() {
203
public Object run() {
204
okOptionListener.actionPerformed( e ); // XXX maybe create new event
205
actionPerformed(e, categories);
208
Project prj = prov.getLookup().lookup(Project.class);
209
if (ProjectManager.getDefault().isModified(prj)) {
211
ProjectManager.getDefault().saveProject(prj);
212
} catch (IOException ex) {
213
Exceptions.printStackTrace(ex);
214
} catch (IllegalArgumentException ex) {
215
Exceptions.printStackTrace(ex);
225
private void actionPerformed(ActionEvent e, ProjectCustomizer.Category[] categs) {
226
for (int i = 0; i < categs.length; i++) {
227
ActionListener list = categs[i].getOkButtonListener();
229
list.actionPerformed(e);// XXX maybe create new event
231
if (categs[i].getSubcategories() != null) {
232
actionPerformed(e, categs[i].getSubcategories());
239
private static class HelpCtxChangeListener implements PropertyChangeListener {
241
DialogDescriptor dialogDescriptor;
242
HelpCtx defaultHelpCtx;
244
HelpCtxChangeListener( DialogDescriptor dialogDescriptor, HelpCtx defaultHelpCtx ) {
245
this.dialogDescriptor = dialogDescriptor;
246
this.defaultHelpCtx = defaultHelpCtx;
249
public void propertyChange( PropertyChangeEvent evt ) {
251
if ( CustomizerPane.HELP_CTX_PROPERTY.equals( evt.getPropertyName() ) ) {
252
HelpCtx newHelp = (HelpCtx)evt.getNewValue();
253
dialogDescriptor.setHelpCtx( newHelp == null || newHelp == HelpCtx.DEFAULT_HELP ? defaultHelpCtx : newHelp );
260
private static class ListeningButton extends JButton implements PropertyChangeListener {
262
private ProjectCustomizer.Category[] categories;
264
public ListeningButton(String label, ProjectCustomizer.Category[] categories) {
266
this.categories = categories;
267
for (ProjectCustomizer.Category c : categories) {
268
Utilities.getCategoryChangeSupport(c).addPropertyChangeListener(this);
273
public void propertyChange(PropertyChangeEvent evt) {
274
if (evt.getPropertyName() == CategoryChangeSupport.VALID_PROPERTY) {
275
boolean valid = (Boolean) evt.getNewValue();
276
// enable only if all categories are valid
277
setEnabled(valid && CustomizerDialog.checkValidity(categories));