1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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  
45  
46  
47  
48  
49  public final class SwingTools {
50  
51    
52  
53  
54    private static final Border ETCHED = BorderFactory.createEtchedBorder();
55  
56    
57  
58  
59    private SwingTools() { }
60  
61    
62  
63  
64  
65  
66  
67    public static JPanel getGroupPanel(String caption) {
68      GridBagLayout layout = new GridBagLayout();
69      return getGroupPanel(caption, layout);
70    }
71  
72    
73  
74  
75  
76  
77  
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  
87  
88  
89  
90  
91  
92  
93  
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 
117 
118 
119 
120 
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 
131 
132 
133 
134 
135   public static void showError(String message, Exception e) {
136     String title = UI.getString("messages.error");
137 
138     
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     
151     JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
152   }
153 
154   
155 
156 
157 
158 
159 
160   public static void showWarning(String message, Exception e) {
161     String title = UI.getString("messages.warning");
162 
163     
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     
176     JOptionPane.showMessageDialog(null, message, title, JOptionPane.WARNING_MESSAGE);
177   }
178 
179   
180 
181 
182 
183 
184 
185   public static void showInformation(String message) {
186     String title = UI.getString("messages.information");
187     
188     JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
189   }
190 }