1 package net.suberic.util.gui; 2 import javax.swing.*; 3 import net.suberic.util.VariableBundle; 4 import java.util.HashMap ; 5 import java.util.Hashtable ; 6 import java.util.StringTokenizer ; 7 import java.util.MissingResourceException ; 8 import javax.swing.Action ; 9 import java.awt.event.*; 10 11 16 17 public class ConfigurableComboBox extends JComboBox implements ConfigurableUI { 18 19 22 protected HashMap selectionMap = new HashMap (); 23 24 protected Hashtable commands = new Hashtable (); 25 26 String mKey = null; 27 28 int minWidth = -1; 29 int minHeight = -1; 30 31 public ConfigurableComboBox() { 32 super(); 33 } 34 35 43 public ConfigurableComboBox(String buttonID, VariableBundle vars) { 44 super(); 45 46 configureComponent(buttonID, vars); 47 } 48 49 55 public void configureComponent(String key, VariableBundle vars) { 56 this.setRenderer(new ConfigurableComboRenderer()); 57 58 mKey = key; 59 60 StringTokenizer iKeys = null; 61 try { 62 iKeys = new StringTokenizer (vars.getProperty(key), ":"); 63 } catch (MissingResourceException mre) { 64 mre.printStackTrace(); 65 try { 66 System.err.println(vars.getProperty("error.NoSuchResource") + " " + mre.getKey()); 67 } catch (MissingResourceException mretwo) { 68 System.err.println("Unable to load resource " + mre.getKey()); 69 return; 70 } 71 return; 72 } 73 String currentToken; 74 75 while (iKeys.hasMoreTokens()) { 76 currentToken=iKeys.nextToken(); 77 Object i = createComboBoxItem(key + "." + currentToken, vars); 78 this.addItem(i); 79 } 80 81 this.addItemListener(new ItemListener() { 82 public void itemStateChanged(ItemEvent e) { 83 if (e.getStateChange() == ItemEvent.SELECTED) { 84 Object selectedItem = e.getItem(); 85 String cmd = (String )selectionMap.get(selectedItem); 86 if (cmd != null) { 87 Action action = getAction(cmd); 88 if (action != null) { 89 action.actionPerformed(new ActionEvent(e.getSource(), e.getID(), cmd)); 90 } 91 } 92 } 93 } 94 }); 95 96 this.setMaximumSize(this.getPreferredSize()); 97 98 String toolTip = vars.getProperty(key + ".ToolTip", ""); 99 if (toolTip != "") { 100 setToolTipText(toolTip); 101 } 102 } 103 104 105 108 protected Object createComboBoxItem(String buttonID, VariableBundle vars) { 109 110 ImageIcon returnValue = null; 111 112 IconManager iconManager = IconManager.getIconManager(vars, "IconManager._default"); 113 ImageIcon icon = iconManager.getIcon(vars.getProperty(buttonID + ".Image")); 114 if (icon != null) { 115 116 if (minWidth < 0) { 117 minWidth = icon.getIconWidth(); 118 } else { 119 minWidth = java.lang.Math.min(minWidth, icon.getIconWidth()); 120 } 121 122 if (minHeight < 0) { 123 minHeight = icon.getIconHeight(); 124 } else { 125 minHeight = java.lang.Math.min(minHeight, icon.getIconHeight()); 126 } 127 128 129 returnValue = icon; 131 } 132 133 String cmd = vars.getProperty(buttonID + ".Action", buttonID); 134 135 selectionMap.put(returnValue, cmd); 136 137 return returnValue; 138 } 139 140 143 public void setActive(javax.swing.Action [] newActions) { 144 Hashtable tmpHash = new Hashtable (); 145 if (newActions != null && newActions.length > 0) { 146 for (int i = 0; i < newActions.length; i++) { 147 String cmdName = (String )newActions[i].getValue(Action.NAME); 148 tmpHash.put(cmdName, newActions[i]); 149 } 150 } 151 setActive(tmpHash); 152 } 153 154 157 public void setActive(Hashtable newCommands) { 158 commands = newCommands; 159 } 160 161 165 166 public Action getAction(String command) { 167 return (Action )commands.get(command); 168 } 169 170 class ConfigurableComboRenderer extends JLabel implements ListCellRenderer { 171 172 public ConfigurableComboRenderer() { 173 setOpaque(true); 174 setHorizontalAlignment(CENTER); 175 setVerticalAlignment(CENTER); 176 } 177 178 public java.awt.Component getListCellRendererComponent( 179 JList list, 180 Object value, 181 int index, 182 boolean isSelected, 183 boolean cellHasFocus) 184 { 185 if (isSelected) { 186 setBackground(list.getSelectionBackground()); 187 setForeground(list.getSelectionForeground()); 188 } else { 189 setBackground(list.getBackground()); 190 setForeground(list.getForeground()); 191 } 192 193 ImageIcon icon = (ImageIcon)value; 194 setIcon(icon); 196 return this; 197 } 198 } 199 } 200 | Popular Tags |