KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > mdi > ClientDesktop


1 package rero.gui.mdi;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5
6 import java.awt.*;
7 import java.awt.event.*;
8
9 import java.util.*;
10
11 import java.beans.*;
12
13 import rero.gui.windows.*;
14 import rero.gui.background.*;
15
16 import rero.util.ClientUtils;
17 import rero.config.*;
18
19 import rero.gui.toolkit.OrientedToolBar;
20
21 /** responsible for mantaining the state of the desktop GUI and switchbar */
22 public class ClientDesktop extends WindowManager implements ClientWindowListener, ClientStateListener
23 {
24     protected JDesktopPane desktop;
25
26     public void init()
27     {
28        desktop = new JDesktopPane();
29        switchbar = new OrientedToolBar();
30
31        setLayout(new BorderLayout());
32        add(desktop, BorderLayout.CENTER);
33
34        switchOptions = new SwitchBarOptions(this, switchbar);
35 // add(switchbar, BorderLayout.NORTH);
36

37        windowMap = new HashMap();
38        windows = new LinkedList();
39
40        desktop.setDesktopManager(new MyModifiedDesktopManager());
41
42        MantainActiveFocus temp = new MantainActiveFocus(this); // object automagically registers itself as a listener.
43

44        BackgroundDesktop wallpaper = new BackgroundDesktop(desktop);
45        wallpaper.setSize(Toolkit.getDefaultToolkit().getScreenSize());
46        desktop.add(wallpaper, new Integer JavaDoc(Integer.MIN_VALUE));
47     }
48
49     public void addWindow(StatusWindow window, boolean selected)
50     {
51        ClientInternalWindow temp = new ClientInternalWindow();
52        window.init(temp);
53
54        Rectangle bounds = ClientState.getClientState().getBounds(window.getWindowType()+".size", desktop.getSize(), new Dimension(480, 300));
55
56        temp.setBounds(bounds);
57
58        windowMap.put(window.getWindow(), window);
59        windowMap.put(window.getButton(), window);
60
61        window.getWindow().addWindowListener(this);
62        window.getButton().addActionListener(this);
63
64        // add to the switchbar
65
addToSwitchbar(window);
66
67        // add to the desktop
68
desktop.add((JInternalFrame)window.getWindow());
69
70        if (!selected)
71        {
72           temp.setVisible(false);
73        }
74        else
75        {
76           window.getWindow().show();
77        }
78
79        // one of my hacks... make sure the selected window knows it is the selected window.
80
// for some reason when I first launch the program this concept doesn't take so well.
81

82        ClientUtils.invokeLater(new Runnable JavaDoc()
83        {
84           public void run()
85           {
86               if (desktop.getSelectedFrame() != null && !desktop.getSelectedFrame().isSelected())
87               {
88                  try
89                  {
90                     refreshFocus(desktop.getSelectedFrame());
91                     desktop.getSelectedFrame().setSelected(true);
92                  }
93                  catch (java.beans.PropertyVetoException JavaDoc ex) { }
94                  desktop.repaint();
95               }
96           }
97        });
98     }
99
100     public void onActive(ClientWindowEvent ev)
101     {
102        StatusWindow temp = getWindowFor(ev.getSource());
103        doActivate(temp);
104     }
105
106     public void onInactive(ClientWindowEvent ev)
107     {
108        doDeactivate(getWindowFor(ev.getSource()));
109     }
110
111     public void onMinimize(ClientWindowEvent ev)
112     {
113        boolean wasSelected = ev.getSource().isSelected();
114        doDeactivate(getWindowFor(ev.getSource()));
115
116        if (wasSelected)
117        {
118           int index = windows.indexOf(getWindowFor(ev.getSource()));
119           newActive(index, false);
120           refreshFocus(desktop.getSelectedFrame());
121        }
122     }
123
124     public void onOpen(ClientWindowEvent ev)
125     {
126        StatusWindow temp = getWindowFor(ev.getSource());
127        doActivate(temp);
128     }
129
130     public void onClose(ClientWindowEvent ev)
131     {
132        int index = windows.indexOf(getWindowFor(ev.getSource()));
133        boolean wasSelected = ev.getSource().isSelected();
134
135        ClientWindow window = ev.getSource();
136        StatusWindow temp = (StatusWindow)windowMap.get(window);
137
138        saveBounds(temp);
139
140        switchbar.remove(temp.getButton());
141
142        windowMap.remove(window);
143        windowMap.remove(temp.getButton());
144        windowMap.remove(temp.getWindow());
145        windows.remove(temp);
146
147        switchbar.validate();
148        switchbar.repaint();
149
150        if (desktop.getSelectedFrame() == null)
151        {
152           newActive(index, true);
153           refreshFocus(desktop.getSelectedFrame());
154        }
155     }
156
157     public StatusWindow getActiveWindow()
158     {
159        JInternalFrame f = desktop.getSelectedFrame();
160        return getWindowFor(f);
161     }
162
163     protected void doActivate(StatusWindow window)
164     {
165        try
166        {
167           JInternalFrame temp = (JInternalFrame)window.getWindow();
168
169           if (!((ClientInternalWindow)window.getWindow()).isOpen())
170           {
171              window.getWindow().show();
172           }
173
174           if (!temp.isSelected())
175           {
176              JInternalFrame[] ftemp = desktop.getAllFrames();
177
178              for (int x = 0; x < ftemp.length; x++)
179              {
180                 if (ftemp[x].isSelected()) { ftemp[x].setSelected(false); }
181              }
182           }
183
184           if (temp.isIcon())
185           {
186              temp.setIcon(false);
187           }
188
189           desktop.setSelectedFrame(temp);
190           window.getButton().setSelected(true);
191
192           temp.setSelected(true);
193
194           if (window.isLegalWindow())
195             window.getInput().requestFocus();
196
197           saveBounds(window);
198        }
199        catch (PropertyVetoException ex) { ex.printStackTrace(); }
200     }
201
202     private void saveBounds(StatusWindow window)
203     {
204        JInternalFrame temp = (JInternalFrame)window.getWindow();
205
206        if (window.getWindow().isMaximum())
207        {
208           ClientState.getClientState().setBounds(window.getWindowType()+".size", new Rectangle(0, 0, (int)desktop.getSize().getWidth(), (int)desktop.getSize().getHeight()));
209        }
210        else
211        {
212           ClientState.getClientState().setBounds(window.getWindowType()+".size", temp.getBounds());
213        }
214     }
215
216     public void refreshFocus(JInternalFrame f)
217     {
218        if (f != null && isShowing() && getWindowFor(f).isLegalWindow() && !rero.gui.KeyBindings.is_dialog_active)
219           getWindowFor(f).getInput().requestFocus();
220     }
221
222     protected void doDeactivate(StatusWindow window)
223     {
224        JInternalFrame temp = (JInternalFrame)window.getWindow();
225
226        if (temp.isSelected())
227        {
228           try
229           {
230              temp.setIcon(true);
231           }
232           catch (Exception JavaDoc ex) { }
233        }
234
235        window.getButton().setSelected(false);
236
237        try
238        {
239           temp.setSelected(false);
240        }
241        catch (Exception JavaDoc ex) { }
242     }
243
244     private int totalOpenWindows()
245     {
246        int wincount = 0;
247        for (int x = 0; x < desktop.getAllFrames().length; x++)
248        {
249           JInternalFrame cwin = desktop.getAllFrames()[x];
250           if (!cwin.isIcon()) { wincount++; }
251        }
252
253        return wincount;
254     }
255
256     public void cascadeWindows()
257     {
258        // Add your handling code here:
259
Dimension size = desktop.getSize();
260        int width = size.width * 4/5;
261        int height = size.height * 2/3;
262        int total = totalOpenWindows();
263
264        if (total <= 0)
265            return;
266
267        JInternalFrame[] frames = desktop.getAllFrames();
268
269        for (int x = 0, pos = total - 1; x < frames.length; x++)
270        {
271           try
272           {
273              if (!frames[x].isIcon())
274              {
275                 try { frames[x].setMaximum(false); } catch (Exception JavaDoc ex) { }
276                 frames[x].setSize(width, height);
277                 frames[x].setLocation(pos * 20, pos * 20);
278                 pos--;
279              }
280           }
281           catch (Exception JavaDoc ex) { }
282        }
283     }
284  
285     public void tileWindows()
286     {
287        JInternalFrame[] frames = desktop.getAllFrames();
288        int total = totalOpenWindows();
289
290        if (total <= 0)
291            return;
292
293        int numcols = (int)(Math.sqrt((double)total));
294        int numrows = (total / numcols);
295        if ((total % numcols) != 0) { numrows++; }
296
297        Dimension size = desktop.getSize();
298        int width = size.width / numcols;
299        int height = size.height / numrows;
300
301        int ypos = 0;
302        int winno = 1;
303
304        for (int z = 0; z < frames.length; z++)
305        {
306           if (!frames[z].isIcon())
307           {
308              try { frames[z].setMaximum(false); } catch (Exception JavaDoc ex) { }
309              frames[z].setSize(new Dimension(width, height));
310              frames[z].setLocation((winno - 1) * width, ypos);
311              if (winno == numcols)
312              {
313                 winno = 0;
314                 ypos += height;
315              }
316              winno++;
317           }
318        }
319     }
320
321     protected class MyModifiedDesktopManager extends DefaultDesktopManager
322     {
323         public void closeFrame(JInternalFrame f) { }
324
325         public void iconifyFrame(JInternalFrame f)
326         {
327            boolean findNew = f.isSelected();
328
329            f.setVisible(false);
330            f.getParent().repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
331         }
332
333         public void deiconifyFrame(JInternalFrame f)
334         {
335            f.setVisible(true);
336         }
337
338         public void dragFrame(JComponent f, int newX, int newY)
339         {
340            super.dragFrame(f, newX, newY);
341
342            if (isRelative)
343            {
344 // f.repaint(); // smart dragging feature, causes window to be repainted while it is being dragged, unfortunately
345
// it comes out jerky and looking like crap. So I'm disabling it here.
346
}
347         }
348  
349         public void endDraggingFrame(JComponent f)
350         {
351            super.endDraggingFrame(f);
352
353            if (isRelative)
354            {
355               f.repaint();
356            }
357         }
358     }
359
360
361     private class MantainActiveFocus extends ComponentAdapter
362     {
363         protected ClientDesktop desktop;
364
365         public MantainActiveFocus(ClientDesktop mine)
366         {
367            desktop = mine;
368            desktop.addComponentListener(this);
369         }
370
371         public void componentShown(ComponentEvent e)
372         {
373            JDesktopPane temp = desktop.desktop;
374
375            if (temp.getSelectedFrame() != null)
376            {
377               SwingUtilities.invokeLater(new Runnable JavaDoc()
378               {
379                  public void run()
380                  {
381                     try
382                     {
383                        if (!desktop.desktop.getSelectedFrame().isSelected())
384                        {
385                           desktop.desktop.getSelectedFrame().setSelected(true);
386                        }
387                        desktop.refreshFocus(desktop.desktop.getSelectedFrame());
388                     }
389                     catch (Exception JavaDoc ex) { ex.printStackTrace(); }
390                  }
391               });
392
393               temp.repaint();
394            }
395         }
396     }
397 }
398
Popular Tags