View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 25-feb-2004
21   *
22   * Copyright (c)2003 Grid Systems
23   */
24  package com.gridsystems.config.tools.swing;
25  
26  import java.awt.GridBagConstraints;
27  import java.awt.GridBagLayout;
28  import java.awt.Insets;
29  import java.awt.LayoutManager;
30  import java.net.URL;
31  
32  import javax.swing.BorderFactory;
33  import javax.swing.Icon;
34  import javax.swing.ImageIcon;
35  import javax.swing.JComponent;
36  import javax.swing.JLabel;
37  import javax.swing.JOptionPane;
38  import javax.swing.JPanel;
39  import javax.swing.border.Border;
40  
41  import com.gridsystems.config.app.UI;
42  
43  /**
44   * Helper methods for Swing UI construction.
45   *
46   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
47   * @version 1.0
48   */
49  public final class SwingTools {
50  
51    /**
52     * Border to use around group panels.
53     */
54    private static final Border ETCHED = BorderFactory.createEtchedBorder();
55  
56    /**
57     * Utility class: should never be instantiated.
58     */
59    private SwingTools() { }
60  
61    /**
62     * Creates a group panel with the specified caption, and a GridBagLayout.
63     *
64     * @param caption The panel caption
65     * @return A JPanel with an etched titled border
66     */
67    public static JPanel getGroupPanel(String caption) {
68      GridBagLayout layout = new GridBagLayout();
69      return getGroupPanel(caption, layout);
70    }
71  
72    /**
73     * Creates a group panel with the specified caption, and layout.
74     *
75     * @param caption The panel caption
76     * @param layout  The panel layout
77     * @return A JPanel with an etched titled border
78     */
79    public static JPanel getGroupPanel(String caption, LayoutManager layout) {
80      JPanel panel = new JPanel(layout);
81      panel.setBorder(BorderFactory.createTitledBorder(ETCHED, caption));
82      return panel;
83    }
84  
85    /**
86     * Adds a row consisting in a label and a component in a panel.
87     * <p>
88     * It supposes the panel uses a GridBoxLayout
89     *
90     * @param panel Where to put the components row
91     * @param label The component label
92     * @param c     The component
93     * @param row   The row index in the GridBoxLayout
94     */
95    public static void addField(JPanel panel, String label, JComponent c, int row) {
96      LayoutManager layout = panel.getLayout();
97      if (layout instanceof GridBagLayout) {
98        JLabel lbl = new JLabel(label);
99        lbl.setHorizontalAlignment(JLabel.RIGHT);
100       lbl.setLabelFor(c);
101       GridBagConstraints gc =
102         new GridBagConstraints(
103           GridBagConstraints.RELATIVE, row, 1, 1,
104           0.4, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE,
105           new Insets(2, 2, 2, 2), 0, 0
106         );
107 
108       panel.add(lbl, gc);
109       gc.anchor = GridBagConstraints.LINE_START;
110       gc.weightx = 1.0;
111       panel.add(c, gc);
112     }
113   }
114 
115   /**
116    * Loads an icon as a class resource.
117    *
118    * @param c    The class to use for resource search
119    * @param name The name of the resource containing the icon
120    * @return     The icon
121    */
122   public static Icon loadIcon(Class c, String name) {
123     URL url = (c == null)
124               ? Thread.currentThread().getContextClassLoader().getResource(name)
125               : c.getResource(name);
126     return new ImageIcon(url);
127   }
128 
129   /**
130    * Shows a "standard" error dialog box.
131    *
132    * @param message The error message. Should be already internationalized
133    * @param e       The exception that causes this dialog box to be shown
134    */
135   public static void showError(String message, Exception e) {
136     String title = UI.getString("messages.error");
137 
138     // If there is a "printable" reason, appends it to the message
139     String msg = e.getMessage();
140     if (msg == null && e.getCause() != null) {
141       msg = e.getCause().getMessage();
142     }
143     if (msg != null) {
144       msg = msg.trim();
145       if (!msg.equals("")) {
146         message += "\n\n      " + msg;
147       }
148     }
149 
150     // Shows the dialog box
151     JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
152   }
153 
154   /**
155    * Shows a warning message.
156    *
157    * @param message The message key
158    * @param e The exception causing this message.
159    */
160   public static void showWarning(String message, Exception e) {
161     String title = UI.getString("messages.warning");
162 
163     // If there is a "printable" reason, appends it to the message
164     String reason = e.getMessage();
165     if (reason == null && e.getCause() != null) {
166       reason = e.getCause().getMessage();
167     }
168     if (reason != null) {
169       reason = reason.trim();
170       if (!reason.equals("")) {
171         message += "\n\nReason: " + reason;
172       }
173     }
174 
175     // Shows the dialog box
176     JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
177   }
178 
179   /**
180    * Shows a information message.
181    *
182    * @param message The message key
183    *
184    */
185   public static void showInformation(String message) {
186     String title = UI.getString("messages.information");
187     // Shows the dialog box
188     JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
189   }
190 }