~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/multiview/src/org/netbeans/modules/xml/multiview/ItemEditorHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.xml.multiview;
 
42
 
 
43
import javax.swing.text.AttributeSet;
 
44
import javax.swing.text.BadLocationException;
 
45
import javax.swing.text.JTextComponent;
 
46
import javax.swing.text.PlainDocument;
 
47
 
 
48
/**
 
49
 * The class provides link between editor text component and related data model
 
50
 * to make easier implementation of item editors
 
51
 *
 
52
 * @author pfiala
 
53
 */
 
54
public class ItemEditorHelper implements Refreshable {
 
55
 
 
56
    protected ItemEditorHelper.ItemDocument doc;
 
57
 
 
58
    /**
 
59
     * Model of item providing unified interface between text component and item data
 
60
     */
 
61
    public static abstract class ItemEditorModel {
 
62
 
 
63
        private ItemEditorHelper itemEditorHelper;
 
64
 
 
65
        /**
 
66
         * Retrieves edited text from editor component
 
67
         *
 
68
         * @return text
 
69
         */
 
70
        public final String getEditorText() {
 
71
            return itemEditorHelper == null ? null : itemEditorHelper.getEditorText();
 
72
        }
 
73
 
 
74
        /**
 
75
         * Editor component getter
 
76
         *
 
77
         * @return editor component
 
78
         */
 
79
        public final JTextComponent getEditorComponent() {
 
80
            return itemEditorHelper == null ? null : itemEditorHelper.getEditorComponent();
 
81
        }
 
82
 
 
83
        /**
 
84
         * Called by editor helper to retrieve item value from model
 
85
         *
 
86
         * @return value of edited item
 
87
         */
 
88
        public abstract String getItemValue();
 
89
 
 
90
        /**
 
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
 
94
         *
 
95
         * @param value a new value of edited item
 
96
         * @return true - the new value is accepted, false - the new value is invalid
 
97
         */
 
98
        public abstract boolean setItemValue(String value);
 
99
 
 
100
        /**
 
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
 
104
         */
 
105
        public abstract void documentUpdated();
 
106
 
 
107
    }
 
108
 
 
109
    private JTextComponent getEditorComponent() {
 
110
        return editorComponent;
 
111
    }
 
112
 
 
113
    private final JTextComponent editorComponent;
 
114
    private ItemEditorModel model;
 
115
 
 
116
    /**
 
117
     * Creates item editor helper for given text component with default implementation of data model.
 
118
     *
 
119
     * @param textComponent editor component
 
120
     */
 
121
    public ItemEditorHelper(final JTextComponent textComponent) {
 
122
        this(textComponent, null);
 
123
    }
 
124
 
 
125
    /**
 
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.
 
130
     *
 
131
     * @param textComponent editor component
 
132
     * @param model         item data model defining behavior
 
133
     */
 
134
    public ItemEditorHelper(final JTextComponent textComponent, ItemEditorModel model) {
 
135
        this.editorComponent = textComponent;
 
136
        doc = new ItemDocument();
 
137
        setModel(model);
 
138
        editorComponent.setDocument(doc);
 
139
        refresh();
 
140
    }
 
141
 
 
142
    /**
 
143
     * Gets item data model of the item editor helper
 
144
     *
 
145
     * @return item data model
 
146
     */
 
147
    public ItemEditorModel getModel() {
 
148
        return model;
 
149
    }
 
150
 
 
151
    private void setModel(ItemEditorModel model) {
 
152
        this.model = model != null ? model : createDefaultModel();
 
153
        this.model.itemEditorHelper = this;
 
154
    }
 
155
 
 
156
    private static ItemEditorModel createDefaultModel() {
 
157
        return new ItemEditorModel() {
 
158
            private String value;
 
159
 
 
160
            public String getItemValue() {
 
161
                return value;
 
162
            }
 
163
 
 
164
            public boolean setItemValue(String value) {
 
165
                this.value = value;
 
166
                return true;
 
167
            }
 
168
 
 
169
            public void documentUpdated() {
 
170
            }
 
171
        };
 
172
    }
 
173
 
 
174
    /**
 
175
     * Updates editor text by item value from model
 
176
     */
 
177
    public void refresh() {
 
178
        doc.refresh();
 
179
    }
 
180
 
 
181
    /**
 
182
     * Retrieves edited text from editor component
 
183
     *
 
184
     * @return text of editor component
 
185
     */
 
186
    public String getEditorText() {
 
187
        return editorComponent.getText();
 
188
    }
 
189
 
 
190
    private class ItemDocument extends PlainDocument {
 
191
 
 
192
        boolean refreshing = false;
 
193
 
 
194
        public void remove(int offs, int len) throws BadLocationException {
 
195
            super.remove(offs, len);
 
196
            updateModel();
 
197
        }
 
198
 
 
199
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
 
200
            super.insertString(offs, str, a);
 
201
            updateModel();
 
202
        }
 
203
 
 
204
        private void updateModel() {
 
205
            if (!refreshing) {
 
206
                model.documentUpdated();
 
207
                refresh();
 
208
            }
 
209
        }
 
210
 
 
211
        public void refresh() {
 
212
            refreshing = true;
 
213
            try {
 
214
                String itemValue = model.getItemValue();
 
215
                String text;
 
216
                try {
 
217
                    text = getText(0, getLength());
 
218
                } catch (BadLocationException e) {
 
219
                    text = "";
 
220
                    e.printStackTrace();
 
221
                }
 
222
                if (!text.equals(itemValue)) {
 
223
                    try {
 
224
                        super.remove(0, getLength());
 
225
                    } catch (BadLocationException e) {
 
226
                        e.printStackTrace();
 
227
                    }
 
228
                    try {
 
229
                        super.insertString(0, itemValue, null);
 
230
                    } catch (BadLocationException e) {
 
231
                        e.printStackTrace();
 
232
                    }
 
233
                }
 
234
            } finally {
 
235
                refreshing = false;
 
236
            }
 
237
        }
 
238
    }
 
239
}