KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > swing > AccCodeEditor


1 /*
2   Copyright (C) 2003 Renaud Pawlak <renaud@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide.swing;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.TextEvent JavaDoc;
23 import java.awt.event.TextListener JavaDoc;
24 import java.io.StringReader JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.event.CaretEvent JavaDoc;
29 import javax.swing.event.CaretListener JavaDoc;
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 /**
51  * A Java source code editor
52  */

53 public class AccCodeEditor extends AbstractCodeEditor
54 {
55     static final Logger logger = Logger.getLogger("ide.editor");
56
57     JLabel JavaDoc statusLine = new JLabel JavaDoc();
58     AccCodeStatus codeStatus;
59     ToolParserWrapper parser;
60     AccCompletionEngine ce;
61
62     public AccCodeEditor(Object JavaDoc 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 JavaDoc() {
84                     public void textValueChanged(TextEvent JavaDoc e) {
85                         logger.debug("textValueChanged");
86                         parser.parse(new StringReader JavaDoc(editor.getText()),"");
87                         updateStatusLine();
88                     }
89                 });
90     }
91
92     static String JavaDoc[] defaultBlockKeywords =
93         new String JavaDoc[] {"class","member","attribute","method","block"};
94
95     protected void init() {
96         editor.clearKeywords();
97         editor.addKeywords(defaultBlockKeywords);
98         if (getSubstance() instanceof AspectConfiguration) {
99             // instanciate the aspect so that we can know its block keywords
100
AspectConfiguration config = (AspectConfiguration)getSubstance();
101             try {
102                 if (!Strings.isEmpty(config.getName())) {
103                     String JavaDoc acClassName =
104                         ACManager.getACM().getACPathFromName(config.getName());
105                     Class JavaDoc acClass = Class.forName(acClassName);
106                     aspectClass = ClassRepository.get().getClass(acClassName);
107                     AspectComponent acInstance = (AspectComponent)acClass.newInstance();
108                     Set JavaDoc 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 JavaDoc e) {
123                 logger.error("Failed to init the code editor",e);
124             }
125         }
126     }
127
128     /*
129       public void setValue(Object value) {
130       super.setValue(value);
131       init();
132       if (parser!=null) {
133       parser.parse(new StringReader(editor.getText()),"");
134       }
135       }
136     */

137
138     ClassItem aspectClass;
139
140     /**
141      * Updates the "modeline" to display the current configuration method
142      */

143     void updateStatusLine() {
144         SyntaxElement currentElement = parser.getSyntaxElementAt(editor.getCaretPosition());
145         logger.debug("currentElement = "+currentElement);
146         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
181         public AccCodeStatus() {
182         }
183         public void caretUpdate(CaretEvent JavaDoc event) {
184             updateStatusLine();
185         }
186     }
187
188     public void fieldUpdated(Object JavaDoc object, FieldItem field,
189                              Object JavaDoc value, Object JavaDoc param) {
190         init();
191         super.fieldUpdated(object,field,value,param);
192     }
193 }
194
195
Popular Tags