InternalFrameDemo.java  
1   /*
2    * @(#)InternalFrameDemo.java   1.14 04/07/26
3    * 
4    * Copyright (c) 2004 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   * @(#)InternalFrameDemo.java   1.14 04/07/26
39   */
40  
41  
42  import javax.swing.*;
43  import javax.swing.event.*;
44  import javax.swing.text.*;
45  import javax.swing.border.*;
46  import javax.swing.colorchooser.*;
47  import javax.swing.filechooser.*;
48  import javax.accessibility.*;
49  
50  import java.awt.*;
51  import java.awt.event.*;
52  import java.beans.*;
53  import java.util.*;
54  import java.io.*;
55  import java.applet.*;
56  import java.net.*;
57  
58  /**
59   * Internal Frames Demo
60   *
61   * @version 1.14 07/26/04
62   * @author Jeff Dinkins
63   */
64  public class InternalFrameDemo extends DemoModule {
65      int windowCount = 0;
66      JDesktopPane desktop = null;
67  
68      ImageIcon icon1, icon2, icon3, icon4;
69      ImageIcon smIcon1, smIcon2, smIcon3, smIcon4;
70  
71      public Integer FIRST_FRAME_LAYER  = new Integer(1);
72      public Integer DEMO_FRAME_LAYER   = new Integer(2);
73      public Integer PALETTE_LAYER     = new Integer(3);
74  
75      public int FRAME0_X        = 15;
76      public int FRAME0_Y        = 280;
77  
78      public int FRAME0_WIDTH    = 320;
79      public int FRAME0_HEIGHT   = 230;
80  
81      public int FRAME_WIDTH     = 225;
82      public int FRAME_HEIGHT    = 150;
83  
84      public int PALETTE_X      = 375;
85      public int PALETTE_Y      = 20;
86  
87      public int PALETTE_WIDTH  = 260;
88      public int PALETTE_HEIGHT = 260;
89  
90      JCheckBox windowResizable   = null;
91      JCheckBox windowClosable    = null;
92      JCheckBox windowIconifiable = null;
93      JCheckBox windowMaximizable = null;
94  
95      JTextField windowTitleField = null;
96      JLabel windowTitleLabel = null;
97  
98  
99      /**
100      * main method allows us to run as a standalone demo.
101      */
102     public static void main(String[] args) {
103     InternalFrameDemo demo = new InternalFrameDemo(null);
104     demo.mainImpl();
105     }
106 
107     /**
108      * InternalFrameDemo Constructor
109      */
110     public InternalFrameDemo(SwingSet2 swingset) {
111     super(swingset, "InternalFrameDemo", "toolbar/JDesktop.gif");
112 
113     // preload all the icons we need for this demo
114     icon1 = createImageIcon("ImageClub/misc/fish.gif", getString("InternalFrameDemo.fish"));
115     icon2 = createImageIcon("ImageClub/misc/moon.gif", getString("InternalFrameDemo.moon"));
116     icon3 = createImageIcon("ImageClub/misc/sun.gif",  getString("InternalFrameDemo.sun"));
117     icon4 = createImageIcon("ImageClub/misc/cab.gif",  getString("InternalFrameDemo.cab"));
118 
119     smIcon1 = createImageIcon("ImageClub/misc/fish_small.gif", getString("InternalFrameDemo.fish"));
120     smIcon2 = createImageIcon("ImageClub/misc/moon_small.gif", getString("InternalFrameDemo.moon"));
121     smIcon3 = createImageIcon("ImageClub/misc/sun_small.gif",  getString("InternalFrameDemo.sun"));
122     smIcon4 = createImageIcon("ImageClub/misc/cab_small.gif",  getString("InternalFrameDemo.cab"));
123 
124     // Create the desktop pane
125     desktop = new JDesktopPane();
126     getDemoPanel().add(desktop, BorderLayout.CENTER);
127 
128     // Create the "frame maker" palette
129     createInternalFramePalette();
130 
131     // Create an initial internal frame to show
132     JInternalFrame frame1 = createInternalFrame(icon1, FIRST_FRAME_LAYER, 1, 1);
133     frame1.setBounds(FRAME0_X, FRAME0_Y, FRAME0_WIDTH, FRAME0_HEIGHT);
134 
135     // Create four more starter windows
136     createInternalFrame(icon1, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
137     createInternalFrame(icon3, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
138     createInternalFrame(icon4, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
139     createInternalFrame(icon2, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
140     }
141 
142 
143 
144     /**
145      * Create an internal frame and add a scrollable imageicon to it
146      */
147     public JInternalFrame createInternalFrame(Icon icon, Integer layer, int width, int height) {
148     JInternalFrame jif = new JInternalFrame();
149 
150     if(!windowTitleField.getText().equals(getString("InternalFrameDemo.frame_label"))) {
151         jif.setTitle(windowTitleField.getText() + "  ");
152     } else {
153         jif = new JInternalFrame(getString("InternalFrameDemo.frame_label") + " " + windowCount + "  ");
154     }
155 
156     // set properties
157     jif.setClosable(windowClosable.isSelected());
158     jif.setMaximizable(windowMaximizable.isSelected());
159     jif.setIconifiable(windowIconifiable.isSelected());
160     jif.setResizable(windowResizable.isSelected());
161 
162     jif.setBounds(20*(windowCount%10), 20*(windowCount%10), width, height);
163     jif.setContentPane(new ImageScroller(this, icon, 0, windowCount));
164 
165     windowCount++;
166     
167     desktop.add(jif, layer);  
168 
169     // Set this internal frame to be selected
170 
171     try {
172         jif.setSelected(true);
173     } catch (java.beans.PropertyVetoException e2) {
174     }
175 
176     jif.show();
177 
178     return jif;
179     }
180 
181     public JInternalFrame createInternalFramePalette() {
182     JInternalFrame palette = new JInternalFrame(
183         getString("InternalFrameDemo.palette_label")
184     );
185     palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
186     palette.getContentPane().setLayout(new BorderLayout());
187     palette.setBounds(PALETTE_X, PALETTE_Y, PALETTE_WIDTH, PALETTE_HEIGHT);
188     palette.setResizable(true);
189     palette.setIconifiable(true);
190     desktop.add(palette, PALETTE_LAYER);
191 
192     // *************************************
193     // * Create create frame maker buttons *
194     // *************************************
195     JButton b1 = new JButton(smIcon1);
196     JButton b2 = new JButton(smIcon2);
197     JButton b3 = new JButton(smIcon3);
198     JButton b4 = new JButton(smIcon4);
199 
200     // add frame maker actions
201     b1.addActionListener(new ShowFrameAction(this, icon1));
202     b2.addActionListener(new ShowFrameAction(this, icon2));
203     b3.addActionListener(new ShowFrameAction(this, icon3));
204     b4.addActionListener(new ShowFrameAction(this, icon4));
205 
206     // add frame maker buttons to panel
207     JPanel p = new JPanel();
208     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
209 
210     JPanel buttons1 = new JPanel();
211     buttons1.setLayout(new BoxLayout(buttons1, BoxLayout.X_AXIS));
212 
213     JPanel buttons2 = new JPanel();
214     buttons2.setLayout(new BoxLayout(buttons2, BoxLayout.X_AXIS));
215 
216     buttons1.add(b1);
217     buttons1.add(Box.createRigidArea(HGAP15));
218     buttons1.add(b2);
219 
220     buttons2.add(b3);
221     buttons2.add(Box.createRigidArea(HGAP15));
222     buttons2.add(b4);
223 
224     p.add(Box.createRigidArea(VGAP10));
225     p.add(buttons1);
226     p.add(Box.createRigidArea(VGAP15));
227     p.add(buttons2);
228     p.add(Box.createRigidArea(VGAP10));
229 
230     palette.getContentPane().add(p, BorderLayout.NORTH);
231 
232     // ************************************
233     // * Create frame property checkboxes *
234     // ************************************
235     p = new JPanel() {
236         Insets insets = new Insets(10,15,10,5);
237         public Insets getInsets() {
238         return insets;
239         }
240     };
241     p.setLayout(new GridLayout(1,2));
242 
243 
244         Box box = new Box(BoxLayout.Y_AXIS);
245     windowResizable   = new JCheckBox(getString("InternalFrameDemo.resizable_label"), true);
246     windowIconifiable = new JCheckBox(getString("InternalFrameDemo.iconifiable_label"), true);
247 
248         box.add(Box.createGlue());
249     box.add(windowResizable);
250     box.add(windowIconifiable);
251         box.add(Box.createGlue());
252         p.add(box);
253 
254         box = new Box(BoxLayout.Y_AXIS);
255     windowClosable    = new JCheckBox(getString("InternalFrameDemo.closable_label"), true);
256     windowMaximizable = new JCheckBox(getString("InternalFrameDemo.maximizable_label"), true);
257 
258         box.add(Box.createGlue());
259     box.add(windowClosable);
260     box.add(windowMaximizable);
261         box.add(Box.createGlue());
262         p.add(box);
263 
264     palette.getContentPane().add(p, BorderLayout.CENTER);
265 
266 
267     // ************************************
268     // *   Create Frame title textfield   *
269     // ************************************
270     p = new JPanel() {
271         Insets insets = new Insets(0,0,10,0);
272         public Insets getInsets() {
273         return insets;
274         }
275     };
276 
277     windowTitleField = new JTextField(getString("InternalFrameDemo.frame_label"));
278     windowTitleLabel = new JLabel(getString("InternalFrameDemo.title_text_field_label"));
279 
280     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
281     p.add(Box.createRigidArea(HGAP5));
282     p.add(windowTitleLabel, BorderLayout.WEST);
283     p.add(Box.createRigidArea(HGAP5));
284     p.add(windowTitleField, BorderLayout.CENTER);
285     p.add(Box.createRigidArea(HGAP5));
286 
287     palette.getContentPane().add(p, BorderLayout.SOUTH);
288 
289     palette.show();
290 
291     return palette;
292     }
293 
294 
295     class ShowFrameAction extends AbstractAction {
296     InternalFrameDemo demo;
297     Icon icon;
298     
299     
300     public ShowFrameAction(InternalFrameDemo demo, Icon icon) {
301         this.demo = demo;
302         this.icon = icon;
303     }
304     
305     public void actionPerformed(ActionEvent e) {
306         demo.createInternalFrame(icon,
307                      getDemoFrameLayer(),
308                      getFrameWidth(),
309                      getFrameHeight()
310         );
311     }
312     }
313 
314     public int getFrameWidth() {
315     return FRAME_WIDTH;
316     }
317 
318     public int getFrameHeight() {
319     return FRAME_HEIGHT;
320     }
321 
322     public Integer getDemoFrameLayer() {
323     return DEMO_FRAME_LAYER;
324     }
325     
326     class ImageScroller extends JScrollPane {
327     
328     public ImageScroller(InternalFrameDemo demo, Icon icon, int layer, int count) {
329         super();
330         JPanel p = new JPanel();
331         p.setBackground(Color.white);
332         p.setLayout(new BorderLayout() );
333         
334         p.add(new JLabel(icon), BorderLayout.CENTER);
335         
336         getViewport().add(p);
337             getHorizontalScrollBar().setUnitIncrement(10);
338             getVerticalScrollBar().setUnitIncrement(10);
339     }
340     
341     public Dimension getMinimumSize() {
342         return new Dimension(25, 25);
343     }
344     
345     }
346     
347     void updateDragEnabled(boolean dragEnabled) {
348         windowTitleField.setDragEnabled(dragEnabled);
349     }
350     
351 }
352