KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > windows > WindowManager


1 package rero.gui.windows;
2
3 import javax.swing.*;
4 import java.util.*;
5 import rero.config.*;
6 import java.awt.*;
7 import java.awt.event.*;
8
9 public abstract class WindowManager extends JPanel implements ClientStateListener, ActionListener
10 {
11     protected SwitchBarOptions switchOptions; // we have to keep a reference to it or it will go bye bye
12
protected JToolBar switchbar;
13     protected LinkedList windows;
14     protected HashMap windowMap; /** key=<JInternalFrame> value=<StatusWindow> or some child thereof */
15
16     protected boolean isRelative; // is the bg being drawn relative to the main window, makes a difference in repainting..
17

18     public StatusWindow getWindowFor(Object JavaDoc o)
19     {
20        return (StatusWindow)windowMap.get(o);
21     }
22
23     public void addToSwitchbar(StatusWindow window)
24     {
25        if (ClientState.getClientState().isOption("switchbar.sort", ClientDefaults.switchbar_sort))
26        {
27           Iterator i = windows.iterator();
28           int pos = 0;
29           while (i.hasNext())
30           {
31              StatusWindow temp = (StatusWindow) i.next();
32              if (window.compareTo(temp) < 0)
33              {
34                 switchbar.add(window.getButton(), pos);
35                 windows.add(pos, window);
36                 switchbar.revalidate();
37                 return;
38              }
39
40              pos++;
41           }
42        }
43
44        windows.add(window);
45        switchbar.add(window.getButton());
46        switchbar.revalidate();
47     }
48
49     public void actionPerformed(ActionEvent e)
50     {
51        JToggleButton source = (JToggleButton)e.getSource();
52
53        if (source.isSelected())
54        {
55           doActivate(getWindowFor(e.getSource()));
56        }
57        else
58        {
59           doDeactivate(getWindowFor(e.getSource()));
60
61           int index = windows.indexOf(getWindowFor(e.getSource()));
62           newActive(index, false);
63        }
64     }
65
66     // find next non-minimized window to the left of the specified index without making the specified index active again
67
public void newActive(int index, boolean includeCurrent)
68     {
69        for (int x = index - 1; x >= 0; x--)
70        {
71           StatusWindow temp = (StatusWindow)windows.get(x);
72
73           if (temp != null && !temp.getWindow().isIcon())
74           {
75              doActivate(temp);
76              return;
77           }
78        }
79  
80        if (includeCurrent) { index = -1; }
81
82        for (int x = windows.size() - 1; x > index; x--)
83        {
84           StatusWindow temp = (StatusWindow)windows.get(x);
85
86           if (temp != null && !temp.getWindow().isIcon())
87           {
88              doActivate(temp);
89              return;
90           }
91        }
92     }
93
94     public void propertyChanged(String JavaDoc key, String JavaDoc value)
95     {
96        isRelative = ClientState.getClientState().isOption("window.relative", false);
97
98        if (ClientState.getClientState().isOption("switchbar.sort", ClientDefaults.switchbar_sort))
99        {
100           switchbar.removeAll();
101
102           Collections.sort(windows);
103
104           Iterator i = windows.iterator();
105           while (i.hasNext())
106           {
107              StatusWindow window = (StatusWindow)i.next();
108              switchbar.add(window.getButton());
109           }
110
111           switchbar.revalidate();
112        }
113     }
114
115     public WindowManager()
116     {
117        init();
118        ClientState.getClientState().addClientStateListener("switchbar.sort", this);
119        ClientState.getClientState().addClientStateListener("window", this);
120        isRelative = ClientState.getClientState().isOption("window.relative", false);
121     }
122
123     public LinkedList getAllWindows()
124     {
125        return windows;
126     }
127
128     public abstract void init();
129     public abstract void addWindow(StatusWindow window, boolean selected);
130     public abstract StatusWindow getActiveWindow();
131     protected abstract void doActivate(StatusWindow window);
132     protected abstract void doDeactivate(StatusWindow window);
133 }
134
Popular Tags