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 | |
40 | import javax.swing.*; |
41 | import javax.swing.event.*; |
42 | import javax.swing.text.*; |
43 | import javax.swing.border.*; |
44 | import javax.swing.colorchooser.*; |
45 | import javax.swing.filechooser.*; |
46 | import javax.accessibility.*; |
47 | |
48 | import java.awt.*; |
49 | import java.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 JApplet { |
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 resourceName = null; |
96 | private String iconPath = null; |
97 | private String 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 resourceName, String 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 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 getString(String key) { |
131 | String 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 key) { |
148 | return (getString(key)).charAt(0); |
149 | } |
150 | |
151 | public ImageIcon createImageIcon(String filename, String description) { |
152 | if(getSwingSet2() != null) { |
153 | return getSwingSet2().createImageIcon(filename, description); |
154 | } else { |
155 | String path = "/resources/images/" + filename; |
156 | return new ImageIcon(getClass().getResource(path), description); |
157 | } |
158 | } |
159 | |
160 | |
161 | public String getSourceCode() { |
162 | return sourceCode; |
163 | } |
164 | |
165 | public void loadSourceCode() { |
166 | if(getResourceName() != null) { |
167 | String filename = "src/" + getResourceName() + ".java"; |
168 | sourceCode = new String("<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 | |
175 | try { |
176 | url = getClass().getResource(filename); |
177 | is = url.openStream(); |
178 | isr = new InputStreamReader(is); |
179 | BufferedReader reader = new BufferedReader(isr); |
180 | |
181 | // Read one line at a time, htmlize using super-spiffy |
182 | // html java code formating utility from www.CoolServlets.com |
183 | String line = reader.readLine(); |
184 | while(line != null) { |
185 | sourceCode += cv.syntaxHighlight(line) + " \n "; |
186 | line = reader.readLine(); |
187 | } |
188 | sourceCode += new String("</pre></body></html>"); |
189 | } catch (Exception ex) { |
190 | sourceCode = "Could not load file: " + filename; |
191 | } |
192 | } |
193 | } |
194 | |
195 | public String getName() { |
196 | return getString(getResourceName() + ".name"); |
197 | }; |
198 | |
199 | public Icon getIcon() { |
200 | return createImageIcon(iconPath, getResourceName() + ".name"); |
201 | }; |
202 | |
203 | public String getToolTip() { |
204 | return getString(getResourceName() + ".tooltip"); |
205 | }; |
206 | |
207 | public void mainImpl() { |
208 | JFrame frame = new JFrame(getName()); |
209 | frame.getContentPane().setLayout(new BorderLayout()); |
210 | frame.getContentPane().add(getDemoPanel(), BorderLayout.CENTER); |
211 | getDemoPanel().setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT)); |
212 | frame.pack(); |
213 | frame.show(); |
214 | } |
215 | |
216 | public JPanel createHorizontalPanel(boolean threeD) { |
217 | JPanel p = new JPanel(); |
218 | p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
219 | p.setAlignmentY(TOP_ALIGNMENT); |
220 | p.setAlignmentX(LEFT_ALIGNMENT); |
221 | if(threeD) { |
222 | p.setBorder(loweredBorder); |
223 | } |
224 | return p; |
225 | } |
226 | |
227 | public JPanel createVerticalPanel(boolean threeD) { |
228 | JPanel p = new JPanel(); |
229 | p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
230 | p.setAlignmentY(TOP_ALIGNMENT); |
231 | p.setAlignmentX(LEFT_ALIGNMENT); |
232 | if(threeD) { |
233 | p.setBorder(loweredBorder); |
234 | } |
235 | return p; |
236 | } |
237 | |
238 | public static void main(String[] args) { |
239 | DemoModule demo = new DemoModule(null); |
240 | demo.mainImpl(); |
241 | } |
242 | |
243 | public void init() { |
244 | getContentPane().setLayout(new BorderLayout()); |
245 | getContentPane().add(getDemoPanel(), BorderLayout.CENTER); |
246 | } |
247 | } |
248 | |