KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > changelog > settings > DefaultServerInfoPE


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 Ralph Krueger.
17  */

18 package org.netbeans.modules.changelog.settings;
19
20 import java.beans.*;
21 import java.util.*;
22
23 import org.openide.util.*;
24
25 import org.netbeans.modules.changelog.html.*;
26
27     /** Property editor for Default Server Info prop. of the ChangeLog settings
28      *
29      * @author Ralph Krueger
30      */

31 public class DefaultServerInfoPE extends PropertyEditorSupport {
32
33     private static final java.util.ResourceBundle JavaDoc bundle = NbBundle.getBundle(DefaultServerInfoPE.class);
34
35     /** localized string*/
36     private final static String JavaDoc DEFAULTS = bundle.getString("DefaultServerInfoPE.noDefault.text"); // NOI18N
37

38     /** array of display names */
39     private String JavaDoc[] texts;
40     /** array of internal Ids */
41     private String JavaDoc[] ids;
42
43     /** @return names of the supported LookAndFeels */
44     public String JavaDoc[] getTags() {
45         if (texts == null ) {
46             initValues();
47         }
48         return texts;
49     }
50     
51     private void initValues() {
52         Lookup.Template template = new Lookup.Template(ChangeLogHTMLService.class);
53         Lookup.Result result = Lookup.getDefault().lookup(template);
54         Collection col = result.allItems();
55         texts = new String JavaDoc[col.size() + 1];
56         ids = new String JavaDoc[col.size() + 1];
57         texts[0] = DEFAULTS;
58         ids[0] = "";
59         Iterator it = col.iterator();
60         int index = 1;
61         while (it.hasNext()) {
62             Lookup.Item item = (Lookup.Item)it.next();
63             ChangeLogHTMLService serv = (ChangeLogHTMLService)item.getInstance();
64             texts[index] = serv.getName();
65             ids[index] = item.getId();
66             index = index + 1;
67         }
68     }
69
70
71     private int findIndexInArray(String JavaDoc[] arr, String JavaDoc value) {
72         for (int i = 0; i < arr.length; i++) {
73             if (value.equals(arr[i])) {
74                 return i;
75             }
76         }
77         return -1;
78     }
79     
80     /** Gets the property value as a string suitable for presentation
81      * to a human to edit.
82      *
83      * @return The property value as a string suitable for presentation
84      * to a human to edit.
85      * <p> Returns "null" is the value can't be expressed as a string.
86      * <p> If a non-null value is returned, then the PropertyEditor should
87      * be prepared to parse that string back in setAsText().
88      */

89     public String JavaDoc getAsText() {
90         if (texts == null ) {
91             initValues();
92         }
93         String JavaDoc value = (String JavaDoc)getValue();
94         if (value == null) {
95             value = "";
96         }
97         int i = findIndexInArray(ids, value);
98         if (i != -1) {
99             return texts[i];
100         }
101         setValue("");
102         return DEFAULTS;
103     }
104
105     /** Sets the property value by parsing a given String. May raise
106      * java.lang.IllegalArgumentException if either the String is
107      * badly formatted or if this kind of property can't be expressed
108      * as text.
109      *
110      * @param text The string to be parsed.
111      */

112     public void setAsText(String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
113         if (texts == null ) {
114             initValues();
115         }
116         int i = findIndexInArray(texts, text);
117         if (i != -1) {
118             setValue(ids[i]);
119             return;
120         }
121         setValue("");
122     }
123     
124 }
125
126
Popular Tags