KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > ComponentBorder


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing;
18
19 import java.awt.BasicStroke JavaDoc;
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.LayoutManager JavaDoc;
28 import java.awt.Stroke JavaDoc;
29 import java.awt.event.ComponentAdapter JavaDoc;
30 import java.awt.event.ComponentEvent JavaDoc;
31 import java.awt.event.WindowAdapter JavaDoc;
32 import java.awt.event.WindowEvent JavaDoc;
33
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.BoxLayout JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JCheckBox JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JFrame JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JTable JavaDoc;
42
43 /**
44  * This panel acts like a line border except that it holds the contents plus
45  * it allowes you to add a component on the line similar to how a TitleBorder
46  * allows you to put a string title on the border.
47  *
48  * <pre>
49  * The Component Tree for a ComponentBorder (Container)
50  *
51  * + ComponentBorder (BorderLayout)
52  * |-+ innerPanel (North)
53  * | |-+ spacingPanel
54  * | |-+ titleComponent
55  * |-+ contentsPanel (Center)
56  *
57  * </pre>
58  *
59  * @author Greg Hinkle (ghinkle@users.sourceforge.net), September 2002
60  * @version $Revision: 480 $($Author: ghinkl $ / $Date: 2004-10-05 01:17:41 -0400 (Tue, 05 Oct 2004) $)
61  */

62 public class ComponentBorder extends JPanel JavaDoc {
63     
64
65     InnerPanel innerPanel;
66     SpacingPanel spacingPanel;
67
68     /** The component that lives in the title (a JCheckBox is typical) */
69     protected JComponent JavaDoc titleComponent;
70
71     int thickness;
72     Color JavaDoc lineColor;
73     JPanel JavaDoc contentsPanel;
74     
75     public ComponentBorder(JComponent JavaDoc titleComponent, Color JavaDoc lineColor, int thickness) {
76         super();
77         this.spacingPanel = new SpacingPanel();
78         this.innerPanel = new InnerPanel();
79         this.contentsPanel = new JPanel JavaDoc();
80         this.contentsPanel.setOpaque(false);
81
82
83         //this.spacingPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
84
//this.innerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
85
//this.contentsPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
86

87         // play around with this:
88
innerPanel.setPositioning(InnerPanel.LEFT);
89         
90         // play around with this:
91
spacingPanel.setSpacing(5);
92         innerPanel.setOpaque(false);
93         spacingPanel.setOpaque(false);
94         
95         this.titleComponent = titleComponent;
96
97         this.thickness = thickness;
98         this.lineColor = lineColor;
99         
100         super.setLayout(new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS));
101         spacingPanel.add(this.titleComponent);
102         innerPanel.add(spacingPanel);
103         super.add(innerPanel);
104         int top = (int) this.titleComponent.getSize().getHeight();
105         
106         if ( top < thickness )
107             top = thickness;
108         
109         // to set insets where border can be painted
110
super.setBorder(BorderFactory.createEmptyBorder(0, thickness + 4, 0 , thickness + 4));
111         super.add(this.contentsPanel);
112
113         this.contentsPanel.setLayout(new FlowLayout JavaDoc(FlowLayout.LEFT));
114
115         this.titleComponent.addComponentListener(new ComponentAdapter JavaDoc() {
116             public void componentResized(ComponentEvent JavaDoc e) {
117                 //System.out.println("Handling title resize");
118
ComponentBorder.this.spacingPanel.invalidate();
119                 ComponentBorder.this.innerPanel.invalidate();
120             }
121         });
122     }
123
124
125     public void minimize() {
126         this.contentsPanel.setVisible(false);
127         //super.remove(this.contentsPanel);
128
invalidate();
129         doLayout();
130         repaint();
131     }
132     
133     public void maximize() {
134         //super.add(this.contentsPanel, BorderLayout.CENTER);
135
this.contentsPanel.setVisible(true);
136         invalidate();
137         doLayout();
138         repaint();
139     }
140     
141     
142     public JComponent JavaDoc getTitledComponent() {
143         return this.titleComponent;
144     }
145     
146      /**
147      * Appends the specified component to the end of this container.
148      * This is a convenience method for {@link #addImpl}.
149      *
150      * @param comp the component to be added
151      * @see #addImpl
152      * @return the component argument
153      */

154     public Component JavaDoc add(Component JavaDoc comp) {
155     return this.contentsPanel.add(comp);
156     }
157
158     /**
159      * Adds the specified component to this container.
160      * This is a convenience method for {@link #addImpl}.
161      * <p>
162      * This method is obsolete as of 1.1. Please use the
163      * method <code>add(Component, Object)</code> instead.
164      */

