KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > LayoutControlPanel


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)LayoutControlPanel.java 1.6 03/01/23
38  */

39 package demo.swingset;
40 import swingwtx.swing.*;
41 import swingwtx.swing.text.*;
42 import swingwtx.swing.border.*;
43
44 import swingwt.awt.*;
45 import swingwt.awt.event.*;
46 import java.util.*;
47
48 /*
49  * The LayoutControlPanel contains controls for setting an
50  * AbstractButton's horizontal and vertical text position and
51  * horizontal and vertical alignment.
52  */

53
54 public class LayoutControlPanel extends JPanel implements SwingConstants {
55
56     private boolean absolutePositions;
57     private DirectionPanel textPosition = null;
58     private DirectionPanel labelAlignment = null;
59     private ButtonDemo demo = null;
60
61     // private ComponentOrientChanger componentOrientChanger = null;
62

63     LayoutControlPanel(ButtonDemo demo) {
64         this.demo = demo;
65
66         // this.componentOrientationChanger = componentOrientationChanger;
67

68     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
69     setAlignmentX(LEFT_ALIGNMENT);
70     setAlignmentY(TOP_ALIGNMENT);
71
72         JLabel l;
73
74         // If SwingSet has a ComponentOrientationChanger, then include control
75
// for choosing between absolute and relative positioning. This will
76
// only happen when we're running on JDK 1.2 or above.
77
//
78
// if(componentOrientationChanger != null ) {
79
// l = new JLabel("Positioning:");
80
// add(l);
81
//
82
// ButtonGroup group = new ButtonGroup();
83
// PositioningListener positioningListener = new PositioningListener();
84
// JRadioButton absolutePos = new JRadioButton("Absolute");
85
// absolutePos.setMnemonic('a');
86
// absolutePos.setToolTipText("Text/Content positioning is independant of line direction");
87
// group.add(absolutePos);
88
// absolutePos.addItemListener(positioningListener);
89
// add(absolutePos);
90
//
91
// JRadioButton relativePos = new JRadioButton("Relative");
92
// relativePos.setMnemonic('r');
93
// relativePos.setToolTipText("Text/Content positioning depends on line direction.");
94
// group.add(relativePos);
95
// relativePos.addItemListener(positioningListener);
96
// add(relativePos);
97
//
98
// add(Box.createRigidArea(demo.VGAP20));
99
//
100
// absolutePositions = false;
101
// relativePos.setSelected(true);
102
//
103
// componentOrientationChanger.addActionListener( new OrientationChangeListener() );
104
//} else {
105
absolutePositions = true;
106         //}
107

108         textPosition = new DirectionPanel(true, "E", new TextPositionListener());
109         labelAlignment = new DirectionPanel(true, "C", new LabelAlignmentListener());
110
111         // Make sure the controls' text position and label alignment match
112
// the initial value of the associated direction panel.
113
for(int i = 0; i < demo.getCurrentControls().size(); i++) {
114             Component c = (Component) demo.getCurrentControls().elementAt(i);
115             setPosition(c, RIGHT, CENTER);
116             setAlignment(c,CENTER,CENTER);
117         }
118
119         l = new JLabel("Text Position:");
120         add(l);
121         add(textPosition);
122
123         add(Box.createRigidArea(demo.VGAP20));
124
125         l = new JLabel("Content Alignment:");
126         add(l);
127         add(labelAlignment);
128
129         add(Box.createGlue());
130     }
131
132
133     class OrientationChangeListener implements ActionListener {
134         public void actionPerformed( ActionEvent e ) {
135             if( !e.getActionCommand().equals("OrientationChanged") ){
136                 return;
137             }
138             if( absolutePositions ){
139                 return;
140             }
141             
142             String JavaDoc currentTextPosition = textPosition.getSelection();
143             if( currentTextPosition.equals("NW") )
144                 textPosition.setSelection("NE");
145             else if( currentTextPosition.equals("NE") )
146                 textPosition.setSelection("NW");
147             else if( currentTextPosition.equals("E") )
148                 textPosition.setSelection("W");
149             else if( currentTextPosition.equals("W") )
150                 textPosition.setSelection("E");
151             else if( currentTextPosition.equals("SE") )
152                 textPosition.setSelection("SW");
153             else if( currentTextPosition.equals("SW") )
154                 textPosition.setSelection("SE");
155
156             String JavaDoc currentLabelAlignment = labelAlignment.getSelection();
157             if( currentLabelAlignment.equals("NW") )
158                 labelAlignment.setSelection("NE");
159             else if( currentLabelAlignment.equals("NE") )
160                 labelAlignment.setSelection("NW");
161             else if( currentLabelAlignment.equals("E") )
162                 labelAlignment.setSelection("W");
163             else if( currentLabelAlignment.equals("W") )
164                 labelAlignment.setSelection("E");
165             else if( currentLabelAlignment.equals("SE") )
166                 labelAlignment.setSelection("SW");
167             else if( currentLabelAlignment.equals("SW") )
168                 labelAlignment.setSelection("SE");
169         }
170     }
171
172     class PositioningListener implements ItemListener {
173
174     public void itemStateChanged(ItemEvent e) {
175         JRadioButton rb = (JRadioButton) e.getSource();
176         if(rb.getText().equals("Absolute") && rb.isSelected()) {
177         absolutePositions = true;
178         } else if(rb.getText().equals("Relative") && rb.isSelected()) {
179         absolutePositions = false;
180         }
181             
182         for(int i = 0; i < demo.getCurrentControls().size(); i++) {
183         Component c = (Component) demo.getCurrentControls().elementAt(i);
184                 int hPos, vPos, hAlign, vAlign;
185                 if( c instanceof AbstractButton ) {
186                    hPos = ((AbstractButton)c).getHorizontalTextPosition();
187                    vPos = ((AbstractButton)c).getVerticalTextPosition();
188                    hAlign = ((AbstractButton)c).getHorizontalAlignment();
189                    vAlign = ((AbstractButton)c).getVerticalAlignment();
190                 } else if( c instanceof JLabel ) {
191                    hPos = ((JLabel)c).getHorizontalTextPosition();
192                    vPos = ((JLabel)c).getVerticalTextPosition();
193                    hAlign = ((JLabel)c).getHorizontalAlignment();
194                    vAlign = ((JLabel)c).getVerticalAlignment();
195                 } else {
196                     continue;
197                 }
198                 setPosition(c, hPos, vPos);
199                 setAlignment(c, hAlign, vAlign);
200         }
201             
202         demo.invalidate();
203         demo.validate();
204         demo.repaint();
205     }
206     };
207
208
209     // Text Position Listener
210
class TextPositionListener implements ActionListener {
211     public void actionPerformed(ActionEvent e) {
212         JRadioButton rb = (JRadioButton) e.getSource();
213         if(!rb.isSelected()) {
214                 return;
215             }
216             String JavaDoc cmd = rb.getActionCommand();
217             int hPos, vPos;
218             if(cmd.equals("NW")) {
219                     hPos = LEFT; vPos = TOP;
220             } else if(cmd.equals("N")) {
221                     hPos = CENTER; vPos = TOP;
222             } else if(cmd.equals("NE")) {
223                     hPos = RIGHT; vPos = TOP;
224             } else if(cmd.equals("W")) {
225                     hPos = LEFT; vPos = CENTER;
226             } else if(cmd.equals("C")) {
227                     hPos = CENTER; vPos = CENTER;
228             } else if(cmd.equals("E")) {
229                     hPos = RIGHT; vPos = CENTER;
230             } else if(cmd.equals("SW")) {
231                     hPos = LEFT; vPos = BOTTOM;
232             } else if(cmd.equals("S")) {
233                     hPos = CENTER; vPos = BOTTOM;
234             } else /*if(cmd.equals("SE"))*/ {
235                     hPos = RIGHT; vPos = BOTTOM;
236             }
237             for(int i = 0; i < demo.getCurrentControls().size(); i++) {
238                 Component c = (Component) demo.getCurrentControls().elementAt(i);
239                 setPosition(c, hPos, vPos);
240             }
241             demo.invalidate();
242             demo.validate();
243             demo.repaint();
244     }
245     };
246
247
248     // Label Alignment Listener
249
class LabelAlignmentListener implements ActionListener {
250     public void actionPerformed(ActionEvent e) {
251         JRadioButton rb = (JRadioButton) e.getSource();
252         if(!rb.isSelected()) {
253                 return;
254             }
255             String JavaDoc cmd = rb.getActionCommand();
256             int hPos, vPos;
257             if(cmd.equals("NW")) {
258                     hPos = LEFT; vPos = TOP;
259             } else if(cmd.equals("N")) {
260                     hPos = CENTER; vPos = TOP;
261             } else if(cmd.equals("NE")) {
262                     hPos = RIGHT; vPos = TOP;
263             } else if(cmd.equals("W")) {
264                     hPos = LEFT; vPos = CENTER;
265             } else if(cmd.equals("C")) {
266                     hPos = CENTER; vPos = CENTER;
267             } else if(cmd.equals("E")) {
268                     hPos = RIGHT; vPos = CENTER;
269             } else if(cmd.equals("SW")) {
270                     hPos = LEFT; vPos = BOTTOM;
271             } else if(cmd.equals("S")) {
272                     hPos = CENTER; vPos = BOTTOM;
273             } else /*if(cmd.equals("SE"))*/ {
274                     hPos = RIGHT; vPos = BOTTOM;
275             }
276             for(int i = 0; i < demo.getCurrentControls().size(); i++) {
277                 Component c = (Component) demo.getCurrentControls().elementAt(i);
278                 setAlignment(c,hPos,vPos);
279                 c.invalidate();
280             }
281             demo.invalidate();
282             demo.validate();
283             demo.repaint();
284     }
285     };
286
287     // Position
288
void setPosition(Component c, int hPos, int vPos) {
289         boolean ltr = true;
290         ltr = c.getComponentOrientation().isLeftToRight();
291         if( absolutePositions ) {
292             if( hPos == LEADING ) {
293                 hPos = ltr ? LEFT : RIGHT;
294             } else if( hPos == TRAILING ) {
295                 hPos = ltr ? RIGHT : LEFT;
296             }
297         } else {
298             if( hPos == LEFT ) {
299                 hPos = ltr ? LEADING : TRAILING;
300             } else if( hPos == RIGHT ) {
301                 hPos = ltr ? TRAILING : LEADING;
302             }
303         }
304         if(c instanceof AbstractButton) {
305             AbstractButton x = (AbstractButton) c;
306             x.setHorizontalTextPosition(hPos);
307             x.setVerticalTextPosition(vPos);
308         } else if(c instanceof JLabel) {
309             JLabel x = (JLabel) c;
310             x.setHorizontalTextPosition(hPos);
311             x.setVerticalTextPosition(vPos);
312         }
313     }
314
315     void setAlignment(Component c, int hPos, int vPos) {
316         boolean ltr = true;
317         ltr = c.getComponentOrientation().isLeftToRight();
318         if( absolutePositions ) {
319             if( hPos == LEADING ) {
320                 hPos = ltr ? LEFT : RIGHT;
321             } else if( hPos == TRAILING ) {
322                 hPos = ltr ? RIGHT : LEFT;
323             }
324         } else {
325             if( hPos == LEFT ) {
326                 hPos = ltr ? LEADING : TRAILING;
327             } else if( hPos == RIGHT ) {
328                 hPos = ltr ? TRAILING : LEADING;
329             }
330         }
331         if(c instanceof AbstractButton) {
332             AbstractButton x = (AbstractButton) c;
333             x.setHorizontalAlignment(hPos);
334             x.setVerticalAlignment(vPos);
335         } else if(c instanceof JLabel) {
336             JLabel x = (JLabel) c;
337             x.setHorizontalAlignment(hPos);
338             x.setVerticalAlignment(vPos);
339         }
340     }
341 }
342
Popular Tags