8210994: Create test for SwingSet3 FrameDemo

Reviewed-by: serb
This commit is contained in:
Abdul Kolarkunnu 2018-09-24 03:25:19 -07:00 committed by Abdul Kolarkunnu
parent cad60d14a6
commit e6149b08de
6 changed files with 720 additions and 10 deletions

View File

@ -0,0 +1,296 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import static com.sun.swingset3.demos.frame.FrameDemo.BUSY_CHECKBOX;
import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL;
import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL_COLOR;
import static com.sun.swingset3.demos.frame.FrameDemo.CONTENT_LABEL_SIZE;
import static com.sun.swingset3.demos.frame.FrameDemo.DEMO_TITLE;
import static com.sun.swingset3.demos.frame.FrameDemo.INTERNAL_FRAME;
import static com.sun.swingset3.demos.frame.FrameDemo.MENU;
import static com.sun.swingset3.demos.frame.FrameDemo.MENU_ITEM1;
import static com.sun.swingset3.demos.frame.FrameDemo.MENU_ITEM2;
import static com.sun.swingset3.demos.frame.FrameDemo.SHOW_BUTTON;
import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL;
import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL_BORDER;
import static com.sun.swingset3.demos.frame.FrameDemo.STATUS_LABEL_HOR_ALIGNMENT;
import static com.sun.swingset3.demos.frame.FrameDemo.TOOLBAR_BUTTON;
import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
import static org.testng.AssertJUnit.assertEquals;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import org.jtregext.GuiTestListener;
import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.ComponentChooser;
import org.netbeans.jemmy.WindowWaiter;
import org.netbeans.jemmy.operators.ComponentOperator;
import org.netbeans.jemmy.operators.FrameOperator;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JCheckBoxOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JLabelOperator;
import org.netbeans.jemmy.operators.JMenuBarOperator;
import org.netbeans.jemmy.operators.JMenuItemOperator;
import org.netbeans.jemmy.operators.JMenuOperator;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com.sun.swingset3.demos.frame.FrameDemo;
/*
* @test
* @key headful
* @summary Verifies SwingSet3 FrameDemo page by checking the different actions
* on the frame, properties and different actions on subcomponents of frame
* and control panel actions by checking and unchecking the busy check box and
* pressing the show button.
*
* @library /sanity/client/lib/jemmy/src
* @library /sanity/client/lib/Extensions/src
* @library /sanity/client/lib/SwingSet3/src
* @modules java.desktop
* java.logging
* @build org.jemmy2ext.JemmyExt
* @build com.sun.swingset3.demos.frame.FrameDemo
* @run testng FrameDemoTest
*/
@Listeners(GuiTestListener.class)
public class FrameDemoTest {
private final static Dimension NEW_SIZE = new Dimension(500, 500);
private final static Point NEW_LOCATION = new Point(200, 200);
private final static int NUMBER_OF_MENUS = 1;
private final static int NUMBER_OF_MENU_ITEMS = 2;
private final static int MAX_NUMBER_OF_FRAMES = 1;
private final static int DELAY_AFTER_SHOW_BUTTON_PRESS = 500;
/**
* Testing the different actions on the frame, properties and different
* actions on subcomponents of the frame and control panel action by
* checking and unchecking the busy check box and pressing the show button.
*
* @throws Exception
*/
@Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
public void test(String lookAndFeel) throws Exception {
UIManager.setLookAndFeel(lookAndFeel);
new ClassReference(FrameDemo.class.getCanonicalName()).startApplication();
JFrameOperator masterFrameOperator = new JFrameOperator(DEMO_TITLE);
masterFrameOperator.setComparator(EXACT_STRING_COMPARATOR);
JFrameOperator internalFrameOperator = new JFrameOperator(INTERNAL_FRAME);
internalFrameOperator.setComparator(EXACT_STRING_COMPARATOR);
internalFrameOperator.setVerification(true);
internalFrameOperator.waitComponentVisible(true);
checkSubComponents(internalFrameOperator);
checkFrameActions(internalFrameOperator);
checkControlPanelActions(masterFrameOperator, internalFrameOperator);
}
/**
* Verifying the status of added components to the frame
* @param internalFrameOperator
*/
private void checkSubComponents(JFrameOperator internalFrameOperator) {
// Verifying the properties of added button to the frame
JButtonOperator buttonOperator =
new JButtonOperator(internalFrameOperator, TOOLBAR_BUTTON);
AtomicBoolean buttonActionStatus = new AtomicBoolean(false);
buttonOperator.addActionListener(event -> buttonActionStatus.set(true));
buttonOperator.push();
buttonOperator.waitStateOnQueue(comp -> buttonActionStatus.get());
// Verifying the properties of added labels to the frame
JLabelOperator contentLabelOperator =
new JLabelOperator(internalFrameOperator, CONTENT_LABEL);
contentLabelOperator.waitStateOnQueue(comp
-> CONTENT_LABEL_SIZE.equals(comp.getSize()));
contentLabelOperator.waitStateOnQueue(comp
-> CONTENT_LABEL_COLOR.equals(comp.getBackground()));
JLabelOperator statusLabelOperator =
new JLabelOperator(internalFrameOperator, STATUS_LABEL);
statusLabelOperator.waitStateOnQueue(comp
-> STATUS_LABEL_BORDER.equals(((JLabel)comp).getBorder()));
statusLabelOperator.waitStateOnQueue((component) -> STATUS_LABEL_HOR_ALIGNMENT
== ((JLabel)component).getHorizontalAlignment());
// Verifying the properties of added menu to the frame
JMenuBarOperator menuBarOperator = new JMenuBarOperator(internalFrameOperator);
menuBarOperator.waitStateOnQueue(comp -> NUMBER_OF_MENUS
== ((JMenuBar)comp).getMenuCount());
JMenuOperator menuOperator = new JMenuOperator(internalFrameOperator, MENU);
menuOperator.waitStateOnQueue(comp -> NUMBER_OF_MENU_ITEMS
== ((JMenu)comp).getMenuComponentCount());
AtomicBoolean menuActionStatus = new AtomicBoolean(false);
addMenuListener(menuOperator, menuActionStatus);
menuOperator.push();
menuOperator.waitStateOnQueue(comp -> menuActionStatus.get());
// Verifying the properties of the menu items
checkMenuItem((JMenuItem) menuOperator.getMenuComponent(0), MENU_ITEM1);
checkMenuItem((JMenuItem) menuOperator.getMenuComponent(1), MENU_ITEM2);
}
/**
* Verifying different actions on the frame
* @param internalFrameOperator
*/
private void checkFrameActions(JFrameOperator internalFrameOperator)
throws InterruptedException {
// Verifying the maximized status
internalFrameOperator.maximize();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
internalFrameOperator.demaximize();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// Verifying the iconified status
internalFrameOperator.iconify();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
internalFrameOperator.deiconify();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// Verifying the resize of the frame
TestHelpers.checkChangeSize(internalFrameOperator, NEW_SIZE);
// Verifying the change of location of the frame
TestHelpers.checkChangeLocation(internalFrameOperator, NEW_LOCATION);
}
/**
* Verifying control panel actions on the frame
* @param masterFrameOprator
* @param internalFrameOperator
* @throws InterruptedException
*/
private void checkControlPanelActions(JFrameOperator masterFrameOprator,
JFrameOperator internalFrameOperator) throws InterruptedException {
// Verifying the visibility and number of frames after pressing Show Button
internalFrameOperator.requestClose();
internalFrameOperator.waitClosed();
JButtonOperator showButtonOperator = new JButtonOperator(masterFrameOprator, SHOW_BUTTON);
showButtonOperator.push();
internalFrameOperator.waitComponentVisible(true);
showButtonOperator.push();
showButtonOperator.push();
Thread.sleep(DELAY_AFTER_SHOW_BUTTON_PRESS);
int count = WindowWaiter.countWindows(masterFrameOprator.getOwner(),
new FrameOperator.FrameFinder(new ComponentChooser() {
@Override
public String getDescription() {
return "frames with name != " + DEMO_TITLE;
}
@Override
public boolean checkComponent(Component comp) {
return comp.isShowing()
&& ((Frame) comp).getTitle() != DEMO_TITLE;
}
}));
assertEquals("Number of frames after clicking Show Button two times"
+ " validation failed,", MAX_NUMBER_OF_FRAMES, count);
// Verifying the visibility and cursor type after selecting busy check box
JCheckBoxOperator busyCheckBoxOperator =
new JCheckBoxOperator(masterFrameOprator, BUSY_CHECKBOX);
busyCheckBoxOperator.setVerification(true);
checkBusyCheckBox(internalFrameOperator, busyCheckBoxOperator, true);
internalFrameOperator.waitStateOnQueue(comp -> Cursor.WAIT_CURSOR
== internalFrameOperator.getGlassPane().getCursor().getType());
checkBusyCheckBox(internalFrameOperator, busyCheckBoxOperator, false);
internalFrameOperator.waitStateOnQueue(comp -> Cursor.DEFAULT_CURSOR
== internalFrameOperator.getCursor().getType());
}
private void checkBusyCheckBox(JFrameOperator internalFrameOperator,
JCheckBoxOperator busyCheckBoxOperator, boolean isSelect) {
busyCheckBoxOperator.changeSelection(isSelect);
new ComponentOperator(internalFrameOperator.
getGlassPane()).waitComponentVisible(isSelect);
}
/**
* Verifying the properties of the menu item
* @param menuItem : menu item component
* @param menuExpectedName : expected menu item name/text
*/
private void checkMenuItem(JMenuItem menuItem, String menuExpectedName) {
JMenuItemOperator menuItemOperator = new JMenuItemOperator(menuItem);
AtomicBoolean menuItemActionStatus = new AtomicBoolean(false);
menuItemOperator.addActionListener(event -> menuItemActionStatus.set(true));
menuItemOperator.waitStateOnQueue((component)
-> menuExpectedName.equals(((JMenuItem)component).getText()));
menuItemOperator.push();
menuItemOperator.waitStateOnQueue(comp -> menuItemActionStatus.get());
}
/**
* Add menu listener to the operator
* @param menuOperator : JMenuOperator on which menu listener has to be added
* @param menuActionStatus : menu action status variable
*/
private void addMenuListener(JMenuOperator menuOperator,
AtomicBoolean menuActionStatus) {
menuOperator.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
menuActionStatus.set(true);
}
@Override
public void menuDeselected(MenuEvent e) {
}
@Override
public void menuCanceled(MenuEvent e) {
}
});
}
}

