Path: blob/master/src/demo/share/jfc/SwingSet2/InternalFrameDemo.java
41152 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* Internal Frames Demo51*52* @author Jeff Dinkins53*/54public class InternalFrameDemo extends DemoModule {55int windowCount = 0;56JDesktopPane desktop = null;5758ImageIcon icon1, icon2, icon3, icon4;59ImageIcon smIcon1, smIcon2, smIcon3, smIcon4;6061public Integer FIRST_FRAME_LAYER = Integer.valueOf(1);62public Integer DEMO_FRAME_LAYER = Integer.valueOf(2);63public Integer PALETTE_LAYER = Integer.valueOf(3);6465public int FRAME0_X = 15;66public int FRAME0_Y = 280;6768public int FRAME0_WIDTH = 320;69public int FRAME0_HEIGHT = 230;7071public int FRAME_WIDTH = 225;72public int FRAME_HEIGHT = 150;7374public int PALETTE_X = 375;75public int PALETTE_Y = 20;7677public int PALETTE_WIDTH = 260;78public int PALETTE_HEIGHT = 260;7980JCheckBox windowResizable = null;81JCheckBox windowClosable = null;82JCheckBox windowIconifiable = null;83JCheckBox windowMaximizable = null;8485JTextField windowTitleField = null;86JLabel windowTitleLabel = null;878889/**90* main method allows us to run as a standalone demo.91*/92public static void main(String[] args) {93InternalFrameDemo demo = new InternalFrameDemo(null);94demo.mainImpl();95}9697/**98* InternalFrameDemo Constructor99*/100public InternalFrameDemo(SwingSet2 swingset) {101super(swingset, "InternalFrameDemo", "toolbar/JDesktop.gif");102103// preload all the icons we need for this demo104icon1 = createImageIcon("misc/toast.gif", getString("InternalFrameDemo.toast"));105icon2 = createImageIcon("misc/duke.gif", getString("InternalFrameDemo.duke"));106icon3 = createImageIcon("misc/duchess.gif", getString("InternalFrameDemo.duchess"));107icon4 = createImageIcon("misc/cab.gif", getString("InternalFrameDemo.cab"));108109smIcon1 = createImageIcon("misc/toast_small.gif", getString("InternalFrameDemo.toast"));110smIcon2 = createImageIcon("misc/duke_small.gif", getString("InternalFrameDemo.duke"));111smIcon3 = createImageIcon("misc/duchess_small.gif", getString("InternalFrameDemo.duchess"));112smIcon4 = createImageIcon("misc/cab_small.gif", getString("InternalFrameDemo.cab"));113114// Create the desktop pane115desktop = new JDesktopPane();116getDemoPanel().add(desktop, BorderLayout.CENTER);117118// Create the "frame maker" palette119createInternalFramePalette();120121// Create an initial internal frame to show122JInternalFrame frame1 = createInternalFrame(icon1, FIRST_FRAME_LAYER, 1, 1);123frame1.setBounds(FRAME0_X, FRAME0_Y, FRAME0_WIDTH, FRAME0_HEIGHT);124125// Create four more starter windows126createInternalFrame(icon1, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);127createInternalFrame(icon3, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);128createInternalFrame(icon4, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);129createInternalFrame(icon2, DEMO_FRAME_LAYER, FRAME_WIDTH, FRAME_HEIGHT);130}131132133134/**135* Create an internal frame and add a scrollable imageicon to it136*/137public JInternalFrame createInternalFrame(Icon icon, Integer layer, int width, int height) {138JInternalFrame jif = new JInternalFrame();139140if(!windowTitleField.getText().equals(getString("InternalFrameDemo.frame_label"))) {141jif.setTitle(windowTitleField.getText() + " ");142} else {143jif = new JInternalFrame(getString("InternalFrameDemo.frame_label") + " " + windowCount + " ");144}145146// set properties147jif.setClosable(windowClosable.isSelected());148jif.setMaximizable(windowMaximizable.isSelected());149jif.setIconifiable(windowIconifiable.isSelected());150jif.setResizable(windowResizable.isSelected());151152jif.setBounds(20*(windowCount%10), 20*(windowCount%10), width, height);153jif.setContentPane(new ImageScroller(this, icon, 0, windowCount));154155windowCount++;156157desktop.add(jif, layer);158159// Set this internal frame to be selected160161try {162jif.setSelected(true);163} catch (java.beans.PropertyVetoException e2) {164}165166jif.show();167168return jif;169}170171public JInternalFrame createInternalFramePalette() {172JInternalFrame palette = new JInternalFrame(173getString("InternalFrameDemo.palette_label")174);175palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);176palette.getContentPane().setLayout(new BorderLayout());177palette.setBounds(PALETTE_X, PALETTE_Y, PALETTE_WIDTH, PALETTE_HEIGHT);178palette.setResizable(true);179palette.setIconifiable(true);180desktop.add(palette, PALETTE_LAYER);181182// *************************************183// * Create create frame maker buttons *184// *************************************185JButton b1 = new JButton(smIcon1);186JButton b2 = new JButton(smIcon2);187JButton b3 = new JButton(smIcon3);188JButton b4 = new JButton(smIcon4);189190// add frame maker actions191b1.addActionListener(new ShowFrameAction(this, icon1));192b2.addActionListener(new ShowFrameAction(this, icon2));193b3.addActionListener(new ShowFrameAction(this, icon3));194b4.addActionListener(new ShowFrameAction(this, icon4));195196// add frame maker buttons to panel197JPanel p = new JPanel();198p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));199200JPanel buttons1 = new JPanel();201buttons1.setLayout(new BoxLayout(buttons1, BoxLayout.X_AXIS));202203JPanel buttons2 = new JPanel();204buttons2.setLayout(new BoxLayout(buttons2, BoxLayout.X_AXIS));205206buttons1.add(b1);207buttons1.add(Box.createRigidArea(HGAP15));208buttons1.add(b2);209210buttons2.add(b3);211buttons2.add(Box.createRigidArea(HGAP15));212buttons2.add(b4);213214p.add(Box.createRigidArea(VGAP10));215p.add(buttons1);216p.add(Box.createRigidArea(VGAP15));217p.add(buttons2);218p.add(Box.createRigidArea(VGAP10));219220palette.getContentPane().add(p, BorderLayout.NORTH);221222// ************************************223// * Create frame property checkboxes *224// ************************************225p = new JPanel() {226Insets insets = new Insets(10,15,10,5);227public Insets getInsets() {228return insets;229}230};231p.setLayout(new GridLayout(1,2));232233234Box box = new Box(BoxLayout.Y_AXIS);235windowResizable = new JCheckBox(getString("InternalFrameDemo.resizable_label"), true);236windowIconifiable = new JCheckBox(getString("InternalFrameDemo.iconifiable_label"), true);237238box.add(Box.createGlue());239box.add(windowResizable);240box.add(windowIconifiable);241box.add(Box.createGlue());242p.add(box);243244box = new Box(BoxLayout.Y_AXIS);245windowClosable = new JCheckBox(getString("InternalFrameDemo.closable_label"), true);246windowMaximizable = new JCheckBox(getString("InternalFrameDemo.maximizable_label"), true);247248box.add(Box.createGlue());249box.add(windowClosable);250box.add(windowMaximizable);251box.add(Box.createGlue());252p.add(box);253254palette.getContentPane().add(p, BorderLayout.CENTER);255256257// ************************************258// * Create Frame title textfield *259// ************************************260p = new JPanel() {261Insets insets = new Insets(0,0,10,0);262public Insets getInsets() {263return insets;264}265};266267windowTitleField = new JTextField(getString("InternalFrameDemo.frame_label"));268windowTitleLabel = new JLabel(getString("InternalFrameDemo.title_text_field_label"));269270p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));271p.add(Box.createRigidArea(HGAP5));272p.add(windowTitleLabel, BorderLayout.WEST);273p.add(Box.createRigidArea(HGAP5));274p.add(windowTitleField, BorderLayout.CENTER);275p.add(Box.createRigidArea(HGAP5));276277palette.getContentPane().add(p, BorderLayout.SOUTH);278279palette.show();280281return palette;282}283284285class ShowFrameAction extends AbstractAction {286InternalFrameDemo demo;287Icon icon;288289290public ShowFrameAction(InternalFrameDemo demo, Icon icon) {291this.demo = demo;292this.icon = icon;293}294295public void actionPerformed(ActionEvent e) {296demo.createInternalFrame(icon,297getDemoFrameLayer(),298getFrameWidth(),299getFrameHeight()300);301}302}303304public int getFrameWidth() {305return FRAME_WIDTH;306}307308public int getFrameHeight() {309return FRAME_HEIGHT;310}311312public Integer getDemoFrameLayer() {313return DEMO_FRAME_LAYER;314}315316class ImageScroller extends JScrollPane {317318public ImageScroller(InternalFrameDemo demo, Icon icon, int layer, int count) {319super();320JPanel p = new JPanel();321p.setBackground(Color.white);322p.setLayout(new BorderLayout() );323324p.add(new JLabel(icon), BorderLayout.CENTER);325326getViewport().add(p);327getHorizontalScrollBar().setUnitIncrement(10);328getVerticalScrollBar().setUnitIncrement(10);329}330331public Dimension getMinimumSize() {332return new Dimension(25, 25);333}334335}336337void updateDragEnabled(boolean dragEnabled) {338windowTitleField.setDragEnabled(dragEnabled);339}340341}342343344