KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > core > TestSetIEAction


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 package org.netbeans.test.editor.app.core;
20
21 import java.util.ArrayList JavaDoc;
22 import org.netbeans.modules.java.editor.options.JavaOptions;
23 import org.netbeans.test.editor.app.gui.*;
24 import javax.swing.text.EditorKit JavaDoc;
25 import javax.swing.JEditorPane JavaDoc;
26 import org.netbeans.modules.editor.plain.PlainKit;
27 import javax.swing.text.PlainDocument JavaDoc;
28 import org.openide.text.IndentEngine;
29 import org.openide.options.SystemOption;
30 import org.netbeans.modules.editor.options.BaseOptions;
31 import java.util.Enumeration JavaDoc;
32 import org.netbeans.test.editor.app.util.Scheduler;
33 import javax.swing.SwingUtilities JavaDoc;
34
35 import javax.swing.text.Document JavaDoc;
36 import org.netbeans.modules.editor.NbEditorDocument;
37 import org.netbeans.modules.editor.java.JavaIndentEngine;
38 import org.netbeans.modules.editor.java.JavaKit;
39 import org.netbeans.modules.editor.options.AllOptions;
40 import org.netbeans.modules.editor.options.BaseOptions;
41 import org.netbeans.test.editor.app.Main;
42 import org.netbeans.test.editor.app.core.properties.ArrayProperty;
43 import org.netbeans.test.editor.app.core.properties.BadPropertyNameException;
44 import org.netbeans.test.editor.app.core.properties.Properties;
45 import org.netbeans.test.editor.app.gui.actions.TestDeleteAction;
46 import org.netbeans.test.editor.app.gui.tree.ActionsCache;
47 import org.openide.util.Lookup;
48 import org.w3c.dom.Element JavaDoc;
49
50 /**
51  *
52  * @author jlahoda
53  * @version
54  */

