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.Container ; 24 import java.awt.Dimension ; 25 import java.awt.Rectangle ; 26 import java.awt.Toolkit ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.awt.event.ContainerEvent ; 30 import java.awt.event.ContainerListener ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.event.KeyListener ; 33 import java.awt.event.WindowAdapter ; 34 import java.awt.event.WindowEvent ; 35 import java.util.Arrays ; 36 import java.util.Map ; 37 import javax.swing.BorderFactory ; 38 import javax.swing.JButton ; 39 import javax.swing.JDialog ; 40 import javax.swing.JEditorPane ; 41 import javax.swing.JPanel ; 42 import org.apache.log4j.Logger; 43 import org.objectweb.jac.aspects.gui.*; 44 import org.objectweb.jac.core.Collaboration; 45 import org.objectweb.jac.core.rtti.FieldItem; 46 import org.objectweb.jac.core.rtti.MethodItem; 47 import org.objectweb.jac.util.Semaphore; 48 import org.objectweb.jac.util.Strings; 49 50 59 public class Dialog extends JDialog 60 implements ActionListener , KeyListener , ContainerListener , DialogView 61 { 62 static Logger loggerEvents = Logger.getLogger("gui.events"); 63 64 String label; 65 DisplayContext context; 66 Length width; 67 Length height; 68 ViewFactory factory; 69 Object [] parameters; 70 String type; 71 72 boolean ok = false; 73 74 private JButton okButton; 75 private JButton cancelButton; 76 77 Semaphore semaphore = new Semaphore(); 78 String description; 79 View contentView; 80 81 88 public Dialog(View content, Object parent, 89 String title, String description) { 90 this.description = description; 91 this.contentView = content; 92 setModal(true); 93 setTitle(title); 94 95 Container contentPane = getContentPane(); 96 97 addWindowListener( new WindowAdapter () { 98 public void windowClosing(WindowEvent e) { 99 e.getWindow().dispose(); 100 } 101 } 102 ); 103 104 contentPane.add((Component )content, BorderLayout.CENTER); 105 content.setParentView(this); 106 107 if (description != null) 108 { 109 JEditorPane descr = new JEditorPane ("text/plain",description); 110 descr.setEditable(false); 111 descr.setBackground(null); 112 contentPane.add(descr,BorderLayout.NORTH); 113 } 114 115 JPanel p2 = new JPanel (); 117 p2.setBorder(BorderFactory.createEtchedBorder()); 118 okButton = addButton(p2,"Ok"); 119 cancelButton = addButton(p2,"Cancel"); 120 getRootPane().setDefaultButton(okButton); 121 contentPane.add(p2,BorderLayout.SOUTH); 122 pack(); 123 124 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 126 Rectangle rect = getBounds(); 127 double left = (screenDim.getWidth()-rect.getWidth())/2; 128 double top = (screenDim.getHeight()-rect.getHeight())/2; 129 Rectangle newRect = new Rectangle ( 130 (int)left,(int)top, 131 (int)rect.getWidth(),(int)rect.getHeight()); 132 setBounds(newRect); 133 134 addKeyAndContainerListenerRecursively(this); 135 136 attributes = Collaboration.get().getAttributes(); 137 138 setVisible (true); 139 } 141 142 144 Map attributes; 145 146 148 Border viewBorder; 149 150 154 public Border getViewBorder() { 155 return viewBorder; 156 } 157 158 162 public void setViewBorder(Border v) { 163 this.viewBorder = v; 164 } 165 166 String style; 168 169 public void setStyle(String style) { 170 this.style = style; 171 } 172 173 public String getStyle() { 174 return style; 175 } 176 177 MethodItem message; 178 179 183 public MethodItem getMessage() { 184 return message; 185 } 186 187 191 public String getDescription() { 192 return description; 193 } 194 195 199 public void setDescription(String v) { 200 this.description = v; 201 } 202 203 View parentView; 204 205 209 public View getParentView() { 210 return parentView; 211 } 212 213 217 public void setParentView(View v) { 218 this.parentView = v; 219 } 220 221 public View getRootView() { 222 if (parentView==null) 223 return this; 224 return parentView.getRootView(); 225 } 226 227 public boolean isDescendantOf(View ancestor) { 228 if (this==ancestor) 229 return true; 230 else if (parentView==null) 231 return false; 232 else 233 return parentView.isDescendantOf(ancestor); 234 } 235 236 240 public void setMessage(MethodItem v) { 241 this.message = v; 242 } 243 244 public void setContext(DisplayContext context) { 245 this.context = context; 246 } 247 248 public DisplayContext getContext() { 249 return context; 250 } 251 252 public void setFactory(ViewFactory factory) { 253 this.factory = factory; 254 } 255 256 public ViewFactory getFactory() { 257 return factory; 258 } 259 260 public void setLabel(String label) { 261 this.label = label; 262 setTitle(label); 263 } 264 265 public String getLabel() { 266 return label; 267 } 268 269 public void setSize(Length width, Length height) { 270 this.width = width; 271 this.height = height; 272 } 274 275 public void setType(String type) { 276 this.type = type; 277 } 278 279 public String getType() { 280 return type; 281 } 282 283 public void setParameters(Object [] parameters) { 284 this.parameters = parameters; 285 } 286 287 public Object [] getParameters() { 288 return parameters; 289 } 290 291 public boolean equalsView(ViewIdentity view) { 292 return 293 ( ( type!=null && 294 type.equals(view.getType()) ) 295 || (type==null && view.getType()==null ) ) 296 && ( ( parameters!=null && 297 Arrays.equals(parameters,view.getParameters()) ) 298 || (parameters==null && view.getParameters()==null) ); 299 } 300 301 public boolean equalsView(String type, Object [] parameters) { 302 return this.type.equals(type) 303 && Arrays.equals(this.parameters,parameters); 304 } 305 306 public void close(boolean validate) { 307 contentView.close(validate); 308 closed = true; 309 dispose(); 310 } 311 312 boolean closed = false; 313 314 public boolean isClosed() { 315 return closed; 316 } 317 318 public void setFocus(FieldItem field, Object option) { 319 } 320 321 323 public boolean waitForClose() { 324 loggerEvents.debug("waiting for "+Strings.hex(this)+" to be closed"); 325 semaphore.acquire(); 326 loggerEvents.debug("closed "+Strings.hex(this)+" -> "+ok); 327 return ok; 328 } 329 330 public View getContentView() { 331 return contentView; 332 } 333 334 public void restoreContext() { 335 loggerEvents.debug("Restoring attributes: "+attributes.keySet()); 336 Collaboration.get().setAttributes(attributes); 337 } 338 339 342 JButton addButton(Container c, String name) { 343 JButton button = new JButton (name); 344 button.addActionListener(this); 345 c.add(button); 346 return button; 347 } 348 349 355 public void actionPerformed(ActionEvent evt) { 356 try { 357 Object source = evt.getSource(); 358 if (source==okButton) { 359 ok = true; 360 semaphore.release(); 361 } else if (source == cancelButton) { 362 ok = false; 363 semaphore.release(); 364 } 365 dispose(); 366 } catch (Exception e) { 367 e.printStackTrace(); 368 } 369 } 370 371 public void keyPressed(KeyEvent event) { 373 int code = event.getKeyCode(); 374 switch (code) { 375 case KeyEvent.VK_ESCAPE: 376 ok = false; 377 semaphore.release(); 378 dispose(); 379 break; 380 default: 381 } 382 } 383 public void keyTyped(KeyEvent event) {} 384 public void keyReleased(KeyEvent event) {} 385 386 389 public void componentAdded(ContainerEvent event) { 390 addKeyAndContainerListenerRecursively(event.getChild()); 391 } 392 393 398 protected void addKeyAndContainerListenerRecursively(Component c) { 399 c.addKeyListener(this); 400 if (c instanceof Container ) { 401 Container cont = (Container )c; 402 cont.addContainerListener(this); 403 Component [] children = cont.getComponents(); 404 for(int i=0; i<children.length; i++){ 405 addKeyAndContainerListenerRecursively(children[i]); 406 } 407 } 408 } 409 410 public void componentRemoved(ContainerEvent event) { 411 removeKeyAndContainerListenerRecursively(event.getChild()); 412 } 413 414 419 protected void removeKeyAndContainerListenerRecursively(Component c) { 420 c.removeKeyListener(this); 421 if (c instanceof Container ) { 422 Container cont = (Container )c; 423 cont.removeContainerListener(this); 424 Component [] children = cont.getComponents(); 425 for(int i=0; i<children.length; i++){ 426 removeKeyAndContainerListenerRecursively(children[i]); 427 } 428 } 429 } 430 } 431 | Popular Tags |