1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Rectangle ; 25 import java.awt.Toolkit ; 26 import java.awt.event.WindowAdapter ; 27 import java.awt.event.WindowEvent ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import javax.swing.Box ; 33 import javax.swing.ImageIcon ; 34 import javax.swing.JComponent ; 35 import javax.swing.JFrame ; 36 import javax.swing.JMenuBar ; 37 import javax.swing.JPanel ; 38 import org.apache.log4j.Logger; 39 import org.objectweb.jac.aspects.gui.*; 40 import org.objectweb.jac.aspects.gui.Menu; 41 import org.objectweb.jac.core.rtti.FieldItem; 42 import org.objectweb.jac.core.rtti.MethodItem; 43 import org.objectweb.jac.util.Strings; 44 45 46 public class SwingCustomized extends JFrame implements CustomizedView { 47 static Logger logger = Logger.getLogger("display"); 48 static Logger loggerContext = Logger.getLogger("display-context"); 49 50 String label; 51 String type; 52 DisplayContext context; 53 ViewFactory factory; 54 Object [] parameters; 55 56 CustomizedGUI customized; 57 JPanel contentPanel; 58 PanelView mainView; 59 60 public SwingCustomized(ViewFactory factory, DisplayContext context, 61 CustomizedGUI customized) { 62 this.factory = factory; 63 this.customized = customized; 64 this.context = context; 65 context.setCustomizedView(this); 66 contentPanel = new JPanel (); 67 contentPanel.setLayout(new BorderLayout ()); 68 setContentPane(contentPanel); 69 try { 70 mainView = (PanelView)factory.createCompositeView( 71 "main", 72 "Panel", 73 new Object [] { 74 new Integer (customized.getSubPanesCount()), 75 new Integer (customized.getGeometry()), 76 customized.getPaneContainers(), 77 customized.getScrollings(), 78 customized.getSplitters() }, 79 context 80 ); 81 contentPanel.add((JComponent )mainView,BorderLayout.CENTER); 82 } catch (ViewFactory.UnhandledViewTypeException e) { 83 e.printStackTrace(); 84 } 85 86 GenericFactory.initCustomized(factory, context, mainView, customized, null); 87 GenericFactory.setMenuBars(factory,context,this,customized.getMenus()); 88 GenericFactory.setToolBar(factory,context,this,customized.getToolbar()); 89 90 if (customized.hasStatusBar()) 91 GenericFactory.setStatusBar(factory, context, 92 this, customized.getStatusBarMethod(), 93 customized.getStatusPosition()); 94 95 addMouseListener(CollaborationInitializer.get()); 96 97 this.addWindowListener( 98 new WindowAdapter () { 99 public void windowClosing(WindowEvent e) { 100 close(true); 101 if(getCustomized().getOnCloseHandler()!=null) { 102 EventHandler.get().onInvoke( 103 getContext(), 104 new InvokeEvent( 105 SwingCustomized.this, 106 null, 107 getCustomized().getOnCloseHandler()), 108 false, 109 null,null); 110 } 111 GuiAC.removeDisplay( 112 GuiAC.getDisplay(getCustomized().getId())); 113 114 116 } 117 } 118 ); 119 120 if (customized.getIcon()!=null) { 121 ImageIcon icon = ResourceManager.getIconResource(customized.getIcon()); 122 if (icon!=null) 123 setIconImage(icon.getImage()); 124 } 125 126 pack(); 127 128 if (customized.isGeometrySet()) { 129 setPosition(customized.getLeft(),customized.getUp(), 130 customized.getWidth(),customized.getHeight()); 131 } 132 } 133 134 public void addHorizontalStrut(int width) {} 135 public void addVerticalStrut(int height) {} 136 137 public void setSplitters() { 138 Iterator i = customized.getSplitters().entrySet().iterator(); 139 while(i.hasNext()) { 140 Map.Entry entry = (Map.Entry )i.next(); 141 mainView.setSplitterLocation(((Integer )entry.getKey()).intValue(), 142 ((Float )entry.getValue()).floatValue()); 143 } 144 } 145 146 154 155 public void setPosition(int left, int up, int width, int height) { 156 logger.debug("setPosition("+left+","+up+","+width+","+height+")"); 157 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 158 int w = (int) ((float)screenDim.getWidth()*width)/100; 159 int h = (int) ((float)screenDim.getHeight()*height)/100; 160 logger.debug("dimension = "+w+"x"+h); 161 setBounds(new Rectangle ( 162 left,up, 163 (int)(((float)screenDim.getWidth()*width)/100), 164 (int)(((float)screenDim.getHeight()*height)/100))); 165 } 166 167 168 public void close(boolean validate) { 169 mainView.close(validate); 170 closed = true; 171 dispose(); 172 } 173 174 boolean closed = false; 175 176 public boolean isClosed() { 177 return closed; 178 } 179 180 182 Border viewBorder; 183 184 188 public Border getViewBorder() { 189 return viewBorder; 190 } 191 192 196 public void setViewBorder(Border v) { 197 this.viewBorder = v; 198 } 199 200 String style; 202 203 public void setStyle(String style) { 204 this.style = style; 205 } 206 207 public String getStyle() { 208 return style; 209 } 210 211 212 String description; 213 214 218 public String getDescription() { 219 return description; 220 } 221 222 226 public void setDescription(String v) { 227 this.description = v; 228 } 229 230 View parentView; 231 232 236 public View getParentView() { 237 return parentView; 238 } 239 240 244 public void setParentView(View v) { 245 this.parentView = v; 246 } 247 248 public View getRootView() { 249 if (parentView==null) 250 return this; 251 return parentView.getRootView(); 252 } 253 254 public boolean isDescendantOf(View ancestor) { 255 if (this==ancestor) 256 return true; 257 else if (parentView==null) 258 return false; 259 else 260 return parentView.isDescendantOf(ancestor); 261 } 262 263 MethodItem message; 264 265 269 public MethodItem getMessage() { 270 return message; 271 } 272 273 277 public void setMessage(MethodItem v) { 278 this.message = v; 279 } 280 281 public void setLabel(String label) { 282 setTitle(label); 283 } 284 285 public String getLabel() { 286 return getTitle(); 287 } 288 289 public void setFactory(ViewFactory factory) { 290 this.factory = factory; 291 } 292 293 public ViewFactory getFactory() { 294 return factory; 295 } 296 297 public void setContext(DisplayContext context) { 298 loggerContext.debug("setContext on "+getClass().getName()); 299 this.context = context; 300 Iterator i = mainView.getViews().iterator(); 302 while (i.hasNext()) { 303 View view = (View)i.next(); 304 loggerContext.debug("set context on subView "+view); 305 view.setContext(context); 306 } 307 } 308 309 public void setSize(Length width, Length height) { 310 } 312 313 public void setParameters(Object [] parameters) { 314 this.parameters = parameters; 315 } 316 317 public Object [] getParameters() { 318 return parameters; 319 } 320 321 public void setType(String type) { 322 this.type = type; 323 } 324 325 public String getType() { 326 return type; 327 } 328 329 public boolean equalsView(ViewIdentity view) { 330 return 331 ( ( type!=null && 332 type.equals(view.getType()) ) 333 || (type==null && view.getType()==null ) ) 334 && ( ( parameters!=null && 335 Arrays.equals(parameters,view.getParameters()) ) 336 || (parameters==null && view.getParameters()==null) ); 337 } 338 339 public boolean equalsView(String type, Object [] parameters) { 340 return this.type.equals(type) 341 && Arrays.equals(this.parameters,parameters); 342 } 343 344 public void addView(View view, Object extraInfos) { 345 view.setContext(context); 346 mainView.addView(view,extraInfos); 347 } 348 349 public void addView(View view) { 350 addView(view,null); 351 } 352 353 public Collection getViews() { 354 return mainView.getViews(); 355 } 356 357 public View getView(Object id) { 358 return mainView.getView(id); 359 } 360 361 public void removeView(View component, boolean validate) 362 { 363 mainView.removeView(component, validate); 364 } 365 366 public void removeAllViews(boolean validate) { 367 mainView.removeAllViews(validate); 368 } 369 370 public boolean containsView(String viewType, Object [] parameters) { 371 Iterator it = getViews().iterator(); 372 while (it.hasNext()) { 373 View view = (View)it.next(); 374 if (view.equalsView(viewType,parameters)) 375 return true; 376 } 377 return false; 378 } 379 380 public void setFocus(FieldItem field, Object option) { 381 } 382 383 385 public CustomizedGUI getCustomizedGUI() { 386 return customized; 387 } 388 389 public void setMenuBar(MenuView menuBar,String position) { 390 if (position==null) { 391 position = Menu.TOP; 392 } 393 menuBar.setPosition(position); 394 if(position.equals(Menu.TOP)) { 395 setJMenuBar((JMenuBar )menuBar); 396 } else { 397 if(position.equals(Menu.BOTTOM)) { 398 getContentPane().add((JMenuBar )menuBar,BorderLayout.SOUTH); 399 } else if(position.equals(Menu.LEFT)) { 400 getContentPane().add((JMenuBar )menuBar,BorderLayout.WEST); 401 } else if(position.equals(Menu.RIGHT)) { 402 getContentPane().add((JMenuBar )menuBar,BorderLayout.EAST); 403 } 404 } 405 if(position.equals(Menu.LEFT)||position.equals(Menu.BOTTOM)) { 406 for(int i=0;i<50;i++) { 408 ((JMenuBar )menuBar).add(Box.createVerticalGlue()); 409 } 410 } 411 } 412 413 public void setToolBar(MenuView toolBar) { 414 contentPanel.add((Component )toolBar,BorderLayout.NORTH); 415 } 416 417 StatusView statusView; 418 419 public void setStatusBar(StatusView view, String position) { 420 statusView=view; 421 contentPanel.add((Component )view,BorderLayout.SOUTH); 422 } 423 424 public void showStatus(String message) { 425 statusView.showMessage(message); 426 } 427 428 public PanelView getPanelView() { 429 return mainView; 430 } 431 432 public DisplayContext getContext() { 433 return context; 434 } 435 436 CustomizedGUI getCustomized() { 437 return customized; 438 } 439 440 public void requestFocus() { 441 show(); 442 super.requestFocus(); 443 } 444 445 public String toString() { 446 return Strings.hex(this); 447 } 448 } 449 450 | Popular Tags |