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.
41
package org.netbeans.modules.xml.multiview;
43
import javax.swing.text.AttributeSet;
44
import javax.swing.text.BadLocationException;
45
import javax.swing.text.JTextComponent;
46
import javax.swing.text.PlainDocument;
49
* The class provides link between editor text component and related data model
50
* to make easier implementation of item editors
54
public class ItemEditorHelper implements Refreshable {
56
protected ItemEditorHelper.ItemDocument doc;
59
* Model of item providing unified interface between text component and item data
61
public static abstract class ItemEditorModel {
63
private ItemEditorHelper itemEditorHelper;
66
* Retrieves edited text from editor component
70
public final String getEditorText() {
71
return itemEditorHelper == null ? null : itemEditorHelper.getEditorText();
75
* Editor component getter
77
* @return editor component
79
public final JTextComponent getEditorComponent() {
80
return itemEditorHelper == null ? null : itemEditorHelper.getEditorComponent();
84
* Called by editor helper to retrieve item value from model
86
* @return value of edited item
88
public abstract String getItemValue();
91
* Called by the editor helper when finished editing of the text component.
92
* An implementation can perform validation, update item value by the editor text
93
* or define behavior in case of fail
95
* @param value a new value of edited item
96
* @return true - the new value is accepted, false - the new value is invalid
98
public abstract boolean setItemValue(String value);
101
* Called by the editor support whenever edited text is changed.
102
* An implementation can perform immediate validation
103
* or update of item value in the model
105
public abstract void documentUpdated();
109
private JTextComponent getEditorComponent() {
110
return editorComponent;
113
private final JTextComponent editorComponent;
114
private ItemEditorModel model;
117
* Creates item editor helper for given text component with default implementation of data model.
119
* @param textComponent editor component
121
public ItemEditorHelper(final JTextComponent textComponent) {
122
this(textComponent, null);
126
* Creates item editor helper for given text component using user defined data model.
127
* Various implementations of model's methods {@link ItemEditorHelper.ItemEditorModel#getItemValue()},
128
* {@link ItemEditorHelper.ItemEditorModel#setItemValue(String)} and
129
* {@link ItemEditorHelper.ItemEditorModel#documentUpdated()} can define required behavior of the editor.
131
* @param textComponent editor component
132
* @param model item data model defining behavior
134
public ItemEditorHelper(final JTextComponent textComponent, ItemEditorModel model) {
135
this.editorComponent = textComponent;
136
doc = new ItemDocument();
138
editorComponent.setDocument(doc);
143
* Gets item data model of the item editor helper
145
* @return item data model
147
public ItemEditorModel getModel() {
151
private void setModel(ItemEditorModel model) {
152
this.model = model != null ? model : createDefaultModel();
153
this.model.itemEditorHelper = this;
156
private static ItemEditorModel createDefaultModel() {
157
return new ItemEditorModel() {
158
private String value;
160
public String getItemValue() {
164
public boolean setItemValue(String value) {
169
public void documentUpdated() {
175
* Updates editor text by item value from model
177
public void refresh() {
182
* Retrieves edited text from editor component
184
* @return text of editor component
186
public String getEditorText() {
187
return editorComponent.getText();
190
private class ItemDocument extends PlainDocument {
192
boolean refreshing = false;
194
public void remove(int offs, int len) throws BadLocationException {
195
super.remove(offs, len);
199
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
200
super.insertString(offs, str, a);
204
private void updateModel() {
206
model.documentUpdated();
211
public void refresh() {
214
String itemValue = model.getItemValue();
217
text = getText(0, getLength());
218
} catch (BadLocationException e) {
222
if (!text.equals(itemValue)) {
224
super.remove(0, getLength());
225
} catch (BadLocationException e) {
229
super.insertString(0, itemValue, null);
230
} catch (BadLocationException e) {