KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors2 > CursorEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.form.editors2;
22
23 import org.openide.awt.Mnemonics;
24 import org.openide.explorer.propertysheet.editors.*;
25
26 import java.beans.*;
27 import java.awt.*;
28 import java.util.HashMap JavaDoc;
29 import javax.swing.*;
30 import java.util.ResourceBundle JavaDoc;
31
32 /**
33  *
34  * @author Pavel Buzek
35  */

36
37 public class CursorEditor extends PropertyEditorSupport implements
38                                                          EnhancedPropertyEditor,
39                                                          org.openide.explorer.propertysheet.editors.XMLPropertyEditor,
40                                                          org.netbeans.modules.form.NamedPropertyEditor {
41
42     private static HashMap JavaDoc CURSOR_TYPES = new HashMap JavaDoc();
43     private static HashMap JavaDoc CURSOR_CONSTANTS = new HashMap JavaDoc();
44     static {
45         CURSOR_TYPES.put(new Cursor(Cursor.CROSSHAIR_CURSOR).getName(), new Integer JavaDoc(Cursor.CROSSHAIR_CURSOR));
46         CURSOR_TYPES.put(new Cursor(Cursor.DEFAULT_CURSOR).getName(), new Integer JavaDoc(Cursor.DEFAULT_CURSOR));
47         CURSOR_TYPES.put(new Cursor(Cursor.E_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.E_RESIZE_CURSOR));
48         CURSOR_TYPES.put(new Cursor(Cursor.HAND_CURSOR).getName(), new Integer JavaDoc(Cursor.HAND_CURSOR));
49         CURSOR_TYPES.put(new Cursor(Cursor.MOVE_CURSOR).getName(), new Integer JavaDoc(Cursor.MOVE_CURSOR));
50         CURSOR_TYPES.put(new Cursor(Cursor.N_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.N_RESIZE_CURSOR));
51         CURSOR_TYPES.put(new Cursor(Cursor.NE_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.NE_RESIZE_CURSOR));
52         CURSOR_TYPES.put(new Cursor(Cursor.NW_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.NW_RESIZE_CURSOR));
53         CURSOR_TYPES.put(new Cursor(Cursor.S_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.S_RESIZE_CURSOR));
54         CURSOR_TYPES.put(new Cursor(Cursor.SE_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.SE_RESIZE_CURSOR));
55         CURSOR_TYPES.put(new Cursor(Cursor.SW_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.SW_RESIZE_CURSOR));
56         CURSOR_TYPES.put(new Cursor(Cursor.TEXT_CURSOR).getName(), new Integer JavaDoc(Cursor.TEXT_CURSOR));
57         CURSOR_TYPES.put(new Cursor(Cursor.W_RESIZE_CURSOR).getName(), new Integer JavaDoc(Cursor.W_RESIZE_CURSOR));
58         CURSOR_TYPES.put(new Cursor(Cursor.WAIT_CURSOR).getName(), new Integer JavaDoc(Cursor.WAIT_CURSOR));
59
60         CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.CROSSHAIR_CURSOR), "java.awt.Cursor.CROSSHAIR_CURSOR"); // NOI18N
61
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.DEFAULT_CURSOR), "java.awt.Cursor.DEFAULT_CURSOR"); // NOI18N
62
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.E_RESIZE_CURSOR), "java.awt.Cursor.E_RESIZE_CURSOR"); // NOI18N
63
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.HAND_CURSOR), "java.awt.Cursor.HAND_CURSOR"); // NOI18N
64
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.MOVE_CURSOR), "java.awt.Cursor.MOVE_CURSOR"); // NOI18N
65
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.N_RESIZE_CURSOR), "java.awt.Cursor.N_RESIZE_CURSOR"); // NOI18N
66
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.NE_RESIZE_CURSOR), "java.awt.Cursor.NE_RESIZE_CURSOR"); // NOI18N
67
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.NW_RESIZE_CURSOR), "java.awt.Cursor.NW_RESIZE_CURSOR"); // NOI18N
68
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.S_RESIZE_CURSOR), "java.awt.Cursor.S_RESIZE_CURSOR"); // NOI18N
69
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.SE_RESIZE_CURSOR), "java.awt.Cursor.SE_RESIZE_CURSOR"); // NOI18N
70
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.SW_RESIZE_CURSOR), "java.awt.Cursor.SW_RESIZE_CURSOR"); // NOI18N
71
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.TEXT_CURSOR), "java.awt.Cursor.TEXT_CURSOR"); // NOI18N
72
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.W_RESIZE_CURSOR), "java.awt.Cursor.W_RESIZE_CURSOR"); // NOI18N
73
CURSOR_CONSTANTS.put(new Integer JavaDoc(Cursor.WAIT_CURSOR), "java.awt.Cursor.WAIT_CURSOR"); // NOI18N
74
}
75
76     Cursor current;
77
78     /** Creates new CursorEditor */
79     public CursorEditor() {
80         current = new Cursor(Cursor.DEFAULT_CURSOR);
81     }
82
83     public Object JavaDoc getValue() {
84         return current;
85     }
86
87     public void setValue(Object JavaDoc value) {
88         if (value == null) return;
89         if (value instanceof Cursor) {
90             current =(Cursor) value;
91             firePropertyChange();
92         } else {
93             throw new IllegalArgumentException JavaDoc();
94         }
95     }
96
97     public String JavaDoc getAsText() {
98         if (current == null)
99             return "null"; // NOI18N
100
else
101             return current.getName();
102     }
103
104     public void setAsText(String JavaDoc string) {
105         Object JavaDoc o = CURSOR_TYPES.get(string);
106         if (o != null) {
107             int type =((Integer JavaDoc) o).intValue();
108             setValue(new Cursor(type));
109         }
110     }
111
112     public boolean supportsEditingTaggedValues() {
113         return true;
114     }
115
116     public String JavaDoc[] getTags() {
117         String JavaDoc [] tags = new String JavaDoc[CURSOR_TYPES.size()];
118         int i=0;
119         for (java.util.Iterator JavaDoc iter = CURSOR_TYPES.keySet().iterator(); iter.hasNext(); i++)
120             tags [i] =(String JavaDoc) iter.next();
121         return tags;
122     }
123
124     public boolean hasInPlaceCustomEditor() {
125         return false;
126     }
127
128     public Component getInPlaceCustomEditor() {
129         return null;
130     }
131
132     public boolean supportsCustomEditor() {
133         return true;
134     }
135
136     public Component getCustomEditor() {
137         return new CursorPanel(current);
138     }
139
140     public String JavaDoc getJavaInitializationString() {
141         if (current == null) return null; // no code to generate
142
String JavaDoc cursorName =(String JavaDoc) CURSOR_CONSTANTS.get(new Integer JavaDoc(current.getType()));
143         if (cursorName != null)
144             return "new java.awt.Cursor("+cursorName+")"; // NOI18N
145
return "new java.awt.Cursor("+current.getType()+")"; // NOI18N
146
}
147
148     static class CursorPanel extends JPanel implements EnhancedCustomPropertyEditor {
149         CursorPanel(Cursor value) {
150             setLayout(new java.awt.GridBagLayout JavaDoc());
151             java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
152             list = new JList(new java.util.Vector JavaDoc(CURSOR_TYPES.keySet()));
153             list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
154             if (value != null)
155                 list.setSelectedValue(value.getName(), true);
156
157             ResourceBundle JavaDoc bundle = org.openide.util.NbBundle.getBundle(CursorEditor.class);
158             JLabel cursorListLabel = new JLabel();
159             Mnemonics.setLocalizedText(cursorListLabel, bundle.getString("CTL_SelectCursorName")); // NOI18N
160
cursorListLabel.setLabelFor(list);
161             
162             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
163             gridBagConstraints1.gridx = 0;
164             gridBagConstraints1.gridy = 1;
165             gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
166             gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 8, 8);
167             gridBagConstraints1.weightx = 1.0;
168             gridBagConstraints1.weighty = 1.0;
169             JScrollPane scrollPane = new JScrollPane(list);
170             add(scrollPane, gridBagConstraints1);
171
172             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
173             gridBagConstraints1.gridx = 0;
174             gridBagConstraints1.gridy = 0;
175             gridBagConstraints1.insets = new java.awt.Insets JavaDoc(8, 8, 0, 8);
176             gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
177             
178             add(cursorListLabel, gridBagConstraints1);
179
180             list.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_SelectCursorName"));
181             scrollPane.getVerticalScrollBar().getAccessibleContext().setAccessibleName(bundle.getString("ACSD_CTL_SelectCursorName")); // NOI18N
182
scrollPane.getVerticalScrollBar().getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_SelectCursorName")); // NOI18N
183
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CursorCustomEditor"));
184         }
185
186         public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
187             if (list.getSelectedValue()==null) return null;
188             int type =((Integer JavaDoc) CURSOR_TYPES.get(list.getSelectedValue())).intValue();
189             return new Cursor(type);
190         }
191
192         private JList list;
193     }
194
195     //--------------------------------------------------------------------------
196
// XMLPropertyEditor implementation
197

