KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > BasicCustomizer


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.apisupport.project.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.WindowAdapter JavaDoc;
27 import java.awt.event.WindowEvent JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.io.IOException JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.spi.project.ui.CustomizerProvider;
35 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
36 import org.openide.ErrorManager;
37 import org.openide.util.Lookup;
38 import org.openide.util.Mutex;
39 import org.openide.util.MutexException;
40 import org.openide.util.NbBundle;
41 import org.openide.util.lookup.Lookups;
42 import org.openide.util.lookup.ProxyLookup;
43
44 /**
45  * Convenient class to be used by {@link CustomizerProvider} implementations.
46  *
47  * @author Martin Krauskopf
48  */

49 abstract class BasicCustomizer implements CustomizerProvider, PropertyChangeListener JavaDoc {
50     
51     static final String JavaDoc LAST_SELECTED_PANEL = "lastSelectedPanel"; // NOI18N
52

53     /** Project <code>this</code> customizer customizes. */
54     private final Project project;
55     
56     /** Keeps reference to a dialog representing <code>this</code> customizer. */
57     private Dialog JavaDoc dialog;
58     
59     private Component JavaDoc lastSelectedPanel;
60     
61     
62     private String JavaDoc layerPath;
63     
64     protected BasicCustomizer(final Project project, String JavaDoc path) {
65         this.project = project;
66         layerPath = path;
67     }
68     
69     /**
70      * All changes should be store at this point. Is called under the write
71      * access from {@link ProjectManager#mutex}.
72      */

73     abstract void storeProperties() throws IOException JavaDoc;
74     
75     /**
76      * Gives a chance to do some work after all the changes in a customizer
77      * were successfully saved. Is called under the write access from {@link
78      * ProjectManager#mutex}.
79      */

80     abstract void postSave() throws IOException JavaDoc;
81     
82     /**
83      * Be sure that you will prepare all the data (typically subclass of {@link
84      * ModuleProperties}) needed by a customizer and its panels and that the
85      * data is always up-to-date after this method was called.
86      */

87     abstract Lookup prepareData();
88     
89     abstract void dialogCleanup();
90     
91     
92     protected Project getProject() {
93         return project;
94     }
95     
96     /** Show customizer with the first category selected. */
97     public void showCustomizer() {
98         showCustomizer(null);
99     }
100     
101     /** Show customizer with preselected category. */
102     public void showCustomizer(String JavaDoc preselectedCategory) {
103         showCustomizer(preselectedCategory, null);
104     }
105     
106     public void showCustomizer(String JavaDoc preselectedCategory, String JavaDoc preselectedSubCategory) {
107         if (dialog != null) {
108             dialog.setVisible(true);
109             return;
110         } else {
111             Lookup context = prepareData();
112 // if (preselectedCategory == null) {
113
// preselectedCategory = findLastSelectedCategory();
114
// }
115
context = new ProxyLookup(context, Lookups.fixed(new SubCategoryProvider(preselectedCategory, preselectedSubCategory)));
116             OptionListener listener = new OptionListener();
117             dialog = ProjectCustomizer.createCustomizerDialog(layerPath, context,
118                     preselectedCategory, listener,
119                     null);
120             dialog.addWindowListener(listener);
121             dialog.setTitle(NbBundle.getMessage(getClass(), "LBL_CustomizerTitle",
122                     ProjectUtils.getInformation(getProject()).getDisplayName()));
123             dialog.setVisible(true);
124         }
125         // this is thrash, replace either by checks at panel creation or category creation.
126
// // check panels validity - gives them a chance to set an error message or a warning
127
// for (Iterator it = panels.values().iterator(); it.hasNext();) {
128
// NbPropertyPanel panel = (NbPropertyPanel) it.next();
129
// panel.checkForm();
130
// }
131
}
132     
133  
134 //TODO this is for selecting last active panel only..
135
// protected void listenToPanels() {
136
// for (Iterator it = panels.values().iterator(); it.hasNext(); ) {
137
// ((Component) it.next()).addPropertyChangeListener(this);
138
// }
139
// }
140

141     
142     public final void save() {
143         try {
144             ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
145                 public Object JavaDoc run() throws IOException JavaDoc {
146                     storeProperties();
147                     ProjectManager.getDefault().saveProject(project);
148                     return null;
149                 }
150             });
151         } catch (MutexException e) {
152             ErrorManager.getDefault().notify((IOException JavaDoc)e.getException());
153         }
154     }
155     
156     /** Listens to the actions on the Customizer's option buttons */
157     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
158         String JavaDoc propertyName = evt.getPropertyName();
159         if (propertyName == BasicCustomizer.LAST_SELECTED_PANEL) {
160             lastSelectedPanel = (Component JavaDoc) evt.getSource();
161         }
162     }
163     
164 // private String findLastSelectedCategory() {
165
// String preselectedCategory = null;
166
// for (Iterator it = panels.entrySet().iterator(); it.hasNext(); ) {
167
// Map.Entry entry = (Map.Entry) it.next();
168
// Component panel = (Component) entry.getValue();
169
// if (panel == lastSelectedPanel) {
170
// preselectedCategory = ((ProjectCustomizer.Category) entry.getKey()).getName();
171
// break;
172
// }
173
// }
174
// return preselectedCategory;
175
// }
176

177     protected class OptionListener extends WindowAdapter JavaDoc implements ActionListener JavaDoc {
178         
179         // Listening to OK button ----------------------------------------------
180
public void actionPerformed(ActionEvent JavaDoc e) {
181             save();
182         }
183         
184         // remove dialog for this customizer's project
185
public void windowClosed(WindowEvent JavaDoc e) {
186             doClose();
187         }
188         
189         public void windowClosing(WindowEvent JavaDoc e) {
190             // Dispose the dialog otherwise the
191
// {@link WindowAdapter#windowClosed} may not be called
192
doClose();
193         }
194         
195         public void doClose() {
196             if (dialog != null) {
197                 dialog.removeWindowListener(this);
198                 dialog.setVisible(false);
199                 dialog.dispose();
200                 dialogCleanup();
201             }
202             dialog = null;
203         }
204         
205     }
206     
207
208     
209     static final class SubCategoryProvider {
210
211         private String JavaDoc subcategory;
212
213         private String JavaDoc category;
214
215         SubCategoryProvider(String JavaDoc category, String JavaDoc subcategory) {
216             this.category = category;
217             this.subcategory = subcategory;
218         }
219         public String JavaDoc getCategory() {
220             return category;
221         }
222         public String JavaDoc getSubcategory() {
223             return subcategory;
224         }
225     }
226     
227 }
228
229
Popular Tags