KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > form > FormI18nString


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.form;
22
23 import org.netbeans.modules.form.I18nValue;
24 import org.netbeans.modules.form.FormDesignValue;
25 import org.netbeans.modules.form.FormEditor;
26 import org.netbeans.modules.form.FormModel;
27 import org.netbeans.modules.form.FormProperty;
28 import org.netbeans.modules.i18n.I18nSupport;
29 import org.netbeans.modules.i18n.I18nUtil;
30 import org.netbeans.modules.i18n.ResourceHolder;
31 import org.netbeans.modules.i18n.java.JavaI18nString;
32 import org.netbeans.modules.i18n.java.JavaResourceHolder;
33 import org.openide.loaders.DataObject;
34
35 /**
36  * This class extends the capability of <code>JavaI18nString</code> to be
37  * <code>FormDesignValue</code> to be used in form property sheets.
38  *
39  * @author Peter Zavadsky
40  * @see org.netbeans.modules.i18n.java.JavaI18nString
41  * @see ResourceBundleStringFormEditor
42  * @see org.netbeans.modules.form.FormDesignValue
43  */

44 public class FormI18nString extends JavaI18nString implements I18nValue {
45
46     String JavaDoc bundleName; // to be saved again if file can't be found after form is loaded
47

48     Object JavaDoc allData; // complete data for given key across all locales
49
// stored here for undo/redo purposes
50

51     /** Creates new <code>FormI18nString</code>. */
52     public FormI18nString(I18nSupport i18nSupport) {
53         super(i18nSupport);
54     }
55
56     /** Cretaes new <code>FormI18nString</code> from <code>JavaI18nString</code>.
57      * @param source source which is created new <code>FormI18nString</code> from. */

58     public FormI18nString(JavaI18nString source) {
59         this(createNewSupport(source.getSupport()),
60              source.getKey(),
61              source.getValue(),
62              source.getComment(),
63              source.getArguments(),
64              source.getReplaceFormat());
65     }
66
67     FormI18nString(DataObject srcDataObject) {
68         super(new FormI18nSupport.Factory().createI18nSupport(srcDataObject));
69
70         boolean nbBundle = org.netbeans.modules.i18n.Util.isNbBundleAvailable(srcDataObject);
71         if (I18nUtil.getDefaultReplaceFormat(!nbBundle).equals(getReplaceFormat())) {
72             setReplaceFormat(I18nUtil.getDefaultReplaceFormat(nbBundle));
73         }
74     }
75
76     private FormI18nString(I18nSupport i18nSupport, String JavaDoc key, String JavaDoc value, String JavaDoc commment, String JavaDoc[] arguments, String JavaDoc replaceFormat) {
77         super(i18nSupport);
78
79         this.key = key;
80         this.value = value;
81         this.comment = comment;
82         
83         this.arguments = arguments;
84         this.replaceFormat = replaceFormat;
85     }
86
87     public Object JavaDoc copy(FormProperty formProperty) {
88         FormModel form = formProperty.getPropertyContext().getFormModel();
89         if (form == null)
90             return getValue();
91
92         DataObject sourceDO = FormEditor.getFormDataObject(form);
93         if (sourceDO == null)
94             return getValue();
95
96         boolean sameForm = (sourceDO == support.getSourceDataObject());
97         boolean autoMode = form.getSettings().getI18nAutoMode();
98         DataObject resource = sameForm || !autoMode ?
99                               support.getResourceHolder().getResource() : null;
100         I18nSupport newSupport = createNewSupport(sourceDO, resource);
101
102         FormI18nString newI18nString;
103         if (autoMode) { // need auto-generated key (form module must provide)
104
newI18nString = new FormI18nString(newSupport,
105                                 COMPUTE_AUTO_KEY, getValue(), getComment(),
106                                 getArguments(), getReplaceFormat());
107             JavaResourceHolder jrh = (JavaResourceHolder) support.getResourceHolder();
108             newI18nString.allData = jrh.getAllData(getKey());
109         }
110         else {
111             newI18nString = new FormI18nString(newSupport,
112                                 getKey(), getValue(), getComment(),
113                                 getArguments(), getReplaceFormat());
114             if (!sameForm) { // make sure the value is actual according to the target locale
115
ResourceHolder rh = newSupport.getResourceHolder();
116                 newI18nString.value = rh.getValueForKey(getKey());
117                 newI18nString.comment = rh.getCommentForKey(getKey());
118             }
119         }
120         return newI18nString;
121     }
122
123     public String JavaDoc toString() {
124         return getValue();
125     }
126
127     private static I18nSupport createNewSupport(I18nSupport support) {
128         return createNewSupport(support.getSourceDataObject(), support.getResourceHolder().getResource());
129     }
130
131     private static I18nSupport createNewSupport(DataObject sourceDataObject, DataObject resource) {
132         I18nSupport newSupport = new FormI18nSupport.Factory().createI18nSupport(sourceDataObject);
133         if(resource != null) {
134             newSupport.getResourceHolder().setResource(resource);
135         }
136         return newSupport;
137     }
138
139     /**
140      * Implements <code>FormDesignValue</code> interface. Gets design value.
141      * @see org.netbeans.modules.form.FormDesignValue#getDesignValue(RADComponent radComponent) */

142     public Object JavaDoc getDesignValue() {
143         String JavaDoc designValue = getValue(); //getSupport().getResourceHolder().getValueForKey(getKey());
144

145         if(designValue == null)
146             return FormDesignValue.IGNORED_VALUE;
147         else
148             return designValue;
149     }
150     
151     /** Gets description of the design value. Implements <code>FormDesignValue</code> interface.
152      * @return key value */

153     public String JavaDoc getDescription() {
154         return "<" + getKey() + ">"; // NOI18N
155
}
156 }
157
Popular Tags