KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > plugins > AbstractTool


1 /*
2  * $Id: AbstractTool.java,v 1.5 2005/04/06 12:08:18 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.plugins;
51
52 import java.awt.event.ActionEvent;
53 import java.awt.event.ActionListener;
54 import java.awt.event.KeyEvent;
55 import java.io.File;
56 import java.io.IOException;
57 import java.io.PipedInputStream;
58 import java.io.PipedOutputStream;
59 import java.io.PrintStream;
60 import java.util.ArrayList;
61 import java.util.Iterator;
62
63 import javax.swing.JInternalFrame;
64 import javax.swing.JMenu;
65 import javax.swing.JMenuBar;
66 import javax.swing.JMenuItem;
67 import javax.swing.JOptionPane;
68 import javax.swing.JScrollPane;
69 import javax.swing.JTextArea;
70
71 import com.lowagie.tools.Executable;
72 import com.lowagie.tools.ToolMenuItems;
73 import com.lowagie.tools.arguments.ToolArgument;
74
75 /**
76  * Every iText tool has to implement this interface.
77  */

78 public abstract class AbstractTool implements ToolMenuItems, ActionListener {
79        
80     /**
81      * A Class that redirects output to System.out and System.err.
82      */

83     public class Console {
84         PipedInputStream piOut;
85         PipedInputStream piErr;
86         PipedOutputStream poOut;
87         PipedOutputStream poErr;
88         JTextArea textArea = new JTextArea();
89     
90         /**
91          * Creates a new Console object.
92          * @param columns
93          * @param rows
94          * @throws IOException
95          */

96         public Console(int columns, int rows) throws IOException {
97             // Set up System.out
98
piOut = new PipedInputStream();
99             poOut = new PipedOutputStream(piOut);
100             System.setOut(new PrintStream(poOut, true));
101     
102             // Set up System.err
103
piErr = new PipedInputStream();
104             poErr = new PipedOutputStream(piErr);
105             System.setErr(new PrintStream(poErr, true));
106     
107             // Add a scrolling text area
108
textArea.setEditable(false);
109             textArea.setRows(rows);
110             textArea.setColumns(columns);
111     
112             // Create reader threads
113
new ReaderThread(piOut).start();
114             new ReaderThread(piErr).start();
115         }
116     
117         class ReaderThread extends Thread {
118             PipedInputStream pi;
119     
120             ReaderThread(PipedInputStream pi) {
121                 this.pi = pi;
122             }
123     
124             /**
125              * @see java.lang.Thread#run()
126              */

127             public void run() {
128                 final byte[] buf = new byte[1024];
129                 try {
130                     while (true) {
131                         final int len = pi.read(buf);
132                         if (len == -1) {
133                             break;
134                         }
135                         textArea.append(new String(buf, 0, len));
136                         textArea.setCaretPosition(textArea.getDocument().getLength());
137                     }
138                 } catch (IOException e) {
139                 }
140             }
141         }
142     }
143     
144     
145     /** The internal frame of the tool. */
146     protected JInternalFrame internalFrame = null;
147     /** The list of arguments needed by the tool. */
148     protected ArrayList arguments = new ArrayList();
149     /** Execute menu options */
150     protected int menuoptions = MENU_EXECUTE;
151     /** a menu option */
152     public static final int MENU_EXECUTE = 1;
153     /** a menu option */
154     public static final int MENU_EXECUTE_SHOW = 2;
155     /** a menu option */
156     public static final int MENU_EXECUTE_PRINT = 4;
157     /** a menu option */
158     public static final int MENU_EXECUTE_PRINT_SILENT = 8;
159     
160     /**
161      * Sets the arguments.
162      * @param arguments The arguments to set.
163      */

164     public void setArguments(ArrayList arguments) {
165         this.arguments = arguments;
166     }
167     
168     /**
169      * Sets the arguments.
170      * @param args the arguments as String-array.
171      */

172     public void setArguments(String[] args) {
173         int counter = 0;
174         ToolArgument argument;
175         for (Iterator i = arguments.iterator(); i.hasNext(); ) {
176             argument = (ToolArgument) i.next();
177             if (args.length > counter) {
178                 argument.setValue(args[counter]);
179             }
180             else {
181                 break;
182             }
183             counter++;
184         }
185     }
186     
187     /**
188      * Gets the arguments.
189      * @return Returns the arguments.
190      */

191     public ArrayList getArguments() {
192         return arguments;
193     }
194     
195     /**
196      * Gets the value of a given argument.
197      * @param name the name of the argument
198      * @return the value of an argument as an Object.
199      * @throws InstantiationException
200      */

201     public Object getValue(String name) throws InstantiationException {
202         ToolArgument argument;
203         for (Iterator i = arguments.iterator(); i.hasNext(); ) {
204             argument = (ToolArgument) i.next();
205             if (name.equals(argument.getName())) {
206                 return argument.getArgument();
207             }
208         }
209         return null;
210     }
211
212     /**
213      * Sets the internal frame.
214      * @param internalFrame The internalFrame to set.
215      */

216     public void setInternalFrame(JInternalFrame internalFrame) {
217         this.internalFrame = internalFrame;
218     }
219     
220     /**
221      * Returns the internal frame. Creates one if it's null.
222      * @return Returns the internalFrame.
223      */

224     public JInternalFrame getInternalFrame() {
225         if (internalFrame == null) {
226             createFrame();
227         }
228         return internalFrame;
229     }
230     
231     /**
232      * Gets the menubar.
233      * @return a menubar for this tool
234      */

235     public JMenuBar getMenubar() {
236         JMenuBar menubar = new JMenuBar();
237         JMenu tool = new JMenu(TOOL);
238         tool.setMnemonic(KeyEvent.VK_F);
239         JMenuItem usage = new JMenuItem(USAGE);
240         usage.setMnemonic(KeyEvent.VK_U);
241         usage.addActionListener(this);
242         tool.add(usage);
243         JMenuItem args = new JMenuItem(ARGUMENTS);
244         args.setMnemonic(KeyEvent.VK_A);
245         args.addActionListener(this);
246         tool.add(args);
247         if ((menuoptions & MENU_EXECUTE) > 0) {
248             JMenuItem execute = new JMenuItem(EXECUTE);
249             execute.setMnemonic(KeyEvent.VK_E);
250             execute.addActionListener(this);
251             tool.add(execute);
252         }
253         if ((menuoptions & MENU_EXECUTE_SHOW) > 0) {
254             JMenuItem execute = new JMenuItem(EXECUTESHOW);
255             execute.addActionListener(this);
256             tool.add(execute);
257         }
258         if ((menuoptions & MENU_EXECUTE_PRINT) > 0) {
259             JMenuItem execute = new JMenuItem(EXECUTEPRINT);
260             execute.addActionListener(this);
261             tool.add(execute);
262         }
263         if ((menuoptions & MENU_EXECUTE_PRINT_SILENT) > 0) {
264             JMenuItem execute = new JMenuItem(EXECUTEPRINTSILENT);
265             execute.addActionListener(this);
266             tool.add(execute);
267         }
268         JMenuItem close = new JMenuItem(CLOSE);
269         close.setMnemonic(KeyEvent.VK_C);
270         close.addActionListener(this);
271         tool.add(close);
272         menubar.add(tool);
273         if (arguments.size() > 0) {
274             JMenu params = new JMenu(ARGUMENTS);
275             tool.setMnemonic(KeyEvent.VK_T);
276             JMenuItem item;
277             ToolArgument argument;
278             for (Iterator i = arguments.iterator(); i.hasNext(); ) {
279                 argument = (ToolArgument)i.next();
280                 item = new JMenuItem(argument.getName());
281                 item.setToolTipText(argument.getDescription());
282                 item.addActionListener(argument);
283                 params.add(item);
284             }
285             menubar.add(params);
286         }
287         return menubar;
288     }
289     
290     /**
291      * Gets a console JScrollPanel that listens to the System.err and System.out.
292      * @param columns a number of columns for the console
293      * @param rows a number of rows for the console
294      * @return a JScrollPane with a Console that shows everything that was written to System.out or System.err
295      */

296     public JScrollPane getConsole(int columns, int rows) {
297         try {
298             Console console = new Console(columns, rows);
299             return new JScrollPane(console.textArea);
300         }
301         catch(IOException ioe) {
302             ioe.printStackTrace();
303             return null;
304         }
305     }
306     
307     /**
308      * Gets the usage of the tool.
309      * @return a String describing how to use the tool.
310      */

311     public String getUsage() {
312         StringBuffer buf = new StringBuffer("java ");
313         buf.append(getClass().getName());
314         ToolArgument argument;
315         for (Iterator i = arguments.iterator(); i.hasNext(); ) {
316             argument = (ToolArgument) i.next();
317             buf.append(" ");
318             buf.append(argument.getName());
319         }
320         buf.append("\n");
321         for (Iterator i = arguments.iterator(); i.hasNext(); ) {
322             argument = (ToolArgument) i.next();
323             buf.append(argument.getUsage());
324         }
325         return buf.toString();
326     }
327     
328     /**
329      * Gets the current arguments of the tool.
330      * @return a String with the list of arguments and their values.
331      */

332     public String getArgs() {
333         StringBuffer buf = new StringBuffer("Current arguments:\n");
334         ToolArgument argument;
335         for (Iterator i = arguments.iterator(); i.hasNext(); ) {
336             argument = (ToolArgument) i.next();
337             buf.append(" ");
338             buf.append(argument.getName());
339             if (argument.getValue() == null) {
340                 buf.append(" = null\n");
341             }
342             else {
343                 buf.append(" = '");
344                 buf.append(argument.getValue());
345                 buf.append("'\n");
346             }
347         }
348         return buf.toString();
349     }
350
351     /**
352      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
353      */

354     public void actionPerformed(ActionEvent evt) {
355         if (CLOSE.equals(evt.getActionCommand())) {
356             internalFrame.dispose();
357         }
358         if (USAGE.equals(evt.getActionCommand())) {
359             JOptionPane.showMessageDialog(internalFrame, getUsage());
360         }
361         if (ARGUMENTS.equals(evt.getActionCommand())) {
362             JOptionPane.showMessageDialog(internalFrame, getArgs());
363         }
364         if (EXECUTE.equals(evt.getActionCommand())) {
365             this.execute();
366         }
367         if (EXECUTESHOW.equals(evt.getActionCommand())) {
368             this.execute();
369             try {
370                 Executable.openDocument(getDestPathPDF());
371             } catch (Exception e) {
372                 System.err.println(e.getMessage());
373             }
374         }
375         if (EXECUTEPRINT.equals(evt.getActionCommand())) {
376             this.execute();
377             try {
378                 Executable.printDocument(getDestPathPDF());
379             } catch (Exception e) {
380                 System.err.println(e.getMessage());
381             }
382         }
383         if (EXECUTEPRINTSILENT.equals(evt.getActionCommand())) {
384             this.execute();
385             try {
386                 Executable.printDocumentSilent(getDestPathPDF());
387             } catch (Exception e) {
388                 System.err.println(e.getMessage());
389             }
390         }
391     }
392     
393     /**
394      * Gets the PDF file that should be generated (or null if the output isn't a PDF file).
395      * @return the PDF file that should be generated
396      * @throws InstantiationException
397      */

398     protected abstract File getDestPathPDF() throws InstantiationException;
399     
400     /**
401      * Creates the internal frame.
402      */

403     protected abstract void createFrame();
404
405     /**
406      * Executes the tool (in most cases this generates a PDF file).
407      */

408     public abstract void execute();
409     
410     /**
411      * Indicates that the value of an argument has changed.
412      * @param arg the argument that has changed
413      */

414     public abstract void valueHasChanged(ToolArgument arg);
415 }
Popular Tags