KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > demo > DemoFrame


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.demo;
32
33 import java.awt.*;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.net.URL JavaDoc;
37
38 import javax.swing.*;
39 import javax.swing.border.Border JavaDoc;
40 import javax.swing.border.EmptyBorder JavaDoc;
41 import javax.swing.plaf.UIResource JavaDoc;
42 import javax.swing.plaf.metal.DefaultMetalTheme JavaDoc;
43 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
44
45 import com.jgoodies.looks.LookUtils;
46 import com.jgoodies.looks.Options;
47 import com.jgoodies.looks.plastic.PlasticLookAndFeel;
48 import com.jgoodies.looks.windows.WindowsLookAndFeel;
49
50 /**
51  * Builds the main frame in the Simple Looks Demo.
52  * Demonstrates and tests different multi-platform issues by
53  * showing a variety of Swing widgets in different configurations.
54  * Also, this frame contains examples for Swing misuse,
55  * that can be automatically corrected by ClearLook.<p>
56  *
57  * This class provides a couple of protected methods that create
58  * components or a builder. The full JGoodies Looks Demo overrides
59  * these methods to vend components or builders from the
60  * JGoodies UI framework that better handle different platforms.
61  *
62  * @author Karsten Lentzsch
63  * @version $Revision: 1.15 $
64  */

65 public class DemoFrame extends JFrame {
66
67     protected static final Dimension PREFERRED_SIZE =
68         LookUtils.IS_LOW_RESOLUTION ? new Dimension(650, 510) : new Dimension(730, 560);
69
70     /** Describes optional settings of the JGoodies Looks */
71     private final Settings settings;
72
73     /**
74      * Constructs a <code>DemoFrame</code>, configures the UI,
75      * and builds the content.
76      */

77     protected DemoFrame(Settings settings) {
78         this.settings = settings;
79         configureUI();
80         build();
81         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
82     }
83     
84     public static void main(String JavaDoc[] args) {
85         DemoFrame instance = new DemoFrame(createSettings());
86         instance.setSize(PREFERRED_SIZE);
87         instance.locateOnScreen(instance);
88         instance.setVisible(true);
89     }
90     
91     private static Settings createSettings() {
92         Settings settings = Settings.createDefault();
93         
94         // Configure the settings here.
95

96         return settings;
97     }
98     
99
100     /**
101      * Configures the user interface; requests Swing settings and
102      * jGoodies Looks options from the launcher.
103      */

104     private void configureUI() {
105         Options.setDefaultIconSize(new Dimension(18, 18));
106
107         // Set font options
108
UIManager.put(
109             Options.USE_SYSTEM_FONTS_APP_KEY,
110             settings.isUseSystemFonts());
111         Options.setGlobalFontSizeHints(settings.getFontSizeHints());
112         Options.setUseNarrowButtons(settings.isUseNarrowButtons());
113         
114         // Global options
115
Options.setTabIconsEnabled(settings.isTabIconsEnabled());
116         UIManager.put(Options.POPUP_DROP_SHADOW_ENABLED_KEY,
117                 settings.isPopupDropShadowEnabled());
118
119         // Swing Settings
120
LookAndFeel JavaDoc selectedLaf = settings.getSelectedLookAndFeel();
121         if (selectedLaf instanceof PlasticLookAndFeel) {
122             PlasticLookAndFeel.setMyCurrentTheme(settings.getSelectedTheme());
123             PlasticLookAndFeel.setTabStyle(settings.getPlasticTabStyle());
124             PlasticLookAndFeel.setHighContrastFocusColorsEnabled(
125                 settings.isPlasticHighContrastFocusEnabled());
126         } else if (selectedLaf.getClass() == MetalLookAndFeel JavaDoc.class) {
127             MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme JavaDoc());
128         }
129         
130         // Work around caching in MetalRadioButtonUI
131
JRadioButton radio = new JRadioButton();
132         radio.getUI().uninstallUI(radio);
133         JCheckBox checkBox = new JCheckBox();
134         checkBox.getUI().uninstallUI(checkBox);
135
136         try {
137             UIManager.setLookAndFeel(selectedLaf);
138         } catch (Exception JavaDoc e) {
139             System.out.println("Can't change L&F: " + e);
140         }
141
142     }
143
144     /**
145      * Builds the <code>DemoFrame</code> using Options from the Launcher.
146      */

