DirectionPanel.java revision 13978:1993af50385d
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.event.ActionListener;
26import javax.swing.Box;
27import javax.swing.BoxLayout;
28import javax.swing.ButtonGroup;
29import javax.swing.Icon;
30import javax.swing.ImageIcon;
31import javax.swing.JPanel;
32import javax.swing.JRadioButton;
33import javax.swing.border.Border;
34
35/**
36 * @version 1.8 11/17/05
37 * @author Jeff Dinkins
38 * @author Chester Rose
39 * @author Brian Beck
40 */
41public class DirectionPanel extends JPanel {
42
43    private final ButtonGroup group;
44
45    public DirectionPanel(boolean enable, String selection, ActionListener l) {
46        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
47        setAlignmentY(TOP_ALIGNMENT);
48        setAlignmentX(LEFT_ALIGNMENT);
49
50        Box firstThree = Box.createHorizontalBox();
51        Box secondThree = Box.createHorizontalBox();
52        Box thirdThree = Box.createHorizontalBox();
53
54        if (!enable) {
55            selection = "None";
56        }
57
58        group = new ButtonGroup();
59        DirectionButton b;
60        b = (DirectionButton) firstThree.add(new DirectionButton(tl_dot, tldn_dot, "NW", "Sets the orientation to the North-West", l, group, selection.equals("NW")));
61        b.setEnabled(enable);
62        b = (DirectionButton) firstThree.add(new DirectionButton(tm_dot, tmdn_dot, "N", "Sets the orientation to the North", l, group, selection.equals("N")));
63        b.setEnabled(enable);
64        b = (DirectionButton) firstThree.add(new DirectionButton(tr_dot, trdn_dot, "NE", "Sets the orientation to the North-East", l, group, selection.equals("NE")));
65        b.setEnabled(enable);
66        b = (DirectionButton) secondThree.add(new DirectionButton(ml_dot, mldn_dot, "W", "Sets the orientation to the West", l, group, selection.equals("W")));
67        b.setEnabled(enable);
68        b = (DirectionButton) secondThree.add(new DirectionButton(c_dot, cdn_dot, "C", "Sets the orientation to the Center", l, group, selection.equals("C")));
69        b.setEnabled(enable);
70        b = (DirectionButton) secondThree.add(new DirectionButton(mr_dot, mrdn_dot, "E", "Sets the orientation to the East", l, group, selection.equals("E")));
71        b.setEnabled(enable);
72        b = (DirectionButton) thirdThree.add(new DirectionButton(bl_dot, bldn_dot, "SW", "Sets the orientation to the South-West", l, group, selection.equals("SW")));
73        b.setEnabled(enable);
74        b = (DirectionButton) thirdThree.add(new DirectionButton(bm_dot, bmdn_dot, "S", "Sets the orientation to the South", l, group, selection.equals("S")));
75        b.setEnabled(enable);
76        b = (DirectionButton) thirdThree.add(new DirectionButton(br_dot, brdn_dot, "SE", "Sets the orientation to the South-East", l, group, selection.equals("SE")));
77        b.setEnabled(enable);
78
79        add(firstThree);
80        add(secondThree);
81        add(thirdThree);
82    }
83
84    // Chester's way cool layout buttons
85    private final ImageIcon bl_dot = loadImageIcon("bl.gif", "bottom left layout button");
86    private final ImageIcon bldn_dot = loadImageIcon("bldn.gif", "selected bottom left layout button");
87    private final ImageIcon bm_dot = loadImageIcon("bm.gif", "bottom middle layout button");
88    private final ImageIcon bmdn_dot = loadImageIcon("bmdn.gif", "selected bottom middle layout button");
89    private final ImageIcon br_dot = loadImageIcon("br.gif", "bottom right layout button");
90    private final ImageIcon brdn_dot = loadImageIcon("brdn.gif", "selected bottom right layout button");
91    private final ImageIcon c_dot = loadImageIcon("c.gif", "center layout button");
92    private final ImageIcon cdn_dot = loadImageIcon("cdn.gif", "selected center layout button");
93    private final ImageIcon ml_dot = loadImageIcon("ml.gif", "middle left layout button");
94    private final ImageIcon mldn_dot = loadImageIcon("mldn.gif", "selected middle left layout button");
95    private final ImageIcon mr_dot = loadImageIcon("mr.gif", "middle right layout button");
96    private final ImageIcon mrdn_dot = loadImageIcon("mrdn.gif", "selected middle right layout button");
97    private final ImageIcon tl_dot = loadImageIcon("tl.gif", "top left layout button");
98    private final ImageIcon tldn_dot = loadImageIcon("tldn.gif", "selected top left layout button");
99    private final ImageIcon tm_dot = loadImageIcon("tm.gif", "top middle layout button");
100    private final ImageIcon tmdn_dot = loadImageIcon("tmdn.gif", "selected top middle layout button");
101    private final ImageIcon tr_dot = loadImageIcon("tr.gif", "top right layout button");
102    private final ImageIcon trdn_dot = loadImageIcon("trdn.gif", "selected top right layout button");
103
104    private ImageIcon loadImageIcon(String filename, String description) {
105        String path = "resources/images/" + filename;
106        return new ImageIcon(getClass().getResource(path), description);
107    }
108
109    private static class DirectionButton extends JRadioButton {
110
111        /**
112         * A layout direction button
113         */
114        public DirectionButton(Icon icon, Icon downIcon, String direction,
115                String description, ActionListener l,
116                ButtonGroup group, boolean selected) {
117            super();
118            this.addActionListener(l);
119            setFocusPainted(false);
120            setHorizontalTextPosition(CENTER);
121            group.add(this);
122            setIcon(icon);
123            setSelectedIcon(downIcon);
124            setActionCommand(direction);
125            getAccessibleContext().setAccessibleName(direction);
126            getAccessibleContext().setAccessibleDescription(description);
127            setSelected(selected);
128        }
129
130        @Override
131        @Deprecated
132        public boolean isFocusTraversable() {
133            return false;
134        }
135
136        @Override
137        public void setBorder(Border b) {
138        }
139    }
140}
141