1 19 package org.netbeans.test.editor.app.core; 20 21 import java.util.ArrayList ; 22 import org.netbeans.modules.java.editor.options.JavaOptions; 23 import org.netbeans.test.editor.app.gui.*; 24 import javax.swing.text.EditorKit ; 25 import javax.swing.JEditorPane ; 26 import org.netbeans.modules.editor.plain.PlainKit; 27 import javax.swing.text.PlainDocument ; 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 ; 32 import org.netbeans.test.editor.app.util.Scheduler; 33 import javax.swing.SwingUtilities ; 34 35 import javax.swing.text.Document ; 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 ; 49 50 55 public class TestSetIEAction extends TestSetAction { 56 57 58 protected IndentEngine indentEngine; 59 60 public static String INDENT_ENGINE = "IndentationEngine"; 61 62 public TestSetIEAction(int num) { 63 this("setIE"+Integer.toString(num)); 64 } 65 66 public TestSetIEAction(String name) { 67 super(name); 68 indentEngine=((BaseOptions)(SystemOption.findObject(JavaOptions.class, true))).getIndentEngine(); 69 } 70 71 public TestSetIEAction(Element node) { 72 super(node); 73 indentEngine = findIndentEngine(node.getAttribute(INDENT_ENGINE)); 74 } 75 76 public Element toXML(Element node) { 77 node = super.toXML(node); 78 79 node.setAttribute(INDENT_ENGINE, getIndentEngine().getName()); 80 return node; 81 } 82 83 public void fromXML(Element 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 getProperty(String 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 name, Object 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().getEditorKit(); 121 122 if (kit == null) { 123 System.err.println("TestSetIEAction: perform: kit == null!"); 124 return; 125 } 126 127 Class kitClass = kit.getClass(); 128 129 BaseOptions options = BaseOptions.getOptions(kitClass); 130 131 if (options != null) { 132 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 144 public IndentEngine getIndentEngine() { 145 return indentEngine; 146 } 147 148 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 [] getIndentEngines() { 160 String [] ret=null; 161 int count=0; 162 Enumeration 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 [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 name) { 180 if (name == null) 181 return null; 182 183 Enumeration 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 clazz) { 195 if (clazz == null) 196 return null; 197 198 Enumeration 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 [] getIndentEnginesNames() { 210 ArrayList a=new ArrayList (); 211 Enumeration e=IndentEngine.indentEngines(); 212 while (e.hasMoreElements()) { 213 IndentEngine item = (IndentEngine) e.nextElement(); 214 a.add(item.getName()); 215 } 216 return (String [])(a.toArray(new String [a.size()])); 217 } 218 219 public static void main(String [] args) { 220 TestSetIEAction act=new TestSetIEAction("action"); 221 String [] names=act.getIndentEnginesNames(); 222 IndentEngine eng; 223 String 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 |