Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.openide.windows; |
44 |
|
45 |
import java.util.HashSet; |
46 |
import java.util.Set; |
47 |
import javax.annotation.processing.Processor; |
48 |
import javax.annotation.processing.RoundEnvironment; |
49 |
import javax.annotation.processing.SupportedSourceVersion; |
50 |
import javax.lang.model.SourceVersion; |
51 |
import javax.lang.model.element.Element; |
52 |
import javax.lang.model.element.TypeElement; |
53 |
import javax.lang.model.type.DeclaredType; |
54 |
import javax.lang.model.type.ExecutableType; |
55 |
import org.openide.awt.ActionID; |
56 |
import org.openide.filesystems.annotations.LayerBuilder.File; |
57 |
import org.openide.filesystems.annotations.LayerGeneratingProcessor; |
58 |
import org.openide.filesystems.annotations.LayerGenerationException; |
59 |
import org.openide.util.lookup.ServiceProvider; |
60 |
import org.openide.windows.TopComponent; |
61 |
import org.openide.windows.TopComponent.Description; |
62 |
|
63 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
64 |
@ServiceProvider(service=Processor.class) |
65 |
public final class TopComponentProcessor extends LayerGeneratingProcessor { |
66 |
public TopComponentProcessor() { |
67 |
} |
68 |
|
69 |
@Override |
70 |
public Set<String> getSupportedAnnotationTypes() { |
71 |
Set<String> hash = new HashSet<String>(); |
72 |
hash.add(TopComponent.Registration.class.getCanonicalName()); |
73 |
hash.add(TopComponent.OpenActionRegistration.class.getCanonicalName()); |
74 |
hash.add(TopComponent.Description.class.getCanonicalName()); |
75 |
return hash; |
76 |
} |
77 |
|
78 |
@Override |
79 |
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException { |
80 |
for (Element e : roundEnv.getElementsAnnotatedWith(TopComponent.Registration.class)) { |
81 |
TopComponent.Registration reg = e.getAnnotation(TopComponent.Registration.class); |
82 |
assert reg != null; |
83 |
|
84 |
Description info = findInfo(e); |
85 |
String id = info.preferredID().replace('.', '-'); |
86 |
|
87 |
File settingsFile = layer(e). |
88 |
file("Windows2/Components/" + id + ".settings"). |
89 |
contents(settingsFile(e)); |
90 |
settingsFile.write(); |
91 |
|
92 |
File modeFile = layer(e). |
93 |
file("Windows2/Modes/" + reg.mode() + "/" + id + ".wstcref"). |
94 |
contents(modeFile(info.preferredID(), reg.openAtStartup())); |
95 |
modeFile.write(); |
96 |
} |
97 |
|
98 |
for (Element e : roundEnv.getElementsAnnotatedWith(TopComponent.OpenActionRegistration.class)) { |
99 |
TopComponent.OpenActionRegistration reg = e.getAnnotation(TopComponent.OpenActionRegistration.class); |
100 |
assert reg != null; |
101 |
Description info = findInfo(e); |
102 |
|
103 |
ActionID aid = e.getAnnotation(ActionID.class); |
104 |
if (aid != null) { |
105 |
File actionFile = layer(e). |
106 |
file("Actions/" + aid.category() + "/" + aid.id().replace('.', '-') + ".instance"). |
107 |
methodvalue("instanceCreate", "org.openide.windows.TopComponent", "openAction"); |
108 |
actionFile.instanceAttribute("component", TopComponent.class); |
109 |
if (reg.preferredID().length() > 0) { |
110 |
actionFile.stringvalue("preferredID", reg.preferredID()); |
111 |
} |
112 |
actionFile.bundlevalue("displayName", reg.displayName()); |
113 |
if (info != null && info.iconBase().length() > 0) { |
114 |
actionFile.stringvalue("iconBase", info.iconBase()); |
115 |
} |
116 |
actionFile.write(); |
117 |
} |
118 |
} |
119 |
return true; |
120 |
} |
121 |
|
122 |
private Description findInfo(Element e) throws LayerGenerationException { |
123 |
Element type; |
124 |
switch (e.asType().getKind()) { |
125 |
case DECLARED: type = e; break; |
126 |
case EXECUTABLE: type = ((DeclaredType)((ExecutableType)e.asType()).getReturnType()).asElement(); break; |
127 |
default: throw new LayerGenerationException("" + e.asType().getKind(), e); |
128 |
} |
129 |
TopComponent.Description info = type.getAnnotation(TopComponent.Description.class); |
130 |
return info; |
131 |
} |
132 |
|
133 |
private static String settingsFile(Element e) throws LayerGenerationException { |
134 |
String clazz, method; |
135 |
switch (e.getKind()) { |
136 |
case CLASS: clazz = e.toString(); method = null; break; |
137 |
case METHOD: clazz = e.getEnclosingElement().toString(); method = e.getSimpleName().toString(); break; |
138 |
default: |
139 |
throw new LayerGenerationException("Cannot work on given element", e); |
140 |
} |
141 |
StringBuilder sb = new StringBuilder(); |
142 |
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
143 |
sb.append("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">\n"); |
144 |
sb.append("<settings version=\"1.0\">\n"); |
145 |
sb.append(" <instance class=\"").append(clazz).append("\""); |
146 |
if (method != null) { |
147 |
sb.append(" method=\"").append(method).append("\""); |
148 |
} |
149 |
sb.append("/>\n"); |
150 |
sb.append("</settings>\n"); |
151 |
return sb.toString(); |
152 |
} |
153 |
|
154 |
private static String modeFile(String id, boolean openAtStart) |
155 |
throws LayerGenerationException { |
156 |
StringBuilder sb = new StringBuilder(); |
157 |
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
158 |
sb.append("<!DOCTYPE tc-ref PUBLIC \"-//NetBeans//DTD Top Component in Mode Properties 2.0//EN\" \"http://www.netbeans.org/dtds/tc-ref2_0.dtd\">\n"); |
159 |
sb.append("<tc-ref version=\"2.0\">\n"); |
160 |
sb.append(" <tc-id id=\"").append(id).append("\"/>\n"); |
161 |
sb.append(" <state opened=\"").append(openAtStart).append("\"/>\n"); |
162 |
sb.append("</tc-ref>\n"); |
163 |
return sb.toString(); |
164 |
} |
165 |
} |