1 50 package com.lowagie.tools.arguments; 51 52 import java.awt.Color; 53 import java.awt.event.ActionEvent; 54 import java.awt.event.ActionListener; 55 import java.io.File; 56 57 import javax.swing.JColorChooser; 58 import javax.swing.JFileChooser; 59 import javax.swing.JOptionPane; 60 61 import com.lowagie.text.Image; 62 import com.lowagie.tools.plugins.AbstractTool; 63 64 67 public class ToolArgument implements ActionListener { 68 69 protected AbstractTool tool; 70 71 protected String description; 72 73 protected String name; 74 75 protected String classname; 76 77 protected String value = null; 78 79 80 public ToolArgument() {} 81 82 89 public ToolArgument(AbstractTool tool, String name, String description, String classname) { 90 this.tool = tool; 91 this.name = name; 92 this.description = description; 93 this.classname = classname; 94 } 95 96 101 public Object getArgument() throws InstantiationException { 102 if (value == null) return null; 103 try { 104 if (String.class.getName().equals(classname)) return value; 105 if (Image.class.getName().equals(classname)) return Image.getInstance(value); 106 if (File.class.getName().equals(classname)) return new File(value); 107 if (Color.class.getName().equals(classname)) return Color.decode(value); 108 } catch (Exception e) { 109 throw new InstantiationException(e.getMessage()); 110 } 111 return value; 112 } 113 114 117 public void actionPerformed(ActionEvent e) { 118 if (String.class.getName().equals(classname)) 119 setValue(JOptionPane.showInputDialog(tool.getInternalFrame(), "Enter a value for " + name + ":")); 120 if (Image.class.getName().equals(classname)) { 121 JFileChooser fc = new JFileChooser(); 122 fc.showOpenDialog(tool.getInternalFrame()); 123 setValue(fc.getSelectedFile().getAbsolutePath()); 124 } 125 if (File.class.getName().equals(classname)) { 126 JFileChooser fc = new JFileChooser(); 127 fc.showOpenDialog(tool.getInternalFrame()); 128 setValue(fc.getSelectedFile().getAbsolutePath()); 129 } 130 if (Color.class.getName().equals(classname)) { 131 Color initialColor = new Color(0xFF, 0xFF, 0xFF); 132 if (value != null) initialColor = Color.decode(value); 133 Color newColor = JColorChooser.showDialog(tool.getInternalFrame(), "Choose Color", initialColor); 134 setValue("0x" + Integer.toHexString((newColor.getRed() << 16) | (newColor.getGreen() << 8) | (newColor.getBlue() << 0)).toUpperCase()); 135 } 136 } 137 138 142 public String getUsage() { 143 StringBuffer buf = new StringBuffer(" "); 144 buf.append(name); 145 buf.append(" - "); 146 buf.append(description); 147 buf.append("\n"); 148 return buf.toString(); 149 } 150 151 154 public String getClassname() { 155 return classname; 156 } 157 158 161 public void setClassname(String classname) { 162 this.classname = classname; 163 } 164 165 168 public String getDescription() { 169 return description; 170 } 171 172 175 public void setDescription(String description) { 176 this.description = description; 177 } 178 179 182 public String getName() { 183 return name; 184 } 185 186 189 public void setName(String name) { 190 this.name = name; 191 } 192 193 196 public String getValue() { 197 return value; 198 } 199 200 203 public void setValue(String value) { 204 this.value = value; 205 tool.valueHasChanged(this); 206 } 207 } | Popular Tags |