KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > toolkit > OrientedToolBar


1 package rero.gui.toolkit;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5
6 import java.awt.*;
7 import java.awt.event.*;
8
9 import rero.config.*;
10
11 public class OrientedToolBar extends JToolBar implements ClientStateListener
12 {
13    public static final int BUTTON_FIXED_WIDTH = 95;
14
15    private FlowLayout2 fixed = null;
16    private GridLayout2 fill = null;
17
18    public OrientedToolBar()
19    {
20       setFloatable(false);
21       propertyChanged(null, null);
22
23       ClientState.getClientState().addClientStateListener("switchbar.fixed", this);
24 // ClientState.getClientState().addClientStateListener("switchbar.position", this);
25
}
26
27    public void propertyChanged(String JavaDoc var, String JavaDoc var2)
28    {
29       int orientation = ClientState.getClientState().getInteger("switchbar.position", 0);
30  
31       if (orientation == 2 || orientation == 3)
32       {
33 // setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
34
setLayout(new GridLayout3());
35          validate();
36       }
37       else if (ClientState.getClientState().isOption("switchbar.fixed", ClientDefaults.switchbar_fixed))
38       {
39          if (fixed == null || fill == null)
40          {
41             fixed = new FlowLayout2(FlowLayout.LEFT, 0, 0);
42             fill = new GridLayout2();
43          }
44
45          setLayout(fixed); // not perfect but whatever...
46
validate();
47       }
48       else
49       {
50          setLayout(new GridLayout());
51          validate();
52       }
53    }
54
55    /** not really applicable anymore, jIRCii doesn't let you dock the switchbar to the left or right anymore,
56        if that feature is added then this method will need to be updated */

57    public void setOrientation (int o)
58    {
59       propertyChanged(null, null);
60    }
61
62    private class FlowLayout2 extends FlowLayout
63    {
64       public FlowLayout2(int dir, int vgap, int hgap)
65       {
66          super(dir, vgap, hgap);
67       }
68
69       public void layoutContainer(Container c)
70       {
71       
72          if ((c.getComponentCount() * (BUTTON_FIXED_WIDTH + 2)) < c.getWidth())
73          {
74             super.layoutContainer(c);
75          }
76          else
77          {
78             c.setLayout(fill);
79             c.validate();
80          }
81       }
82    }
83
84    private class GridLayout2 extends GridLayout
85    {
86        public void layoutContainer(Container c)
87        {
88          if ((c.getComponentCount() * (BUTTON_FIXED_WIDTH + 2)) < c.getWidth())
89          {
90             c.setLayout(fixed);
91             c.validate();
92          }
93          else
94          {
95             super.layoutContainer(c);
96          }
97       }
98    }
99
100
101    private class GridLayout3 extends GridLayout
102    {
103       public int getColumns() { return 1; }
104
105       public void layoutContainer(Container c)
106       {
107          if (c.getComponentCount() > 0)
108          {
109             int buttonheight = getComponent(0).getPreferredSize().height + getVgap();
110
111             if ((buttonheight * c.getComponentCount()) > c.getHeight())
112             {
113                setRows(c.getComponentCount());
114                setColumns(1);
115             }
116             else
117             {
118                setRows(c.getHeight() / buttonheight);
119                setColumns(1);
120             }
121          }
122
123          super.layoutContainer(c);
124       }
125    }
126 }
127
128
Popular Tags