KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LayoutControlPanel


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

36
37 /*
38  * @(#)LayoutControlPanel.java 1.12 05/11/17
39  */

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

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

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

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

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