KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DirectionPanel


1 /*
2  * @(#)DirectionPanel.java 1.8 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  * @(#)DirectionPanel.java 1.8 05/11/17
39  */

40
41 import javax.swing.*;
42 import javax.swing.border.*;
43
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.util.*;
47
48
49 /**
50  * @version 1.8 11/17/05
51  * @author Jeff Dinkins
52  * @author Chester Rose
53  * @author Brian Beck
54  */

55
56 public class DirectionPanel extends JPanel {
57
58     private ButtonGroup group;
59
60     public DirectionPanel(boolean enable, String JavaDoc selection, ActionListener l) {
61     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
62     setAlignmentY(TOP_ALIGNMENT);
63     setAlignmentX(LEFT_ALIGNMENT);
64
65     Box firstThree = Box.createHorizontalBox();
66     Box secondThree = Box.createHorizontalBox();
67     Box thirdThree = Box.createHorizontalBox();
68
69     if(!enable) {
70         selection = "None";
71     }
72
73         group = new ButtonGroup();
74     DirectionButton b;
75     b = (DirectionButton) firstThree.add(new DirectionButton( tl_dot, tldn_dot, "NW", "Sets the orientation to the North-West", l, group, selection.equals("NW")));
76     b.setEnabled(enable);
77     b = (DirectionButton) firstThree.add(new DirectionButton( tm_dot, tmdn_dot, "N", "Sets the orientation to the North", l, group, selection.equals("N")));
78     b.setEnabled(enable);
79     b = (DirectionButton) firstThree.add(new DirectionButton( tr_dot, trdn_dot, "NE", "Sets the orientation to the North-East", l, group, selection.equals("NE")));
80     b.setEnabled(enable);
81     b = (DirectionButton) secondThree.add(new DirectionButton( ml_dot, mldn_dot, "W", "Sets the orientation to the West", l, group, selection.equals("W")));
82     b.setEnabled(enable);
83     b = (DirectionButton) secondThree.add(new DirectionButton( c_dot, cdn_dot, "C", "Sets the orientation to the Center", l, group, selection.equals("C")));
84     b.setEnabled(enable);
85     b = (DirectionButton) secondThree.add(new DirectionButton( mr_dot, mrdn_dot, "E", "Sets the orientation to the East", l, group, selection.equals("E")));
86     b.setEnabled(enable);
87     b = (DirectionButton) thirdThree.add(new DirectionButton( bl_dot, bldn_dot, "SW", "Sets the orientation to the South-West", l, group, selection.equals("SW")));
88     b.setEnabled(enable);
89     b = (DirectionButton) thirdThree.add(new DirectionButton( bm_dot, bmdn_dot, "S", "Sets the orientation to the South", l, group, selection.equals("S")));
90     b.setEnabled(enable);
91     b = (DirectionButton) thirdThree.add(new DirectionButton( br_dot, brdn_dot, "SE", "Sets the orientation to the South-East", l, group, selection.equals("SE")));
92     b.setEnabled(enable);
93
94     add(firstThree);
95     add(secondThree);
96     add(thirdThree);
97     }
98
99     public String JavaDoc getSelection() {
100         return group.getSelection().getActionCommand();
101     }
102
103     public void setSelection( String JavaDoc selection ) {
104         Enumeration e = group.getElements();
105         while( e.hasMoreElements() ) {
106             JRadioButton b = (JRadioButton)e.nextElement();
107             if( b.getActionCommand().equals(selection) ) {
108                b.setSelected(true);
109             }
110         }
111     }
112     
113     // Chester's way cool layout buttons
114
public ImageIcon bl_dot = loadImageIcon("bl.gif","bottom left layout button");
115     public ImageIcon bldn_dot = loadImageIcon("bldn.gif","selected bottom left layout button");
116     public ImageIcon bm_dot = loadImageIcon("bm.gif","bottom middle layout button");
117     public ImageIcon bmdn_dot = loadImageIcon("bmdn.gif","selected bottom middle layout button");
118     public ImageIcon br_dot = loadImageIcon("br.gif","bottom right layout button");
119     public ImageIcon brdn_dot = loadImageIcon("brdn.gif","selected bottom right layout button");
120     public ImageIcon c_dot = loadImageIcon("c.gif","center layout button");
121     public ImageIcon cdn_dot = loadImageIcon("cdn.gif","selected center layout button");
122     public ImageIcon ml_dot = loadImageIcon("ml.gif","middle left layout button");
123     public ImageIcon mldn_dot = loadImageIcon("mldn.gif","selected middle left layout button");
124     public ImageIcon mr_dot = loadImageIcon("mr.gif","middle right layout button");
125     public ImageIcon mrdn_dot = loadImageIcon("mrdn.gif","selected middle right layout button");
126     public ImageIcon tl_dot = loadImageIcon("tl.gif","top left layout button");
127     public ImageIcon tldn_dot = loadImageIcon("tldn.gif","selected top left layout button");
128     public ImageIcon tm_dot = loadImageIcon("tm.gif","top middle layout button");
129     public ImageIcon tmdn_dot = loadImageIcon("tmdn.gif","selected top middle layout button");
130     public ImageIcon tr_dot = loadImageIcon("tr.gif","top right layout button");
131     public ImageIcon trdn_dot = loadImageIcon("trdn.gif","selected top right layout button");
132     
133     public ImageIcon loadImageIcon(String JavaDoc filename, String JavaDoc description) {
134     String JavaDoc path = "/resources/images/buttons/" + filename;
135     return new ImageIcon(getClass().getResource(path), description);
136     }
137
138     
139     public class DirectionButton extends JRadioButton {
140         
141         /**
142          * A layout direction button
143          */

144         public DirectionButton(Icon icon, Icon downIcon, String JavaDoc direction,
145                                String JavaDoc description, ActionListener l,
146                                ButtonGroup group, boolean selected)
147         {
148             super();
149             this.addActionListener(l);
150             setFocusPainted(false);
151             setHorizontalTextPosition(CENTER);
152             group.add(this);
153             setIcon(icon);
154             setSelectedIcon(downIcon);
155             setActionCommand(direction);
156             getAccessibleContext().setAccessibleName(direction);
157             getAccessibleContext().setAccessibleDescription(description);
158             setSelected(selected);
159         }
160
161         public boolean isFocusTraversable() {
162             return false;
163         }
164
165         public void setBorder(Border b) {
166         }
167     }
168 }
169
Popular Tags