KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > service > MDIFrameServiceProvider


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.service;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Toolkit JavaDoc;
12 import java.beans.PropertyVetoException JavaDoc;
13 import java.beans.beancontext.BeanContextServices JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import javax.swing.ButtonGroup JavaDoc;
18 import javax.swing.JInternalFrame JavaDoc;
19 import javax.swing.WindowConstants JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.ejtools.adwt.action.Command;
23 import org.ejtools.adwt.action.window.CascadeAction;
24 import org.ejtools.adwt.action.window.CloseAction;
25 import org.ejtools.adwt.action.window.CloseAllAction;
26 import org.ejtools.adwt.action.window.TileAction;
27
28 /**
29  * Description of the Class
30  *
31  * @author Laurent Etiemble
32  * @version $Revision: 1.5 $
33  * @todo Javadoc to complete
34  */

35 public class MDIFrameServiceProvider extends FrameServiceProvider implements MDIFrameService
36 {
37    /** Description of the Field */
38    protected MDIDesktopPane desktop = new MDIDesktopPane();
39    /** Description of the Field */
40    protected Vector JavaDoc frames = new Vector JavaDoc();
41    /** Description of the Field */
42    protected ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
43    /** Description of the Field */
44    private static Logger logger = Logger.getLogger(MDIFrameServiceProvider.class);
45
46
47    /** Constructor for the FrameServiceProvider object */
48    public MDIFrameServiceProvider()
49    {
50       super();
51    }
52
53
54    /**
55     * Description of the Method
56     *
57     * @param frame Description of Parameter
58     */

59    public void activate(BeanContextInternalFrame frame)
60    {
61       try
62       {
63          JInternalFrame JavaDoc internalFrame = (JInternalFrame JavaDoc) frame.getComponent();
64          internalFrame.setSelected(true);
65          this.desktop.getDesktopManager().activateFrame(internalFrame);
66       }
67       catch (PropertyVetoException JavaDoc e)
68       {
69       }
70    }
71
72
73    /**
74     * Gets the activated attribute of the FrameServiceProvider object
75     *
76     * @return The activated value
77     */

78    public BeanContextInternalFrame getActivated()
79    {
80       Iterator JavaDoc it = frames.iterator();
81       while (it.hasNext())
82       {
83          BeanContextInternalFrame frame = (BeanContextInternalFrame) it.next();
84          if (this.isActivated(frame))
85          {
86             return frame;
87          }
88       }
89       return null;
90    }
91
92
93    /**
94     * Gets the activated attribute of the FrameServiceProvider object
95     *
96     * @param frame Description of Parameter
97     * @return The activated value
98     */

99    public boolean isActivated(BeanContextInternalFrame frame)
100    {
101       JInternalFrame JavaDoc selected = this.desktop.getSelectedFrame();
102       JInternalFrame JavaDoc internalFrame = (JInternalFrame JavaDoc) frame.getComponent();
103       return selected.equals(internalFrame);
104    }
105
106
107    /**
108     * Description of the Method
109     *
110     * @param frame Description of Parameter
111     */

112    public void register(BeanContextInternalFrame frame)
113    {
114       JInternalFrame JavaDoc internalFrame = (JInternalFrame JavaDoc) frame.getComponent();
115       this.frames.add(frame);
116       this.desktop.add(internalFrame);
117       this.group.add(frame.getAction().getMenuItem());
118    }
119
120
121    /**
122     * Description of the Method
123     *
124     * @param frame Description of Parameter
125     */

126    public void unregister(BeanContextInternalFrame frame)
127    {
128       JInternalFrame JavaDoc internalFrame = (JInternalFrame JavaDoc) frame.getComponent();
129       this.group.remove(frame.getAction().getMenuItem());
130       this.desktop.remove(internalFrame);
131       this.frames.remove(frame);
132    }
133
134
135    /**
136     * @return The serviceClass value
137     */

138    protected Class JavaDoc[] getServiceClass()
139    {
140       return new Class JavaDoc[]{FrameService.class, MDIFrameService.class};
141    }
142
143
144    /** Description of the Method */
145    protected void initializeBeanContextResources()
146    {
147       super.initializeBeanContextResources();
148
149       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) this.getBeanContext();
150
151       this.frame.setSize(new Dimension JavaDoc(700, 500));
152
153       this.scrollPane.getViewport().add(desktop);
154       this.frame.getContentPane().add(BorderLayout.CENTER, scrollPane);
155
156       Dimension JavaDoc screen = Toolkit.getDefaultToolkit().getScreenSize();
157       this.frame.setLocation(screen.width / 2 - frame.getSize().width / 2, screen.height / 2 - frame.getSize().height / 2);
158       this.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
159
160       this.add(new CascadeAction(
161          new Command()
162          {
163             public void execute()
164             {
165                MDIFrameServiceProvider.this.desktop.cascadeFrames();
166             }
167          }
168          ));
169
170       this.add(new TileAction(
171          new Command()
172          {
173             public void execute()
174             {
175                MDIFrameServiceProvider.this.desktop.tileFrames();
176             }
177          }
178          ));
179
180       this.add(new CloseAction(
181          new Command()
182          {
183             public void execute()
184             {
185                BeanContextInternalFrame frame = MDIFrameServiceProvider.this.getActivated();
186                if (frame != null)
187                {
188                   frame.close();
189                }
190             }
191          }
192          ));
193
194       this.add(new CloseAllAction(
195          new Command()
196          {
197             public void execute()
198             {
199                while (!MDIFrameServiceProvider.this.frames.isEmpty())
200                {
201                   BeanContextInternalFrame frame = (BeanContextInternalFrame) MDIFrameServiceProvider.this.frames.lastElement();
202                   frame.close();
203                }
204             }
205          }
206          ));
207
208       this.useServices(context);
209       this.frame.setVisible(true);
210
211       logger.debug("MDIFrameService added");
212    }
213
214
215    /** */
216    protected void releaseBeanContextResources()
217    {
218       super.releaseBeanContextResources();
219
220       logger.debug("MDIFrameService removed");
221    }
222 }
223
Popular Tags