KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > GraphicalToolbar


1 package rero.gui;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5
6 import java.awt.*;
7 import java.awt.event.*;
8
9 import java.awt.image.*;
10
11 import rero.config.*;
12
13 import rero.gui.toolbar.*;
14 import java.util.*;
15
16 public class GraphicalToolbar extends JToolBar
17 {
18    public static ImageIcon[] images = new ImageIcon[37];
19    protected LinkedList tools = new LinkedList();
20
21    private static GraphicalToolbar toolbar = null;
22    private static StateListener state; // we have to keep a reference or else it will get garbage collected
23

24    public static void startup()
25    {
26       // set myself up... why make the other classes do all of the work, eh?
27

28       state = new StateListener();
29       state.propertyChanged("ui.usetoolbar", "");
30
31       ClientState.getClientState().addClientStateListener("ui.usetoolbar", state);
32    }
33
34    /** this method is cheating and I should feel very guilty for doing my toolbar code like this, I'll probably pay for it later */
35    public static void stateChanged()
36    {
37       if (toolbar != null)
38          toolbar.refresh();
39    }
40
41    public void refresh()
42    {
43       Iterator i = tools.iterator();
44       while (i.hasNext())
45       {
46          ((ToolButton)i.next()).displayIcon();
47       }
48    }
49
50    private static class StateListener implements ClientStateListener
51    {
52       public void propertyChanged(String JavaDoc property, String JavaDoc parameter)
53       {
54          if (toolbar != null)
55             SessionManager.getGlobalCapabilities().getFrame().getContentPane().remove(toolbar);
56
57          if (ClientState.getClientState().isOption("ui.usetoolbar", ClientDefaults.ui_usetoolbar))
58          {
59             if (toolbar == null)
60                toolbar = new GraphicalToolbar();
61
62             SessionManager.getGlobalCapabilities().getFrame().getContentPane().add(toolbar, BorderLayout.NORTH);
63          }
64
65         if (toolbar != null)
66           SessionManager.getGlobalCapabilities().getFrame().getContentPane().validate();
67       }
68    }
69
70    public GraphicalToolbar()
71    {
72       int[] tiles;
73       ToolAction[] actions;
74
75       if (ClientState.getClientState().isOption("ui.sdi", ClientDefaults.ui_sdi))
76       {
77          tiles = new int[] { 0, -1, 4, 7, -1, 10, -1, 22, 23, 24, -1, 26, 28, -1, 35, 36 };
78          actions = new ToolAction[] {
79             new ConnectAction(),
80             null,
81             new OptionsAction(),
82             new ScriptAction(), // popup menus aka script manager
83
null,
84             new ListAction(), // list action
85
null,
86             new SendAction(), // dcc send
87
new ChatAction(), // dcc chat
88
new DCCAction(), // dcc options
89
null,
90             new NotifyAction(), // notify list options
91
new NotifyAction2(), // notify list options
92
null,
93             new HelpAction(),
94             new AboutAction(),
95             new EvilAction()
96          };
97       }
98       else
99       {
100          tiles = new int[] { 0, -1, 4, 7, -1, 10, -1, 22, 23, 24, -1, 26, 28, -1, 31, 32, -1, 35, 36 };
101          actions = new ToolAction[] {
102             new ConnectAction(),
103             null,
104             new OptionsAction(),
105             new ScriptAction(), // popup menus aka script manager
106
null,
107             new ListAction(), // list action
108
null,
109             new SendAction(), // dcc send
110
new ChatAction(), // dcc chat
111
new DCCAction(), // dcc options
112
null,
113             new NotifyAction(), // notify list options
114
new NotifyAction2(), // notify list options
115
null,
116             new CascadeAction(),
117             new TileAction(),
118             null,
119             new HelpAction(),
120             new AboutAction(),
121             new EvilAction()
122          };
123       }
124
125       BufferedImage all = LoadToolbarImage();
126
127       for (int x = 0; x < 37; x++)
128       {
129          images[x] = new ImageIcon(all.getSubimage(x * 16, 0, 16, 16));
130       }
131
132       for (int x = 0; x < actions.length; x++)
133       {
134          if (actions[x] == null)
135          {
136             addSeparator(new Dimension(12, 12));
137          }
138          else
139          {
140             ToolButton temp = new ToolButton(actions[x]);
141             add(temp);
142             tools.add(temp);
143          }
144       }
145
146       setFloatable(false);
147       setBorderPainted(true);
148    }
149    
150    private static class ToolButton extends JLabel
151    {
152       private ImageIcon normal;
153       private ImageIcon pressed;
154       private ToolAction action;
155
156       public ToolButton(ToolAction taction)
157       {
158          setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
159
160          addMouseListener(new Listeners());
161
162          action = taction;
163          displayIcon();
164       }
165
166       public void displayIcon()
167       {
168          normal = images[action.getIndex()];
169          pressed = new ImageIcon(GrayFilter.createDisabledImage(normal.getImage()));
170          setToolTipText(action.getDescription());
171
172          setIcon(normal);
173       }
174
175       private class Listeners extends MouseAdapter
176       {
177          public void mouseClicked(MouseEvent ev)
178          {
179             action.actionPerformed(ev);
180          }
181
182          public void mousePressed(MouseEvent ev)
183          {
184             setIcon(pressed);
185          }
186
187          public void mouseReleased(MouseEvent ev)
188          {
189             setIcon(normal);
190          }
191       }
192    }
193
194    private static BufferedImage LoadToolbarImage()
195    {
196       ImageIcon original = ClientState.getClientState().getIcon("jirc.toolbar", "toolbar.gif");
197      
198       Image image = original.getImage();
199       BufferedImage value = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
200
201       Graphics g = value.getGraphics();
202       g.drawImage(image, 0, 0, null);
203
204       original.getImage().flush();
205       
206       return value;
207    }
208 }
209
Popular Tags