KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > SplashComponentPreview


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.customizer;
21
22 import java.awt.AlphaComposite JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.EventQueue JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.FontMetrics JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Graphics2D JavaDoc;
30 import java.awt.Image JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.RenderingHints JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.SwingConstants JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39
40
41 /**
42  * @author Radek Matous
43  */

44 class SplashComponentPreview extends JLabel JavaDoc {
45     private FontMetrics JavaDoc fm;
46     private Rectangle JavaDoc view;
47     private Color JavaDoc color_text;
48     private Color JavaDoc color_bar;
49     private Color JavaDoc color_edge;
50     private Color JavaDoc color_corner;
51     
52     private boolean draw_bar;
53     
54     protected Image JavaDoc image;
55     private Rectangle JavaDoc dirty = new Rectangle JavaDoc();
56     private String JavaDoc text;
57     private Rectangle JavaDoc rect = new Rectangle JavaDoc();
58     private Rectangle JavaDoc bar = new Rectangle JavaDoc();
59     private Rectangle JavaDoc bar_inc = new Rectangle JavaDoc();
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     /**
72      * Creates a new splash screen component.
73      */

74     public SplashComponentPreview() {
75         //setBorder(new TitledBorder(NbBundle.getMessage(getClass(),"LBL_SplashPreview")));
76
dragManager = new DragManager(this);
77         textDragItem = dragManager.createNewItem();
78         progressDragItem = dragManager.createNewItem();
79     }
80     
81     void setFontSize(final String JavaDoc fontSize) throws NumberFormatException JavaDoc {
82         int size;
83         String JavaDoc sizeStr = fontSize;
84         size = Integer.parseInt(sizeStr);
85         
86         Font JavaDoc font = new Font JavaDoc("Dialog", Font.PLAIN, size);//NOI18N
87

88         setFont(font); // NOI18N
89
fm = getFontMetrics(font);
90     }
91     
92     void setSplashImageIcon(final URL JavaDoc url) {
93         ImageIcon JavaDoc imgIcon = new ImageIcon JavaDoc(url);
94         this.image = imgIcon.getImage();
95         //this.image = image.getScaledInstance(398, 299, Image.SCALE_DEFAULT);
96
}
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 JavaDoc {
107         Font JavaDoc font = new Font JavaDoc("Dialog", Font.PLAIN, size); // NOI18N
108

109         setFont(font); // NOI18N
110
fm = getFontMetrics(font);
111     }
112     
113     
114     void setRunningTextBounds(final Rectangle JavaDoc bounds) throws NumberFormatException JavaDoc {
115         view = bounds;
116     }
117
118     
119     void setProgressBarEnabled(final boolean enabled) {
120         draw_bar = enabled; // NOI18N
121
progressDragItem.setEnabled(enabled);
122     }
123     
124     void setProgressBarBounds(final Rectangle JavaDoc bounds) throws NumberFormatException JavaDoc {
125         bar = bounds;
126         progressDragItem.setRectangle(bar);
127     }
128     
129     void setColorCorner(final Color JavaDoc color) throws NumberFormatException JavaDoc {
130         color_corner = color;
131     }
132     
133     void setColorEdge(final Color JavaDoc color) throws NumberFormatException JavaDoc {
134         color_edge = color;
135     }
136
137     
138     void setTextColor(final Color JavaDoc color) throws NumberFormatException JavaDoc {
139         color_text = color;
140     }
141     
142     void setColorBar(final Color JavaDoc color) throws NumberFormatException JavaDoc {
143         color_bar = color;
144     }
145     
146     /**
147      * Defines the single line of text this component will display.
148      */

149     public void setText(final String JavaDoc text) {
150         // run in AWT, there were problems with accessing font metrics
151
// from now AWT thread
152
EventQueue.invokeLater(new Runnable JavaDoc() {
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 JavaDoc(), rect, 0);
167                 //textDragItem.setRectangle(rect);
168
textDragItem.setRectangle(SplashComponentPreview.this.view);
169                 dirty = dirty.union(rect);
170                 // update screen (assume repaint manager optimizes unions;)
171
// repaint(dirty);
172
repaint();
173                 dirty = new Rectangle JavaDoc(rect);
174             }
175         });
176     }
177     
178     // Defines a max value for splash progress bar.
179
public void setMaxSteps(int maxSteps) {
180         this.maxSteps = maxSteps;
181     }
182     
183     // Adds temporary steps to create a max value for splash progress bar later.
184
public void addToMaxSteps(int steps) {
185         tmpSteps += steps;
186     }
187     
188     // Adds temporary steps and creates a max value for splash progress bar.
189
public void addAndSetMaxSteps(int steps) {
190         tmpSteps += steps;
191         maxSteps = tmpSteps;
192     }
193     
194     // Increments a current value of splash progress bar by given steps.
195
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 JavaDoc(bar.x + barStart, bar.y, barLength + 1, bar.height);
205 // System.out.println("progress: " + progress + "/" + maxSteps);
206
repaint(bar_inc);
207                     //System.err.println("(painting " + bar_inc + ")");
208
} else {
209                     // too small, don't waste time painting it
210
}
211             }
212         }
213     }
214     
215     public void resetSteps() {
216         progress = 0;
217         barStart = 0;
218         barLength = 0;
219         increment(maxSteps);
220     }
221     
222     
223     //Creates new text with the ellipsis at the end when text width is
224
// bigger than allowed space
225
private void adjustText(String JavaDoc text){
226         String JavaDoc newText = null;
227         String JavaDoc 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 JavaDoc st = new StringTokenizer JavaDoc(text);
239             while (st.hasMoreTokens()) {
240                 String JavaDoc element = st.nextToken();
241                 if (newText == null)
242                     newString = element;
243                 else
244                     newString = newText + " " + element; // NOI18N
245
if (fm.stringWidth(newString + "...") > view.width) { // NOI18N
246
this.text = newText + "..."; // NOI18N
247
break;
248                 } else
249                     newText = newString;
250                 
251             }
252         } else
253             this.text = text;
254     }
255     /**
256      * Override update to *not* erase the background before painting.
257      */

