home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Stylepad / Wonderland.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  8.4 KB  |  262 lines

  1. /*
  2.  * @(#)Wonderland.java    1.6 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16.  
  17. import java.util.Locale;
  18. import java.util.MissingResourceException;
  19. import java.util.ResourceBundle;
  20.  
  21. import java.net.URL;
  22. import java.util.Hashtable;
  23. import java.awt.Color;
  24. import javax.swing.*;
  25. import javax.swing.text.*;
  26.  
  27. /**
  28.  * hack to load attributed content
  29.  */
  30. public class Wonderland {
  31.  
  32.   Wonderland(DefaultStyledDocument doc, StyleContext styles) {
  33.     this.doc = doc;
  34.     this.styles = styles;
  35.     runAttr = new Hashtable();
  36.   }
  37.  
  38.   void loadDocument() {
  39.     createStyles();
  40.     for (int i = 0; i < data.length; i++) {
  41.       Paragraph p = data[i];
  42.       addParagraph(p);
  43.     }
  44.   }
  45.  
  46.   void addParagraph(Paragraph p) {
  47.     try {
  48.       Style s = null;
  49.       for (int i = 0; i < p.data.length; i++) {
  50.     Run run = p.data[i];
  51.     s = (Style) runAttr.get(run.attr);
  52.     doc.insertString(doc.getLength(), run.content, s);
  53.       }
  54.  
  55.       // set logical style
  56.       Style ls = styles.getStyle(p.logical);
  57.       doc.setLogicalStyle(doc.getLength() - 1, ls);
  58.       doc.insertString(doc.getLength(), "\n", null);
  59.     } catch (BadLocationException e) {
  60.       System.err.println("Internal error: " + e);
  61.     }
  62.   }
  63.  
  64.   void createStyles() {
  65.     // no attributes defined
  66.     Style s = styles.addStyle(null, null);
  67.     runAttr.put("none", s);
  68.     s = styles.addStyle(null, null);
  69.     StyleConstants.setItalic(s, true);
  70.     StyleConstants.setForeground(s, new Color(153,153,102));
  71.     runAttr.put("cquote", s); // catepillar quote
  72.  
  73.     s = styles.addStyle(null, null);
  74.     StyleConstants.setItalic(s, true);
  75.     StyleConstants.setForeground(s, new Color(51,102,153));
  76.     runAttr.put("aquote", s); // alice quote
  77.  
  78.     try {
  79.             ResourceBundle resources = ResourceBundle.getBundle("resources.Stylepad", 
  80.                                 Locale.getDefault());
  81.         s = styles.addStyle(null, null);
  82.         Icon alice = new ImageIcon(resources.getString("aliceGif"));
  83.         StyleConstants.setIcon(s, alice);
  84.         runAttr.put("alice", s); // alice
  85.  
  86.         s = styles.addStyle(null, null);
  87.         Icon caterpillar = new ImageIcon(resources.getString("caterpillarGif"));
  88.         StyleConstants.setIcon(s, caterpillar);
  89.         runAttr.put("caterpillar", s); // caterpillar
  90.  
  91.         s = styles.addStyle(null, null);
  92.         Icon hatter = new ImageIcon(resources.getString("hatterGif"));
  93.         StyleConstants.setIcon(s, hatter);
  94.         runAttr.put("hatter", s); // hatter
  95.  
  96.  
  97.     } catch (MissingResourceException mre) {
  98.       // can't display image
  99.     }
  100.  
  101.     Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
  102.  
  103.     Style heading = styles.addStyle("heading", def);
  104.     StyleConstants.setFontFamily(heading, "SansSerif");
  105.     StyleConstants.setBold(heading, true);
  106.     StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
  107.     StyleConstants.setSpaceAbove(heading, 10);
  108.     StyleConstants.setSpaceBelow(heading, 10);
  109.     StyleConstants.setFontSize(heading, 18);
  110.  
  111.     // Title
  112.     Style sty = styles.addStyle("title", heading);
  113.     StyleConstants.setFontSize(sty, 32);
  114.  
  115.     // edition
  116.     sty = styles.addStyle("edition", heading);
  117.     StyleConstants.setFontSize(sty, 16);
  118.  
  119.     // author
  120.     sty = styles.addStyle("author", heading);
  121.     StyleConstants.setItalic(sty, true);
  122.     StyleConstants.setSpaceBelow(sty, 25);
  123.  
  124.     // subtitle
  125.     sty = styles.addStyle("subtitle", heading);
  126.     StyleConstants.setSpaceBelow(sty, 35);
  127.  
  128.     // normal
  129.     sty = styles.addStyle("normal", def);
  130.     StyleConstants.setLeftIndent(sty, 10);
  131.     StyleConstants.setRightIndent(sty, 10);
  132.     StyleConstants.setFontFamily(sty, "SansSerif");
  133.     StyleConstants.setFontSize(sty, 14);
  134.     StyleConstants.setSpaceAbove(sty, 4);
  135.     StyleConstants.setSpaceBelow(sty, 4);
  136.   }
  137.  
  138.   DefaultStyledDocument doc;
  139.   StyleContext styles;
  140.   Hashtable runAttr;
  141.  
  142.   static class Paragraph {
  143.     Paragraph(String logical, Run[] data) {
  144.       this.logical = logical;
  145.       this.data = data;
  146.     }
  147.     String logical;
  148.     Run[] data;
  149.   }
  150.  
  151.   static class Run {
  152.     Run(String attr, String content) {
  153.       this.attr = attr;
  154.       this.content = content;
  155.     }
  156.     String attr;
  157.     String content;
  158.   }
  159.  
  160.   Paragraph[] data = new Paragraph[] {
  161.     new Paragraph("title", new Run[] {
  162.       new Run("none", "ALICE'S ADVENTURES IN WONDERLAND")
  163.     }),
  164.     new Paragraph("author", new Run[] {
  165.       new Run("none", "Lewis Carroll")
  166.     }),
  167.     new Paragraph("heading", new Run[] {
  168.       new Run("alice", " ")
  169.     }),
  170.     new Paragraph("edition", new Run[] {
  171.       new Run("none", "THE MILLENNIUM FULCRUM EDITION 3.0")
  172.     }),
  173.     new Paragraph("heading", new Run[] {
  174.       new Run("none", "CHAPTER V")
  175.     }),
  176.     new Paragraph("subtitle", new Run[] {
  177.       new Run("none", "Advice from a Caterpillar")
  178.     }),
  179.     new Paragraph("normal", new Run[] {
  180.       new Run("none", " "),
  181.     }),
  182.     new Paragraph("normal", new Run[] {
  183.       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.")
  184.     }),
  185.     new Paragraph("normal", new Run[] {
  186.       new Run("cquote", "Who are YOU?  "),
  187.       new Run("none", "said the Caterpillar.")
  188.     }),
  189.     new Paragraph("normal", new Run[] {
  190.       new Run("none", "This was not an encouraging opening for a conversation.  Alice replied, rather shyly, "),
  191.       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. "),
  192.     }),
  193.     new Paragraph("heading", new Run[] {
  194.       new Run("caterpillar", " ")
  195.     }),
  196.     new Paragraph("normal", new Run[] {
  197.       new Run("cquote", "What do you mean by that? "),
  198.       new Run("none", " said the Caterpillar sternly.  "),
  199.       new Run("cquote", "Explain yourself!"),
  200.     }),
  201.     new Paragraph("normal", new Run[] {
  202.       new Run("aquote", "I can't explain MYSELF, I'm afraid, sir"),
  203.       new Run("none", " said Alice, "),
  204.       new Run("aquote", "because I'm not myself, you see."),
  205.     }),
  206.     new Paragraph("normal", new Run[] {
  207.       new Run("cquote", "I don't see,"),
  208.       new Run("none", " said the Caterpillar."),
  209.     }),
  210.     new Paragraph("normal", new Run[] {
  211.       new Run("aquote", "I'm afraid I can't put it more clearly,  "),
  212.       new Run("none", "Alice replied very politely, "),
  213.       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."),
  214.     }),
  215.     new Paragraph("normal", new Run[] {
  216.       new Run("cquote", "It isn't,  "),
  217.       new Run("none", "said the Caterpillar.")
  218.     }),
  219.     new Paragraph("normal", new Run[] {
  220.       new Run("aquote", "Well, perhaps you haven't found it so yet,"),
  221.       new Run("none", " said Alice; "),
  222.       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?")
  223.     }),
  224.     new Paragraph("normal", new Run[] {
  225.       new Run("cquote", "Not a bit, "),
  226.       new Run("none", "said the Caterpillar.")
  227.     }),
  228.     new Paragraph("normal", new Run[] {
  229.       new Run("aquote", "Well, perhaps your feelings may be different,"),
  230.       new Run("none", " said Alice; "),
  231.       new Run("aquote", "all I know is, it would feel very queer to ME."),
  232.     }),
  233.     new Paragraph("normal", new Run[] {
  234.       new Run("cquote", "You!"),
  235.       new Run("none", " said the Caterpillar contemptuously.  "),
  236.       new Run("cquote", "Who are YOU?"),
  237.     }),
  238.     new Paragraph("normal", new Run[] {
  239.       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, "),
  240.       new Run("aquote", "I think, you ought to tell me who YOU are, first."),
  241.     }),
  242.     new Paragraph("normal", new Run[] {
  243.       new Run("cquote", "Why?  "),
  244.       new Run("none", "said the Caterpillar."),
  245.     }),
  246.     new Paragraph("heading", new Run[] {
  247.       new Run("hatter", " ")
  248.     }),
  249.     new Paragraph("normal", new Run[] {
  250.       new Run("none", " "),
  251.     }),
  252.     new Paragraph("normal", new Run[] {
  253.       new Run("none", " "),
  254.     }),
  255.     new Paragraph("normal", new Run[] {
  256.       new Run("none", " "),
  257.     })
  258.   };
  259.  
  260.  
  261. }
  262.