~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to apisupport/project/src/org/netbeans/modules/apisupport/project/ui/customizer/SplashComponentPreview.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.apisupport.project.ui.customizer;
 
43
 
 
44
import java.awt.AlphaComposite;
 
45
import java.awt.Color;
 
46
import java.awt.Dimension;
 
47
import java.awt.EventQueue;
 
48
import java.awt.Font;
 
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;
 
55
import java.net.URL;
 
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;
 
61
 
 
62
 
 
63
/**
 
64
 * @author Radek Matous
 
65
 */
 
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;
 
73
    
 
74
    private boolean draw_bar;
 
75
    
 
76
    protected Image image;
 
77
    private Rectangle dirty = new Rectangle();
 
78
    private String text;
 
79
    private Rectangle rect = new Rectangle();
 
80
    private Rectangle bar = new Rectangle();
 
81
    private Rectangle bar_inc = new Rectangle();
 
82
    
 
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;
 
88
    
 
89
    private DragManager dragManager;
 
90
    private DragManager.DragItem textDragItem;
 
91
    private DragManager.DragItem progressDragItem;
 
92
        
 
93
    /**
 
94
     * Creates a new splash screen component.
 
95
     */
 
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();
 
101
    }
 
102
    
 
103
    void setFontSize(final String fontSize) 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
    
 
136
    void setRunningTextBounds(final Rectangle bounds) throws NumberFormatException {        
 
137
        view = bounds;
 
138
    }
 
139
 
 
140
    
 
141
    void setProgressBarEnabled(final boolean enabled) {
 
142
        draw_bar = enabled; // NOI18N
 
143
        progressDragItem.setEnabled(enabled);
 
144
    }
 
145
    
 
146
    void setProgressBarBounds(final Rectangle bounds) 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
    
 
160
    void setTextColor(final Color color) throws NumberFormatException {
 
161
        color_text = color;
 
162
    }
 
163
    
 
164
    void setColorBar(final Color color) throws NumberFormatException {
 
165
        color_bar = color;
 
166
    }
 
167
    
 
168
    /**
 
169
     * Defines the single line of text this component will display.
 
170
     */
 
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() {
 
175
            public void run() {
 
176
                if (text == null) {
 
177
                    repaint(dirty);
 
178
                    return;
 
179
                }
 
180
                
 
181
                if (fm == null)
 
182
                    return;
 
183
                
 
184
                adjustText(text);
 
185
                
 
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;)
 
193
//                repaint(dirty);
 
194
                repaint();
 
195
                dirty = new Rectangle(rect);
 
196
            }
 
197
        });
 
198
    }
 
199
    
 
200
    // Defines a max value for splash progress bar.
 
201
    public void setMaxSteps(int maxSteps) {
 
202
        this.maxSteps = maxSteps;
 
203
    }
 
204
    
 
205
    // Adds temporary steps to create a max value for splash progress bar later.
 
206
    public void addToMaxSteps(int steps) {
 
207
        tmpSteps += steps;
 
208
    }
 
209
    
 
210
    // Adds temporary steps and creates a max value for splash progress bar.
 
211
    public void addAndSetMaxSteps(int steps) {
 
212
        tmpSteps += steps;
 
213
        maxSteps = tmpSteps;
 
214
    }
 
215
    
 
216
    // Increments a current value of splash progress bar by given steps.
 
217
    public void increment(int steps) {
 
218
        if (draw_bar) {
 
219
            progress += steps;
 
220
            if (progress > maxSteps)
 
221
                progress = maxSteps;
 
222
            else if (maxSteps > 0) {
 
223
                int bl = bar.width * progress / maxSteps - barStart;
 
224
                if (bl > 1 || barStart % 2 == 0) {
 
225
                    barLength = bl;
 
226
                    bar_inc = new Rectangle(bar.x + barStart, bar.y, barLength + 1, bar.height);
 
227
//                    System.out.println("progress: " + progress + "/" + maxSteps);
 
228
                    repaint(bar_inc);
 
229
                    //System.err.println("(painting " + bar_inc + ")");
 
230
                } else {
 
231
                    // too small, don't waste time painting it
 
232
                }
 
233
            }
 
234
        }
 
235
    }
 
236
    
 
237
    public void resetSteps() {
 
238
        progress = 0;
 
239
        barStart = 0;
 
240
        barLength = 0;
 
241
        increment(maxSteps);
 
242
    }
 
243
    
 
244
    
 
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;
 
249
        String newString;
 
250
        
 
251
        if (text == null)
 
252
            return ;
 
253
        
 
254
        if (fm == null)
 
255
            return;
 
256
        
 
257
        int width = fm.stringWidth(text);
 
258
        
 
259
        if (width > view.width) {
 
260
            StringTokenizer st = new StringTokenizer(text);
 
261
            while (st.hasMoreTokens()) {
 
262
                String element = st.nextToken();
 
263
                if (newText == null)
 
264
                    newString = element;
 
265
                else
 
266
                    newString = newText + " " + element; // NOI18N
 
267
                if (fm.stringWidth(newString + "...") > view.width) { // NOI18N
 
268
                    this.text = newText + "..."; // NOI18N
 
269
                    break;
 
270
                } else
 
271
                    newText = newString;
 
272
                
 
273
            }
 
274
        } else
 
275
            this.text = text;
 
276
    }
 
277
    /**
 
278
     * Override update to *not* erase the background before painting.
 
279
     */
 
280
    public void update(Graphics g) {
 
281
        paint(g);
 
282
    }
 
283
    
 
284
    /**
 
285
     * Renders this component to the given graphics.
 
286
     */
 
287
    public void paint(Graphics g) {
 
288
        super.paint(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);
 
293
        
 
294
        Graphics2D g2d = (Graphics2D) g;
 
295
        AffineTransform tx = g2d.getTransform();
 
296
        
 
297
        
 
298
        tx.translate(x, y);
 
299
        dragManager.setTranslate(x,y);
 
300
        g2d.setTransform(tx);
 
301
        */
 
302
        dragManager.setTranslate(0,0);
 
303
        originalPaint(g);
 
304
        dragManager.paint(g);
 
305
    }
 
306
    
 
307
    public void originalPaint(Graphics graphics) {
 
308
        Graphics2D g2d = (Graphics2D)graphics;
 
309
        if (!isEnabled()) {
 
310
            g2d.setComposite(AlphaComposite.getInstance(
 
311
                    AlphaComposite.SRC_OVER, 0.3f));
 
312
        }
 
313
        
 
314
        graphics.setColor(color_text);
 
315
        graphics.drawImage(image, 0, 0, null);
 
316
        
 
317
        if (text == null) {
 
318
            // no text to draw
 
319
            return;
 
320
        }
 
321
        
 
322
        if (fm == null) {
 
323
            // XXX(-ttran) this happened on Japanese Windows NT, don't
 
324
            // fully understand why
 
325
            return;
 
326
        }
 
327
        
 
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
 
336
        
 
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;
 
347
            barLength = 0;
 
348
        }
 
349
    }
 
350
    
 
351
    public Dimension getPreferredSize() {
 
352
        return new Dimension(image.getWidth(null), image.getHeight(null));
 
353
    }
 
354
    
 
355
    /*public boolean isOpaque() {
 
356
        return true;
 
357
    }*/
 
358
    
 
359
    public Rectangle getView() {
 
360
        return view;
 
361
    }
 
362
 
 
363
    public void setEnabled(boolean enabled) {
 
364
        super.setEnabled(enabled);
 
365
        textDragItem.setEnabled(enabled);
 
366
        progressDragItem.setEnabled(enabled & draw_bar);
 
367
    }
 
368
}