KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ItemEditorHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.multiview;
20
21 import javax.swing.text.AttributeSet JavaDoc;
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.JTextComponent JavaDoc;
24 import javax.swing.text.PlainDocument JavaDoc;
25
26 /**
27  * The class provides link between editor text component and related data model
28  * to make easier implementation of item editors
29  *
30  * @author pfiala
31  */

32 public class ItemEditorHelper implements Refreshable {
33
34     protected ItemEditorHelper.ItemDocument doc;
35
36     /**
37      * Model of item providing unified interface between text component and item data
38      */

39     public static abstract class ItemEditorModel {
40
41         private ItemEditorHelper itemEditorHelper;
42
43         /**
44          * Retrieves edited text from editor component
45          *
46          * @return text
47          */

48         public final String JavaDoc getEditorText() {
49             return itemEditorHelper == null ? null : itemEditorHelper.getEditorText();
50         }
51
52         /**
53          * Editor component getter
54          *
55          * @return editor component
56          */

57         public final JTextComponent JavaDoc getEditorComponent() {
58             return itemEditorHelper == null ? null : itemEditorHelper.getEditorComponent();
59         }
60
61         /**
62          * Called by editor helper to retrieve item value from model
63          *
64          * @return value of edited item
65          */

66         public abstract String JavaDoc getItemValue();
67
68         /**
69          * Called by the editor helper when finished editing of the text component.
70          * An implementation can perform validation, update item value by the editor text
71          * or define behavior in case of fail
72          *
73          * @param value a new value of edited item
74          * @return true - the new value is accepted, false - the new value is invalid
75          */

76         public abstract boolean setItemValue(String JavaDoc value);
77
78         /**
79          * Called by the editor support whenever edited text is changed.
80          * An implementation can perform immediate validation
81          * or update of item value in the model
82          */

83         public abstract void documentUpdated();
84
85     }
86
87     private JTextComponent JavaDoc getEditorComponent() {
88         return editorComponent;
89     }
90
91     private final JTextComponent JavaDoc editorComponent;
92     private ItemEditorModel model;
93
94     /**
95      * Creates item editor helper for given text component with default implementation of data model.
96      *
97      * @param textComponent editor component
98      */

99     public ItemEditorHelper(final JTextComponent JavaDoc textComponent) {
100         this(textComponent, null);
101     }
102
103     /**
104      * Creates item editor helper for given text component using user defined data model.
105      * Various implementations of model's methods {@link ItemEditorHelper.ItemEditorModel#getItemValue()},
106      * {@link ItemEditorHelper.ItemEditorModel#setItemValue(String)} and
107      * {@link ItemEditorHelper.ItemEditorModel#documentUpdated()} can define required behavior of the editor.
108      *
109      * @param textComponent editor component
110      * @param model item data model defining behavior
111      */

112     public ItemEditorHelper(final JTextComponent JavaDoc textComponent, ItemEditorModel model) {
113         this.editorComponent = textComponent;
114         doc = new ItemDocument();
115         setModel(model);
116         editorComponent.setDocument(doc);
117         refresh();
118     }
119
120     /**
121      * Gets item data model of the item editor helper
122      *
123      * @return item data model
124      */

125     public ItemEditorModel getModel() {
126         return model;
127     }
128
129     private void setModel(ItemEditorModel model) {
130         this.model = model != null ? model : createDefaultModel();
131         this.model.itemEditorHelper = this;
132     }
133
134     private static ItemEditorModel createDefaultModel() {
135         return new ItemEditorModel() {
136             private String JavaDoc value;
137
138             public String JavaDoc getItemValue() {
139                 return value;
140             }
141
142             public boolean setItemValue(String JavaDoc value) {
143                 this.value = value;
144                 return true;
145             }
146
147             public void documentUpdated() {
148             }
149         };
150     }
151
152     /**
153      * Updates editor text by item value from model
154      */

155     public void refresh() {
156         doc.refresh();
157     }
158
159     /**
160      * Retrieves edited text from editor component
161      *
162      * @return text of editor component
163      */

164     public String JavaDoc getEditorText() {
165         return editorComponent.getText();
166     }
167
168     private class ItemDocument extends PlainDocument JavaDoc {
169
170         boolean refreshing = false;
171
172         public void remove(int offs, int len) throws BadLocationException JavaDoc {
173             super.remove(offs, len);
174             updateModel();
175         }
176
177         public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a) throws BadLocationException JavaDoc {
178             super.insertString(offs, str, a);
179             updateModel();
180         }
181
182         private void updateModel() {
183             if (!refreshing) {
184                 model.documentUpdated();
185                 refresh();
186             }
187         }
188
189         public void refresh() {
190             refreshing = true;
191             try {
192                 String JavaDoc itemValue = model.getItemValue();
193                 String JavaDoc text;
194                 try {
195                     text = getText(0, getLength());
196                 } catch (BadLocationException JavaDoc e) {
197                     text = "";
198                     e.printStackTrace();
199                 }
200                 if (!text.equals(itemValue)) {
201                     try {
202                         super.remove(0, getLength());
203                     } catch (BadLocationException JavaDoc e) {
204                         e.printStackTrace();
205                     }
206                     try {
207                         super.insertString(0, itemValue, null);
208                     } catch (BadLocationException JavaDoc e) {
209                         e.printStackTrace();
210                     }
211                 }
212             } finally {
213                 refreshing = false;
214             }
215         }
216     }
217 }
218
Popular Tags