147     private void build() {
148         setContentPane(buildContentPane());
149         setTitle(getWindowTitle());
150         setJMenuBar(
151             createMenuBuilder().buildMenuBar(
152                 settings,
153                 createHelpActionListener(),
154                 createAboutActionListener()));
155         setIconImage(readImageIcon("eye_16x16.gif").getImage());
156     }
157     
158     
159     /**
160      * Creates and returns a builder that builds the menu.
161      * This method is overriden by the full JGoodies Looks Demo to use
162      * a more sophisticated menu builder that uses the JGoodies
163      * UI Framework.
164      *
165      * @return the builder that builds the menu bar
166      */

167     protected MenuBuilder createMenuBuilder() {
168         return new MenuBuilder();
169     }
170
171     /**
172      * Builds and answers the content.
173      */

174     private JComponent buildContentPane() {
175         JPanel panel = new JPanel(new BorderLayout());
176         panel.add(buildToolBar(), BorderLayout.NORTH);
177         panel.add(buildMainPanel(), BorderLayout.CENTER);
178         return panel;
179     }
180
181     // Tool Bar *************************************************************
182

183     /**
184      * Builds, configures and returns the toolbar. Requests
185      * HeaderStyle, look-specific BorderStyles, and Plastic 3D Hint
186      * from Launcher.
187      */

188     private Component buildToolBar() {
189         JToolBar toolBar = new JToolBar();
190         toolBar.setFloatable(false);
191         toolBar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
192         // Swing
193
toolBar.putClientProperty(
194             Options.HEADER_STYLE_KEY,
195             settings.getToolBarHeaderStyle());
196         toolBar.putClientProperty(
197             PlasticLookAndFeel.BORDER_STYLE_KEY,
198             settings.getToolBarPlasticBorderStyle());
199         toolBar.putClientProperty(
200             WindowsLookAndFeel.BORDER_STYLE_KEY,
201             settings.getToolBarWindowsBorderStyle());
202         toolBar.putClientProperty(
203             PlasticLookAndFeel.IS_3D_KEY,
204             settings.getToolBar3DHint());
205
206         AbstractButton button;
207
208         toolBar.add(createToolBarButton("backward.gif"));
209         button = createToolBarButton("forward.gif");
210         button.setEnabled(false);
211         toolBar.add(button);
212         toolBar.add(createToolBarButton("home.gif"));
213         toolBar.addSeparator();
214         toolBar.add(createOpenButton());
215         toolBar.add(createToolBarButton("print.gif"));
216         toolBar.add(createToolBarButton("refresh.gif"));
217         toolBar.addSeparator();
218
219         ButtonGroup group = new ButtonGroup();
220         button = createToolBarRadioButton("pie_mode.png");
221         button.setSelectedIcon(readImageIcon("pie_mode_selected.gif"));
222         group.add(button);
223         button.setSelected(true);
224         toolBar.add(button);
225
226         button = createToolBarRadioButton("bar_mode.png");
227         button.setSelectedIcon(readImageIcon("bar_mode_selected.gif"));
228         group.add(button);
229         toolBar.add(button);
230
231         button = createToolBarRadioButton("table_mode.png");
232         button.setSelectedIcon(readImageIcon("table_mode_selected.gif"));
233         group.add(button);
234         toolBar.add(button);
235         toolBar.addSeparator();
236
237         button = createToolBarButton("help.gif");
238         button.addActionListener(createHelpActionListener());
239         toolBar.add(button);
240
241         toolBar.add(Box.createGlue());
242
243         button = new RolloverCheckButton();
244         button.setToolTipText("Shall show border when mouse is over");
245         button.setMargin(new Insets(0, 0, 0, 0));
246         toolBar.add(button);
247         return toolBar;
248     }
249
250     private AbstractButton createOpenButton() {
251         AbstractButton button = createToolBarButton("open.gif");
252         button.addActionListener(new ActionListener JavaDoc() {
253             public void actionPerformed(ActionEvent JavaDoc e) {
254                 new JFileChooser().showOpenDialog(DemoFrame.this);
255             }
256
257         });
258         return button;
259     }
260
261     /**
262      * Creates and returns a <code>JButton</code>
263      * configured for use in a JToolBar.<p>
264      *
265      * This is a simplified method that is overriden by the Looks Demo.
266      * The full code uses the JGoodies UI framework's ToolBarButton
267      * that better handles platform differences.
268      */

