KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > BasicBrandingModel


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 package org.netbeans.modules.apisupport.project.ui.customizer;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.imageio.ImageIO JavaDoc;
33 import javax.swing.ImageIcon JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import org.netbeans.modules.apisupport.project.Util;
37 import org.netbeans.modules.apisupport.project.suite.BrandingSupport;
38 import org.netbeans.modules.apisupport.project.suite.BrandingSupport.BrandedFile;
39 import org.netbeans.modules.apisupport.project.suite.SuiteProjectType;
40 import org.netbeans.spi.project.support.ant.PropertyUtils;
41 import org.openide.ErrorManager;
42 import org.openide.util.NbBundle;
43 import org.w3c.dom.Element JavaDoc;
44
45 /**
46  *
47  * @author Radek Matous
48  */

49 public class BasicBrandingModel {
50     
51     private BrandingSupport branding;
52     private final SuiteProperties suiteProps;
53     
54     /** generated properties*/
55     public static final String JavaDoc NAME_PROPERTY = "app.name";//NOI18N
56
public static final String JavaDoc TITLE_PROPERTY = "app.title";//NOI18N
57
public static final String JavaDoc ICON_LOCATION_PROPERTY = "app.icon";//NOI18N
58

59     public static final String JavaDoc BRANDING_TOKEN_PROPERTY = "branding.token";//NOI18N
60

61     static final int ICON_WIDTH = 48;
62     static final int ICON_HEIGHT = 48;
63     
64     /** for generating property branding.token*/
65     private boolean brandingEnabled;
66     
67     /** for properties (app.name, app.title, app.icon)*/
68     private String JavaDoc name;
69     private String JavaDoc title;
70     private BrandingSupport.BrandedFile icon = null;
71     private BrandingSupport.BrandedFile icon16 = null;
72     
73     /** representation of bundle keys depending on app.title */
74     private BrandingSupport.BundleKey productInformation = null;
75     private BrandingSupport.BundleKey mainWindowTitle = null;
76     private BrandingSupport.BundleKey splashWindowTitle = null;
77     private BrandingSupport.BundleKey mainWindowTitleNoProject = null;
78     private BrandingSupport.BundleKey currentVersion = null;
79     
80     /** representation of bundle keys for splash section */
81     private BrandingSupport.BrandedFile splash = null;
82     
83     private BrandingSupport.BundleKey splashWidth = null;
84     private BrandingSupport.BundleKey splashHeight = null;
85     private BrandingSupport.BundleKey splashShowProgressBar = null;
86     private BrandingSupport.BundleKey splashRunningTextBounds = null;
87     private BrandingSupport.BundleKey splashProgressBarBounds = null;
88     private BrandingSupport.BundleKey splashRunningTextFontSize = null;
89     private BrandingSupport.BundleKey splashRunningTextColor = null;
90     private BrandingSupport.BundleKey splashProgressBarColor = null;
91     private BrandingSupport.BundleKey splashProgressBarEdgeColor = null;
92     private BrandingSupport.BundleKey splashProgressBarCornerColor = null;
93     
94     private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc();
95     
96     /**all above splash BundleKeys in set*/
97     private final Set JavaDoc splashKeys = new HashSet JavaDoc();
98     
99     /** Creates a new instance of ApplicationDetails */
100     public BasicBrandingModel(final SuiteProperties suiteProps) {
101         this.suiteProps = suiteProps;
102         init();
103     }
104     
105     public boolean isBrandingEnabled() {
106         return brandingEnabled;
107     }
108     
109     public void setBrandingEnabled(boolean brandingEnabled) {
110         if (this.brandingEnabled != brandingEnabled) {
111             this.brandingEnabled = brandingEnabled;
112             fireChange();
113         }
114     }
115     
116     public String JavaDoc getName() {
117         return name;
118     }
119     
120     public void setName(String JavaDoc name) /*throws IllegalArgumentException*/ {
121         /*if (name != null && !name.matches("[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*")) { // NOI18N
122             throw new IllegalArgumentException("Malformed name: " + name); // NOI18N
123         }*/

124      
125         if (isBrandingEnabled()) {
126             this.name = name;
127             suiteProps.setProperty(NAME_PROPERTY, getName());
128             suiteProps.setProperty(BRANDING_TOKEN_PROPERTY, "${" + NAME_PROPERTY + "}");//NOI18N
129
}
130     }
131     
132     public String JavaDoc getTitle() {
133         return title;
134     }
135     
136     public void addChangeListener(ChangeListener JavaDoc l) {
137         listeners.add(l);
138     }
139     
140     public void removeChangeListener(ChangeListener JavaDoc l) {
141         listeners.remove(l);
142     }
143     
144     private void fireChange() {
145         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
146         Iterator JavaDoc it = listeners.iterator();
147         while (it.hasNext()) {
148             ((ChangeListener JavaDoc) it.next()).stateChanged(e);
149         }
150     }
151     
152     public void setTitle(String JavaDoc title) {
153         if (isBrandingEnabled()) {
154             this.title = title;
155             if (productInformation != null) {
156                 productInformation.setValue(title);
157             }
158             if (mainWindowTitle != null) {
159                 mainWindowTitle.setValue(title + " {0}"); //NOI18N
160
}
161             if (splashWindowTitle != null) {
162                 StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
163                 sb.append(NbBundle.getMessage(BasicBrandingModel.class, "LBL_splash_window_title_prefix"));//NOI18N
164
sb.append(" ").append(title);//NOI18N
165
splashWindowTitle.setValue(sb.toString());//NOI18N
166
}
167             if (mainWindowTitleNoProject != null) {
168                 mainWindowTitleNoProject.setValue(title + " {0}"); //NOI18N
169
}
170             if (currentVersion != null) {
171                 currentVersion.setValue(title + " {0}"); //NOI18N
172
}
173             suiteProps.setProperty(TITLE_PROPERTY, getTitle());
174         }
175     }
176     
177     public URL JavaDoc getIconSource() {
178         return icon != null ? icon.getBrandingSource() : null;
179     }
180     
181     public void setIconSource(final URL JavaDoc url) {
182         if (isBrandingEnabled()) {
183             icon.setBrandingSource(url);
184             suiteProps.setProperty(ICON_LOCATION_PROPERTY, getIconLocation());
185         }
186     }
187     
188     public String JavaDoc getIconLocation() {
189         File JavaDoc prj = suiteProps.getProjectDirectoryFile();
190         String JavaDoc relativePath = PropertyUtils.relativizeFile(prj ,icon.getFileLocation());
191         
192         return relativePath;
193     }
194     
195     public String JavaDoc getSplashLocation() {
196         File JavaDoc prj = suiteProps.getProjectDirectoryFile();
197         String JavaDoc relativePath = PropertyUtils.relativizeFile(prj ,splash.getFileLocation());
198         
199         return relativePath;
200     }
201     
202     public void store() throws IOException JavaDoc {
203         if (brandingEnabled) {
204             getBranding().brandBundleKey(productInformation);
205             getBranding().brandBundleKey(mainWindowTitle);
206             getBranding().brandBundleKey(splashWindowTitle);
207             getBranding().brandBundleKey(mainWindowTitleNoProject);
208             getBranding().brandBundleKey(currentVersion);
209             
210             getBranding().brandFile(icon,
211                     getScaleAndStoreIconTask(icon, BasicBrandingModel.ICON_WIDTH,BasicBrandingModel.ICON_HEIGHT));
212             
213             icon16.setBrandingSource(icon.getBrandingSource());
214             getBranding().brandFile(icon16,
215                     getScaleAndStoreIconTask(icon16, 16,16));
216                                     
217             getBranding().brandBundleKeys(splashKeys);
218             getBranding().brandFile(splash);
219         } else {
220             suiteProps.removeProperty(BasicBrandingModel.BRANDING_TOKEN_PROPERTY);
221             suiteProps.removeProperty(BasicBrandingModel.NAME_PROPERTY);
222             suiteProps.removeProperty(BasicBrandingModel.TITLE_PROPERTY);
223             suiteProps.removeProperty(BasicBrandingModel.ICON_LOCATION_PROPERTY);
224         }
225     }
226     
227     private static Runnable JavaDoc getScaleAndStoreIconTask(final BrandedFile icon, final int width, final int height) throws IOException JavaDoc {
228         return new Runnable JavaDoc() {
229             public void run() {
230                 BufferedImage JavaDoc bi = new BufferedImage JavaDoc(
231                         width,
232                         height,
233                         BufferedImage.TYPE_INT_RGB);
234                 
235                 Graphics2D JavaDoc g2 = bi.createGraphics();
236                 ImageIcon JavaDoc image = new ImageIcon JavaDoc(icon.getBrandingSource());
237                 g2.drawImage(image.getImage(),0, 0,
238                         width, height,
239                         Color.LIGHT_GRAY,null);//NOI18N
240

241                 g2.dispose();
242                 try {
243                     ImageIO.write(bi,"png",icon.getFileLocation());//NOI18N
244
} catch (IOException JavaDoc ex) {
245                     ErrorManager.getDefault().notify(ex);
246                 }
247             }
248         };
249     }
250     
251     private BrandingSupport getBranding() {
252         if (branding == null) {
253             try {
254                 branding = BrandingSupport.getInstance(suiteProps);
255             } catch (IOException JavaDoc ex) {
256                 ErrorManager.getDefault().notify(ex);
257                 throw new IllegalStateException JavaDoc(ex.getLocalizedMessage());
258             }
259         }
260         return branding;
261     }
262     
263     private void init() {
264         initBundleKeys();
265         initName(false);
266         initTitle(false);
267         brandingEnabledRefresh();
268     }
269     
270     void brandingEnabledRefresh() {
271         brandingEnabled = (suiteProps.getProperty(BRANDING_TOKEN_PROPERTY) != null);
272     }
273     
274     private String JavaDoc getSimpleName() {
275         Element JavaDoc nameEl = Util.findElement(suiteProps.getProject().getHelper().getPrimaryConfigurationData(true), "name", SuiteProjectType.NAMESPACE_SHARED); // NOI18N
276
String JavaDoc text = (nameEl != null) ? Util.findText(nameEl) : null;
277         return (text != null) ? text : "???"; // NOI18N
278
}
279
280     void initName(boolean reread) {
281         if (name == null || reread) {
282             name = suiteProps.getProperty(NAME_PROPERTY);
283         }
284         
285         if (name == null) {
286             name = getSimpleName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9]", "_"); // NOI18N
287
if (!name.matches("[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*")) { // NOI18N
288
// Too far from a decent name, forget it.
289
name = "app"; // NOI18N
290
}
291         }
292         
293         assert name != null;
294     }
295     
296     void initTitle(boolean reread) {
297         if (title == null || reread) {
298             String JavaDoc initTitle = suiteProps.getProperty(TITLE_PROPERTY);
299             
300             if (initTitle == null) {
301                 initTitle = getSimpleName();
302                 // Just make a rough attempt to uppercase it, to hint that it can be a display name.
303
if (Character.isLowerCase(initTitle.charAt(0))) {
304                     initTitle = String.valueOf(Character.toLowerCase(initTitle.charAt(0))) + initTitle.substring(1);
305                 }
306             }
307             assert initTitle != null;
308             title = initTitle;
309         }
310     }
311     
312     private void initBundleKeys() {
313         productInformation = getBranding().getBundleKey(
314                 "org.netbeans.core",//NOI18N
315
"org/netbeans/core/ui/Bundle.properties" ,//NOI18N
316
"LBL_ProductInformation");//NOI18N
317

318         mainWindowTitle = getBranding().getBundleKey(
319                 "org.netbeans.core.windows",//NOI18N
320
"org/netbeans/core/windows/view/ui/Bundle.properties", // NOI18N
321
"CTL_MainWindow_Title");//NOI18N
322

323         splashWindowTitle = getBranding().getBundleKey(
324                 "org.netbeans.core.startup",//NOI18N
325
"org/netbeans/core/startup/Bundle.properties",//NOI18N
326
"LBL_splash_window_title");//NOI18N
327

328         mainWindowTitleNoProject = getBranding().getBundleKey(
329                 "org.netbeans.core.windows",//NOI18N
330
"org/netbeans/core/windows/view/ui/Bundle.properties",//NOI18N
331
"CTL_MainWindow_Title_No_Project");//NOI18N
332

333         currentVersion = getBranding().getBundleKey(
334                 "org.netbeans.core.startup",//NOI18N
335
"org/netbeans/core/startup/Bundle.properties",//NOI18N
336
"currentVersion");//NOI18N
337

338         icon = getBranding().getBrandedFile(
339                 "org.netbeans.core.startup",//NOI18N
340
"org/netbeans/core/startup/frame48.gif");//NOI18N
341

342         icon16 = getBranding().getBrandedFile(
343                 "org.netbeans.core.startup",//NOI18N
344
"org/netbeans/core/startup/frame.gif");//NOI18N
345

346         splash = getBranding().getBrandedFile(
347                 "org.netbeans.core.startup",//NOI18N
348
"org/netbeans/core/startup/splash.gif");//NOI18N
349

350         // init of splash keys
351

352         splashWidth = getBranding().getBundleKey(
353                 "org.netbeans.core.startup",//NOI18N
354
"org/netbeans/core/startup/Bundle.properties",//NOI18N
355
"SPLASH_WIDTH");//NOI18N
356

357         splashHeight = getBranding().getBundleKey(
358                 "org.netbeans.core.startup",//NOI18N
359
"org/netbeans/core/startup/Bundle.properties",//NOI18N
360
"SPLASH_HEIGHT");//NOI18N
361

362         splashShowProgressBar = getBranding().getBundleKey(
363                 "org.netbeans.core.startup",//NOI18N
364
"org/netbeans/core/startup/Bundle.properties",//NOI18N
365
"SplashShowProgressBar");//NOI18N
366

367         splashRunningTextFontSize= getBranding().getBundleKey(
368                 "org.netbeans.core.startup",//NOI18N
369
"org/netbeans/core/startup/Bundle.properties",//NOI18N
370
"SplashRunningTextFontSize");//NOI18N
371

372         splashProgressBarBounds= getBranding().getBundleKey(
373                 "org.netbeans.core.startup",//NOI18N
374
"org/netbeans/core/startup/Bundle.properties",//NOI18N
375
"SplashProgressBarBounds");//NOI18N
376

377         splashRunningTextBounds= getBranding().getBundleKey(
378                 "org.netbeans.core.startup",//NOI18N
379
"org/netbeans/core/startup/Bundle.properties",//NOI18N
380
"SplashRunningTextBounds");//NOI18N
381

382         splashRunningTextColor= getBranding().getBundleKey(
383                 "org.netbeans.core.startup",//NOI18N
384
"org/netbeans/core/startup/Bundle.properties",//NOI18N
385
"SplashRunningTextColor");//NOI18N
386

387         splashProgressBarColor= getBranding().getBundleKey(
388                 "org.netbeans.core.startup",//NOI18N
389
"org/netbeans/core/startup/Bundle.properties",//NOI18N
390
"SplashProgressBarColor");//NOI18N
391

392         splashProgressBarEdgeColor= getBranding().getBundleKey(
393                 "org.netbeans.core.startup",//NOI18N
394
"org/netbeans/core/startup/Bundle.properties",//NOI18N
395
"SplashProgressBarEdgeColor");//NOI18N
396

397         splashProgressBarCornerColor= getBranding().getBundleKey(
398                 "org.netbeans.core.startup",//NOI18N
399
"org/netbeans/core/startup/Bundle.properties",//NOI18N
400
"SplashProgressBarCornerColor");//NOI18N
401

402         splashKeys.clear();
403         
404         if (splashWidth != null) {
405             splashKeys.add(splashWidth);
406         }
407         if (splashHeight != null) {
408             splashKeys.add(splashHeight);
409         }
410         if (splashShowProgressBar != null) {
411             splashKeys.add(splashShowProgressBar);
412         }
413         if (splashRunningTextBounds != null) {
414             splashKeys.add(splashRunningTextBounds);
415         }
416         if (splashProgressBarBounds != null) {
417             splashKeys.add(splashProgressBarBounds);
418         }
419         if (splashRunningTextFontSize != null) {
420             splashKeys.add(splashRunningTextFontSize);
421         }
422         if (splashRunningTextColor != null) {
423             splashKeys.add(splashRunningTextColor );
424         }
425         if (splashProgressBarColor != null) {
426             splashKeys.add(splashProgressBarColor);
427         }
428         if (splashProgressBarEdgeColor != null) {
429             splashKeys.add(splashProgressBarEdgeColor);
430         }
431         if (splashProgressBarCornerColor != null) {
432             splashKeys.add(splashProgressBarCornerColor);
433         }
434         splashKeys.remove(null);
435     }
436     
437     public BrandingSupport.BundleKey getSplashWidth() {
438         return splashWidth;
439     }
440     
441     public BrandingSupport.BundleKey getSplashHeight() {
442         return splashHeight;
443     }
444     
445     public BrandingSupport.BundleKey getSplashShowProgressBar() {
446         return splashShowProgressBar;
447     }
448     
449     public BrandingSupport.BundleKey getSplashRunningTextBounds() {
450         return splashRunningTextBounds;
451     }
452     
453     public BrandingSupport.BundleKey getSplashProgressBarBounds() {
454         return splashProgressBarBounds;
455     }
456     
457     public BrandingSupport.BundleKey getSplashRunningTextFontSize() {
458         return splashRunningTextFontSize;
459     }
460     
461     public BrandingSupport.BundleKey getSplashRunningTextColor() {
462         return splashRunningTextColor;
463     }
464     
465     public BrandingSupport.BundleKey getSplashProgressBarColor() {
466         return splashProgressBarColor;
467     }
468     
469     public BrandingSupport.BundleKey getSplashProgressBarEdgeColor() {
470         return splashProgressBarEdgeColor;
471     }
472     
473     public BrandingSupport.BundleKey getSplashProgressBarCornerColor() {
474         return splashProgressBarCornerColor;
475     }
476     
477     public BrandingSupport.BrandedFile getSplash() {
478         return splash;
479     }
480 }
481
Popular Tags