1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 21 22 import java.awt.AlphaComposite ; 23 import java.awt.Color ; 24 import java.awt.Dimension ; 25 import java.awt.EventQueue ; 26 import java.awt.Font ; 27 import java.awt.FontMetrics ; 28 import java.awt.Graphics ; 29 import java.awt.Graphics2D ; 30 import java.awt.Image ; 31 import java.awt.Rectangle ; 32 import java.awt.RenderingHints ; 33 import java.net.URL ; 34 import java.util.StringTokenizer ; 35 import javax.swing.ImageIcon ; 36 import javax.swing.JLabel ; 37 import javax.swing.SwingConstants ; 38 import javax.swing.SwingUtilities ; 39 40 41 44 class SplashComponentPreview extends JLabel { 45 private FontMetrics fm; 46 private Rectangle view; 47 private Color color_text; 48 private Color color_bar; 49 private Color color_edge; 50 private Color color_corner; 51 52 private boolean draw_bar; 53 54 protected Image image; 55 private Rectangle dirty = new Rectangle (); 56 private String text; 57 private Rectangle rect = new Rectangle (); 58 private Rectangle bar = new Rectangle (); 59 private Rectangle bar_inc = new Rectangle (); 60 61 private int progress = 0; 62 private int maxSteps = 0; 63 private int tmpSteps = 0; 64 private int barStart = 0; 65 private int barLength = 0; 66 67 private DragManager dragManager; 68 private DragManager.DragItem textDragItem; 69 private DragManager.DragItem progressDragItem; 70 71 74 public SplashComponentPreview() { 75 dragManager = new DragManager(this); 77 textDragItem = dragManager.createNewItem(); 78 progressDragItem = dragManager.createNewItem(); 79 } 80 81 void setFontSize(final String fontSize) throws NumberFormatException { 82 int size; 83 String sizeStr = fontSize; 84 size = Integer.parseInt(sizeStr); 85 86 Font font = new Font ("Dialog", Font.PLAIN, size); 88 setFont(font); fm = getFontMetrics(font); 90 } 91 92 void setSplashImageIcon(final URL url) { 93 ImageIcon imgIcon = new ImageIcon (url); 94 this.image = imgIcon.getImage(); 95 } 97 98 void setDropHandletForProgress (DragManager.DropHandler dHandler) { 99 this.progressDragItem.setDropHandler(dHandler); 100 } 101 102 void setDropHandletForText (DragManager.DropHandler dHandler) { 103 this.textDragItem.setDropHandler(dHandler); 104 } 105 106 void setFontSize(final int size) throws NumberFormatException { 107 Font font = new Font ("Dialog", Font.PLAIN, size); 109 setFont(font); fm = getFontMetrics(font); 111 } 112 113 114 void setRunningTextBounds(final Rectangle bounds) throws NumberFormatException { 115 view = bounds; 116 } 117 118 119 void setProgressBarEnabled(final boolean enabled) { 120 draw_bar = enabled; progressDragItem.setEnabled(enabled); 122 } 123 124 void setProgressBarBounds(final Rectangle bounds) throws NumberFormatException { 125 bar = bounds; 126 progressDragItem.setRectangle(bar); 127 } 128 129 void setColorCorner(final Color color) throws NumberFormatException { 130 color_corner = color; 131 } 132 133 void setColorEdge(final Color color) throws NumberFormatException { 134 color_edge = color; 135 } 136 137 138 void setTextColor(final Color color) throws NumberFormatException { 139 color_text = color; 140 } 141 142 void setColorBar(final Color color) throws NumberFormatException { 143 color_bar = color; 144 } 145 146 149 public void setText(final String text) { 150 EventQueue.invokeLater(new Runnable () { 153 public void run() { 154 if (text == null) { 155 repaint(dirty); 156 return; 157 } 158 159 if (fm == null) 160 return; 161 162 adjustText(text); 163 164 SwingUtilities.layoutCompoundLabel(fm, text, null, 165 SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT, 166 SplashComponentPreview.this.view, new Rectangle (), rect, 0); 167 textDragItem.setRectangle(SplashComponentPreview.this.view); 169 dirty = dirty.union(rect); 170 repaint(); 173 dirty = new Rectangle (rect); 174 } 175 }); 176 } 177 178 public void setMaxSteps(int maxSteps) { 180 this.maxSteps = maxSteps; 181 } 182 183 public void addToMaxSteps(int steps) { 185 tmpSteps += steps; 186 } 187 188 public void addAndSetMaxSteps(int steps) { 190 tmpSteps += steps; 191 maxSteps = tmpSteps; 192 } 193 194 public void increment(int steps) { 196 if (draw_bar) { 197 progress += steps; 198 if (progress > maxSteps) 199 progress = maxSteps; 200 else if (maxSteps > 0) { 201 int bl = bar.width * progress / maxSteps - barStart; 202 if (bl > 1 || barStart % 2 == 0) { 203 barLength = bl; 204 bar_inc = new Rectangle (bar.x + barStart, bar.y, barLength + 1, bar.height); 205 repaint(bar_inc); 207 } else { 209 } 211 } 212 } 213 } 214 215 public void resetSteps() { 216 progress = 0; 217 barStart = 0; 218 barLength = 0; 219 increment(maxSteps); 220 } 221 222 223 private void adjustText(String text){ 226 String newText = null; 227 String newString; 228 229 if (text == null) 230 return ; 231 232 if (fm == null) 233 return; 234 235 int width = fm.stringWidth(text); 236 237 if (width > view.width) { 238 StringTokenizer st = new StringTokenizer (text); 239 while (st.hasMoreTokens()) { 240 String element = st.nextToken(); 241 if (newText == null) 242 newString = element; 243 else 244 newString = newText + " " + element; if (fm.stringWidth(newString + "...") > view.width) { this.text = newText + "..."; break; 248 } else 249 newText = newString; 250 251 } 252 } else 253 this.text = text; 254 } 255 258 public void update(Graphics g) { 259 paint(g); 260 } 261 262 265 public void paint(Graphics g) { 266 super.paint(g); 267 280 dragManager.setTranslate(0,0); 281 originalPaint(g); 282 dragManager.paint(g); 283 } 284 285 public void originalPaint(Graphics graphics) { 286 Graphics2D g2d = (Graphics2D )graphics; 287 if (!isEnabled()) { 288 g2d.setComposite(AlphaComposite.getInstance( 289 AlphaComposite.SRC_OVER, 0.3f)); 290 } 291 292 graphics.setColor(color_text); 293 graphics.drawImage(image, 0, 0, null); 294 295 if (text == null) { 296 return; 298 } 299 300 if (fm == null) { 301 return; 304 } 305 306 SwingUtilities.layoutCompoundLabel(fm, text, null, 307 SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT, 308 this.view, new Rectangle (), rect, 0); 309 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 311 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 312 graphics.drawString(text, rect.x, rect.y + fm.getAscent()); 313 315 if (draw_bar && Boolean.getBoolean("netbeans.splash.nobar") == false && maxSteps > 0) { 316 graphics.setColor(color_bar); 317 graphics.fillRect(bar.x, bar.y, barStart + barLength, bar.height); 318 graphics.setColor(color_corner); 319 graphics.drawLine(bar.x, bar.y, bar.x, bar.y + bar.height); 320 graphics.drawLine(bar.x + barStart + barLength, bar.y, bar.x + barStart + barLength, bar.y + bar.height); 321 graphics.setColor(color_edge); 322 graphics.drawLine(bar.x, bar.y + bar.height / 2, bar.x, bar.y + bar.height / 2); 323 graphics.drawLine(bar.x + barStart + barLength, bar.y + bar.height / 2, bar.x + barStart + barLength, bar.y + bar.height / 2); 324 barStart += barLength; 325 barLength = 0; 326 } 327 } 328 329 public Dimension getPreferredSize() { 330 return new Dimension (image.getWidth(null), image.getHeight(null)); 331 } 332 333 336 337 public Rectangle getView() { 338 return view; 339 } 340 341 public void setEnabled(boolean enabled) { 342 super.setEnabled(enabled); 343 textDragItem.setEnabled(enabled); 344 progressDragItem.setEnabled(enabled & draw_bar); 345 } 346 } 347 | Popular Tags |