home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Stylepad / src / Wonderland.java < prev   
Encoding:
Java Source  |  2002-09-06  |  9.8 KB  |  287 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.  * @(#)Wonderland.java    1.10 02/06/13
  38.  */
  39.  
  40.  
  41.  
  42. import java.util.Locale;
  43. import java.util.MissingResourceException;
  44. import java.util.ResourceBundle;
  45.  
  46. import java.net.URL;
  47. import java.util.Hashtable;
  48. import java.awt.Color;
  49. import javax.swing.*;
  50. import javax.swing.text.*;
  51.  
  52. /**
  53.  * hack to load attributed content
  54.  */
  55. public class Wonderland {
  56.  
  57.   Wonderland(DefaultStyledDocument doc, StyleContext styles) {
  58.     this.doc = doc;
  59.     this.styles = styles;
  60.     runAttr = new Hashtable();
  61.   }
  62.  
  63.   void loadDocument() {
  64.     createStyles();
  65.     for (int i = 0; i < data.length; i++) {
  66.       Paragraph p = data[i];
  67.       addParagraph(p);
  68.     }
  69.   }
  70.  
  71.   void addParagraph(Paragraph p) {
  72.     try {
  73.       Style s = null;
  74.       for (int i = 0; i < p.data.length; i++) {
  75.     Run run = p.data[i];
  76.     s = (Style) runAttr.get(run.attr);
  77.     doc.insertString(doc.getLength(), run.content, s);
  78.       }
  79.  
  80.       // set logical style
  81.       Style ls = styles.getStyle(p.logical);
  82.       doc.setLogicalStyle(doc.getLength() - 1, ls);
  83.       doc.insertString(doc.getLength(), "\n", null);
  84.     } catch (BadLocationException e) {
  85.       System.err.println("Internal error: " + e);
  86.     }
  87.   }
  88.  
  89.   void createStyles() {
  90.     // no attributes defined
  91.     Style s = styles.addStyle(null, null);
  92.     runAttr.put("none", s);
  93.     s = styles.addStyle(null, null);
  94.     StyleConstants.setItalic(s, true);
  95.     StyleConstants.setForeground(s, new Color(153,153,102));
  96.     runAttr.put("cquote", s); // catepillar quote
  97.  
  98.     s = styles.addStyle(null, null);
  99.     StyleConstants.setItalic(s, true);
  100.     StyleConstants.setForeground(s, new Color(51,102,153));
  101.     runAttr.put("aquote", s); // alice quote
  102.  
  103.     try {
  104.             ResourceBundle resources = ResourceBundle.getBundle("resources.Stylepad", 
  105.                                 Locale.getDefault());
  106.         s = styles.addStyle(null, null);
  107.         Icon alice = new ImageIcon(resources.getString("aliceGif"));
  108.         StyleConstants.setIcon(s, alice);
  109.         runAttr.put("alice", s); // alice
  110.  
  111.         s = styles.addStyle(null, null);
  112.         Icon caterpillar = new ImageIcon(resources.getString("caterpillarGif"));
  113.         StyleConstants.setIcon(s, caterpillar);
  114.         runAttr.put("caterpillar", s); // caterpillar
  115.  
  116.         s = styles.addStyle(null, null);
  117.         Icon hatter = new ImageIcon(resources.getString("hatterGif"));
  118.         StyleConstants.setIcon(s, hatter);
  119.         runAttr.put("hatter", s); // hatter
  120.  
  121.  
  122.     } catch (MissingResourceException mre) {
  123.       // can't display image
  124.     }
  125.  
  126.     Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
  127.  
  128.     Style heading = styles.addStyle("heading", def);
  129.     StyleConstants.setFontFamily(heading, "SansSerif");
  130.     StyleConstants.setBold(heading, true);
  131.     StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
  132.     StyleConstants.setSpaceAbove(heading, 10);
  133.     StyleConstants.setSpaceBelow(heading, 10);
  134.     StyleConstants.setFontSize(heading, 18);
  135.  
  136.     // Title
  137.     Style sty = styles.addStyle("title", heading);
  138.     StyleConstants.setFontSize(sty, 32);
  139.  
  140.     // edition
  141.     sty = styles.addStyle("edition", heading);
  142.     StyleConstants.setFontSize(sty, 16);
  143.  
  144.     // author
  145.     sty = styles.addStyle("author", heading);
  146.     StyleConstants.setItalic(sty, true);
  147.     StyleConstants.setSpaceBelow(sty, 25);
  148.  
  149.     // subtitle
  150.     sty = styles.addStyle("subtitle", heading);
  151.     StyleConstants.setSpaceBelow(sty, 35);
  152.  
  153.     // normal
  154.     sty = styles.addStyle("normal", def);
  155.     StyleConstants.setLeftIndent(sty, 10);
  156.     StyleConstants.setRightIndent(sty, 10);
  157.     StyleConstants.setFontFamily(sty, "SansSerif");
  158.     StyleConstants.setFontSize(sty, 14);
  159.     StyleConstants.setSpaceAbove(sty, 4);
  160.     StyleConstants.setSpaceBelow(sty, 4);
  161.   }
  162.  
  163.   DefaultStyledDocument doc;
  164.   StyleContext styles;
  165.   Hashtable runAttr;
  166.  
  167.   static class Paragraph {
  168.     Paragraph(String logical, Run[] data) {
  169.       this.logical = logical;
  170.       this.data = data;
  171.     }
  172.     String logical;
  173.     Run[] data;
  174.   }
  175.  
  176.   static class Run {
  177.     Run(String attr, String content) {
  178.       this.attr = attr;
  179.       this.content = content;
  180.     }
  181.     String attr;
  182.     String content;
  183.   }
  184.  
  185.   Paragraph[] data = new Paragraph[] {
  186.     new Paragraph("title", new Run[] {
  187.       new Run("none", "ALICE'S ADVENTURES IN WONDERLAND")
  188.     }),
  189.     new Paragraph("author", new Run[] {
  190.       new Run("none", "Lewis Carroll")
  191.     }),
  192.     new Paragraph("heading", new Run[] {
  193.       new Run("alice", " ")
  194.     }),
  195.     new Paragraph("edition", new Run[] {
  196.       new Run("none", "THE MILLENNIUM FULCRUM EDITION 3.0")
  197.     }),
  198.     new Paragraph("heading", new Run[] {
  199.       new Run("none", "CHAPTER V")
  200.     }),
  201.     new Paragraph("subtitle", new Run[] {
  202.       new Run("none", "Advice from a Caterpillar")
  203.     }),
  204.     new Paragraph("normal", new Run[] {
  205.       new Run("none", " "),
  206.     }),
  207.     new Paragraph("normal", new Run[] {
  208.       new Run("none", "The Caterpillar and Alice looked at each other for some time in silence:  at last the Caterpillar took the hookah out of its mouth, and addressed her in a languid, sleepy voice.")
  209.     }),
  210.     new Paragraph("normal", new Run[] {
  211.       new Run("cquote", "Who are YOU?  "),
  212.       new Run("none", "said the Caterpillar.")
  213.     }),
  214.     new Paragraph("normal", new Run[] {
  215.       new Run("none", "This was not an encouraging opening for a conversation.  Alice replied, rather shyly, "),
  216.       new Run("aquote", "I--I hardly know, sir, just at present--at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then. "),
  217.     }),
  218.     new Paragraph("heading", new Run[] {
  219.       new Run("caterpillar", " ")
  220.     }),
  221.     new Paragraph("normal", new Run[] {
  222.       new Run("cquote", "What do you mean by that? "),
  223.       new Run("none", " said the Caterpillar sternly.  "),
  224.       new Run("cquote", "Explain yourself!"),
  225.     }),
  226.     new Paragraph("normal", new Run[] {
  227.       new Run("aquote", "I can't explain MYSELF, I'm afraid, sir"),
  228.       new Run("none", " said Alice, "),
  229.       new Run("aquote", "because I'm not myself, you see."),
  230.     }),
  231.     new Paragraph("normal", new Run[] {
  232.       new Run("cquote", "I don't see,"),
  233.       new Run("none", " said the Caterpillar."),
  234.     }),
  235.     new Paragraph("normal", new Run[] {
  236.       new Run("aquote", "I'm afraid I can't put it more clearly,  "),
  237.       new Run("none", "Alice replied very politely, "),
  238.       new Run("aquote", "for I can't understand it myself to begin with; and being so many different sizes in a day is very confusing."),
  239.     }),
  240.     new Paragraph("normal", new Run[] {
  241.       new Run("cquote", "It isn't,  "),
  242.       new Run("none", "said the Caterpillar.")
  243.     }),
  244.     new Paragraph("normal", new Run[] {
  245.       new Run("aquote", "Well, perhaps you haven't found it so yet,"),
  246.       new Run("none", " said Alice; "),
  247.       new Run("aquote", "but when you have to turn into a chrysalis--you will some day, you know--and then after that into a butterfly, I should think you'll feel it a little queer, won't you?")
  248.     }),
  249.     new Paragraph("normal", new Run[] {
  250.       new Run("cquote", "Not a bit, "),
  251.       new Run("none", "said the Caterpillar.")
  252.     }),
  253.     new Paragraph("normal", new Run[] {
  254.       new Run("aquote", "Well, perhaps your feelings may be different,"),
  255.       new Run("none", " said Alice; "),
  256.       new Run("aquote", "all I know is, it would feel very queer to ME."),
  257.     }),
  258.     new Paragraph("normal", new Run[] {
  259.       new Run("cquote", "You!"),
  260.       new Run("none", " said the Caterpillar contemptuously.  "),
  261.       new Run("cquote", "Who are YOU?"),
  262.     }),
  263.     new Paragraph("normal", new Run[] {
  264.       new Run("normal", "Which brought them back again to the beginning of the conversation.  Alice felt a little irritated at the Caterpillar's making such VERY short remarks, and she drew herself up and said, very gravely, "),
  265.       new Run("aquote", "I think, you ought to tell me who YOU are, first."),
  266.     }),
  267.     new Paragraph("normal", new Run[] {
  268.       new Run("cquote", "Why?  "),
  269.       new Run("none", "said the Caterpillar."),
  270.     }),
  271.     new Paragraph("heading", new Run[] {
  272.       new Run("hatter", " ")
  273.     }),
  274.     new Paragraph("normal", new Run[] {
  275.       new Run("none", " "),
  276.     }),
  277.     new Paragraph("normal", new Run[] {
  278.       new Run("none", " "),
  279.     }),
  280.     new Paragraph("normal", new Run[] {
  281.       new Run("none", " "),
  282.     })
  283.   };
  284.  
  285.  
  286. }
  287.