2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
package org.netbeans.modules.form.editors2;
45
import org.openide.awt.Mnemonics;
46
import org.openide.explorer.propertysheet.editors.*;
47
import org.openide.explorer.propertysheet.ExPropertyEditor;
51
import java.util.HashMap;
53
import java.util.ResourceBundle;
54
import org.openide.explorer.propertysheet.PropertyEnv;
61
public class CursorEditor extends PropertyEditorSupport implements
62
ExPropertyEditor, XMLPropertyEditor,
63
org.netbeans.modules.form.NamedPropertyEditor {
65
private static HashMap CURSOR_TYPES = new HashMap();
66
private static HashMap CURSOR_CONSTANTS = new HashMap();
68
CURSOR_TYPES.put(new Cursor(Cursor.CROSSHAIR_CURSOR).getName(), new Integer(Cursor.CROSSHAIR_CURSOR));
69
CURSOR_TYPES.put(new Cursor(Cursor.DEFAULT_CURSOR).getName(), new Integer(Cursor.DEFAULT_CURSOR));
70
CURSOR_TYPES.put(new Cursor(Cursor.E_RESIZE_CURSOR).getName(), new Integer(Cursor.E_RESIZE_CURSOR));
71
CURSOR_TYPES.put(new Cursor(Cursor.HAND_CURSOR).getName(), new Integer(Cursor.HAND_CURSOR));
72
CURSOR_TYPES.put(new Cursor(Cursor.MOVE_CURSOR).getName(), new Integer(Cursor.MOVE_CURSOR));
73
CURSOR_TYPES.put(new Cursor(Cursor.N_RESIZE_CURSOR).getName(), new Integer(Cursor.N_RESIZE_CURSOR));
74
CURSOR_TYPES.put(new Cursor(Cursor.NE_RESIZE_CURSOR).getName(), new Integer(Cursor.NE_RESIZE_CURSOR));
75
CURSOR_TYPES.put(new Cursor(Cursor.NW_RESIZE_CURSOR).getName(), new Integer(Cursor.NW_RESIZE_CURSOR));
76
CURSOR_TYPES.put(new Cursor(Cursor.S_RESIZE_CURSOR).getName(), new Integer(Cursor.S_RESIZE_CURSOR));
77
CURSOR_TYPES.put(new Cursor(Cursor.SE_RESIZE_CURSOR).getName(), new Integer(Cursor.SE_RESIZE_CURSOR));
78
CURSOR_TYPES.put(new Cursor(Cursor.SW_RESIZE_CURSOR).getName(), new Integer(Cursor.SW_RESIZE_CURSOR));
79
CURSOR_TYPES.put(new Cursor(Cursor.TEXT_CURSOR).getName(), new Integer(Cursor.TEXT_CURSOR));
80
CURSOR_TYPES.put(new Cursor(Cursor.W_RESIZE_CURSOR).getName(), new Integer(Cursor.W_RESIZE_CURSOR));
81
CURSOR_TYPES.put(new Cursor(Cursor.WAIT_CURSOR).getName(), new Integer(Cursor.WAIT_CURSOR));
83
CURSOR_CONSTANTS.put(new Integer(Cursor.CROSSHAIR_CURSOR), "java.awt.Cursor.CROSSHAIR_CURSOR"); // NOI18N
84
CURSOR_CONSTANTS.put(new Integer(Cursor.DEFAULT_CURSOR), "java.awt.Cursor.DEFAULT_CURSOR"); // NOI18N
85
CURSOR_CONSTANTS.put(new Integer(Cursor.E_RESIZE_CURSOR), "java.awt.Cursor.E_RESIZE_CURSOR"); // NOI18N
86
CURSOR_CONSTANTS.put(new Integer(Cursor.HAND_CURSOR), "java.awt.Cursor.HAND_CURSOR"); // NOI18N
87
CURSOR_CONSTANTS.put(new Integer(Cursor.MOVE_CURSOR), "java.awt.Cursor.MOVE_CURSOR"); // NOI18N
88
CURSOR_CONSTANTS.put(new Integer(Cursor.N_RESIZE_CURSOR), "java.awt.Cursor.N_RESIZE_CURSOR"); // NOI18N
89
CURSOR_CONSTANTS.put(new Integer(Cursor.NE_RESIZE_CURSOR), "java.awt.Cursor.NE_RESIZE_CURSOR"); // NOI18N
90
CURSOR_CONSTANTS.put(new Integer(Cursor.NW_RESIZE_CURSOR), "java.awt.Cursor.NW_RESIZE_CURSOR"); // NOI18N
91
CURSOR_CONSTANTS.put(new Integer(Cursor.S_RESIZE_CURSOR), "java.awt.Cursor.S_RESIZE_CURSOR"); // NOI18N
92
CURSOR_CONSTANTS.put(new Integer(Cursor.SE_RESIZE_CURSOR), "java.awt.Cursor.SE_RESIZE_CURSOR"); // NOI18N
93
CURSOR_CONSTANTS.put(new Integer(Cursor.SW_RESIZE_CURSOR), "java.awt.Cursor.SW_RESIZE_CURSOR"); // NOI18N
94
CURSOR_CONSTANTS.put(new Integer(Cursor.TEXT_CURSOR), "java.awt.Cursor.TEXT_CURSOR"); // NOI18N
95
CURSOR_CONSTANTS.put(new Integer(Cursor.W_RESIZE_CURSOR), "java.awt.Cursor.W_RESIZE_CURSOR"); // NOI18N
96
CURSOR_CONSTANTS.put(new Integer(Cursor.WAIT_CURSOR), "java.awt.Cursor.WAIT_CURSOR"); // NOI18N
99
private Cursor current;
101
private PropertyEnv env;
103
/** Creates new CursorEditor */
104
public CursorEditor() {
105
current = new Cursor(Cursor.DEFAULT_CURSOR);
108
public void attachEnv(PropertyEnv env) {
110
env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE); // NOI18N
113
public Object getValue() {
117
public void setValue(Object value) {
118
if (value == null) return;
119
if (value instanceof Cursor) {
120
current =(Cursor) value;
121
firePropertyChange();
123
throw new IllegalArgumentException();
127
public String getAsText() {
129
return "null"; // NOI18N
131
return current.getName();
134
public void setAsText(String string) {
135
Object o = CURSOR_TYPES.get(string);
137
int type =((Integer) o).intValue();
138
setValue(new Cursor(type));
142
public String[] getTags() {
143
String [] tags = new String[CURSOR_TYPES.size()];
145
for (java.util.Iterator iter = CURSOR_TYPES.keySet().iterator(); iter.hasNext(); i++)
146
tags [i] =(String) iter.next();
150
public boolean supportsCustomEditor() {
154
public Component getCustomEditor() {
155
return new CursorPanel();
158
public String getJavaInitializationString() {
159
if (current == null) return null; // no code to generate
160
String cursorName =(String) CURSOR_CONSTANTS.get(new Integer(current.getType()));
161
if (cursorName != null)
162
return "new java.awt.Cursor("+cursorName+")"; // NOI18N
163
return "new java.awt.Cursor("+current.getType()+")"; // NOI18N
166
class CursorPanel extends JPanel implements PropertyChangeListener {
170
setLayout(new java.awt.GridBagLayout());
171
java.awt.GridBagConstraints gridBagConstraints1;
172
list = new JList(new java.util.Vector(CURSOR_TYPES.keySet()));
173
list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
174
if (current != null) {
175
list.setSelectedValue(current.getName(), true);
177
env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
178
env.addPropertyChangeListener(this);
180
ResourceBundle bundle = org.openide.util.NbBundle.getBundle(CursorEditor.class);
181
JLabel cursorListLabel = new JLabel();
182
Mnemonics.setLocalizedText(cursorListLabel, bundle.getString("CTL_SelectCursorName")); // NOI18N
183
cursorListLabel.setLabelFor(list);
185
gridBagConstraints1 = new java.awt.GridBagConstraints();
186
gridBagConstraints1.gridx = 0;
187
gridBagConstraints1.gridy = 1;
188
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
189
gridBagConstraints1.insets = new java.awt.Insets(8, 8, 8, 8);
190
gridBagConstraints1.weightx = 1.0;
191
gridBagConstraints1.weighty = 1.0;
192
JScrollPane scrollPane = new JScrollPane(list);
193
add(scrollPane, gridBagConstraints1);
195
gridBagConstraints1 = new java.awt.GridBagConstraints();
196
gridBagConstraints1.gridx = 0;
197
gridBagConstraints1.gridy = 0;
198
gridBagConstraints1.insets = new java.awt.Insets(8, 8, 0, 8);
199
gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
201
add(cursorListLabel, gridBagConstraints1);
203
list.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_SelectCursorName"));
204
scrollPane.getVerticalScrollBar().getAccessibleContext().setAccessibleName(bundle.getString("ACSD_CTL_SelectCursorName")); // NOI18N
205
scrollPane.getVerticalScrollBar().getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CTL_SelectCursorName")); // NOI18N
206
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CursorCustomEditor"));
209
public void propertyChange(PropertyChangeEvent evt) {
210
if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
211
&& evt.getNewValue() == PropertyEnv.STATE_VALID) {
213
if (list.getSelectedValue() == null) {
216
int type =((Integer) CURSOR_TYPES.get(list.getSelectedValue())).intValue();
217
cursor = new Cursor(type);
224
//--------------------------------------------------------------------------
225
// XMLPropertyEditor implementation
227
public static final String XML_CURSOR = "Color"; // NOI18N
229
public static final String ATTR_ID = "id"; // NOI18N
231
/** Called to load property value from specified XML subtree. If succesfully loaded,
232
* the value should be available via the getValue method.
233
* An IOException should be thrown when the value cannot be restored from the specified XML element
234
* @param element the XML DOM element representing a subtree of XML from which the value should be loaded
235
* @exception IOException thrown when the value cannot be restored from the specified XML element
237
public void readFromXML(org.w3c.dom.Node element) throws java.io.IOException {
238
if (!XML_CURSOR.equals(element.getNodeName())) {
239
throw new java.io.IOException();
241
org.w3c.dom.NamedNodeMap attributes = element.getAttributes();
243
String id = attributes.getNamedItem(ATTR_ID).getNodeValue();
245
} catch (NullPointerException e) {
246
throw new java.io.IOException();
250
/** Called to store current property value into XML subtree. The property value should be set using the
251
* setValue method prior to calling this method.
252
* @param doc The XML document to store the XML in - should be used for creating nodes only
253
* @return the XML DOM element representing a subtree of XML from which the value should be loaded
255
public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) {
256
org.w3c.dom.Element el = doc.createElement(XML_CURSOR);
257
el.setAttribute(ATTR_ID, getAsText());
262
// ------------------------------------------
263
// NamedPropertyEditor implementation
265
/** @return display name of the property editor */
266
public String getDisplayName() {
267
return org.openide.util.NbBundle.getBundle(CursorEditor.class).getString("CTL_CursorEditor_DisplayName");