home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Stylepad / src / Stylepad.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  9.4 KB  |  319 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Stylepad.java    1.16 02/06/13
  38.  */
  39.  
  40.  
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import java.net.URL;
  44. import java.util.MissingResourceException;
  45. import java.util.ResourceBundle;
  46. import javax.swing.text.*;
  47. import javax.swing.*;
  48.  
  49. import java.io.*;
  50.  
  51. /**
  52.  * Sample application using JTextPane.
  53.  *
  54.  * @author Timothy Prinzing
  55.  * @version 1.16 06/13/02
  56.  */
  57. public class Stylepad extends Notepad {
  58.  
  59.     private static ResourceBundle resources;
  60.  
  61.     static {
  62.         try {
  63.             resources = ResourceBundle.getBundle("resources.Stylepad");
  64.         } catch (MissingResourceException mre) {
  65.             System.err.println("Stylepad.properties not found");
  66.             System.exit(0);
  67.         }
  68.     }
  69.  
  70.     public Stylepad() {
  71.     super();
  72.     }
  73.     
  74.     public static void main(String[] args) {
  75.         String vers = System.getProperty("java.version");
  76.         if (vers.compareTo("1.1.2") < 0) {
  77.             System.out.println("!!!WARNING: Swing must be run with a " +
  78.                                "1.1.2 or higher version VM!!!");
  79.         }
  80.         JFrame frame = new JFrame();
  81.         frame.setTitle(resources.getString("Title"));
  82.     frame.setBackground(Color.lightGray);
  83.     frame.getContentPane().setLayout(new BorderLayout());
  84.     frame.getContentPane().add("Center", new Stylepad());
  85.     frame.addWindowListener(new AppCloser());
  86.     frame.pack();
  87.     frame.setSize(600, 480);
  88.         frame.show();
  89.     }
  90.  
  91.     /**
  92.      * Fetch the list of actions supported by this
  93.      * editor.  It is implemented to return the list
  94.      * of actions supported by the superclass
  95.      * augmented with the actions defined locally.
  96.      */
  97.     public Action[] getActions() {
  98.         Action[] defaultActions = {
  99.             new NewAction(),
  100.             new OpenAction(),
  101.             new SaveAction(),
  102.             new StyledEditorKit.FontFamilyAction("font-family-LucidaSans", 
  103.                                                  "Lucida Sans"),
  104.         };
  105.         Action[] a = TextAction.augmentList(super.getActions(), defaultActions);
  106.     return a;
  107.     }
  108.  
  109.     /**
  110.      * Try and resolve the resource name in the local
  111.      * resource file, and if not found fall back to
  112.      * the superclass resource file.
  113.      */
  114.     protected String getResourceString(String nm) {
  115.     String str;
  116.     try {
  117.         str = this.resources.getString(nm);
  118.     } catch (MissingResourceException mre) {
  119.         str = super.getResourceString(nm);
  120.     }
  121.     return str;
  122.     }
  123.  
  124.     /**
  125.      * Create an editor to represent the given document.  
  126.      */
  127.     protected JTextComponent createEditor() {
  128.     StyleContext sc = new StyleContext();
  129.     DefaultStyledDocument doc = new DefaultStyledDocument(sc);
  130.     initDocument(doc, sc);
  131.         JTextPane p = new JTextPane(doc);
  132.     p.setDragEnabled(true);
  133.         
  134.         //p.getCaret().setBlinkRate(0);
  135.         
  136.         return p;
  137.     }
  138.  
  139.     /**
  140.      * Create a menu for the app.  This is redefined to trap 
  141.      * a couple of special entries for now.
  142.      */
  143.     protected JMenu createMenu(String key) {
  144.     if (key.equals("color")) {
  145.         return createColorMenu();
  146.     } 
  147.     return super.createMenu(key);
  148.     }
  149.  
  150.  
  151.     // this will soon be replaced
  152.     JMenu createColorMenu() {
  153.     ActionListener a;
  154.     JMenuItem mi;
  155.     JMenu menu = new JMenu(getResourceString("color" + labelSuffix));
  156.     mi = new JMenuItem(resources.getString("Red"));
  157.     mi.setHorizontalTextPosition(JButton.RIGHT);
  158.     mi.setIcon(new ColoredSquare(Color.red));
  159.     a = new StyledEditorKit.ForegroundAction("set-foreground-red", Color.red);
  160.     //a = new ColorAction(se, Color.red);
  161.     mi.addActionListener(a);
  162.     menu.add(mi);
  163.     mi = new JMenuItem(resources.getString("Green"));
  164.     mi.setHorizontalTextPosition(JButton.RIGHT);
  165.     mi.setIcon(new ColoredSquare(Color.green));
  166.     a = new StyledEditorKit.ForegroundAction("set-foreground-green", Color.green);
  167.     //a = new ColorAction(se, Color.green);
  168.     mi.addActionListener(a);
  169.     menu.add(mi);
  170.     mi = new JMenuItem(resources.getString("Blue"));
  171.     mi.setHorizontalTextPosition(JButton.RIGHT);
  172.     mi.setIcon(new ColoredSquare(Color.blue));
  173.     a = new StyledEditorKit.ForegroundAction("set-foreground-blue", Color.blue);
  174.     //a = new ColorAction(se, Color.blue);
  175.     mi.addActionListener(a);
  176.     menu.add(mi);
  177.  
  178.     return menu;
  179.     }
  180.  
  181.     void initDocument(DefaultStyledDocument doc, StyleContext sc) {
  182.     Wonderland w = new Wonderland(doc, sc);
  183.     //HelloWorld h = new HelloWorld(doc, sc);
  184.     Icon alice = new ImageIcon(resources.getString("aliceGif"));
  185.     w.loadDocument();
  186.     }
  187.  
  188.     JComboBox createFamilyChoices() {
  189.         JComboBox b = new JComboBox();
  190.     String[] fonts = getToolkit().getFontList();
  191.     for (int i = 0; i < fonts.length; i++) {
  192.         b.addItem(fonts[i]);
  193.     }
  194.     return b;
  195.     }
  196.  
  197.     /**
  198.      * Trys to read a file which is assumed to be a 
  199.      * serialization of a document.
  200.      */
  201.     class OpenAction extends AbstractAction {
  202.  
  203.     OpenAction() {
  204.         super(openAction);
  205.     }
  206.  
  207.         public void actionPerformed(ActionEvent e) {
  208.         Frame frame = getFrame();
  209.         if (fileDialog == null) {
  210.         fileDialog = new FileDialog(frame);
  211.         }
  212.         fileDialog.setMode(FileDialog.LOAD);
  213.         fileDialog.show();
  214.         
  215.         String file = fileDialog.getFile();
  216.         if (file == null) {
  217.         return;
  218.         }
  219.         String directory = fileDialog.getDirectory();
  220.         File f = new File(directory, file);
  221.         if (f.exists()) {
  222.         try {
  223.             FileInputStream fin = new FileInputStream(f);
  224.             ObjectInputStream istrm = new ObjectInputStream(fin);
  225.             Document doc = (Document) istrm.readObject();
  226.             if(getEditor().getDocument() != null)
  227.             getEditor().getDocument().removeUndoableEditListener
  228.                     (undoHandler);
  229.             getEditor().setDocument(doc);
  230.             doc.addUndoableEditListener(undoHandler);
  231.             resetUndoManager();
  232.             frame.setTitle(file);
  233.             validate();
  234.         } catch (IOException io) {
  235.             // should put in status panel
  236.             System.err.println("IOException: " + io.getMessage());
  237.         } catch (ClassNotFoundException cnf) {
  238.             // should put in status panel
  239.             System.err.println("Class not found: " + cnf.getMessage());
  240.         }
  241.         } else {
  242.         // should put in status panel
  243.         System.err.println("No such file: " + f);
  244.         }
  245.     }
  246.     }
  247.  
  248.     /**
  249.      * Trys to write the document as a serialization.
  250.      */
  251.     class SaveAction extends AbstractAction {
  252.  
  253.     SaveAction() {
  254.         super(saveAction);
  255.     }
  256.  
  257.         public void actionPerformed(ActionEvent e) {
  258.         Frame frame = getFrame();
  259.         if (fileDialog == null) {
  260.         fileDialog = new FileDialog(frame);
  261.         }
  262.         fileDialog.setMode(FileDialog.SAVE);
  263.         fileDialog.show();
  264.         String file = fileDialog.getFile();
  265.         if (file == null) {
  266.         return;
  267.         }
  268.         String directory = fileDialog.getDirectory();
  269.         File f = new File(directory, file);
  270.         try {
  271.         FileOutputStream fstrm = new FileOutputStream(f);
  272.         ObjectOutput ostrm = new ObjectOutputStream(fstrm);
  273.         ostrm.writeObject(getEditor().getDocument());
  274.         ostrm.flush();
  275.         } catch (IOException io) {
  276.         // should put in status panel
  277.         System.err.println("IOException: " + io.getMessage());
  278.         }
  279.     }
  280.     }
  281.  
  282.     /**
  283.      * Creates an empty document.
  284.      */
  285.     class NewAction extends AbstractAction {
  286.  
  287.     NewAction() {
  288.         super(newAction);
  289.     }
  290.  
  291.         public void actionPerformed(ActionEvent e) {
  292.         if(getEditor().getDocument() != null)
  293.         getEditor().getDocument().removeUndoableEditListener
  294.                     (undoHandler);
  295.         getEditor().setDocument(new DefaultStyledDocument());
  296.         getEditor().getDocument().addUndoableEditListener(undoHandler);
  297.         resetUndoManager();
  298.         validate();
  299.     }
  300.     }
  301.  
  302.     class ColoredSquare implements Icon {
  303.     Color color;
  304.     public ColoredSquare(Color c) {
  305.         this.color = c;
  306.     }
  307.  
  308.     public void paintIcon(Component c, Graphics g, int x, int y) {
  309.         Color oldColor = g.getColor();
  310.         g.setColor(color);
  311.         g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
  312.         g.setColor(oldColor);
  313.     }
  314.     public int getIconWidth() { return 12; }
  315.     public int getIconHeight() { return 12; }
  316.  
  317.     }
  318. }
  319.