KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Stylepad


1 /*
2  * @(#)Stylepad.java 1.22 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Stylepad.java 1.22 05/11/17
39  */

40
41
42 import java.awt.*;
43 import java.awt.event.*;
44 import java.net.URL JavaDoc;
45 import java.util.MissingResourceException JavaDoc;
46 import java.util.ResourceBundle JavaDoc;
47 import javax.swing.text.*;
48 import javax.swing.*;
49
50 import java.io.*;
51
52 /**
53  * Sample application using JTextPane.
54  *
55  * @author Timothy Prinzing
56  * @version 1.22 11/17/05
57  */

58 public class Stylepad extends Notepad {
59
60     private static ResourceBundle JavaDoc resources;
61
62     static {
63         try {
64             resources = ResourceBundle.getBundle("resources.Stylepad");
65         } catch (MissingResourceException JavaDoc mre) {
66             System.err.println("Stylepad.properties not found");
67             System.exit(0);
68         }
69     }
70
71     public Stylepad() {
72     super();
73     }
74     
75     public static void main(String JavaDoc[] args) {
76         String JavaDoc vers = System.getProperty("java.version");
77         if (vers.compareTo("1.1.2") < 0) {
78             System.out.println("!!!WARNING: Swing must be run with a " +
79                                "1.1.2 or higher version VM!!!");
80         }
81         JFrame frame = new JFrame();
82         frame.setTitle(resources.getString("Title"));
83     frame.setBackground(Color.lightGray);
84     frame.getContentPane().setLayout(new BorderLayout());
85         Stylepad stylepad = new Stylepad();
86     frame.getContentPane().add("Center", stylepad);
87         frame.setJMenuBar(stylepad.createMenubar());
88     frame.addWindowListener(new AppCloser());
89     frame.pack();
90     frame.setSize(600, 480);
91         frame.show();
92     }
93
94     /**
95      * Fetch the list of actions supported by this
96      * editor. It is implemented to return the list
97      * of actions supported by the superclass
98      * augmented with the actions defined locally.
99      */

100     public Action[] getActions() {
101         Action[] defaultActions = {
102             new NewAction(),
103             new OpenAction(),
104             new SaveAction(),
105             new StyledEditorKit.FontFamilyAction("font-family-LucidaSans",
106                                                  "Lucida Sans"),
107         };
108         Action[] a = TextAction.augmentList(super.getActions(), defaultActions);
109     return a;
110     }
111
112     /**
113      * Try and resolve the resource name in the local
114      * resource file, and if not found fall back to
115      * the superclass resource file.
116      */

117     protected String JavaDoc getResourceString(String JavaDoc nm) {
118     String JavaDoc str;
119     try {
120         str = this.resources.getString(nm);
121     } catch (MissingResourceException JavaDoc mre) {
122         str = super.getResourceString(nm);
123     }
124     return str;
125     }
126
127     /**
128      * Create an editor to represent the given document.
129      */

130     protected JTextComponent createEditor() {
131     StyleContext sc = new StyleContext();
132     DefaultStyledDocument doc = new DefaultStyledDocument(sc);
133     initDocument(doc, sc);
134         JTextPane p = new JTextPane(doc);
135     p.setDragEnabled(true);
136         
137         //p.getCaret().setBlinkRate(0);
138

139         return p;
140     }
141
142     /**
143      * Create a menu for the app. This is redefined to trap
144      * a couple of special entries for now.
145      */

146     protected JMenu createMenu(String JavaDoc key) {
147     if (key.equals("color")) {
148         return createColorMenu();
149     }
150     return super.createMenu(key);
151     }
152
153
154     // this will soon be replaced
155
JMenu createColorMenu() {
156     ActionListener a;
157     JMenuItem mi;
158     JMenu menu = new JMenu(getResourceString("color" + labelSuffix));
159     mi = new JMenuItem(resources.getString("Red"));
160     mi.setHorizontalTextPosition(JButton.RIGHT);
161     mi.setIcon(new ColoredSquare(Color.red));
162     a = new StyledEditorKit.ForegroundAction("set-foreground-red", Color.red);
163     //a = new ColorAction(se, Color.red);
164
mi.addActionListener(a);
165     menu.add(mi);
166     mi = new JMenuItem(resources.getString("Green"));
167     mi.setHorizontalTextPosition(JButton.RIGHT);
168     mi.setIcon(new ColoredSquare(Color.green));
169     a = new StyledEditorKit.ForegroundAction("set-foreground-green", Color.green);
170     //a = new ColorAction(se, Color.green);
171
mi.addActionListener(a);
172     menu.add(mi);
173     mi = new JMenuItem(resources.getString("Blue"));
174     mi.setHorizontalTextPosition(JButton.RIGHT);
175     mi.setIcon(new ColoredSquare(Color.blue));
176     a = new StyledEditorKit.ForegroundAction("set-foreground-blue", Color.blue);
177     //a = new ColorAction(se, Color.blue);
178
mi.addActionListener(a);
179     menu.add(mi);
180
181     return menu;
182     }
183
184     void initDocument(DefaultStyledDocument doc, StyleContext sc) {
185     Wonderland w = new Wonderland(doc, sc);
186     //HelloWorld h = new HelloWorld(doc, sc);
187
Icon alice = new ImageIcon(resources.getString("aliceGif"));
188     w.loadDocument();
189     }
190
191     JComboBox createFamilyChoices() {
192         JComboBox b = new JComboBox();
193     String JavaDoc[] fonts = getToolkit().getFontList();
194     for (int i = 0; i < fonts.length; i++) {
195         b.addItem(fonts[i]);
196     }
197     return b;
198     }
199
200     /**
201      * Trys to read a file which is assumed to be a
202      * serialization of a document.
203      */

204     class OpenAction extends AbstractAction {
205
206     OpenAction() {
207         super(openAction);
208     }
209
210         public void actionPerformed(ActionEvent e) {
211         Frame frame = getFrame();
212         if (fileDialog == null) {
213         fileDialog = new FileDialog(frame);
214         }
215         fileDialog.setMode(FileDialog.LOAD);
216         fileDialog.show();
217         
218         String JavaDoc file = fileDialog.getFile();
219         if (file == null) {
220         return;
221         }
222         String JavaDoc directory = fileDialog.getDirectory();
223         File f = new File(directory, file);
224         if (f.exists()) {
225         try {
226             FileInputStream fin = new FileInputStream(f);
227             ObjectInputStream istrm = new ObjectInputStream(fin);
228             Document doc = (Document) istrm.readObject();
229             if(getEditor().getDocument() != null)
230             getEditor().getDocument().removeUndoableEditListener
231                     (undoHandler);
232             getEditor().setDocument(doc);
233             doc.addUndoableEditListener(undoHandler);
234             resetUndoManager();
235             frame.setTitle(file);
236             validate();
237         } catch (IOException io) {
238             // should put in status panel
239
System.err.println("IOException: " + io.getMessage());
240         } catch (ClassNotFoundException JavaDoc cnf) {
241             // should put in status panel
242
System.err.println("Class not found: " + cnf.getMessage());
243         }
244         } else {
245         // should put in status panel
246
System.err.println("No such file: " + f);
247         }
248     }
249     }
250
251     /**
252      * Trys to write the document as a serialization.
253      */

254     class SaveAction extends AbstractAction {
255
256     SaveAction() {
257         super(saveAction);
258     }
259
260         public void actionPerformed(ActionEvent e) {
261         Frame frame = getFrame();
262         if (fileDialog == null) {
263         fileDialog = new FileDialog(frame);
264         }
265         fileDialog.setMode(FileDialog.SAVE);
266         fileDialog.show();
267         String JavaDoc file = fileDialog.getFile();
268         if (file == null) {
269         return;
270         }
271         String JavaDoc directory = fileDialog.getDirectory();
272         File f = new File(directory, file);
273         try {
274         FileOutputStream fstrm = new FileOutputStream(f);
275         ObjectOutput ostrm = new ObjectOutputStream(fstrm);
276         ostrm.writeObject(getEditor().getDocument());
277         ostrm.flush();
278                 frame.setTitle(f.getName());
279         } catch (IOException io) {
280         // should put in status panel
281
System.err.println("IOException: " + io.getMessage());
282         }
283     }
284     }
285
286     /**
287      * Creates an empty document.
288      */

289     class NewAction extends AbstractAction {
290
291     NewAction() {
292         super(newAction);
293     }
294
295         public void actionPerformed(ActionEvent e) {
296         if(getEditor().getDocument() != null)
297         getEditor().getDocument().removeUndoableEditListener
298                     (undoHandler);
299         getEditor().setDocument(new DefaultStyledDocument());
300         getEditor().getDocument().addUndoableEditListener(undoHandler);
301         resetUndoManager();
302             getFrame().setTitle(resources.getString("Title"));
303         validate();
304     }
305     }
306
307     class ColoredSquare implements Icon {
308     Color color;
309     public ColoredSquare(Color c) {
310         this.color = c;
311     }
312
313     public void paintIcon(Component c, Graphics g, int x, int y) {
314         Color oldColor = g.getColor();
315         g.setColor(color);
316         g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
317         g.setColor(oldColor);
318     }
319     public int getIconWidth() { return 12; }
320     public int getIconHeight() { return 12; }
321
322     }
323 }
324
Popular Tags