001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.ui.customizer;
043:
044: import java.awt.AlphaComposite;
045: import java.awt.Color;
046: import java.awt.Dimension;
047: import java.awt.EventQueue;
048: import java.awt.Font;
049: import java.awt.FontMetrics;
050: import java.awt.Graphics;
051: import java.awt.Graphics2D;
052: import java.awt.Image;
053: import java.awt.Rectangle;
054: import java.awt.RenderingHints;
055: import java.net.URL;
056: import java.util.StringTokenizer;
057: import javax.swing.ImageIcon;
058: import javax.swing.JLabel;
059: import javax.swing.SwingConstants;
060: import javax.swing.SwingUtilities;
061:
062: /**
063: * @author Radek Matous
064: */
065: class SplashComponentPreview extends JLabel {
066: private FontMetrics fm;
067: private Rectangle view;
068: private Color color_text;
069: private Color color_bar;
070: private Color color_edge;
071: private Color color_corner;
072:
073: private boolean draw_bar;
074:
075: protected Image image;
076: private Rectangle dirty = new Rectangle();
077: private String text;
078: private Rectangle rect = new Rectangle();
079: private Rectangle bar = new Rectangle();
080: private Rectangle bar_inc = new Rectangle();
081:
082: private int progress = 0;
083: private int maxSteps = 0;
084: private int tmpSteps = 0;
085: private int barStart = 0;
086: private int barLength = 0;
087:
088: private DragManager dragManager;
089: private DragManager.DragItem textDragItem;
090: private DragManager.DragItem progressDragItem;
091:
092: /**
093: * Creates a new splash screen component.
094: */
095: public SplashComponentPreview() {
096: //setBorder(new TitledBorder(NbBundle.getMessage(getClass(),"LBL_SplashPreview")));
097: dragManager = new DragManager(this );
098: textDragItem = dragManager.createNewItem();
099: progressDragItem = dragManager.createNewItem();
100: }
101:
102: void setFontSize(final String fontSize)
103: throws NumberFormatException {
104: int size;
105: String sizeStr = fontSize;
106: size = Integer.parseInt(sizeStr);
107:
108: Font font = new Font("Dialog", Font.PLAIN, size);//NOI18N
109:
110: setFont(font); // NOI18N
111: fm = getFontMetrics(font);
112: }
113:
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);
118: }
119:
120: void setDropHandletForProgress(DragManager.DropHandler dHandler) {
121: this .progressDragItem.setDropHandler(dHandler);
122: }
123:
124: void setDropHandletForText(DragManager.DropHandler dHandler) {
125: this .textDragItem.setDropHandler(dHandler);
126: }
127:
128: void setFontSize(final int size) throws NumberFormatException {
129: Font font = new Font("Dialog", Font.PLAIN, size); // NOI18N
130:
131: setFont(font); // NOI18N
132: fm = getFontMetrics(font);
133: }
134:
135: void setRunningTextBounds(final Rectangle bounds)
136: throws NumberFormatException {
137: view = bounds;
138: }
139:
140: void setProgressBarEnabled(final boolean enabled) {
141: draw_bar = enabled; // NOI18N
142: progressDragItem.setEnabled(enabled);
143: }
144:
145: void setProgressBarBounds(final Rectangle bounds)
146: throws NumberFormatException {
147: bar = bounds;
148: progressDragItem.setRectangle(bar);
149: }
150:
151: void setColorCorner(final Color color) throws NumberFormatException {
152: color_corner = color;
153: }
154:
155: void setColorEdge(final Color color) throws NumberFormatException {
156: color_edge = color;
157: }
158:
159: void setTextColor(final Color color) throws NumberFormatException {
160: color_text = color;
161: }
162:
163: void setColorBar(final Color color) throws NumberFormatException {
164: color_bar = color;
165: }
166:
167: /**
168: * Defines the single line of text this component will display.
169: */
170: public void setText(final String text) {
171: // run in AWT, there were problems with accessing font metrics
172: // from now AWT thread
173: EventQueue.invokeLater(new Runnable() {
174: public void run() {
175: if (text == null) {
176: repaint(dirty);
177: return;
178: }
179:
180: if (fm == null)
181: return;
182:
183: adjustText(text);
184:
185: SwingUtilities.layoutCompoundLabel(fm, text, null,
186: SwingConstants.BOTTOM, SwingConstants.LEFT,
187: SwingConstants.BOTTOM, SwingConstants.LEFT,
188: SplashComponentPreview.this .view,
189: new Rectangle(), rect, 0);
190: //textDragItem.setRectangle(rect);
191: textDragItem
192: .setRectangle(SplashComponentPreview.this .view);
193: dirty = dirty.union(rect);
194: // update screen (assume repaint manager optimizes unions;)
195: // repaint(dirty);
196: repaint();
197: dirty = new Rectangle(rect);
198: }
199: });
200: }
201:
202: // Defines a max value for splash progress bar.
203: public void setMaxSteps(int maxSteps) {
204: this .maxSteps = maxSteps;
205: }
206:
207: // Adds temporary steps to create a max value for splash progress bar later.
208: public void addToMaxSteps(int steps) {
209: tmpSteps += steps;
210: }
211:
212: // Adds temporary steps and creates a max value for splash progress bar.
213: public void addAndSetMaxSteps(int steps) {
214: tmpSteps += steps;
215: maxSteps = tmpSteps;
216: }
217:
218: // Increments a current value of splash progress bar by given steps.
219: public void increment(int steps) {
220: if (draw_bar) {
221: progress += steps;
222: if (progress > maxSteps)
223: progress = maxSteps;
224: else if (maxSteps > 0) {
225: int bl = bar.width * progress / maxSteps - barStart;
226: if (bl > 1 || barStart % 2 == 0) {
227: barLength = bl;
228: bar_inc = new Rectangle(bar.x + barStart, bar.y,
229: barLength + 1, bar.height);
230: // System.out.println("progress: " + progress + "/" + maxSteps);
231: repaint(bar_inc);
232: //System.err.println("(painting " + bar_inc + ")");
233: } else {
234: // too small, don't waste time painting it
235: }
236: }
237: }
238: }
239:
240: public void resetSteps() {
241: progress = 0;
242: barStart = 0;
243: barLength = 0;
244: increment(maxSteps);
245: }
246:
247: //Creates new text with the ellipsis at the end when text width is
248: // bigger than allowed space
249: private void adjustText(String text) {
250: String newText = null;
251: String newString;
252:
253: if (text == null)
254: return;
255:
256: if (fm == null)
257: return;
258:
259: int width = fm.stringWidth(text);
260:
261: if (width > view.width) {
262: StringTokenizer st = new StringTokenizer(text);
263: while (st.hasMoreTokens()) {
264: String element = st.nextToken();
265: if (newText == null)
266: newString = element;
267: else
268: newString = newText + " " + element; // NOI18N
269: if (fm.stringWidth(newString + "...") > view.width) { // NOI18N
270: this .text = newText + "..."; // NOI18N
271: break;
272: } else
273: newText = newString;
274:
275: }
276: } else
277: this .text = text;
278: }
279:
280: /**
281: * Override update to *not* erase the background before painting.
282: */
283: public void update(Graphics g) {
284: paint(g);
285: }
286:
287: /**
288: * Renders this component to the given graphics.
289: */
290: public void paint(Graphics g) {
291: super .paint(g);
292: /*int width = image.getWidth(null);//BasicBrandingModel.SPLASH_WIDTH;
293: int height = image.getHeight(null);//BasicBrandingModel.SPLASH_HEIGHT;
294: int x = (getWidth()/2)-(width/2);
295: int y = (getHeight()/2)-(height/2);
296:
297: Graphics2D g2d = (Graphics2D) g;
298: AffineTransform tx = g2d.getTransform();
299:
300:
301: tx.translate(x, y);
302: dragManager.setTranslate(x,y);
303: g2d.setTransform(tx);
304: */
305: dragManager.setTranslate(0, 0);
306: originalPaint(g);
307: dragManager.paint(g);
308: }
309:
310: public void originalPaint(Graphics graphics) {
311: Graphics2D g2d = (Graphics2D) graphics;
312: if (!isEnabled()) {
313: g2d.setComposite(AlphaComposite.getInstance(
314: AlphaComposite.SRC_OVER, 0.3f));
315: }
316:
317: graphics.setColor(color_text);
318: graphics.drawImage(image, 0, 0, null);
319:
320: if (text == null) {
321: // no text to draw
322: return;
323: }
324:
325: if (fm == null) {
326: // XXX(-ttran) this happened on Japanese Windows NT, don't
327: // fully understand why
328: return;
329: }
330:
331: SwingUtilities.layoutCompoundLabel(fm, text, null,
332: SwingConstants.BOTTOM, SwingConstants.LEFT,
333: SwingConstants.BOTTOM, SwingConstants.LEFT, this .view,
334: new Rectangle(), rect, 0);
335: // turn anti-aliasing on for the splash text
336: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
337: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
338: graphics.drawString(text, rect.x, rect.y + fm.getAscent());
339: // Draw progress bar if applicable
340:
341: if (draw_bar
342: && Boolean.getBoolean("netbeans.splash.nobar") == false
343: && maxSteps > 0/* && barLength > 0*/) {
344: graphics.setColor(color_bar);
345: graphics.fillRect(bar.x, bar.y, barStart + barLength,
346: bar.height);
347: graphics.setColor(color_corner);
348: graphics.drawLine(bar.x, bar.y, bar.x, bar.y + bar.height);
349: graphics.drawLine(bar.x + barStart + barLength, bar.y,
350: bar.x + barStart + barLength, bar.y + bar.height);
351: graphics.setColor(color_edge);
352: graphics.drawLine(bar.x, bar.y + bar.height / 2, bar.x,
353: bar.y + bar.height / 2);
354: graphics.drawLine(bar.x + barStart + barLength, bar.y
355: + bar.height / 2, bar.x + barStart + barLength,
356: bar.y + bar.height / 2);
357: barStart += barLength;
358: barLength = 0;
359: }
360: }
361:
362: public Dimension getPreferredSize() {
363: return new Dimension(image.getWidth(null), image
364: .getHeight(null));
365: }
366:
367: /*public boolean isOpaque() {
368: return true;
369: }*/
370:
371: public Rectangle getView() {
372: return view;
373: }
374:
375: public void setEnabled(boolean enabled) {
376: super.setEnabled(enabled);
377: textDragItem.setEnabled(enabled);
378: progressDragItem.setEnabled(enabled & draw_bar);
379: }
380: }
|