Path: blob/master/src/demo/share/jfc/SwingSet2/DirectionPanel.java
41152 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132import javax.swing.*;33import javax.swing.border.*;3435import java.awt.*;36import java.awt.event.*;37import java.util.*;383940/**41* @author Jeff Dinkins42* @author Chester Rose43* @author Brian Beck44*/4546public class DirectionPanel extends JPanel {4748private ButtonGroup group;4950public DirectionPanel(boolean enable, String selection, ActionListener l) {51setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));52setAlignmentY(TOP_ALIGNMENT);53setAlignmentX(LEFT_ALIGNMENT);5455Box firstThree = Box.createHorizontalBox();56Box secondThree = Box.createHorizontalBox();57Box thirdThree = Box.createHorizontalBox();5859if(!enable) {60selection = "None";61}6263group = new ButtonGroup();64DirectionButton b;65b = (DirectionButton) firstThree.add(new DirectionButton( tl_dot, tldn_dot, "NW", "Sets the orientation to the North-West", l, group, selection.equals("NW")));66b.setEnabled(enable);67b = (DirectionButton) firstThree.add(new DirectionButton( tm_dot, tmdn_dot, "N", "Sets the orientation to the North", l, group, selection.equals("N")));68b.setEnabled(enable);69b = (DirectionButton) firstThree.add(new DirectionButton( tr_dot, trdn_dot, "NE", "Sets the orientation to the North-East", l, group, selection.equals("NE")));70b.setEnabled(enable);71b = (DirectionButton) secondThree.add(new DirectionButton( ml_dot, mldn_dot, "W", "Sets the orientation to the West", l, group, selection.equals("W")));72b.setEnabled(enable);73b = (DirectionButton) secondThree.add(new DirectionButton( c_dot, cdn_dot, "C", "Sets the orientation to the Center", l, group, selection.equals("C")));74b.setEnabled(enable);75b = (DirectionButton) secondThree.add(new DirectionButton( mr_dot, mrdn_dot, "E", "Sets the orientation to the East", l, group, selection.equals("E")));76b.setEnabled(enable);77b = (DirectionButton) thirdThree.add(new DirectionButton( bl_dot, bldn_dot, "SW", "Sets the orientation to the South-West", l, group, selection.equals("SW")));78b.setEnabled(enable);79b = (DirectionButton) thirdThree.add(new DirectionButton( bm_dot, bmdn_dot, "S", "Sets the orientation to the South", l, group, selection.equals("S")));80b.setEnabled(enable);81b = (DirectionButton) thirdThree.add(new DirectionButton( br_dot, brdn_dot, "SE", "Sets the orientation to the South-East", l, group, selection.equals("SE")));82b.setEnabled(enable);8384add(firstThree);85add(secondThree);86add(thirdThree);87}8889public String getSelection() {90return group.getSelection().getActionCommand();91}9293public void setSelection( String selection ) {94Enumeration<AbstractButton> e = group.getElements();95while( e.hasMoreElements() ) {96AbstractButton b = e.nextElement();97if( b.getActionCommand().equals(selection) ) {98b.setSelected(true);99}100}101}102103// Chester's way cool layout buttons104public ImageIcon bl_dot = loadImageIcon("bl.gif","bottom left layout button");105public ImageIcon bldn_dot = loadImageIcon("bldn.gif","selected bottom left layout button");106public ImageIcon bm_dot = loadImageIcon("bm.gif","bottom middle layout button");107public ImageIcon bmdn_dot = loadImageIcon("bmdn.gif","selected bottom middle layout button");108public ImageIcon br_dot = loadImageIcon("br.gif","bottom right layout button");109public ImageIcon brdn_dot = loadImageIcon("brdn.gif","selected bottom right layout button");110public ImageIcon c_dot = loadImageIcon("c.gif","center layout button");111public ImageIcon cdn_dot = loadImageIcon("cdn.gif","selected center layout button");112public ImageIcon ml_dot = loadImageIcon("ml.gif","middle left layout button");113public ImageIcon mldn_dot = loadImageIcon("mldn.gif","selected middle left layout button");114public ImageIcon mr_dot = loadImageIcon("mr.gif","middle right layout button");115public ImageIcon mrdn_dot = loadImageIcon("mrdn.gif","selected middle right layout button");116public ImageIcon tl_dot = loadImageIcon("tl.gif","top left layout button");117public ImageIcon tldn_dot = loadImageIcon("tldn.gif","selected top left layout button");118public ImageIcon tm_dot = loadImageIcon("tm.gif","top middle layout button");119public ImageIcon tmdn_dot = loadImageIcon("tmdn.gif","selected top middle layout button");120public ImageIcon tr_dot = loadImageIcon("tr.gif","top right layout button");121public ImageIcon trdn_dot = loadImageIcon("trdn.gif","selected top right layout button");122123public ImageIcon loadImageIcon(String filename, String description) {124String path = "/resources/images/buttons/" + filename;125return new ImageIcon(getClass().getResource(path), description);126}127128129public class DirectionButton extends JRadioButton {130131/**132* A layout direction button133*/134public DirectionButton(Icon icon, Icon downIcon, String direction,135String description, ActionListener l,136ButtonGroup group, boolean selected)137{138super();139this.addActionListener(l);140setFocusPainted(false);141setHorizontalTextPosition(CENTER);142group.add(this);143setIcon(icon);144setSelectedIcon(downIcon);145setActionCommand(direction);146getAccessibleContext().setAccessibleName(direction);147getAccessibleContext().setAccessibleDescription(description);148setSelected(selected);149setFocusable(false);150}151152public void setBorder(Border b) {153}154}155}156157158