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-2007 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: package org.netbeans.modules.sql.framework.ui.editor.property.impl;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.event.ActionListener;
047: import java.beans.PropertyEditor;
048: import java.beans.PropertyEditorSupport;
049: import java.util.Vector;
050: import javax.swing.JComponent;
051: import javax.swing.JList;
052: import javax.swing.JPanel;
053: import javax.swing.JPasswordField;
054: import javax.swing.JScrollPane;
055: import javax.swing.JTextField;
056: import javax.swing.KeyStroke;
057: import javax.swing.ListModel;
058: import org.openide.explorer.propertysheet.ExPropertyEditor;
059: import org.openide.explorer.propertysheet.InplaceEditor;
060: import org.openide.explorer.propertysheet.PropertyEnv;
061: import org.openide.explorer.propertysheet.PropertyModel;
062:
063: /**
064: * @author Ritesh Adval
065: * @version $Revision$
066: */
067: public class DefaultPropertyEditor {
068:
069: /**
070: * Concrete implementation of PropertyEditorSupport to provide a list editor widget
071: * for use in a bean property sheet.
072: */
073: public static class ListEditor extends PropertyEditorSupport {
074:
075: JList list;
076: JPanel panel;
077: JScrollPane sPane;
078:
079: public ListEditor(ListModel model) {
080: this ();
081: list = new JList(model);
082: panel.add(list, BorderLayout.CENTER);
083: }
084:
085: public ListEditor(Vector listData) {
086: this ();
087: list = new JList(listData);
088: panel.add(list, BorderLayout.CENTER);
089: }
090:
091: private ListEditor() {
092: panel = new JPanel();
093: panel.setLayout(new BorderLayout());
094: sPane = new JScrollPane(panel);
095: sPane.setPreferredSize(new Dimension(300, 150));
096: }
097:
098: @Override
099: public Component getCustomEditor() {
100: return sPane;
101: }
102:
103: @Override
104: public boolean supportsCustomEditor() {
105: return true;
106: }
107: }
108:
109: public static class PasswordTextEditor extends
110: PropertyEditorSupport implements ExPropertyEditor,
111: InplaceEditor.Factory {
112:
113: private InplaceEditor ed = null;
114:
115: public InplaceEditor getInplaceEditor() {
116: if (ed == null) {
117: ed = new InplacePassword();
118: }
119: return ed;
120: }
121:
122: public void attachEnv(PropertyEnv env) {
123: env.registerInplaceEditorFactory(this );
124: }
125:
126: @Override
127: public String getAsText() {
128: return getEncPassword((String) this .getValue());
129: }
130:
131: private String getEncPassword(String val) {
132: StringBuilder buf = new StringBuilder(30);
133: for (int i = 0; i < val.length(); i++) {
134: buf.append('*');
135: }
136: return buf.toString();
137: }
138: }
139:
140: private static class InplacePassword implements InplaceEditor {
141:
142: private JPasswordField passField = new JPasswordField();
143: private PropertyEditor editor = null;
144:
145: public void connect(PropertyEditor propertyEditor,
146: PropertyEnv env) {
147: editor = propertyEditor;
148: reset();
149: }
150:
151: public JComponent getComponent() {
152: return passField;
153: }
154:
155: public void clear() {
156: //avoid memory leaks:
157: editor = null;
158: model = null;
159: }
160:
161: public Object getValue() {
162: return new String(passField.getPassword());
163: }
164:
165: public void setValue(Object value) {
166: passField.setText((String) value);
167: }
168:
169: public boolean supportsTextEntry() {
170: return true;
171: }
172:
173: public void reset() {
174: String value = (String) editor.getValue();
175: if (value != null) {
176: passField.setText(value);
177: }
178: }
179:
180: public KeyStroke[] getKeyStrokes() {
181: return new KeyStroke[0];
182: }
183:
184: public PropertyEditor getPropertyEditor() {
185: return editor;
186: }
187:
188: public PropertyModel getPropertyModel() {
189: return model;
190: }
191:
192: private PropertyModel model;
193:
194: public void setPropertyModel(PropertyModel propertyModel) {
195: this .model = propertyModel;
196: }
197:
198: public boolean isKnownComponent(Component component) {
199: return component == passField
200: || passField.isAncestorOf(component);
201: }
202:
203: public void addActionListener(ActionListener actionListener) {
204: //do nothing - not needed for this component
205: }
206:
207: public void removeActionListener(ActionListener actionListener) {
208: //do nothing - not needed for this component
209: }
210: }
211:
212: /**
213: * Concrete implementation of PropertyEditorSupport to provide a single-line textfield
214: * widget (with no custom editor) for use in a bean property sheet.
215: */
216: public static class SingleLineTextEditor extends
217: PropertyEditorSupport {
218:
219: private JTextField tf;
220:
221: public SingleLineTextEditor() {
222: super ();
223: tf = new JTextField();
224: }
225:
226: @Override
227: public Component getCustomEditor() {
228: return tf;
229: }
230:
231: @Override
232: public boolean supportsCustomEditor() {
233: return false;
234: }
235: }
236:
237: /** Creates a new instance of DefaultPropertyEditor */
238: public DefaultPropertyEditor() {
239: }
240: }
|