001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.editors2;
043:
044: import org.openide.awt.Mnemonics;
045: import org.openide.explorer.propertysheet.editors.*;
046: import org.openide.explorer.propertysheet.ExPropertyEditor;
047:
048: import java.beans.*;
049: import java.awt.*;
050: import java.util.HashMap;
051: import java.util.Map;
052: import javax.swing.*;
053: import java.util.ResourceBundle;
054: import org.openide.explorer.propertysheet.PropertyEnv;
055:
056: /**
057: *
058: * @author Pavel Buzek
059: */
060:
061: public class CursorEditor extends PropertyEditorSupport implements
062: ExPropertyEditor, XMLPropertyEditor,
063: org.netbeans.modules.form.NamedPropertyEditor {
064:
065: private static Map<String, Integer> CURSOR_TYPES = new HashMap<String, Integer>();
066: private static Map<Integer, String> CURSOR_CONSTANTS = new HashMap<Integer, String>();
067: static {
068: CURSOR_TYPES.put(new Cursor(Cursor.CROSSHAIR_CURSOR).getName(),
069: new Integer(Cursor.CROSSHAIR_CURSOR));
070: CURSOR_TYPES.put(new Cursor(Cursor.DEFAULT_CURSOR).getName(),
071: new Integer(Cursor.DEFAULT_CURSOR));
072: CURSOR_TYPES.put(new Cursor(Cursor.E_RESIZE_CURSOR).getName(),
073: new Integer(Cursor.E_RESIZE_CURSOR));
074: CURSOR_TYPES.put(new Cursor(Cursor.HAND_CURSOR).getName(),
075: new Integer(Cursor.HAND_CURSOR));
076: CURSOR_TYPES.put(new Cursor(Cursor.MOVE_CURSOR).getName(),
077: new Integer(Cursor.MOVE_CURSOR));
078: CURSOR_TYPES.put(new Cursor(Cursor.N_RESIZE_CURSOR).getName(),
079: new Integer(Cursor.N_RESIZE_CURSOR));
080: CURSOR_TYPES.put(new Cursor(Cursor.NE_RESIZE_CURSOR).getName(),
081: new Integer(Cursor.NE_RESIZE_CURSOR));
082: CURSOR_TYPES.put(new Cursor(Cursor.NW_RESIZE_CURSOR).getName(),
083: new Integer(Cursor.NW_RESIZE_CURSOR));
084: CURSOR_TYPES.put(new Cursor(Cursor.S_RESIZE_CURSOR).getName(),
085: new Integer(Cursor.S_RESIZE_CURSOR));
086: CURSOR_TYPES.put(new Cursor(Cursor.SE_RESIZE_CURSOR).getName(),
087: new Integer(Cursor.SE_RESIZE_CURSOR));
088: CURSOR_TYPES.put(new Cursor(Cursor.SW_RESIZE_CURSOR).getName(),
089: new Integer(Cursor.SW_RESIZE_CURSOR));
090: CURSOR_TYPES.put(new Cursor(Cursor.TEXT_CURSOR).getName(),
091: new Integer(Cursor.TEXT_CURSOR));
092: CURSOR_TYPES.put(new Cursor(Cursor.W_RESIZE_CURSOR).getName(),
093: new Integer(Cursor.W_RESIZE_CURSOR));
094: CURSOR_TYPES.put(new Cursor(Cursor.WAIT_CURSOR).getName(),
095: new Integer(Cursor.WAIT_CURSOR));
096:
097: CURSOR_CONSTANTS.put(new Integer(Cursor.CROSSHAIR_CURSOR),
098: "java.awt.Cursor.CROSSHAIR_CURSOR"); // NOI18N
099: CURSOR_CONSTANTS.put(new Integer(Cursor.DEFAULT_CURSOR),
100: "java.awt.Cursor.DEFAULT_CURSOR"); // NOI18N
101: CURSOR_CONSTANTS.put(new Integer(Cursor.E_RESIZE_CURSOR),
102: "java.awt.Cursor.E_RESIZE_CURSOR"); // NOI18N
103: CURSOR_CONSTANTS.put(new Integer(Cursor.HAND_CURSOR),
104: "java.awt.Cursor.HAND_CURSOR"); // NOI18N
105: CURSOR_CONSTANTS.put(new Integer(Cursor.MOVE_CURSOR),
106: "java.awt.Cursor.MOVE_CURSOR"); // NOI18N
107: CURSOR_CONSTANTS.put(new Integer(Cursor.N_RESIZE_CURSOR),
108: "java.awt.Cursor.N_RESIZE_CURSOR"); // NOI18N
109: CURSOR_CONSTANTS.put(new Integer(Cursor.NE_RESIZE_CURSOR),
110: "java.awt.Cursor.NE_RESIZE_CURSOR"); // NOI18N
111: CURSOR_CONSTANTS.put(new Integer(Cursor.NW_RESIZE_CURSOR),
112: "java.awt.Cursor.NW_RESIZE_CURSOR"); // NOI18N
113: CURSOR_CONSTANTS.put(new Integer(Cursor.S_RESIZE_CURSOR),
114: "java.awt.Cursor.S_RESIZE_CURSOR"); // NOI18N
115: CURSOR_CONSTANTS.put(new Integer(Cursor.SE_RESIZE_CURSOR),
116: "java.awt.Cursor.SE_RESIZE_CURSOR"); // NOI18N
117: CURSOR_CONSTANTS.put(new Integer(Cursor.SW_RESIZE_CURSOR),
118: "java.awt.Cursor.SW_RESIZE_CURSOR"); // NOI18N
119: CURSOR_CONSTANTS.put(new Integer(Cursor.TEXT_CURSOR),
120: "java.awt.Cursor.TEXT_CURSOR"); // NOI18N
121: CURSOR_CONSTANTS.put(new Integer(Cursor.W_RESIZE_CURSOR),
122: "java.awt.Cursor.W_RESIZE_CURSOR"); // NOI18N
123: CURSOR_CONSTANTS.put(new Integer(Cursor.WAIT_CURSOR),
124: "java.awt.Cursor.WAIT_CURSOR"); // NOI18N
125: }
126:
127: private Cursor current;
128:
129: private PropertyEnv env;
130:
131: /** Creates new CursorEditor */
132: public CursorEditor() {
133: current = new Cursor(Cursor.DEFAULT_CURSOR);
134: }
135:
136: public void attachEnv(PropertyEnv env) {
137: this .env = env;
138: env.getFeatureDescriptor().setValue("canEditAsText",
139: Boolean.TRUE); // NOI18N
140: }
141:
142: @Override
143: public Object getValue() {
144: return current;
145: }
146:
147: @Override
148: public void setValue(Object value) {
149: if (value == null)
150: return;
151: if (value instanceof Cursor) {
152: current = (Cursor) value;
153: firePropertyChange();
154: } else {
155: throw new IllegalArgumentException();
156: }
157: }
158:
159: @Override
160: public String getAsText() {
161: if (current == null)
162: return "null"; // NOI18N
163: else
164: return current.getName();
165: }
166:
167: @Override
168: public void setAsText(String string) {
169: Object o = CURSOR_TYPES.get(string);
170: if (o != null) {
171: int type = ((Integer) o).intValue();
172: setValue(new Cursor(type));
173: }
174: }
175:
176: @Override
177: public String[] getTags() {
178: String[] tags = new String[CURSOR_TYPES.size()];
179: int i = 0;
180: for (java.util.Iterator iter = CURSOR_TYPES.keySet().iterator(); iter
181: .hasNext(); i++)
182: tags[i] = (String) iter.next();
183: return tags;
184: }
185:
186: @Override
187: public boolean supportsCustomEditor() {
188: return true;
189: }
190:
191: @Override
192: public Component getCustomEditor() {
193: return new CursorPanel();
194: }
195:
196: @Override
197: public String getJavaInitializationString() {
198: if (current == null)
199: return null; // no code to generate
200: String cursorName = CURSOR_CONSTANTS.get(new Integer(current
201: .getType()));
202: if (cursorName != null)
203: return "new java.awt.Cursor(" + cursorName + ")"; // NOI18N
204: return "new java.awt.Cursor(" + current.getType() + ")"; // NOI18N
205: }
206:
207: class CursorPanel extends JPanel implements PropertyChangeListener {
208: private JList list;
209:
210: CursorPanel() {
211: setLayout(new java.awt.GridBagLayout());
212: java.awt.GridBagConstraints gridBagConstraints1;
213: list = new JList(new java.util.Vector<String>(CURSOR_TYPES
214: .keySet()));
215: list
216: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
217: if (current != null) {
218: list.setSelectedValue(current.getName(), true);
219: }
220: env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
221: env.addPropertyChangeListener(this );
222:
223: ResourceBundle bundle = org.openide.util.NbBundle
224: .getBundle(CursorEditor.class);
225: JLabel cursorListLabel = new JLabel();
226: Mnemonics.setLocalizedText(cursorListLabel, bundle
227: .getString("CTL_SelectCursorName")); // NOI18N
228: cursorListLabel.setLabelFor(list);
229:
230: gridBagConstraints1 = new java.awt.GridBagConstraints();
231: gridBagConstraints1.gridx = 0;
232: gridBagConstraints1.gridy = 1;
233: gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
234: gridBagConstraints1.insets = new java.awt.Insets(8, 8, 8, 8);
235: gridBagConstraints1.weightx = 1.0;
236: gridBagConstraints1.weighty = 1.0;
237: JScrollPane scrollPane = new JScrollPane(list);
238: add(scrollPane, gridBagConstraints1);
239:
240: gridBagConstraints1 = new java.awt.GridBagConstraints();
241: gridBagConstraints1.gridx = 0;
242: gridBagConstraints1.gridy = 0;
243: gridBagConstraints1.insets = new java.awt.Insets(8, 8, 0, 8);
244: gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
245:
246: add(cursorListLabel, gridBagConstraints1);
247:
248: list.getAccessibleContext().setAccessibleDescription(
249: bundle.getString("ACSD_CTL_SelectCursorName"));
250: scrollPane
251: .getVerticalScrollBar()
252: .getAccessibleContext()
253: .setAccessibleName(
254: bundle
255: .getString("ACSD_CTL_SelectCursorName")); // NOI18N
256: scrollPane
257: .getVerticalScrollBar()
258: .getAccessibleContext()
259: .setAccessibleDescription(
260: bundle
261: .getString("ACSD_CTL_SelectCursorName")); // NOI18N
262: getAccessibleContext().setAccessibleDescription(
263: bundle.getString("ACSD_CursorCustomEditor"));
264: }
265:
266: public void propertyChange(PropertyChangeEvent evt) {
267: if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName())
268: && evt.getNewValue() == PropertyEnv.STATE_VALID) {
269: Cursor cursor;
270: if (list.getSelectedValue() == null) {
271: cursor = null;
272: } else {
273: int type = CURSOR_TYPES
274: .get(list.getSelectedValue());
275: cursor = new Cursor(type);
276: }
277: setValue(cursor);
278: }
279: }
280: }
281:
282: //--------------------------------------------------------------------------
283: // XMLPropertyEditor implementation
284:
285: public static final String XML_CURSOR = "Color"; // NOI18N
286:
287: public static final String ATTR_ID = "id"; // NOI18N
288:
289: /** Called to load property value from specified XML subtree. If succesfully loaded,
290: * the value should be available via the getValue method.
291: * An IOException should be thrown when the value cannot be restored from the specified XML element
292: * @param element the XML DOM element representing a subtree of XML from which the value should be loaded
293: * @exception IOException thrown when the value cannot be restored from the specified XML element
294: */
295: public void readFromXML(org.w3c.dom.Node element)
296: throws java.io.IOException {
297: if (!XML_CURSOR.equals(element.getNodeName())) {
298: throw new java.io.IOException();
299: }
300: org.w3c.dom.NamedNodeMap attributes = element.getAttributes();
301: try {
302: String id = attributes.getNamedItem(ATTR_ID).getNodeValue();
303: setAsText(id);
304: } catch (NullPointerException e) {
305: throw new java.io.IOException();
306: }
307: }
308:
309: /** Called to store current property value into XML subtree. The property value should be set using the
310: * setValue method prior to calling this method.
311: * @param doc The XML document to store the XML in - should be used for creating nodes only
312: * @return the XML DOM element representing a subtree of XML from which the value should be loaded
313: */
314: public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) {
315: org.w3c.dom.Element el = doc.createElement(XML_CURSOR);
316: el.setAttribute(ATTR_ID, getAsText());
317: return el;
318: }
319:
320: // ------------------------------------------
321: // NamedPropertyEditor implementation
322:
323: /** @return display name of the property editor */
324: public String getDisplayName() {
325: return org.openide.util.NbBundle.getBundle(CursorEditor.class)
326: .getString("CTL_CursorEditor_DisplayName");
327: }
328:
329: }
|