KickJava   Java API By Example, From Geeks To Geeks.

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