1 | |
package org.jtheque.utils.ui; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import javax.swing.Action; |
20 | |
import javax.swing.JComponent; |
21 | |
import javax.swing.JLabel; |
22 | |
import javax.swing.JOptionPane; |
23 | |
import javax.swing.JTextField; |
24 | |
import javax.swing.KeyStroke; |
25 | |
import javax.swing.SwingUtilities; |
26 | |
import javax.swing.text.AbstractDocument; |
27 | |
import javax.swing.text.Document; |
28 | |
import javax.swing.text.DocumentFilter; |
29 | |
import java.awt.Color; |
30 | |
import java.awt.Component; |
31 | |
import java.awt.DisplayMode; |
32 | |
import java.awt.Font; |
33 | |
import java.awt.GraphicsDevice; |
34 | |
import java.awt.GraphicsEnvironment; |
35 | |
import java.awt.Insets; |
36 | |
import java.awt.Toolkit; |
37 | |
import java.awt.Window; |
38 | |
import java.awt.event.KeyEvent; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public final class SwingUtils { |
46 | |
private static DisplayMode mode; |
47 | |
private static GraphicsDevice device; |
48 | |
|
49 | |
private static Font defaultFont; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private SwingUtils() { |
55 | 0 | super(); |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private static void loadDisplayInfos() { |
62 | 0 | GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
63 | 0 | device = gEnv.getDefaultScreenDevice(); |
64 | 0 | mode = device.getDisplayMode(); |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private static Insets getInsets() { |
73 | 0 | return Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration()); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static void centerFrame(Window frame) { |
82 | 0 | if (mode == null) { |
83 | 0 | loadDisplayInfos(); |
84 | |
} |
85 | |
|
86 | 0 | frame.setLocation((getWidth() - frame.getWidth()) / 2, |
87 | |
(getHeight() - frame.getHeight()) / 2); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private static int getHeight() { |
96 | 0 | if (mode == null) { |
97 | 0 | loadDisplayInfos(); |
98 | |
} |
99 | |
|
100 | 0 | return mode.getHeight() - getInsets().bottom - getInsets().top; |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
private static int getWidth() { |
109 | 0 | if (mode == null) { |
110 | 0 | loadDisplayInfos(); |
111 | |
} |
112 | |
|
113 | 0 | return mode.getWidth() - getInsets().left - getInsets().right; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public static JOptionPane getOptionPane(Component c) { |
123 | 0 | Component parent = c; |
124 | |
|
125 | 0 | while (parent != null) { |
126 | 0 | if (parent instanceof JOptionPane) { |
127 | 0 | return (JOptionPane) parent; |
128 | |
} |
129 | |
|
130 | 0 | parent = parent.getParent(); |
131 | |
} |
132 | |
|
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public static void setOptionPaneValue(Component c, Object value) { |
143 | 0 | JOptionPane optionPane = SwingUtils.getOptionPane(c); |
144 | 0 | if (optionPane != null) { |
145 | 0 | optionPane.setValue(value); |
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public static Component createButtonBar(Action... actions) { |
156 | 0 | return createButtonBar(false, actions); |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public static Component createButtonBar(boolean leftAligned, Action... actions) { |
167 | 0 | ButtonBarBuilder builderButton = new ButtonBarBuilder(); |
168 | 0 | builderButton.getPanel().setBackground(Color.white); |
169 | |
|
170 | 0 | if (!leftAligned) { |
171 | 0 | builderButton.addGlue(); |
172 | |
} |
173 | |
|
174 | 0 | builderButton.addActions(actions); |
175 | |
|
176 | 0 | if (leftAligned) { |
177 | 0 | builderButton.addGlue(); |
178 | |
} |
179 | |
|
180 | 0 | return builderButton.getPanel(); |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public static void addFieldValidateAction(JComponent field, Action action) { |
190 | 0 | field.getActionMap().put("validate", action); |
191 | 0 | field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "validate"); |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static void addFieldLengthLimit(JTextField field, int length) { |
201 | 0 | DocumentFilter filterNom = new DocumentLengthFilterAvert(length, field); |
202 | 0 | Document documentFieldNom = field.getDocument(); |
203 | 0 | ((AbstractDocument) documentFieldNom).setDocumentFilter(filterNom); |
204 | 0 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public static Font getDefaultFont() { |
212 | 2 | if (defaultFont == null) { |
213 | 2 | defaultFont = new JLabel("Font").getFont(); |
214 | |
} |
215 | |
|
216 | 2 | return defaultFont; |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public static void inEdt(Runnable runnable) { |
225 | 2 | if (SwingUtilities.isEventDispatchThread()) { |
226 | 0 | runnable.run(); |
227 | |
} else { |
228 | 2 | SwingUtilities.invokeLater(runnable); |
229 | |
} |
230 | 2 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
public static void refresh(final Component component) { |
238 | 0 | inEdt(new Runnable() { |
239 | |
@Override |
240 | |
public void run() { |
241 | 0 | SwingUtilities.updateComponentTreeUI(component); |
242 | 0 | } |
243 | |
}); |
244 | 0 | } |
245 | |
} |