269     protected AbstractButton createToolBarButton(String JavaDoc iconName) {
270         JButton button = new JButton(readImageIcon(iconName));
271         button.setFocusable(false);
272         return button;
273     }
274
275     /**
276      * Creates and returns a <code>JToggleButton</code>
277      * configured for use in a JToolBar.<p>
278      *
279      * This is a simplified method that is overriden by the Looks Demo.
280      * The full code uses the JGoodies UI framework's ToolBarButton
281      * that better handles platform differences.
282      */

283     protected AbstractButton createToolBarRadioButton(String JavaDoc iconName) {
284         JToggleButton button = new JToggleButton(readImageIcon(iconName));
285         button.setFocusable(false);
286         return button;
287     }
288
289     // Tabbed Pane **********************************************************
290

291     /**
292      * Builds and answers the tabbed pane.
293      */

294     private Component buildMainPanel() {
295         JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
296         //tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
297

298         addTabs(tabbedPane);
299
300         tabbedPane.setBorder(new EmptyBorder JavaDoc(10, 10, 10, 10));
301         return tabbedPane;
302     }
303
304     private void addTabs(JTabbedPane tabbedPane) {
305         tabbedPane.addTab("State", new StatesTab().build());
306         tabbedPane.addTab("Align", new AlignmentTab().build());
307         tabbedPane.addTab("Tab", new TabTestTab().build());
308         tabbedPane.addTab("Split", new SplitTab().build());
309         tabbedPane.addTab("HTML", new HtmlTab().build());
310         tabbedPane.addTab("Dialog", new DialogsTab().build(tabbedPane));
311         tabbedPane.addTab("Desktop", new DesktopTab().build());
312         tabbedPane.addTab("Narrow", new NarrowTab().build());
313     }
314     
315     protected String JavaDoc getWindowTitle() {
316         return "Simple Looks Demo";
317     }
318     
319
320     // Helper Code **********************************************************************
321

322     /*
323      * Looks up and answers an icon for the specified filename suffix.<p>
324      */

325     protected static ImageIcon readImageIcon(String JavaDoc filename) {
326         URL JavaDoc url =
327             DemoFrame.class.getClassLoader().getResource("images/" + filename);
328         return new ImageIcon(url);
329     }
330
331     /**
332      * Locates the given component on the screen's center.
333      */

334     protected void locateOnScreen(Component component) {
335         Dimension paneSize = component.getSize();
336         Dimension screenSize = component.getToolkit().getScreenSize();
337         component.setLocation(
338             (screenSize.width - paneSize.width) / 2,
339             (screenSize.height - paneSize.height) / 2);
340     }
341
342     /**
343      * Creates and answers an ActionListener that opens the help viewer.
344      */

345     protected ActionListener JavaDoc createHelpActionListener() {
346         return null;
347     }
348
349     /**
350      * Creates and answers an ActionListener that opens the about dialog.
351      */

352     protected ActionListener JavaDoc createAboutActionListener() {
353         return new ActionListener JavaDoc() {
354             public void actionPerformed(ActionEvent JavaDoc e) {
355                 JOptionPane.showMessageDialog(
356                     DemoFrame.this,
357                     "The simple Looks Demo Application\n"
358                         + "\n\u00a9 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.\n\n");
359             }
360         };
361     }
362
363     // Checks that all tool bar buttons have a UIResource border
364
private static class RolloverCheckButton extends JButton {
365
366         private boolean checked = false;
367
368         public void paint(Graphics g) {
369             if (!checked) {
370                 checkAndSetResult();
371             }
372             super.paint(g);
373         }
374
375         private void checkAndSetResult() {
376             Icon passedIcon = readImageIcon("passed.gif");
377             Icon failedIcon = readImageIcon("failed.gif");
378
379             boolean passed = allButtonBordersAreUIResources();
380             setIcon(passed ? passedIcon : failedIcon);
381             setText(passed ? "Can Swap L&F" : "Can't Swap L&F");
382
383             checked = true;
384         }
385
386         /**
387          * Checks and answers whether all button borders implement UIResource.
388          */

389         private boolean allButtonBordersAreUIResources() {
390             JToolBar bar = (JToolBar) getParent();
391             for (int i = bar.getComponentCount() - 1; i >= 0; i--) {
392                 Component child = bar.getComponent(i);
393                 if (child instanceof JButton) {
394                     Border JavaDoc b = ((JButton) child).getBorder();
395                     if (!(b instanceof UIResource JavaDoc))
396                         return false;
397                 }
398             }
399             return true;
400         }
401
402     }
403
404 }
Popular Tags