165     public Component JavaDoc add(String JavaDoc name, Component JavaDoc comp) {
166     return this.contentsPanel.add(name, comp);
167     }
168
169     /**
170      * Adds the specified component to this container at the given
171      * position.
172      * This is a convenience method for {@link #addImpl}.
173      *
174      * @param comp the component to be added
175      * @param index the position at which to insert the component,
176      * or <code>-1</code> to append the component to the end
177      * @return the component <code>comp</code>
178      * @see #addImpl
179      * @see #remove
180      */

181     public Component JavaDoc add(Component JavaDoc comp, int index) {
182     return this.contentsPanel.add(comp, index);
183     }
184
185     /**
186      * Adds the specified component to the end of this container.
187      * Also notifies the layout manager to add the component to
188      * this container's layout using the specified constraints object.
189      * This is a convenience method for {@link #addImpl}.
190      *
191      * @param comp the component to be added
192      * @param constraints an object expressing
193      * layout contraints for this component
194      * @see #addImpl
195      * @see LayoutManager
196      * @since JDK1.1
197      */

198     public void add(Component JavaDoc comp, Object JavaDoc constraints) {
199     this.contentsPanel.add(comp, constraints);
200     }
201
202     /**
203      * Adds the specified component to this container with the specified
204      * constraints at the specified index. Also notifies the layout
205      * manager to add the component to the this container's layout using
206      * the specified constraints object.
207      * This is a convenience method for {@link #addImpl}.
208      *
209      * @param comp the component to be added
210      * @param constraints an object expressing layout contraints for this
211      * @param index the position in the container's list at which to insert
212      * the component. -1 means insert at the end.
213      * component
214      * @see #addImpl
215      * @see #remove
216      * @see LayoutManager
217      */

