KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dimension JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.ContainerEvent JavaDoc;
30 import java.awt.event.ContainerListener JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.awt.event.KeyListener JavaDoc;
33 import java.awt.event.WindowAdapter JavaDoc;
34 import java.awt.event.WindowEvent JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.Map JavaDoc;
37 import javax.swing.BorderFactory JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JDialog JavaDoc;
40 import javax.swing.JEditorPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
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 /**
51  * This dialog is used to ask the parameters values when a method is
52  * called on a viewed JAC object.<p>
53  *
54  * @see View
55  *
56  * @author <a HREF="mailto:renaud@cnam.fr">Renaud Pawlak</a>
57  * @author <a HREF="mailto:laurent@aopsys.com">Laurent Martelli</a>
58  */

59 public class Dialog extends JDialog JavaDoc
60     implements ActionListener JavaDoc, KeyListener JavaDoc, ContainerListener JavaDoc, DialogView
61 {
62     static Logger loggerEvents = Logger.getLogger("gui.events");
63
64     String JavaDoc label;
65     DisplayContext context;
66     Length width;
67     Length height;
68     ViewFactory factory;
69     Object JavaDoc[] parameters;
70     String JavaDoc type;
71
72     boolean ok = false;
73
74     private JButton JavaDoc okButton;
75     private JButton JavaDoc cancelButton;
76
77     Semaphore semaphore = new Semaphore();
78     String JavaDoc description;
79     View contentView;
80
81    /**
82     * Construct a dialog window.
83     *
84     * @param content the content of the dialog
85     * @param parent the parent window of the dialog
86     * @param description a text describing the dialog to the user
87     */

88     public Dialog(View content, Object JavaDoc parent,
89                   String JavaDoc title, String JavaDoc description) {
90         this.description = description;
91         this.contentView = content;
92         setModal(true);
93         setTitle(title);
94
95         Container JavaDoc contentPane = getContentPane();
96
97         addWindowListener( new WindowAdapter JavaDoc() {
98                 public void windowClosing(WindowEvent JavaDoc e) {
99                     e.getWindow().dispose();
100                 }
101             }
102         );
103
104         contentPane.add((Component JavaDoc)content, BorderLayout.CENTER);
105         content.setParentView(this);
106
107         if (description != null)
108         {
109             JEditorPane JavaDoc descr = new JEditorPane JavaDoc("text/plain",description);
110             descr.setEditable(false);
111             descr.setBackground(null);
112             contentPane.add(descr,BorderLayout.NORTH);
113         }
114
115         // Buttons panel
116
JPanel JavaDoc p2 = new JPanel JavaDoc();
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         // open the box centerd in the screen...
125
Dimension JavaDoc screenDim = Toolkit.getDefaultToolkit().getScreenSize();
126         Rectangle JavaDoc rect = getBounds();
127         double left = (screenDim.getWidth()-rect.getWidth())/2;
128         double top = (screenDim.getHeight()-rect.getHeight())/2;
129         Rectangle JavaDoc newRect = new Rectangle JavaDoc(
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         // Do not place anything after this, since this a blocking call
140
}
141
142     /** Stores context attributes at creation time so they can be
143         restored by components when invoking methods */

144     Map JavaDoc attributes;
145
146     // View interface
147

148     Border viewBorder;
149    
150     /**
151      * Get the value of viewBorder.
152      * @return value of viewBorder.
153      */

154     public Border getViewBorder() {
155         return viewBorder;
156     }
157    
158     /**
159      * Set the value of viewBorder.
160      * @param v Value to assign to viewBorder.
161      */

162     public void setViewBorder(Border v) {
163         this.viewBorder = v;
164     }
165    
166     // style used to change display (css for web)
167
String JavaDoc style;
168
169     public void setStyle(String JavaDoc style) {
170         this.style = style;
171     }
172
173     public String JavaDoc getStyle() {
174         return style;
175     }
176
177     MethodItem message;
178    
179     /**
180      * Get the value of message.
181      * @return value of message.
182      */

183     public MethodItem getMessage() {
184         return message;
185     }
186
187     /**
188      * Get the value of description.
189      * @return value of description.
190      */

191     public String JavaDoc getDescription() {
192         return description;
193     }
194    
195     /**
196      * Set the value of description.
197      * @param v Value to assign to description.
198      */

199     public void setDescription(String JavaDoc v) {
200         this.description = v;
201     }
202    
203     View parentView;
204    
205    /**
206     * Get the value of parentView.
207     * @return value of parentView.
208     */

209     public View getParentView() {
210         return parentView;
211     }
212    
213     /**
214     * Set the value of parentView.
215     * @param v Value to assign to parentView.
216     */

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     /**
237     * Set the value of message.
238     * @param v Value to assign to message.
239     */

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 JavaDoc label) {
261         this.label = label;
262         setTitle(label);
263     }
264
265     public String JavaDoc getLabel() {
266         return label;
267     }
268
269     public void setSize(Length width, Length height) {
270         this.width = width;
271         this.height = height;
272         //SwingUtils.setSize(this,width,height);
273
}
274
275     public void setType(String JavaDoc type) {
276         this.type = type;
277     }
278
279     public String JavaDoc getType() {
280         return type;
281     }
282
283     public void setParameters(Object JavaDoc[] parameters) {
284         this.parameters = parameters;
285     }
286    
287     public Object JavaDoc[] 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 JavaDoc type, Object JavaDoc[] 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 JavaDoc option) {
319     }
320
321     // DialogView interface
322

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     /**
340      * For internal use.<p>
341      */

