1 /*
2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package com.sun.swingset3.demos.internalframe;
25 
26 import java.awt.BorderLayout;
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.GridLayout;
30 import java.awt.event.ActionEvent;
31 
32 import javax.swing.AbstractAction;
33 import javax.swing.Box;
34 import javax.swing.BoxLayout;
35 import javax.swing.Icon;
36 import javax.swing.ImageIcon;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JDesktopPane;
40 import javax.swing.JFrame;
41 import javax.swing.JInternalFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JScrollPane;
45 import javax.swing.JTextField;
46 import javax.swing.border.EmptyBorder;
47 
48 import com.sun.swingset3.DemoProperties;
49 import com.sun.swingset3.demos.ResourceManager;
50 
51 
52 /**
53  * Internal Frames Demo
54  *
55  * @version 1.16 11/17/05
56  * @author Jeff Dinkins
57  */
58 @DemoProperties(
59         value = "JInternalFrame Demo",
60         category = "Containers",
61         description = "Demonstrates JInternalFrame, a frame which can be embedded within another container to" +
62                 "implement an MDI style interface.",
63         sourceFiles = {
64                 "com/sun/swingset3/demos/internalframe/InternalFrameDemo.java",
65                 "com/sun/swingset3/demos/ResourceManager.java",
66                 "com/sun/swingset3/demos/internalframe/resources/InternalFrameDemo.properties",
67                 "com/sun/swingset3/demos/internalframe/resources/images/bananas.png",
68                 "com/sun/swingset3/demos/internalframe/resources/images/bananas_small.png",
69                 "com/sun/swingset3/demos/internalframe/resources/images/globe.png",
70                 "com/sun/swingset3/demos/internalframe/resources/images/globe_small.png",
71                 "com/sun/swingset3/demos/internalframe/resources/images/InternalFrameDemo.gif",
72                 "com/sun/swingset3/demos/internalframe/resources/images/package.png",
73                 "com/sun/swingset3/demos/internalframe/resources/images/package_small.png",
74                 "com/sun/swingset3/demos/internalframe/resources/images/soccer_ball.png",
75                 "com/sun/swingset3/demos/internalframe/resources/images/soccer_ball_small.png"
76                 }
77 )
78 public class InternalFrameDemo extends JPanel {
79 
80     public static final String DEMO_TITLE = InternalFrameDemo.class.getAnnotation(DemoProperties.class).value();
81     public static final String PALETTE_LABEL;
82     public static final String INTERNAL_FRAME_LABEL;
83     public static final String RESIZABLE_LABEL;
84     public static final String ICONIFIABLE_LABEL;
85     public static final String CLOSABLE_LABEL;
86     public static final String MAXIMIZABLE_LABEL;
87 
88     public static final int PALETTE_X = 20;
89     public static final int PALETTE_Y = 20;
90 
91     public static final int PALETTE_WIDTH = 250;
92     public static final int PALETTE_HEIGHT = 250;
93 
94     public static final int FRAME_WIDTH = 300;
95     public static final int FRAME_HEIGHT = 300;
96 
97     public static final int FRAME0_X = PALETTE_X + PALETTE_WIDTH + 20;
98     public static final int FRAME0_Y = 20;
99     public static final int FRAME_GAP = 20;
100 
101     private static final Dimension HGAP5 = new Dimension(5, 1);
102     private static final Dimension VGAP10 = new Dimension(1, 10);
103     private static final Dimension HGAP15 = new Dimension(15, 1);
104     private static final Dimension VGAP15 = new Dimension(1, 15);
105 
106     private final static ResourceManager resourceManager = new ResourceManager(InternalFrameDemo.class);
107 
108     static {
109         PALETTE_LABEL = resourceManager.getString("InternalFrameDemo.palette_label");
110         INTERNAL_FRAME_LABEL = resourceManager.getString("InternalFrameDemo.frame_label");
111         RESIZABLE_LABEL = resourceManager.getString("InternalFrameDemo.resizable_label");
112         ICONIFIABLE_LABEL = resourceManager.getString("InternalFrameDemo.iconifiable_label");
113         CLOSABLE_LABEL = resourceManager.getString("InternalFrameDemo.closable_label");
114         MAXIMIZABLE_LABEL = resourceManager.getString("InternalFrameDemo.maximizable_label");
115     }
116 
117     private int windowCount = 0;
118     private JDesktopPane desktop = null;
119 
120     private final ImageIcon icon1;
121     private final ImageIcon icon2;
122     private final ImageIcon icon3;
123     private final ImageIcon icon4;
124     private final ImageIcon smIcon1;
125     private final ImageIcon smIcon2;
126     private final ImageIcon smIcon3;
127     private final ImageIcon smIcon4;
128 
129     private final Integer DEMO_FRAME_LAYER = new Integer(2);
130     private final Integer PALETTE_LAYER = new Integer(3);
131 
132     private JCheckBox windowResizable = null;
133     private JCheckBox windowClosable = null;
134     private JCheckBox windowIconifiable = null;
135     private JCheckBox windowMaximizable = null;
136 
137     private JTextField windowTitleField = null;
138     private JLabel windowTitleLabel = null;
139 
140 
141     /**
142      * main method allows us to run as a standalone demo.
143      */
main(String[] args)144     public static void main(String[] args) {
145         JFrame frame = new JFrame(DEMO_TITLE);
146 
147         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
148         frame.getContentPane().add(new InternalFrameDemo());
149         frame.setPreferredSize(new Dimension(800, 600));
150         frame.pack();
151         frame.setLocationRelativeTo(null);
152         frame.setVisible(true);
153     }
154 
155     /**
156      * InternalFrameDemo Constructor
157      */
InternalFrameDemo()158     public InternalFrameDemo() {
159         setLayout(new BorderLayout());
160 
161         // preload all the icons we need for this demo
162         icon1 = resourceManager.createImageIcon("bananas.png",
163                 resourceManager.getString("InternalFrameDemo.bananas"));
164         icon2 = resourceManager.createImageIcon("globe.png",
165                 resourceManager.getString("InternalFrameDemo.globe"));
166         icon3 = resourceManager.createImageIcon("package.png",
167                 resourceManager.getString("InternalFrameDemo.package"));
168         icon4 = resourceManager.createImageIcon("soccer_ball.png",
169                 resourceManager.getString("InternalFrameDemo.soccerball"));
170 
171         smIcon1 = resourceManager.createImageIcon("bananas_small.png",
172                 resourceManager.getString("InternalFrameDemo.bananas"));
173         smIcon2 = resourceManager.createImageIcon("globe_small.png",
174                 resourceManager.getString("InternalFrameDemo.globe"));
175         smIcon3 = resourceManager.createImageIcon("package_small.png",
176                 resourceManager.getString("InternalFrameDemo.package"));
177         smIcon4 = resourceManager.createImageIcon("soccer_ball_small.png",
178                 resourceManager.getString("InternalFrameDemo.soccerball"));
179 
180         //<snip>Create desktop pane
181         // The desktop pane will contain all the internal frames
182         desktop = new JDesktopPane();
183         add(desktop, BorderLayout.CENTER);
184         //</snip>
185 
186         // Create the "frame maker" palette
187         createInternalFramePalette();
188 
189         // Create an initial internal frame to show
190         JInternalFrame frame1 = createInternalFrame(icon2, DEMO_FRAME_LAYER, 1, 1);
191         frame1.setBounds(FRAME0_X, FRAME0_Y, FRAME_WIDTH, FRAME_HEIGHT);
192 
193     }
194 
195 
196     /**
197      * Create an internal frame and add a scrollable imageicon to it
198      */
createInternalFrame(Icon icon, Integer layer, int width, int height)199     private JInternalFrame createInternalFrame(Icon icon, Integer layer, int width, int height) {
200         //<snip>Create internal frame
201         JInternalFrame internalFrame = new JInternalFrame();
202         //</snip>
203 
204         if (!windowTitleField.getText().equals(resourceManager.getString("InternalFrameDemo.frame_label"))) {
205             internalFrame.setTitle(windowTitleField.getText() + "  ");
206         } else {
207             internalFrame = new JInternalFrame(INTERNAL_FRAME_LABEL + " " + windowCount + "  ");
208         }
209 
210         //<snip>Set internal frame properties
211         internalFrame.setClosable(windowClosable.isSelected());
212         internalFrame.setMaximizable(windowMaximizable.isSelected());
213         internalFrame.setIconifiable(windowIconifiable.isSelected());
214         internalFrame.setResizable(windowResizable.isSelected());
215         //</snip>
216 
217         internalFrame.setBounds(FRAME0_X + FRAME_GAP * (windowCount % 10),
218                 FRAME0_Y + FRAME_GAP * (windowCount % 10), width, height);
219         internalFrame.setContentPane(new ImageScroller(icon));
220 
221         windowCount++;
222 
223         //<snip>Add internal frame to desktop pane
224         desktop.add(internalFrame, layer);
225         //</snip>
226 
227         //<snip>Set internal frame to be active
228         try {
229             internalFrame.setSelected(true);
230         } catch (java.beans.PropertyVetoException e2) {
231         }
232         //</snip>
233 
234         internalFrame.show();
235 
236         return internalFrame;
237     }
238 
createInternalFramePalette()239     private void createInternalFramePalette() {
240         JInternalFrame palette = new JInternalFrame(PALETTE_LABEL);
241         palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
242         palette.getContentPane().setLayout(new BorderLayout());
243         palette.setBounds(PALETTE_X, PALETTE_Y, PALETTE_WIDTH, PALETTE_HEIGHT);
244         palette.setResizable(true);
245         palette.setIconifiable(true);
246         desktop.add(palette, PALETTE_LAYER);
247 
248         // *************************************
249         // * Create create frame maker buttons *
250         // *************************************
251         JButton b1 = new JButton(smIcon1);
252         JButton b2 = new JButton(smIcon2);
253         JButton b3 = new JButton(smIcon3);
254         JButton b4 = new JButton(smIcon4);
255 
256         // add frame maker actions
257         b1.addActionListener(new CreateFrameAction(this, icon1));
258         b2.addActionListener(new CreateFrameAction(this, icon2));
259         b3.addActionListener(new CreateFrameAction(this, icon3));
260         b4.addActionListener(new CreateFrameAction(this, icon4));
261 
262         // add frame maker buttons to panel
263         JPanel p = new JPanel();
264         p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
265 
266         JPanel buttons1 = new JPanel();
267         buttons1.setLayout(new BoxLayout(buttons1, BoxLayout.X_AXIS));
268 
269         JPanel buttons2 = new JPanel();
270         buttons2.setLayout(new BoxLayout(buttons2, BoxLayout.X_AXIS));
271 
272         buttons1.add(b1);
273         buttons1.add(Box.createRigidArea(HGAP15));
274         buttons1.add(b2);
275 
276         buttons2.add(b3);
277         buttons2.add(Box.createRigidArea(HGAP15));
278         buttons2.add(b4);
279 
280         p.add(Box.createRigidArea(VGAP10));
281         p.add(buttons1);
282         p.add(Box.createRigidArea(VGAP15));
283         p.add(buttons2);
284         p.add(Box.createRigidArea(VGAP10));
285 
286         palette.getContentPane().add(p, BorderLayout.NORTH);
287 
288         // ************************************
289         // * Create frame property checkboxes *
290         // ************************************
291         p = new JPanel();
292         p.setBorder(new EmptyBorder(10, 15, 10, 5));
293         p.setLayout(new GridLayout(1, 2));
294 
295 
296         Box box = new Box(BoxLayout.Y_AXIS);
297         windowResizable = new JCheckBox(RESIZABLE_LABEL, true);
298         windowIconifiable = new JCheckBox(ICONIFIABLE_LABEL, true);
299 
300         box.add(Box.createGlue());
301         box.add(windowResizable);
302         box.add(windowIconifiable);
303         box.add(Box.createGlue());
304         p.add(box);
305 
306         box = new Box(BoxLayout.Y_AXIS);
307         windowClosable = new JCheckBox(CLOSABLE_LABEL, true);
308         windowMaximizable = new JCheckBox(MAXIMIZABLE_LABEL, true);
309 
310         box.add(Box.createGlue());
311         box.add(windowClosable);
312         box.add(windowMaximizable);
313         box.add(Box.createGlue());
314         p.add(box);
315 
316         palette.getContentPane().add(p, BorderLayout.CENTER);
317 
318         // ************************************
319         // *   Create Frame title textfield   *
320         // ************************************
321         p = new JPanel();
322         p.setBorder(new EmptyBorder(0, 0, 10, 0));
323 
324         windowTitleField = new JTextField(resourceManager.getString("InternalFrameDemo.frame_label"));
325         windowTitleLabel = new JLabel(resourceManager.getString("InternalFrameDemo.title_text_field_label"));
326 
327         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
328         p.add(Box.createRigidArea(HGAP5));
329         p.add(windowTitleLabel, BorderLayout.WEST);
330         p.add(Box.createRigidArea(HGAP5));
331         p.add(windowTitleField, BorderLayout.CENTER);
332         p.add(Box.createRigidArea(HGAP5));
333 
334         palette.getContentPane().add(p, BorderLayout.SOUTH);
335 
336         palette.show();
337     }
338 
339 
340     private class CreateFrameAction extends AbstractAction {
341         final InternalFrameDemo demo;
342         final Icon icon;
343 
CreateFrameAction(InternalFrameDemo demo, Icon icon)344         public CreateFrameAction(InternalFrameDemo demo, Icon icon) {
345             this.demo = demo;
346             this.icon = icon;
347         }
348 
actionPerformed(ActionEvent e)349         public void actionPerformed(ActionEvent e) {
350             demo.createInternalFrame(icon,
351                     DEMO_FRAME_LAYER,
352                     FRAME_WIDTH,
353                     FRAME_HEIGHT
354             );
355         }
356     }
357 
358     private static class ImageScroller extends JScrollPane {
359 
ImageScroller(Icon icon)360         public ImageScroller(Icon icon) {
361             super();
362             JPanel p = new JPanel();
363             p.setBackground(Color.white);
364             p.setLayout(new BorderLayout());
365 
366             p.add(new JLabel(icon), BorderLayout.CENTER);
367 
368             getViewport().add(p);
369             getHorizontalScrollBar().setUnitIncrement(10);
370             getVerticalScrollBar().setUnitIncrement(10);
371         }
372 
getMinimumSize()373         public Dimension getMinimumSize() {
374             return new Dimension(25, 25);
375         }
376 
377     }
378 }
379