1 /*
   2  *
   3  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 import javax.swing.*;
  34 import javax.swing.border.*;
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import java.util.*;
  39 
  40 
  41 /**
  42  * @author Jeff Dinkins
  43  * @author Chester Rose
  44  * @author Brian Beck
  45  */
  46 
  47 public class DirectionPanel extends JPanel {
  48 
  49     private ButtonGroup group;
  50 
  51     public DirectionPanel(boolean enable, String selection, ActionListener l) {
  52         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  53         setAlignmentY(TOP_ALIGNMENT);
  54         setAlignmentX(LEFT_ALIGNMENT);
  55 
  56         Box firstThree = Box.createHorizontalBox();
  57         Box secondThree = Box.createHorizontalBox();
  58         Box thirdThree = Box.createHorizontalBox();
  59 
  60         if(!enable) {
  61             selection = "None";
  62         }
  63 
  64         group = new ButtonGroup();
  65         DirectionButton b;
  66         b = (DirectionButton) firstThree.add(new DirectionButton(  tl_dot, tldn_dot, "NW", "Sets the orientation to the North-West", l, group, selection.equals("NW")));
  67         b.setEnabled(enable);
  68         b = (DirectionButton) firstThree.add(new DirectionButton(  tm_dot, tmdn_dot, "N",  "Sets the orientation to the North", l, group, selection.equals("N")));
  69         b.setEnabled(enable);
  70         b = (DirectionButton) firstThree.add(new DirectionButton(  tr_dot, trdn_dot, "NE", "Sets the orientation to the North-East", l, group, selection.equals("NE")));
  71         b.setEnabled(enable);
  72         b = (DirectionButton) secondThree.add(new DirectionButton( ml_dot, mldn_dot, "W", "Sets the orientation to the West", l, group, selection.equals("W")));
  73         b.setEnabled(enable);
  74         b = (DirectionButton) secondThree.add(new DirectionButton( c_dot,  cdn_dot,  "C", "Sets the orientation to the Center", l, group, selection.equals("C")));
  75         b.setEnabled(enable);
  76         b = (DirectionButton) secondThree.add(new DirectionButton( mr_dot, mrdn_dot, "E", "Sets the orientation to the East", l, group, selection.equals("E")));
  77         b.setEnabled(enable);
  78         b = (DirectionButton) thirdThree.add(new DirectionButton(  bl_dot, bldn_dot, "SW", "Sets the orientation to the South-West", l, group, selection.equals("SW")));
  79         b.setEnabled(enable);
  80         b = (DirectionButton) thirdThree.add(new DirectionButton(  bm_dot, bmdn_dot, "S", "Sets the orientation to the South", l, group, selection.equals("S")));
  81         b.setEnabled(enable);
  82         b = (DirectionButton) thirdThree.add(new DirectionButton(  br_dot, brdn_dot, "SE", "Sets the orientation to the South-East", l, group, selection.equals("SE")));
  83         b.setEnabled(enable);
  84 
  85         add(firstThree);
  86         add(secondThree);
  87         add(thirdThree);
  88     }
  89 
  90     public String getSelection() {
  91         return group.getSelection().getActionCommand();
  92     }
  93 
  94     public void setSelection( String selection  ) {
  95         Enumeration e = group.getElements();
  96         while( e.hasMoreElements() ) {
  97             JRadioButton b = (JRadioButton)e.nextElement();
  98             if( b.getActionCommand().equals(selection) ) {
  99                b.setSelected(true);
 100             }
 101         }
 102     }
 103 
 104     // Chester's way cool layout buttons
 105     public ImageIcon bl_dot   = loadImageIcon("bl.gif","bottom left layout button");
 106     public ImageIcon bldn_dot = loadImageIcon("bldn.gif","selected bottom left layout button");
 107     public ImageIcon bm_dot   = loadImageIcon("bm.gif","bottom middle layout button");
 108     public ImageIcon bmdn_dot = loadImageIcon("bmdn.gif","selected bottom middle layout button");
 109     public ImageIcon br_dot   = loadImageIcon("br.gif","bottom right layout button");
 110     public ImageIcon brdn_dot = loadImageIcon("brdn.gif","selected bottom right layout button");
 111     public ImageIcon c_dot    = loadImageIcon("c.gif","center layout button");
 112     public ImageIcon cdn_dot  = loadImageIcon("cdn.gif","selected center layout button");
 113     public ImageIcon ml_dot   = loadImageIcon("ml.gif","middle left layout button");
 114     public ImageIcon mldn_dot = loadImageIcon("mldn.gif","selected middle left layout button");
 115     public ImageIcon mr_dot   = loadImageIcon("mr.gif","middle right layout button");
 116     public ImageIcon mrdn_dot = loadImageIcon("mrdn.gif","selected middle right layout button");
 117     public ImageIcon tl_dot   = loadImageIcon("tl.gif","top left layout button");
 118     public ImageIcon tldn_dot = loadImageIcon("tldn.gif","selected top left layout button");
 119     public ImageIcon tm_dot   = loadImageIcon("tm.gif","top middle layout button");
 120     public ImageIcon tmdn_dot = loadImageIcon("tmdn.gif","selected top middle layout button");
 121     public ImageIcon tr_dot   = loadImageIcon("tr.gif","top right layout button");
 122     public ImageIcon trdn_dot = loadImageIcon("trdn.gif","selected top right layout button");
 123 
 124     public ImageIcon loadImageIcon(String filename, String description) {
 125         String path = "/resources/images/buttons/" + filename;
 126         return new ImageIcon(getClass().getResource(path), description);
 127     }
 128 
 129 
 130     public class DirectionButton extends JRadioButton {
 131 
 132         /**
 133          * A layout direction button
 134          */
 135         public DirectionButton(Icon icon, Icon downIcon, String direction,
 136                                String description, ActionListener l,
 137                                ButtonGroup group, boolean selected)
 138         {
 139             super();
 140             this.addActionListener(l);
 141             setFocusPainted(false);
 142             setHorizontalTextPosition(CENTER);
 143             group.add(this);
 144             setIcon(icon);
 145             setSelectedIcon(downIcon);
 146             setActionCommand(direction);
 147             getAccessibleContext().setAccessibleName(direction);
 148             getAccessibleContext().setAccessibleDescription(description);
 149             setSelected(selected);
 150         }
 151 
 152         public boolean isFocusTraversable() {
 153             return false;
 154         }
 155 
 156         public void setBorder(Border b) {
 157         }
 158     }
 159 }