342     JButton JavaDoc addButton(Container JavaDoc c, String JavaDoc name) {
343         JButton JavaDoc button = new JButton JavaDoc(name);
344         button.addActionListener(this);
345         c.add(button);
346         return button;
347     }
348
349     /**
350      * Implements what is done when a button is pressed (may be either
351      * OK or CANCEL).<p>
352      *
353      * @param evt tell what button was pressed
354      */

355     public void actionPerformed(ActionEvent JavaDoc evt) {
356         try {
357             Object JavaDoc 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 JavaDoc e) {
367             e.printStackTrace();
368         }
369     }
370
371     // KeyListener interface
372
public void keyPressed(KeyEvent JavaDoc 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 JavaDoc event) {}
384     public void keyReleased(KeyEvent JavaDoc event) {}
385
386     // ContainerListener interface
387
// Copied from http://www.javaworld.com/javaworld/javatips/jw-javatip69.html
388

389     public void componentAdded(ContainerEvent JavaDoc event) {
390         addKeyAndContainerListenerRecursively(event.getChild());
391     }
392
393     /**
394      * Register as a KeyListener and ContainerListener on the component
395      * and its children recursively.
396      * @param c the component
397      */

398     protected void addKeyAndContainerListenerRecursively(Component JavaDoc c) {
399         c.addKeyListener(this);
400         if (c instanceof Container JavaDoc) {
401             Container JavaDoc cont = (Container JavaDoc)c;
402             cont.addContainerListener(this);
403             Component JavaDoc[] 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 JavaDoc event) {
411         removeKeyAndContainerListenerRecursively(event.getChild());
412     }
413
414     /**
415      * Unregister as a KeyListener and ContainerListener on the
416      * component and its children recursively.
417      * @param c the component
418      */

419     protected void removeKeyAndContainerListenerRecursively(Component JavaDoc c) {
420         c.removeKeyListener(this);
421         if (c instanceof Container JavaDoc) {
422             Container JavaDoc cont = (Container JavaDoc)c;
423             cont.removeContainerListener(this);
424             Component JavaDoc[] children = cont.getComponents();
425             for(int i=0; i<children.length; i++){
426                 removeKeyAndContainerListenerRecursively(children[i]);
427             }
428         }
429     }
430 }
431
Popular Tags