KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalogsupport > ui > customizer > CustomizerProviderImpl


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.xml.catalogsupport.ui.customizer;
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.text.MessageFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35
36 import org.openide.util.NbBundle;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.ProjectUtils;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.spi.project.support.ant.ReferenceHelper;
41 import org.netbeans.spi.project.ui.CustomizerProvider;
42 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
43
44
45 /**
46  * Customization of project to support XML
47  *
48  * @author Ajit Bhate
49  */

50 public class CustomizerProviderImpl implements CustomizerProvider {
51     
52     private final Project project;
53     private final AntProjectHelper antProjectHelper;
54     private final ReferenceHelper refHelper;
55     
56     private List JavaDoc<ProjectCustomizer.Category> categories;
57     private PanelProvider panelProvider;
58     
59     // Option indexes
60
private static final int OPTION_OK = 0;
61     private static final int OPTION_CANCEL = OPTION_OK + 1;
62     
63     // Option command names
64
private static final String JavaDoc COMMAND_OK = "OK"; // NOI18N
65
private static final String JavaDoc COMMAND_CANCEL = "CANCEL"; // NOI18N
66

67     private static Map JavaDoc<Project,Dialog JavaDoc> project2Dialog = new HashMap JavaDoc<Project,Dialog JavaDoc>();
68     
69     // Names of categories
70
private static final String JavaDoc REFERENCE = "Project References";
71     private static final String JavaDoc CATALOG = "XML Catalog";
72     
73     public CustomizerProviderImpl(Project project, AntProjectHelper antProjectHelper, ReferenceHelper refHelper) {
74         this.project = project;
75         this.refHelper = refHelper;
76         this.antProjectHelper = antProjectHelper;
77     }
78             
79     public void showCustomizer() {
80         showCustomizer(null);
81     }
82     
83     
84     public void showCustomizer (String JavaDoc preselectedCategory) {
85         showCustomizer (preselectedCategory, null);
86     }
87     
88     public void showCustomizer(String JavaDoc preselectedCategory, String JavaDoc preselectedSubCategory) {
89         
90         Dialog JavaDoc dialog = project2Dialog.get(getProject());
91         if (dialog != null) {
92             dialog.setVisible(true);
93             return;
94         }
95         else {
96             init();
97             OptionListener listener = new OptionListener(getProject());
98
99             List JavaDoc<ProjectCustomizer.Category> listCategories = getCategories();
100             if (preselectedCategory != null && preselectedSubCategory != null) {
101                 for (ProjectCustomizer.Category category:listCategories) {
102                     if (preselectedCategory.equals(category.getName())) {
103                         JComponent JavaDoc component = panelProvider.create (category);
104                         if (component instanceof SubCategoryProvider) {
105                             ((SubCategoryProvider)component).showSubCategory(preselectedSubCategory);
106                         }
107                         break;
108                     }
109                 }
110             }
111             dialog = ProjectCustomizer.createCustomizerDialog(listCategories.toArray(
112                     new ProjectCustomizer.Category[listCategories.size()]),
113                     panelProvider, preselectedCategory, listener, null);
114             dialog.addWindowListener(new OptionListener(getProject()));
115             dialog.setTitle(NbBundle.getMessage(
116                     CustomizerProviderImpl.class, "LBL_Customizer_Title", // NOI18N
117
ProjectUtils.getInformation(getProject()).getDisplayName()));
118
119             project2Dialog.put(getProject(), dialog);
120             dialog.setVisible(true);
121         }
122     }
123     
124     /**
125      * This api is called when showCustomizer is invoked and if customizer
126      * provider is not fully initialized.
127      * It initializes the categories list and the panel provider (CategoryComponentProvider)
128      */

129     protected void init() {
130         categories = new ArrayList JavaDoc<ProjectCustomizer.Category>();
131         panelProvider = new PanelProvider(createCategoriesMap());
132     }
133     
134     /**
135      * Getter for project
136      */

137     protected Project getProject() {
138         return project;
139     }
140
141     /**
142      * Getter for antProjectHelper
143      */

144     protected AntProjectHelper getAntProjectHelper() {
145         return antProjectHelper;
146     }
147
148     /**
149      * Getter for refHelper
150      */

151     protected ReferenceHelper getRefHelper() {
152         return refHelper;
153     }
154
155     /**
156      * Getter for categories
157      */

158     protected List JavaDoc<ProjectCustomizer.Category> getCategories() {
159         return categories;
160     }
161
162     /**
163      * This api is called when showCustomizer is invoked and if customizer
164      * provider is not fully initialized.
165      * The default implementation creates Project Reference and XML Catalog Categories.
166      * Subclasses can override this and may or may not call super depending upon,
167      * the categories are desired in project customizer.
168      */

169     protected Map JavaDoc<ProjectCustomizer.Category,JComponent JavaDoc> createCategoriesMap() {
170         ProjectCustomizer.Category references = ProjectCustomizer.Category.create(
171                 REFERENCE,
172                 NbBundle.getMessage(CustomizerProviderImpl.class,
173                 "LBL_Customizer_Category_ProjectReferences"),
174                 null,
175                 null);
176         
177         ProjectCustomizer.Category catalog = ProjectCustomizer.Category.create(
178                 CATALOG,
179                 NbBundle.getMessage(CustomizerProviderImpl.class,
180                 "LBL_Customizer_Category_XMLCatalog"),
181                 null,
182                 null);
183         
184         getCategories().add(references);
185         getCategories().add(catalog);
186         
187         Map JavaDoc<ProjectCustomizer.Category,JComponent JavaDoc> panels =
188                 new HashMap JavaDoc<ProjectCustomizer.Category,JComponent JavaDoc>();
189         panels.put(references, new CustomizerProjectReferences(getProject(), getRefHelper()));
190         panels.put(catalog, new CustomizerXMLCatalog(getProject()));
191         return panels;
192     }
193
194     /**
195      * This api is called when OK button on the Project customizer is clicked.
196      * The default implementation just stores the changes in the XML catalog file.
197      * Project specific implemenataions must override this api to store project
198      * specific properties and should call super, so that XML catalog changes are persisted.
199      */

200     protected void storeProjectData() {
201         // default Impl saves catalog data
202
for (JComponent JavaDoc comp :panelProvider.panels.values()) {
203             if(comp instanceof CustomizerXMLCatalog) {
204                 ((CustomizerXMLCatalog)comp).storeProjectData();
205             }
206         }
207     }
208
209     /**
210      * CategoryComponentProvider provider class.
211      * It stores categories and there corresponding UI in a map.
212      * An instance of PanelProvider is stored in CustomizerProviderImpl instance
213      */

214     private static class PanelProvider implements ProjectCustomizer.CategoryComponentProvider {
215         
216         private JPanel JavaDoc EMPTY_PANEL = new JPanel JavaDoc();
217         
218         private Map JavaDoc<ProjectCustomizer.Category,JComponent JavaDoc> panels;
219         
220         PanelProvider(Map JavaDoc<ProjectCustomizer.Category,JComponent JavaDoc> panels) {
221             this.panels = panels;
222         }
223         
224         public JComponent JavaDoc create(ProjectCustomizer.Category category) {
225             JComponent JavaDoc panel = panels.get(category);
226             return panel == null ? EMPTY_PANEL : panel;
227         }
228                         
229     }
230     
231     /** Listens to the actions on the Customizer's option buttons */
232     private class OptionListener extends WindowAdapter JavaDoc implements ActionListener JavaDoc {
233     
234         private Project project;
235         
236         OptionListener(Project project) {
237             this.project = project;
238         }
239         
240         // Listening to OK button ----------------------------------------------
241

242         public void actionPerformed(ActionEvent JavaDoc e) {
243             // Store the properties into project
244
storeProjectData();
245             // Close & dispose the the dialog
246
Dialog JavaDoc dialog = project2Dialog.get(project);
247             if (dialog != null) {
248                 dialog.setVisible(false);
249                 dialog.dispose();
250             }
251         }
252         
253         // Listening to window events ------------------------------------------
254

255         public void windowClosed(WindowEvent JavaDoc e) {
256             project2Dialog.remove(project);
257         }
258         
259         public void windowClosing(WindowEvent JavaDoc e) {
260             //Dispose the dialog otherwsie the {@link WindowAdapter#windowClosed}
261
//may not be called
262
Dialog JavaDoc dialog = project2Dialog.get(project);
263             if (dialog != null) {
264                 dialog.setVisible(false);
265                 dialog.dispose();
266             }
267         }
268     }
269     
270     public static interface SubCategoryProvider {
271         public void showSubCategory (String JavaDoc name);
272     }
273
274 }
275
Popular Tags