KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > editors > NameValuePairsPropertyEditor


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  * ParamPropertyEditorEditor.java
21  *
22  * Created on January 15, 2002, 2:05 PM
23  *
24  * Author: Shirley Chiang
25  */

26
27 package org.netbeans.modules.j2ee.sun.ide.editors;
28
29 import java.util.Set JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31 import java.util.Vector JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33 import java.awt.FontMetrics JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Rectangle JavaDoc;
36
37 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePair;
38
39 import org.netbeans.modules.j2ee.sun.ide.editors.ui.DDTablePanel;
40 import org.netbeans.modules.j2ee.sun.ide.editors.ui.AbstractDDTableModel;
41 import org.netbeans.modules.j2ee.sun.ide.editors.ui.SortableDDTableModel;
42
43 public class NameValuePairsPropertyEditor extends java.beans.PropertyEditorSupport JavaDoc {
44
45     NameValuePair[] params;
46     
47     static final ResourceBundle JavaDoc bundle =
48         ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/ide/editors/Bundle");
49
50     public NameValuePairsPropertyEditor(NameValuePair[] val) {
51         super();
52         if (val == null)
53             params = new NameValuePair[0];
54         else
55             params = val;
56     }
57     
58     public NameValuePairsPropertyEditor(Object JavaDoc val) {
59        super();
60         if (val == null)
61             params = new NameValuePair[0];
62         else
63             params = getNameValuePairs(val);
64     }
65     
66     //Fix for bug# 5023038 - selecting the property replaces string with null
67
public void setAsText(String JavaDoc string) throws IllegalArgumentException JavaDoc {
68     }
69     
70     public String JavaDoc getAsText() {
71         return getPaintableString();
72     }
73     
74 //// protected DDTablePanel panel = null;
75

76     static protected String JavaDoc[] requiredToolTips = {
77         bundle.getString("tipParamName"), //NOI18N
78
bundle.getString("tipParamValue")}; //NOI18N
79
//bundle.getString("tipParamDescription")}; //NOI18N
80

81     public java.awt.Component JavaDoc getCustomEditor() {
82         ParamModel model = null;
83         model = new ParamModel(params);
84         
85         DDTablePanel panel = new DDTablePanel(new SortableDDTableModel(model),
86             requiredToolTips);
87 // org.openide.util.HelpCtx.setHelpIDString(panel, "AS_RTT_NameValueEditor"); //NOI18N
88
return panel;
89     }
90         
91     public boolean isPaintable () {
92         return true;
93     }
94     
95     public void paintValue (Graphics JavaDoc gfx, Rectangle JavaDoc box) {
96         String JavaDoc s = getPaintableString();
97         FontMetrics JavaDoc fm = gfx.getFontMetrics ();
98     gfx.drawString (s, 4, (box.height - fm.getHeight ()) / 2 + 1 + fm.getMaxAscent ());
99     }
100     
101     protected java.lang.String JavaDoc getPaintableString() {
102         Object JavaDoc [] entries = (Object JavaDoc[]) getValue();
103         if ((entries == null) || (entries.length == 0)) {
104             return bundle.getString("TXT_Param") ; //NOI18N
105
} else if (entries.length == 1) {
106             return bundle.getString("TXT_OneParam"); //NOI18N
107
} else {
108             return MessageFormat.format(bundle.getString("TXT_MultiParam"), //NOI18N
109
new Object JavaDoc [] {
110                 Integer.toString(entries.length)
111             });
112         }
113      }
114     
115     public boolean supportsCustomEditor() {
116         return true;
117     }
118
119     public void setValue(Object JavaDoc value) {
120             if(value instanceof NameValuePair[]) {
121             NameValuePair[] tmpValue = (NameValuePair[])value;
122             params = new NameValuePair[tmpValue.length];
123             for (int i = 0; i < tmpValue.length; i++) {
124                 NameValuePair param = new NameValuePair();
125                 param.setParamName(tmpValue[i].getParamName());
126                 param.setParamValue(tmpValue[i].getParamValue());
127                 params[i] = param;
128             }
129         }else
130             params = getNameValuePairs(value);
131     }
132
133     public Object JavaDoc getValue() {
134         NameValuePair[] retVal = new NameValuePair[params.length];
135         for (int i = 0; i < params.length; i++) {
136             NameValuePair val = new NameValuePair();
137             val.setParamName(params[i].getParamName());
138             val.setParamValue(params[i].getParamValue());
139             retVal[i] = val;
140         }
141         return retVal;
142     }
143  
144     private NameValuePair[] getNameValuePairs(Object JavaDoc attrVal){
145         java.util.Map JavaDoc attributeMap = (java.util.Map JavaDoc)attrVal;
146         Set JavaDoc attributeKeys = attributeMap.keySet();
147         java.util.Iterator JavaDoc it = attributeKeys.iterator();
148         NameValuePair[] pairs = new NameValuePair[attributeKeys.size()];
149         int i=0;
150         while(it.hasNext()){
151             NameValuePair pair = new NameValuePair();
152             Object JavaDoc key = it.next();
153             pair.setParamName(key.toString());
154             pair.setParamValue(attributeMap.get(key).toString());
155             pairs[i] = pair;
156             i++;
157         }
158         return pairs;
159     }
160     
161     public class ParamModel extends AbstractDDTableModel {
162         public ParamModel(NameValuePair[] rows) {
163             super( rows );
164         }
165         
166         public String JavaDoc getColumnName(int col) {
167             if (0 == col)
168                 return bundle.getString("colHdrParamName"); // NOI18N
169
else
170                 return bundle.getString("colHdrParamValue"); //NOI18N
171
}
172         
173         public java.lang.Object JavaDoc getValueAt(int row, int col) {
174             NameValuePair rowObj = null;
175             rowObj = (NameValuePair) data.get(row);
176             
177             if (null != rowObj) {
178                 if (0 == col)
179                     return rowObj.getParamName();
180                 else
181                     return rowObj.getParamValue();
182             }
183             return null;
184         }
185         
186         public java.util.List JavaDoc isValueValid(java.lang.Object JavaDoc obj, int param) {
187             Vector JavaDoc errors = new Vector JavaDoc();
188             NameValuePair edit = (NameValuePair) obj;
189             String JavaDoc editParamName = edit.getParamName();
190             String JavaDoc editParamValue = edit.getParamValue();
191             NameValuePair row = null;
192             String JavaDoc rowParamName = null;
193             if (editParamName == null || editParamName.trim().length() == 0)
194                 errors.add(bundle.getString("ERR_InvalidEntry")); //NOI18N
195
else if (editParamValue == null || editParamValue.trim().length() == 0){
196                 errors.add(bundle.getString("ERR_NoValue")); //NOI18N
197
}
198             
199             for (int i = 0; i < data.size(); i++) {
200                 row = (NameValuePair) data.elementAt(i);
201                 rowParamName = row.getParamName();
202                 if (i != param && rowParamName.equals(editParamName)){
203                     errors.add(bundle.getString("ERR_DuplicateEntry")); //NOI18N
204
}
205             }
206             return errors;
207         }
208         
209         public boolean isEditValid(Object JavaDoc obj, int index) {
210             return true;
211         }
212         
213         public java.lang.String JavaDoc getModelName() {
214             return bundle.getString("ParamModel_modelName"); //NOI18N
215
}
216         
217         public org.netbeans.modules.j2ee.sun.ide.editors.ui.DDTableModelEditor getEditor() {
218             return new NameValuePairEditor();
219         }
220         
221         protected void setValueAt(java.lang.String JavaDoc str, java.lang.Object JavaDoc obj, int param) {
222             NameValuePair inVal = (NameValuePair) obj;
223             if (0 == param)
224                 inVal.setParamName(str);
225             else
226                 inVal.setParamValue(str);
227         }
228         
229         public java.lang.Object JavaDoc makeNewElement() {
230             NameValuePair retVal = new NameValuePair();
231             
232             retVal.setParamName(""); //NOI18N
233
retVal.setParamValue(""); //NOI18N
234
//retVal.setParamDescription(""); //NOI18N
235
return retVal;
236         }
237         
238         public java.lang.Object JavaDoc[] getValue() {
239             return data.toArray();
240         }
241         
242         public int getColumnCount() {
243             return 2;
244         }
245     }
246 }
247
Popular Tags