1 50 package com.lowagie.tools.arguments; 51 52 import java.awt.event.ActionEvent; 53 import java.util.Iterator; 54 import java.util.TreeMap; 55 56 import javax.swing.JComboBox; 57 import javax.swing.JOptionPane; 58 59 import com.lowagie.tools.plugins.AbstractTool; 60 61 64 public class OptionArgument extends ToolArgument { 65 66 69 public class Entry { 70 71 private Object description; 72 73 private Object value; 74 78 public Entry(Object value) { 79 this.value = value; 80 this.description = value; 81 } 82 87 public Entry(Object description, Object value) { 88 this.description = description; 89 this.value = value; 90 } 91 95 public String toString() { 96 return description.toString(); 97 } 98 102 public String getValueToString() { 103 return value.toString(); 104 } 105 108 public Object getDescription() { 109 return description; 110 } 111 114 public void setDescription(Object description) { 115 this.description = description; 116 } 117 120 public Object getValue() { 121 return value; 122 } 123 126 public void setValue(Object value) { 127 this.value = value; 128 } 129 } 130 131 private TreeMap options = new TreeMap(); 132 133 139 public OptionArgument(AbstractTool tool, String name, String description) { 140 super(tool, name, description, Entry.class.getName()); 141 } 142 143 148 public void addOption(Object description, Object value) { 149 options.put(value.toString(), new Entry(description, value)); 150 } 151 152 157 public Object getArgument() throws InstantiationException { 158 if (value == null) return null; 159 try { 160 return ((Entry)options.get(value)).getValue(); 161 } catch (Exception e) { 162 throw new InstantiationException(e.getMessage()); 163 } 164 } 165 166 169 public String getUsage() { 170 StringBuffer buf = new StringBuffer(super.getUsage()); 171 buf.append(" possible options:\n"); 172 Entry entry; 173 for (Iterator i = options.values().iterator(); i.hasNext(); ) { 174 entry = (Entry)i.next(); 175 buf.append(" - "); 176 buf.append(entry.getValueToString()); 177 buf.append(": "); 178 buf.append(entry.toString()); 179 buf.append("\n"); 180 } 181 return buf.toString(); 182 } 183 184 187 public void actionPerformed(ActionEvent evt) { 188 Object[] message = new Object[2]; 189 message[0] = "Choose one of the following options:"; 190 JComboBox cb = new JComboBox(); 191 for(Iterator i = options.values().iterator(); i.hasNext(); ) { 192 cb.addItem(i.next()); 193 } 194 message[1] = cb; 195 int result = JOptionPane.showOptionDialog( 196 tool.getInternalFrame(), 197 message, 198 description, 199 JOptionPane.OK_CANCEL_OPTION, 200 JOptionPane.QUESTION_MESSAGE, 201 null, 202 null, 203 null 204 ); 205 if (result == 0) { 206 Entry entry = (Entry)cb.getSelectedItem(); 207 setValue(entry.getValueToString()); 208 } 209 } 210 } | Popular Tags |