KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > InternalFrameDemo


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  * @(#)InternalFrameDemo.java 1.10 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.border.*;
45 import swingwtx.swing.colorchooser.*;
46 import swingwtx.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import swingwt.awt.*;
50 import swingwt.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  * Internal Frames Demo
59  *
60  * @version 1.10 01/23/03
61  * @author Jeff Dinkins
62  */

63 public class InternalFrameDemo extends DemoModule {
64     int windowCount = 0;
65     JDesktopPane desktop = null;
66
67     ImageIcon icon1, icon2, icon3, icon4;
68     ImageIcon smIcon1, smIcon2, smIcon3, smIcon4;
69
70     public Integer JavaDoc FIRST_FRAME_LAYER = new Integer JavaDoc(1);
71     public Integer JavaDoc DEMO_FRAME_LAYER = new Integer JavaDoc(2);
72     public Integer JavaDoc PALETTE_LAYER = new Integer JavaDoc(3);
73
74     public int FRAME0_X = 15;
75     public int FRAME0_Y = 280;
76
77     public int FRAME0_WIDTH = 320;
78     public int FRAME0_HEIGHT = 230;
79
80     public int FRAME_WIDTH = 225;
81     public int FRAME_HEIGHT = 150;
82
83     public int PALETTE_X = 375;
84     public int PALETTE_Y = 20;
85
86     public int PALETTE_WIDTH = 260;
87     public int PALETTE_HEIGHT = 230;
88
89     JCheckBox windowResizable = null;
90     JCheckBox windowClosable = null;
91     JCheckBox windowIconifiable = null;
92     JCheckBox windowMaximizable = null;
93
94     JTextField windowTitleField = null;
95     JLabel windowTitleLabel = null;
96
97
98     /**
99      * main method allows us to run as a standalone demo.
100      */

101     public static void main(String JavaDoc[] args) {
102     InternalFrameDemo demo = new InternalFrameDemo(null);
103     demo.mainImpl();
104     }
105
106     /**
107      * InternalFrameDemo Constructor
108      */

109     public InternalFrameDemo(SwingSet2 swingset) {
110     super(swingset, "InternalFrameDemo", "toolbar/JDesktop.gif");
111
112     // preload all the icons we need for this demo
113
icon1 = createImageIcon("ImageClub/misc/fish.gif", getString("InternalFrameDemo.fish"));
114     icon2 = createImageIcon("ImageClub/misc/moon.gif", getString("InternalFrameDemo.moon"));
115     icon3 = createImageIcon("ImageClub/misc/sun.gif", getString("InternalFrameDemo.sun"));
116     icon4 = createImageIcon("ImageClub/misc/cab.gif", getString("InternalFrameDemo.cab"));
117
118     smIcon1 = createImageIcon("ImageClub/misc/fish_small.gif", getString("InternalFrameDemo.fish"));
119     smIcon2 = createImageIcon("ImageClub/misc/moon_small.gif", getString("InternalFrameDemo.moon"));
120     smIcon3 = createImageIcon("ImageClub/misc/sun_small.gif", getString("InternalFrameDemo.sun"));
121     smIcon4 = createImageIcon("ImageClub/misc/cab_small.gif", getString("InternalFrameDemo.cab"));
122
123     // Create the desktop pane
124
desktop = new JDesktopPane();
125     getDemoPanel().add(desktop, BorderLayout.CENTER);
126
127     // Create the "frame maker" palette
128
final JInternalFrame p = createInternalFramePalette();
129
130     // Create an initial internal frame to show
131
JInternalFrame frame1 = createInternalFrame(icon1, FIRST_FRAME_LAYER, 1, 1);
132     frame1.setBounds(FRAME0_X, FRAME0_Y, FRAME0_WIDTH, FRAME0_HEIGHT);
133
134     // Create four more starter windows
135
createInternalFrame(icon1, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
136     createInternalFrame(icon3, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
137     createInternalFrame(icon4, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
138     createInternalFrame(icon2, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
139
140         try {
141             p.setSelected(true);
142         }
143         catch (Exception JavaDoc e) {}
144         
145     }
146
147
148
149     /**
150      * Create an internal frame and add a scrollable imageicon to it
151      */

152     public JInternalFrame createInternalFrame(ImageIcon icon, Integer JavaDoc layer, int width, int height) {
153     JInternalFrame jif = new JInternalFrame();
154
155     if(!windowTitleField.getText().equals(getString("InternalFrameDemo.frame_label"))) {
156         jif.setTitle(windowTitleField.getText() + " ");
157     } else {
158         jif = new JInternalFrame(getString("InternalFrameDemo.frame_label") + " " + windowCount + " ");
159     }
160
161     // set properties
162
jif.setClosable(windowClosable.isSelected());
163     jif.setMaximizable(windowMaximizable.isSelected());
164     jif.setIconifiable(windowIconifiable.isSelected());
165     jif.setResizable(windowResizable.isSelected());
166
167     jif.setBounds(20*(windowCount%10), 20*(windowCount%10), width, height);
168         jif.setLayout(new BorderLayout());
169     jif.getContentPane().add(new ImageScroller(this, icon, 0, windowCount), BorderLayout.CENTER);
170
171     windowCount++;
172     
173     desktop.add(jif, layer);
174
175     // Set this internal frame to be selected
176
try {
177             jif.setSelected(true);
178         }
179         catch (PropertyVetoException e) {}
180
181     jif.show();
182
183     return jif;
184     }
185
186     public JInternalFrame createInternalFramePalette() {
187     JInternalFrame palette = new JInternalFrame(
188         getString("InternalFrameDemo.palette_label")
189     );
190     palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
191     palette.getContentPane().setLayout(new BorderLayout());
192     palette.setBounds(PALETTE_X, PALETTE_Y, PALETTE_WIDTH, PALETTE_HEIGHT);
193     palette.setResizable(true);
194     palette.setIconifiable(true);
195     desktop.add(palette, PALETTE_LAYER);
196
197     // *************************************
198
// * Create create frame maker buttons *
199
// *************************************
200
JButton b1 = new JButton(smIcon1);
201     JButton b2 = new JButton(smIcon2);
202     JButton b3 = new JButton(smIcon3);
203     JButton b4 = new JButton(smIcon4);
204
205     // add frame maker actions
206
b1.addActionListener(new ShowFrameAction(this, icon1));
207     b2.addActionListener(new ShowFrameAction(this, icon2));
208     b3.addActionListener(new ShowFrameAction(this, icon3));
209     b4.addActionListener(new ShowFrameAction(this, icon4));
210
211     // add frame maker buttons to panel
212
JPanel p = new JPanel();
213     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
214
215     JPanel buttons1 = new JPanel();
216     buttons1.setLayout(new BoxLayout(buttons1, BoxLayout.X_AXIS));
217
218     JPanel buttons2 = new JPanel();
219     buttons2.setLayout(new BoxLayout(buttons2, BoxLayout.X_AXIS));
220
221     buttons1.add(b1);
222     buttons1.add(Box.createRigidArea(HGAP15));
223     buttons1.add(b2);
224
225     buttons2.add(b3);
226     buttons2.add(Box.createRigidArea(HGAP15));
227     buttons2.add(b4);
228
229     p.add(Box.createRigidArea(VGAP10));
230     p.add(buttons1);
231     p.add(Box.createRigidArea(VGAP15));
232     p.add(buttons2);
233     p.add(Box.createRigidArea(VGAP10));
234
235     palette.getContentPane().add(p, BorderLayout.NORTH);
236
237     // ************************************
238
// * Create frame property checkboxes *
239
// ************************************
240
p = new JPanel() {
241         Insets insets = new Insets(10,15,10,5);
242         public Insets getInsets() {
243         return insets;
244         }
245     };
246     p.setLayout(new GridLayout(1,2));
247
248
249         Box box = new Box(BoxLayout.Y_AXIS);
250     windowResizable = new JCheckBox(getString("InternalFrameDemo.resizable_label"), true);
251     windowIconifiable = new JCheckBox(getString("InternalFrameDemo.iconifiable_label"), true);
252         
253     //box.add(windowResizable);
254
//box.add(windowIconifiable);
255
p.add(box);
256
257         box = new Box(BoxLayout.Y_AXIS);
258     windowClosable = new JCheckBox(getString("InternalFrameDemo.closable_label"), true);
259     windowMaximizable = new JCheckBox(getString("InternalFrameDemo.maximizable_label"), true);
260
261     //box.add(windowClosable);
262
//box.add(windowMaximizable);
263
p.add(box);
264
265     palette.getContentPane().add(p, BorderLayout.CENTER);
266
267
268     // ************************************
269
// * Create Frame title textfield *
270
// ************************************
271
p = new JPanel() {
272         Insets insets = new Insets(0,0,10,0);
273         public Insets getInsets() {
274         return insets;
275         }
276     };
277
278     windowTitleField = new JTextField(getString("InternalFrameDemo.frame_label"));
279         windowTitleField.setPreferredSize(new Dimension(200, 25));
280     windowTitleLabel = new JLabel(getString("InternalFrameDemo.title_text_field_label"));
281
282     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
283     p.add(Box.createRigidArea(HGAP5));
284     p.add(windowTitleLabel, BorderLayout.WEST);
285     p.add(Box.createRigidArea(HGAP5));
286     p.add(windowTitleField, BorderLayout.CENTER);
287     p.add(Box.createRigidArea(HGAP5));
288
289     palette.getContentPane().add(p, BorderLayout.SOUTH);
290
291     palette.show();
292
293     return palette;
294     }
295
296
297     class ShowFrameAction extends AbstractAction {
298     InternalFrameDemo demo;
299     ImageIcon icon;
300     
301     
302     public ShowFrameAction(InternalFrameDemo demo, ImageIcon icon) {
303         this.demo = demo;
304         this.icon = icon;
305     }
306     
307     public void actionPerformed(ActionEvent e) {
308         demo.createInternalFrame(icon,
309                      getDemoFrameLayer(),
310                      getFrameWidth(),
311                      getFrameHeight()
312         );
313     }
314     }
315
316     public int getFrameWidth() {
317     return FRAME_WIDTH;
318     }
319
320     public int getFrameHeight() {
321     return FRAME_HEIGHT;
322     }
323
324     public Integer JavaDoc getDemoFrameLayer() {
325     return DEMO_FRAME_LAYER;
326     }
327     
328     class ImageScroller extends JScrollPane {
329     
330     public ImageScroller(InternalFrameDemo demo, ImageIcon icon, int layer, int count) {
331         super();
332         JPanel p = new JPanel();
333         p.setBackground(Color.white);
334         p.setLayout(new BorderLayout() );
335         
336         p.add(new JLabel(icon), BorderLayout.CENTER);
337         setLayout(new BorderLayout());
338             add(p);
339     }
340     
341     public Dimension getMinimumSize() {
342         return new Dimension(25, 25);
343     }
344     
345     }
346
347     
348 }
349
Popular Tags