KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > DemoModule


1 /*
2  * Copyright (c) 2003 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  * @(#)DemoModule.java 1.14 03/01/23
38  */

39 package demo.swingset;
40 import swingwtx.swing.*;
41 import swingwtx.swing.event.*;
42 //import swingwtx.swing.text.*;
43
import swingwtx.swing.border.*;
44 //import swingwtx.swing.colorchooser.*;
45
import swingwtx.swing.filechooser.*;
46 import javax.accessibility.*;
47
48 import swingwt.awt.*;
49 import swingwt.awt.event.*;
50 import java.beans.*;
51 import java.util.*;
52 import java.io.*;
53 import java.applet.*;
54 import java.net.*;
55
56 /**
57  * A generic SwingSet2 demo module
58  *
59  * @version 1.14 01/23/03
60  * @author Jeff Dinkins
61  */

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