1 19 20 package org.netbeans.core.startup; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.EventQueue ; 27 import java.awt.Font ; 28 import java.awt.FontMetrics ; 29 import java.awt.Frame ; 30 import java.awt.Graphics ; 31 import java.awt.Graphics2D ; 32 import java.awt.Image ; 33 import java.awt.Rectangle ; 34 import java.awt.RenderingHints ; 35 import java.awt.Window ; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.ActionListener ; 38 import java.awt.event.KeyEvent ; 39 import java.lang.ref.Reference ; 40 import java.lang.ref.WeakReference ; 41 import java.util.MissingResourceException ; 42 import java.util.ResourceBundle ; 43 import java.util.StringTokenizer ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 import javax.accessibility.Accessible ; 47 import javax.swing.ImageIcon ; 48 import javax.swing.JComponent ; 49 import javax.swing.JDialog ; 50 import javax.swing.JPanel ; 51 import javax.swing.JTabbedPane ; 52 import javax.swing.KeyStroke ; 53 import static javax.swing.SwingConstants .*; 54 import javax.swing.SwingUtilities ; 55 import javax.swing.WindowConstants ; 56 import org.netbeans.Util; 57 import org.openide.util.Utilities; 58 import org.openide.util.NbBundle; 59 60 64 public final class Splash { 65 66 private static Splash splash; 67 68 69 private static boolean noBar = Boolean.getBoolean("netbeans.splash.nobar"); 70 71 static { 72 ResourceBundle bundle = NbBundle.getBundle(Splash.class); 73 noBar |= !Boolean.parseBoolean(bundle.getString("SplashShowProgressBar")); 74 } 75 76 public static Splash getInstance() { 77 if (splash == null) { 78 splash = new Splash(); 79 } 80 return splash; 81 } 82 83 public static void showAboutDialog (java.awt.Frame parent, javax.swing.JComponent info) { 84 createAboutDialog (parent, info).setVisible(true); 85 } 86 87 private static JDialog createAboutDialog (java.awt.Frame parent, javax.swing.JComponent info) { 88 SplashDialog splashDialog = new SplashDialog (parent, info); 89 return splashDialog; 90 } 91 92 private static final String ICON_SMALL = "org/netbeans/core/startup/frame.gif"; private static final String ICON_BIG = "org/netbeans/core/startup/frame32.gif"; 96 private static Image createIDEImage() { 97 return Utilities.loadImage(Utilities.isLargeFrameIcons() ? ICON_BIG : ICON_SMALL, true); 98 } 99 100 private Frame frame; 101 102 private SplashComponent comp; 103 104 private Splash() { 105 comp = new SplashComponent(false); 106 } 107 108 111 public void setRunning(boolean running) { 112 if (CLIOptions.isNoSplash()) 113 return; 114 115 if (running) { 116 if (frame == null) { 117 frame = new Frame (NbBundle.getMessage(Splash.class, "LBL_splash_window_title")); frame.setIconImage(createIDEImage()); frame.setUndecorated(true); 120 frame.setLayout (new BorderLayout ()); 122 frame.add(comp, BorderLayout.CENTER); 123 124 int width = Integer.parseInt(NbBundle.getMessage(Splash.class, "SPLASH_WIDTH")); 125 int height = Integer.parseInt(NbBundle.getMessage(Splash.class, "SPLASH_HEIGHT")); 126 frame.setPreferredSize(new Dimension (width, height)); 127 128 SwingUtilities.invokeLater (new SplashRunner(frame, true)); 129 } 130 } 131 else { 132 SwingUtilities.invokeLater (new SplashRunner(frame, false)); 133 } 134 } 135 136 public void dispose() { 137 setRunning(false); 138 splash = null; 139 } 140 141 public void increment(int steps) { 142 if (noBar || CLIOptions.isNoSplash()) 143 return; 144 145 if (comp != null) { 147 comp.increment(steps); 148 } 149 } 150 151 public Component getComponent() { 152 return comp; 153 } 154 155 157 public void print (String s) { 158 if (CLIOptions.isNoSplash() || comp == null) 159 return; 160 161 comp.setText(s); 162 } 164 165 167 public void addToMaxSteps(int steps) { 168 if (noBar || CLIOptions.isNoSplash()) 169 return; 170 171 if (comp != null) { 173 comp.addToMaxSteps(steps); 174 } 175 } 176 177 181 static final void center(Window c) { 182 c.pack(); 183 c.setBounds(Utilities.findCenterBounds(c.getSize())); 184 } 185 186 187 private static Image loadSplash() { 188 return Utilities.loadImage("org/netbeans/core/startup/splash.gif", true); 189 } 190 191 192 private static Image loadAbout() { 193 Image img = Utilities.loadImage("org/netbeans/core/startup/about.gif", true); 194 if (img != null) { 195 return img; 196 } else { 197 return loadSplash(); 198 } 199 } 200 201 204 private static class SplashComponent extends JComponent implements Accessible { 205 206 Rectangle view; 207 Color color_text; 208 Color color_bar; 209 Color color_edge; 210 Color color_corner; 211 212 213 private int size = 12; 214 private Rectangle dirty = new Rectangle (); 215 private Rectangle rect = new Rectangle (); 216 private Rectangle bar = new Rectangle (); 217 private Rectangle bar_inc = new Rectangle (); 218 219 private int progress = 0; 220 private int maxSteps = 0; 221 private int tmpSteps = 0; 222 private int barStart = 0; 223 private int barLength = 0; 224 225 private Image image; 226 private String text; 227 private FontMetrics fm; 228 229 233 public SplashComponent(boolean about) { 234 235 maxSteps = 140; 238 239 ResourceBundle bundle = NbBundle.getBundle(Splash.class); 240 StringTokenizer st = new StringTokenizer ( 241 bundle.getString("SplashRunningTextBounds"), " ,"); view = new Rectangle (Integer.parseInt(st.nextToken()), 243 Integer.parseInt(st.nextToken()), 244 Integer.parseInt(st.nextToken()), 245 Integer.parseInt(st.nextToken())); 246 st = new StringTokenizer ( 247 bundle.getString("SplashProgressBarBounds"), " ,"); try { 249 bar = new Rectangle (Integer.parseInt(st.nextToken()), 250 Integer.parseInt(st.nextToken()), 251 Integer.parseInt(st.nextToken()), 252 Integer.parseInt(st.nextToken())); 253 Integer rgb = Integer.decode(bundle.getString("SplashRunningTextColor")); color_text = new Color (rgb.intValue()); 255 rgb = Integer.decode(bundle.getString("SplashProgressBarColor")); color_bar = new Color (rgb.intValue()); 257 rgb = Integer.decode(bundle.getString("SplashProgressBarEdgeColor")); color_edge = new Color (rgb.intValue()); 259 rgb = Integer.decode(bundle.getString("SplashProgressBarCornerColor")); color_corner = new Color (rgb.intValue()); 261 } catch (NumberFormatException nfe) { 262 Util.err.warning("Number format exception " + "loading splash screen parameters."); Logger.getLogger("global").log(Level.WARNING, null, nfe); 266 color_text = Color.BLACK; 267 color_bar = Color.ORANGE; 268 color_edge = Color.BLUE; 269 color_corner = Color.GREEN; 270 bar = new Rectangle (0, 0, 80, 10); 271 } 272 try { 273 String sizeStr = bundle.getString("SplashRunningTextFontSize"); 274 size = Integer.parseInt(sizeStr); 275 } catch(MissingResourceException e){ 276 } catch (NumberFormatException nfe) { 278 } 280 281 image = new ImageIcon (about? loadAbout(): loadSplash()).getImage(); 283 Font font = new Font ("Dialog", Font.PLAIN, size); 284 285 setFont(font); fm = getFontMetrics(font); 287 } 288 289 292 public void setText(final String text) { 293 if (text != null && text.equals(this.text)) return; 295 296 EventQueue.invokeLater(new Runnable () { 299 public void run () { 300 if (text == null) { 301 repaint(dirty); 302 return; 303 } 304 305 if (fm == null) 306 return; 307 308 adjustText(text); 309 310 SwingUtilities.layoutCompoundLabel(fm, text, null, 311 BOTTOM, LEFT, BOTTOM, LEFT, 312 view, new Rectangle (), rect, 0); 313 dirty = dirty.union(rect); 314 repaint(dirty); 316 dirty = new Rectangle (rect); 317 } 318 }); 319 } 320 321 325 private void adjustText(String text) { 326 String newText = null; 327 String newString; 328 329 if (text == null) 330 return ; 331 332 if (fm == null) 333 return; 334 335 int width = fm.stringWidth(text); 336 337 if (width > view.width) { 338 StringTokenizer st = new StringTokenizer (text); 339 while (st.hasMoreTokens()) { 340 String element = st.nextToken(); 341 if (newText == null) 342 newString = element; 343 else 344 newString = newText + " " + element; if (fm.stringWidth(newString + "...") > view.width) { this.text = newText + "..."; break; 348 } else 349 newText = newString; 350 351 } 352 if (newText == null) { 356 this.text = ""; 357 newString = ""; 358 newText = ""; 359 for (int i = 0; i < text.length(); i++) { 360 newString += text.charAt(i); 361 if (fm.stringWidth(newString + "...") > view.width) { this.text = newText + "..."; break; 364 } else { 365 newText = newString; 366 } 367 } 368 } 369 } else 370 this.text = text; 371 } 372 373 public void increment(int steps) { 374 progress += steps; 375 if (progress > maxSteps) 376 progress = maxSteps; 377 else if (maxSteps > 0) { 378 int bl = bar.width * progress / maxSteps - barStart; 379 if (bl > 1 || barStart % 2 == 0) { 380 barLength = bl; 381 bar_inc = new Rectangle (bar.x + barStart, bar.y, barLength + 1, bar.height); 382 repaint(bar_inc); 384 } 386 } 387 } 388 389 390 394 private void addToMaxSteps(int steps) { 395 int max = maxSteps + steps; 396 int prog = progress / maxSteps * max; 397 maxSteps = max; 398 progress = prog; 399 } 401 402 405 public void update(Graphics g) { 406 paint(g); 407 } 408 409 412 public void paint(Graphics graphics) { 413 graphics.setColor(color_text); 414 graphics.drawImage(image, 0, 0, null); 415 416 if (text != null) { 417 if (fm == null) { 418 return; 421 } 422 423 SwingUtilities.layoutCompoundLabel(fm, text, null, 424 BOTTOM, LEFT, BOTTOM, LEFT, 425 view, new Rectangle (), rect, 0); 426 Graphics2D g2d = (Graphics2D )graphics; 428 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 429 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 430 graphics.drawString(text, rect.x, rect.y + fm.getAscent()); 431 } 432 433 if (!noBar && maxSteps > 0) 435 { 436 graphics.setColor(color_bar); 437 graphics.fillRect(bar.x, bar.y, barStart + barLength, bar.height); 438 graphics.setColor(color_corner); 439 graphics.drawLine(bar.x, bar.y, bar.x, bar.y + bar.height); 440 graphics.drawLine(bar.x + barStart + barLength, bar.y, bar.x + barStart + barLength, bar.y + bar.height); 441 graphics.setColor(color_edge); 442 graphics.drawLine(bar.x, bar.y + bar.height / 2, bar.x, bar.y + bar.height / 2); 443 graphics.drawLine(bar.x + barStart + barLength, bar.y + bar.height / 2, bar.x + barStart + barLength, bar.y + bar.height / 2); 444 barStart += barLength; 445 barLength = 0; 446 } 447 } 448 449 public Dimension getPreferredSize() { 450 return new Dimension (image.getWidth (null), image.getHeight (null)); 451 } 452 453 public boolean isOpaque () { 454 return true; 455 } 456 457 public String toString() { 458 return "SplashComponent - " + 459 "progress: " + progress + "/" + maxSteps + 460 " text: " + text; 461 } 462 } 463 464 private static class SplashDialog extends JDialog implements ActionListener { 465 466 static final long serialVersionUID = 5185644855500178404L; 467 468 private final SplashComponent splashComponent = new SplashComponent(true); 469 470 471 public SplashDialog (java.awt.Frame parent, javax.swing.JComponent infoPanel) { 472 super (parent, true); 473 474 JPanel splashPanel = new JPanel (); 475 JTabbedPane tabbedPane = new JTabbedPane (); 476 setTitle (NbBundle.getMessage(Splash.class, "CTL_About_Title")); 477 splashPanel.add (splashComponent); 479 tabbedPane.addTab(NbBundle.getMessage(Splash.class, "CTL_About_Title"), splashPanel); 480 tabbedPane.addTab(NbBundle.getMessage(Splash.class, "CTL_About_Detail"), infoPanel); 481 getContentPane().add(tabbedPane, BorderLayout.CENTER); 482 483 getRootPane().registerKeyboardAction( 484 this, 485 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 486 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT 487 ); 488 489 tabbedPane.getAccessibleContext().setAccessibleName(NbBundle.getMessage(Splash.class, "ACSN_AboutTabs")); 490 tabbedPane.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(Splash.class, "ACSD_AboutTabs")); 491 getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(Splash.class, "ACSD_AboutDialog")); 492 493 Splash.center(this); 494 495 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 496 } 497 498 public void actionPerformed(ActionEvent e) { 499 setVisible (false); 500 dispose(); 501 } 502 } 503 504 private static class SplashRunner implements Runnable { 505 506 private Window splashWindow; 507 private boolean visible; 508 509 public SplashRunner(Window splashWindow, boolean visible) { 510 this.splashWindow = splashWindow; 511 this.visible = visible; 512 } 513 514 public void run() { 515 if (visible) { 516 Splash.center(splashWindow); 517 splashWindow.setVisible(true); 518 splashWindow.toFront (); 519 } 520 else { 521 splashWindow.setVisible (false); 522 splashWindow.dispose (); 523 } 524 } 525 } 526 527 } 528 | Popular Tags |