/* * * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
void loadDemos() { for(int i = 0; i < demos.length;) {
loadDemo(demos[i]);
i++;
}
}
// The current Look & Feel privatestatic LookAndFeelData currentLookAndFeel; privatestatic LookAndFeelData[] lookAndFeelData; // List of demos private ArrayList<DemoModule> demosList = new ArrayList<DemoModule>();
// The preferred size of the demo privatestaticfinalint PREFERRED_WIDTH = 720; privatestaticfinalint PREFERRED_HEIGHT = 640;
// Box spacers private Dimension HGAP = new Dimension(1,5); private Dimension VGAP = new Dimension(5,1);
// A place to hold on to the visible demo private DemoModule currentDemo = null; private JPanel demoPanel = null;
// About Box private JDialog aboutBox = null;
// Status Bar private JTextField statusField = null;
// Tool Bar private ToggleButtonToolBar toolbar = null; private ButtonGroup toolbarGroup = new ButtonGroup();
// Popup menu private JPopupMenu popupMenu = null; private ButtonGroup popupMenuGroup = new ButtonGroup();
// Used only if swingset is an application private JFrame frame = null;
// To debug or not to debug, that is the question privateboolean DEBUG = true; privateint debugCounter = 0;
// The tab pane that holds the demo private JTabbedPane tabbedPane = null;
private JEditorPane demoSrcPane = null;
// contentPane cache, saved from the applet or application frame
Container contentPane = null;
// number of swingsets - for multiscreen // keep track of the number of SwingSets created - we only want to exit // the program when the last one has been closed. privatestaticint numSSs = 0; privatestatic Vector<SwingSet2> swingSets = new Vector<SwingSet2>();
// set the preferred size of the demo
setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT));
initializeDemo();
preloadFirstDemo();
showSwingSet2();
// Start loading the rest of the demo in the background
DemoLoadThread demoLoader = new DemoLoadThread(this);
demoLoader.start();
}
/** * SwingSet2 Main. Called only if we're an application, not an applet.
*/ publicstaticvoid main(final String[] args) { // must run in EDT when constructing the GUI components
SwingUtilities.invokeLater(() -> { // Create SwingSet on the default monitor
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingSet2 swingset = new SwingSet2(GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice().
getDefaultConfiguration());
});
}
createMenuItem(fileMenu, "FileMenu.exit_label", "FileMenu.exit_mnemonic", "FileMenu.exit_accessible_description", new ExitAction(this)
);
// Create these menu items for the first SwingSet only. if (numSSs == 0) { // ***** create laf switcher menu
lafMenu = (JMenu) menuBar.add(new JMenu(getString("LafMenu.laf_label")));
lafMenu.setMnemonic(getMnemonic("LafMenu.laf_mnemonic"));
lafMenu.getAccessibleContext().setAccessibleDescription(
getString("LafMenu.laf_accessible_description"));
for (LookAndFeelData lafData : lookAndFeelData) {
mi = createLafMenuItem(lafMenu, lafData);
mi.setSelected(lafData.equals(currentLookAndFeel));
}
// ***** create the audio submenu under the theme menu
audioMenu = (JMenu) themesMenu.add(new JMenu(getString("AudioMenu.audio_label")));
audioMenu.setMnemonic(getMnemonic("AudioMenu.audio_mnemonic"));
audioMenu.getAccessibleContext().setAccessibleDescription(
getString("AudioMenu.audio_accessible_description"));
createAudioMenuItem(audioMenu, "AudioMenu.on_label", "AudioMenu.on_mnemonic", "AudioMenu.on_accessible_description", new OnAudioAction(this));
mi = createAudioMenuItem(audioMenu, "AudioMenu.default_label", "AudioMenu.default_mnemonic", "AudioMenu.default_accessible_description", new DefaultAudioAction(this));
mi.setSelected(true); // This is the default feedback setting
createAudioMenuItem(audioMenu, "AudioMenu.off_label", "AudioMenu.off_mnemonic", "AudioMenu.off_accessible_description", new OffAudioAction(this));
// ***** create the font submenu under the theme menu
JMenu fontMenu = (JMenu) themesMenu.add(new JMenu(getString("FontMenu.fonts_label")));
fontMenu.setMnemonic(getMnemonic("FontMenu.fonts_mnemonic"));
fontMenu.getAccessibleContext().setAccessibleDescription(
getString("FontMenu.fonts_accessible_description"));
ButtonGroup fontButtonGroup = new ButtonGroup();
mi = createButtonGroupMenuItem(fontMenu, "FontMenu.plain_label", "FontMenu.plain_mnemonic", "FontMenu.plain_accessible_description", new ChangeFontAction(this, true), fontButtonGroup);
mi.setSelected(true);
mi = createButtonGroupMenuItem(fontMenu, "FontMenu.bold_label", "FontMenu.bold_mnemonic", "FontMenu.bold_accessible_description", new ChangeFontAction(this, false), fontButtonGroup);
// *** now back to adding color/font themes to the theme menu
mi = createThemesMenuItem(themesMenu, "ThemesMenu.ocean_label", "ThemesMenu.ocean_mnemonic", "ThemesMenu.ocean_accessible_description", new OceanTheme());
mi.setSelected(true); // This is the default theme
createThemesMenuItem(themesMenu, "ThemesMenu.steel_label", "ThemesMenu.steel_mnemonic", "ThemesMenu.steel_accessible_description", new DefaultMetalTheme());
createThemesMenuItem(themesMenu, "ThemesMenu.aqua_label", "ThemesMenu.aqua_mnemonic", "ThemesMenu.aqua_accessible_description", new AquaTheme());
createThemesMenuItem(themesMenu, "ThemesMenu.charcoal_label", "ThemesMenu.charcoal_mnemonic", "ThemesMenu.charcoal_accessible_description", new CharcoalTheme());
createThemesMenuItem(themesMenu, "ThemesMenu.contrast_label", "ThemesMenu.contrast_mnemonic", "ThemesMenu.contrast_accessible_description", new ContrastTheme());
createThemesMenuItem(themesMenu, "ThemesMenu.emerald_label", "ThemesMenu.emerald_mnemonic", "ThemesMenu.emerald_accessible_description", new EmeraldTheme());
createThemesMenuItem(themesMenu, "ThemesMenu.ruby_label", "ThemesMenu.ruby_mnemonic", "ThemesMenu.ruby_accessible_description", new RubyTheme());
// Enable theme menu based on L&F
themesMenu.setEnabled("Metal".equals(currentLookAndFeel.name));
// ***** create the options menu
optionsMenu = (JMenu)menuBar.add( new JMenu(getString("OptionsMenu.options_label")));
optionsMenu.setMnemonic(getMnemonic("OptionsMenu.options_mnemonic"));
optionsMenu.getAccessibleContext().setAccessibleDescription(
getString("OptionsMenu.options_accessible_description"));
// ***** create tool tip submenu item.
mi = createCheckBoxMenuItem(optionsMenu, "OptionsMenu.tooltip_label", "OptionsMenu.tooltip_mnemonic", "OptionsMenu.tooltip_accessible_description", new ToolTipAction());
mi.setSelected(true);
// ***** create drag support submenu item.
createCheckBoxMenuItem(optionsMenu, "OptionsMenu.dragEnabled_label", "OptionsMenu.dragEnabled_mnemonic", "OptionsMenu.dragEnabled_accessible_description", new DragSupportAction());
}
// ***** create the multiscreen menu, if we have multiple screens
GraphicsDevice[] screens = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getScreenDevices(); if (screens.length > 1) {
createMultiscreenMenuItem(multiScreenMenu, MultiScreenAction.ALL_SCREENS); for (int i = 0; i < screens.length; i++) {
createMultiscreenMenuItem(multiScreenMenu, i);
}
}
return menuBar;
}
/** * Create a checkbox menu menu item
*/ private JMenuItem createCheckBoxMenuItem(JMenu menu, String label,
String mnemonic,
String accessibleDescription,
Action action) {
JCheckBoxMenuItem mi = (JCheckBoxMenuItem)menu.add( new JCheckBoxMenuItem(getString(label)));
mi.setMnemonic(getMnemonic(mnemonic));
mi.getAccessibleContext().setAccessibleDescription(getString(
accessibleDescription));
mi.addActionListener(action); return mi;
}
/** * Create a radio button menu menu item for items that are part of a * button group.
*/ private JMenuItem createButtonGroupMenuItem(JMenu menu, String label,
String mnemonic,
String accessibleDescription,
Action action,
ButtonGroup buttonGroup) {
JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add( new JRadioButtonMenuItem(getString(label)));
buttonGroup.add(mi);
mi.setMnemonic(getMnemonic(mnemonic));
mi.getAccessibleContext().setAccessibleDescription(getString(
accessibleDescription));
mi.addActionListener(action); return mi;
}
/** * Creates a generic menu item
*/ public JMenuItem createMenuItem(JMenu menu, String label, String mnemonic,
String accessibleDescription, Action action) {
JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(getString(label)));
mi.setMnemonic(getMnemonic(mnemonic));
mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
mi.addActionListener(action); if(action == null) {
mi.setEnabled(false);
} return mi;
}
/** * Creates a JRadioButtonMenuItem for the Themes menu
*/ public JMenuItem createThemesMenuItem(JMenu menu, String label, String mnemonic,
String accessibleDescription, MetalTheme theme) {
JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
themesMenuGroup.add(mi);
mi.setMnemonic(getMnemonic(mnemonic));
mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
mi.addActionListener(new ChangeThemeAction(this, theme));
return mi;
}
/** * Creates a JRadioButtonMenuItem for the Look and Feel menu
*/ public JMenuItem createLafMenuItem(JMenu menu, LookAndFeelData lafData) {
JMenuItem mi = menu.add(new JRadioButtonMenuItem(lafData.label));
lafMenuGroup.add(mi);
mi.setMnemonic(lafData.mnemonic);
mi.getAccessibleContext().setAccessibleDescription(lafData.accDescription);
mi.addActionListener(new ChangeLookAndFeelAction(this, lafData)); return mi;
}
/** * Creates a multi-screen menu item
*/ public JMenuItem createMultiscreenMenuItem(JMenu menu, int screen) {
JMenuItem mi = null; if (screen == MultiScreenAction.ALL_SCREENS) {
mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.all_label")));
mi.setMnemonic(getMnemonic("MultiMenu.all_mnemonic"));
mi.getAccessibleContext().setAccessibleDescription(getString( "MultiMenu.all_accessible_description"));
} else {
mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.single_label") + " " +
screen));
mi.setMnemonic(KeyEvent.VK_0 + screen);
mi.getAccessibleContext().setAccessibleDescription(getString( "MultiMenu.single_accessible_description") + " " + screen);
public JPopupMenu createPopupMenu() {
JPopupMenu popup = new JPopupMenu("JPopupMenu demo");
for (LookAndFeelData lafData : lookAndFeelData) {
createPopupMenuItem(popup, lafData);
}
// register key binding to activate popup menu
InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, InputEvent.SHIFT_DOWN_MASK), "postMenuAction");
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTEXT_MENU, 0), "postMenuAction");
getActionMap().put("postMenuAction", new ActivatePopupMenuAction(this, popup));
return popup;
}
/** * Creates a JMenuItem for the Look and Feel popup menu
*/ public JMenuItem createPopupMenuItem(JPopupMenu menu, LookAndFeelData lafData) {
JMenuItem mi = menu.add(new JMenuItem(lafData.label));
popupMenuGroup.add(mi);
mi.setMnemonic(lafData.mnemonic);
mi.getAccessibleContext().setAccessibleDescription(lafData.accDescription);
mi.addActionListener(new ChangeLookAndFeelAction(this, lafData)); return mi;
}
/** * Load the first demo. This is done separately from the remaining demos * so that we can get SwingSet2 up and available to the user quickly.
*/ publicvoid preloadFirstDemo() {
DemoModule demo = addDemo(new InternalFrameDemo(this));
setDemo(demo);
}
/** * Add a demo to the toolbar
*/ public DemoModule addDemo(DemoModule demo) {
demosList.add(demo); if (dragEnabled) {
demo.updateDragEnabled(true);
} // do the following on the gui thread
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) { publicvoid run() {
SwitchToDemoAction action = new SwitchToDemoAction(swingset, (DemoModule) obj);
JToggleButton tb = swingset.getToolBar().addToggleButton(action);
swingset.getToolBarGroup().add(tb); if(swingset.getToolBarGroup().getSelection() == null) {
tb.setSelected(true);
}
tb.setText(null);
tb.setToolTipText(((DemoModule)obj).getToolTip());
/** * Sets the current demo
*/ publicvoid setDemo(DemoModule demo) {
currentDemo = demo;
// Ensure panel's UI is current before making visible
JComponent currentDemoPanel = demo.getDemoPanel();
SwingUtilities.updateComponentTreeUI(currentDemoPanel);
/** * Bring up the SwingSet2 demo by showing the frame
*/ publicvoid showSwingSet2() { // put swingset in a frame and show it
JFrame f = getFrame();
f.setTitle(getString("Frame.title"));
f.getContentPane().add(this, BorderLayout.CENTER);
f.pack();
// Make sure we don't place the demo off the screen. int centerWidth = screenRect.width < f.getSize().width ?
screenRect.x :
screenRect.x + screenRect.width/2 - f.getSize().width/2; int centerHeight = screenRect.height < f.getSize().height ?
screenRect.y :
screenRect.y + screenRect.height/2 - f.getSize().height/2;
/** * Returns the frame instance
*/ public JFrame getFrame() { return frame;
}
/** * Returns the menubar
*/ public JMenuBar getMenuBar() { return menuBar;
}
/** * Returns the toolbar
*/ public ToggleButtonToolBar getToolBar() { return toolbar;
}
/** * Returns the toolbar button group
*/ public ButtonGroup getToolBarGroup() { return toolbarGroup;
}
/** * Returns the content pane whether we're in an applet * or application
*/ public Container getContentPane() { if(contentPane == null) { if(getFrame() != null) {
contentPane = getFrame().getContentPane();
}
} return contentPane;
}
/** * Create a frame for SwingSet2 to reside in if brought up * as an application.
*/ publicstatic JFrame createFrame(GraphicsConfiguration gc) {
JFrame frame = new JFrame(gc); if (numSSs == 0) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else {
WindowListener l = new WindowAdapter() { publicvoid windowClosing(WindowEvent e) {
numSSs--;
swingSets.remove(this);
}
};
frame.addWindowListener(l);
} return frame;
}
/** * Set the status
*/ publicvoid setStatus(String s) { // do the following on the gui thread
SwingUtilities.invokeLater(new SwingSetRunnable(this, s) { publicvoid run() {
swingset.statusField.setText((String) obj);
}
});
}
/** * This method returns a string from the demo's resource bundle.
*/ publicstatic String getString(String key) {
String value = null; try {
value = TextAndMnemonicUtils.getTextAndMnemonicString(key);
} catch (MissingResourceException e) {
System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
} if(value == null) {
value = "Could not find resource: " + key + " ";
} return value;
}
for (DemoModule dm : demosList) {
dm.updateDragEnabled(dragEnabled);
}
demoSrcPane.setDragEnabled(dragEnabled);
}
boolean isDragEnabled() { return dragEnabled;
}
/** * Returns a mnemonic from the resource bundle. Typically used as * keyboard shortcuts in menu items.
*/ publicchar getMnemonic(String key) { return (getString(key)).charAt(0);
}
/** * Creates an icon from an image contained in the "images" directory.
*/ public ImageIcon createImageIcon(String filename, String description) {
String path = "/resources/images/" + filename; returnnew ImageIcon(getClass().getResource(path));
}
/** * If DEBUG is defined, prints debug information out to std ouput.
*/ publicvoid debug(String s) { if(DEBUG) {
System.out.println((debugCounter++) + ": " + s);
}
}
/** * Stores the current L&F, and calls updateLookAndFeel, below
*/ publicvoid setLookAndFeel(LookAndFeelData laf) { if(!currentLookAndFeel.equals(laf)) {
currentLookAndFeel = laf; /* The recommended way of synchronizing state between multiple * controls that represent the same command is to use Actions. * The code below is a workaround and will be replaced in future * version of SwingSet2 demo.
*/
String lafName = laf.label;
themesMenu.setEnabled(laf.name.equals("Metal"));
updateLookAndFeel(); for(int i=0;i<lafMenu.getItemCount();i++) {
JMenuItem item = lafMenu.getItem(i);
item.setSelected(item.getText().equals(lafName));
}
}
}
SwingUtilities.updateComponentTreeUI(popupMenu); if (aboutBox != null) {
SwingUtilities.updateComponentTreeUI(aboutBox);
}
}
/** * Sets the current L&F on each demo module
*/ publicvoid updateLookAndFeel() { try {
UIManager.setLookAndFeel(currentLookAndFeel.className); for (SwingSet2 ss : swingSets) {
ss.updateThisSwingSet();
}
} catch (Exception ex) {
System.out.println("Failed loading L&F: " + currentLookAndFeel);
System.out.println(ex);
}
}
/** * Loads and puts the source code text into JEditorPane in the "Source Code" tab
*/ publicvoid setSourceCode(DemoModule demo) { // do the following on the gui thread
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) { publicvoid run() {
swingset.demoSrcPane.setText(((DemoModule)obj).getSourceCode());
swingset.demoSrcPane.setCaretPosition(0);
/** * Generic SwingSet2 runnable. This is intended to run on the * AWT gui event thread so as not to muck things up by doing * gui work off the gui thread. Accepts a SwingSet2 and an Object * as arguments, which gives subtypes of this class the two * "must haves" needed in most runnables for this demo.
*/ class SwingSetRunnable implements Runnable { protected SwingSet2 swingset; protected Object obj;
publicvoid actionPerformed(ActionEvent e) { if (plain) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
} else {
UIManager.put("swing.boldMetal", Boolean.TRUE);
} // Change the look and feel to force the settings to take effect.
updateLookAndFeel();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.