55 public class TestSetIEAction extends TestSetAction {
56     
57     /** Holds value of property IndentEngine. */
58     protected IndentEngine indentEngine;
59     
60     public static String JavaDoc INDENT_ENGINE = "IndentationEngine";
61     
62     public TestSetIEAction(int num) {
63         this("setIE"+Integer.toString(num));
64     }
65     
66     public TestSetIEAction(String JavaDoc name) {
67         super(name);
68         indentEngine=((BaseOptions)(SystemOption.findObject(JavaOptions.class, true))).getIndentEngine();
69     }
70     
71     public TestSetIEAction(Element JavaDoc node) {
72         super(node);
73         indentEngine = findIndentEngine(node.getAttribute(INDENT_ENGINE));
74     }
75     
76     public Element JavaDoc toXML(Element JavaDoc node) {
77         node = super.toXML(node);
78         
79         node.setAttribute(INDENT_ENGINE, getIndentEngine().getName());
80         return node;
81     }
82     
83     public void fromXML(Element JavaDoc node) throws BadPropertyNameException {
84         super.fromXML(node);
85         indentEngine = findIndentEngine(node.getAttribute(INDENT_ENGINE));
86     }
87     
88     public Properties getProperties() {
89         Properties ret=super.getProperties();
90         ret.put(INDENT_ENGINE, new ArrayProperty(indentEngine.getName(),getIndentEnginesNames()));
91         return ret;
92     }
93     
94     public Object JavaDoc getProperty(String JavaDoc name) throws BadPropertyNameException {
95         if (name.compareTo(INDENT_ENGINE) == 0) {
96             return new ArrayProperty(indentEngine.getName(),getIndentEnginesNames());
97         } else {
98             return super.getProperty(name);
99         }
100     }
101     
102     public void setProperty(String JavaDoc name, Object JavaDoc value) throws BadPropertyNameException {
103         if (name.compareTo(INDENT_ENGINE) == 0) {
104             setIndentEngine(findIndentEngine(((ArrayProperty)value).getProperty()));
105         } else {
106             super.setProperty(name, value);
107         }
108     }
109     
110     public void perform() {
111         super.perform();
112         IndentEngine toSet = getIndentEngine();
113         
114         if (toSet == null) {
115             System.err.println("TestSetIEAction: perform: Trying to set null indent engine!");
116             return;
117         }
118         
119         // EditorKit kit = Main.frame.getEditor().getUI().getEditorKit(Main.frame.getEditor());
120
EditorKit JavaDoc kit = Main.frame.getEditor().getEditorKit();
121         
122         if (kit == null) {
123             System.err.println("TestSetIEAction: perform: kit == null!");
124             return;
125         }
126         
127         Class JavaDoc kitClass = kit.getClass();
128         
129         BaseOptions options = BaseOptions.getOptions(kitClass);
130         
131         if (options != null) {
132 //HACK --> workaround to Issue #25784
133
Lookup.getDefault().lookup(toSet.getClass());
134             
135             options.setIndentEngine(toSet);
136         } else {
137             System.err.println("TestSetIEAction: perform kit class " + kitClass + " not found.");
138         }
139     }
140     
141     /** Getter for property IndentEngine.
142      * @return Value of property IndentEngine.
143      */

144     public IndentEngine getIndentEngine() {
145         return indentEngine;
146     }
147     
148     /** Setter for property IndentEngine.
149      * @param IndentEngine New value of property IndentEngine.
150      */

151     public void setIndentEngine(IndentEngine indentEngine) {
152         IndentEngine old = this.indentEngine;
153         
154         this.indentEngine = indentEngine;
155         
156         firePropertyChange(INDENT_ENGINE, old, indentEngine);
157     }
158     
159     public String JavaDoc[] getIndentEngines() {
160         String JavaDoc[] ret=null;
161         int count=0;
162         Enumeration JavaDoc e;
163         IndentEngine en;
164         
165         e=IndentEngine.indentEngines();
166         while (e.hasMoreElements()) {
167             en=(IndentEngine)(e.nextElement());
168             count++;
169         }
170         ret=new String JavaDoc[count];
171         count=0;
172         e=IndentEngine.indentEngines();
173         while (e.hasMoreElements()) {
174             ret[count++]=((IndentEngine)(e.nextElement())).getName();
175         }
176         return ret;
177     }
178     
179     protected IndentEngine findIndentEngine(String JavaDoc name) {
180         if (name == null)
181             return null;
182         
183         Enumeration JavaDoc e=IndentEngine.indentEngines();
184         while (e.hasMoreElements()) {
185             IndentEngine item = (IndentEngine) e.nextElement();
186             
187             if (name.equals(item.getName())) {
188                 return item;
189             }
190         }
191         return null;
192     }
193     
194     protected IndentEngine findIndentEngine(Class JavaDoc clazz) {
195         if (clazz == null)
196             return null;
197         
198         Enumeration JavaDoc e=IndentEngine.indentEngines();
199         while (e.hasMoreElements()) {
200             IndentEngine item = (IndentEngine) e.nextElement();
201             
202             if (clazz.isInstance(item)) {
203                 return item;
204             }
205         }
206         return null;
207     }
208     
209     public static String JavaDoc[] getIndentEnginesNames() {
210         ArrayList JavaDoc a=new ArrayList JavaDoc();
211         Enumeration JavaDoc e=IndentEngine.indentEngines();
212         while (e.hasMoreElements()) {
213             IndentEngine item = (IndentEngine) e.nextElement();
214             a.add(item.getName());
215         }
216         return (String JavaDoc[])(a.toArray(new String JavaDoc[a.size()]));
217     }
218     
219     public static void main(String JavaDoc[] args) {
220         TestSetIEAction act=new TestSetIEAction("action");
221         String JavaDoc[] names=act.getIndentEnginesNames();
222         IndentEngine eng;
223         String JavaDoc id=null;
224         for (int i=0;i < names.length;i++) {
225             eng=act.findIndentEngine(names[i]);
226             Lookup.Template tmp = new Lookup.Template(null, null, eng);
227             Lookup.Item item = Lookup.getDefault().lookupItem(tmp);
228             if (item != null) id = item.getId();
229             System.err.println("ID for "+names[i]+": "+id);
230         }
231     }
232 }
233
Popular Tags