258     public void update(Graphics JavaDoc g) {
259         paint(g);
260     }
261     
262     /**
263      * Renders this component to the given graphics.
264      */

265     public void paint(Graphics JavaDoc g) {
266         super.paint(g);
267         /*int width = image.getWidth(null);//BasicBrandingModel.SPLASH_WIDTH;
268         int height = image.getHeight(null);//BasicBrandingModel.SPLASH_HEIGHT;
269         int x = (getWidth()/2)-(width/2);
270         int y = (getHeight()/2)-(height/2);
271         
272         Graphics2D g2d = (Graphics2D) g;
273         AffineTransform tx = g2d.getTransform();
274         
275         
276         tx.translate(x, y);
277         dragManager.setTranslate(x,y);
278         g2d.setTransform(tx);
279         */

280         dragManager.setTranslate(0,0);
281         originalPaint(g);
282         dragManager.paint(g);
283     }
284     
285     public void originalPaint(Graphics JavaDoc graphics) {
286         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)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             // no text to draw
297
return;
298         }
299         
300         if (fm == null) {
301             // XXX(-ttran) this happened on Japanese Windows NT, don't
302
// fully understand why
303
return;
304         }
305         
306         SwingUtilities.layoutCompoundLabel(fm, text, null,
307                 SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT,
308                 this.view, new Rectangle JavaDoc(), rect, 0);
309         // turn anti-aliasing on for the splash text
310
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
311                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
312         graphics.drawString(text, rect.x, rect.y + fm.getAscent());
313         // Draw progress bar if applicable
314

315         if (draw_bar && Boolean.getBoolean("netbeans.splash.nobar") == false && maxSteps > 0/* && barLength > 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 JavaDoc getPreferredSize() {
330         return new Dimension JavaDoc(image.getWidth(null), image.getHeight(null));
331     }
332     
333     /*public boolean isOpaque() {
334         return true;
335     }*/

336     
337     public Rectangle JavaDoc 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