OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [swing/] [Demo.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* SwingDemo.java -- An example of using the javax.swing UI.
2
   Copyright (C) 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath examples.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
*/
21
 
22
 
23
package gnu.classpath.examples.swing;
24
 
25
import gnu.classpath.examples.java2d.JNIOverhead;
26
 
27
import java.awt.*;
28
import java.awt.event.*;
29
 
30
import javax.swing.*;
31
 
32
import javax.swing.plaf.basic.BasicLookAndFeel;
33
import javax.swing.plaf.metal.DefaultMetalTheme;
34
import javax.swing.plaf.metal.MetalLookAndFeel;
35
import javax.swing.plaf.metal.MetalTheme;
36
import javax.swing.plaf.metal.OceanTheme;
37
 
38
import java.lang.reflect.Method;
39
import java.net.URL;
40
 
41
public class Demo
42
{
43
  JFrame frame;
44
 
45
  /**
46
   * The main desktop. This is package private to avoid synthetic accessor
47
   * method.
48
   */
49
  JDesktopPane desktop;
50
 
51
  /**
52
   * The themes menu. This is implemented as a field so that the L&F switcher
53
   * can disable the menu when a non-Metal L&F is selected.
54
   */
55
  JMenu themesMenu;
56
 
57
  static Color blueGray = new Color(0xdc, 0xda, 0xd5);
58
 
59
  private static Icon stockIcon(String s)
60
  {
61
    return getIcon("/gnu/classpath/examples/icons/stock-" + s + ".png", s);
62
  }
63
 
64
  static Icon bigStockIcon(String s)
65
  {
66
    return getIcon("/gnu/classpath/examples/icons/big-" + s + ".png", s);
67
  }
68
 
69
  static Icon getIcon(String location, String name)
70
  {
71
    URL url = Demo.class.getResource(location);
72
    if (url == null) System.err.println("WARNING " + location + " not found.");
73
    return new ImageIcon(url, name);
74
  }
75
 
76
  private JMenuBar mkMenuBar()
77
  {
78
    JMenuBar bar = new JMenuBar();
79
 
80
    JMenu file = new JMenu("File");
81
    JMenu edit = new JMenu("Edit");
82
    JMenu help = new JMenu("Help");
83
 
84
    file.setMnemonic(KeyEvent.VK_F);
85
    edit.setMnemonic(KeyEvent.VK_E);
86
    help.setMnemonic(KeyEvent.VK_H);
87
 
88
    file.add(new JMenuItem("New", stockIcon("new")));
89
    file.add(new JMenuItem("Open", stockIcon("open")));
90
 
91
    JMenu recent = new JMenu("Recent Files...");
92
    recent.add(new JMenuItem("war-and-peace.txt"));
93
    recent.add(new JMenuItem("taming-of-shrew.txt"));
94
    recent.add(new JMenuItem("sun-also-rises.txt"));
95
    file.add(recent);
96
    file.add(new JMenuItem("Save", stockIcon("save")));
97
    file.add(new JMenuItem("Save as...", stockIcon("save-as")));
98
 
99
    JMenuItem exit = new JMenuItem("Exit", stockIcon("quit"));
100
    exit.addActionListener(new ActionListener()
101
      {
102
        public void actionPerformed(ActionEvent e)
103
        {
104
          System.exit(1);
105
        }
106
      });
107
 
108
    file.add(exit);
109
 
110
    edit.add(new JMenuItem("Cut", stockIcon("cut")));
111
    edit.add(new JMenuItem("Copy", stockIcon("copy")));
112
    edit.add(new JMenuItem("Paste", stockIcon("paste")));
113
 
114
    JMenu preferences = new JMenu("Preferences...");
115
    preferences.add(new JCheckBoxMenuItem("Microphone Active",
116
                    stockIcon("mic")));
117
    preferences.add(new JCheckBoxMenuItem("Check Spelling",
118
                    stockIcon("spell-check")));
119
    preferences.add(new JCheckBoxMenuItem("World Peace"));
120
    preferences.add(new JSeparator());
121
    preferences.add(new JRadioButtonMenuItem("Radio Button"));
122
    edit.add(preferences);
123
 
124
    JMenu examples = new JMenu("Examples");
125
    examples.add(new JMenuItem(new PopupAction("Buttons",
126
                                             ButtonDemo.createDemoFactory())));
127
    examples.add(new JMenuItem(new PopupAction("Slider",
128
                                             SliderDemo.createDemoFactory())));
129
    examples.add(new JMenuItem(new PopupAction("ProgressBar",
130
                                        ProgressBarDemo.createDemoFactory())));
131
    examples.add(new JMenuItem(new PopupAction("Scrollbar",
132
                                          ScrollBarDemo.createDemoFactory())));
133
    examples.add(new JMenuItem(new PopupAction("Spinner",
134
                                            SpinnerDemo.createDemoFactory())));
135
    examples.add(new JMenuItem(new PopupAction("TextField",
136
                                          TextFieldDemo.createDemoFactory())));
137
    examples.add(new JMenuItem(new PopupAction("TextArea",
138
                                           TextAreaDemo.createDemoFactory())));
139
    examples.add(new JMenuItem(new PopupAction("FileChooser",
140
                                        FileChooserDemo.createDemoFactory())));
141
 
142
    examples.add(new JMenuItem(new PopupAction("ComboBox",
143
                                           ComboBoxDemo.createDemoFactory())));
144
 
145
    examples.add(new JMenuItem(new PopupAction("Table",
146
                                              TableDemo.createDemoFactory())));
147
    examples.add(new JMenuItem(new PopupAction("List",
148
                                               ListDemo.createDemoFactory())));
149
    examples.add(new JMenuItem(new PopupAction("TabbedPane",
150
                                         TabbedPaneDemo.createDemoFactory())));
151
    examples.add(new JMenuItem(new PopupAction("Tree",
152
                                               TreeDemo.createDemoFactory())));
153
    examples.add(new JMenuItem(new PopupAction("Theme Editor",
154
                                       MetalThemeEditor.createDemoFactory())));
155
 
156
    examples.add(new JMenuItem(new PopupAction("DocumentFilter",
157
                                     DocumentFilterDemo.createDemoFactory())));
158
 
159
    examples.add(new JMenuItem(new PopupAction("NavigationFilter",
160
                                               NavigationFilterDemo.createDemoFactory())));
161
    examples.add(new JMenuItem(new PopupAction("JNI Overhead",
162
                                               JNIOverhead.createDemoFactory())));
163
    examples.add(new JMenuItem(new PopupAction("HTML Demo",
164
                                               HtmlDemo.createDemoFactory())));
165
 
166
 
167
    final JMenuItem vmMenu;
168
 
169
    help.add(new JMenuItem("just play with the widgets"));
170
    help.add(new JMenuItem("and enjoy the sensation of"));
171
    help.add(new JMenuItem("your neural connections growing"));
172
    help.add(new JSeparator());
173
    help.add(vmMenu = new JMenuItem("Really, which VM is this running on?"));
174
    vmMenu.addActionListener(new ActionListener()
175
      {
176
        public void actionPerformed(ActionEvent ae)
177
          {
178
            String message = "This is "
179
                             + System.getProperty("java.vm.name")
180
                             + " Version "
181
                             + System.getProperty("java.vm.version")
182
                             + " distributed by "
183
                             + System.getProperty("java.vm.vendor")
184
                             + ".";
185
 
186
            String gnuClasspath = System.getProperty("gnu.classpath.version");
187
            if(gnuClasspath != null)
188
              message += "\nThe runtime's libraries are "
189
                         + "kindly provided by the "
190
                         + "members of GNU Classpath and are in version "
191
                         + gnuClasspath + ".";
192
 
193
                         JOptionPane.showMessageDialog(vmMenu, message);
194
            }
195
      });
196
 
197
    // Installs the BasicLookAndFeel.
198
    UIManager.installLookAndFeel("(Basic Look And Feel)",
199
                                 InstantiableBasicLookAndFeel.class.getName());
200
 
201
    // Create L&F menu.
202
    JMenu lafMenu = new JMenu("Look and Feel");
203
    ButtonGroup lafGroup = new ButtonGroup();
204
    UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
205
    String currentLaf = UIManager.getLookAndFeel().getClass().getName();
206
    for (int i = 0; i < lafs.length; ++i)
207
      {
208
        UIManager.LookAndFeelInfo laf = lafs[i];
209
        ChangeLAFAction action = new ChangeLAFAction(laf);
210
        JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(action);
211
        boolean selected = laf.getClassName().equals(currentLaf);
212
        lafItem.setSelected(selected);
213
        lafMenu.add(lafItem);
214
 
215
        lafGroup.add(lafItem);
216
      }
217
 
218
    // Create themes menu.
219
    themesMenu = new JMenu("Themes");
220
    ButtonGroup themesGroup = new ButtonGroup();
221
 
222
    // In order to make the demo runable on a 1.4 type VM we have to avoid calling
223
    // MetalLookAndFeel.getCurrentTheme(). We simply check whether this method exists
224
    // and is public.
225
    Method m = null;
226
    try
227
      {
228
        m = MetalLookAndFeel.class.getMethod("getCurrentTheme", null);
229
      }
230
    catch (NoSuchMethodException nsme)
231
      {
232
        // Ignore it.
233
      }
234
 
235
    if (m != null)
236
      {
237
        JRadioButtonMenuItem ocean =
238
          new JRadioButtonMenuItem(new ChangeThemeAction(new OceanTheme()));
239
        ocean.setSelected(MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme);
240
        themesMenu.add(ocean);
241
        themesGroup.add(ocean);
242
 
243
        JRadioButtonMenuItem steel =
244
          new JRadioButtonMenuItem(new ChangeThemeAction(new DefaultMetalTheme()));
245
        ocean.setSelected(MetalLookAndFeel.getCurrentTheme()
246
                          instanceof DefaultMetalTheme);
247
        themesMenu.add(steel);
248
        themesGroup.add(steel);
249
      }
250
    else
251
      {
252
        themesMenu.setEnabled(false);
253
      }
254
 
255
    bar.add(file);
256
    bar.add(edit);
257
    bar.add(examples);
258
    bar.add(lafMenu);
259
    bar.add(themesMenu);
260
    bar.add(help);
261
    return bar;
262
  }
263
 
264
  private static void triggerDialog(final JButton but, final String dir)
265
  {
266
    but.addActionListener(new ActionListener()
267
      {
268
        public void actionPerformed(ActionEvent e)
269
        {
270
          JOptionPane.showConfirmDialog(but,
271
                                        "Sure you want to go " + dir + "?",
272
                                        "Confirm",
273
                                        JOptionPane.OK_CANCEL_OPTION,
274
                                        JOptionPane.QUESTION_MESSAGE,
275
                                        bigStockIcon("warning"));
276
        }
277
      });
278
  }
279
 
280
  static JToolBar mkToolBar()
281
  {
282
    JToolBar bar = new JToolBar();
283
 
284
    JButton b = mkButton(stockIcon("go-back"));
285
    triggerDialog(b, "back");
286
    bar.add(b);
287
 
288
    b = mkButton(stockIcon("go-down"));
289
    triggerDialog(b, "down");
290
    bar.add(b);
291
 
292
    b = mkButton(stockIcon("go-forward"));
293
    triggerDialog(b, "forward");
294
    bar.add(b);
295
    return bar;
296
  }
297
 
298
  static String halign2str(int a)
299
  {
300
    switch (a)
301
      {
302
      case SwingConstants.CENTER:
303
        return "Center";
304
      case SwingConstants.RIGHT:
305
        return "Right";
306
      case SwingConstants.LEFT:
307
        return "Left";
308
      default:
309
        return "Unknown";
310
      }
311
  }
312
 
313
  private static JButton mkButton(String title, Icon icon,
314
                                  int hAlign, int vAlign,
315
                                  int hPos, int vPos)
316
  {
317
    JButton b;
318
    if (icon == null)
319
      b = new JButton(title);
320
    else if (title == null)
321
      b = new JButton(icon);
322
    else
323
      b = new JButton(title, icon);
324
 
325
    b.setToolTipText(title);
326
    if (hAlign != -1) b.setHorizontalAlignment(hAlign);
327
    if (vAlign != -1) b.setVerticalAlignment(vAlign);
328
    if (hPos != -1) b.setHorizontalTextPosition(hPos);
329
    if (vPos != -1) b.setVerticalTextPosition(vPos);
330
    return b;
331
  }
332
 
333
  static JButton mkButton(String title)
334
  {
335
    return mkButton(title, null, -1, -1, -1, -1);
336
  }
337
 
338
  static JButton mkButton(Icon i)
339
  {
340
    return mkButton(null, i, -1, -1, -1, -1);
341
  }
342
 
343
  public Demo()
344
  {
345
    frame = new JFrame("Swing Activity Board");
346
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
347
    frame.setJMenuBar(mkMenuBar());
348
    JComponent component = (JComponent) frame.getContentPane();
349
    component.setLayout(new BorderLayout());
350
    component.add(mkToolBar(), BorderLayout.NORTH);
351
    JPanel main = new JPanel();
352
    main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
353
    desktop = createDesktop();
354
    main.add(desktop);
355
    main.add(mkButtonBar());
356
    component.add(main, BorderLayout.CENTER);
357
    frame.pack();
358
    frame.show();
359
  }
360
 
361
  public static class LaterMain
362
    implements Runnable
363
  {
364
    public void run()
365
    {
366
      new Demo();
367
    }
368
  }
369
 
370
  public static void main(String args[])
371
  {
372
    SwingUtilities.invokeLater(new LaterMain());
373
  }
374
 
375
  private static JButton mkBigButton(String title)
376
  {
377
    JButton b = new JButton(title);
378
    b.setMargin(new Insets(5,5,5,5));
379
    return b;
380
  }
381
 
382
  static JButton mkDisposerButton(final JFrame c)
383
  {
384
    JButton close = mkBigButton("Close");
385
    close.addActionListener(new ActionListener()
386
      {
387
        public void actionPerformed(ActionEvent e)
388
        {
389
          c.dispose();
390
        }
391
      });
392
    return close;
393
  }
394
 
395
  public static JColorChooser mkColorChooser()
396
  {
397
    return new JColorChooser();
398
  }
399
 
400
  /**
401
   * This action brings up a new Window with the specified content.
402
   */
403
  private class PopupAction
404
    extends AbstractAction
405
  {
406
    /**
407
     * The component to be shown.
408
     */
409
    private DemoFactory demoFactory;
410
 
411
    /**
412
     * Creates a new PopupAction with the specified name and showing the
413
     * component created by the specified DemoFactory when activated.
414
     *
415
     * @param n the name of the action
416
     * @param factory the demo factory
417
     */
418
    PopupAction(String n, DemoFactory factory)
419
    {
420
      putValue(NAME, n);
421
      demoFactory = factory;
422
    }
423
 
424
    /**
425
     * Brings up the new window showing the component stored in the
426
     * constructor.
427
     *
428
     * @param e the action event that triggered the action
429
     */
430
    public void actionPerformed(ActionEvent e)
431
    {
432
      JInternalFrame frame = new JInternalFrame((String) getValue(NAME));
433
      frame.setClosable(true);
434
      frame.setIconifiable(true);
435
      frame.setMaximizable(true);
436
      frame.setResizable(true);
437
      frame.setContentPane(demoFactory.createDemo());
438
      frame.pack();
439
      desktop.add(frame);
440
      frame.setVisible(true);
441
    }
442
  }
443
 
444
  private JPanel mkButtonBar()
445
  {
446
    JPanel panel = new JPanel(new GridLayout(3, 1, 5, 5));
447
    panel.add(new JButton(new PopupAction("Buttons",
448
                                          ButtonDemo.createDemoFactory())));
449
    panel.add(new JButton(new PopupAction("Slider",
450
                                          SliderDemo.createDemoFactory())));
451
    panel.add(new JButton(new PopupAction("ProgressBar",
452
                                        ProgressBarDemo.createDemoFactory())));
453
    panel.add(new JButton(new PopupAction("ScrollBar",
454
                                          ScrollBarDemo.createDemoFactory())));
455
    panel.add(new JButton(new PopupAction("Spinner",
456
                                          SpinnerDemo.createDemoFactory())));
457
    panel.add(new JButton(new PopupAction("TextField",
458
                                          TextFieldDemo.createDemoFactory())));
459
    panel.add(new JButton(new PopupAction("TextArea",
460
                                          TextAreaDemo.createDemoFactory())));
461
    panel.add(new JButton(new PopupAction("FileChooser",
462
                                        FileChooserDemo.createDemoFactory())));
463
    panel.add(new JButton(new PopupAction("ComboBox",
464
                                          ComboBoxDemo.createDemoFactory())));
465
    panel.add(new JButton(new PopupAction("Table",
466
                                          TableDemo.createDemoFactory())));
467
    panel.add(new JButton(new PopupAction("List",
468
                                          ListDemo.createDemoFactory())));
469
    panel.add(new JButton(new PopupAction("TabbedPane",
470
                                         TabbedPaneDemo.createDemoFactory())));
471
    panel.add(new JButton(new PopupAction("Tree",
472
                                          TreeDemo.createDemoFactory())));
473
    panel.add(new JButton(new PopupAction("Theme Editor",
474
                                      MetalThemeEditor.createDemoFactory())));
475
    panel.add(new JButton(new PopupAction("JNI Overhead",
476
                                          JNIOverhead.createDemoFactory())));
477
    panel.add(new JButton(new PopupAction("HTML",
478
                                          HtmlDemo.createDemoFactory())));
479
 
480
    JButton exitDisposer = mkDisposerButton(frame);
481
    panel.add(exitDisposer);
482
 
483
    panel.setMaximumSize(new Dimension(Integer.MAX_VALUE,
484
                                       panel.getPreferredSize().height));
485
    exitDisposer.addActionListener(new ActionListener()
486
      {
487
        public void actionPerformed(ActionEvent e)
488
        {
489
          System.exit(1);
490
        }
491
      });
492
    return panel;
493
  }
494
 
495
  /**
496
   * Creates and returns the main desktop.
497
   *
498
   * @return the main desktop
499
   */
500
  private JDesktopPane createDesktop()
501
  {
502
    JDesktopPane d = new DemoDesktop();
503
    d.setPreferredSize(new Dimension(900, 500));
504
    return d;
505
  }
506
 
507
  /**
508
   * This Action is used to switch Metal themes.
509
   */
510
  class ChangeThemeAction extends AbstractAction
511
  {
512
    /**
513
     * The theme to switch to.
514
     */
515
    MetalTheme theme;
516
 
517
    /**
518
     * Creates a new ChangeThemeAction for the specified theme.
519
     *
520
     * @param t the theme to switch to
521
     */
522
    ChangeThemeAction(MetalTheme t)
523
    {
524
      theme = t;
525
      putValue(NAME, t.getName());
526
    }
527
 
528
    /**
529
     * Changes the theme to the one specified in the constructor.
530
     *
531
     * @param event the action event that triggered this action
532
     */
533
    public void actionPerformed(ActionEvent event)
534
    {
535
      MetalLookAndFeel.setCurrentTheme(theme);
536
      try
537
        {
538
          // Only switch theme if we have a metal L&F. It is still necessary
539
          // to install a new MetalLookAndFeel instance.
540
          if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel)
541
            UIManager.setLookAndFeel(new MetalLookAndFeel());
542
        }
543
      catch (UnsupportedLookAndFeelException ex)
544
        {
545
          ex.printStackTrace();
546
        }
547
      SwingUtilities.updateComponentTreeUI(frame);
548
    }
549
 
550
  }
551
 
552
  /**
553
   * This Action is used to switch Metal themes.
554
   */
555
  class ChangeLAFAction extends AbstractAction
556
  {
557
    /**
558
     * The theme to switch to.
559
     */
560
    private UIManager.LookAndFeelInfo laf;
561
 
562
    /**
563
     * Creates a new ChangeLAFAction for the specified L&F.
564
     *
565
     * @param l the L&F to switch to
566
     */
567
    ChangeLAFAction(UIManager.LookAndFeelInfo l)
568
    {
569
      laf = l;
570
      putValue(NAME, laf.getName());
571
    }
572
 
573
    /**
574
     * Changes the theme to the one specified in the constructor.
575
     *
576
     * @param event the action event that triggered this action
577
     */
578
    public void actionPerformed(ActionEvent event)
579
    {
580
      try
581
        {
582
          UIManager.setLookAndFeel(laf.getClassName());
583
        }
584
      catch (Exception ex)
585
        {
586
          ex.printStackTrace();
587
        }
588
 
589
      SwingUtilities.updateComponentTreeUI(frame);
590
      themesMenu.setEnabled(laf.getClassName()
591
                           .equals("javax.swing.plaf.metal.MetalLookAndFeel"));
592
    }
593
  }
594
 
595
  /**
596
   * An implementation of BasicLookAndFeel which can be instantiated.
597
   *
598
   * @author Robert Schuster (robertschuster@fsfe.org)
599
   *
600
   */
601
  public static class InstantiableBasicLookAndFeel extends BasicLookAndFeel
602
  {
603
    public String getDescription()
604
    {
605
      return "An instantiable implementation of BasicLookAndFeel";
606
    }
607
 
608
    public String getID()
609
    {
610
      return "instantiableBasicLookAndFeel";
611
    }
612
 
613
    public String getName()
614
    {
615
      return "Instantiable Basic Look And Feel";
616
    }
617
 
618
    public boolean isNativeLookAndFeel()
619
    {
620
      return false;
621
    }
622
 
623
    public boolean isSupportedLookAndFeel()
624
    {
625
      return true;
626
    }
627
  }
628
 
629
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.