1 18 19 package org.objectweb.jac.ide.swing; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.TextEvent ; 23 import java.awt.event.TextListener ; 24 import java.io.StringReader ; 25 import java.util.Arrays ; 26 import java.util.Set ; 27 import javax.swing.JLabel ; 28 import javax.swing.event.CaretEvent ; 29 import javax.swing.event.CaretListener ; 30 import org.apache.log4j.Logger; 31 import org.objectweb.jac.aspects.gui.GuiAC; 32 import org.objectweb.jac.aspects.gui.swing.AbstractCodeEditor; 33 import org.objectweb.jac.core.ACManager; 34 import org.objectweb.jac.core.AspectComponent; 35 import org.objectweb.jac.core.Collaboration; 36 import org.objectweb.jac.core.parsers.acc.NonTerminal; 37 import org.objectweb.jac.core.parsers.acc.SyntaxElement; 38 import org.objectweb.jac.core.parsers.acc.Terminal; 39 import org.objectweb.jac.core.parsers.acc.ToolParserWrapper; 40 import org.objectweb.jac.core.rtti.ClassItem; 41 import org.objectweb.jac.core.rtti.ClassRepository; 42 import org.objectweb.jac.core.rtti.FieldItem; 43 import org.objectweb.jac.core.rtti.MethodItem; 44 import org.objectweb.jac.core.rtti.NoSuchMethodException; 45 import org.objectweb.jac.ide.Application; 46 import org.objectweb.jac.ide.Projects; 47 import org.objectweb.jac.ide.AspectConfiguration; 48 import org.objectweb.jac.util.Strings; 49 50 53 public class AccCodeEditor extends AbstractCodeEditor 54 { 55 static final Logger logger = Logger.getLogger("ide.editor"); 56 57 JLabel statusLine = new JLabel (); 58 AccCodeStatus codeStatus; 59 ToolParserWrapper parser; 60 AccCompletionEngine ce; 61 62 public AccCodeEditor(Object substance, FieldItem field) { 63 super(substance,field); 64 editor.setConfig(Projects.prefs.getEditorPrefs()); 65 editor.setMinDisplayedLines(10); 66 editor.setWordSeparators( 67 new char [] { '\n', ' ', ',', '(', ')', '{', '}', '[', ']', '/', '-', '+', '*', 68 '<', '>', '=', ';', '"', '\'', '&', '|', '!' }); 69 add(BorderLayout.SOUTH,statusLine); 70 parser = new ToolParserWrapper(); 71 Application application = ((AspectConfiguration)getSubstance()).getApplication(); 72 if (application==null) { 73 application = (Application)Collaboration.get().getAttribute(GuiAC.SUBSTANCE); 74 } 75 76 if (application!=null) { 77 ce = new AccCompletionEngine(parser,application.getProject()); 78 editor.setCompletionEngine(ce); 79 } 80 editor.addCaretListener(new AccCodeStatus()); 81 init(); 82 editor.addTextListener( 83 new TextListener () { 84 public void textValueChanged(TextEvent e) { 85 logger.debug("textValueChanged"); 86 parser.parse(new StringReader (editor.getText()),""); 87 updateStatusLine(); 88 } 89 }); 90 } 91 92 static String [] defaultBlockKeywords = 93 new String [] {"class","member","attribute","method","block"}; 94 95 protected void init() { 96 editor.clearKeywords(); 97 editor.addKeywords(defaultBlockKeywords); 98 if (getSubstance() instanceof AspectConfiguration) { 99 AspectConfiguration config = (AspectConfiguration)getSubstance(); 101 try { 102 if (!Strings.isEmpty(config.getName())) { 103 String acClassName = 104 ACManager.getACM().getACPathFromName(config.getName()); 105 Class acClass = Class.forName(acClassName); 106 aspectClass = ClassRepository.get().getClass(acClassName); 107 AspectComponent acInstance = (AspectComponent)acClass.newInstance(); 108 Set keywords = acInstance.getBlockKeywords(); 109 logger.debug("keywords for "+config.getName()+": "+keywords); 110 editor.addKeywords(keywords); 111 112 if (parser!=null) 113 parser.setBlockKeywords(keywords); 114 if (ce!=null) { 115 ce.setAspectInstance(acInstance); 116 ce.clearBaseWords(); 117 ce.addBaseWords(acInstance.getConfigurationMethodsName()); 118 ce.addBaseWords(acInstance.getBlockKeywords()); 119 ce.addBaseWords(Arrays.asList(defaultBlockKeywords)); 120 } 121 } 122 } catch (Exception e) { 123 logger.error("Failed to init the code editor",e); 124 } 125 } 126 } 127 128 137 138 ClassItem aspectClass; 139 140 143 void updateStatusLine() { 144 SyntaxElement currentElement = parser.getSyntaxElementAt(editor.getCaretPosition()); 145 logger.debug("currentElement = "+currentElement); 146 String newStatus = " "; 147 NonTerminal parent=null; 148 if (currentElement!=null) 149 parent = (NonTerminal)currentElement.findParent("conf_method"); 150 if (parent!=null) { 151 logger.debug("parent = "+parent); 152 String currentName = parent.getName(); 153 Terminal confMethod = 154 (Terminal)parent.getChild("CONF_METHOD"); 155 if (confMethod!=null) { 156 if (aspectClass!=null) { 157 try { 158 MethodItem[] methods = 159 aspectClass.getMethods(confMethod.getValue()); 160 if (methods.length==1) { 161 newStatus = methods[0].getCompactFullName(); 162 } else { 163 newStatus = confMethod.getValue()+"(...)"; 164 } 165 } catch (NoSuchMethodException e) { 166 newStatus = "???"; 167 } 168 } else { 169 newStatus = confMethod.getValue(); 170 } 171 } else { 172 newStatus = currentElement.toString(); 173 } 174 } else if (currentElement!=null) { 175 newStatus = currentElement.toString(); 176 } 177 statusLine.setText(newStatus); 178 } 179 180 class AccCodeStatus implements CaretListener { 181 public AccCodeStatus() { 182 } 183 public void caretUpdate(CaretEvent event) { 184 updateStatusLine(); 185 } 186 } 187 188 public void fieldUpdated(Object object, FieldItem field, 189 Object value, Object param) { 190 init(); 191 super.fieldUpdated(object,field,value,param); 192 } 193 } 194 195 | Popular Tags |