1/*
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package com.sun.swingset3.demos.togglebutton;
24
25import java.awt.*;
26import java.awt.event.ActionEvent;
27import java.awt.event.ActionListener;
28import javax.swing.*;
29
30import com.sun.swingset3.demos.ResourceManager;
31
32/*
33 * The LayoutControlPanel contains controls for setting an
34 * AbstractButton's horizontal and vertical text position and
35 * horizontal and vertical alignment.
36 */
37public final class LayoutControlPanel extends JPanel implements SwingConstants {
38
39    private static final Dimension VGAP20 = new Dimension(1, 20);
40    private static final ResourceManager resourceManager = ToggleButtonDemo.resourceManager;
41    public static final String CONTENT_ALIGNMENT = resourceManager.getString("LayoutControlPanel.contentalignment_label");
42    public static final String TEXT_POSITION = resourceManager.getString("LayoutControlPanel.textposition_label");
43    private final boolean absolutePositions;
44    private ToggleButtonDemo demo = null;
45
46    // private ComponentOrientChanger componentOrientChanger = null;
47    LayoutControlPanel(ToggleButtonDemo demo) {
48        this.demo = demo;
49
50        // this.componentOrientationChanger = componentOrientationChanger;
51        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
52        setAlignmentX(LEFT_ALIGNMENT);
53        setAlignmentY(TOP_ALIGNMENT);
54
55        JLabel l;
56
57        // If SwingSet has a ComponentOrientationChanger, then include control
58        // for choosing between absolute and relative positioning.  This will
59        // only happen when we're running on JDK 1.2 or above.
60        //
61        // if(componentOrientationChanger != null ) {
62        //     l = new JLabel("Positioning:");
63        //     add(l);
64        //
65        //    ButtonGroup group = new ButtonGroup();
66        //    PositioningListener positioningListener = new PositioningListener();
67        //    JRadioButton absolutePos = new JRadioButton("Absolute");
68        //    absolutePos.setMnemonic('a');
69        //    absolutePos.setToolTipText("Text/Content positioning is independant of line direction");
70        //    group.add(absolutePos);
71        //    absolutePos.addItemListener(positioningListener);
72        //    add(absolutePos);
73        //
74        //    JRadioButton relativePos = new JRadioButton("Relative");
75        //    relativePos.setMnemonic('r');
76        //    relativePos.setToolTipText("Text/Content positioning depends on line direction.");
77        //    group.add(relativePos);
78        //    relativePos.addItemListener(positioningListener);
79        //    add(relativePos);
80        //
81        //    add(Box.createRigidArea(demo.VGAP20));
82        //
83        //    absolutePositions = false;
84        //    relativePos.setSelected(true);
85        //
86        //    componentOrientationChanger.addActionListener( new OrientationChangeListener() );
87        //} else {
88        absolutePositions = true;
89        //}
90
91        DirectionPanel textPosition = new DirectionPanel(true, "E", new TextPositionListener());
92        DirectionPanel labelAlignment = new DirectionPanel(true, "C", new LabelAlignmentListener());
93
94        // Make sure the controls' text position and label alignment match
95        // the initial value of the associated direction panel.
96        for (JComponent control : demo.getCurrentControls()) {
97            setPosition(control, RIGHT, CENTER);
98            setAlignment(control, CENTER, CENTER);
99        }
100
101        l = new JLabel(TEXT_POSITION);
102        add(l);
103        add(textPosition);
104
105        add(Box.createRigidArea(VGAP20));
106
107        l = new JLabel(CONTENT_ALIGNMENT);
108        add(l);
109        add(labelAlignment);
110
111        add(Box.createGlue());
112    }
113
114    // Text Position Listener
115    private class TextPositionListener implements ActionListener {
116
117        @Override
118        public void actionPerformed(ActionEvent e) {
119            JRadioButton rb = (JRadioButton) e.getSource();
120            if (!rb.isSelected()) {
121                return;
122            }
123            String cmd = rb.getActionCommand();
124            int hPos, vPos;
125            switch (cmd) {
126                case "NW":
127                    hPos = LEFT;
128                    vPos = TOP;
129                    break;
130                case "N":
131                    hPos = CENTER;
132                    vPos = TOP;
133                    break;
134                case "NE":
135                    hPos = RIGHT;
136                    vPos = TOP;
137                    break;
138                case "W":
139                    hPos = LEFT;
140                    vPos = CENTER;
141                    break;
142                case "C":
143                    hPos = CENTER;
144                    vPos = CENTER;
145                    break;
146                case "E":
147                    hPos = RIGHT;
148                    vPos = CENTER;
149                    break;
150                case "SW":
151                    hPos = LEFT;
152                    vPos = BOTTOM;
153                    break;
154                case "S":
155                    hPos = CENTER;
156                    vPos = BOTTOM;
157                    break;
158                /*if(cmd.equals("SE"))*/
159                default:
160                    hPos = RIGHT;
161                    vPos = BOTTOM;
162                    break;
163            }
164            for (JComponent control : demo.getCurrentControls()) {
165                setPosition(control, hPos, vPos);
166            }
167            demo.invalidate();
168            demo.validate();
169            demo.repaint();
170        }
171    }
172
173    // Label Alignment Listener
174    private class LabelAlignmentListener implements ActionListener {
175
176        @Override
177        public void actionPerformed(ActionEvent e) {
178            JRadioButton rb = (JRadioButton) e.getSource();
179            if (!rb.isSelected()) {
180                return;
181            }
182            String cmd = rb.getActionCommand();
183            int hPos, vPos;
184            switch (cmd) {
185                case "NW":
186                    hPos = LEFT;
187                    vPos = TOP;
188                    break;
189                case "N":
190                    hPos = CENTER;
191                    vPos = TOP;
192                    break;
193                case "NE":
194                    hPos = RIGHT;
195                    vPos = TOP;
196                    break;
197                case "W":
198                    hPos = LEFT;
199                    vPos = CENTER;
200                    break;
201                case "C":
202                    hPos = CENTER;
203                    vPos = CENTER;
204                    break;
205                case "E":
206                    hPos = RIGHT;
207                    vPos = CENTER;
208                    break;
209                case "SW":
210                    hPos = LEFT;
211                    vPos = BOTTOM;
212                    break;
213                case "S":
214                    hPos = CENTER;
215                    vPos = BOTTOM;
216                    break;
217                /*if(cmd.equals("SE"))*/
218                default:
219                    hPos = RIGHT;
220                    vPos = BOTTOM;
221                    break;
222            }
223            for (JComponent control : demo.getCurrentControls()) {
224                setAlignment(control, hPos, vPos);
225                control.invalidate();
226            }
227            demo.invalidate();
228            demo.validate();
229            demo.repaint();
230        }
231    }
232
233    // Position
234    void setPosition(Component c, int hPos, int vPos) {
235        boolean ltr = c.getComponentOrientation().isLeftToRight();
236        if (absolutePositions) {
237            if (hPos == LEADING) {
238                hPos = ltr ? LEFT : RIGHT;
239            } else if (hPos == TRAILING) {
240                hPos = ltr ? RIGHT : LEFT;
241            }
242        } else if (hPos == LEFT) {
243            hPos = ltr ? LEADING : TRAILING;
244        } else if (hPos == RIGHT) {
245            hPos = ltr ? TRAILING : LEADING;
246        }
247        if (c instanceof AbstractButton) {
248            AbstractButton x = (AbstractButton) c;
249            x.setHorizontalTextPosition(hPos);
250            x.setVerticalTextPosition(vPos);
251        } else if (c instanceof JLabel) {
252            JLabel x = (JLabel) c;
253            x.setHorizontalTextPosition(hPos);
254            x.setVerticalTextPosition(vPos);
255        }
256    }
257
258    void setAlignment(Component c, int hPos, int vPos) {
259        boolean ltr = c.getComponentOrientation().isLeftToRight();
260        if (absolutePositions) {
261            if (hPos == LEADING) {
262                hPos = ltr ? LEFT : RIGHT;
263            } else if (hPos == TRAILING) {
264                hPos = ltr ? RIGHT : LEFT;
265            }
266        } else if (hPos == LEFT) {
267            hPos = ltr ? LEADING : TRAILING;
268        } else if (hPos == RIGHT) {
269            hPos = ltr ? TRAILING : LEADING;
270        }
271        if (c instanceof AbstractButton) {
272            AbstractButton x = (AbstractButton) c;
273            x.setHorizontalAlignment(hPos);
274            x.setVerticalAlignment(vPos);
275        } else if (c instanceof JLabel) {
276            JLabel x = (JLabel) c;
277            x.setHorizontalAlignment(hPos);
278            x.setVerticalAlignment(vPos);
279        }
280    }
281}
282