Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sanity/client/lib/SwingSet2/src/SwingSet2.java
41161 views
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
import java.awt.BorderLayout;
25
import java.awt.Component;
26
import java.awt.Container;
27
import java.awt.Dimension;
28
import java.awt.Graphics;
29
import java.awt.GraphicsConfiguration;
30
import java.awt.GraphicsDevice;
31
import java.awt.GraphicsEnvironment;
32
import java.awt.Insets;
33
import java.awt.Rectangle;
34
import java.awt.Toolkit;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ContainerEvent;
37
import java.awt.event.ContainerListener;
38
import java.awt.event.InputEvent;
39
import java.awt.event.KeyEvent;
40
import java.awt.event.WindowAdapter;
41
import java.awt.event.WindowEvent;
42
import java.awt.event.WindowListener;
43
import java.util.ArrayList;
44
import java.util.Arrays;
45
import java.util.MissingResourceException;
46
import java.util.Vector;
47
48
import javax.swing.AbstractAction;
49
import javax.swing.Action;
50
import javax.swing.ButtonGroup;
51
import javax.swing.Icon;
52
import javax.swing.ImageIcon;
53
import javax.swing.InputMap;
54
import javax.swing.JButton;
55
import javax.swing.JCheckBoxMenuItem;
56
import javax.swing.JComponent;
57
import javax.swing.JDialog;
58
import javax.swing.JEditorPane;
59
import javax.swing.JFrame;
60
import javax.swing.JMenu;
61
import javax.swing.JMenuBar;
62
import javax.swing.JMenuItem;
63
import javax.swing.JPanel;
64
import javax.swing.JPopupMenu;
65
import javax.swing.JRadioButtonMenuItem;
66
import javax.swing.JScrollPane;
67
import javax.swing.JTabbedPane;
68
import javax.swing.JTextField;
69
import javax.swing.JToggleButton;
70
import javax.swing.JToolBar;
71
import javax.swing.KeyStroke;
72
import javax.swing.SingleSelectionModel;
73
import javax.swing.SwingUtilities;
74
import javax.swing.ToolTipManager;
75
import javax.swing.UIManager;
76
import javax.swing.border.EtchedBorder;
77
import javax.swing.event.ChangeEvent;
78
import javax.swing.event.ChangeListener;
79
import javax.swing.plaf.metal.DefaultMetalTheme;
80
import javax.swing.plaf.metal.MetalLookAndFeel;
81
import javax.swing.plaf.metal.MetalTheme;
82
import javax.swing.plaf.metal.OceanTheme;
83
84
/**
85
* A demo that shows all of the Swing components.
86
*
87
* @author Jeff Dinkins
88
*/
89
public class SwingSet2 extends JPanel {
90
91
String[] demos = {
92
"ButtonDemo",
93
"ColorChooserDemo",
94
"ComboBoxDemo",
95
"FileChooserDemo",
96
"HtmlDemo",
97
"ListDemo",
98
"OptionPaneDemo",
99
"ProgressBarDemo",
100
"ScrollPaneDemo",
101
"SliderDemo",
102
"SplitPaneDemo",
103
"TabbedPaneDemo",
104
"TableDemo",
105
"ToolTipDemo",
106
"TreeDemo"
107
};
108
109
// The current Look & Feel
110
private static LookAndFeelData currentLookAndFeel;
111
private static LookAndFeelData[] lookAndFeelData;
112
// List of demos
113
private ArrayList<DemoModule> demosList = new ArrayList<DemoModule>();
114
115
public static final String FRAME_TITLE = getString("Frame.title");
116
public static final String THEMES_MENU_TEXT = getString("ThemesMenu.themes_label");
117
public static final String OPTIONS_MENU_TEXT = getString("OptionsMenu.options_label");
118
public static final String FONT_MENU_TEXT = getString("FontMenu.fonts_label");
119
public static final String FONT_PLAIN_MENU_TEXT = getString("FontMenu.plain_label");
120
public static final String FONT_BOLD_MENU_TEXT = getString("FontMenu.bold_label");
121
public static final String TOOLTIP_MENU_TEXT = getString("OptionsMenu.tooltip_label");
122
public static final String OCEAN_MENU_TEXT = getString("ThemesMenu.ocean_label");
123
public static final String AQUA_MENU_TEXT = getString("ThemesMenu.aqua_label");
124
public static final String STEEL_MENU_TEXT = getString("ThemesMenu.steel_label");
125
public static final String CONTRAST_MENU_TEXT = getString("ThemesMenu.contrast_label");
126
public static final String CHARCOAL_MENU_TEXT = getString("ThemesMenu.charcoal_label");
127
public static final String EMERALD_MENU_TEXT = getString("ThemesMenu.emerald_label");
128
public static final String RUBY_MENU_TEXT= getString("ThemesMenu.ruby_label");
129
public static final String NAME_PROP_SUFFIX = ".name";
130
131
// The preferred size of the demo
132
private static final int PREFERRED_WIDTH = 720;
133
private static final int PREFERRED_HEIGHT = 640;
134
135
// A place to hold on to the visible demo
136
private DemoModule currentDemo = null;
137
private JPanel demoPanel = null;
138
139
// About Box
140
private JDialog aboutBox = null;
141
142
// Status Bar
143
private JTextField statusField = null;
144
145
// Tool Bar
146
private ToggleButtonToolBar toolbar = null;
147
private ButtonGroup toolbarGroup = new ButtonGroup();
148
149
// Menus
150
private JMenuBar menuBar = null;
151
private JMenu lafMenu = null;
152
private JMenu themesMenu = null;
153
private JMenu audioMenu = null;
154
private JMenu optionsMenu = null;
155
private ButtonGroup lafMenuGroup = new ButtonGroup();
156
private ButtonGroup themesMenuGroup = new ButtonGroup();
157
private ButtonGroup audioMenuGroup = new ButtonGroup();
158
159
// Popup menu
160
private JPopupMenu popupMenu = null;
161
private ButtonGroup popupMenuGroup = new ButtonGroup();
162
163
// Used only if swingset is an application
164
private JFrame frame = null;
165
166
// The tab pane that holds the demo
167
private JTabbedPane tabbedPane = null;
168
169
private JEditorPane demoSrcPane = null;
170
171
172
// contentPane cache, saved from the application frame
173
Container contentPane = null;
174
175
176
// number of swingsets - for multiscreen
177
// keep track of the number of SwingSets created - we only want to exit
178
// the program when the last one has been closed.
179
private static int numSSs = 0;
180
private static Vector<SwingSet2> swingSets = new Vector<SwingSet2>();
181
182
private boolean dragEnabled = false;
183
184
/**
185
* SwingSet2 Constructor
186
*/
187
public SwingSet2(GraphicsConfiguration gc) {
188
189
String lafClassName = UIManager.getLookAndFeel().getClass().getName();
190
lookAndFeelData = getInstalledLookAndFeelData();
191
currentLookAndFeel = Arrays.stream(lookAndFeelData)
192
.filter(laf -> lafClassName.equals(laf.className))
193
.findFirst().get();
194
195
frame = createFrame(gc);
196
197
// set the layout
198
setLayout(new BorderLayout());
199
200
// set the preferred size of the demo
201
setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT));
202
203
initializeDemo();
204
preloadFirstDemo();
205
206
showSwingSet2();
207
}
208
209
/**
210
* SwingSet2 Main.
211
*/
212
public static void main(final String[] args) {
213
// must run in EDT when constructing the GUI components
214
SwingUtilities.invokeLater(() -> {
215
// Create SwingSet on the default monitor
216
UIManager.put("swing.boldMetal", Boolean.FALSE);
217
SwingSet2 swingset = new SwingSet2(GraphicsEnvironment.
218
getLocalGraphicsEnvironment().
219
getDefaultScreenDevice().
220
getDefaultConfiguration());
221
});
222
}
223
224
// *******************************************************
225
// *************** Demo Loading Methods ******************
226
// *******************************************************
227
228
229
public void initializeDemo() {
230
JPanel top = new JPanel();
231
top.setLayout(new BorderLayout());
232
add(top, BorderLayout.NORTH);
233
234
menuBar = createMenus();
235
frame.setJMenuBar(menuBar);
236
237
// creates popup menu accessible via keyboard
238
popupMenu = createPopupMenu();
239
240
ToolBarPanel toolbarPanel = new ToolBarPanel();
241
toolbarPanel.setLayout(new BorderLayout());
242
toolbar = new ToggleButtonToolBar();
243
toolbarPanel.add(toolbar, BorderLayout.CENTER);
244
top.add(toolbarPanel, BorderLayout.SOUTH);
245
toolbarPanel.addContainerListener(toolbarPanel);
246
247
tabbedPane = new JTabbedPane();
248
add(tabbedPane, BorderLayout.CENTER);
249
tabbedPane.getModel().addChangeListener(new TabListener());
250
251
statusField = new JTextField("");
252
statusField.setEditable(false);
253
add(statusField, BorderLayout.SOUTH);
254
255
demoPanel = new JPanel();
256
demoPanel.setLayout(new BorderLayout());
257
demoPanel.setBorder(new EtchedBorder());
258
tabbedPane.addTab("Hi There!", demoPanel);
259
260
// Add html src code viewer
261
demoSrcPane = new JEditorPane("text/html", getString("SourceCode.loading"));
262
demoSrcPane.setEditable(false);
263
264
JScrollPane scroller = new JScrollPane();
265
scroller.getViewport().add(demoSrcPane);
266
267
tabbedPane.addTab(
268
getString("TabbedPane.src_label"),
269
null,
270
scroller,
271
getString("TabbedPane.src_tooltip")
272
);
273
}
274
275
DemoModule currentTabDemo = null;
276
class TabListener implements ChangeListener {
277
public void stateChanged(ChangeEvent e) {
278
SingleSelectionModel model = (SingleSelectionModel) e.getSource();
279
boolean srcSelected = model.getSelectedIndex() == 1;
280
if(currentTabDemo != currentDemo && demoSrcPane != null && srcSelected) {
281
demoSrcPane.setText(getString("SourceCode.loading"));
282
repaint();
283
}
284
if(currentTabDemo != currentDemo && srcSelected) {
285
currentTabDemo = currentDemo;
286
setSourceCode(currentDemo);
287
}
288
}
289
}
290
291
/**
292
* Create menus
293
*/
294
public JMenuBar createMenus() {
295
JMenuItem mi;
296
// ***** create the menubar ****
297
JMenuBar menuBar = new JMenuBar();
298
menuBar.getAccessibleContext().setAccessibleName(
299
getString("MenuBar.accessible_description"));
300
301
// ***** create File menu
302
JMenu fileMenu = (JMenu) menuBar.add(new JMenu(getString("FileMenu.file_label")));
303
fileMenu.setMnemonic(getMnemonic("FileMenu.file_mnemonic"));
304
fileMenu.getAccessibleContext().setAccessibleDescription(getString("FileMenu.accessible_description"));
305
306
createMenuItem(fileMenu, "FileMenu.about_label", "FileMenu.about_mnemonic",
307
"FileMenu.about_accessible_description", new AboutAction(this));
308
309
fileMenu.addSeparator();
310
311
createMenuItem(fileMenu, "FileMenu.open_label", "FileMenu.open_mnemonic",
312
"FileMenu.open_accessible_description", null);
313
314
createMenuItem(fileMenu, "FileMenu.save_label", "FileMenu.save_mnemonic",
315
"FileMenu.save_accessible_description", null);
316
317
createMenuItem(fileMenu, "FileMenu.save_as_label", "FileMenu.save_as_mnemonic",
318
"FileMenu.save_as_accessible_description", null);
319
320
fileMenu.addSeparator();
321
322
createMenuItem(fileMenu, "FileMenu.exit_label", "FileMenu.exit_mnemonic",
323
"FileMenu.exit_accessible_description", new ExitAction(this)
324
);
325
326
// ***** create laf switcher menu
327
lafMenu = (JMenu) menuBar.add(new JMenu(getString("LafMenu.laf_label")));
328
lafMenu.setMnemonic(getMnemonic("LafMenu.laf_mnemonic"));
329
lafMenu.getAccessibleContext().setAccessibleDescription(
330
getString("LafMenu.laf_accessible_description"));
331
332
for (LookAndFeelData lafData : lookAndFeelData) {
333
mi = createLafMenuItem(lafMenu, lafData);
334
mi.setSelected(lafData.equals(currentLookAndFeel));
335
}
336
337
// ***** create themes menu
338
themesMenu = (JMenu) menuBar.add(new JMenu(THEMES_MENU_TEXT));
339
themesMenu.setMnemonic(getMnemonic("ThemesMenu.themes_mnemonic"));
340
themesMenu.getAccessibleContext().setAccessibleDescription(
341
getString("ThemesMenu.themes_accessible_description"));
342
343
// ***** create the audio submenu under the theme menu
344
audioMenu = (JMenu) themesMenu.add(new JMenu(getString("AudioMenu.audio_label")));
345
audioMenu.setMnemonic(getMnemonic("AudioMenu.audio_mnemonic"));
346
audioMenu.getAccessibleContext().setAccessibleDescription(
347
getString("AudioMenu.audio_accessible_description"));
348
349
createAudioMenuItem(audioMenu, "AudioMenu.on_label",
350
"AudioMenu.on_mnemonic",
351
"AudioMenu.on_accessible_description",
352
new OnAudioAction(this));
353
354
mi = createAudioMenuItem(audioMenu, "AudioMenu.default_label",
355
"AudioMenu.default_mnemonic",
356
"AudioMenu.default_accessible_description",
357
new DefaultAudioAction(this));
358
mi.setSelected(true); // This is the default feedback setting
359
360
createAudioMenuItem(audioMenu, "AudioMenu.off_label",
361
"AudioMenu.off_mnemonic",
362
"AudioMenu.off_accessible_description",
363
new OffAudioAction(this));
364
365
366
// ***** create the font submenu under the theme menu
367
JMenu fontMenu = (JMenu) themesMenu.add(new JMenu(FONT_MENU_TEXT));
368
fontMenu.setMnemonic(getMnemonic("FontMenu.fonts_mnemonic"));
369
fontMenu.getAccessibleContext().setAccessibleDescription(
370
getString("FontMenu.fonts_accessible_description"));
371
ButtonGroup fontButtonGroup = new ButtonGroup();
372
mi = createButtonGroupMenuItem(fontMenu, FONT_PLAIN_MENU_TEXT,
373
"FontMenu.plain_mnemonic",
374
"FontMenu.plain_accessible_description",
375
new ChangeFontAction(this, true), fontButtonGroup);
376
mi.setSelected(true);
377
mi = createButtonGroupMenuItem(fontMenu, FONT_BOLD_MENU_TEXT,
378
"FontMenu.bold_mnemonic",
379
"FontMenu.bold_accessible_description",
380
new ChangeFontAction(this, false), fontButtonGroup);
381
382
383
384
// *** now back to adding color/font themes to the theme menu
385
mi = createThemesMenuItem(themesMenu, OCEAN_MENU_TEXT,
386
"ThemesMenu.ocean_mnemonic",
387
"ThemesMenu.ocean_accessible_description",
388
new OceanTheme());
389
mi.setSelected(true); // This is the default theme
390
391
createThemesMenuItem(themesMenu, STEEL_MENU_TEXT,
392
"ThemesMenu.steel_mnemonic",
393
"ThemesMenu.steel_accessible_description",
394
new DefaultMetalTheme());
395
396
createThemesMenuItem(themesMenu, AQUA_MENU_TEXT, "ThemesMenu.aqua_mnemonic",
397
"ThemesMenu.aqua_accessible_description", new AquaTheme());
398
399
createThemesMenuItem(themesMenu, CHARCOAL_MENU_TEXT, "ThemesMenu.charcoal_mnemonic",
400
"ThemesMenu.charcoal_accessible_description", new CharcoalTheme());
401
402
createThemesMenuItem(themesMenu, CONTRAST_MENU_TEXT, "ThemesMenu.contrast_mnemonic",
403
"ThemesMenu.contrast_accessible_description", new ContrastTheme());
404
405
createThemesMenuItem(themesMenu, EMERALD_MENU_TEXT, "ThemesMenu.emerald_mnemonic",
406
"ThemesMenu.emerald_accessible_description", new EmeraldTheme());
407
408
createThemesMenuItem(themesMenu, RUBY_MENU_TEXT, "ThemesMenu.ruby_mnemonic",
409
"ThemesMenu.ruby_accessible_description", new RubyTheme());
410
411
// Enable theme menu based on L&F
412
themesMenu.setEnabled("Metal".equals(currentLookAndFeel.name));
413
414
// ***** create the options menu
415
optionsMenu = (JMenu)menuBar.add(
416
new JMenu(OPTIONS_MENU_TEXT));
417
optionsMenu.setMnemonic(getMnemonic("OptionsMenu.options_mnemonic"));
418
optionsMenu.getAccessibleContext().setAccessibleDescription(
419
getString("OptionsMenu.options_accessible_description"));
420
421
// ***** create tool tip submenu item.
422
mi = createCheckBoxMenuItem(optionsMenu, TOOLTIP_MENU_TEXT,
423
"OptionsMenu.tooltip_mnemonic",
424
"OptionsMenu.tooltip_accessible_description",
425
new ToolTipAction());
426
mi.setSelected(true);
427
428
// ***** create drag support submenu item.
429
createCheckBoxMenuItem(optionsMenu, getString("OptionsMenu.dragEnabled_label"),
430
"OptionsMenu.dragEnabled_mnemonic",
431
"OptionsMenu.dragEnabled_accessible_description",
432
new DragSupportAction());
433
434
// ***** create the multiscreen menu, if we have multiple screens
435
GraphicsDevice[] screens = GraphicsEnvironment.
436
getLocalGraphicsEnvironment().
437
getScreenDevices();
438
if (screens.length > 1) {
439
JMenu multiScreenMenu = (JMenu) menuBar.add(new JMenu(
440
getString("MultiMenu.multi_label")));
441
multiScreenMenu.setMnemonic(getMnemonic("MultiMenu.multi_mnemonic"));
442
multiScreenMenu.getAccessibleContext().setAccessibleDescription(
443
getString("MultiMenu.multi_accessible_description"));
444
445
createMultiscreenMenuItem(multiScreenMenu, MultiScreenAction.ALL_SCREENS);
446
for (int i = 0; i < screens.length; i++) {
447
createMultiscreenMenuItem(multiScreenMenu, i);
448
}
449
}
450
return menuBar;
451
}
452
453
/**
454
* Create a checkbox menu menu item
455
*/
456
private JMenuItem createCheckBoxMenuItem(JMenu menu, String label,
457
String mnemonic,
458
String accessibleDescription,
459
Action action) {
460
JCheckBoxMenuItem mi = (JCheckBoxMenuItem)menu.add(
461
new JCheckBoxMenuItem(label));
462
mi.setMnemonic(getMnemonic(mnemonic));
463
mi.getAccessibleContext().setAccessibleDescription(getString(
464
accessibleDescription));
465
mi.addActionListener(action);
466
return mi;
467
}
468
469
/**
470
* Create a radio button menu menu item for items that are part of a
471
* button group.
472
*/
473
private JMenuItem createButtonGroupMenuItem(JMenu menu, String label,
474
String mnemonic,
475
String accessibleDescription,
476
Action action,
477
ButtonGroup buttonGroup) {
478
JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add(
479
new JRadioButtonMenuItem(label));
480
buttonGroup.add(mi);
481
mi.setMnemonic(getMnemonic(mnemonic));
482
mi.getAccessibleContext().setAccessibleDescription(getString(
483
accessibleDescription));
484
mi.addActionListener(action);
485
return mi;
486
}
487
488
/**
489
* Create the theme's audio submenu
490
*/
491
public JMenuItem createAudioMenuItem(JMenu menu, String label,
492
String mnemonic,
493
String accessibleDescription,
494
Action action) {
495
JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
496
audioMenuGroup.add(mi);
497
mi.setMnemonic(getMnemonic(mnemonic));
498
mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
499
mi.addActionListener(action);
500
return mi;
501
}
502
503
/**
504
* Creates a generic menu item
505
*/
506
public JMenuItem createMenuItem(JMenu menu, String label, String mnemonic,
507
String accessibleDescription, Action action) {
508
JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(getString(label)));
509
mi.setMnemonic(getMnemonic(mnemonic));
510
mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
511
mi.addActionListener(action);
512
if(action == null) {
513
mi.setEnabled(false);
514
}
515
return mi;
516
}
517
518
/**
519
* Creates a JRadioButtonMenuItem for the Themes menu
520
*/
521
public JMenuItem createThemesMenuItem(JMenu menu, String label, String mnemonic,
522
String accessibleDescription, MetalTheme theme) {
523
JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(label));
524
themesMenuGroup.add(mi);
525
mi.setMnemonic(getMnemonic(mnemonic));
526
mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
527
mi.addActionListener(new ChangeThemeAction(this, theme));
528
return mi;
529
}
530
531
/**
532
* Creates a JRadioButtonMenuItem for the Look and Feel menu
533
*/
534
public JMenuItem createLafMenuItem(JMenu menu, LookAndFeelData lafData) {
535
JMenuItem mi = menu.add(new JRadioButtonMenuItem(lafData.label));
536
lafMenuGroup.add(mi);
537
mi.setMnemonic(lafData.mnemonic);
538
mi.getAccessibleContext().setAccessibleDescription(lafData.accDescription);
539
mi.addActionListener(new ChangeLookAndFeelAction(this, lafData));
540
return mi;
541
}
542
543
/**
544
* Creates a multi-screen menu item
545
*/
546
public JMenuItem createMultiscreenMenuItem(JMenu menu, int screen) {
547
JMenuItem mi = null;
548
if (screen == MultiScreenAction.ALL_SCREENS) {
549
mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.all_label")));
550
mi.setMnemonic(getMnemonic("MultiMenu.all_mnemonic"));
551
mi.getAccessibleContext().setAccessibleDescription(getString(
552
"MultiMenu.all_accessible_description"));
553
}
554
else {
555
mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.single_label") + " " +
556
screen));
557
mi.setMnemonic(KeyEvent.VK_0 + screen);
558
mi.getAccessibleContext().setAccessibleDescription(getString(
559
"MultiMenu.single_accessible_description") + " " + screen);
560
561
}
562
mi.addActionListener(new MultiScreenAction(this, screen));
563
return mi;
564
}
565
566
public JPopupMenu createPopupMenu() {
567
JPopupMenu popup = new JPopupMenu("JPopupMenu demo");
568
569
for (LookAndFeelData lafData : lookAndFeelData) {
570
createPopupMenuItem(popup, lafData);
571
}
572
573
// register key binding to activate popup menu
574
InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
575
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, InputEvent.SHIFT_MASK),
576
"postMenuAction");
577
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTEXT_MENU, 0), "postMenuAction");
578
getActionMap().put("postMenuAction", new ActivatePopupMenuAction(this, popup));
579
580
return popup;
581
}
582
583
/**
584
* Creates a JMenuItem for the Look and Feel popup menu
585
*/
586
public JMenuItem createPopupMenuItem(JPopupMenu menu, LookAndFeelData lafData) {
587
JMenuItem mi = menu.add(new JMenuItem(lafData.label));
588
popupMenuGroup.add(mi);
589
mi.setMnemonic(lafData.mnemonic);
590
mi.getAccessibleContext().setAccessibleDescription(lafData.accDescription);
591
mi.addActionListener(new ChangeLookAndFeelAction(this, lafData));
592
return mi;
593
}
594
595
/**
596
* Load the first demo. This is done separately from the remaining demos
597
* so that we can get SwingSet2 up and available to the user quickly.
598
*/
599
public void preloadFirstDemo() {
600
DemoModule demo = addDemo(new OptionPaneDemo(this));
601
setDemo(demo);
602
}
603
604
/**
605
* Add a demo to the toolbar
606
*/
607
public DemoModule addDemo(DemoModule demo) {
608
demosList.add(demo);
609
if (dragEnabled) {
610
demo.updateDragEnabled(true);
611
}
612
// do the following on the gui thread
613
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
614
public void run() {
615
SwitchToDemoAction action = new SwitchToDemoAction(swingset, (DemoModule) obj);
616
JToggleButton tb = swingset.getToolBar().addToggleButton(action);
617
swingset.getToolBarGroup().add(tb);
618
if(swingset.getToolBarGroup().getSelection() == null) {
619
tb.setSelected(true);
620
}
621
tb.setText(null);
622
tb.setToolTipText(((DemoModule)obj).getToolTip());
623
624
if(demos[demos.length-1].equals(obj.getClass().getName())) {
625
setStatus(getString("Status.popupMenuAccessible"));
626
}
627
}
628
});
629
return demo;
630
}
631
632
/**
633
* Sets the current demo
634
*/
635
public void setDemo(DemoModule demo) {
636
currentDemo = demo;
637
638
// Ensure panel's UI is current before making visible
639
JComponent currentDemoPanel = demo.getDemoPanel();
640
SwingUtilities.updateComponentTreeUI(currentDemoPanel);
641
642
demoPanel.removeAll();
643
demoPanel.add(currentDemoPanel, BorderLayout.CENTER);
644
645
tabbedPane.setSelectedIndex(0);
646
tabbedPane.setTitleAt(0, demo.getName());
647
tabbedPane.setToolTipTextAt(0, demo.getToolTip());
648
}
649
650
/**
651
* Bring up the SwingSet2 demo by showing the frame
652
*/
653
public void showSwingSet2() {
654
if(getFrame() != null) {
655
// put swingset in a frame and show it
656
JFrame f = getFrame();
657
f.setTitle(FRAME_TITLE);
658
f.getContentPane().add(this, BorderLayout.CENTER);
659
f.pack();
660
661
Rectangle screenRect = f.getGraphicsConfiguration().getBounds();
662
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(
663
f.getGraphicsConfiguration());
664
665
// Make sure we don't place the demo off the screen.
666
int centerWidth = screenRect.width < f.getSize().width ?
667
screenRect.x :
668
screenRect.x + screenRect.width/2 - f.getSize().width/2;
669
int centerHeight = screenRect.height < f.getSize().height ?
670
screenRect.y :
671
screenRect.y + screenRect.height/2 - f.getSize().height/2;
672
673
centerHeight = centerHeight < screenInsets.top ?
674
screenInsets.top : centerHeight;
675
676
f.setLocation(centerWidth, centerHeight);
677
f.show();
678
numSSs++;
679
swingSets.add(this);
680
}
681
}
682
683
// *******************************************************
684
// ****************** Utility Methods ********************
685
// *******************************************************
686
687
/**
688
* Returns the frame instance
689
*/
690
public JFrame getFrame() {
691
return frame;
692
}
693
694
/**
695
* Returns the toolbar
696
*/
697
public ToggleButtonToolBar getToolBar() {
698
return toolbar;
699
}
700
701
/**
702
* Returns the toolbar button group
703
*/
704
public ButtonGroup getToolBarGroup() {
705
return toolbarGroup;
706
}
707
708
/**
709
* Create a frame for SwingSet2 to reside in if brought up
710
* as an application.
711
*/
712
public static JFrame createFrame(GraphicsConfiguration gc) {
713
JFrame frame = new JFrame(gc);
714
if (numSSs == 0) {
715
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
716
} else {
717
WindowListener l = new WindowAdapter() {
718
public void windowClosing(WindowEvent e) {
719
numSSs--;
720
swingSets.remove(this);
721
}
722
};
723
frame.addWindowListener(l);
724
}
725
return frame;
726
}
727
728
/**
729
* Set the status
730
*/
731
public void setStatus(String s) {
732
// do the following on the gui thread
733
SwingUtilities.invokeLater(new SwingSetRunnable(this, s) {
734
public void run() {
735
swingset.statusField.setText((String) obj);
736
}
737
});
738
}
739
740
/**
741
* This method returns a string from the demo's resource bundle.
742
*/
743
public static String getString(String key) {
744
String value = null;
745
try {
746
value = TextAndMnemonicUtils.getTextAndMnemonicString(key);
747
} catch (MissingResourceException e) {
748
System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
749
}
750
if(value == null) {
751
value = "Could not find resource: " + key + " ";
752
}
753
return value;
754
}
755
756
void setDragEnabled(boolean dragEnabled) {
757
if (dragEnabled == this.dragEnabled) {
758
return;
759
}
760
761
this.dragEnabled = dragEnabled;
762
763
for (DemoModule dm : demosList) {
764
dm.updateDragEnabled(dragEnabled);
765
}
766
767
demoSrcPane.setDragEnabled(dragEnabled);
768
}
769
770
/**
771
* Returns a mnemonic from the resource bundle. Typically used as
772
* keyboard shortcuts in menu items.
773
*/
774
public char getMnemonic(String key) {
775
return (getString(key)).charAt(0);
776
}
777
778
/**
779
* Creates an icon from an image contained in the "images" directory.
780
*/
781
public ImageIcon createImageIcon(String filename, String description) {
782
String path = "/resources/images/" + filename;
783
return new ImageIcon(getClass().getResource(path));
784
}
785
786
/**
787
* Stores the current L&F, and calls updateLookAndFeel, below
788
*/
789
public void setLookAndFeel(LookAndFeelData laf) {
790
if(!currentLookAndFeel.equals(laf)) {
791
currentLookAndFeel = laf;
792
/* The recommended way of synchronizing state between multiple
793
* controls that represent the same command is to use Actions.
794
* The code below is a workaround and will be replaced in future
795
* version of SwingSet2 demo.
796
*/
797
String lafName = laf.label;
798
themesMenu.setEnabled(laf.name.equals("Metal"));
799
updateLookAndFeel();
800
for(int i=0;i<lafMenu.getItemCount();i++) {
801
JMenuItem item = lafMenu.getItem(i);
802
item.setSelected(item.getText().equals(lafName));
803
}
804
}
805
}
806
807
private void updateThisSwingSet() {
808
JFrame frame = getFrame();
809
if (frame == null) {
810
SwingUtilities.updateComponentTreeUI(this);
811
} else {
812
SwingUtilities.updateComponentTreeUI(frame);
813
}
814
815
SwingUtilities.updateComponentTreeUI(popupMenu);
816
if (aboutBox != null) {
817
SwingUtilities.updateComponentTreeUI(aboutBox);
818
}
819
}
820
821
/**
822
* Sets the current L&F on each demo module
823
*/
824
public void updateLookAndFeel() {
825
try {
826
UIManager.setLookAndFeel(currentLookAndFeel.className);
827
for (SwingSet2 ss : swingSets) {
828
ss.updateThisSwingSet();
829
}
830
} catch (Exception ex) {
831
System.out.println("Failed loading L&F: " + currentLookAndFeel);
832
System.out.println(ex);
833
}
834
}
835
836
/**
837
* Loads and puts the source code text into JEditorPane in the "Source Code" tab
838
*/
839
public void setSourceCode(DemoModule demo) {
840
// do the following on the gui thread
841
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
842
public void run() {
843
swingset.demoSrcPane.setText(((DemoModule)obj).getSourceCode());
844
swingset.demoSrcPane.setCaretPosition(0);
845
846
}
847
});
848
}
849
850
// *******************************************************
851
// ************** ToggleButtonToolbar *****************
852
// *******************************************************
853
static Insets zeroInsets = new Insets(1,1,1,1);
854
protected class ToggleButtonToolBar extends JToolBar {
855
public ToggleButtonToolBar() {
856
super();
857
}
858
859
JToggleButton addToggleButton(Action a) {
860
JToggleButton tb = new JToggleButton(
861
(String)a.getValue(Action.NAME),
862
(Icon)a.getValue(Action.SMALL_ICON)
863
);
864
tb.setName((String)a.getValue(Action.NAME));
865
tb.setMargin(zeroInsets);
866
tb.setText(null);
867
tb.setEnabled(a.isEnabled());
868
tb.setToolTipText((String)a.getValue(Action.SHORT_DESCRIPTION));
869
tb.setAction(a);
870
add(tb);
871
return tb;
872
}
873
}
874
875
// *******************************************************
876
// ********* ToolBar Panel / Docking Listener ***********
877
// *******************************************************
878
class ToolBarPanel extends JPanel implements ContainerListener {
879
880
public boolean contains(int x, int y) {
881
Component c = getParent();
882
if (c != null) {
883
Rectangle r = c.getBounds();
884
return (x >= 0) && (x < r.width) && (y >= 0) && (y < r.height);
885
}
886
else {
887
return super.contains(x,y);
888
}
889
}
890
891
public void componentAdded(ContainerEvent e) {
892
Container c = e.getContainer().getParent();
893
if (c != null) {
894
c.getParent().validate();
895
c.getParent().repaint();
896
}
897
}
898
899
public void componentRemoved(ContainerEvent e) {
900
Container c = e.getContainer().getParent();
901
if (c != null) {
902
c.getParent().validate();
903
c.getParent().repaint();
904
}
905
}
906
}
907
908
// *******************************************************
909
// ****************** Runnables ***********************
910
// *******************************************************
911
912
/**
913
* Generic SwingSet2 runnable. This is intended to run on the
914
* AWT gui event thread so as not to muck things up by doing
915
* gui work off the gui thread. Accepts a SwingSet2 and an Object
916
* as arguments, which gives subtypes of this class the two
917
* "must haves" needed in most runnables for this demo.
918
*/
919
class SwingSetRunnable implements Runnable {
920
protected SwingSet2 swingset;
921
protected Object obj;
922
923
public SwingSetRunnable(SwingSet2 swingset, Object obj) {
924
this.swingset = swingset;
925
this.obj = obj;
926
}
927
928
public void run() {
929
}
930
}
931
932
933
// *******************************************************
934
// ******************** Actions ***********************
935
// *******************************************************
936
937
public class SwitchToDemoAction extends AbstractAction {
938
SwingSet2 swingset;
939
DemoModule demo;
940
941
public SwitchToDemoAction(SwingSet2 swingset, DemoModule demo) {
942
super(demo.getName(), demo.getIcon());
943
this.swingset = swingset;
944
this.demo = demo;
945
}
946
947
public void actionPerformed(ActionEvent e) {
948
swingset.setDemo(demo);
949
}
950
}
951
952
class OkAction extends AbstractAction {
953
JDialog aboutBox;
954
955
protected OkAction(JDialog aboutBox) {
956
super("OkAction");
957
this.aboutBox = aboutBox;
958
}
959
960
public void actionPerformed(ActionEvent e) {
961
aboutBox.setVisible(false);
962
}
963
}
964
965
class ChangeLookAndFeelAction extends AbstractAction {
966
SwingSet2 swingset;
967
LookAndFeelData lafData;
968
protected ChangeLookAndFeelAction(SwingSet2 swingset, LookAndFeelData lafData) {
969
super("ChangeTheme");
970
this.swingset = swingset;
971
this.lafData = lafData;
972
}
973
974
public void actionPerformed(ActionEvent e) {
975
swingset.setLookAndFeel(lafData);
976
}
977
}
978
979
class ActivatePopupMenuAction extends AbstractAction {
980
SwingSet2 swingset;
981
JPopupMenu popup;
982
protected ActivatePopupMenuAction(SwingSet2 swingset, JPopupMenu popup) {
983
super("ActivatePopupMenu");
984
this.swingset = swingset;
985
this.popup = popup;
986
}
987
988
public void actionPerformed(ActionEvent e) {
989
Dimension invokerSize = getSize();
990
Dimension popupSize = popup.getPreferredSize();
991
popup.show(swingset, (invokerSize.width - popupSize.width) / 2,
992
(invokerSize.height - popupSize.height) / 2);
993
}
994
}
995
996
// Turns on all possible auditory feedback
997
class OnAudioAction extends AbstractAction {
998
SwingSet2 swingset;
999
protected OnAudioAction(SwingSet2 swingset) {
1000
super("Audio On");
1001
this.swingset = swingset;
1002
}
1003
public void actionPerformed(ActionEvent e) {
1004
UIManager.put("AuditoryCues.playList",
1005
UIManager.get("AuditoryCues.allAuditoryCues"));
1006
swingset.updateLookAndFeel();
1007
}
1008
}
1009
1010
// Turns on the default amount of auditory feedback
1011
class DefaultAudioAction extends AbstractAction {
1012
SwingSet2 swingset;
1013
protected DefaultAudioAction(SwingSet2 swingset) {
1014
super("Audio Default");
1015
this.swingset = swingset;
1016
}
1017
public void actionPerformed(ActionEvent e) {
1018
UIManager.put("AuditoryCues.playList",
1019
UIManager.get("AuditoryCues.defaultCueList"));
1020
swingset.updateLookAndFeel();
1021
}
1022
}
1023
1024
// Turns off all possible auditory feedback
1025
class OffAudioAction extends AbstractAction {
1026
SwingSet2 swingset;
1027
protected OffAudioAction(SwingSet2 swingset) {
1028
super("Audio Off");
1029
this.swingset = swingset;
1030
}
1031
public void actionPerformed(ActionEvent e) {
1032
UIManager.put("AuditoryCues.playList",
1033
UIManager.get("AuditoryCues.noAuditoryCues"));
1034
swingset.updateLookAndFeel();
1035
}
1036
}
1037
1038
// Turns on or off the tool tips for the demo.
1039
class ToolTipAction extends AbstractAction {
1040
protected ToolTipAction() {
1041
super("ToolTip Control");
1042
}
1043
1044
public void actionPerformed(ActionEvent e) {
1045
boolean status = ((JCheckBoxMenuItem)e.getSource()).isSelected();
1046
ToolTipManager.sharedInstance().setEnabled(status);
1047
}
1048
}
1049
1050
class DragSupportAction extends AbstractAction {
1051
protected DragSupportAction() {
1052
super("DragSupport Control");
1053
}
1054
1055
public void actionPerformed(ActionEvent e) {
1056
boolean dragEnabled = ((JCheckBoxMenuItem)e.getSource()).isSelected();
1057
for (SwingSet2 ss : swingSets) {
1058
ss.setDragEnabled(dragEnabled);
1059
}
1060
}
1061
}
1062
1063
class ChangeThemeAction extends AbstractAction {
1064
SwingSet2 swingset;
1065
MetalTheme theme;
1066
protected ChangeThemeAction(SwingSet2 swingset, MetalTheme theme) {
1067
super("ChangeTheme");
1068
this.swingset = swingset;
1069
this.theme = theme;
1070
}
1071
1072
public void actionPerformed(ActionEvent e) {
1073
MetalLookAndFeel.setCurrentTheme(theme);
1074
swingset.updateLookAndFeel();
1075
}
1076
}
1077
1078
class ExitAction extends AbstractAction {
1079
SwingSet2 swingset;
1080
protected ExitAction(SwingSet2 swingset) {
1081
super("ExitAction");
1082
this.swingset = swingset;
1083
}
1084
1085
public void actionPerformed(ActionEvent e) {
1086
System.exit(0);
1087
}
1088
}
1089
1090
class AboutAction extends AbstractAction {
1091
SwingSet2 swingset;
1092
protected AboutAction(SwingSet2 swingset) {
1093
super("AboutAction");
1094
this.swingset = swingset;
1095
}
1096
1097
public void actionPerformed(ActionEvent e) {
1098
if(aboutBox == null) {
1099
// JPanel panel = new JPanel(new BorderLayout());
1100
JPanel panel = new AboutPanel(swingset);
1101
panel.setLayout(new BorderLayout());
1102
1103
aboutBox = new JDialog(swingset.getFrame(), getString("AboutBox.title"), false);
1104
aboutBox.setResizable(false);
1105
aboutBox.getContentPane().add(panel, BorderLayout.CENTER);
1106
1107
// JButton button = new JButton(getString("AboutBox.ok_button_text"));
1108
JPanel buttonpanel = new JPanel();
1109
buttonpanel.setBorder(new javax.swing.border.EmptyBorder(0, 0, 3, 0));
1110
buttonpanel.setOpaque(false);
1111
JButton button = (JButton) buttonpanel.add(
1112
new JButton(getString("AboutBox.ok_button_text"))
1113
);
1114
panel.add(buttonpanel, BorderLayout.SOUTH);
1115
1116
button.addActionListener(new OkAction(aboutBox));
1117
}
1118
aboutBox.pack();
1119
aboutBox.setLocationRelativeTo(getFrame());
1120
aboutBox.show();
1121
}
1122
}
1123
1124
class MultiScreenAction extends AbstractAction {
1125
static final int ALL_SCREENS = -1;
1126
int screen;
1127
protected MultiScreenAction(SwingSet2 swingset, int screen) {
1128
super("MultiScreenAction");
1129
this.screen = screen;
1130
}
1131
1132
public void actionPerformed(ActionEvent e) {
1133
GraphicsDevice[] gds = GraphicsEnvironment.
1134
getLocalGraphicsEnvironment().
1135
getScreenDevices();
1136
if (screen == ALL_SCREENS) {
1137
for (int i = 0; i < gds.length; i++) {
1138
SwingSet2 swingset = new SwingSet2(
1139
gds[i].getDefaultConfiguration());
1140
swingset.setDragEnabled(dragEnabled);
1141
}
1142
}
1143
else {
1144
SwingSet2 swingset = new SwingSet2(
1145
gds[screen].getDefaultConfiguration());
1146
swingset.setDragEnabled(dragEnabled);
1147
}
1148
}
1149
}
1150
1151
// *******************************************************
1152
// ********************** Misc *************************
1153
// *******************************************************
1154
1155
class AboutPanel extends JPanel {
1156
ImageIcon aboutimage = null;
1157
SwingSet2 swingset = null;
1158
1159
public AboutPanel(SwingSet2 swingset) {
1160
this.swingset = swingset;
1161
aboutimage = swingset.createImageIcon("About.jpg", "AboutBox.accessible_description");
1162
setOpaque(false);
1163
}
1164
1165
public void paint(Graphics g) {
1166
aboutimage.paintIcon(this, g, 0, 0);
1167
super.paint(g);
1168
}
1169
1170
public Dimension getPreferredSize() {
1171
return new Dimension(aboutimage.getIconWidth(),
1172
aboutimage.getIconHeight());
1173
}
1174
}
1175
1176
private class ChangeFontAction extends AbstractAction {
1177
private SwingSet2 swingset;
1178
private boolean plain;
1179
1180
ChangeFontAction(SwingSet2 swingset, boolean plain) {
1181
super("FontMenu");
1182
this.swingset = swingset;
1183
this.plain = plain;
1184
}
1185
1186
public void actionPerformed(ActionEvent e) {
1187
if (plain) {
1188
UIManager.put("swing.boldMetal", Boolean.FALSE);
1189
}
1190
else {
1191
UIManager.put("swing.boldMetal", Boolean.TRUE);
1192
}
1193
// Change the look and feel to force the settings to take effect.
1194
updateLookAndFeel();
1195
}
1196
}
1197
1198
private static LookAndFeelData[] getInstalledLookAndFeelData() {
1199
return Arrays.stream(UIManager.getInstalledLookAndFeels())
1200
.map(laf -> getLookAndFeelData(laf))
1201
.toArray(LookAndFeelData[]::new);
1202
}
1203
1204
private static LookAndFeelData getLookAndFeelData(
1205
UIManager.LookAndFeelInfo info) {
1206
switch (info.getName()) {
1207
case "Metal":
1208
return new LookAndFeelData(info, "java");
1209
case "Nimbus":
1210
return new LookAndFeelData(info, "nimbus");
1211
case "Windows":
1212
return new LookAndFeelData(info, "windows");
1213
case "GTK+":
1214
return new LookAndFeelData(info, "gtk");
1215
case "CDE/Motif":
1216
return new LookAndFeelData(info, "motif");
1217
case "Mac OS X":
1218
return new LookAndFeelData(info, "mac");
1219
default:
1220
return new LookAndFeelData(info);
1221
}
1222
}
1223
1224
private static class LookAndFeelData {
1225
String name;
1226
String className;
1227
String label;
1228
char mnemonic;
1229
String accDescription;
1230
1231
public LookAndFeelData(UIManager.LookAndFeelInfo info) {
1232
this(info.getName(), info.getClassName(), info.getName(),
1233
info.getName(), info.getName());
1234
}
1235
1236
public LookAndFeelData(UIManager.LookAndFeelInfo info, String property) {
1237
this(info.getName(), info.getClassName(),
1238
getString(String.format("LafMenu.%s_label", property)),
1239
getString(String.format("LafMenu.%s_mnemonic", property)),
1240
getString(String.format("LafMenu.%s_accessible_description",
1241
property)));
1242
}
1243
1244
public LookAndFeelData(String name, String className, String label,
1245
String mnemonic, String accDescription) {
1246
this.name = name;
1247
this.className = className;
1248
this.label = label;
1249
this.mnemonic = mnemonic.charAt(0);
1250
this.accDescription = accDescription;
1251
}
1252
1253
@Override
1254
public String toString() {
1255
return className;
1256
}
1257
}
1258
}
1259
1260