File: Toolbox.java

package info (click to toggle)
libitext1-java 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,384 kB
  • ctags: 12,267
  • sloc: java: 86,866; xml: 391; makefile: 22
file content (334 lines) | stat: -rw-r--r-- 11,046 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * $Id: Toolbox.java,v 1.15 2005/12/03 16:41:53 blowagie Exp $
 * $Name:  $
 *
 * Copyright 2005 by Bruno Lowagie.
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */
package com.lowagie.tools;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.TreeMap;

import javax.swing.Box;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import com.lowagie.tools.plugins.AbstractTool;

import java.awt.Toolkit;
import java.awt.Dimension;

/**
 * This is a utility that allows you to use a number of iText tools.
 */
public class Toolbox extends JFrame implements ToolMenuItems, ActionListener {

	/** The DesktopPane of the toolbox. */
	private JDesktopPane desktop;
	/** The ConsolePane of the toolbox. */
	private JScrollPane console;

	/** The list of tools in the toolbox. */
	private Properties toolmap = new Properties();

	/** x-coordinate of the location of a new internal frame. */
	private int locationX = 0;

	/** y-coordinate of the location of a new internal frame. */
	private int locationY = 0;

	/**
	 * Constructs the Toolbox object.
	 */
	public Toolbox() {
		super();
		setSize(600, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(true);
		setTitle("iText Toolbox");
		setJMenuBar(getMenubar());
		desktop = new JDesktopPane();
		Console c;
		try {
			c = new Console();
			console = new JScrollPane(c.textArea);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, desktop, console); 
        splitPane.setContinuousLayout(true); 
     	splitPane.setOneTouchExpandable(true); 
     	splitPane.setDividerLocation(300);
		setContentPane(splitPane);
		centerFrame(this);
		setVisible(true);
	}

	/**
	 * Starts the Toolbox utility.
	 * 
	 * @param args
	 *            no arguments needed
	 */
	public static void main(String[] args) {
		Toolbox toolbox = new Toolbox();
	}

	/**
	 * Gets the menubar.
	 * 
	 * @return a menubar
	 */
	private JMenuBar getMenubar() {
		Properties p = new Properties();
		try {
			p.load(Toolbox.class.getClassLoader().getResourceAsStream(
					"com/lowagie/tools/plugins/tools.txt"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		toolmap = new Properties();
		TreeMap tmp = new TreeMap();
		tmp.putAll(p);
		JMenuBar menubar = new JMenuBar();
		JMenu file = new JMenu(FILE);
		JMenuItem close = new JMenuItem(CLOSE);
		close.setMnemonic(KeyEvent.VK_C);
		close.addActionListener(this);
		file.add(close);
		JMenu tools = new JMenu(TOOLS);
		file.setMnemonic(KeyEvent.VK_T);
		String name, tool;
		JMenu current = null;
		JMenuItem item;
		for (Iterator i = tmp.keySet().iterator(); i.hasNext();) {
			name = (String) i.next();
			if (current == null || !name.startsWith(current.getText())) {
				current = new JMenu(name.substring(0, name.indexOf(".")));
				tools.add(current);
			}
			item = new JMenuItem(name.substring(current.getText().length() + 1));
			item.addActionListener(this);
			tool = (String) tmp.get(name);
			try {
				Class.forName(tool);
				toolmap.put(item.getText(), tool);
				current.add(item);
			} catch (ClassNotFoundException e) {
				System.err.println("Plugin " + name
						+ " was not found in your CLASSPATH.");
			}
		}
		JMenu help = new JMenu(HELP);
		JMenuItem about = new JMenuItem(ABOUT);
		about.setMnemonic(KeyEvent.VK_A);
		about.addActionListener(this);
		help.add(about);
		JMenuItem versions = new JMenuItem(VERSION);
		versions.addActionListener(this);
		help.add(versions);
		menubar.add(file);
		menubar.add(tools);
		menubar.add(Box.createGlue());
		menubar.add(help);
		return menubar;
	}

	/**
	 * Creates an Internal Frame.
	 * 
	 * @param name
	 *            the name of the app
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws PropertyVetoException
	 */
	private void createFrame(String name) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException,
			PropertyVetoException {
		AbstractTool ti = (AbstractTool) Class.forName(
				(String) toolmap.get(name)).newInstance();
		JInternalFrame f = ti.getInternalFrame();
		f.setLocation(locationX, locationY);
		locationX += 25;
		if (locationX > this.getWidth() + 50)
			locationX = 0;
		locationY += 25;
		if (locationY > this.getHeight() + 50)
			locationY = 0;
		f.setVisible(true);
		desktop.add(f);
		f.setSelected(true);
	}

	public static void centerFrame(JFrame f) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = f.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		f.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent evt) {
		if (CLOSE.equals(evt.getActionCommand())) {
			System.out.println("The Toolbox is closed.");
			System.exit(0);
		} else if (ABOUT.equals(evt.getActionCommand())) {
			System.out
					.println("The iText Toolbox is part of iText, a Free Java-PDF Library.\nVisit http://www.lowagie.com/iText/toolbox.html for more info.");
			try {
				Executable
						.launchBrowser("http://www.lowagie.com/iText/toolbox.html");
			} catch (IOException ioe) {
				JOptionPane
						.showMessageDialog(
								this,
								"The iText Toolbox is part of iText, a Free Java-PDF Library.\nVisit http://www.lowagie.com/iText/toolbox.html for more info.");
			}
		} else if (VERSION.equals(evt.getActionCommand())) {
			JFrame f = new Versions();
			centerFrame(f);
			f.setVisible(true);
		} else {
			try {
				createFrame(evt.getActionCommand());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
    /**
     * A Class that redirects output to System.out and System.err.
     */
    public class Console {
        PipedInputStream piOut;
        PipedInputStream piErr;
        PipedOutputStream poOut;
        PipedOutputStream poErr;
        JTextArea textArea = new JTextArea();
    
        /**
         * Creates a new Console object.
         * @param columns
         * @param rows
         * @throws IOException
         */
        public Console() throws IOException {
            // Set up System.out
            piOut = new PipedInputStream();
            poOut = new PipedOutputStream(piOut);
            System.setOut(new PrintStream(poOut, true));
    
            // Set up System.err
            piErr = new PipedInputStream();
            poErr = new PipedOutputStream(piErr);
            System.setErr(new PrintStream(poErr, true));
    
            // Add a scrolling text area
            textArea.setEditable(false);
    
            // Create reader threads
            new ReaderThread(piOut).start();
            new ReaderThread(piErr).start();
        }
    
        class ReaderThread extends Thread {
            PipedInputStream pi;
    
            ReaderThread(PipedInputStream pi) {
                this.pi = pi;
            }
    
            /**
             * @see java.lang.Thread#run()
             */
            public void run() {
                final byte[] buf = new byte[1024];

                while (true) {
                	try {
                        final int len = pi.read(buf);
                        if (len == -1) {
                            break;
                        }
                        textArea.append(new String(buf, 0, len));
                        textArea.setCaretPosition(textArea.getDocument().getLength());
                    
                	} catch (IOException e) {
                	}
                }
            }
        }
    }
	
}