1
35
36
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
62 public class DemoModule extends JApplet {
63
64 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 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 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 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
249