KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > Gui


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@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,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU 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;
20
21 // Look and feels
22

23 import java.awt.Component JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Vector JavaDoc;
30 import javax.swing.LookAndFeel JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.UIManager JavaDoc;
33 import javax.swing.UnsupportedLookAndFeelException JavaDoc;
34 import org.apache.log4j.Logger;
35 import org.objectweb.jac.aspects.cache.CacheAC;
36 import org.objectweb.jac.aspects.gui.CustomizedDisplay;
37 import org.objectweb.jac.aspects.gui.DisplayContext;
38 import org.objectweb.jac.aspects.gui.ResourceManager;
39 import org.objectweb.jac.core.ACManager;
40 import org.objectweb.jac.core.Collaboration;
41 import org.objectweb.jac.core.Display;
42 import org.objectweb.jac.core.ObjectRepository;
43 import org.objectweb.jac.core.rtti.ClassRepository;
44 import org.objectweb.jac.core.rtti.FieldItem;
45 import org.objectweb.jac.lib.Attachment;
46 import org.objectweb.jac.util.File;
47 import org.objectweb.jac.util.Predicate;
48 import org.objectweb.jac.util.Streams;
49 import org.objectweb.jac.aspects.gui.GuiAC;
50
51 /**
52  * Gui methods for the menus.
53  */

