KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > arguments > ToolArgument


1 /*
2  * $Id: ToolArgument.java,v 1.3 2005/03/25 14:35:04 blowagie Exp $
3  * $Name: $
4  *
5  * Copyright 2005 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

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 /**
65  * This is an argument of one of the tools in the toolbox.
66  */

67 public class ToolArgument implements ActionListener {
68     /** reference to the internal frame */
69     protected AbstractTool tool;
70     /** describes the argument. */
71     protected String description;
72     /** short name for the argument. */
73     protected String name;
74     /** type of the argument. */
75     protected String classname;
76     /** value of the argument. */
77     protected String value = null;
78     
79     /** Constructs a ToolArgument. */
80     public ToolArgument() {}
81     
82     /**
83      * Constructs a ToolArgument.
84      * @param tool the tool that needs this argument
85      * @param name the name of the argument
86      * @param description the description of the argument
87      * @param classname the type of the argument
88      */

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     /**
97      * Gets the argument as an object.
98      * @return an object
99      * @throws InstantiationException
100      */

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     /**
115      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
116      */

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     /**
139      * Give you a String that can be used in a usage description.
140      * @return a String
141      */

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     /**
152      * @return Returns the classname.
153      */

154     public String getClassname() {
155         return classname;
156     }
157     
158     /**
159      * @param classname The classname to set.
160      */

161     public void setClassname(String classname) {
162         this.classname = classname;
163     }
164     
165     /**
166      * @return Returns the description.
167      */

168     public String getDescription() {
169         return description;
170     }
171     
172     /**
173      * @param description The description to set.
174      */

175     public void setDescription(String description) {
176         this.description = description;
177     }
178     
179     /**
180      * @return Returns the name.
181      */

182     public String getName() {
183         return name;
184     }
185     
186     /**
187      * @param name The name to set.
188      */

189     public void setName(String name) {
190         this.name = name;
191     }
192     
193     /**
194      * @return Returns the value.
195      */

196     public String getValue() {
197         return value;
198     }
199     
200     /**
201      * @param value The value to set.
202      */

203     public void setValue(String value) {
204         this.value = value;
205         tool.valueHasChanged(this);
206     }
207 }
Popular Tags