Added
Link Here
|
1 |
/* |
2 |
* CustPanel.java |
3 |
* |
4 |
* Created on July 6, 2005, 7:19 AM |
5 |
* |
6 |
* To change this template, choose Tools | Options and locate the template under |
7 |
* the Source Creation and Management node. Right-click the template and choose |
8 |
* Open. You can then make changes to the template in the Source Editor. |
9 |
*/ |
10 |
|
11 |
package org.netbeans.modules.form; |
12 |
import java.awt.Color; |
13 |
import java.awt.Component; |
14 |
import java.awt.Dimension; |
15 |
import java.awt.Font; |
16 |
import java.awt.Graphics; |
17 |
import java.awt.Insets; |
18 |
import java.awt.event.ActionEvent; |
19 |
import java.awt.event.ActionListener; |
20 |
import java.awt.event.FocusEvent; |
21 |
import java.awt.event.FocusListener; |
22 |
import java.awt.event.KeyEvent; |
23 |
import java.awt.event.KeyListener; |
24 |
import java.awt.event.MouseEvent; |
25 |
import java.awt.event.MouseListener; |
26 |
import java.lang.reflect.InvocationTargetException; |
27 |
import javax.swing.BoxLayout; |
28 |
import javax.swing.JCheckBox; |
29 |
import javax.swing.JLabel; |
30 |
import javax.swing.JPanel; |
31 |
import javax.swing.JSeparator; |
32 |
import javax.swing.JTextField; |
33 |
import javax.swing.SwingConstants; |
34 |
import javax.swing.UIManager; |
35 |
import javax.swing.border.Border; |
36 |
import org.openide.ErrorManager; |
37 |
import org.openide.explorer.propertysheet.PropertyPanel; |
38 |
import org.openide.nodes.Node; |
39 |
import org.openide.util.NbBundle; |
40 |
|
41 |
/** |
42 |
* |
43 |
* @author tim |
44 |
*/ |
45 |
class CustPanel extends JPanel implements MouseListener, FocusListener, Border, KeyListener, ActionListener { |
46 |
private JLabel nameLbl = new JLabel(); |
47 |
private JLabel typeLbl = new JLabel(); |
48 |
private JTextField nameField = new JTextField(); |
49 |
private JSeparator js = new JSeparator(); |
50 |
private PropertyPanel custom = new PropertyPanel(); |
51 |
private JCheckBox enabled = new JCheckBox(); |
52 |
private JLabel propLbl = new JLabel(); |
53 |
|
54 |
/** Creates a new instance of CustPanel */ |
55 |
CustPanel() { |
56 |
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); |
57 |
init(); |
58 |
} |
59 |
|
60 |
void init() { |
61 |
nameLbl.setBorder (this); |
62 |
nameLbl.addMouseListener (this); |
63 |
nameField.addFocusListener (this); |
64 |
nameField.addKeyListener(this); |
65 |
nameField.addActionListener(this); |
66 |
enabled.addActionListener(this); |
67 |
enabled.setText (NbBundle.getMessage(CustPanel.class, "LBL_Enabled")); //NOI18N |
68 |
add (nameLbl); |
69 |
add (typeLbl); |
70 |
nameLbl.setHorizontalAlignment(SwingConstants.CENTER); |
71 |
typeLbl.setHorizontalAlignment(SwingConstants.CENTER); |
72 |
nameField.setHorizontalAlignment(SwingConstants.CENTER); |
73 |
add (custom); |
74 |
Font f = UIManager.getFont("Label.font"); //NOI18N |
75 |
if (f == null) { |
76 |
f = new Font ("Dialog", Font.PLAIN, 12); //NOI18N |
77 |
} |
78 |
propLbl.setFont (f.deriveFont(Font.BOLD)); |
79 |
f = f.deriveFont (Font.BOLD, 16f); |
80 |
nameLbl.setFont (f); |
81 |
nameField.setFont (f); |
82 |
//Turn this on if we want to use custom editors |
83 |
// custom.setChangeImmediate(true); |
84 |
custom.putClientProperty ("radioButtonMax", new Integer (3)); //NOI18N |
85 |
custom.putClientProperty ("useLabels", Boolean.FALSE); |
86 |
enabled.setVisible(false); |
87 |
propLbl.setVisible(false); |
88 |
enabled.setVisible(false); |
89 |
js.setVisible(false); |
90 |
add (js); |
91 |
add (propLbl); |
92 |
add (enabled); |
93 |
} |
94 |
|
95 |
public Dimension getPreferredSize() { |
96 |
return new Dimension (300, 300); |
97 |
} |
98 |
|
99 |
public void paint (java.awt.Graphics g) { |
100 |
super.paint (g); |
101 |
} |
102 |
|
103 |
private RADComponentNode node = null; |
104 |
void setNode (RADComponentNode node) { |
105 |
this.node = node; |
106 |
updateContents(); |
107 |
} |
108 |
|
109 |
private void updateContents() { |
110 |
if (nameField.isShowing()) { |
111 |
stopEditing(false); |
112 |
} |
113 |
if (node != null) { |
114 |
nameLbl.setText (node.getRADComponent().getName()); |
115 |
String s = node.getRADComponent().getBeanClass().getName(); |
116 |
int idx = s.lastIndexOf ("."); |
117 |
if (idx != -1) { |
118 |
s = s.substring (idx + 1); |
119 |
} |
120 |
s = NbBundle.getMessage(CustPanel.class, "FMT_ClassName", s); //NOI18N |
121 |
typeLbl.setText (s); |
122 |
} |
123 |
updateProperty(); |
124 |
validate(); |
125 |
repaint(); |
126 |
} |
127 |
|
128 |
public void doLayout() { |
129 |
Dimension d = nameLbl.getPreferredSize(); |
130 |
int gap = 6; |
131 |
int wf = getWidth() / 4; |
132 |
int y = gap; |
133 |
nameLbl.setBounds(y, y, getWidth() - 8, d.height); |
134 |
y += d.height + gap; |
135 |
typeLbl.setBounds(4, y, getWidth() -8, typeLbl.getPreferredSize().height); |
136 |
nameField.setBounds (nameLbl.getBounds()); |
137 |
y += d.height + gap; |
138 |
|
139 |
d = propLbl.getPreferredSize(); |
140 |
propLbl.setBounds (gap, y, d.width, d.height); |
141 |
js.setBounds (gap + d.width + gap, y + (d.height / 2), getWidth() - (gap + gap + gap + d.width), d.height / 2); |
142 |
|
143 |
y += propLbl.getHeight() + gap; |
144 |
y += gap; |
145 |
d = custom.getPreferredSize(); |
146 |
custom.setBounds (4, y, getWidth() - 8, Math.max (20, d.height)); |
147 |
y += gap + custom.getHeight(); |
148 |
d = enabled.getPreferredSize(); |
149 |
enabled.setBounds (getWidth() - (d.width + gap), getHeight() - (gap + d.height), d.width, d.height); |
150 |
} |
151 |
|
152 |
private void stopEditing (boolean take) { |
153 |
if (take && node != null && nameField.isShowing()) { |
154 |
try { |
155 |
String s = nameField.getText().trim(); |
156 |
node.setName(s); |
157 |
} catch (Exception e) { |
158 |
ErrorManager.getDefault().notify(e); |
159 |
} |
160 |
} |
161 |
nameLbl.setVisible (true); |
162 |
nameField.setVisible(false); |
163 |
remove (nameField); |
164 |
updateContents(); |
165 |
revalidate(); |
166 |
repaint(); |
167 |
} |
168 |
|
169 |
private void startEditing () { |
170 |
if (!nameField.isShowing()) { |
171 |
nameField.setText (nameLbl.getText()); |
172 |
nameField.setVisible(true); |
173 |
nameLbl.setVisible(false); |
174 |
add (nameField); |
175 |
validate(); |
176 |
repaint(); |
177 |
nameField.requestFocus(); |
178 |
} |
179 |
} |
180 |
|
181 |
public void mouseClicked(MouseEvent e) { |
182 |
} |
183 |
|
184 |
boolean armed = false; |
185 |
public void mousePressed(MouseEvent e) { |
186 |
armed = node != null; |
187 |
} |
188 |
|
189 |
public void mouseReleased(MouseEvent e) { |
190 |
if (armed && node != null) { |
191 |
startEditing(); |
192 |
} |
193 |
armed = false; |
194 |
} |
195 |
|
196 |
boolean containsMouse = false; |
197 |
public void mouseEntered(MouseEvent e) { |
198 |
containsMouse = true; |
199 |
nameLbl.repaint(); |
200 |
} |
201 |
|
202 |
public void mouseExited(MouseEvent e) { |
203 |
containsMouse = false; |
204 |
nameLbl.repaint(); |
205 |
} |
206 |
|
207 |
public void focusGained(FocusEvent e) { |
208 |
nameField.setSelectionStart (0); |
209 |
nameField.setSelectionEnd (nameField.getText().length()); |
210 |
} |
211 |
|
212 |
public void focusLost(FocusEvent e) { |
213 |
stopEditing (true); |
214 |
} |
215 |
|
216 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
217 |
if (containsMouse) { |
218 |
g.setColor (new Color (180, 180, 255)); |
219 |
g.drawRect (x, y, width - 1, height - 1); |
220 |
} |
221 |
} |
222 |
|
223 |
public Insets getBorderInsets(Component c) { |
224 |
return new Insets (1, 1, 1, 1); |
225 |
} |
226 |
|
227 |
public boolean isBorderOpaque() { |
228 |
return false; |
229 |
} |
230 |
|
231 |
private void updateProperty() { |
232 |
Node.Property p = null; |
233 |
if (node != null) { |
234 |
// Code below should work is form editor sets preferred properties |
235 |
// correctly; it doesn't. Could hack w/ check for html display |
236 |
// name |
237 |
// |
238 |
// Node.PropertySet[] ps = node.getPropertySets(); |
239 |
// for (int i=0; i < ps.length; i++) { |
240 |
// Node.Property[] props = ps[i].getProperties(); |
241 |
// for (int j=0; j < props.length; j++) { |
242 |
// if (props[j].isPreferred()) { |
243 |
// p = props[j]; |
244 |
// break; |
245 |
// } |
246 |
// } |
247 |
// } |
248 |
if (p == null) { |
249 |
Class clazz = node.getRADComponent().getBeanClass(); |
250 |
String propName = getPropForClass (clazz); |
251 |
if (propName != null) { |
252 |
p = node.getProperty(propName); |
253 |
} |
254 |
} |
255 |
} |
256 |
if (p != null) { |
257 |
//Optional - use custom editor - looks weird w/ form connection |
258 |
//editor, though |
259 |
// custom.setPreferences(PropertyPanel.PREF_CUSTOM_EDITOR); |
260 |
custom.setProperty (p); |
261 |
} |
262 |
custom.setVisible (p != null); |
263 |
js.setVisible (p != null); |
264 |
propLbl.setVisible (p != null); |
265 |
propLbl.setText (p == null ? "" : capitalize (p.getDisplayName())); |
266 |
Node.Property en = node.getProperty("enabled"); |
267 |
if (en == null) { |
268 |
//??? sometimes it's capitalized |
269 |
en = node.getProperty("Enabled"); |
270 |
} |
271 |
enabled.setVisible (en != null); |
272 |
if (en != null) { |
273 |
try { |
274 |
enabled.setSelected (Boolean.TRUE.equals(en.getValue())); |
275 |
} catch (Exception e) { |
276 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
private String getPropForClass (Class clazz) { |
282 |
String result = null; |
283 |
String s = clazz.getName(); |
284 |
int ix = s.lastIndexOf ("."); |
285 |
if (ix != -1) { |
286 |
s = s.substring (ix + 1); |
287 |
} |
288 |
if ("JTable".equals(s) || "JList".equals(s) || "JTree".equals(s) || |
289 |
"JComboBox".equals(s)) { |
290 |
return "model"; |
291 |
} else if ("JLabel".equals(s) || "JTextField".equals (s) || |
292 |
"JButton".equals(s) || |
293 |
"JToggleButton".equals(s) || "JMenu".equals(s) || |
294 |
"JMenuItem".equals(s) || |
295 |
"JTextArea".equals(s) || |
296 |
"JRadioButton".equals(s) || |
297 |
"JCheckBox".equals(s)) { |
298 |
|
299 |
return "text"; |
300 |
} else if ("JSpinner".equals(s) || "JSlider".equals(s) || |
301 |
"JProgressBar".equals(s) || "JScrollBar".equals(s)) { |
302 |
return "value"; |
303 |
} else if ("JSplitPane".equals(s)) { |
304 |
return "dividerLocation"; |
305 |
} else if ("JScrollPane".equals(s)) { |
306 |
return "horizontalScrollBarPolicy"; |
307 |
} else if ("JFrame".equals(s) || "JDialog".equals(s)) { |
308 |
return "title"; |
309 |
} |
310 |
|
311 |
//XXX to be continued...AWT components & such |
312 |
return null; |
313 |
} |
314 |
|
315 |
private static String capitalize (String s) { |
316 |
if (s.length() > 0) { |
317 |
StringBuffer sb = new StringBuffer (s); |
318 |
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); |
319 |
return sb.toString(); |
320 |
} else { |
321 |
return s; |
322 |
} |
323 |
} |
324 |
|
325 |
public void keyTyped(KeyEvent e) { |
326 |
if (Character.isDigit(e.getKeyChar()) |
327 |
&& (nameField.getText().trim().length() == 0 || |
328 |
(nameField.getSelectionStart() == 0 && |
329 |
nameField.getSelectionEnd() == nameField.getText().length()))) { |
330 |
System.err.println("block typed a"); |
331 |
e.consume(); |
332 |
} else { |
333 |
char[] c = new char[] { |
334 |
',', |
335 |
'.', |
336 |
';', |
337 |
'+', |
338 |
'-', |
339 |
'%', |
340 |
'*', |
341 |
'(', |
342 |
')', |
343 |
'{', |
344 |
'}', |
345 |
'[', |
346 |
']', |
347 |
' ', |
348 |
'&', |
349 |
'!', |
350 |
'^', |
351 |
'@', |
352 |
'\\', |
353 |
'/'}; |
354 |
boolean block = false; |
355 |
for (int i=0; i < c.length; i++) { |
356 |
if (c[i] == e.getKeyCode()) { |
357 |
block = true; |
358 |
break; |
359 |
} |
360 |
} |
361 |
if (block) { |
362 |
System.err.println("block typed b"); |
363 |
e.consume(); |
364 |
} |
365 |
} |
366 |
} |
367 |
|
368 |
public void keyPressed(KeyEvent e) { |
369 |
if (e.getKeyCode() >= KeyEvent.VK_0 && e.getKeyCode() <= KeyEvent.VK_9) { |
370 |
if (nameField.getText().trim().length() == 0 || |
371 |
(nameField.getSelectionStart() == 0 && |
372 |
nameField.getSelectionEnd() == nameField.getText().length())) { |
373 |
System.err.println(" block pressed a"); |
374 |
e.consume(); |
375 |
} else if (e.getKeyCode() == KeyEvent.VK_COMMA || |
376 |
e.getKeyCode() == KeyEvent.VK_DIVIDE || |
377 |
e.getKeyCode() == KeyEvent.VK_PERIOD || |
378 |
e.getKeyCode() == KeyEvent.VK_SEMICOLON || |
379 |
e.getKeyCode() == KeyEvent.VK_PLUS || |
380 |
e.getKeyCode() == KeyEvent.VK_ASTERISK || |
381 |
e.getKeyCode() == KeyEvent.VK_LEFT_PARENTHESIS || |
382 |
e.getKeyCode() == KeyEvent.VK_RIGHT_PARENTHESIS || |
383 |
e.getKeyCode() == KeyEvent.VK_OPEN_BRACKET || |
384 |
e.getKeyCode() == KeyEvent.VK_SPACE || |
385 |
e.getKeyCode() == KeyEvent.VK_CLOSE_BRACKET || |
386 |
e.getKeyCode() == KeyEvent.VK_BRACELEFT || |
387 |
e.getKeyCode() == KeyEvent.VK_BRACERIGHT || |
388 |
e.getKeyCode() == KeyEvent.VK_AMPERSAND || |
389 |
e.getKeyCode() == KeyEvent.VK_COLON || |
390 |
e.getKeyCode() == KeyEvent.VK_CIRCUMFLEX || |
391 |
e.getKeyCode() == KeyEvent.VK_EXCLAMATION_MARK || |
392 |
e.getKeyCode() == KeyEvent.VK_SLASH) { |
393 |
System.err.println(" block pressed b"); |
394 |
e.consume(); |
395 |
} |
396 |
} |
397 |
} |
398 |
|
399 |
public void keyReleased(KeyEvent e) { |
400 |
// keyPressed (e); |
401 |
} |
402 |
|
403 |
public void actionPerformed(ActionEvent e) { |
404 |
if (e.getSource() == enabled) { |
405 |
Node.Property p = node.getProperty("enabled"); |
406 |
if (p != null) { |
407 |
try { |
408 |
p.setValue (enabled.isSelected() ? Boolean.TRUE : Boolean.FALSE); |
409 |
} catch (IllegalAccessException ex) { |
410 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); |
411 |
} catch (InvocationTargetException ex1) { |
412 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex1); |
413 |
} |
414 |
} |
415 |
} else { |
416 |
stopEditing(true); |
417 |
} |
418 |
} |
419 |
|
420 |
} |