View Javadoc

1   package com.explosion.expf.supportmodules;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.awt.Dimension;
23  import java.awt.event.ActionEvent;
24  import java.beans.PropertyVetoException;
25  import java.util.Arrays;
26  import java.util.Comparator;
27  import java.util.HashMap;
28  import java.util.Hashtable;
29  import java.util.Map;
30  import java.util.Properties;
31  import java.util.Vector;
32  
33  import javax.swing.JCheckBoxMenuItem;
34  import javax.swing.JInternalFrame;
35  import javax.swing.JPanel;
36  
37  import com.explosion.expf.Application;
38  import com.explosion.expf.ExpActionListener;
39  import com.explosion.expf.ExpConstants;
40  import com.explosion.expf.ExpFrame;
41  import com.explosion.expf.ExpInternalFrame;
42  import com.explosion.expf.ExpModuleManager;
43  import com.explosion.expf.menusandtools.menu.ExpMenu;
44  import com.explosion.expf.menusandtools.menu.ExpMenuBar;
45  import com.explosion.expf.menusandtools.menu.MenuHelper;
46  import com.explosion.expf.menusandtools.menu.segmented.ExpMenuSegment;
47  import com.explosion.expf.menusandtools.menu.segmented.ExpSegmentedMenu;
48  import com.explosion.utilities.exception.ExceptionManagerFactory;
49  import com.explosion.utilities.preferences.Preference;
50  
51  /***
52   * @author Stephen Cowx
53   * Date created:@08-Feb-2003
54   */
55  public class WindowSupportModule implements ExpModuleManager
56  {
57    private Vector preferences;
58    private Hashtable preferenceHash;
59    private WindowSupportModule instance;
60    private WindowSupportListener listener;
61    
62    public WindowSupportModule()
63    {
64    	instance = this;
65    }
66    
67    public void initialiseGui() throws Exception
68    {
69      ExpMenuBar menuBar = ((ExpFrame) Application.getApplicationFrame()).getExpMenuBar();
70      MenuHelper helper = menuBar.getMenuHelper();
71      ExpMenu menuWindow = (ExpMenu) menuBar.createExpMenu("Window",'w',ExpConstants.MENU_WINDOW,1,true);
72      
73      /* File menu */
74      ExpMenuSegment windowSegment = menuWindow.createNewSegment(ExpMenuSegment.ANY_POSITION);
75      windowSegment.addElement(helper.createMenuItem("Cascade",'c',ExpConstants.MENU_CASCADE_WINDOWS,null,2));
76      windowSegment.addElement(helper.createMenuItem("Tile vertically",'v',ExpConstants.MENU_TILE_WINDOWS_V,null,2));
77      windowSegment.addElement(helper.createMenuItem("Tile horizontally",'h',ExpConstants.MENU_TILE_WINDOWS_H,null,2));
78          
79      listener = new WindowSupportListener(menuBar,menuWindow);
80    }
81  
82    public String getVersion()
83    {
84      return "1.0";
85    }
86  
87    public String getName()
88    {
89      return "Window Support";
90    }
91  
92    public String getDescription()
93    {
94      return "Menu and toolbar support for managing windows.";
95    }
96   
97    public void initialiseCore(Properties properties)
98    {
99    }
100 
101   public Vector getPreferences()
102   {
103     return preferences;
104   }
105   
106   public Preference getPreference(String preferenceName)
107   {
108   	return (Preference) preferenceHash.get(preferenceName);
109   }
110 
111   public JPanel getPreferencesEditor()
112   {
113     return null;
114   }
115   
116   public ExpActionListener getGlobalListener()
117   {
118   	return listener;
119   }  
120   
121 }
122 
123 class WindowSupportListener implements ExpActionListener
124 {
125 
126   private HashMap map;
127   private ExpSegmentedMenu menuWindow;
128   private ExpMenuBar menuBar;
129   private Hashtable windowItems= new Hashtable();
130   private ExpMenuSegment segment;
131   
132   /***
133    * Constructor for exqlListener.
134    */
135   public WindowSupportListener(ExpMenuBar menuBar, ExpSegmentedMenu menuWindow)
136   {
137     this.menuWindow = menuWindow;
138     this.menuBar = menuBar;
139     map = new HashMap();
140     map.put(ExpConstants.MENU_CASCADE_WINDOWS,ExpConstants.MENU_CASCADE_WINDOWS);
141     map.put(ExpConstants.MENU_TILE_WINDOWS_V,ExpConstants.MENU_TILE_WINDOWS_V);
142     map.put(ExpConstants.MENU_TILE_WINDOWS_H,ExpConstants.MENU_TILE_WINDOWS_H);
143     map.put(ExpConstants.FRAME_ACTIVATED,ExpConstants.FRAME_ACTIVATED);    
144     map.put(ExpConstants.FRAME_DEACTIVATED,ExpConstants.FRAME_DEACTIVATED);    
145     map.put(ExpConstants.FRAME_OPENED,ExpConstants.FRAME_OPENED);    
146     map.put(ExpConstants.FRAME_CLOSED,ExpConstants.FRAME_CLOSED);    
147     map.put(ExpConstants.MENU_WINDOW_ITEM_PRESSED,ExpConstants.MENU_WINDOW_ITEM_PRESSED); 
148   }
149 
150   /***
151    * @see package com.explosion.expf.Interfaces.ExpActionListener#getListensFor()
152    */
153   public Map getListensFor()
154   {
155     return map;
156   }
157 
158   /***
159    * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
160    */
161   public void actionPerformed(ActionEvent e)
162   {
163     try
164     {
165       if (e.getActionCommand().equals(ExpConstants.MENU_CASCADE_WINDOWS))
166       {
167       	cascadeWindows();
168       }
169       else if (e.getActionCommand().equals(ExpConstants.MENU_TILE_WINDOWS_V))
170       {
171       	tileWindows(true);
172       }
173       else if (e.getActionCommand().equals(ExpConstants.MENU_TILE_WINDOWS_H))
174       {
175       	tileWindows(false);
176       }
177       else if (e.getActionCommand().equals(ExpConstants.MENU_WINDOW_ITEM_PRESSED))
178       {
179       	JInternalFrame frame = (JInternalFrame) windowItems.get(e.getSource());
180       	  try
181 	      {
182 	        if (frame.isIcon())
183 	          frame.setIcon(false);
184 	      }
185 	      catch (PropertyVetoException ex)
186 	      {
187 	        ExceptionManagerFactory.getExceptionManager().manageException(ex,"Unable to de-iconify frame.");
188 	      }
189 	      
190       	frame.setSelected(true);
191       }
192       else if (e.getActionCommand().equals(ExpConstants.FRAME_ACTIVATED))
193       {
194       	rebuildWindowMenu();
195       }
196       else if (e.getActionCommand().equals(ExpConstants.FRAME_DEACTIVATED))
197       {
198       	rebuildWindowMenu();
199       }
200       else if (e.getActionCommand().equals(ExpConstants.FRAME_OPENED))
201       {
202         Application.addToGlobalCookie(ExpConstants.MENU_CASCADE_WINDOWS,1);
203         Application.addToGlobalCookie(ExpConstants.MENU_TILE_WINDOWS_H,1);
204         Application.addToGlobalCookie(ExpConstants.MENU_TILE_WINDOWS_V,1);
205     
206       	rebuildWindowMenu();
207       }
208       else if (e.getActionCommand().equals(ExpConstants.FRAME_CLOSED))
209       {
210       	Application.removeFromGlobalCookie(ExpConstants.MENU_CASCADE_WINDOWS,1);
211         Application.removeFromGlobalCookie(ExpConstants.MENU_TILE_WINDOWS_H,1);
212         Application.removeFromGlobalCookie(ExpConstants.MENU_TILE_WINDOWS_V,1);
213          
214       	rebuildWindowMenu();
215       }
216     }
217     catch (Exception ex)
218     {
219        com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while responding to SimpleProcess Event." );
220     }   
221     
222   }
223   
224   /***
225    * This method arranges the windows in a cascade pattern
226    */
227   private void cascadeWindows()
228   {
229     Dimension windowSize = ((ExpFrame)Application.getApplicationFrame()).getDesktop().getSize();
230     JInternalFrame[] frames = ((ExpFrame)Application.getApplicationFrame()).getDesktop().getAllFramesInLayer(ExpFrame.DOC_FRAME_LAYER.intValue());
231     
232     if (frames == null || frames.length <1)
233        return;
234     
235     int newWidth = (int) (windowSize.getWidth() - (windowSize.getWidth() / 3));//canvasWidth - 33%
236     int newHeight = (int) (windowSize.getHeight() - (windowSize.getHeight() / 3));//height - 33%
237     Dimension newDimension = new Dimension(newWidth, newHeight);
238 
239     int xpos = 20;
240     int ypos = 20;
241     int count = 0;
242 
243     for (int i = 0; i < frames.length; i++)
244     {
245       JInternalFrame frame = frames[i];
246       try
247       {
248         if (frame.isIcon())
249           frame.setIcon(false);
250       }
251       catch (PropertyVetoException e)
252       {
253         ExceptionManagerFactory.getExceptionManager().manageException(e,"Unable to de-iconify frame.");
254       }
255       
256       frame.setSize(newDimension);
257       frame.setLocation(xpos, ypos);
258       if (count >= 4)
259       {
260         xpos = 20;
261         ypos = 20;
262         count = 0;
263       }
264       else
265       {
266         xpos += 20;
267         ypos += 20;
268         count++;
269       }
270     }
271   }
272   
273   /***
274    * This method tile the windows vertically
275    */
276   private void tileWindows(boolean vertical)
277   {
278     Dimension windowSize = ((ExpFrame)Application.getApplicationFrame()).getDesktop().getSize();
279     JInternalFrame[] frames = ((ExpFrame)Application.getApplicationFrame()).getDesktop().getAllFramesInLayer(ExpFrame.DOC_FRAME_LAYER.intValue());
280     
281     if (frames == null || frames.length <1)
282        return;
283     
284     int newWidth = 0;
285     int newHeight = 0;
286     int xpos = 0;
287     int ypos = 0;
288     int xIncrement = 0;
289     int yIncrement = 0;
290 
291     if (vertical)
292     {
293       newWidth = (int) windowSize.getWidth() / frames.length;
294       newHeight = (int) windowSize.getHeight();
295       xIncrement = newWidth;
296     }
297     else
298     {
299       newWidth = (int) windowSize.getWidth();
300       newHeight = (int) windowSize.getHeight() / frames.length;
301       yIncrement = newHeight;
302     }
303 
304     Dimension newDimension = new Dimension(newWidth, newHeight);
305 
306 
307     for (int i = 0; i < frames.length; i++)
308     {
309       JInternalFrame frame = frames[i];
310       try
311       {
312         if (frame.isIcon())
313           frame.setIcon(false);
314       }
315       catch (PropertyVetoException e)
316       {
317         ExceptionManagerFactory.getExceptionManager().manageException(e,"Unable to de-iconify frame.");
318       }
319       
320       frame.setSize(newDimension);
321       frame.setLocation(xpos, ypos);
322       xpos += xIncrement;
323       ypos += yIncrement;
324 
325     }
326   }
327   
328   public void rebuildWindowMenu() throws Exception
329   {
330   	JInternalFrame[] frames = ((ExpFrame)Application.getApplicationFrame()).getDesktop().getAllFramesInLayer(ExpFrame.DOC_FRAME_LAYER.intValue());
331     
332 //    Enumeration en = windowItems.keys();
333 //    while (en.hasMoreElements())
334 //    {
335 //      menuWindow.remove((JMenuItem)en.nextElement());
336 //    }   
337     
338     /* File menu */
339     if (segment == null)
340     {
341     	segment = menuWindow.createNewSegment(ExpMenuSegment.ANY_POSITION);
342     }
343     else
344     {
345     	menuWindow.removeSegment(segment);
346     	segment = menuWindow.createNewSegment(ExpMenuSegment.ANY_POSITION);
347     }
348     
349     if (frames == null || frames.length <1)
350     {
351        Application.ensureGlobalCookie(ExpConstants.MENU_WINDOW, 0);
352     }   
353     else   
354     {
355       windowItems = new Hashtable();
356       Application.ensureGlobalCookie(ExpConstants.MENU_WINDOW, 1);
357       /* sorting this by reference should produce the youngest first */
358       Arrays.sort(frames, new ExpFrameComparator());
359       
360       for (int i=0;i<frames.length;i++)
361       {
362          String displayName  = (i+1) + "." + (String) frames[i].getTitle();
363          
364          JCheckBoxMenuItem item = menuBar.getMenuHelper().createCheckBoxMenuItem(displayName, (char)i, ExpConstants.MENU_WINDOW_ITEM_PRESSED, null, 1);
365          if (frames[i].isSelected())
366             item.setSelected(true);
367          else
368             item.setSelected(false);
369             
370          menuWindow.addElementToSegment(segment,item);
371          windowItems.put(item,frames[i]);
372          Application.ensureGlobalCookie(ExpConstants.MENU_WINDOW_ITEM_PRESSED, 1);
373       } 
374     }  
375   }
376 }
377 
378 /***
379  * compares frame id's
380  */
381 class ExpFrameComparator implements Comparator
382 {
383 	
384 	public int compare(Object object1, Object object2)
385 	{
386 		int id1 = ((ExpInternalFrame) object1).getId();
387 		int id2 = ((ExpInternalFrame) object2).getId();
388 		
389         if (id1 < id2)
390            return -1;
391         else if (id1 == id2)
392            return 0;
393         else
394            return 1;
395 	}
396 }
397 
398