1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 21 22 import java.awt.Color ; 23 import java.awt.Graphics2D ; 24 import java.awt.image.BufferedImage ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Locale ; 31 import java.util.Set ; 32 import javax.imageio.ImageIO ; 33 import javax.swing.ImageIcon ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 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 ; 44 45 49 public class BasicBrandingModel { 50 51 private BrandingSupport branding; 52 private final SuiteProperties suiteProps; 53 54 55 public static final String NAME_PROPERTY = "app.name"; public static final String TITLE_PROPERTY = "app.title"; public static final String ICON_LOCATION_PROPERTY = "app.icon"; 59 public static final String BRANDING_TOKEN_PROPERTY = "branding.token"; 61 static final int ICON_WIDTH = 48; 62 static final int ICON_HEIGHT = 48; 63 64 65 private boolean brandingEnabled; 66 67 68 private String name; 69 private String title; 70 private BrandingSupport.BrandedFile icon = null; 71 private BrandingSupport.BrandedFile icon16 = null; 72 73 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 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 <ChangeListener > listeners = new HashSet (); 95 96 97 private final Set splashKeys = new HashSet (); 98 99 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 getName() { 117 return name; 118 } 119 120 public void setName(String name) { 121 124 125 if (isBrandingEnabled()) { 126 this.name = name; 127 suiteProps.setProperty(NAME_PROPERTY, getName()); 128 suiteProps.setProperty(BRANDING_TOKEN_PROPERTY, "${" + NAME_PROPERTY + "}"); } 130 } 131 132 public String getTitle() { 133 return title; 134 } 135 136 public void addChangeListener(ChangeListener l) { 137 listeners.add(l); 138 } 139 140 public void removeChangeListener(ChangeListener l) { 141 listeners.remove(l); 142 } 143 144 private void fireChange() { 145 ChangeEvent e = new ChangeEvent (this); 146 Iterator it = listeners.iterator(); 147 while (it.hasNext()) { 148 ((ChangeListener ) it.next()).stateChanged(e); 149 } 150 } 151 152 public void setTitle(String 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}"); } 161 if (splashWindowTitle != null) { 162 StringBuilder sb = new StringBuilder (); 163 sb.append(NbBundle.getMessage(BasicBrandingModel.class, "LBL_splash_window_title_prefix")); sb.append(" ").append(title); splashWindowTitle.setValue(sb.toString()); } 167 if (mainWindowTitleNoProject != null) { 168 mainWindowTitleNoProject.setValue(title + " {0}"); } 170 if (currentVersion != null) { 171 currentVersion.setValue(title + " {0}"); } 173 suiteProps.setProperty(TITLE_PROPERTY, getTitle()); 174 } 175 } 176 177 public URL getIconSource() { 178 return icon != null ? icon.getBrandingSource() : null; 179 } 180 181 public void setIconSource(final URL url) { 182 if (isBrandingEnabled()) { 183 icon.setBrandingSource(url); 184 suiteProps.setProperty(ICON_LOCATION_PROPERTY, getIconLocation()); 185 } 186 } 187 188 public String getIconLocation() { 189 File prj = suiteProps.getProjectDirectoryFile(); 190 String relativePath = PropertyUtils.relativizeFile(prj ,icon.getFileLocation()); 191 192 return relativePath; 193 } 194 195 public String getSplashLocation() { 196 File prj = suiteProps.getProjectDirectoryFile(); 197 String relativePath = PropertyUtils.relativizeFile(prj ,splash.getFileLocation()); 198 199 return relativePath; 200 } 201 202 public void store() throws IOException { 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 getScaleAndStoreIconTask(final BrandedFile icon, final int width, final int height) throws IOException { 228 return new Runnable () { 229 public void run() { 230 BufferedImage bi = new BufferedImage ( 231 width, 232 height, 233 BufferedImage.TYPE_INT_RGB); 234 235 Graphics2D g2 = bi.createGraphics(); 236 ImageIcon image = new ImageIcon (icon.getBrandingSource()); 237 g2.drawImage(image.getImage(),0, 0, 238 width, height, 239 Color.LIGHT_GRAY,null); 241 g2.dispose(); 242 try { 243 ImageIO.write(bi,"png",icon.getFileLocation()); } catch (IOException 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 ex) { 256 ErrorManager.getDefault().notify(ex); 257 throw new IllegalStateException (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 getSimpleName() { 275 Element nameEl = Util.findElement(suiteProps.getProject().getHelper().getPrimaryConfigurationData(true), "name", SuiteProjectType.NAMESPACE_SHARED); String text = (nameEl != null) ? Util.findText(nameEl) : null; 277 return (text != null) ? text : "???"; } 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]", "_"); if (!name.matches("[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*")) { name = "app"; } 291 } 292 293 assert name != null; 294 } 295 296 void initTitle(boolean reread) { 297 if (title == null || reread) { 298 String initTitle = suiteProps.getProperty(TITLE_PROPERTY); 299 300 if (initTitle == null) { 301 initTitle = getSimpleName(); 302 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", "org/netbeans/core/ui/Bundle.properties" , "LBL_ProductInformation"); 318 mainWindowTitle = getBranding().getBundleKey( 319 "org.netbeans.core.windows", "org/netbeans/core/windows/view/ui/Bundle.properties", "CTL_MainWindow_Title"); 323 splashWindowTitle = getBranding().getBundleKey( 324 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "LBL_splash_window_title"); 328 mainWindowTitleNoProject = getBranding().getBundleKey( 329 "org.netbeans.core.windows", "org/netbeans/core/windows/view/ui/Bundle.properties", "CTL_MainWindow_Title_No_Project"); 333 currentVersion = getBranding().getBundleKey( 334 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "currentVersion"); 338 icon = getBranding().getBrandedFile( 339 "org.netbeans.core.startup", "org/netbeans/core/startup/frame48.gif"); 342 icon16 = getBranding().getBrandedFile( 343 "org.netbeans.core.startup", "org/netbeans/core/startup/frame.gif"); 346 splash = getBranding().getBrandedFile( 347 "org.netbeans.core.startup", "org/netbeans/core/startup/splash.gif"); 350 352 splashWidth = getBranding().getBundleKey( 353 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SPLASH_WIDTH"); 357 splashHeight = getBranding().getBundleKey( 358 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SPLASH_HEIGHT"); 362 splashShowProgressBar = getBranding().getBundleKey( 363 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashShowProgressBar"); 367 splashRunningTextFontSize= getBranding().getBundleKey( 368 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashRunningTextFontSize"); 372 splashProgressBarBounds= getBranding().getBundleKey( 373 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashProgressBarBounds"); 377 splashRunningTextBounds= getBranding().getBundleKey( 378 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashRunningTextBounds"); 382 splashRunningTextColor= getBranding().getBundleKey( 383 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashRunningTextColor"); 387 splashProgressBarColor= getBranding().getBundleKey( 388 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashProgressBarColor"); 392 splashProgressBarEdgeColor= getBranding().getBundleKey( 393 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashProgressBarEdgeColor"); 397 splashProgressBarCornerColor= getBranding().getBundleKey( 398 "org.netbeans.core.startup", "org/netbeans/core/startup/Bundle.properties", "SplashProgressBarCornerColor"); 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 |