2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.apisupport.project.ui.customizer;
44
import java.awt.AlphaComposite;
45
import java.awt.Color;
46
import java.awt.Dimension;
47
import java.awt.EventQueue;
49
import java.awt.FontMetrics;
50
import java.awt.Graphics;
51
import java.awt.Graphics2D;
52
import java.awt.Image;
53
import java.awt.Rectangle;
54
import java.awt.RenderingHints;
56
import java.util.StringTokenizer;
57
import javax.swing.ImageIcon;
58
import javax.swing.JLabel;
59
import javax.swing.SwingConstants;
60
import javax.swing.SwingUtilities;
64
* @author Radek Matous
66
class SplashComponentPreview extends JLabel {
67
private FontMetrics fm;
68
private Rectangle view;
69
private Color color_text;
70
private Color color_bar;
71
private Color color_edge;
72
private Color color_corner;
74
private boolean draw_bar;
76
protected Image image;
77
private Rectangle dirty = new Rectangle();
79
private Rectangle rect = new Rectangle();
80
private Rectangle bar = new Rectangle();
81
private Rectangle bar_inc = new Rectangle();
83
private int progress = 0;
84
private int maxSteps = 0;
85
private int tmpSteps = 0;
86
private int barStart = 0;
87
private int barLength = 0;
89
private DragManager dragManager;
90
private DragManager.DragItem textDragItem;
91
private DragManager.DragItem progressDragItem;
94
* Creates a new splash screen component.
96
public SplashComponentPreview() {
97
//setBorder(new TitledBorder(NbBundle.getMessage(getClass(),"LBL_SplashPreview")));
98
dragManager = new DragManager(this);
99
textDragItem = dragManager.createNewItem();
100
progressDragItem = dragManager.createNewItem();
103
void setFontSize(final String fontSize) throws NumberFormatException {
105
String sizeStr = fontSize;
106
size = Integer.parseInt(sizeStr);
108
Font font = new Font("Dialog", Font.PLAIN, size);//NOI18N
110
setFont(font); // NOI18N
111
fm = getFontMetrics(font);
114
void setSplashImageIcon(final URL url) {
115
ImageIcon imgIcon = new ImageIcon(url);
116
this.image = imgIcon.getImage();
117
//this.image = image.getScaledInstance(398, 299, Image.SCALE_DEFAULT);
120
void setDropHandletForProgress (DragManager.DropHandler dHandler) {
121
this.progressDragItem.setDropHandler(dHandler);
124
void setDropHandletForText (DragManager.DropHandler dHandler) {
125
this.textDragItem.setDropHandler(dHandler);
128
void setFontSize(final int size) throws NumberFormatException {
129
Font font = new Font("Dialog", Font.PLAIN, size); // NOI18N
131
setFont(font); // NOI18N
132
fm = getFontMetrics(font);
136
void setRunningTextBounds(final Rectangle bounds) throws NumberFormatException {
141
void setProgressBarEnabled(final boolean enabled) {
142
draw_bar = enabled; // NOI18N
143
progressDragItem.setEnabled(enabled);
146
void setProgressBarBounds(final Rectangle bounds) throws NumberFormatException {
148
progressDragItem.setRectangle(bar);
151
void setColorCorner(final Color color) throws NumberFormatException {
152
color_corner = color;
155
void setColorEdge(final Color color) throws NumberFormatException {
160
void setTextColor(final Color color) throws NumberFormatException {
164
void setColorBar(final Color color) throws NumberFormatException {
169
* Defines the single line of text this component will display.
171
public void setText(final String text) {
172
// run in AWT, there were problems with accessing font metrics
173
// from now AWT thread
174
EventQueue.invokeLater(new Runnable() {
186
SwingUtilities.layoutCompoundLabel(fm, text, null,
187
SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT,
188
SplashComponentPreview.this.view, new Rectangle(), rect, 0);
189
//textDragItem.setRectangle(rect);
190
textDragItem.setRectangle(SplashComponentPreview.this.view);
191
dirty = dirty.union(rect);
192
// update screen (assume repaint manager optimizes unions;)
195
dirty = new Rectangle(rect);
200
// Defines a max value for splash progress bar.
201
public void setMaxSteps(int maxSteps) {
202
this.maxSteps = maxSteps;
205
// Adds temporary steps to create a max value for splash progress bar later.
206
public void addToMaxSteps(int steps) {
210
// Adds temporary steps and creates a max value for splash progress bar.
211
public void addAndSetMaxSteps(int steps) {
216
// Increments a current value of splash progress bar by given steps.
217
public void increment(int steps) {
220
if (progress > maxSteps)
222
else if (maxSteps > 0) {
223
int bl = bar.width * progress / maxSteps - barStart;
224
if (bl > 1 || barStart % 2 == 0) {
226
bar_inc = new Rectangle(bar.x + barStart, bar.y, barLength + 1, bar.height);
227
// System.out.println("progress: " + progress + "/" + maxSteps);
229
//System.err.println("(painting " + bar_inc + ")");
231
// too small, don't waste time painting it
237
public void resetSteps() {
245
//Creates new text with the ellipsis at the end when text width is
246
// bigger than allowed space
247
private void adjustText(String text){
248
String newText = null;
257
int width = fm.stringWidth(text);
259
if (width > view.width) {
260
StringTokenizer st = new StringTokenizer(text);
261
while (st.hasMoreTokens()) {
262
String element = st.nextToken();
266
newString = newText + " " + element; // NOI18N
267
if (fm.stringWidth(newString + "...") > view.width) { // NOI18N
268
this.text = newText + "..."; // NOI18N
278
* Override update to *not* erase the background before painting.
280
public void update(Graphics g) {
285
* Renders this component to the given graphics.
287
public void paint(Graphics g) {
289
/*int width = image.getWidth(null);//BasicBrandingModel.SPLASH_WIDTH;
290
int height = image.getHeight(null);//BasicBrandingModel.SPLASH_HEIGHT;
291
int x = (getWidth()/2)-(width/2);
292
int y = (getHeight()/2)-(height/2);
294
Graphics2D g2d = (Graphics2D) g;
295
AffineTransform tx = g2d.getTransform();
299
dragManager.setTranslate(x,y);
300
g2d.setTransform(tx);
302
dragManager.setTranslate(0,0);
304
dragManager.paint(g);
307
public void originalPaint(Graphics graphics) {
308
Graphics2D g2d = (Graphics2D)graphics;
310
g2d.setComposite(AlphaComposite.getInstance(
311
AlphaComposite.SRC_OVER, 0.3f));
314
graphics.setColor(color_text);
315
graphics.drawImage(image, 0, 0, null);
323
// XXX(-ttran) this happened on Japanese Windows NT, don't
324
// fully understand why
328
SwingUtilities.layoutCompoundLabel(fm, text, null,
329
SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT,
330
this.view, new Rectangle(), rect, 0);
331
// turn anti-aliasing on for the splash text
332
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
333
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
334
graphics.drawString(text, rect.x, rect.y + fm.getAscent());
335
// Draw progress bar if applicable
337
if (draw_bar && Boolean.getBoolean("netbeans.splash.nobar") == false && maxSteps > 0/* && barLength > 0*/) {
338
graphics.setColor(color_bar);
339
graphics.fillRect(bar.x, bar.y, barStart + barLength, bar.height);
340
graphics.setColor(color_corner);
341
graphics.drawLine(bar.x, bar.y, bar.x, bar.y + bar.height);
342
graphics.drawLine(bar.x + barStart + barLength, bar.y, bar.x + barStart + barLength, bar.y + bar.height);
343
graphics.setColor(color_edge);
344
graphics.drawLine(bar.x, bar.y + bar.height / 2, bar.x, bar.y + bar.height / 2);
345
graphics.drawLine(bar.x + barStart + barLength, bar.y + bar.height / 2, bar.x + barStart + barLength, bar.y + bar.height / 2);
346
barStart += barLength;
351
public Dimension getPreferredSize() {
352
return new Dimension(image.getWidth(null), image.getHeight(null));
355
/*public boolean isOpaque() {
359
public Rectangle getView() {
363
public void setEnabled(boolean enabled) {
364
super.setEnabled(enabled);
365
textDragItem.setEnabled(enabled);
366
progressDragItem.setEnabled(enabled & draw_bar);