KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > Toolbox


1 /*
2  * $Id: Toolbox.java,v 1.6 2005/03/31 07:14:26 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;
51
52 import java.awt.event.ActionEvent;
53 import java.awt.event.ActionListener;
54 import java.awt.event.KeyEvent;
55 import java.io.IOException;
56 import java.util.Iterator;
57 import java.util.Properties;
58
59 import javax.swing.JDesktopPane;
60 import javax.swing.JFrame;
61 import javax.swing.JInternalFrame;
62 import javax.swing.JMenu;
63 import javax.swing.JMenuBar;
64 import javax.swing.JMenuItem;
65
66 import com.lowagie.tools.plugins.AbstractTool;
67
68 /**
69  * This is a utility that allows you to use a number of iText tools.
70  */

71 public class Toolbox extends JFrame implements ToolMenuItems, ActionListener {
72     
73     /** The DesktopPane of the toolbox. */
74     private JDesktopPane desktop;
75     /** The list of tools in the toolbox. */
76     private Properties toolmap = new Properties();
77     
78     /**
79      * Constructs the Toolbox object.
80      */

81     public Toolbox() {
82         super();
83         setSize(600, 400);
84         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
85         setResizable(true);
86         setTitle("iText Toolbox");
87         setJMenuBar(getMenubar());
88         desktop = new JDesktopPane();
89         setContentPane(desktop);
90         setVisible(true);
91     }
92     
93     /**
94      * Starts the Toolbox utility.
95      * @param args no arguments needed
96      */

97     public static void main(String[] args) {
98         JFrame.setDefaultLookAndFeelDecorated(true);
99         Toolbox toolbox = new Toolbox();
100     }
101     
102     /**
103      * Gets the menubar.
104      * @return a menubar
105      */

106     private JMenuBar getMenubar() {
107         if (toolmap.size() == 0) {
108             try {
109                 toolmap.load(Toolbox.class.getClassLoader().getResourceAsStream("com/lowagie/tools/plugins/tools.txt"));
110             } catch (IOException e) {
111                 e.printStackTrace();
112             }
113         }
114         JMenuBar menubar = new JMenuBar();
115         JMenu file = new JMenu(FILE);
116         file.setMnemonic(KeyEvent.VK_F);
117         JMenuItem close = new JMenuItem(CLOSE);
118         close.setMnemonic(KeyEvent.VK_C);
119         close.addActionListener(this);
120         file.add(close);
121         JMenu tools = new JMenu(TOOLS);
122         file.setMnemonic(KeyEvent.VK_T);
123         JMenuItem item;
124         String name;
125         for (Iterator i = toolmap.keySet().iterator(); i.hasNext(); ) {
126             name = (String)i.next();
127             item = new JMenuItem(name);
128             item.addActionListener(this);
129             tools.add(item);
130         }
131         menubar.add(file);
132         menubar.add(tools);
133         return menubar;
134     }
135     
136     /**
137      * Creates an Internal Frame.
138      * @param name the name of the app
139      * @throws ClassNotFoundException
140      * @throws IllegalAccessException
141      * @throws InstantiationException
142      */

143     private void createFrame(String name) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
144         AbstractTool ti = (AbstractTool)Class.forName((String)toolmap.get(name)).newInstance();
145         JInternalFrame f = ti.getInternalFrame();
146         f.setVisible(true);
147         desktop.add(f);
148     }
149
150     /**
151      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
152      */

153     public void actionPerformed(ActionEvent evt) {
154         if (CLOSE.equals(evt.getActionCommand())) {
155             System.exit(0);
156         }
157         else {
158             try {
159                 createFrame(evt.getActionCommand());
160             }
161             catch(Exception e) {
162                 e.printStackTrace();
163             }
164         }
165     }
166 }
Popular Tags