KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > commonws > ComponentBeanMultiple


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  * Superclass that implements DisplayNameInterface and IconInterface for Servlet2.4 beans.
22  *
23  * @author Milan Kuchtiak
24  */

25 package org.netbeans.modules.j2ee.dd.impl.commonws;
26
27 import org.netbeans.modules.schema2beans.Version;
28 import org.netbeans.modules.j2ee.dd.api.common.*;
29 import org.openide.ErrorManager;
30
31 public abstract class ComponentBeanMultiple extends DescriptionBeanMultiple implements DisplayNameInterface, IconInterface {
32     
33     public ComponentBeanMultiple(java.util.Vector JavaDoc comps, Version version) {
34         super(comps, version);
35     }
36     // methods implemented by specific BaseBeans e.g. Servlet
37
public void setIcon(int index, org.netbeans.modules.j2ee.dd.api.common.Icon icon){}
38     public void setIcon(org.netbeans.modules.j2ee.dd.api.common.Icon[] icons){}
39     public org.netbeans.modules.j2ee.dd.api.common.Icon getIcon(int i){return null;}
40     public org.netbeans.modules.j2ee.dd.api.common.Icon[] getIcon(){return null;}
41     public int sizeIcon(){return 0;}
42     public int addIcon(org.netbeans.modules.j2ee.dd.api.common.Icon icon){return 0;}
43     public int removeIcon(org.netbeans.modules.j2ee.dd.api.common.Icon icon){return 0;}
44     
45     public abstract void setDisplayName(int index, java.lang.String JavaDoc value);
46     public abstract String JavaDoc getDisplayName(int index);
47     public abstract void setDisplayName(java.lang.String JavaDoc[] value);
48     //public abstract java.lang.String[] getDisplayName();
49
public abstract int sizeDisplayName();
50     public abstract int addDisplayName(java.lang.String JavaDoc value);
51     //public abstract int removeDisplayName(java.lang.String value);
52
public abstract void setDisplayNameXmlLang(int index, java.lang.String JavaDoc value);
53     public abstract String JavaDoc getDisplayNameXmlLang(int index);
54     
55     public void setDisplayName(String JavaDoc locale, String JavaDoc displayName) throws VersionNotSupportedException {
56         if (displayName==null) removeDisplayNameForLocale(locale);
57         else {
58             int size = sizeDisplayName();
59             boolean found=false;
60             for (int i=0;i<size;i++) {
61                 String JavaDoc loc=getDisplayNameXmlLang(i);
62                 if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
63                     found=true;
64                     setDisplayName(i, displayName);
65                     break;
66                 }
67             }
68             if (!found) {
69                 addDisplayName(displayName);
70                 if (locale!=null) setDisplayNameXmlLang(size, locale.toLowerCase());
71             }
72         }
73     }
74     
75     public void setDisplayName(String JavaDoc displayName) {
76         try {
77             setDisplayName(null,displayName);
78         } catch (VersionNotSupportedException ex){
79             ErrorManager.getDefault().notify(ex);
80         }
81     }
82     
83     public void setAllDisplayNames(java.util.Map JavaDoc displayNames) throws VersionNotSupportedException {
84         removeAllDisplayNames();
85         if (displayNames!=null) {
86             java.util.Iterator JavaDoc keys = displayNames.keySet().iterator();
87             int i=0;
88             while (keys.hasNext()) {
89                 String JavaDoc key = (String JavaDoc) keys.next();
90                 addDisplayName((String JavaDoc)displayNames.get(key));
91                 setDisplayNameXmlLang(i++, key);
92             }
93         }
94     }
95     
96     public String JavaDoc getDisplayName(String JavaDoc locale) throws VersionNotSupportedException {
97         for (int i=0;i<sizeDisplayName();i++) {
98             String JavaDoc loc=getDisplayNameXmlLang(i);
99             if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
100                 return getDisplayName(i);
101             }
102         }
103         return null;
104     }
105     public String JavaDoc getDefaultDisplayName() {
106         try {
107             return getDisplayName(null);
108         } catch (VersionNotSupportedException ex){return null;}
109     }
110     public java.util.Map JavaDoc getAllDisplayNames() {
111         java.util.Map JavaDoc map =new java.util.HashMap JavaDoc();
112         for (int i=0;i<sizeDisplayName();i++) {
113             String JavaDoc desc=getDisplayName(i);
114             String JavaDoc loc=getDisplayNameXmlLang(i);
115             map.put(loc, desc);
116         }
117         return map;
118     }
119     
120     public void removeDisplayNameForLocale(String JavaDoc locale) throws VersionNotSupportedException {
121         java.util.Map JavaDoc map = new java.util.HashMap JavaDoc();
122         for (int i=0;i<sizeDisplayName();i++) {
123             String JavaDoc desc=getDisplayName(i);
124             String JavaDoc loc=getDisplayNameXmlLang(i);
125             if ((locale==null && loc!=null) || (locale!=null && !locale.equalsIgnoreCase(loc)))
126                 map.put(loc, desc);
127         }
128         setAllDisplayNames(map);
129     }
130     
131     public void removeDisplayName() {
132         try {
133             removeDisplayNameForLocale(null);
134         } catch (VersionNotSupportedException ex){
135             ErrorManager.getDefault().notify(ex);
136         }
137     }
138     public void removeAllDisplayNames() {
139         setDisplayName(new String JavaDoc[]{});
140     }
141     
142     // setters
143
public void setSmallIcon(String JavaDoc locale, String JavaDoc icon) throws VersionNotSupportedException {
144         setIcon(locale, icon, true);
145     }
146     public void setSmallIcon(String JavaDoc icon) {
147         try {
148             setSmallIcon(null,icon);
149         } catch (VersionNotSupportedException ex){
150             ErrorManager.getDefault().notify(ex);
151         }
152     }
153     public void setLargeIcon(String JavaDoc locale, String JavaDoc icon) throws VersionNotSupportedException {
154         setIcon(locale, icon, false);
155     }
156     public void setLargeIcon(String JavaDoc icon) {
157         try {
158             setLargeIcon(null,icon);
159         } catch (VersionNotSupportedException ex){
160             ErrorManager.getDefault().notify(ex);
161         }
162     }
163     public void setAllIcons(String JavaDoc[] locales, String JavaDoc[] smallIcons, String JavaDoc[] largeIcons) throws VersionNotSupportedException {
164         org.netbeans.modules.j2ee.dd.api.common.Icon[] newIcons = new org.netbeans.modules.j2ee.dd.api.common.Icon[locales.length];
165         for (int i=0;i<locales.length;i++) {
166             try {
167                 newIcons[i] = (org.netbeans.modules.j2ee.dd.api.common.Icon)createBean("Icon"); //NOI18N
168
if (smallIcons[i]!=null) newIcons[i].setSmallIcon(smallIcons[i]);
169                 if (largeIcons[i]!=null) newIcons[i].setLargeIcon(largeIcons[i]);
170                 if (locales[i]!=null) newIcons[i].setXmlLang(locales[i]);
171             } catch (ClassNotFoundException JavaDoc ex){}
172         }
173         setIcon(newIcons);
174     }
175     
176     public void setIcon(org.netbeans.modules.j2ee.dd.api.common.Icon icon) {
177         if (icon==null) removeIcon();
178         else {
179             org.netbeans.modules.j2ee.dd.api.common.Icon[] oldIcons = getIcon();
180             boolean found=false;
181             try {
182                 String JavaDoc locale = icon.getXmlLang();
183                 for (int i=0;i<oldIcons.length;i++) {
184                     String JavaDoc loc=oldIcons[i].getXmlLang();
185                     if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
186                         found=true;
187                         setIcon(i, icon);
188                     }
189                 }
190             } catch (VersionNotSupportedException ex){}
191             if (!found) {
192                 addIcon(icon);
193             }
194         }
195     }
196     
197     public String JavaDoc getSmallIcon(String JavaDoc locale) throws VersionNotSupportedException {
198         return getIcon(locale,true);
199     }
200     public String JavaDoc getSmallIcon() {
201         try {
202             return getSmallIcon(null);
203         } catch (VersionNotSupportedException ex){return null;}
204     }
205     public String JavaDoc getLargeIcon(String JavaDoc locale) throws VersionNotSupportedException {
206         return getIcon(locale,false);
207     }
208     public String JavaDoc getLargeIcon() {
209         try {
210             return getLargeIcon(null);
211         } catch (VersionNotSupportedException ex){return null;}
212     }
213     public org.netbeans.modules.j2ee.dd.api.common.Icon getDefaultIcon() {
214         org.netbeans.modules.j2ee.dd.api.common.Icon[] icons = getIcon();
215         for (int i=0;i<icons.length;i++) {
216             try {
217                 String JavaDoc loc=icons[i].getXmlLang();
218                 if (loc==null) return icons[i];
219             } catch (VersionNotSupportedException ex){}
220         }
221         return null;
222     }
223     public java.util.Map JavaDoc getAllIcons() {
224         java.util.Map JavaDoc map =new java.util.HashMap JavaDoc();
225         org.netbeans.modules.j2ee.dd.api.common.Icon[] icons = getIcon();
226         for (int i=0;i<icons.length;i++) {
227             String JavaDoc[] iconPair = new String JavaDoc[] {icons[i].getSmallIcon(),icons[i].getLargeIcon()};
228             String JavaDoc loc=null;
229             try {
230                 loc=icons[i].getXmlLang();
231             } catch (VersionNotSupportedException ex){
232                 ErrorManager.getDefault().notify(ex);
233             }
234             map.put(loc, iconPair);
235         }
236         return map;
237     }
238     
239     public void removeSmallIcon(String JavaDoc locale) throws VersionNotSupportedException {
240         removeIcon(locale, true);
241     }
242     public void removeLargeIcon(String JavaDoc locale) throws VersionNotSupportedException {
243         removeIcon(locale, false);
244     }
245     public void removeIcon(String JavaDoc locale) throws VersionNotSupportedException {
246         org.netbeans.modules.j2ee.dd.api.common.Icon[] icons = getIcon();
247         for (int i=0;i<icons.length;i++) {
248             String JavaDoc loc=icons[i].getXmlLang();
249             if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
250                 removeIcon(icons[i]);
251             }
252         }
253     }
254     public void removeSmallIcon() {
255         try {
256             removeSmallIcon(null);
257         } catch (VersionNotSupportedException ex){
258             ErrorManager.getDefault().notify(ex);
259         }
260     }
261     public void removeLargeIcon() {
262         try {
263             removeLargeIcon(null);
264         } catch (VersionNotSupportedException ex){
265             ErrorManager.getDefault().notify(ex);
266         }
267     }
268     public void removeIcon() {
269         org.netbeans.modules.j2ee.dd.api.common.Icon[] icons = getIcon();
270         for (int i=0;i<icons.length;i++) {
271             try {
272                 String JavaDoc loc=icons[i].getXmlLang();
273                 if (loc==null) removeIcon(icons[i]);
274             } catch (VersionNotSupportedException ex){}
275         }
276     }
277     public void removeAllIcons() {
278         setIcon(new org.netbeans.modules.j2ee.dd.api.common.Icon[]{});
279     }
280     private void setIcon(String JavaDoc locale, String JavaDoc icon, boolean isSmall) throws VersionNotSupportedException {
281         if (icon==null) {
282             if (isSmall) removeSmallIcon(locale);
283             else removeLargeIcon(locale);
284         } else {
285             org.netbeans.modules.j2ee.dd.api.common.Icon[] oldIcons = getIcon();
286             boolean found=false;
287             for (int i=0;i<oldIcons.length;i++) {
288                 String JavaDoc loc=oldIcons[i].getXmlLang();
289                 if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
290                     found=true;
291                     if (isSmall) oldIcons[i].setSmallIcon(icon);
292                     else oldIcons[i].setLargeIcon(icon);
293                     break;
294                 }
295             }
296             if (!found) {
297                 try {
298                     org.netbeans.modules.j2ee.dd.api.common.Icon newIcon = (org.netbeans.modules.j2ee.dd.api.common.Icon)createBean("Icon"); //NOI18N
299
if (locale!=null) newIcon.setXmlLang(locale.toLowerCase());
300                     if (isSmall) newIcon.setSmallIcon(icon);
301                     else newIcon.setLargeIcon(icon);
302                     addIcon(newIcon);
303                 } catch (ClassNotFoundException JavaDoc ex){}
304             }
305         }
306     }
307     private String JavaDoc getIcon(String JavaDoc locale, boolean isSmall) throws VersionNotSupportedException {
308         for (int i=0;i<sizeIcon();i++) {
309             org.netbeans.modules.j2ee.dd.api.common.Icon icon = getIcon(i);
310             String JavaDoc loc=icon.getXmlLang();
311             if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
312                 if (isSmall) return icon.getSmallIcon();
313                 else return icon.getLargeIcon();
314             }
315         }
316         return null;
317     }
318     
319     public void removeIcon(String JavaDoc locale, boolean isSmall) throws VersionNotSupportedException {
320         org.netbeans.modules.j2ee.dd.api.common.Icon[] icons = getIcon();
321         java.util.List JavaDoc iconList = new java.util.ArrayList JavaDoc();
322         for (int i=0;i<icons.length;i++) {
323             String JavaDoc loc=icons[i].getXmlLang();
324             if ((locale==null && loc==null) || (locale!=null && locale.equalsIgnoreCase(loc))) {
325                 if (isSmall) {
326                     icons[i].setSmallIcon(null);
327                     if (icons[i].getLargeIcon()==null) iconList.add(icons[i]);
328                 } else {
329                     icons[i].setLargeIcon(null);
330                     if (icons[i].getSmallIcon()==null) iconList.add(icons[i]);
331                 }
332             }
333         }
334         java.util.Iterator JavaDoc it = iconList.iterator();
335         while(it.hasNext()) removeIcon((org.netbeans.modules.j2ee.dd.api.common.Icon)it.next());
336     }
337 }
338
Popular Tags