Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
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]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
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. |
40 |
*/ |
41 |
package org.netbeans.modules.editor.codegen; |
42 |
|
43 |
import javax.swing.text.AbstractDocument; |
44 |
import javax.swing.text.Document; |
45 |
import org.netbeans.api.editor.mimelookup.MimePath; |
46 |
import org.netbeans.api.lexer.TokenHierarchy; |
47 |
import org.netbeans.api.lexer.TokenSequence; |
48 |
import org.netbeans.modules.editor.*; |
49 |
import java.awt.Point; |
50 |
import java.awt.Rectangle; |
51 |
import java.awt.event.ActionEvent; |
52 |
import java.util.ArrayList; |
53 |
import java.util.Iterator; |
54 |
import java.util.List; |
55 |
import javax.swing.JMenuItem; |
56 |
import javax.swing.SwingUtilities; |
57 |
import javax.swing.text.BadLocationException; |
58 |
import javax.swing.text.JTextComponent; |
59 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
60 |
import org.netbeans.editor.BaseAction; |
61 |
import org.netbeans.editor.ext.ExtKit; |
62 |
import org.netbeans.spi.editor.codegen.CodeGenerator; |
63 |
import org.netbeans.spi.editor.codegen.CodeGeneratorContextProvider; |
64 |
import org.openide.util.Lookup; |
65 |
import org.openide.util.NbBundle; |
66 |
import org.openide.util.lookup.Lookups; |
67 |
|
68 |
/** |
69 |
* |
70 |
* @author Dusan Balek, Jan Lahoda |
71 |
*/ |
72 |
public class NbGenerateCodeAction extends BaseAction { |
73 |
|
74 |
public static final String generateCode = "generate-code"; //NOI18N |
75 |
|
76 |
public NbGenerateCodeAction(){ |
77 |
super(generateCode); |
78 |
putValue(ExtKit.TRIMMED_TEXT, NbBundle.getBundle(NbGenerateCodeAction.class).getString("generate-code-trimmed")); //NOI18N |
79 |
putValue(SHORT_DESCRIPTION, NbBundle.getBundle(NbGenerateCodeAction.class).getString("desc-generate-code")); //NOI18N |
80 |
putValue(POPUP_MENU_TEXT, NbBundle.getBundle(NbGenerateCodeAction.class).getString("popup-generate-code")); //NOI18N |
81 |
} |
82 |
|
83 |
public void actionPerformed(ActionEvent evt, final JTextComponent target) { |
84 |
Task task = new Task(getFullMimePath(target.getDocument(), target.getCaretPosition())); |
85 |
task.run(Lookups.singleton(target)); |
86 |
if (task.codeGenerators.size() > 0) { |
87 |
int altHeight = -1; |
88 |
Point where = null; |
89 |
try { |
90 |
Rectangle carretRectangle = target.modelToView(target.getCaretPosition()); |
91 |
altHeight = carretRectangle.height; |
92 |
where = new Point( carretRectangle.x, carretRectangle.y + carretRectangle.height ); |
93 |
SwingUtilities.convertPointToScreen(where, target); |
94 |
} catch (BadLocationException ble) { |
95 |
} |
96 |
if (where == null) |
97 |
where = new Point(-1, -1); |
98 |
PopupUtil.showPopup(new GenerateCodePanel(target, task.codeGenerators), null, where.x, where.y, true, altHeight); |
99 |
} else { |
100 |
target.getToolkit().beep(); |
101 |
} |
102 |
} |
103 |
|
104 |
static String[] test(Document doc, int pos) { |
105 |
Task task = new Task(getFullMimePath(doc, pos)); |
106 |
task.run(Lookups.fixed()); |
107 |
String[] ret = new String[task.codeGenerators.size()]; |
108 |
int i = 0; |
109 |
for (CodeGenerator codeGenerator : task.codeGenerators) |
110 |
ret[i++] = codeGenerator.getDisplayName(); |
111 |
return ret; |
112 |
} |
113 |
|
114 |
private static MimePath getFullMimePath(Document document, int offset) { |
115 |
String langPath = null; |
116 |
|
117 |
if (document instanceof AbstractDocument) { |
118 |
AbstractDocument adoc = (AbstractDocument)document; |
119 |
adoc.readLock(); |
120 |
try { |
121 |
List<TokenSequence<?>> list = TokenHierarchy.get(document).embeddedTokenSequences(offset, true); |
122 |
if (list.size() > 1) { |
123 |
langPath = list.get(list.size() - 1).languagePath().mimePath(); |
124 |
} |
125 |
} finally { |
126 |
adoc.readUnlock(); |
127 |
} |
128 |
} |
129 |
|
130 |
if (langPath == null) { |
131 |
langPath = NbEditorUtilities.getMimeType(document); |
132 |
} |
133 |
|
134 |
if (langPath != null) { |
135 |
return MimePath.parse(langPath); |
136 |
} else { |
137 |
return null; |
138 |
} |
139 |
} |
140 |
|
141 |
private static class Task implements CodeGeneratorContextProvider.Task { |
142 |
private MimePath mimePath; |
143 |
private Iterator<? extends CodeGeneratorContextProvider> contextProviders; |
144 |
private List<CodeGenerator> codeGenerators = new ArrayList<CodeGenerator>(); |
145 |
|
146 |
private Task(MimePath mimePath) { |
147 |
this.mimePath = mimePath; |
148 |
contextProviders = MimeLookup.getLookup(mimePath).lookupAll(CodeGeneratorContextProvider.class).iterator(); |
149 |
} |
150 |
|
151 |
public void run(Lookup context) { |
152 |
if (contextProviders.hasNext()) { |
153 |
contextProviders.next().runTaskWithinContext(context, this); |
154 |
} else { |
155 |
for (CodeGenerator.Factory factory : MimeLookup.getLookup(mimePath).lookupAll(CodeGenerator.Factory.class)) |
156 |
codeGenerators.addAll(factory.create(context)); |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
public static final class GlobalAction extends MainMenuAction { |
162 |
|
163 |
private final JMenuItem menuPresenter; |
164 |
|
165 |
public GlobalAction() { |
166 |
super(); |
167 |
this.menuPresenter = new JMenuItem(getMenuItemText()); |
168 |
setMenu(); |
169 |
} |
170 |
|
171 |
protected String getMenuItemText() { |
172 |
return NbBundle.getBundle(GlobalAction.class).getString("generate-code-main-menu-source-item"); //NOI18N |
173 |
} |
174 |
|
175 |
protected String getActionName() { |
176 |
return generateCode; |
177 |
} |
178 |
|
179 |
public JMenuItem getMenuPresenter() { |
180 |
return menuPresenter; |
181 |
} |
182 |
|
183 |
} // End of GlobalAction class |
184 |
} |