KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > action > DataModel


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.wizard.action;
21
22 import java.net.URL JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
32 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
33 import org.openide.WizardDescriptor;
34 import org.openide.filesystems.FileObject;
35
36 /**
37  * Data model used across the <em>New Action Wizard</em>.
38  */

39 final class DataModel extends BasicWizardIterator.BasicDataModel {
40     
41     static final String JavaDoc[] PREDEFINED_COOKIE_CLASSES;
42     
43     private static final String JavaDoc[] HARDCODED_IMPORTS = new String JavaDoc[] {
44         "org.openide.nodes.Node", // NOI18N
45
"org.openide.util.HelpCtx", // NOI18N
46
"org.openide.util.NbBundle", // NOI18N
47
"org.openide.util.actions.CookieAction" // NOI18N
48
};
49     
50     /** Maps FQCN to CNB. */
51     private static final Map JavaDoc<String JavaDoc,String JavaDoc> CLASS_TO_CNB;
52     
53     static {
54         Map JavaDoc map = new HashMap JavaDoc(5);
55         map.put("org.openide.loaders.DataObject", "org.openide.loaders"); // NOI18N
56
map.put("org.openide.cookies.EditCookie", "org.openide.nodes"); // NOI18N
57
map.put("org.openide.cookies.OpenCookie", "org.openide.nodes"); // NOI18N
58
map.put("org.netbeans.api.project.Project", "org.netbeans.modules.projectapi"); // NOI18N
59
map.put("org.openide.cookies.EditorCookie", "org.openide.text"); // NOI18N
60
CLASS_TO_CNB = Collections.unmodifiableMap(map);
61         PREDEFINED_COOKIE_CLASSES = new String JavaDoc[5];
62         DataModel.CLASS_TO_CNB.keySet().toArray(PREDEFINED_COOKIE_CLASSES);
63     }
64     
65     private static final String JavaDoc NEW_LINE = System.getProperty("line.separator"); // NOI18N
66

67     /** Default indent. (four spaces hardcoded currently). */
68     private static final String JavaDoc INDENT = " "; // NOI18N
69
/** Double {@link #INDENT}. */
70     private static final String JavaDoc INDENT_2X = INDENT + INDENT; // NOI18N
71

72     private CreatedModifiedFiles cmf;
73     
74     // first panel data (Action Type)
75
private boolean alwaysEnabled;
76     private String JavaDoc[] cookieClasses;
77     private boolean multiSelection;
78     
79     // second panel data (GUI Registration)
80
private String JavaDoc category;
81     
82     // global menu item fields
83
private boolean globalMenuItemEnabled;
84     private String JavaDoc gmiParentMenuPath;
85     private Position gmiPosition;
86     private boolean gmiSeparatorAfter;
87     private boolean gmiSeparatorBefore;
88     
89     // global toolbar button fields
90
private boolean toolbarEnabled;
91     private String JavaDoc toolbar;
92     private Position toolbarPosition;
93     
94     // global keyboard shortcut
95
private boolean kbShortcutEnabled;
96     private final Set JavaDoc keyStrokes = new HashSet JavaDoc();
97     
98     // file type context menu item
99
private boolean ftContextEnabled;
100     private String JavaDoc ftContextType;
101     private Position ftContextPosition;
102     private boolean ftContextSeparatorAfter;
103     private boolean ftContextSeparatorBefore;
104     
105     // editor context menu item
106
private boolean edContextEnabled;
107     private String JavaDoc edContextType;
108     private Position edContextPosition;
109     private boolean edContextSeparatorAfter;
110     private boolean edContextSeparatorBefore;
111     
112     // third panel data (Name, Icon, and Location)
113
private String JavaDoc className;
114     private String JavaDoc displayName;
115     private String JavaDoc origIconPath;
116     private String JavaDoc largeIconPath;
117     
118     DataModel(WizardDescriptor wiz) {
119         super(wiz);
120     }
121     
122     private void regenerate() {
123         String JavaDoc dashedPkgName = getPackageName().replace('.', '-');
124         String JavaDoc dashedFqClassName = dashedPkgName + '-' + className;
125         String JavaDoc shadow = dashedFqClassName + ".shadow"; // NOI18N
126

127         cmf = new CreatedModifiedFiles(getProject());
128         
129         String JavaDoc actionPath = getDefaultPackagePath(className + ".java", false); // NOI18N
130
// XXX use nbresloc URL protocol rather than DataModel.class.getResource(...):
131
URL JavaDoc template = DataModel.class.getResource(alwaysEnabled
132                 ? "callableSystemAction.javx" : "cookieAction.javx"); // NOI18N
133
String JavaDoc actionNameKey = "CTL_" + className; // NOI18N
134
Map JavaDoc replaceTokens = new HashMap JavaDoc();
135         replaceTokens.put("@@CLASS_NAME@@", className); // NOI18N
136
replaceTokens.put("@@PACKAGE_NAME@@", getPackageName()); // NOI18N
137
replaceTokens.put("@@DISPLAY_NAME_KEY@@", actionNameKey); // NOI18N
138
replaceTokens.put("@@MODE@@", getSelectionMode()); // NOI18N
139
Set JavaDoc imports = new TreeSet JavaDoc(Arrays.asList(HARDCODED_IMPORTS));
140         Set JavaDoc addedFQNCs = new TreeSet JavaDoc();
141         if (!alwaysEnabled) {
142             StringBuffer JavaDoc cookieSB = new StringBuffer JavaDoc();
143             for (int i = 0; i < cookieClasses.length; i++) {
144                 // imports for predefined chosen cookie classes
145
if (CLASS_TO_CNB.containsKey(cookieClasses[i])) {
146                     addedFQNCs.add(cookieClasses[i]);
147                 }
148                 // cookie block
149
cookieSB.append(INDENT_2X + INDENT + parseClassName(cookieClasses[i]) + ".class"); // NOI18N
150
if (i != cookieClasses.length - 1) {
151                     cookieSB.append(',' + NEW_LINE);
152                 }
153             }
154             replaceTokens.put("@@COOKIE_CLASSES_BLOCK@@", cookieSB.toString()); // NOI18N
155
String JavaDoc impl;
156             if (cookieClasses.length == 1) {
157                 String JavaDoc cName = parseClassName(cookieClasses[0]);
158                 String JavaDoc cNameVar = Character.toLowerCase(cName.charAt(0)) + cName.substring(1);
159                 impl = cName + ' ' + cNameVar + " = (" + cName + ") activatedNodes[0].getLookup().lookup(" + cName + ".class);\n" // NOI18N
160
+ INDENT_2X + "// TODO use " + cNameVar; // NOI18N
161
} else {
162                 impl = "// TODO implement action body"; // NOI18N
163
}
164             replaceTokens.put("@@PERFORM_ACTION_CODE@@", impl); // NOI18N
165
}
166         // imports
167
imports.addAll(addedFQNCs);
168         StringBuffer JavaDoc importsBuffer = new StringBuffer JavaDoc();
169         for (Iterator JavaDoc it = imports.iterator(); it.hasNext();) {
170             importsBuffer.append("import " + it.next() + ';' + NEW_LINE); // NOI18N
171
}
172         replaceTokens.put("@@IMPORTS@@", importsBuffer.toString()); // NOI18N
173
cmf.add(cmf.createFileWithSubstitutions(actionPath, template, replaceTokens));
174         
175         // Bundle.properties for localized action name
176
cmf.add(cmf.bundleKey(getDefaultPackagePath("Bundle.properties", true), actionNameKey, displayName)); // NOI18N
177

178         // Copy action icon
179
if (origIconPath != null) {
180             String JavaDoc relativeIconPath = addCreateIconOperation(cmf, origIconPath);
181             replaceTokens.put("@@ICON_RESOURCE_METHOD@@", DataModel.generateIconResourceMethod(relativeIconPath)); // NOI18N
182
replaceTokens.put("@@INITIALIZE_METHOD@@", ""); // NOI18N
183
} else {
184             replaceTokens.put("@@ICON_RESOURCE_METHOD@@", ""); // NOI18N
185
replaceTokens.put("@@INITIALIZE_METHOD@@", DataModel.generateNoIconInitializeMethod()); // NOI18N
186
}
187         
188         if (isToolbarEnabled() && largeIconPath != null) {
189             addCreateIconOperation(cmf, largeIconPath);
190         }
191         
192         // add layer entry about the action
193
String JavaDoc instanceFullPath = category + "/" // NOI18N
194
+ dashedFqClassName + ".instance"; // NOI18N
195
cmf.add(cmf.createLayerEntry(instanceFullPath, null, null, null, null));
196         
197         // add dependency on util to project.xml
198
cmf.add(cmf.addModuleDependency("org.openide.util")); // NOI18N
199
if (!alwaysEnabled) {
200             cmf.add(cmf.addModuleDependency("org.openide.nodes")); // NOI18N
201
for (Iterator JavaDoc it = addedFQNCs.iterator(); it.hasNext(); ) {
202                 cmf.add(cmf.addModuleDependency((String JavaDoc) CLASS_TO_CNB.get(it.next())));
203             }
204         }
205         
206         // create layer entry for global menu item
207
if (globalMenuItemEnabled) {
208             generateShadowWithOrderAndSeparator(gmiParentMenuPath, shadow,
209                     dashedPkgName, instanceFullPath, gmiSeparatorBefore,
210                     gmiSeparatorAfter, gmiPosition);
211         }
212         
213         // create layer entry for toolbar button
214
if (toolbarEnabled) {
215             generateOrder(toolbar, toolbarPosition.getBefore(), shadow);
216             generateShadow(toolbar + "/" + shadow, instanceFullPath); // NOI18N
217
generateOrder(toolbar, shadow, toolbarPosition.getAfter());
218         }
219         
220         // create layer entry for keyboard shortcut
221
if (kbShortcutEnabled) {
222             String JavaDoc parentPath = "Shortcuts"; // NOI18N
223
for (Iterator JavaDoc it = keyStrokes.iterator(); it.hasNext();) {
224                 String JavaDoc keyStroke = (String JavaDoc) it.next();
225                 generateShadow(parentPath + "/" + keyStroke + ".shadow", instanceFullPath); // NOI18N
226
}
227         }
228         
229         // create file type context menu item
230
if (ftContextEnabled) {
231             generateShadowWithOrderAndSeparator(ftContextType, shadow,
232                     dashedPkgName, instanceFullPath, ftContextSeparatorBefore,
233                     ftContextSeparatorAfter, ftContextPosition);
234         }
235         
236         // create editor context menu item
237
if (edContextEnabled) {
238             generateShadowWithOrderAndSeparator(edContextType, shadow,
239                     dashedPkgName, instanceFullPath, edContextSeparatorBefore,
240                     edContextSeparatorAfter, edContextPosition);
241         }
242     }
243     
244     private void generateShadowWithOrderAndSeparator(
245             final String JavaDoc parentPath,
246             final String JavaDoc shadow,
247             final String JavaDoc dashedPkgName,
248             final String JavaDoc instanceFullPath,
249             final boolean separatorBefore,
250             final boolean separatorAfter,
251             final Position position) {
252         if (separatorBefore) {
253             String JavaDoc sepName = dashedPkgName + "-separatorBefore.instance"; // NOI18N
254
generateSeparator(parentPath, sepName);
255             generateOrder(parentPath, position.getBefore(), sepName);
256             generateOrder(parentPath, sepName, shadow);
257         } else {
258             generateOrder(parentPath, position.getBefore(), shadow);
259         }
260         generateShadow(parentPath + "/" + shadow, instanceFullPath); // NOI18N
261
if (separatorAfter) {
262             String JavaDoc sepName = dashedPkgName + "-separatorAfter.instance"; // NOI18N
263
generateSeparator(parentPath, sepName);
264             generateOrder(parentPath, shadow, sepName);
265             generateOrder(parentPath, sepName, position.getAfter());
266         } else {
267             generateOrder(parentPath, shadow, position.getAfter());
268         }
269     }
270     
271     /**
272      * Just a helper convenient mehtod for cleaner code. If either
273      * <em>before</em> or <em>after</em> is <code>null</code>, nothing will be
274      * generated.
275      */

276     private void generateOrder(String JavaDoc layerPath, String JavaDoc before, String JavaDoc after) {
277         if (before != null && after != null) {
278             cmf.add(cmf.orderLayerEntry(layerPath, before, after));
279         }
280     }
281     
282     /** Checks whether a proposed class exists. */
283     boolean classExists() {
284         FileObject classFO = getProject().getProjectDirectory().getFileObject(
285                 getDefaultPackagePath(className + ".java", false)); // NOI18N
286
return classFO != null;
287     }
288     
289     private void generateShadow(final String JavaDoc itemPath, final String JavaDoc origInstance) {
290         cmf.add(cmf.createLayerEntry(itemPath, null, null, null, null));
291         cmf.add(cmf.createLayerAttribute(itemPath, "originalFile", origInstance)); // NOI18N
292
}
293     
294     CreatedModifiedFiles getCreatedModifiedFiles() {
295         if (cmf == null) {
296             regenerate();
297         }
298         return cmf;
299     }
300     
301     private void reset() {
302         cmf = null;
303     }
304     
305     void setAlwaysEnabled(boolean alwaysEnabled) {
306         this.alwaysEnabled = alwaysEnabled;
307     }
308     
309     boolean isAlwaysEnabled() {
310         return alwaysEnabled;
311     }
312     
313     void setCookieClasses(String JavaDoc[] cookieClasses) {
314         this.cookieClasses = cookieClasses;
315     }
316     
317     void setMultiSelection(boolean multiSelection) {
318         this.multiSelection = multiSelection;
319     }
320     
321     private String JavaDoc getSelectionMode() {
322         return multiSelection ? "MODE_ALL" : "MODE_EXACTLY_ONE"; // NOI18N
323
}
324     
325     void setCategory(String JavaDoc category) {
326         this.category = category;
327     }
328     
329     void setClassName(String JavaDoc className) {
330         reset();
331         this.className = className;
332     }
333     
334     public String JavaDoc getClassName() {
335         return className;
336     }
337     
338     void setDisplayName(String JavaDoc display) {
339         this.displayName = display;
340     }
341     
342     public String JavaDoc getDisplayName() {
343         return displayName;
344     }
345     
346     void setIconPath(String JavaDoc origIconPath) {
347         reset();
348         this.origIconPath = origIconPath;
349     }
350     
351     public String JavaDoc getIconPath() {
352         return origIconPath;
353     }
354     
355     public void setPackageName(String JavaDoc pkg) {
356         super.setPackageName(pkg);
357         reset();
358     }
359     
360     void setGlobalMenuItemEnabled(boolean globalMenuItemEnabled) {
361         this.globalMenuItemEnabled = globalMenuItemEnabled;
362     }
363     
364     void setGMIParentMenu(String JavaDoc gmiParentMenuPath) {
365         this.gmiParentMenuPath = gmiParentMenuPath;
366     }
367     
368     void setGMISeparatorAfter(boolean gmiSeparatorAfter) {
369         this.gmiSeparatorAfter = gmiSeparatorAfter;
370     }
371     
372     void setGMISeparatorBefore(boolean gmiSeparatorBefore) {
373         this.gmiSeparatorBefore = gmiSeparatorBefore;
374     }
375     
376     void setGMIPosition(Position position) {
377         this.gmiPosition = position;
378     }
379     
380     void setToolbarEnabled(boolean toolbarEnabled) {
381         this.toolbarEnabled = toolbarEnabled;
382     }
383     
384     boolean isToolbarEnabled() {
385         return toolbarEnabled;
386     }
387     
388     void setToolbar(String JavaDoc toolbar) {
389         this.toolbar = toolbar;
390     }
391     
392     void setToolbarPosition(Position position) {
393         this.toolbarPosition = position;
394     }
395     
396     void setKeyboardShortcutEnabled(boolean kbShortcutEnabled) {
397         this.kbShortcutEnabled = kbShortcutEnabled;
398     }
399     
400     void setKeyStroke(String JavaDoc keyStroke) {
401         keyStrokes.add(keyStroke);
402     }
403     
404     void setFileTypeContextEnabled(boolean contextEnabled) {
405         this.ftContextEnabled = contextEnabled;
406     }
407     
408     void setFTContextType(String JavaDoc contextType) {
409         this.ftContextType = contextType;
410     }
411     
412     void setFTContextPosition(Position position) {
413         this.ftContextPosition = position;
414     }
415     
416     void setFTContextSeparatorAfter(boolean separator) {
417         this.ftContextSeparatorAfter = separator;
418     }
419     
420     void setFTContextSeparatorBefore(boolean separator) {
421         this.ftContextSeparatorBefore = separator;
422     }
423     
424     void setEditorContextEnabled(boolean contextEnabled) {
425         this.edContextEnabled = contextEnabled;
426     }
427     
428     void setEdContextType(String JavaDoc contextType) {
429         this.edContextType = contextType;
430     }
431     
432     void setEdContextPosition(Position position) {
433         this.edContextPosition = position;
434     }
435     
436     void setEdContextSeparatorAfter(boolean separator) {
437         this.edContextSeparatorAfter = separator;
438     }
439     
440     void setEdContextSeparatorBefore(boolean separator) {
441         this.edContextSeparatorBefore = separator;
442     }
443     
444     static final class Position {
445         
446         private String JavaDoc before;
447         private String JavaDoc after;
448         private String JavaDoc beforeName;
449         private String JavaDoc afterName;
450         
451         Position(String JavaDoc before, String JavaDoc after) {
452             this(before, after, null, null);
453         }
454         
455         Position(String JavaDoc before, String JavaDoc after, String JavaDoc beforeName, String JavaDoc afterName) {
456             this.before = before;
457             this.after = after;
458             this.beforeName = beforeName;
459             this.afterName = afterName;
460         }
461         
462         String JavaDoc getBefore() {
463             return before;
464         }
465         
466         String JavaDoc getAfter() {
467             return after;
468         }
469         
470         String JavaDoc getBeforeName() {
471             return beforeName;
472         }
473         
474         String JavaDoc getAfterName() {
475             return afterName;
476         }
477     }
478     
479     private void generateSeparator(final String JavaDoc parentPath, final String JavaDoc sepName) {
480         String JavaDoc sepPath = parentPath + "/" + sepName; // NOI18N
481
cmf.add(cmf.createLayerEntry(sepPath,
482                 null, null, null, null));
483         cmf.add(cmf.createLayerAttribute(sepPath, "instanceClass", // NOI18N
484
"javax.swing.JSeparator")); // NOI18N
485
}
486     
487     /**
488      * Parse class name from a fully qualified class name. If the given name
489      * doesn't contain dot (<em>.</em>), given parameter is returned.
490      */

491     static String JavaDoc parseClassName(final String JavaDoc name) {
492         int lastDot = name.lastIndexOf('.');
493         return lastDot == -1 ? name : name.substring(lastDot + 1);
494     }
495     
496     private static String JavaDoc generateIconResourceMethod(final String JavaDoc relativeIconPath) {
497         return NEW_LINE +
498                 INDENT + "protected String iconResource() {" + NEW_LINE + // NOI18N
499
INDENT_2X + "return \"" + relativeIconPath + "\";" + NEW_LINE + // NOI18N
500
INDENT + "}"; // NOI18N
501
}
502     
503     private static String JavaDoc generateNoIconInitializeMethod() {
504         return "protected void initialize() {" + NEW_LINE + // NOI18N
505
INDENT_2X + "super.initialize();" + NEW_LINE + // NOI18N
506
INDENT_2X + "// see org.openide.util.actions.SystemAction.iconResource() javadoc for more details" + NEW_LINE + // NOI18N
507
INDENT_2X + "putValue(\"noIconInMenu\", Boolean.TRUE);" + NEW_LINE + // NOI18N
508
INDENT + "}" + NEW_LINE; // NOI18N
509
}
510
511     public void setLargeIconPath(String JavaDoc largeIconPath) {
512         this.largeIconPath = largeIconPath;
513     }
514     
515 }
516
517
Popular Tags