001 /* 002 Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, but 010 WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.aspects.gui.swing; 020 021 import java.util.Arrays; 022 import java.util.Collection; 023 import java.util.Hashtable; 024 import java.util.Iterator; 025 import javax.swing.JComponent; 026 import javax.swing.JDesktopPane; 027 import javax.swing.JInternalFrame; 028 import org.apache.log4j.Logger; 029 import org.objectweb.jac.aspects.gui.Border; 030 import org.objectweb.jac.aspects.gui.CompositeView; 031 import org.objectweb.jac.aspects.gui.DisplayContext; 032 import org.objectweb.jac.aspects.gui.View; 033 import org.objectweb.jac.aspects.gui.ViewFactory; 034 import org.objectweb.jac.aspects.gui.ViewIdentity; 035 import org.objectweb.jac.core.rtti.FieldItem; 036 import org.objectweb.jac.core.rtti.MethodItem; 037 038 /** 039 * This class defines a Swing component tree view for objects that are 040 * related to a root object through relations or collections. 041 */ 042 043 public class DesktopView extends JDesktopPane implements CompositeView { 044 static Logger logger = Logger.getLogger("gui.swing"); 045 046 Hashtable views = new Hashtable(); 047 048 DisplayContext context; 049 int width; 050 int height; 051 ViewFactory factory; 052 Object[] parameters; 053 String type; 054 String label; 055 056 public DesktopView() { 057 } 058 059 public void addHorizontalStrut(int width) {} 060 public void addVerticalStrut(int height) {} 061 062 // style used to change display (css for web) 063 String style; 064 065 public void setStyle(String style) { 066 this.style = style; 067 } 068 069 public String getStyle() { 070 return style; 071 } 072 073 Border viewBorder; 074 075 /** 076 * Get the value of viewBorder. 077 * @return value of viewBorder. 078 */ 079 public Border getViewBorder() { 080 return viewBorder; 081 } 082 083 /** 084 * Set the value of viewBorder. 085 * @param v Value to assign to viewBorder. 086 */ 087 public void setViewBorder(Border v) { 088 this.viewBorder = v; 089 } 090 091 String description; 092 093 /** 094 * Get the value of description. 095 * @return value of description. 096 */ 097 public String getDescription() { 098 return description; 099 } 100 101 /** 102 * Set the value of description. 103 * @param v Value to assign to description. 104 */ 105 public void setDescription(String v) { 106 this.description = v; 107 } 108 109 View parentView; 110 111 /** 112 * Get the value of parentView. 113 * @return value of parentView. 114 */ 115 public View getParentView() { 116 return parentView; 117 } 118 119 /** 120 * Set the value of parentView. 121 * @param v Value to assign to parentView. 122 */ 123 public void setParentView(View v) { 124 this.parentView = v; 125 } 126 127 public View getRootView() { 128 if (parentView==null) 129 return this; 130 return parentView.getRootView(); 131 } 132 133 public boolean isDescendantOf(View ancestor) { 134 if (this==ancestor) 135 return true; 136 else if (parentView==null) 137 return false; 138 else 139 return parentView.isDescendantOf(ancestor); 140 } 141 142 MethodItem message; 143 144 /** 145 * Get the value of message. 146 * @return value of message. 147 */ 148 public MethodItem getMessage() { 149 return message; 150 } 151 152 /** 153 * Set the value of message. 154 * @param v Value to assign to message. 155 */ 156 public void setMessage(MethodItem v) { 157 this.message = v; 158 } 159 160 161 // CompositeView interface 162 163 public void addView(View view, Object extraInfos) { 164 logger.debug("Adding view in desktop"); 165 JInternalFrame frame=new JInternalFrame(); 166 frame.getContentPane().add((JComponent)view); 167 frame.setVisible(true); //necessary as of kestrel 168 frame.setTitle((String)extraInfos); 169 frame.setResizable(true); 170 frame.setClosable(true); 171 frame.setIconifiable(true); 172 frame.setMaximizable(true); 173 add(frame); 174 try { 175 frame.setSelected(true); 176 } catch (java.beans.PropertyVetoException e) {} 177 views.put(extraInfos,view); 178 frame.pack(); 179 frame.show(); 180 } 181 182 public void addView(View view) { 183 addView(view,view.getLabel()); 184 } 185 186 public void removeView(View component, boolean validate) { 187 for (int i=0; i<getComponentCount(); i++) { 188 JInternalFrame frame = (JInternalFrame)getComponent(i); 189 if (frame.getContentPane().getComponent(0).equals(component)) { 190 component.close(validate); 191 remove(frame); 192 } 193 } 194 } 195 196 public View getView(Object id) { 197 return (View)views.get(id); 198 } 199 200 public Collection getViews() { 201 return views.values(); 202 } 203 204 public void removeAllViews(boolean validate) { 205 Iterator i = views.values().iterator(); 206 while (i.hasNext()) { 207 ((View)i.next()).close(validate); 208 } 209 removeAll(); 210 } 211 212 public boolean containsView(String viewType, Object[] parameters) { 213 Iterator it = getViews().iterator(); 214 while (it.hasNext()) { 215 View view = (View)it.next(); 216 if (view.equalsView(viewType,parameters)) 217 return true; 218 } 219 return false; 220 } 221 222 // View interface 223 224 public void setContext(DisplayContext context) { 225 this.context = context; 226 } 227 228 public DisplayContext getContext() { 229 return context; 230 } 231 232 public void setLabel(String label) { 233 this.label = label; 234 } 235 236 public String getLabel() { 237 return label; 238 } 239 240 public void setWidth(int width) { 241 this.width = width; 242 } 243 244 public void setHeight(int height) { 245 this.height = height; 246 } 247 248 public void close(boolean validate) { 249 closed = true; 250 } 251 252 boolean closed = false; 253 254 public boolean isClosed() { 255 return closed; 256 } 257 258 public ViewFactory getFactory() { 259 return factory; 260 } 261 262 public void setFactory(ViewFactory factory) { 263 this.factory = factory; 264 } 265 266 public void setType(String type) { 267 this.type = type; 268 } 269 270 public String getType() { 271 return type; 272 } 273 274 public void setParameters(Object[] parameters) { 275 this.parameters = parameters; 276 } 277 278 public Object[] getParameters() { 279 return parameters; 280 } 281 282 public boolean equalsView(ViewIdentity view) { 283 return 284 ( ( type!=null && 285 type.equals(view.getType()) ) 286 || (type==null && view.getType()==null ) ) 287 && ( ( parameters!=null && 288 Arrays.equals(parameters,view.getParameters()) ) 289 || (parameters==null && view.getParameters()==null) ); 290 } 291 292 public boolean equalsView(String type, Object[] parameters) { 293 return this.type.equals(type) 294 && Arrays.equals(this.parameters,parameters); 295 } 296 297 public void setFocus(FieldItem field, Object option) { 298 } 299 300 public String toString() { 301 return getClass().getName()+"@"+Integer.toString(hashCode()); 302 } 303 304 }