KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > dialog > model > TextFieldModel


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.dialog.model;
25
26 import org.objectweb.fractal.gui.model.Component;
27
28 import javax.swing.text.AttributeSet JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.PlainDocument JavaDoc;
31
32 /**
33  * A {@link javax.swing.text.Document} model based on a {@link Component} model.
34  * This model makes a conversion from a {@link Component} model to a
35  * {@link javax.swing.text.Document}. It represents the name, the type, the
36  * implementation... of this component, depending on how it was constructed.
37  */

38
39 public class TextFieldModel extends PlainDocument JavaDoc {
40
41   /**
42    * Type of a {@link TextFieldModel} that represents component names.
43    * See {@link #TextFieldModel TextFieldModel}.
44    */

45
46   public final static int NAME = 0;
47
48   /**
49    * Type of a {@link TextFieldModel} that represents component types.
50    * See {@link #TextFieldModel TextFieldModel}.
51    */

52
53   public final static int TYPE = 1;
54
55   /**
56    * Type of a {@link TextFieldModel} that represents component implementations.
57    * See {@link #TextFieldModel TextFieldModel}.
58    */

59
60   public final static int IMPLEMENTATION = 2;
61
62   /**
63    * Type of a {@link TextFieldModel} that represents component attribute
64    * controllers. See {@link #TextFieldModel TextFieldModel}.
65    */

66
67   public final static int ATTRIBUTE_CONTROLLER = 3;
68
69   /**
70    * Type of a {@link TextFieldModel} that represents component template
71    * controller descriptors. See {@link #TextFieldModel TextFieldModel}.
72    */

73
74   public final static int TEMPLATE_CONTROLLER_DESC = 4;
75
76   /**
77    * Type of a {@link TextFieldModel} that represents component controller
78    * descriptors. See {@link #TextFieldModel TextFieldModel}.
79    */

80
81   public final static int COMPONENT_CONTROLLER_DESC = 5;
82
83   /**
84    * The type of this model. This type must be one of the constants defined in
85    * this class.
86    */

87
88   private int type;
89
90   /**
91    * The component model on which this model is based.
92    */

93
94   private Component model;
95
96   /**
97    * If the {@link #insertString} or {@link #remove} method is currently being
98    * executed. This flag is used to ignore some notifications, in order to avoid
99    * infinite loops (see
100    * <a HREF="../../../../../../overview-summary.html#mvc">here</a>).
101    */

102
103   private boolean isTyping;
104
105   /**
106    * Constructs a new {@link TextFieldModel} object.
107    *
108    * @param type code of the component's part that must be represented by this
109    * model. This code must be one of the constants defined in this class.
110    */

111
112   public TextFieldModel (final int type) {
113     this.type = type;
114   }
115
116   /**
117    * Sets the component model on which this model is based.
118    *
119    * @param model a component.
120    */

121
122   void setComponentModel (final Component model) {
123     this.model = model;
124     String JavaDoc s = null;
125     if (model != null) {
126       switch (type) {
127         case NAME:
128           s = model.getName();
129           break;
130         case TYPE:
131           s = model.getType();
132           break;
133         case IMPLEMENTATION:
134           s = model.getImplementation();
135           break;
136         case ATTRIBUTE_CONTROLLER:
137           s = model.getAttributeController();
138           break;
139         case TEMPLATE_CONTROLLER_DESC:
140           s = model.getTemplateControllerDescriptor();
141           break;
142         case COMPONENT_CONTROLLER_DESC:
143           s = model.getComponentControllerDescriptor();
144           break;
145         default:
146       }
147     }
148     componentTextChanged(s);
149   }
150
151   /**
152    * Notifies this listener that the part of the component model on which it is
153    * based has changed.
154    *
155    * @param s the new component's name, type, implementation... depending on the
156    * {@link #type} of this model.
157    */

158
159   void componentTextChanged (final String JavaDoc s) {
160     try {
161       if (!isTyping) {
162         super.remove(0, getLength());
163         super.insertString(0, s, null);
164       }
165     } catch (BadLocationException JavaDoc ignored) {
166     }
167   }
168
169   // -------------------------------------------------------------------------
170
// Overriden PlainDocument methods
171
// -------------------------------------------------------------------------
172

173   public void insertString (
174     final int offs,
175     final String JavaDoc str,
176     final AttributeSet JavaDoc a) throws BadLocationException JavaDoc
177   {
178     super.insertString(offs, str, a);
179     setComponentText(getText(0, getLength()));
180   }
181
182   public void remove (
183     final int offs,
184     final int len) throws BadLocationException JavaDoc
185   {
186     super.remove(offs, len);
187     setComponentText(getText(0, getLength()));
188   }
189
190   /**
191    * Modifies the model on which this model is based, to reflect a change in
192    * this model.
193    *
194    * @param s the new component's name, type, implementation... depending on the
195    * {@link #type} of this model.
196    */

197
198   private void setComponentText (final String JavaDoc s) {
199     if (model == null) {
200       return;
201     }
202     isTyping = true;
203     try {
204       switch (type) {
205         case NAME:
206           model.setName(s);
207           break;
208         case TYPE:
209           model.setType(s);
210           break;
211         case IMPLEMENTATION:
212           model.setImplementation(s);
213           break;
214         case ATTRIBUTE_CONTROLLER:
215           model.setAttributeController(s);
216           break;
217         case TEMPLATE_CONTROLLER_DESC:
218           model.setTemplateControllerDescriptor(s);
219           break;
220         case COMPONENT_CONTROLLER_DESC:
221           model.setComponentControllerDescriptor(s);
222           break;
223         default:
224       }
225     } finally {
226       isTyping = false;
227     }
228   }
229 }
230
Popular Tags