KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Tiny


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 import java.awt.BorderLayout JavaDoc;
32 import java.awt.Component JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Frame JavaDoc;
35
36 import javax.swing.*;
37 import javax.swing.border.EmptyBorder JavaDoc;
38
39 import com.jgoodies.looks.FontSizeHints;
40 import com.jgoodies.looks.LookUtils;
41 import com.jgoodies.looks.Options;
42
43
44 /**
45  * The main class of the JGoodies Tiny application.
46  * It configures the UI, builds the main frame and opens it.
47  * <p>
48  * The JGoodies Looks Professional comes with Skeleton, a much better sample
49  * application that separates concerns and uses a scalable architecture.
50  *
51  * @author Karsten Lentzsch
52  */

53 public final class Tiny {
54
55     /**
56      * Configures the UI, then builds and opens the UI.
57      */

58     public static void main(String JavaDoc[] args) {
59         Tiny instance = new Tiny();
60         instance.configureUI();
61         instance.buildInterface();
62     }
63
64     /**
65      * Configures the UI; tries to set the system look on Mac,
66      * <code>WindowsLookAndFeel</code> on general Windows, and
67      * <code>Plastic3DLookAndFeel</code> on Windows XP and all other OS.<p>
68      *
69      * The JGoodies Swing Suite's <code>ApplicationStarter</code>,
70      * <code>ExtUIManager</code>, and <code>LookChoiceStrategies</code>
71      * classes provide a much more fine grained algorithm to choose and
72      * restore a look and theme.
73      */

74     private void configureUI() {
75         UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE);
76         Options.setGlobalFontSizeHints(FontSizeHints.MIXED);
77         Options.setDefaultIconSize(new Dimension JavaDoc(18, 18));
78
79         String JavaDoc lafName =
80             LookUtils.IS_OS_WINDOWS_XP
81                 ? Options.getCrossPlatformLookAndFeelClassName()
82                 : Options.getSystemLookAndFeelClassName();
83
84         try {
85             UIManager.setLookAndFeel(lafName);
86         } catch (Exception JavaDoc e) {
87             System.err.println("Can't set look & feel:" + e);
88         }
89     }
90
91     /**
92      * Creates and configures a frame, builds the menu bar, builds the
93      * content, locates the frame on the screen, and finally shows the frame.
94      */

95     private void buildInterface() {
96         JFrame frame = new JFrame();
97         frame.setJMenuBar(buildMenuBar());
98         frame.setContentPane(buildContentPane());
99         frame.setSize(600, 400);
100         locateOnScreen(frame);
101         frame.setTitle("JGoodies :: Tiny");
102         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
103         frame.setVisible(true);
104     }
105
106     /**
107      * Locates the frame on the screen center.
108      */

109     private void locateOnScreen(Frame JavaDoc frame) {
110         Dimension JavaDoc paneSize = frame.getSize();
111         Dimension JavaDoc screenSize = frame.getToolkit().getScreenSize();
112         frame.setLocation(
113             (screenSize.width - paneSize.width) / 2,
114             (screenSize.height - paneSize.height) / 2);
115     }
116
117     /**
118      * Builds and answers the menu bar.
119      */

120     private JMenuBar buildMenuBar() {
121         JMenu menu;
122         JMenuBar menuBar = new JMenuBar();
123         menuBar.putClientProperty(Options.HEADER_STYLE_KEY, Boolean.TRUE);
124
125         menu = new JMenu("File");
126         menu.add(new JMenuItem("New..."));
127         menu.add(new JMenuItem("Open..."));
128         menu.add(new JMenuItem("Save"));
129         menu.addSeparator();
130         menu.add(new JMenuItem("Print..."));
131         menuBar.add(menu);
132
133         menu = new JMenu("Edit");
134         menu.add(new JMenuItem("Cut"));
135         menu.add(new JMenuItem("Copy"));
136         menu.add(new JMenuItem("Paste"));
137         menuBar.add(menu);
138
139         return menuBar;
140     }
141
142     /**
143      * Builds and answers the content pane.
144      */

145     private JComponent buildContentPane() {
146         JPanel panel = new JPanel(new BorderLayout JavaDoc());
147         panel.add(buildToolBar(), BorderLayout.NORTH);
148         panel.add(buildSplitPane(), BorderLayout.CENTER);
149         panel.add(buildStatusBar(), BorderLayout.SOUTH);
150         return panel;
151     }
152
153     /**
154      * Builds and answers the tool bar.
155      */

156     private Component JavaDoc buildToolBar() {
157         JToolBar toolBar = new JToolBar();
158         toolBar.putClientProperty(Options.HEADER_STYLE_KEY, Boolean.TRUE);
159         
160         toolBar.add(createCenteredLabel("Tool Bar"));
161         return toolBar;
162     }
163
164     /**
165      * Builds and answers the split panel.
166      */

167     private Component JavaDoc buildSplitPane() {
168         JSplitPane splitPane =
169             new JSplitPane(
170                 JSplitPane.HORIZONTAL_SPLIT,
171                 buildSideBar(),
172                 buildMainPanel());
173         return splitPane;
174     }
175
176     /**
177      * Builds and answers the side bar.
178      */

179     private Component JavaDoc buildSideBar() {
180         return createStrippedScrollPane(new JTree());
181     }
182
183     /**
184      * Builds and answers the main panel.
185      */

186     private Component JavaDoc buildMainPanel() {
187         JEditorPane editor = new JEditorPane();
188         editor.setText(
189             "This is a minimal Swing application, that demos,\n" +
190             "how to install and use a JGoodies look&feel\n" +
191             "in a Swing application.");
192         return createStrippedScrollPane(editor);
193     }
194
195     /**
196      * Builds and answers the tool bar.
197      */

198     private Component JavaDoc buildStatusBar() {
199         JPanel statusBar = new JPanel(new BorderLayout JavaDoc());
200         statusBar.add(createCenteredLabel("Status Bar"));
201         return statusBar;
202     }
203
204     // Helper Code ********************************************************
205

206     /**
207      * Creates and answers a <code>JScrollpane</code> that has no border.
208      */

209     private JScrollPane createStrippedScrollPane(Component JavaDoc c) {
210         JScrollPane scrollPane = new JScrollPane(c);
211         scrollPane.setBorder(null);
212         return scrollPane;
213     }
214
215     /**
216      * Creates and answers a <code>JLabel</code> that has the text
217      * centered and that is wrapped with an empty border.
218      */

219     private Component JavaDoc createCenteredLabel(String JavaDoc text) {
220         JLabel label = new JLabel(text);
221         label.setHorizontalAlignment(SwingConstants.CENTER);
222         label.setBorder(new EmptyBorder JavaDoc(3, 3, 3, 3));
223         return label;
224     }
225
226 }
Popular Tags