1
36
37
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
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
64 LayoutControlPanel(ButtonDemo demo) {
65 this.demo = demo;
66
67
69 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
70 setAlignmentX(LEFT_ALIGNMENT);
71 setAlignmentY(TOP_ALIGNMENT);
72
73 JLabel l;
74
75 absolutePositions = true;
107
109 textPosition = new DirectionPanel(true, "E", new TextPositionListener());
110 labelAlignment = new DirectionPanel(true, "C", new LabelAlignmentListener());
111
112 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 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 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 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 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 {
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 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 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 {
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 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 x.revalidate();
310 } else if(c instanceof JLabel) {
311 JLabel x = (JLabel) c;
312 x.setHorizontalTextPosition(hPos);
313 x.setVerticalTextPosition(vPos);
314 x.revalidate();
315 }
316 }
317
318 void setAlignment(Component c, int hPos, int vPos) {
319 boolean ltr = true;
320 ltr = c.getComponentOrientation().isLeftToRight();
321 if( absolutePositions ) {
322 if( hPos == LEADING ) {
323 hPos = ltr ? LEFT : RIGHT;
324 } else if( hPos == TRAILING ) {
325 hPos = ltr ? RIGHT : LEFT;
326 }
327 } else {
328 if( hPos == LEFT ) {
329 hPos = ltr ? LEADING : TRAILING;
330 } else if( hPos == RIGHT ) {
331 hPos = ltr ? TRAILING : LEADING;
332 }
333 }
334 if(c instanceof AbstractButton) {
335 AbstractButton x = (AbstractButton) c;
336 x.setHorizontalAlignment(hPos);
337 x.setVerticalAlignment(vPos);
338 } else if(c instanceof JLabel) {
339 JLabel x = (JLabel) c;
340 x.setHorizontalAlignment(hPos);
341 x.setVerticalAlignment(vPos);
342 }
343 }
344 }
345