KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > I18nSupport


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
20
21 package org.netbeans.modules.i18n;
22
23
24 import java.io.IOException JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28 import org.openide.cookies.EditorCookie;
29 import org.openide.loaders.DataObject;
30
31
32 /**
33  * Abstract class. Supports 'one i18n session' -> i18n-zing of one source file.
34  * Used as a base class for concrete support implementations.
35  *
36  * @author Peter Zavadsky
37  */

38 public abstract class I18nSupport {
39
40     /** <code>I18nFinder</code>. */
41     private I18nFinder finder;
42
43     /** <code>I18nReplacer</code>. */
44     private I18nReplacer replacer;
45
46     /** <code>DataObject</code> which document the i18n session will run thru. */
47     protected DataObject sourceDataObject;
48
49     /** Document on which the i18n-session will be performed. */
50     protected StyledDocument JavaDoc document;
51     
52     /** Resource holder for sepcific subclass instance. */
53     protected final ResourceHolder resourceHolder;
54     
55
56     /** Constructor. Note: It gets document from data object's editor cookie. If the document
57      * is not available it tries to load it. Therefore this construction could take some time.
58      * @exception IOException when the document is could not be loaded
59      */

60     public I18nSupport(DataObject sourceDataObject) {
61         this.sourceDataObject = sourceDataObject;
62         
63         EditorCookie editorCookie = (EditorCookie)sourceDataObject.getCookie(EditorCookie.class);
64         
65         if(editorCookie == null)
66             throw new IllegalArgumentException JavaDoc("I18N: Illegal data object type"+ sourceDataObject); // NOI18N
67

68         this.document = editorCookie.getDocument();
69         
70         this.resourceHolder = createResourceHolder();
71     }
72     
73
74     /** Loads document if was not get in constructor should be called after creation if the
75      * work on document is necessary. */

76     private void loadDocument() throws IOException JavaDoc {
77         if(document == null) {
78             EditorCookie editorCookie = (EditorCookie)sourceDataObject.getCookie(EditorCookie.class);
79
80             if(editorCookie == null)
81                 throw new IllegalArgumentException JavaDoc("I18N: Illegal data object type"+ sourceDataObject); // NOI18N
82

83             document = editorCookie.openDocument();
84         }
85     }
86     
87     /** Creates <code>I18nFinder</code> instance used by this instance. */
88     protected abstract I18nFinder createFinder();
89     
90     /** Cretates <code>I18nReplacer</code> instance used by this instance. */
91     protected abstract I18nReplacer createReplacer();
92     
93     /** Creates <code>ResourceHolder<code> for this instance. */
94     protected abstract ResourceHolder createResourceHolder();
95
96     /** Gets <code>I18nFinder</code>. */
97     public final I18nFinder getFinder() {
98         if(finder == null)
99             finder = createFinder();
100             
101         return finder;
102     }
103     
104     /** Gets <code>I18nReplacer</code> for this support. */
105     public final I18nReplacer getReplacer() {
106         if(replacer == null)
107             replacer = createReplacer();
108         
109         return replacer;
110     }
111
112     /** Getter for <code>sourceDataObject</code> property. */
113     public final DataObject getSourceDataObject() {
114         return sourceDataObject;
115     }
116     
117     /** Getter for <code>document</code> property. */
118     public final StyledDocument JavaDoc getDocument() {
119         return document;
120     }
121     
122     /** Gets default <code>I18nString</code> instance for this i18n session,
123      * has a non-null resource holder field, but resource of that holder may be not initialized yet. */

124     public I18nString getDefaultI18nString() {
125         return getDefaultI18nString(null);
126     }
127     
128     /** Gets default <code>I18nString</code> for this instance. */
129     public abstract I18nString getDefaultI18nString(HardCodedString hcString);
130     
131     /** Gets JPanel showing info about found hard coded value. */
132     public abstract JPanel JavaDoc getInfo(HardCodedString hcString);
133
134     /** Getter for <code>resourceHolder</code>. */
135     public ResourceHolder getResourceHolder() {
136         return resourceHolder;
137     }
138
139     /** Gets <code>PropertyPanel</code> which is used for customizing <code>i18nStrings</code>
140      * linked to this support instance. Is possible to override in subclasses if any additional customization is needed. */

141     public PropertyPanel getPropertyPanel() {
142         return new PropertyPanel();
143     }
144     
145     /** Indicates if supports customizer for additional source specific values. Override in subclasses if nedded.
146      * @return false
147      * @see #getAdditionalCustommizer */

148     public boolean hasAdditionalCustomizer() {
149         return false;
150     }
151     
152     /** Gets additional customizer. Override in subclasses if needed.
153      * @return null
154      * @see #hasAdditionalCustomizer */

155     public JPanel JavaDoc getAdditionalCustomizer() {
156         return null;
157     }
158     
159     /** Provides addtional changes. Usually connected with values
160      * set via <code>getAdditionalCustomizer</code> method. Override in subclasses.
161      * Default implementation does nothing. */

162     public void performAdditionalChanges() {
163     }
164
165     
166     /**
167      * Interface for finder which will search for hard coded (non-i18n-ized)
168      * string in i18n-ized source document.
169      */

170     public interface I18nFinder {
171
172         /** Gets next hard coded string. Starts from the beginning of the source.
173          * @return next hard coded string or null if the search reached end. */

174         public HardCodedString findNextHardCodedString();
175
176         /** Gets all hard coded strings from document.
177          * @return all hard coded strings from source document or null if such don't exist */

178         public HardCodedString[] findAllHardCodedStrings();
179         
180         /** Gets next hard coded but i18n-ized string. Starts from the beginning of the source. Used in test tool.
181          * @return next hard coded string or null if the search reached end. */

182         public HardCodedString findNextI18nString();
183
184         /** Gets all i18n-zed hard coded strings from document. Used in test tool.
185          * @return all hard coded strings from source document or null if such don't exist */

186         public HardCodedString[] findAllI18nStrings();
187     }
188
189     /**
190      * Interface implemented by objects which replaces (i18n-zes) hard coded strings
191      * source specific way. (The way of i18n-zing source is different from java file to
192      * jsp file etc.)
193      */

194     public interface I18nReplacer {
195
196         /** Replaces hard coded string using settigns encapsulated by <code>I18nString</code> typically customized by user. */
197         public void replace(HardCodedString hardString, I18nString i18nString);
198     }
199
200     /** Factory inteface for creating <code>I18nSupport</code> instances. */
201     public static abstract class Factory {
202         
203         /** Gets <code>I18nSupport</code> instance for specified data object and document.
204          * @exception IOException when the document could not be loaded */

205         public I18nSupport create(DataObject dataObject) throws IOException JavaDoc {
206             I18nSupport support = createI18nSupport(dataObject);
207             support.loadDocument();
208           
209             return support;
210         };
211         
212         /** Actually creates i18n support instance. */
213         protected abstract I18nSupport createI18nSupport(DataObject dataObject);
214         
215         /** Gets class type of <code>DataObject</code> which can to internationalize. */
216         public abstract Class JavaDoc getDataObjectClass();
217         
218     } // End of nested Factory class.
219

220 }
221
222
Popular Tags