1 18 19 package org.objectweb.jac.ide; 20 21 23 import java.awt.Component ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 import javax.swing.LookAndFeel ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.UIManager ; 33 import javax.swing.UnsupportedLookAndFeelException ; 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 54 public class Gui { 55 static final Logger logger = Logger.getLogger("umlaf.gui"); 56 57 protected static void setLookAndFeel(LookAndFeel look) 58 throws UnsupportedLookAndFeelException 59 { 60 Object d = Collaboration.get().getAttribute("Gui.display"); 61 if (d instanceof CustomizedDisplay) { 62 CustomizedDisplay display = (CustomizedDisplay)d; 63 UIManager.setLookAndFeel(look); 64 Iterator i = display.getCustomizedViews().iterator(); 65 while (i.hasNext()) { 66 Object frame = i.next(); 67 if (frame instanceof Component ) { 68 SwingUtilities.updateComponentTreeUI((Component )frame); 69 } 70 } 71 } 72 73 } 74 75 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 ) { 88 return ((Class )element).getProject(); 89 } 90 return project; 91 } 92 93 100 public static Collection getAvailableTypes(ModelElement element) { 101 Vector result = new Vector (); 102 Project project = getProject(element); 103 Collection types = ObjectRepository.getObjects( 104 ClassRepository.get().getClass("org.objectweb.jac.ide.Type")); 105 Iterator 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 && project!=null && 112 ((Class )type).getProject()!=project)) { 113 result.add(type); 114 } 115 } 116 return result; 117 } 118 119 122 public static Collection getMatchingTypes(ModelElement element, final String search) { 123 Vector result = new Vector (); 124 new Predicate() { 125 public boolean apply(Object o) { 126 return ((ModelElement)o).getName().compareToIgnoreCase(search)==0; 127 } 128 }.filter(getAvailableTypes(element),result); 129 return result; 130 } 131 132 139 public static Collection 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 ("Could not find cache aspect \"ide.cache\""); 150 } 151 152 public static void edit(Attachment attachment, DisplayContext context) 153 throws IOException 154 { 155 String editor = Projects.prefs.getExternalEditor(); 156 if (editor!=null) { 157 new ExternalEditorThread(context,editor,attachment).start(); 158 } else { 159 throw new RuntimeException ( 160 "You must specify an external for your projects"); 161 } 162 } 163 164 public static void editWith(Attachment attachment, 165 DisplayContext context, String editor) 166 throws IOException 167 { 168 new ExternalEditorThread(context,editor,attachment).start(); 169 } 170 171 static class ExternalEditorThread extends Thread { 172 public ExternalEditorThread( 173 DisplayContext context, 174 String editor, Attachment attachment) 175 { 176 this.context = context; 177 this.editor = editor; 178 this.attachment = attachment; 179 } 180 181 DisplayContext context; 182 String editor; 183 Attachment attachment; 184 185 public void run() 186 { 187 try { 188 java.io.File tmpFile = 189 File.createTempFile("UMLAF", 190 attachment.getName()); 191 FileOutputStream out = 192 new FileOutputStream (tmpFile); 193 out.write(attachment.getData()); 194 out.close(); 195 logger.info( 196 "Editing resource "+attachment.getName()+ 197 " with "+editor); 198 Process proc = 199 Runtime.getRuntime().exec( 200 new String [] { 201 editor, 202 tmpFile.getPath() 203 }); 204 int status = proc.waitFor(); 205 if (status!=0) 206 throw new Exception ( 207 "Editor process for "+editor+" failed with status "+status); 208 attachment.setData( 209 Streams.readStream(new FileInputStream (tmpFile))); 210 logger.info( 211 "Resource "+attachment.getName()+" edited ("+status+")"); 212 tmpFile.delete(); 213 } catch (Exception 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 getType(FieldItem field, Attachment attachment) { 225 String 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 getAttachmentIcon(Attachment attachment) { 234 String 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 |