54 public class Gui {
55     static final Logger logger = Logger.getLogger("umlaf.gui");
56
57     protected static void setLookAndFeel(LookAndFeel JavaDoc look)
58         throws UnsupportedLookAndFeelException JavaDoc
59     {
60         Object JavaDoc d = Collaboration.get().getAttribute("Gui.display");
61         if (d instanceof CustomizedDisplay) {
62             CustomizedDisplay display = (CustomizedDisplay)d;
63             UIManager.setLookAndFeel(look);
64             Iterator JavaDoc i = display.getCustomizedViews().iterator();
65             while (i.hasNext()) {
66                 Object JavaDoc frame = i.next();
67                 if (frame instanceof Component JavaDoc) {
68                     SwingUtilities.updateComponentTreeUI((Component JavaDoc)frame);
69                 }
70             }
71         }
72       
73     }
74
75     /**
76      * Gets the project an element belongs to
77      * @param element the element whose projet to return
78      */

79     protected static Project getProject(ModelElement element) {
80         Project project = null;
81         if (element instanceof Member) {
82             project = ((Member)element).getProject();
83         } else if (element instanceof Parameter) {
84             Method method = ((Parameter)element).getMethod();
85             if (method!=null)
86                 project = method.getProject();
87         } else if (element instanceof Class JavaDoc) {
88             return ((Class JavaDoc)element).getProject();
89         }
90         return project;
91     }
92
93     /**
94      * Returns all types which belong to the same project as a
95      * given member.
96      * @param element the element
97      * @see #getAvailableClasses(ModelElement)
98      * @see #getMatchingTypes(ModelElement,String)
99      */

100     public static Collection JavaDoc getAvailableTypes(ModelElement element) {
101         Vector JavaDoc result = new Vector JavaDoc();
102         Project project = getProject(element);
103         Collection JavaDoc types = ObjectRepository.getObjects(
104             ClassRepository.get().getClass("org.objectweb.jac.ide.Type"));
105         Iterator JavaDoc it = types.iterator();
106         while (it.hasNext()) {
107             Type type = (Type)it.next();
108             if (((element instanceof Field) || (element instanceof Parameter)) &&
109                 type==Projects.types.resolveType("void"))
110                 continue;
111             if (!(type instanceof Class JavaDoc && project!=null &&
112                   ((Class JavaDoc)type).getProject()!=project)) {
113                 result.add(type);
114             }
115         }
116         return result;
117     }
118
119     /**
120      * Returns types with a given short name
121      */

122     public static Collection JavaDoc getMatchingTypes(ModelElement element, final String JavaDoc search) {
123         Vector JavaDoc result = new Vector JavaDoc();
124         new Predicate() {
125                 public boolean apply(Object JavaDoc o) {
126                     return ((ModelElement)o).getName().compareToIgnoreCase(search)==0;
127                 }
128             }.filter(getAvailableTypes(element),result);
129         return result;
130     }
131
132     /**
133      * Returns all classes which belong to the same project as a
134      * given member.
135      * @param element the element
136      * @see #getAvailableTypes(ModelElement)
137      * @see #getMatchingTypes(ModelElement,String)
138      */

139     public static Collection JavaDoc getAvailableClasses(ModelElement element) {
140         Project project = getProject(element);
141         return project.getClasses();
142     }
143
144     public static void invalidateCache() {
145         CacheAC cacheAspect = (CacheAC)ACManager.getACM().getACFromFullName("ide.cache");
146         if (cacheAspect!=null)
147             cacheAspect.invalidateCache();
148         else
149             throw new RuntimeException JavaDoc("Could not find cache aspect \"ide.cache\"");
150     }
151
152     public static void edit(Attachment attachment, DisplayContext context)
153         throws IOException JavaDoc
154     {
155         String JavaDoc editor = Projects.prefs.getExternalEditor();
156         if (editor!=null) {
157             new ExternalEditorThread(context,editor,attachment).start();
158         } else {
159             throw new RuntimeException JavaDoc(
160                 "You must specify an external for your projects");
161         }
162     }
163
164     public static void editWith(Attachment attachment,
165                                 DisplayContext context, String JavaDoc editor)
166         throws IOException JavaDoc
167     {
168         new ExternalEditorThread(context,editor,attachment).start();
169     }
170
171     static class ExternalEditorThread extends Thread JavaDoc {
172         public ExternalEditorThread(
173             DisplayContext context,
174             String JavaDoc editor, Attachment attachment)
175         {
176             this.context = context;
177             this.editor = editor;
178             this.attachment = attachment;
179         }
180         
181         DisplayContext context;
182         String JavaDoc editor;
183         Attachment attachment;
184
185         public void run()
186         {
187             try {
188                 java.io.File JavaDoc tmpFile =
189                     File.createTempFile("UMLAF",
190                                         attachment.getName());
191                 FileOutputStream JavaDoc out =
192                     new FileOutputStream JavaDoc(tmpFile);
193                 out.write(attachment.getData());
194                 out.close();
195                 logger.info(
196                     "Editing resource "+attachment.getName()+
197                     " with "+editor);
198                 Process JavaDoc proc =
199                     Runtime.getRuntime().exec(
200                         new String JavaDoc[] {
201                             editor,
202                             tmpFile.getPath()
203                         });
204                 int status = proc.waitFor();
205                 if (status!=0)
206                     throw new Exception JavaDoc(
207                         "Editor process for "+editor+" failed with status "+status);
208                 attachment.setData(
209                     Streams.readStream(new FileInputStream JavaDoc(tmpFile)));
210                 logger.info(
211                     "Resource "+attachment.getName()+" edited ("+status+")");
212                 tmpFile.delete();
213             } catch (Exception JavaDoc e) {
214                 logger.error(
215                     "Failed to edit resource "+attachment.getName(),e);
216                 Collaboration.get().addAttribute(GuiAC.DISPLAY_CONTEXT,context);
217                 context.getDisplay().showError(
218                     "Failed to edit resource "+attachment.getName(),
219                     e.toString());
220             }
221         }
222     }
223
224     public static Object JavaDoc getType(FieldItem field, Attachment attachment) {
225         String JavaDoc type = attachment.getMimeType();
226         ClassRepository cr = ClassRepository.get();
227         if ("text/x-java".equals(type)) {
228             return "javaCode";
229         }
230         return Attachment.getType(field,attachment);
231     }
232
233     public static String JavaDoc getAttachmentIcon(Attachment attachment) {
234         String JavaDoc type = attachment.getMimeType();
235         ClassRepository cr = ClassRepository.get();
236         if ("text/x-java".equals(type)) {
237             return ResourceManager.getResource("icon_class");
238         } else {
239             return null;
240         }
241     }
242 }
243
244
Popular Tags