KickJava   Java API By Example, From Geeks To Geeks.

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