198     public static final String JavaDoc XML_CURSOR = "Color"; // NOI18N
199

200     public static final String JavaDoc ATTR_ID = "id"; // NOI18N
201

202     /** Called to load property value from specified XML subtree. If succesfully loaded,
203      * the value should be available via the getValue method.
204      * An IOException should be thrown when the value cannot be restored from the specified XML element
205      * @param element the XML DOM element representing a subtree of XML from which the value should be loaded
206      * @exception IOException thrown when the value cannot be restored from the specified XML element
207      */

208     public void readFromXML(org.w3c.dom.Node JavaDoc element) throws java.io.IOException JavaDoc {
209         if (!XML_CURSOR.equals(element.getNodeName())) {
210             throw new java.io.IOException JavaDoc();
211         }
212         org.w3c.dom.NamedNodeMap JavaDoc attributes = element.getAttributes();
213         try {
214             String JavaDoc id = attributes.getNamedItem(ATTR_ID).getNodeValue();
215             setAsText(id);
216         } catch (NullPointerException JavaDoc e) {
217             throw new java.io.IOException JavaDoc();
218         }
219     }
220
221     /** Called to store current property value into XML subtree. The property value should be set using the
222      * setValue method prior to calling this method.
223      * @param doc The XML document to store the XML in - should be used for creating nodes only
224      * @return the XML DOM element representing a subtree of XML from which the value should be loaded
225      */

226     public org.w3c.dom.Node JavaDoc storeToXML(org.w3c.dom.Document JavaDoc doc) {
227         org.w3c.dom.Element JavaDoc el = doc.createElement(XML_CURSOR);
228         el.setAttribute(ATTR_ID, getAsText());
229         return el;
230     }
231
232     
233     // ------------------------------------------
234
// NamedPropertyEditor implementation
235

236     /** @return display name of the property editor */
237     public String JavaDoc getDisplayName() {
238         return org.openide.util.NbBundle.getBundle(CursorEditor.class).getString("CTL_CursorEditor_DisplayName");
239     }
240     
241 }
242
Popular Tags