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.Dialog ; 25 import java.awt.Dimension ; 26 import java.awt.Frame ; 27 import java.awt.Rectangle ; 28 import java.awt.Toolkit ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.ContainerEvent ; 32 import java.awt.event.ContainerListener ; 33 import java.awt.event.KeyEvent ; 34 import java.awt.event.KeyListener ; 35 import java.awt.event.WindowAdapter ; 36 import java.awt.event.WindowEvent ; 37 import java.util.Map ; 38 import javax.swing.BorderFactory ; 39 import javax.swing.JButton ; 40 import javax.swing.JDialog ; 41 import javax.swing.JLabel ; 42 import javax.swing.JPanel ; 43 import javax.swing.SwingConstants ; 44 import org.apache.log4j.Logger; 45 import org.objectweb.jac.aspects.gui.CommitException; 46 import org.objectweb.jac.aspects.gui.DialogView; 47 import org.objectweb.jac.aspects.gui.DisplayContext; 48 import org.objectweb.jac.aspects.gui.GuiAC; 49 import org.objectweb.jac.aspects.gui.View; 50 import org.objectweb.jac.core.Collaboration; 51 import org.objectweb.jac.util.Strings; 52 53 public class ObjectViewDialog extends JDialog 54 implements ActionListener , KeyListener , ContainerListener 55 { 56 static Logger loggerEvents = Logger.getLogger("gui.events"); 57 static Logger loggerFocus = Logger.getLogger("gui.focus"); 58 59 public boolean ok=false; 60 JButton okButton; 61 JButton cancelButton; 62 JButton closeButton; 63 View objectView; 64 protected DisplayContext context; 65 66 68 Map attributes; 69 70 public ObjectViewDialog(View view, 71 String title, String header, 72 Frame parent, 73 boolean okay, boolean cancel, boolean close, 74 DisplayContext context) { 75 super(parent); 76 setModal(parent!=null); 77 context.setWindow(this); 78 init(view,title,header,okay,cancel,close,context); 79 } 80 81 public ObjectViewDialog(View view, 82 String title, String header, 83 Dialog parent, 84 boolean okay, boolean cancel, boolean close, 85 DisplayContext context) { 86 super(parent); 87 setModal(parent!=null); 88 context.setWindow(this); 89 init(view,title,header,okay,cancel,close,context); 90 } 91 92 93 public ObjectViewDialog(View view, 94 String title, String header, 95 boolean okay, boolean cancel, boolean close, 96 DisplayContext context) { 97 setModal(false); 98 context.setWindow(this); 99 init(view,title,header,okay,cancel,close,context); 100 } 101 102 void init(View view, 103 String title, String header, 104 boolean okay, boolean cancel, boolean close, 105 DisplayContext context) 106 { 107 this.context = context; 108 setTitle(title); 109 110 if (okay || cancel) { 111 } 114 115 if (header != null) { 116 JPanel p=new JPanel (); 117 p.setBorder(BorderFactory.createEtchedBorder()); 118 p.add(new JLabel (header,SwingConstants.LEFT)); 119 getContentPane().add(p,BorderLayout.NORTH); 120 } 121 if (view != null) { 122 getContentPane().add((Component )view,BorderLayout.CENTER); 123 objectView = view; 124 } 125 126 if (okay || cancel || close) { 127 JPanel p2 = new JPanel (); 128 p2.setBorder(BorderFactory.createEtchedBorder()); 129 if (okay) { 130 okButton = addButton(p2, "Ok"); 131 } 132 if (cancel) { 133 cancelButton = addButton(p2, "Cancel"); 134 } 135 if (close) { 136 closeButton = addButton(p2,"Close"); 137 } 138 getContentPane().add(p2, BorderLayout.SOUTH); 139 if (closeButton!=null) { 140 getRootPane().setDefaultButton(closeButton); 141 } else if (okButton!=null) { 142 getRootPane().setDefaultButton(okButton); 143 } 144 } 145 pack(); 146 147 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 149 Rectangle rect = getBounds(); 150 rect.width = Math.min(screenDim.width,rect.width); 151 rect.height = Math.min(screenDim.height,rect.height); 152 double left = (screenDim.getWidth()-rect.getWidth())/2; 153 double top = (screenDim.getHeight()-rect.getHeight())/2; 154 Rectangle newRect = new Rectangle ( 155 (int)left,(int)top, 156 (int)rect.getWidth(),(int)rect.getHeight()); 157 setBounds(newRect); 158 159 addWindowListener( new WindowAdapter () { 160 public void windowClosing(WindowEvent e) { 161 loggerEvents.debug("windowClosing: "+e); 162 if (objectView!=null) 163 objectView.close(true); 164 } 165 } 166 ); 167 168 java.util.List editors = context.getEditors(); 169 if (!editors.isEmpty()) { 170 loggerFocus.debug("give focus to "+editors.get(0)); 171 ((Component )editors.get(0)).requestFocus(); 172 } else { 173 loggerFocus.debug("no editor to give focus to"); 174 } 175 176 addKeyAndContainerListenerRecursively(this); 177 178 attributes = Collaboration.get().getAttributes(); 179 180 setVisible(true); 181 } 182 183 public void actionPerformed(ActionEvent evt) { 184 try { 185 Object source = evt.getSource(); 186 if (source==okButton || source==closeButton) { 187 if (objectView!=null) { 188 objectView.close(true); 189 } 190 ok = true; 191 close(true); 192 dispose(); 193 } else if (source==cancelButton) { 194 ok = false; 195 close(false); 197 dispose(); 198 } 199 } catch (CommitException e) { 200 context.getDisplay().showModal(e,"Error","",context.getWindow(),false,false,true); 201 } catch (Exception e) { 202 loggerEvents.error("ObjectViewDialog.actionPerformed failed",e); 203 } 204 } 205 206 public void close(boolean validate) { 207 if (objectView!=null) 208 objectView.close(validate); 209 objectView = null; 210 } 211 212 public JButton addButton(Container c, String name) { 213 JButton button = new JButton (name); 214 button.addActionListener(this); 215 c.add(button); 216 return button; 217 } 218 219 public void restoreContext() { 221 loggerEvents.debug("Restoring attributes: "+attributes.keySet()); 222 Collaboration.get().setAttributes(attributes); 223 } 224 225 public void keyPressed(KeyEvent event) { 227 int code = event.getKeyCode(); 228 switch (code) { 229 case KeyEvent.VK_ESCAPE: 230 ok = false; 231 dispose(); 232 break; 233 default: 234 } 235 } 236 public void keyTyped(KeyEvent event) {} 237 public void keyReleased(KeyEvent event) {} 238 239 242 public void componentAdded(ContainerEvent event) { 243 addKeyAndContainerListenerRecursively(event.getChild()); 244 } 245 246 251 protected void addKeyAndContainerListenerRecursively(Component c) { 252 c.addKeyListener(this); 253 if (c instanceof Container ) { 254 Container cont = (Container )c; 255 cont.addContainerListener(this); 256 Component [] children = cont.getComponents(); 257 for(int i=0; i<children.length; i++){ 258 addKeyAndContainerListenerRecursively(children[i]); 259 } 260 } 261 } 262 263 public void componentRemoved(ContainerEvent event) { 264 removeKeyAndContainerListenerRecursively(event.getChild()); 265 } 266 267 272 protected void removeKeyAndContainerListenerRecursively(Component c) { 273 c.removeKeyListener(this); 274 if (c instanceof Container ) { 275 Container cont = (Container )c; 276 cont.removeContainerListener(this); 277 Component [] children = cont.getComponents(); 278 for(int i=0; i<children.length; i++){ 279 removeKeyAndContainerListenerRecursively(children[i]); 280 } 281 } 282 } 283 284 public String toString() { 285 return Strings.hex(this); 286 } 287 } 288 | Popular Tags |