1 35 36 39 package demo.swingset; 40 import swingwtx.swing.*; 41 import swingwtx.swing.border.*; 42 43 import swingwt.awt.*; 44 import swingwt.awt.event.*; 45 import java.util.*; 46 47 48 54 55 public class DirectionPanel extends JPanel { 56 57 private ButtonGroup group; 58 59 public DirectionPanel(boolean enable, String selection, ActionListener l) { 60 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 61 setAlignmentY(TOP_ALIGNMENT); 62 setAlignmentX(LEFT_ALIGNMENT); 63 64 Box firstThree = Box.createHorizontalBox(); 65 Box secondThree = Box.createHorizontalBox(); 66 Box thirdThree = Box.createHorizontalBox(); 67 68 if(!enable) { 69 selection = "None"; 70 } 71 72 group = new ButtonGroup(); 73 DirectionButton b; 74 b = (DirectionButton) firstThree.add(new DirectionButton( tl_dot, tldn_dot, "NW", "Sets the orientation to the North-West", l, group, selection.equals("NW"))); 75 b.setEnabled(enable); 76 b = (DirectionButton) firstThree.add(new DirectionButton( tm_dot, tmdn_dot, "N", "Sets the orientation to the North", l, group, selection.equals("N"))); 77 b.setEnabled(enable); 78 b = (DirectionButton) firstThree.add(new DirectionButton( tr_dot, trdn_dot, "NE", "Sets the orientation to the North-East", l, group, selection.equals("NE"))); 79 b.setEnabled(enable); 80 b = (DirectionButton) secondThree.add(new DirectionButton( ml_dot, mldn_dot, "W", "Sets the orientation to the West", l, group, selection.equals("W"))); 81 b.setEnabled(enable); 82 b = (DirectionButton) secondThree.add(new DirectionButton( c_dot, cdn_dot, "C", "Sets the orientation to the Center", l, group, selection.equals("C"))); 83 b.setEnabled(enable); 84 b = (DirectionButton) secondThree.add(new DirectionButton( mr_dot, mrdn_dot, "E", "Sets the orientation to the East", l, group, selection.equals("E"))); 85 b.setEnabled(enable); 86 b = (DirectionButton) thirdThree.add(new DirectionButton( bl_dot, bldn_dot, "SW", "Sets the orientation to the South-West", l, group, selection.equals("SW"))); 87 b.setEnabled(enable); 88 b = (DirectionButton) thirdThree.add(new DirectionButton( bm_dot, bmdn_dot, "S", "Sets the orientation to the South", l, group, selection.equals("S"))); 89 b.setEnabled(enable); 90 b = (DirectionButton) thirdThree.add(new DirectionButton( br_dot, brdn_dot, "SE", "Sets the orientation to the South-East", l, group, selection.equals("SE"))); 91 b.setEnabled(enable); 92 93 add(firstThree); 94 add(secondThree); 95 add(thirdThree); 96 } 97 98 public String getSelection() { 99 return group.getSelection().getActionCommand(); 100 } 101 102 public void setSelection( String selection ) { 103 Enumeration e = group.getElements(); 104 while( e.hasMoreElements() ) { 105 JRadioButton b = (JRadioButton)e.nextElement(); 106 if( b.getActionCommand().equals(selection) ) { 107 b.setSelected(true); 108 } 109 } 110 } 111 112 public ImageIcon bl_dot = loadImageIcon("bl.gif","bottom left layout button"); 114 public ImageIcon bldn_dot = loadImageIcon("bldn.gif","selected bottom left layout button"); 115 public ImageIcon bm_dot = loadImageIcon("bm.gif","bottom middle layout button"); 116 public ImageIcon bmdn_dot = loadImageIcon("bmdn.gif","selected bottom middle layout button"); 117 public ImageIcon br_dot = loadImageIcon("br.gif","bottom right layout button"); 118 public ImageIcon brdn_dot = loadImageIcon("brdn.gif","selected bottom right layout button"); 119 public ImageIcon c_dot = loadImageIcon("c.gif","center layout button"); 120 public ImageIcon cdn_dot = loadImageIcon("cdn.gif","selected center layout button"); 121 public ImageIcon ml_dot = loadImageIcon("ml.gif","middle left layout button"); 122 public ImageIcon mldn_dot = loadImageIcon("mldn.gif","selected middle left layout button"); 123 public ImageIcon mr_dot = loadImageIcon("mr.gif","middle right layout button"); 124 public ImageIcon mrdn_dot = loadImageIcon("mrdn.gif","selected middle right layout button"); 125 public ImageIcon tl_dot = loadImageIcon("tl.gif","top left layout button"); 126 public ImageIcon tldn_dot = loadImageIcon("tldn.gif","selected top left layout button"); 127 public ImageIcon tm_dot = loadImageIcon("tm.gif","top middle layout button"); 128 public ImageIcon tmdn_dot = loadImageIcon("tmdn.gif","selected top middle layout button"); 129 public ImageIcon tr_dot = loadImageIcon("tr.gif","top right layout button"); 130 public ImageIcon trdn_dot = loadImageIcon("trdn.gif","selected top right layout button"); 131 132 public ImageIcon loadImageIcon(String filename, String description) { 133 String path = "/resources/images/buttons/" + filename; 134 return new ImageIcon(getClass().getResource(path), description); 135 } 136 137 138 public class DirectionButton extends JRadioButton { 139 140 143 public DirectionButton(ImageIcon icon, ImageIcon downIcon, String direction, 144 String description, ActionListener l, 145 ButtonGroup group, boolean selected) 146 { 147 super(); 148 this.addActionListener(l); 149 setFocusPainted(false); 150 setHorizontalTextPosition(CENTER); 151 group.add(this); 152 setIcon(icon); 153 setSelectedIcon(downIcon); 154 setActionCommand(direction); 155 setSelected(selected); 158 } 159 160 public boolean isFocusTraversable() { 161 return false; 162 } 163 164 public void setBorder(Border b) { 165 } 166 } 167 } 168 | Popular Tags |