home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Stylepad / src / HelloWorld.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  6.6 KB  |  213 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.  * @(#)HelloWorld.java    1.6 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 HelloWorld {
  56.  
  57.   HelloWorld(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
  105.             = ResourceBundle.getBundle("resources.Stylepad", 
  106.                                        Locale.getDefault());
  107.         s = styles.addStyle(null, null);
  108.         Icon alice = new ImageIcon(resources.getString("aliceGif"));    
  109.         StyleConstants.setIcon(s, alice);
  110.         runAttr.put("alice", s); // alice
  111.  
  112.         s = styles.addStyle(null, null);
  113.         Icon caterpillar = new ImageIcon(resources.getString("caterpillarGif"));    
  114.         StyleConstants.setIcon(s, caterpillar);
  115.         runAttr.put("caterpillar", s); // caterpillar
  116.  
  117.         s = styles.addStyle(null, null);
  118.         Icon hatter = new ImageIcon(resources.getString("hatterGif"));    
  119.         StyleConstants.setIcon(s, hatter);
  120.         runAttr.put("hatter", s); // hatter
  121.  
  122.         
  123.     } catch (MissingResourceException mre) {
  124.       // can't display image
  125.     }
  126.  
  127.     Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
  128.  
  129.     Style heading = styles.addStyle("heading", def);
  130.     //StyleConstants.setFontFamily(heading, "SansSerif");
  131.     StyleConstants.setBold(heading, true);
  132.     StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
  133.     StyleConstants.setSpaceAbove(heading, 10);
  134.     StyleConstants.setSpaceBelow(heading, 10);
  135.     StyleConstants.setFontSize(heading, 18);
  136.  
  137.     // Title 
  138.     Style sty = styles.addStyle("title", heading);
  139.     StyleConstants.setFontSize(sty, 32);
  140.  
  141.     // edition
  142.     sty = styles.addStyle("edition", heading);
  143.     StyleConstants.setFontSize(sty, 16);
  144.  
  145.     // author
  146.     sty = styles.addStyle("author", heading);
  147.     StyleConstants.setItalic(sty, true);
  148.     StyleConstants.setSpaceBelow(sty, 25);
  149.  
  150.     // subtitle
  151.     sty = styles.addStyle("subtitle", heading);
  152.     StyleConstants.setSpaceBelow(sty, 35);
  153.  
  154.     // normal 
  155.     sty = styles.addStyle("normal", def);
  156.     StyleConstants.setLeftIndent(sty, 10);
  157.     StyleConstants.setRightIndent(sty, 10);
  158.     //StyleConstants.setFontFamily(sty, "SansSerif");
  159.     StyleConstants.setFontSize(sty, 14);
  160.     StyleConstants.setSpaceAbove(sty, 4);
  161.     StyleConstants.setSpaceBelow(sty, 4);
  162.   }
  163.  
  164.   DefaultStyledDocument doc; 
  165.   StyleContext styles;
  166.   Hashtable runAttr;
  167.   
  168.   static class Paragraph {
  169.     Paragraph(String logical, Run[] data) {
  170.       this.logical = logical;
  171.       this.data = data;
  172.     }
  173.     String logical;
  174.     Run[] data;
  175.   }
  176.  
  177.   static class Run {
  178.     Run(String attr, String content) {
  179.       this.attr = attr;
  180.       this.content = content;
  181.     }
  182.     String attr;
  183.     String content;
  184.   }
  185.  
  186.   Paragraph[] data = new Paragraph[] {
  187.     new Paragraph("title", new Run[] {
  188.           new Run("none", "Hello from Cupertino")
  189.     }),
  190.     new Paragraph("title", new Run[] {
  191.       new Run("none", "\u53F0\u5317\u554F\u5019\u60A8\u0021")
  192.     }),
  193.     new Paragraph("title", new Run[] {
  194.       new Run("none", "\u0391\u03B8\u03B7\u03BD\u03B1\u03B9\u0020"   // Greek
  195.            + "\u03B1\u03C3\u03C0\u03B1\u03B6\u03BF\u03BD"
  196.            + "\u03C4\u03B1\u03B9\u0020\u03C5\u03BC\u03B1"
  197.            + "\u03C2\u0021")
  198.     }),
  199.     new Paragraph("title", new Run[] {
  200.       new Run("none", "\u6771\u4eac\u304b\u3089\u4eca\u65e5\u306f")
  201.     }),
  202.     new Paragraph("title", new Run[] {
  203.       new Run("none", "\u05e9\u05dc\u05d5\u05dd \u05de\u05d9\u05e8\u05d5"
  204.               + "\u05e9\u05dc\u05d9\u05dd")
  205.     }),
  206.     new Paragraph("title", new Run[] {
  207.       new Run("none", "\u0633\u0644\u0627\u0645")
  208.     }),
  209.   };
  210.  
  211.  
  212. }
  213.