218     public void add(Component JavaDoc comp, Object JavaDoc constraints, int index) {
219        this.contentsPanel.add(comp, constraints, index);
220     }
221     
222     
223     public void setLayout(LayoutManager JavaDoc layoutManager) {
224         if (this.contentsPanel != null)
225         this.contentsPanel.setLayout(layoutManager);
226     }
227     
228     public void setOuterComponentPosition(int position) {
229         this.innerPanel.setPositioning(position);
230     }
231     
232     
233     public void paint(Graphics JavaDoc g) {
234         innerPanel.setLocation(0, 0);
235         super.paint(g);
236     }
237     
238     
239     public void paintBorder(Graphics JavaDoc g) {
240         Dimension JavaDoc d = getSize();
241         int panelHeight = innerPanel.getHeight();
242         int outerSpacing = thickness/2;
243         int x = outerSpacing;
244         int y = panelHeight / 2;
245         int width = d.width - 1 - 2*outerSpacing;
246         int height = d.height - y - 1 -outerSpacing;
247         
248         //g.drawRoundRect(x, y, width, height, thickness, thickness);
249
Graphics2D JavaDoc gg = (Graphics2D JavaDoc) g;
250         Stroke JavaDoc stroke = gg.getStroke();
251         gg.setStroke(new BasicStroke JavaDoc(2));
252         gg.setColor(new Color JavaDoc(153,204,255));
253         // Draws a border
254
//gg.draw3DRect(x, y, width, height, true);
255

256         gg.drawLine(x + 15, y + 7, x + width - 15, y + 7);
257         gg.setStroke(stroke);
258         
259     }
260
261     public Dimension JavaDoc getMaximumSize() {
262         //Component comp = this.innerPanel.getComponent(0);
263
//System.out.println(comp);
264

265         Dimension JavaDoc cpMax = this.contentsPanel.getComponent(0).getMaximumSize();
266         if (!this.contentsPanel.isVisible())
267             cpMax = new Dimension JavaDoc(0,0);
268
269         Dimension JavaDoc tMax = this.innerPanel.getMaximumSize();
270
271         //System.out.println("Contents max: " + cpMax);
272
//System.out.println("Title max: " + tMax);
273

274         int w = Integer.MAX_VALUE;
275             //tMax.getWidth());
276
int h = (int)
277             (cpMax.getHeight() + 10 +
278             tMax.getHeight());
279
280         Dimension JavaDoc pSize = new Dimension JavaDoc(w, h);
281         //System.out.println("MAX: " + pSize);
282
return pSize;
283     }
284
285     public Dimension JavaDoc getMinimumSize() {
286         return getPreferredSize();
287     }
288
289     public Dimension JavaDoc getPreferredSize() {
290         Dimension JavaDoc cpPref = this.contentsPanel.getComponent(0).getPreferredSize();
291         Dimension JavaDoc tPref = this.innerPanel.getPreferredSize();
292
293         if (!this.contentsPanel.isVisible())
294             cpPref = new Dimension JavaDoc(0,0);
295         else
296             cpPref.setSize(cpPref.getWidth(), cpPref.getHeight());
297
298         //System.out.println("Contents pref: " + cpPref);
299
//System.out.println("Title pref: " + tPref);
300

301         int w = (int)
302             (cpPref.getWidth() + tPref.getWidth());
303         int h = (int)
304             (cpPref.getHeight() + 10 +
305             tPref.getHeight());
306
307         Dimension JavaDoc pSize = new Dimension JavaDoc(w, h);
308
309         //System.out.println("PREFERRED SIZE: " + pSize);
310
return pSize;
311     }
312     
313     
314     public class InnerPanel extends JPanel JavaDoc {
315         public static final int CUSTOM = 0;
316         public static final int LEFT = 1;
317         public static final int CENTER = 2;
318         public static final int RIGHT = 3;
319         private int positioning;
320         private int position;
321         
322         
323         public InnerPanel() {
324             this.positioning = CUSTOM;
325             this.position = 15;
326             this.setOpaque(false);
327             this.setDoubleBuffered(false);
328         }
329         
330         public void setPositioning(int p) {
331             this.positioning = p;
332         }
333         
334         public Dimension JavaDoc getPreferredSize() {
335             return getComponent(0).getPreferredSize();
336         }
337
338         public Dimension JavaDoc getMaximumSize() {
339             Dimension JavaDoc size = getPreferredSize();
340
341             return new Dimension JavaDoc(Integer.MAX_VALUE, (int) size.getHeight());
342         }
343
344
345         public void paint(Graphics JavaDoc g) {
346             if (positioning < 0 || positioning > 3)
347                 positioning = 1;
348             switch (positioning) {
349                 case CUSTOM:
350                     getComponent(0).setLocation(position, 0);
351                     break;
352                 case LEFT:
353                     getComponent(0).setLocation(15, 0);
354                     break;
355                 case CENTER:
356                     getComponent(0).setLocation(getParent().getWidth() / 2 - getPreferredSize().width / 2, 0);
357                     break;
358                 case RIGHT:
359                     getComponent(0).setLocation(getParent().getWidth() - getPreferredSize().width - 15, 0);
360                     break; }
361             super.paint(g);
362         }
363     }
364     
365     public class SpacingPanel extends JPanel JavaDoc {
366         private int spacing;
367         
368         public SpacingPanel() {
369             this.spacing = 0;
370             this.setOpaque(false);
371         }
372         
373         public void setSpacing(int s) {
374             this.spacing = s;
375         }
376         
377         public void paintComponent(Graphics JavaDoc g) {
378             getComponent(0).setLocation(spacing, 0);
379             super.paintComponent(g);
380         }
381         
382         public Dimension JavaDoc getPreferredSize() {
383             Dimension JavaDoc ps = getComponent(0).getPreferredSize();
384             Dimension JavaDoc d = new Dimension JavaDoc(ps.width + 2*spacing, ps.height);
385             return d;
386         }
387     }
388     
389     public static void main(String JavaDoc args[]) {
390         JFrame JavaDoc fr = new JFrame JavaDoc();
391         //JComboBox titleComponent = new JComboBox(new Object[] { "First", "Second", "Third", "Greg is awesome" });
392
JCheckBox JavaDoc button = new JCheckBox JavaDoc();
393         
394         button.setIcon(
395             new ImageIcon JavaDoc("ToggleOpen.gif"));
396         button.setDisabledIcon(
397             new ImageIcon JavaDoc("ToggleClosed.gif"));
398         button.setSelectedIcon(
399             new ImageIcon JavaDoc("org/mc4j/console/ToggleChanging.gif"));
400         button.setDisabledSelectedIcon(
401             new ImageIcon JavaDoc("org/mc4j/console/ToggleChanging.gif"));
402         
403         //titleComponent.setEditable(true);
404

405         
406         ComponentBorder c1 = new ComponentBorder(button, Color.darkGray, 4);
407         
408         c1.setLayout(new BorderLayout JavaDoc());
409         c1.add(new JTable JavaDoc(new Object JavaDoc[][] { { "A", "B", "C" }, { "Foo", "Bar", "Baz"}, { "X", "Y", "Z" }}, new Object JavaDoc[] { "First", "Second", "Third" }) ,BorderLayout.CENTER);
410         
411         fr.getContentPane().add(c1);
412         fr.pack();
413         fr.setVisible(true);
414         fr.addWindowListener(new WindowAdapter JavaDoc() {
415             public void windowClosing(WindowEvent JavaDoc e) {
416                 System.exit(0);
417             }
418         });
419     }
420     
421 }
422
Popular Tags