1 package net.suberic.util.gui; 2 import javax.swing.*; 3 import net.suberic.util.VariableBundle; 4 import java.util.Hashtable ; 5 import java.util.StringTokenizer ; 6 import java.util.MissingResourceException ; 7 import javax.swing.Action ; 8 9 14 15 public class ConfigurableToolbar extends JToolBar implements ConfigurableUI { 16 17 20 private Hashtable commands = new Hashtable (); 21 22 30 31 public ConfigurableToolbar(String toolbarID, VariableBundle vars) { 32 super(); 33 34 configureComponent(toolbarID, vars); 35 } 36 37 43 44 public void configureComponent(String toolbarID, VariableBundle vars) { 45 if (toolbarID != null) { 46 if (vars.getProperty(toolbarID + "._floatable", "true").equalsIgnoreCase("false")) { 47 setFloatable(false); 48 } 49 if (vars.getProperty(toolbarID, "") != "") { 50 StringTokenizer tokens = new StringTokenizer (vars.getProperty(toolbarID, ""), ":"); 51 while (tokens.hasMoreTokens()) { 52 String nextToken = tokens.nextToken(); 53 String nextID = toolbarID + "." + nextToken; 54 String nextClass = vars.getProperty(nextID + ".class", ""); 55 if (nextClass == "") { 56 JButton b = createToolButton(nextID, vars); 57 if (b != null) { 58 this.add(b); 59 } 60 } else { 61 try { 62 Class buttonClass = Class.forName(nextClass); 63 ConfigurableUI newUI = (ConfigurableUI) buttonClass.newInstance(); 64 newUI.configureComponent(nextID, vars); 65 if (newUI instanceof JComponent) { 66 this.add((JComponent)newUI); 67 } 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 72 73 } 74 } 75 } 76 } 77 } 78 79 protected JButton createToolButton(String key, VariableBundle vars) { 80 JButton bi; 81 82 IconManager iconManager = IconManager.getIconManager(vars, "IconManager._default"); 83 84 try { 85 86 ImageIcon icon = iconManager.getIcon(vars.getProperty(key + ".Image")); 87 88 bi = new JButton(icon); 89 90 bi.setMargin(new java.awt.Insets (1, 1, 1, 1)); 91 92 } catch (MissingResourceException mre) { 93 return null; 94 } 95 96 try { 97 bi.setToolTipText(vars.getProperty(key+ ".ToolTip")); 98 } catch (MissingResourceException mre) { 99 } 100 101 String cmd = vars.getProperty(key + ".Action", key); 102 103 bi.setActionCommand(cmd); 104 105 return bi; 106 } 107 108 113 114 public void setActive(Hashtable newCommands) { 115 clearListeners(); 116 commands=newCommands; 117 for (int i = 0; i < this.getComponentCount(); i++) { 118 Object component = this.getComponentAtIndex(i); 119 if (component instanceof ConfigurableUI) { 120 ((ConfigurableUI) component).setActive(newCommands); 121 } else if (component instanceof JButton) { 122 JButton bi = (JButton)(component); 123 124 Action a = getAction(bi.getActionCommand()); 125 if (a != null) { 126 bi.addActionListener(a); 127 bi.setEnabled(true); 128 } else { 129 bi.setEnabled(false); 130 } 131 } 132 } 133 } 134 135 140 public void setActive(Action [] newActions) { 141 clearListeners(); 142 Hashtable tmpHash = new Hashtable (); 143 if (newActions != null && newActions.length > 0) { 144 for (int i = 0; i < newActions.length; i++) { 145 String cmdName = (String )newActions[i].getValue(Action.NAME); 146 tmpHash.put(cmdName, newActions[i]); 147 } 148 } 149 setActive(tmpHash); 150 } 151 152 157 private void clearListeners() { 158 for (int i = 0; i < this.getComponentCount(); i++) { 159 if ((this.getComponentAtIndex(i)) instanceof JButton) { 160 JButton button = (JButton)(this.getComponentAtIndex(i)); 161 Action a = getAction(button.getActionCommand()); 162 if (a != null) { 163 button.removeActionListener(a); 164 } 165 } 166 167 } 168 } 169 170 private Action getAction(String key) { 171 try { 172 return (Action )commands.get(key); 173 } catch (ClassCastException cce) { 174 return null; 175 } 176 } 177 178 179 } 180 181 182 | Popular Tags |