View Javadoc

1   package com.explosion.expf;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Component;
25  import java.awt.event.ActionEvent;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  
29  import javax.swing.JInternalFrame;
30  import javax.swing.event.InternalFrameEvent;
31  import javax.swing.event.InternalFrameListener;
32  
33  import org.apache.log4j.LogManager;
34  import org.apache.log4j.Logger;
35  
36  import com.explosion.expf.menusandtools.tool.ExpToolBar;
37  import com.explosion.expf.preferences.SystemPreferences;
38  import com.explosion.expf.preferences.Utils.PreferenceResizeRecorder;
39  import com.explosion.utilities.exception.ExceptionManagerFactory;
40  import com.explosion.utilities.preferences.Preference;
41  
42  /***
43   * @author Stephen Cowx Date created:@07-Feb-2003
44   * 
45   * A ExpInternal frame is basically aninternal frame with a component in it. The
46   * component probably represent a piece of functionality hence the name applet.
47   * The applet must be a Component, it gets added BorderLayout.CENTER to the
48   * ExpInternalFrame.The applet is generally not a descendant of the Applet
49   * class.
50   * 
51   * When this frame is activated or when the title of this frame is changed and
52   * it is currently the asctive frame, then the title of the parent Frame will
53   * beset to that title too.
54   *  
55   */
56  public class ExpInternalFrame extends JInternalFrame
57  {
58      private static Logger log = LogManager.getLogger(ExpInternalFrame.class);
59        
60      private static int uniqueFrameId = 0;
61      private int id = uniqueFrameId++;
62      private Component applet = null;
63      private String appname = "";
64      private ExpInternalFrameAdapter listener;
65      private ArrayList toolbars = new ArrayList();
66  
67      public ExpInternalFrame(Component applet, String title)
68      {
69          try
70          {
71              this.setName(title);
72              this.setTitle(title);
73              this.applet = applet;
74              appname = (String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_NAME).getValue();
75              init();
76          } catch (Exception e)
77          {
78              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while constructing ExpInternalFrame.");
79          }
80      }
81  
82      private void init() throws Exception
83      {
84          this.getContentPane().setLayout(new BorderLayout());
85          this.getContentPane().add(applet, BorderLayout.CENTER);
86          this.setResizable(true);
87          this.setClosable(true);
88          this.setMaximizable(true);
89          this.setIconifiable(true);
90          listener = new ExpInternalFrameAdapter(this);
91          this.addInternalFrameListener(listener);
92      }
93  
94      public void checkEnabled()
95      {
96          Iterator it = toolbars.iterator();
97          while (it.hasNext())
98          {
99              ((ExpToolBar) it.next()).checkEnabled();
100         }
101     }
102     
103     /***
104      * This method adds a component listener to the frame which will remember the dimensions of the frame 
105      * every time it is resized.  It will persist the remembered values to the preferences provided.
106      * @param height
107      * @param width
108      */
109     public void addSizePersistence(Preference height, Preference width){
110         addComponentListener(new PreferenceResizeRecorder(height, width, this));
111     }
112 
113     public void addToolbar(ExpToolBar toolBar)
114     {
115         toolbars.add(toolBar);
116         this.getContentPane().add(toolBar, BorderLayout.NORTH);
117     }
118 
119     public void removeToolbar(ExpToolBar toolBar)
120     {
121         toolbars.remove(toolBar);
122     }
123 
124     /***
125      * Returns the applet.
126      * 
127      * @return Component
128      */
129     public Component getApplet()
130     {
131         return applet;
132     }
133 
134     /***
135      * Sets the applet.
136      * 
137      * @param applet The applet to set
138      */
139     public void setApplet(Component applet)
140     {
141         this.applet = applet;
142     }
143 
144     /***
145      * Returns the id.
146      * 
147      * @return int
148      */
149     public int getId()
150     {
151         return id;
152     }
153 
154     /***
155      * Sets the id.
156      * 
157      * @param id The id to set
158      */
159     public void setId(int id)
160     {
161         this.id = id;
162     }
163 
164     /***
165      * Sets the id.
166      * 
167      * @param id The id to set
168      */
169     public void setTitle(String title)
170     {
171         super.setTitle(title);
172         if (Application.getLocalReference() == applet)
173             Application.getApplicationFrame().setTitle(title);
174     }
175 
176     public void close()
177     {
178         log.debug("Called close() on internal frame " + this);
179         log.debug("Applet is " + applet);        
180         if (applet instanceof Closeable)
181         {
182             ActionEvent acev = new ActionEvent(this, 0, ((Closeable) applet).getCloseCommand());
183             ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
184         } else
185         {
186             try
187             {
188                 ((ExpFrame) Application.getApplicationFrame()).closeFrameWithComponent(applet, new Integer(this.getLayer()));
189             } catch (Exception e)
190             {
191                 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while closing frame.");
192             }
193         }
194     }
195 
196     public void centreInParent()
197     {
198         ExpFrame appFrame = ((ExpFrame) Application.getApplicationFrame());
199         int xpos = (int) (appFrame.getDesktop().getWidth() - this.getSize().getWidth()) / 2;
200         int ypos = (int) (appFrame.getDesktop().getHeight() - this.getSize().getHeight()) / 2;
201         setLocation(xpos, ypos);
202     }
203 }
204 
205 /***
206  * An internal class for responding to Internal frame events.
207  */
208 
209 class ExpInternalFrameAdapter implements InternalFrameListener
210 {
211 
212     ExpInternalFrame internalFrame;
213 
214     public ExpInternalFrameAdapter(ExpInternalFrame internalFrame)
215     {
216         this.internalFrame = internalFrame;
217     }
218 
219     public void internalFrameClosing(InternalFrameEvent e)
220     {
221         if (e.getInternalFrame().getDefaultCloseOperation() == JInternalFrame.DO_NOTHING_ON_CLOSE)
222         {
223             ActionEvent acev = new ActionEvent(e.getInternalFrame(), e.getID(), ExpConstants.MENU_CLOSE);
224             ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
225         }
226     }
227 
228     public void internalFrameOpened(InternalFrameEvent e)
229     {
230         ActionEvent acev = new ActionEvent(e.getInternalFrame(), e.getID(), ExpConstants.FRAME_OPENED);
231         ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
232     }
233 
234     public void internalFrameClosed(InternalFrameEvent e)
235     {
236         ActionEvent acev = new ActionEvent(e.getInternalFrame(), e.getID(), ExpConstants.FRAME_CLOSED);
237         ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
238         Application.getApplicationFrame().setTitle((String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_NAME).getValue());
239     }
240 
241     public void internalFrameActivated(InternalFrameEvent e)
242     {
243         ActionEvent acev = new ActionEvent(e.getInternalFrame(), e.getID(), ExpConstants.FRAME_ACTIVATED);
244         ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
245         Application.getApplicationFrame().setTitle((String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_NAME).getValue() + " - " + internalFrame.getTitle());
246     }
247 
248     public void internalFrameDeactivated(InternalFrameEvent e)
249     {
250         ActionEvent acev = new ActionEvent(e.getInternalFrame(), e.getID(), ExpConstants.FRAME_DEACTIVATED);
251         ((ExpFrame) Application.getApplicationFrame()).getListener().actionPerformed(acev);
252     }
253 
254     public void internalFrameIconified(InternalFrameEvent e)
255     {
256         Application.getApplicationFrame().setTitle((String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_NAME).getValue());
257     }
258 
259     public void internalFrameDeiconified(InternalFrameEvent e)
260     {}
261 
262 }