1 /*
   2  *
   3  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 import javax.swing.*;
  35 import javax.swing.event.*;
  36 import javax.swing.text.*;
  37 import javax.swing.border.*;
  38 import javax.swing.colorchooser.*;
  39 import javax.swing.filechooser.*;
  40 import javax.accessibility.*;
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import java.beans.*;
  45 import java.util.*;
  46 import java.io.*;
  47 import java.applet.*;
  48 import java.net.*;
  49 
  50 /**
  51  * Internal Frames Demo
  52  *
  53  * @author Jeff Dinkins
  54  */
  55 public class InternalFrameDemo extends DemoModule {
  56     int windowCount = 0;
  57     JDesktopPane desktop = null;
  58 
  59     ImageIcon icon1, icon2, icon3, icon4;
  60     ImageIcon smIcon1, smIcon2, smIcon3, smIcon4;
  61 
  62     public Integer FIRST_FRAME_LAYER  = Integer.valueOf(1);
  63     public Integer DEMO_FRAME_LAYER   = Integer.valueOf(2);
  64     public Integer PALETTE_LAYER     = Integer.valueOf(3);
  65 
  66     public int FRAME0_X        = 15;
  67     public int FRAME0_Y        = 280;
  68 
  69     public int FRAME0_WIDTH    = 320;
  70     public int FRAME0_HEIGHT   = 230;
  71 
  72     public int FRAME_WIDTH     = 225;
  73     public int FRAME_HEIGHT    = 150;
  74 
  75     public int PALETTE_X      = 375;
  76     public int PALETTE_Y      = 20;
  77 
  78     public int PALETTE_WIDTH  = 260;
  79     public int PALETTE_HEIGHT = 260;
  80 
  81     JCheckBox windowResizable   = null;
  82     JCheckBox windowClosable    = null;
  83     JCheckBox windowIconifiable = null;
  84     JCheckBox windowMaximizable = null;
  85 
  86     JTextField windowTitleField = null;
  87     JLabel windowTitleLabel = null;
  88 
  89 
  90     /**
  91      * main method allows us to run as a standalone demo.
  92      */
  93     public static void main(String[] args) {
  94         InternalFrameDemo demo = new InternalFrameDemo(null);
  95         demo.mainImpl();
  96     }
  97 
  98     /**
  99      * InternalFrameDemo Constructor
 100      */
 101     public InternalFrameDemo(SwingSet2 swingset) {
 102         super(swingset, "InternalFrameDemo", "toolbar/JDesktop.gif");
 103 
 104         // preload all the icons we need for this demo
 105         icon1 = createImageIcon("misc/toast.gif", getString("InternalFrameDemo.toast"));
 106         icon2 = createImageIcon("misc/duke.gif", getString("InternalFrameDemo.duke"));
 107         icon3 = createImageIcon("misc/duchess.gif",  getString("InternalFrameDemo.duchess"));
 108         icon4 = createImageIcon("misc/cab.gif",  getString("InternalFrameDemo.cab"));
 109 
 110         smIcon1 = createImageIcon("misc/toast_small.gif", getString("InternalFrameDemo.toast"));
 111         smIcon2 = createImageIcon("misc/duke_small.gif", getString("InternalFrameDemo.duke"));
 112         smIcon3 = createImageIcon("misc/duchess_small.gif",  getString("InternalFrameDemo.duchess"));
 113         smIcon4 = createImageIcon("misc/cab_small.gif",  getString("InternalFrameDemo.cab"));
 114 
 115         // Create the desktop pane
 116         desktop = new JDesktopPane();
 117         getDemoPanel().add(desktop, BorderLayout.CENTER);
 118 
 119         // Create the "frame maker" palette
 120         createInternalFramePalette();
 121 
 122         // Create an initial internal frame to show
 123         JInternalFrame frame1 = createInternalFrame(icon1, FIRST_FRAME_LAYER, 1, 1);
 124         frame1.setBounds(FRAME0_X, FRAME0_Y, FRAME0_WIDTH, FRAME0_HEIGHT);
 125 
 126         // Create four more starter windows
 127         createInternalFrame(icon1, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
 128         createInternalFrame(icon3, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
 129         createInternalFrame(icon4, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
 130         createInternalFrame(icon2, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);
 131     }
 132 
 133 
 134 
 135     /**
 136      * Create an internal frame and add a scrollable imageicon to it
 137      */
 138     public JInternalFrame createInternalFrame(Icon icon, Integer layer, int width, int height) {
 139         JInternalFrame jif = new JInternalFrame();
 140 
 141         if(!windowTitleField.getText().equals(getString("InternalFrameDemo.frame_label"))) {
 142             jif.setTitle(windowTitleField.getText() + "  ");
 143         } else {
 144             jif = new JInternalFrame(getString("InternalFrameDemo.frame_label") + " " + windowCount + "  ");
 145         }
 146 
 147         // set properties
 148         jif.setClosable(windowClosable.isSelected());
 149         jif.setMaximizable(windowMaximizable.isSelected());
 150         jif.setIconifiable(windowIconifiable.isSelected());
 151         jif.setResizable(windowResizable.isSelected());
 152 
 153         jif.setBounds(20*(windowCount%10), 20*(windowCount%10), width, height);
 154         jif.setContentPane(new ImageScroller(this, icon, 0, windowCount));
 155 
 156         windowCount++;
 157 
 158         desktop.add(jif, layer);
 159 
 160         // Set this internal frame to be selected
 161 
 162         try {
 163             jif.setSelected(true);
 164         } catch (java.beans.PropertyVetoException e2) {
 165         }
 166 
 167         jif.show();
 168 
 169         return jif;
 170     }
 171 
 172     public JInternalFrame createInternalFramePalette() {
 173         JInternalFrame palette = new JInternalFrame(
 174             getString("InternalFrameDemo.palette_label")
 175         );
 176         palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
 177         palette.getContentPane().setLayout(new BorderLayout());
 178         palette.setBounds(PALETTE_X, PALETTE_Y, PALETTE_WIDTH, PALETTE_HEIGHT);
 179         palette.setResizable(true);
 180         palette.setIconifiable(true);
 181         desktop.add(palette, PALETTE_LAYER);
 182 
 183         // *************************************
 184         // * Create create frame maker buttons *
 185         // *************************************
 186         JButton b1 = new JButton(smIcon1);
 187         JButton b2 = new JButton(smIcon2);
 188         JButton b3 = new JButton(smIcon3);
 189         JButton b4 = new JButton(smIcon4);
 190 
 191         // add frame maker actions
 192         b1.addActionListener(new ShowFrameAction(this, icon1));
 193         b2.addActionListener(new ShowFrameAction(this, icon2));
 194         b3.addActionListener(new ShowFrameAction(this, icon3));
 195         b4.addActionListener(new ShowFrameAction(this, icon4));
 196 
 197         // add frame maker buttons to panel
 198         JPanel p = new JPanel();
 199         p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
 200 
 201         JPanel buttons1 = new JPanel();
 202         buttons1.setLayout(new BoxLayout(buttons1, BoxLayout.X_AXIS));
 203 
 204         JPanel buttons2 = new JPanel();
 205         buttons2.setLayout(new BoxLayout(buttons2, BoxLayout.X_AXIS));
 206 
 207         buttons1.add(b1);
 208         buttons1.add(Box.createRigidArea(HGAP15));
 209         buttons1.add(b2);
 210 
 211         buttons2.add(b3);
 212         buttons2.add(Box.createRigidArea(HGAP15));
 213         buttons2.add(b4);
 214 
 215         p.add(Box.createRigidArea(VGAP10));
 216         p.add(buttons1);
 217         p.add(Box.createRigidArea(VGAP15));
 218         p.add(buttons2);
 219         p.add(Box.createRigidArea(VGAP10));
 220 
 221         palette.getContentPane().add(p, BorderLayout.NORTH);
 222 
 223         // ************************************
 224         // * Create frame property checkboxes *
 225         // ************************************
 226         p = new JPanel() {
 227             Insets insets = new Insets(10,15,10,5);
 228             public Insets getInsets() {
 229                 return insets;
 230             }
 231         };
 232         p.setLayout(new GridLayout(1,2));
 233 
 234 
 235         Box box = new Box(BoxLayout.Y_AXIS);
 236         windowResizable   = new JCheckBox(getString("InternalFrameDemo.resizable_label"), true);
 237         windowIconifiable = new JCheckBox(getString("InternalFrameDemo.iconifiable_label"), true);
 238 
 239         box.add(Box.createGlue());
 240         box.add(windowResizable);
 241         box.add(windowIconifiable);
 242         box.add(Box.createGlue());
 243         p.add(box);
 244 
 245         box = new Box(BoxLayout.Y_AXIS);
 246         windowClosable    = new JCheckBox(getString("InternalFrameDemo.closable_label"), true);
 247         windowMaximizable = new JCheckBox(getString("InternalFrameDemo.maximizable_label"), true);
 248 
 249         box.add(Box.createGlue());
 250         box.add(windowClosable);
 251         box.add(windowMaximizable);
 252         box.add(Box.createGlue());
 253         p.add(box);
 254 
 255         palette.getContentPane().add(p, BorderLayout.CENTER);
 256 
 257 
 258         // ************************************
 259         // *   Create Frame title textfield   *
 260         // ************************************
 261         p = new JPanel() {
 262             Insets insets = new Insets(0,0,10,0);
 263             public Insets getInsets() {
 264                 return insets;
 265             }
 266         };
 267 
 268         windowTitleField = new JTextField(getString("InternalFrameDemo.frame_label"));
 269         windowTitleLabel = new JLabel(getString("InternalFrameDemo.title_text_field_label"));
 270 
 271         p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
 272         p.add(Box.createRigidArea(HGAP5));
 273         p.add(windowTitleLabel, BorderLayout.WEST);
 274         p.add(Box.createRigidArea(HGAP5));
 275         p.add(windowTitleField, BorderLayout.CENTER);
 276         p.add(Box.createRigidArea(HGAP5));
 277 
 278         palette.getContentPane().add(p, BorderLayout.SOUTH);
 279 
 280         palette.show();
 281 
 282         return palette;
 283     }
 284 
 285 
 286     class ShowFrameAction extends AbstractAction {
 287         InternalFrameDemo demo;
 288         Icon icon;
 289 
 290 
 291         public ShowFrameAction(InternalFrameDemo demo, Icon icon) {
 292             this.demo = demo;
 293             this.icon = icon;
 294         }
 295 
 296         public void actionPerformed(ActionEvent e) {
 297             demo.createInternalFrame(icon,
 298                                      getDemoFrameLayer(),
 299                                      getFrameWidth(),
 300                                      getFrameHeight()
 301             );
 302         }
 303     }
 304 
 305     public int getFrameWidth() {
 306         return FRAME_WIDTH;
 307     }
 308 
 309     public int getFrameHeight() {
 310         return FRAME_HEIGHT;
 311     }
 312 
 313     public Integer getDemoFrameLayer() {
 314         return DEMO_FRAME_LAYER;
 315     }
 316 
 317     class ImageScroller extends JScrollPane {
 318 
 319         public ImageScroller(InternalFrameDemo demo, Icon icon, int layer, int count) {
 320             super();
 321             JPanel p = new JPanel();
 322             p.setBackground(Color.white);
 323             p.setLayout(new BorderLayout() );
 324 
 325             p.add(new JLabel(icon), BorderLayout.CENTER);
 326 
 327             getViewport().add(p);
 328             getHorizontalScrollBar().setUnitIncrement(10);
 329             getVerticalScrollBar().setUnitIncrement(10);
 330         }
 331 
 332         public Dimension getMinimumSize() {
 333             return new Dimension(25, 25);
 334         }
 335 
 336     }
 337 
 338     void updateDragEnabled(boolean dragEnabled) {
 339         windowTitleField.setDragEnabled(dragEnabled);
 340     }
 341 
 342 }