001 /* 002 Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>, 003 Laurent Martelli <laurent@aopsys.com> 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This program is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 GNU Lesser General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public License 016 along with this program; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 018 019 package org.objectweb.jac.aspects.gui.swing; 020 021 import java.awt.BorderLayout; 022 import java.awt.Component; 023 import java.awt.Dimension; 024 import java.awt.Rectangle; 025 import java.awt.Toolkit; 026 import java.awt.event.WindowAdapter; 027 import java.awt.event.WindowEvent; 028 import java.util.Arrays; 029 import java.util.Collection; 030 import java.util.Iterator; 031 import java.util.Map; 032 import javax.swing.Box; 033 import javax.swing.ImageIcon; 034 import javax.swing.JComponent; 035 import javax.swing.JFrame; 036 import javax.swing.JMenuBar; 037 import javax.swing.JPanel; 038 import org.apache.log4j.Logger; 039 import org.objectweb.jac.aspects.gui.*; 040 import org.objectweb.jac.aspects.gui.Menu; 041 import org.objectweb.jac.core.rtti.FieldItem; 042 import org.objectweb.jac.core.rtti.MethodItem; 043 import org.objectweb.jac.util.Strings; 044 045 046 public class SwingCustomized extends JFrame implements CustomizedView { 047 static Logger logger = Logger.getLogger("display"); 048 static Logger loggerContext = Logger.getLogger("display-context"); 049 050 String label; 051 String type; 052 DisplayContext context; 053 ViewFactory factory; 054 Object[] parameters; 055 056 CustomizedGUI customized; 057 JPanel contentPanel; 058 PanelView mainView; 059 060 public SwingCustomized(ViewFactory factory, DisplayContext context, 061 CustomizedGUI customized) { 062 this.factory = factory; 063 this.customized = customized; 064 this.context = context; 065 context.setCustomizedView(this); 066 contentPanel = new JPanel(); 067 contentPanel.setLayout(new BorderLayout()); 068 setContentPane(contentPanel); 069 try { 070 mainView = (PanelView)factory.createCompositeView( 071 "main", 072 "Panel", 073 new Object[] { 074 new Integer(customized.getSubPanesCount()), 075 new Integer(customized.getGeometry()), 076 customized.getPaneContainers(), 077 customized.getScrollings(), 078 customized.getSplitters() }, 079 context 080 ); 081 contentPanel.add((JComponent)mainView,BorderLayout.CENTER); 082 } catch (ViewFactory.UnhandledViewTypeException e) { 083 e.printStackTrace(); 084 } 085 086 GenericFactory.initCustomized(factory, context, mainView, customized, null); 087 GenericFactory.setMenuBars(factory,context,this,customized.getMenus()); 088 GenericFactory.setToolBar(factory,context,this,customized.getToolbar()); 089 090 if (customized.hasStatusBar()) 091 GenericFactory.setStatusBar(factory, context, 092 this, customized.getStatusBarMethod(), 093 customized.getStatusPosition()); 094 095 addMouseListener(CollaborationInitializer.get()); 096 097 this.addWindowListener( 098 new WindowAdapter() { 099 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 //((GuiAC)ACManager.get().getAC("gui")) 115 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 /** 147 * Sets the dimensions and position of the window regarding to the 148 * main screen. 149 * 150 * @param left left-border pixel 151 * @param up upper-border pixel 152 * @param width in percentage regarding the screen 153 * @param height in percentage regarding the screen */ 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 // View interface 181 182 Border viewBorder; 183 184 /** 185 * Get the value of viewBorder. 186 * @return value of viewBorder. 187 */ 188 public Border getViewBorder() { 189 return viewBorder; 190 } 191 192 /** 193 * Set the value of viewBorder. 194 * @param v Value to assign to viewBorder. 195 */ 196 public void setViewBorder(Border v) { 197 this.viewBorder = v; 198 } 199 200 // style used to change display (css for web) 201 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 /** 215 * Get the value of description. 216 * @return value of description. 217 */ 218 public String getDescription() { 219 return description; 220 } 221 222 /** 223 * Set the value of description. 224 * @param v Value to assign to description. 225 */ 226 public void setDescription(String v) { 227 this.description = v; 228 } 229 230 View parentView; 231 232 /** 233 * Get the value of parentView. 234 * @return value of parentView. 235 */ 236 public View getParentView() { 237 return parentView; 238 } 239 240 /** 241 * Set the value of parentView. 242 * @param v Value to assign to parentView. 243 */ 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 /** 266 * Get the value of message. 267 * @return value of message. 268 */ 269 public MethodItem getMessage() { 270 return message; 271 } 272 273 /** 274 * Set the value of message. 275 * @param v Value to assign to message. 276 */ 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 // recursively set the display of inner components 301 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 setWidth(int width) { 310 } 311 312 public void setHeight(int height) { 313 } 314 315 public void setParameters(Object[] parameters) { 316 this.parameters = parameters; 317 } 318 319 public Object[] getParameters() { 320 return parameters; 321 } 322 323 public void setType(String type) { 324 this.type = type; 325 } 326 327 public String getType() { 328 return type; 329 } 330 331 public boolean equalsView(ViewIdentity view) { 332 return 333 ( ( type!=null && 334 type.equals(view.getType()) ) 335 || (type==null && view.getType()==null ) ) 336 && ( ( parameters!=null && 337 Arrays.equals(parameters,view.getParameters()) ) 338 || (parameters==null && view.getParameters()==null) ); 339 } 340 341 public boolean equalsView(String type, Object[] parameters) { 342 return this.type.equals(type) 343 && Arrays.equals(this.parameters,parameters); 344 } 345 346 public void addView(View view, Object extraInfos) { 347 view.setContext(context); 348 mainView.addView(view,extraInfos); 349 } 350 351 public void addView(View view) { 352 addView(view,null); 353 } 354 355 public Collection getViews() { 356 return mainView.getViews(); 357 } 358 359 public View getView(Object id) { 360 return mainView.getView(id); 361 } 362 363 public void removeView(View component, boolean validate) 364 { 365 mainView.removeView(component, validate); 366 } 367 368 public void removeAllViews(boolean validate) { 369 mainView.removeAllViews(validate); 370 } 371 372 public boolean containsView(String viewType, Object[] parameters) { 373 Iterator it = getViews().iterator(); 374 while (it.hasNext()) { 375 View view = (View)it.next(); 376 if (view.equalsView(viewType,parameters)) 377 return true; 378 } 379 return false; 380 } 381 382 public void setFocus(FieldItem field, Object option) { 383 } 384 385 // CustomizedView interface 386 387 public CustomizedGUI getCustomizedGUI() { 388 return customized; 389 } 390 391 public void setMenuBar(MenuView menuBar,String position) { 392 if (position==null) { 393 position = Menu.TOP; 394 } 395 menuBar.setPosition(position); 396 if(position.equals(Menu.TOP)) { 397 setJMenuBar((JMenuBar)menuBar); 398 } else { 399 if(position.equals(Menu.BOTTOM)) { 400 getContentPane().add((JMenuBar)menuBar,BorderLayout.SOUTH); 401 } else if(position.equals(Menu.LEFT)) { 402 getContentPane().add((JMenuBar)menuBar,BorderLayout.WEST); 403 } else if(position.equals(Menu.RIGHT)) { 404 getContentPane().add((JMenuBar)menuBar,BorderLayout.EAST); 405 } 406 } 407 if(position.equals(Menu.LEFT)||position.equals(Menu.BOTTOM)) { 408 for(int i=0;i<50;i++) { 409 ((JMenuBar)menuBar).add(Box.createVerticalGlue()); 410 } 411 } 412 } 413 414 public void setToolBar(MenuView toolBar) { 415 contentPanel.add((Component)toolBar,BorderLayout.NORTH); 416 } 417 418 StatusView statusView; 419 420 public void setStatusBar(StatusView view, String position) { 421 statusView=view; 422 contentPanel.add((Component)view,BorderLayout.SOUTH); 423 } 424 425 public void showStatus(String message) { 426 statusView.showMessage(message); 427 } 428 429 public PanelView getPanelView() { 430 return mainView; 431 } 432 433 public DisplayContext getContext() { 434 return context; 435 } 436 437 CustomizedGUI getCustomized() { 438 return customized; 439 } 440 441 public void requestFocus() { 442 show(); 443 super.requestFocus(); 444 } 445 446 public String toString() { 447 return Strings.hex(this); 448 } 449 } 450