View File

@ -138,6 +138,9 @@ public class InternalFrameDemoTest {
orignalSize.height - PARENT_FRAME_NEW_SIZE_DELTA);
parentFrameOperator.resize(newSize.width, newSize.height);
parentFrameOperator.waitComponentSize(newSize);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// keeping some delay before checking the internal frame property
// as it is a negative scenario
Thread.sleep(DELAY);
@ -145,26 +148,41 @@ public class InternalFrameDemoTest {
// Resizing parent frame back to original size
parentFrameOperator.resize(orignalSize.width, orignalSize.height);
parentFrameOperator.waitComponentSize(orignalSize);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// Iconifying the parent frame and verifying the iconified status of the internal
// frame(it should not be iconified)
parentFrameOperator.iconify();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// keeping some delay before checking the internal frame property
// as it is a negative scenario
Thread.sleep(DELAY);
assertFalse("Internal Frame should not be iconified when parent frame"
+ " alone is iconified.", internalFrameOperator.isIcon());
parentFrameOperator.deiconify();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// Maximizing the parent frame and verifying the maximized status of the internal
// frame(it should not be maximized)
parentFrameOperator.maximize();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// keeping some delay before checking the internal frame property
// as it is a negative scenario
Thread.sleep(DELAY);
assertFalse("Internal Frame should not be maximized when parent frame"
+ " alone is maximized.", internalFrameOperator.isMaximum());
parentFrameOperator.demaximize();
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// Relocating the parent frame and verifying the location of the internal
// frame(it should not be changed the location)
@ -173,6 +191,9 @@ public class InternalFrameDemoTest {
(orignalLocation.y + PARENT_FRAME_NEW_LOCATION_DELTA));
parentFrameOperator.move(newLocation.x, newLocation.y);
parentFrameOperator.waitComponentLocation(newLocation);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
// keeping some delay before checking the internal frame property
// as it is a negative scenario
Thread.sleep(DELAY);
@ -180,15 +201,19 @@ public class InternalFrameDemoTest {
// Moving back parent frame to original location
parentFrameOperator.move(orignalLocation.x, orignalLocation.y);
parentFrameOperator.waitComponentLocation(orignalLocation);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
TestHelpers.delayBetweenFrameStateChange();
}
/**
* Verifying different actions on the internal frame.
*
* @param internalFrameOperator : internal fame operator
* @throws InterruptedException
*/
private void checkInternalFrameAction(
JInternalFrameOperator internalFrameOperator) {
private void checkInternalFrameAction(JInternalFrameOperator
internalFrameOperator) throws InterruptedException {
// Verifying maximize and demaximize actions
internalFrameOperator.waitStateOnQueue(comp
-> ((JInternalFrame)comp).isMaximizable());

View File

@ -1,16 +1,38 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Dimension;
import java.awt.Point;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import javax.swing.UIManager;
import org.netbeans.jemmy.operators.ComponentOperator;
import org.testng.annotations.DataProvider;
public class TestHelpers {
public static final long DELAY_BTWN_FRAME_STATE_CHANGE = 2000;
/**
* A DataProvider having the class name of all the available look and feels
*
@ -29,21 +51,40 @@ public class TestHelpers {
}
public static void checkChangeLocation(ComponentOperator component,
Point finalLocation) {
Point finalLocation) throws InterruptedException {
Point initialLocation = component.getLocation();
component.setLocation(finalLocation);
component.waitComponentLocation(finalLocation);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
delayBetweenFrameStateChange();
component.setLocation(initialLocation);
component.waitComponentLocation(initialLocation);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
delayBetweenFrameStateChange();
}
public static void checkChangeSize(ComponentOperator component,
Dimension dimensionFinal) {
Dimension dimensionFinal) throws InterruptedException {
Dimension dimensionInitial = component.getSize();
component.setSize(dimensionFinal);
component.waitComponentSize(dimensionFinal);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
delayBetweenFrameStateChange();
component.setSize(dimensionInitial);
component.waitComponentSize(dimensionInitial);
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
delayBetweenFrameStateChange();
}
}
// TODO This is a workaround for JDK-8210638, this delay has to remove
// after fixing this bug, this is an unstable code.
public static void delayBetweenFrameStateChange()
throws InterruptedException {
Thread.sleep(DELAY_BTWN_FRAME_STATE_CHANGE);
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.swingset3.demos.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
* GlassPane component which can be set on toplevel
* containers to makes those containers "busy" be disabling input.
*
* Example usage:
* <pre><code>
* // Install glasspane
* frame.setGlassPane(new BusyGlass());
*
* // Make frame busy
* frame.getGlassPane().setVisible(true);
* </code></pre>
*
* Caution: A well-written client should rarely need to make
* a window "busy" because the app should be as responsive as possible;
* long-winded operations should be off-loaded to non-GUI threads
* whenever possible.
*
* @author aim
*/
//<snip>Make toplevel "busy"
public class BusyGlass extends JPanel {
/**
* Create GlassPane component to block input on toplevel
*/
public BusyGlass() {
setLayout(new BorderLayout());
setVisible(false); //initially invisible
setOpaque(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
protected void paintComponent(Graphics g) {
// Render partially opaque to 'veil' the frame's contents so
// that the user has visual feedback that the components
// arn't responsive.
Color bgColor = getBackground();
g.setColor(new Color(bgColor.getRed(),
bgColor.getGreen(),
bgColor.getBlue(), 150));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
//</snip>

View File

@ -0,0 +1,272 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.swingset3.demos.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.sun.swingset3.DemoProperties;
import com.sun.swingset3.demos.DemoUtilities;
/**
* Demo for Swing's JFrame toplevel component.
*
* @author aim
*/
@DemoProperties(
value = "JFrame Demo",
category = "Toplevel Containers",
description = "Demonstrates JFrame, Swing's top-level primary window container.",
sourceFiles = {
"com/sun/swingset3/demos/frame/BusyGlass.java",
"com/sun/swingset3/demos/frame/FrameDemo.java",
"com/sun/swingset3/demos/DemoUtilities.java",
"com/sun/swingset3/demos/frame/resources/FrameDemo.html",
"com/sun/swingset3/demos/frame/resources/images/FrameDemo.gif"
}
)
public class FrameDemo extends JPanel {
public static final String DEMO_TITLE = FrameDemo.class.getAnnotation(DemoProperties.class).value();
public static final String INTERNAL_FRAME = "Demo JFrame";
public static final String MENU = "File";
public static final String MENU_ITEM1 = "Open";
public static final String MENU_ITEM2 = "Save";
public static final String TOOLBAR = "Toolbar";
public static final String TOOLBAR_BUTTON = "Toolbar Button";
public static final String CONTENT_LABEL = "I'm content but a little blue.";
public static final String STATUS_LABEL = "I show status.";
public static final String SHOW_BUTTON = "Show JFrame...";
public static final String BUSY_CHECKBOX = "Frame busy";
public static final Dimension CONTENT_LABEL_SIZE = new Dimension(300, 160);
public static final Color CONTENT_LABEL_COLOR = new Color(197, 216, 236);
public static final int STATUS_LABEL_HOR_ALIGNMENT = JLabel.LEADING;
public static final EmptyBorder STATUS_LABEL_BORDER = new EmptyBorder(4, 4, 4, 4);
//<snip>Ensure system menubar is used on Mac OSX
static {
// Property must be set *early* due to Apple Bug#3909714
// ignored on other platforms
if (System.getProperty("os.name").equals("Mac OS X")) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
}
//</snip>
// Toplevel frame component
private JFrame frame;
private JComponent frameSpaceholder;
public FrameDemo() {
initComponents();
}
protected void initComponents() {
frame = createFrame();
setLayout(new BorderLayout());
add(createControlPanel(), BorderLayout.WEST);
frameSpaceholder = createFrameSpaceholder(frame);
add(frameSpaceholder, BorderLayout.CENTER);
}
protected JComponent createControlPanel() {
Box controlPanel = Box.createVerticalBox();
controlPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
// Create button to control visibility of frame
JButton showButton = new JButton(SHOW_BUTTON);
showButton.addActionListener(new ShowActionListener());
controlPanel.add(showButton);
// Create checkbox to control busy state of frame
JCheckBox busyCheckBox = new JCheckBox(BUSY_CHECKBOX);
busyCheckBox.setSelected(false);
busyCheckBox.addChangeListener(new BusyChangeListener());
controlPanel.add(busyCheckBox);
return controlPanel;
}
private static JComponent createFrameSpaceholder(JFrame frame) {
JPanel framePlaceholder = new JPanel();
Dimension prefSize = frame.getPreferredSize();
prefSize.width += 12;
prefSize.height += 12;
framePlaceholder.setPreferredSize(prefSize);
return framePlaceholder;
}
private static JFrame createFrame() {
//<snip>Create frame and set simple properties
JFrame frame = new JFrame(INTERNAL_FRAME);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//</snip>
//<snip>Set Minimized/titlebar icon Image
//Note: How the image is used is platform-dependent
Image iconImage = null;
try {
// todo: swingingduke.gif doesn't exist
URL imageURL = FrameDemo.class.getResource("resources/images/swingingduke.gif");
iconImage = ImageIO.read(imageURL);
} catch (Exception e) {
// handle image IO exception
}
frame.setIconImage(iconImage);
//</snip>
//<snip>Make toplevel "busy"
// busy glasspane is initially invisible
frame.setGlassPane(new BusyGlass());
//</snip>
//<snip>Add a menubar
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu = new JMenu(MENU);
menubar.add(menu);
menu.add(MENU_ITEM1);
menu.add(MENU_ITEM2);
//</snip>
//<snip>Add a horizontal toolbar
JToolBar toolbar = new JToolBar(TOOLBAR);
frame.add(toolbar, BorderLayout.NORTH);
toolbar.add(new JButton(TOOLBAR_BUTTON));
//</snip>
//<snip>Add the content area
JLabel label = new JLabel(CONTENT_LABEL);
label.setHorizontalAlignment(JLabel.CENTER);
label.setPreferredSize(CONTENT_LABEL_SIZE);
label.setBackground(CONTENT_LABEL_COLOR);
label.setOpaque(true); // labels non-opaque by default
frame.add(label);
//snip
//<snip>Add a statusbar
JLabel statusLabel = new JLabel(STATUS_LABEL);
statusLabel.setBorder(STATUS_LABEL_BORDER);
statusLabel.setHorizontalAlignment(STATUS_LABEL_HOR_ALIGNMENT);
frame.add(statusLabel, BorderLayout.SOUTH);
//</snip>
//<snip>Initialize frame's size to fit it's content
frame.pack();
//</snip>
return frame;
}
public void start() {
DemoUtilities.setToplevelLocation(frame, frameSpaceholder, SwingConstants.CENTER);
showFrame();
}
public void stop() {
//<snip>Hide frame
frame.setVisible(false);
//</snip>
}
public void showFrame() {
//<snip>Show frame
// if frame already visible, then bring to the front
if (frame.isShowing()) {
frame.toFront();
} else {
frame.setVisible(true);
}
//</snip>
}
//<snip>Make toplevel "busy"
public void setFrameBusy(boolean busy) {
frame.getGlassPane().setVisible(busy);
// Must explicitly disable the menubar because on OSX it will be
// in the system menubar and not covered by the glasspane
frame.getJMenuBar().setEnabled(!busy);
}
public boolean isFrameBusy() {
return frame.getGlassPane().isVisible();
}
//</snip
// remind(aim): replace with Beans binding
private class ShowActionListener implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
showFrame();
}
}
private class BusyChangeListener implements ChangeListener {
public void stateChanged(ChangeEvent changeEvent) {
JCheckBox busyCheckBox = (JCheckBox) changeEvent.getSource();
setFrameBusy(busyCheckBox.isSelected());
showFrame(); // bring frame back to front for demo purposes
}
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame(DEMO_TITLE);
FrameDemo demo = new FrameDemo();
frame.add(demo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
demo.start();
}
});
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 B