Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/trapezium/chisel/gui/ChiselAWTDividedPane.java


1   /*  ChiselAWTDividedPane
2    *
3    */
4   
5   package com.trapezium.chisel.gui;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   import java.io.*;
10  import java.net.URL;
11  import java.util.*;
12  
13  
14  public class ChiselAWTDividedPane extends ChiselAWTPane implements ActionListener {
15  
16      DividerBarComponent db;
17      OrientationButton ob;
18  
19      public ChiselAWTDividedPane(int align, Component before, Component after) {
20          super();
21          setOpaque(false);
22          setLayout(null);
23          setBackground(Color.gray);
24          ob = new OrientationButton();
25          ob.addActionListener(this);
26          add(ob);
27          add(before);
28          add(after);
29          db = new DividerBarComponent(align, before, after);
30          add(db);
31      }
32  
33      public Dimension getMinimumSize() {
34          Dimension dim = db.getMinimumSize();;
35          Dimension minbefore = db.getComponentBefore().getMinimumSize();
36          Dimension minafter = db.getComponentAfter().getMinimumSize();
37          if (db.getAlign() == DividerBarComponent.HORIZONTAL) {
38              dim.width += minbefore.width + minafter.width;
39              dim.height = Math.max(dim.height, Math.max(minbefore.height, minafter.height));
40          } else {
41              dim.width = Math.max(dim.width, Math.max(minbefore.width, minafter.width));
42              dim.height += minbefore.height + minafter.height;
43          }
44          return dim;
45      }
46  
47      public Dimension getPreferredSize() {
48          Dimension dim = db.getPreferredSize();
49          Dimension prefbefore = db.getComponentBefore().getPreferredSize();
50          Dimension prefafter = db.getComponentAfter().getPreferredSize();
51          if (db.getAlign() == DividerBarComponent.HORIZONTAL) {
52              dim.width += prefbefore.width + prefafter.width;
53              dim.height = Math.max(dim.height, Math.max(prefbefore.height, prefafter.height));
54          } else {
55              dim.width = Math.max(dim.width, Math.max(prefbefore.width, prefafter.width));
56              dim.height += prefbefore.height + prefafter.height;
57          }
58          return dim;
59      }
60  
61      int yHoriz = -1;
62      int xVert = -1;
63      public void doLayout() {
64          db.layoutContainer();
65          Rectangle rect = db.getBounds();
66          if (db.getAlign() == DividerBarComponent.HORIZONTAL) {
67              // remember the x position in case we switch alignment
68              xVert = rect.x + rect.width/2;
69              rect.x -= 5;
70              rect.width += 10;
71              if (yHoriz < 0) {
72                  yHoriz = rect.height/2;
73              }
74              rect.height = rect.width;
75              rect.y = yHoriz - rect.height/2;
76          } else {
77              // remember the y position in case we switch alignment
78              yHoriz = rect.y + rect.height/2;
79              rect.y -= 5;
80              rect.height += 10;
81              if (xVert < 0) {
82                  xVert = rect.width/2;
83              }
84              rect.width = rect.height;
85              rect.x = xVert - rect.width/2;
86          }
87          ob.setBounds(rect);
88          ob.setAlign(db.getAlign());
89          super.doLayout();
90      }
91  
92      /** rotate the pane from horizontal to vertical or vice versa */
93       public void actionPerformed( ActionEvent e ) {
94           Component comp = db.getComponentBefore();
95           Dimension totalsize = getSize();
96           Dimension compsize = comp.getSize();
97           if (db.getAlign() == DividerBarComponent.HORIZONTAL) {
98               db.setAlign(DividerBarComponent.VERTICAL);
99               compsize.height = yHoriz;  // totalsize.height * compsize.width / totalsize.width;
100              compsize.width = totalsize.width;
101         } else {
102              db.setAlign(DividerBarComponent.HORIZONTAL);
103              compsize.width = xVert; // totalsize.width * compsize.height / totalsize.height;
104              compsize.height = totalsize.height;
105         }
106          comp.setSize(compsize);
107          validate();
108      }
109 }
110 
111 class OrientationButton extends LabelledImageButton implements DisplayConstants {
112 
113     int align = DividerBarComponent.HORIZONTAL;
114 
115     final static Color bg = new Color(204, 153, 51);
116     public OrientationButton() {
117         super( "", null );
118         setBackground( bg /*DEFAULT_CONTROLCOLOR*/);
119     }
120 
121     public void setAlign(int align) {
122         this.align = align;
123     }
124 
125   public void paint(Graphics g) {
126      Dimension size = getSize();
127     int x = insets.left;
128     int y = insets.top;
129     int width = size.width - insets.left - insets.right;
130     int height = size.height - insets.top - insets.bottom;
131 
132     Color bg = getBackground();
133         Color hiliteColor = bg.brighter();
134         Color liteShadowColor = bg.darker();
135         Color shadowColor = liteShadowColor.darker();
136 
137         // bar indicating alignment if pushed
138         g.setColor(shadowColor);
139         int dxy = 4;
140         int barx, bary, barwidth, barheight;
141         if (align == DividerBarComponent.HORIZONTAL) {
142             barx = x;
143             bary = y + (height / 2) - (dxy / 2);
144             barwidth = width;
145             barheight = dxy;
146             for (int xx = barx - dxy; xx < barx + barwidth - 1; xx += 2) {
147                 g.drawLine(xx + 1, bary, xx + dxy, bary + dxy - 1);
148             }
149         } else {
150             barx = x + (width / 2) - (dxy / 2);
151             bary = y;
152             barwidth = dxy;
153             barheight = height;
154             for (int yy = bary - dxy; yy < bary + barheight - 1; yy += 2) {
155                 g.drawLine(barx, yy + 1, barx + dxy - 1, yy + dxy);
156             }
157         }
158 
159         // shrink the square we're drawing in
160         x += 4;
161         y += 4;
162         width -= 8;
163         height -= 8;
164 
165         g.setColor(bg);
166         g.drawOval(x, y, width - 1, height - 1);
167         g.drawOval(x, y + 1, width - 1, height - 2);
168         g.drawOval(x, y, width - 1, height - 2);
169 
170         // button is drawn in the down state when any of the following are true:
171         //
172         //   -- the button is in a sticky down state
173         //   -- the button is pressed and the mouse is within the bounds of the button
174         //   -- the margins are symmetrical
175         if (stickyDown || (pressed && contains(mousex, mousey)) || (margin.top == margin.bottom && margin.left == margin.right)) {
176         g.setColor(shadowColor);
177         g.drawOval(x, y, width - 1, height - 1);
178 
179         // button up
180         } else {
181         g.setColor(shadowColor);
182         g.drawOval(x + 1, y + 1, width - 2, height - 2);
183         g.setColor(hiliteColor);
184         g.drawOval(x, y, width - 2, height - 2);
185         //g.setColor(liteShadowColor);
186         //g.drawLine(x + 1, y + 1, width - 2, height - 2);
187 
188             g.setColor(bg);
189             g.drawOval(x + 1, y + 1, width - 3, height - 3);
190         }
191 
192   }
193 }