KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DemoModule


1 /*
2  * @(#)DemoModule.java 1.23 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  * @(#)DemoModule.java 1.23 05/11/17
39  */

40
41 import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.text.*;
44 import javax.swing.border.*;
45 import javax.swing.colorchooser.*;
46 import javax.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import java.awt.*;
50 import java.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.applet.*;
55 import java.net.*;
56
57 /**
58  * A generic SwingSet2 demo module
59  *
60  * @version 1.23 11/17/05
61  * @author Jeff Dinkins
62  */

63 public class DemoModule extends JApplet {
64
65     // The preferred size of the demo
66
private int PREFERRED_WIDTH = 680;
67     private int PREFERRED_HEIGHT = 600;
68
69     Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED),
70                           new EmptyBorder(5,5,5,5));
71
72     // Premade convenience dimensions, for use wherever you need 'em.
73
public static Dimension HGAP2 = new Dimension(2,1);
74     public static Dimension VGAP2 = new Dimension(1,2);
75
76     public static Dimension HGAP5 = new Dimension(5,1);
77     public static Dimension VGAP5 = new Dimension(1,5);
78     
79     public static Dimension HGAP10 = new Dimension(10,1);
80     public static Dimension VGAP10 = new Dimension(1,10);
81
82     public static Dimension HGAP15 = new Dimension(15,1);
83     public static Dimension VGAP15 = new Dimension(1,15);
84     
85     public static Dimension HGAP20 = new Dimension(20,1);
86     public static Dimension VGAP20 = new Dimension(1,20);
87
88     public static Dimension HGAP25 = new Dimension(25,1);
89     public static Dimension VGAP25 = new Dimension(1,25);
90
91     public static Dimension HGAP30 = new Dimension(30,1);
92     public static Dimension VGAP30 = new Dimension(1,30);
93     
94     private SwingSet2 swingset = null;
95     private JPanel panel = null;
96     private String JavaDoc resourceName = null;
97     private String JavaDoc iconPath = null;
98     private String JavaDoc sourceCode = null;
99
100     // Resource bundle for internationalized and accessible text
101
private ResourceBundle bundle = null;
102
103     public DemoModule(SwingSet2 swingset) {
104     this(swingset, null, null);
105     }
106
107     public DemoModule(SwingSet2 swingset, String JavaDoc resourceName, String JavaDoc iconPath) {
108         UIManager.put("swing.boldMetal", Boolean.FALSE);
109     panel = new JPanel();
110     panel.setLayout(new BorderLayout());
111
112     this.resourceName = resourceName;
113     this.iconPath = iconPath;
114     this.swingset = swingset;
115
116     loadSourceCode();
117     }
118
119     public String JavaDoc getResourceName() {
120     return resourceName;
121     }
122
123     public JPanel getDemoPanel() {
124     return panel;
125     }
126
127     public SwingSet2 getSwingSet2() {
128     return swingset;
129     }
130
131
132     public String JavaDoc getString(String JavaDoc key) {
133     String JavaDoc value = "nada";
134     if(bundle == null) {
135         if(getSwingSet2() != null) {
136         bundle = getSwingSet2().getResourceBundle();
137         } else {
138         bundle = ResourceBundle.getBundle("resources.swingset");
139         }
140     }
141     try {
142         value = bundle.getString(key);
143     } catch (MissingResourceException e) {
144         System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
145     }
146     return value;
147     }
148
149     public char getMnemonic(String JavaDoc key) {
150     return (getString(key)).charAt(0);
151     }
152
153     public ImageIcon createImageIcon(String JavaDoc filename, String JavaDoc description) {
154     if(getSwingSet2() != null) {
155         return getSwingSet2().createImageIcon(filename, description);
156     } else {
157         String JavaDoc path = "/resources/images/" + filename;
158         return new ImageIcon(getClass().getResource(path), description);
159     }
160     }
161     
162
163     public String JavaDoc getSourceCode() {
164     return sourceCode;
165     }
166
167     public void loadSourceCode() {
168     if(getResourceName() != null) {
169         String JavaDoc filename = getResourceName() + ".java";
170         sourceCode = new String JavaDoc("<html><body bgcolor=\"#ffffff\"><pre>");
171         InputStream is;
172         InputStreamReader isr;
173         CodeViewer cv = new CodeViewer();
174         URL url;
175         
176         try {
177         url = getClass().getResource(filename);
178         is = url.openStream();
179         isr = new InputStreamReader(is, "UTF-8");
180         BufferedReader reader = new BufferedReader(isr);
181         
182         // Read one line at a time, htmlize using super-spiffy
183
// html java code formating utility from www.CoolServlets.com
184
String JavaDoc line = reader.readLine();
185         while(line != null) {
186             sourceCode += cv.syntaxHighlight(line) + " \n ";
187             line = reader.readLine();
188         }
189         sourceCode += new String JavaDoc("</pre></body></html>");
190             } catch (Exception JavaDoc ex) {
191                 sourceCode = "Could not load file: " + filename;
192             }
193     }
194     }
195
196     public String JavaDoc getName() {
197     return getString(getResourceName() + ".name");
198     };
199
200     public Icon getIcon() {
201     return createImageIcon(iconPath, getResourceName() + ".name");
202     };
203
204     public String JavaDoc getToolTip() {
205     return getString(getResourceName() + ".tooltip");
206     };
207
208     public void mainImpl() {
209     JFrame frame = new JFrame(getName());
210         frame.getContentPane().setLayout(new BorderLayout());
211     frame.getContentPane().add(getDemoPanel(), BorderLayout.CENTER);
212     getDemoPanel().setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
213     frame.pack();
214     frame.show();
215     }
216
217     public JPanel createHorizontalPanel(boolean threeD) {
218         JPanel p = new JPanel();
219         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
220         p.setAlignmentY(TOP_ALIGNMENT);
221         p.setAlignmentX(LEFT_ALIGNMENT);
222         if(threeD) {
223             p.setBorder(loweredBorder);
224         }
225         return p;
226     }
227     
228     public JPanel createVerticalPanel(boolean threeD) {
229         JPanel p = new JPanel();
230         p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
231         p.setAlignmentY(TOP_ALIGNMENT);
232         p.setAlignmentX(LEFT_ALIGNMENT);
233         if(threeD) {
234             p.setBorder(loweredBorder);
235         }
236         return p;
237     }
238
239     public static void main(String JavaDoc[] args) {
240     DemoModule demo = new DemoModule(null);
241     demo.mainImpl();
242     }
243
244     public void init() {
245         getContentPane().setLayout(new BorderLayout());
246         getContentPane().add(getDemoPanel(), BorderLayout.CENTER);
247     }
248     
249     void updateDragEnabled(boolean dragEnabled) {}
250 }
251
252
Popular Tags