KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > ObjectViewDialog


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>,
3                           Laurent Martelli <laurent@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Frame JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.awt.Toolkit JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.ContainerEvent JavaDoc;
32 import java.awt.event.ContainerListener JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import java.awt.event.KeyListener JavaDoc;
35 import java.awt.event.WindowAdapter JavaDoc;
36 import java.awt.event.WindowEvent JavaDoc;
37 import java.util.Map JavaDoc;
38 import javax.swing.BorderFactory JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JDialog JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.SwingConstants JavaDoc;
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 JavaDoc
54     implements ActionListener JavaDoc, KeyListener JavaDoc, ContainerListener JavaDoc
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 JavaDoc okButton;
61     JButton JavaDoc cancelButton;
62     JButton JavaDoc closeButton;
63     View objectView;
64     protected DisplayContext context;
65
66     /** Stores context attributes at creation time so they can be
67         restored by components when invoking methods */

68     Map JavaDoc attributes;
69
70     public ObjectViewDialog(View view,
71                             String JavaDoc title, String JavaDoc header,
72                             Frame JavaDoc 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 JavaDoc title, String JavaDoc 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 JavaDoc title, String JavaDoc 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 JavaDoc title, String JavaDoc 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             //((AbstractSwingDisplay)display).addDialog(this);
112
//setModal(true);
113
}
114
115         if (header != null) {
116             JPanel JavaDoc p=new JPanel JavaDoc();
117             p.setBorder(BorderFactory.createEtchedBorder());
118             p.add(new JLabel JavaDoc(header,SwingConstants.LEFT));
119             getContentPane().add(p,BorderLayout.NORTH);
120         }
121         if (view != null) {
122             getContentPane().add((Component JavaDoc)view,BorderLayout.CENTER);
123             objectView = view;
124         }
125
126         if (okay || cancel || close) {
127             JPanel JavaDoc p2 = new JPanel JavaDoc();
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         // open the box centered in the screen...
148
Dimension JavaDoc screenDim = Toolkit.getDefaultToolkit().getScreenSize();
149         Rectangle JavaDoc 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 JavaDoc newRect = new Rectangle JavaDoc(
155             (int)left,(int)top,
156             (int)rect.getWidth(),(int)rect.getHeight());
157         setBounds(newRect);
158
159         addWindowListener( new WindowAdapter JavaDoc () {
160                 public void windowClosing(WindowEvent JavaDoc e) {
161                     loggerEvents.debug("windowClosing: "+e);
162                     if (objectView!=null)
163                         objectView.close(true);
164                 }
165             }
166         );
167
168         java.util.List JavaDoc editors = context.getEditors();
169         if (!editors.isEmpty()) {
170             loggerFocus.debug("give focus to "+editors.get(0));
171             ((Component JavaDoc)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 JavaDoc evt) {
184         try {
185             Object JavaDoc 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                 // ((AbstractSwingDisplay)display).removeDialog(this);
196
close(false);
197                 dispose();
198             }
199         } catch (CommitException e) {
200             context.getDisplay().showModal(e,"Error","",context.getWindow(),false,false,true);
201         } catch (Exception JavaDoc 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 JavaDoc addButton(Container JavaDoc c, String JavaDoc name) {
213         JButton JavaDoc button = new JButton JavaDoc(name);
214         button.addActionListener(this);
215         c.add(button);
216         return button;
217     }
218
219     // We should implement DialogView !!!
220
public void restoreContext() {
221         loggerEvents.debug("Restoring attributes: "+attributes.keySet());
222         Collaboration.get().setAttributes(attributes);
223     }
224
225     // KeyListener interface
226
public void keyPressed(KeyEvent JavaDoc 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 JavaDoc event) {}
237     public void keyReleased(KeyEvent JavaDoc event) {}
238
239     // ContainerListener interface
240
// Copied from http://www.javaworld.com/javaworld/javatips/jw-javatip69.html
241

242     public void componentAdded(ContainerEvent JavaDoc event) {
243         addKeyAndContainerListenerRecursively(event.getChild());
244     }
245
246     /**
247      * Register as a KeyListener and ContainerListener on the component
248      * and its children recursively.
249      * @param c the component
250      */

251     protected void addKeyAndContainerListenerRecursively(Component JavaDoc c) {
252         c.addKeyListener(this);
253         if (c instanceof Container JavaDoc) {
254             Container JavaDoc cont = (Container JavaDoc)c;
255             cont.addContainerListener(this);
256             Component JavaDoc[] 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 JavaDoc event) {
264         removeKeyAndContainerListenerRecursively(event.getChild());
265     }
266
267     /**
268      * Unregister as a KeyListener and ContainerListener on the
269      * component and its children recursively.
270      * @param c the component
271      */

272     protected void removeKeyAndContainerListenerRecursively(Component JavaDoc c) {
273         c.removeKeyListener(this);
274         if (c instanceof Container JavaDoc) {
275             Container JavaDoc cont = (Container JavaDoc)c;
276             cont.removeContainerListener(this);
277             Component JavaDoc[] children = cont.getComponents();
278             for(int i=0; i<children.length; i++){
279                 removeKeyAndContainerListenerRecursively(children[i]);
280             }
281         }
282     }
283
284     public String JavaDoc toString() {
285         return Strings.hex(this);
286     }
287 }
288
Popular Tags