Debian Codegraph @

netx/net/sourceforge/jnlp/splashscreen/impls/defaultsplashscreen2012/BasePainter.java

raw
1 /* BasePainter.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 package net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012;
38
39 import java.awt.BasicStroke;
40 import net.sourceforge.jnlp.splashscreen.impls.*;
41 import java.awt.Color;
42 import java.awt.Font;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Graphics2D;
46 import java.awt.Point;
47 import java.awt.RenderingHints;
48 import java.awt.Stroke;
49 import java.awt.font.TextAttribute;
50 import java.awt.image.BufferedImage;
51 import java.util.HashMap;
52 import java.util.List;
53
54 import java.util.Map;
55 import java.util.Observable;
56 import java.util.Observer;
57
58 import net.sourceforge.swing.SwingUtils;
59 import net.sourceforge.jnlp.runtime.Translator;
60 import net.sourceforge.jnlp.splashscreen.SplashUtils.SplashReason;
61 import net.sourceforge.jnlp.splashscreen.parts.BasicComponentSplashScreen;
62 import net.sourceforge.jnlp.splashscreen.parts.InfoItem;
63 import net.sourceforge.jnlp.splashscreen.parts.InformationElement;
64 import net.sourceforge.jnlp.splashscreen.parts.extensions.ExtensionManager;
65 import net.sourceforge.jnlp.util.logging.OutputController;
66 import net.sourceforge.jnlp.util.ScreenFinder;
67
68 public class BasePainter implements Observer {
69
70 protected final BasicComponentSplashScreen master;
71 //animations
72 //waterLevel of water (0-100%)
73 private int waterLevel = 0;
74 //waving of water and position of shhadowed WEB
75 private int animationsPosition = 0;
76 private int greyTextIncrment = 15; //how quickly is greyed web moving
77 //colors
78 protected static final Color TEA_LIVE_COLOR = new Color(205, 1, 3);
79 protected static final Color BACKGROUND_LIVE_COLOR = ExtensionManager.getExtension().getBackground();
80 protected static final Color TEA_LEAFS_STALKS_LIVE_COLOR = Color.black;
81 protected static final Color PLUGIN_LIVE_COLOR = ExtensionManager.getExtension().getPluginTextColor();
82 public static final Color WATER_LIVE_COLOR = new Color(80, 131, 160);
83 protected static final Color PLAIN_TEXT_LIVE_COLOR = ExtensionManager.getExtension().getTextColor();
84 protected Color teaColor;
85 protected Color backgroundColor;
86 protected Color teaLeafsStalksColor;
87 protected Color pluginColor;
88 protected Color waterColor;
89 protected Color plainTextColor;
90 //BufferedImage tmpBackround; //testingBackground for fitting
91 protected BufferedImage prerenderedStuff;
92 private Font teaFont;
93 private Font icedFont;
94 private Font webFont;
95 private Font pluginFont;
96 private Font plainTextsFont;
97 private Font alternativeTextFont;
98 //those spaces are meaningful for centering the text.. thats why alternative;)
99 private static final String alternativeICED = "Iced ";
100 private static final String alternativeWeb = "Web ";
101 private static final String alternativeTtea = " Tea";
102 private static final String alternativePlugin = "plugin ";
103 private static final String ICED = "Iced";
104 private static final String web = "web";
105 private static final String tea = "Tea";
106 private static final String plugin = "plugin ";
107 //inidivdual sizes, all converging to ZERO!!
108 /**
109 * Experimentaly meassured best top position for painted parts of vectros
110 */
111 private final int WEB_TOP_ALIGMENT = 324;
112 /**
113 * Experimentaly meassured best left position for painted parts of vectors
114 */
115 private final int WEB_LEFT_ALIGMENT = 84;
116 //enabling
117 protected boolean showNiceTexts = true;
118 private boolean showLeaf = true;
119 private boolean showInfo = true;
120 protected TextWithWaterLevel twl;
121 protected TextWithWaterLevel oldTwl;
122 protected boolean canWave = true;
123 private Point aboutOfset = new Point();
124
125 private final static float dash1[] = {10.0f};
126 private final static BasicStroke dashed
127 = new BasicStroke(1.0f,
128 BasicStroke.CAP_BUTT,
129 BasicStroke.JOIN_MITER,
130 10.0f, dash1, 0.0f);
131
132 protected void paintNiceTexts(Graphics2D g2d) {
133 //the only animated stuff
134 oldTwl = twl;
135 twl = new TextWithWaterLevel(ICED, icedFont);
136 if (oldTwl != null && !canWave) {
137 twl.setCachedPolygon(oldTwl.getCachedPolygon());
138 }
139 twl.setPercentageOfWater(waterLevel);
140 twl.setBgColor(backgroundColor);
141 twl.setWaterColor(waterColor);
142 twl.cutTo(g2d, scaleX(42), scaleY(278));
143 MovingText mt = new MovingText(web, webFont);
144 mt.setPercentageOfWater(animationsPosition);
145 mt.cutTo(g2d, scaleX(WEB_LEFT_ALIGMENT), scaleY(WEB_TOP_ALIGMENT));
146 }
147
148 protected void paintPlainTexts(Graphics2D g2d) {
149 g2d.setFont(alternativeTextFont);
150 g2d.setColor(waterColor);
151 drawTextAroundCenter(g2d, -0.6d, alternativeICED);
152 g2d.setColor(teaColor);
153 drawTextAroundCenter(g2d, -0.6d, alternativeTtea);
154 g2d.setColor(pluginColor);
155 String s = getAlternativeProductName();
156 int sub = animationsPosition / greyTextIncrment;
157 sub = sub % s.length();
158 if (!master.isAnimationRunning()) {
159 sub = s.length();
160 }
161 drawTextAroundCenter(g2d, 0.3d, s.substring(0, sub));
162 }
163 //enabling end
164
165 private int scaleAvarage(double origValue) {
166 return (int) (avarageRatio() * origValue);
167 }
168
169 private int scaleMax(double origValue) {
170 return (int) (maxRatio() * origValue);
171 }
172
173 private int scaleMin(double origValue) {
174 return (int) (minRatio() * origValue);
175 }
176
177 private double avarageRatio() {
178 return (getRatioX() + getRatioY()) / 2d;
179 }
180
181 private double minRatio() {
182 return Math.min(getRatioX(), getRatioY());
183 }
184
185 private double maxRatio() {
186 return Math.max(getRatioX(), getRatioY());
187 }
188
189 private int scaleY(double origValue) {
190 return (int) scaleY(master.getSplashHeight(), origValue);
191 }
192
193 private int scaleX(double origValue) {
194 return (int) scaleX(master.getSplashWidth(), origValue);
195 }
196
197 private double getRatioY() {
198 return getRatio(DefaultSplashScreen2012.ORIGINAL_H, master.getSplashHeight());
199 }
200
201 private double getRatioX() {
202 return getRatio(DefaultSplashScreen2012.ORIGINAL_W, master.getSplashWidth());
203 }
204
205 private static double scaleY(double currentSize, double origValue) {
206 return scale(DefaultSplashScreen2012.ORIGINAL_H, currentSize, origValue);
207 }
208
209 private static double scaleX(double currentSize, double origValue) {
210 return scale(DefaultSplashScreen2012.ORIGINAL_W, currentSize, origValue);
211 }
212
213 private static double getRatioY(double currentSize) {
214 return getRatio(DefaultSplashScreen2012.ORIGINAL_H, currentSize);
215 }
216
217 private static double getRatioX(double currentSize) {
218 return getRatio(DefaultSplashScreen2012.ORIGINAL_W, currentSize);
219 }
220
221 public static double scale(double origSize, double currentSize, double origValue) {
222 return getRatio(origSize, currentSize) * origValue;
223 }
224
225 public static double getRatio(double origSize, double currentSize) {
226 return (currentSize / origSize);
227 }
228 //size is considered from 0-origsize as 0-1.
229
230 //scaling end
231 public BasePainter(BasicComponentSplashScreen master) {
232 this(master, false);
233 }
234
235 public BasePainter(BasicComponentSplashScreen master, boolean startAnimation) {
236 //to have this in inner classes
237 this.master = master;
238 setColors();
239 adjustForSize(master.getSplashWidth(), master.getSplashHeight());
240 ExtensionManager.getExtension().adjustForSize(master.getSplashWidth(), master.getSplashHeight());
241 if (startAnimation) {
242 startAnimationThreads();
243 }
244
245 }
246
247 public void increaseAnimationPosition() {
248 ExtensionManager.getExtension().animate();
249 animationsPosition += greyTextIncrment;
250 }
251
252 protected void ensurePrerenderedStuff() {
253 if (this.prerenderedStuff == null) {
254 this.prerenderedStuff = prerenderStill();
255 }
256 }
257
258 public void paint(Graphics g) {
259 Graphics2D g2d = (Graphics2D) g;
260 ensurePrerenderedStuff();
261 if (prerenderedStuff != null) {
262 g2d.drawImage(prerenderedStuff, 0, 0, null);
263 }
264
265 if (showNiceTexts) {
266 ExtensionManager.getExtension().paint(g, this);
267 paintNiceTexts(g2d);
268 } else {
269 paintPlainTexts(g2d);
270 }
271
272 }
273
274 public final void adjustForSize(int width, int height) {
275 prepareFonts(width, height);
276 //enablings depends on fonts
277 setEnablings(width, height, master.getVersion(), master.getInformationElement(), (Graphics2D) (master.getGraphics()));
278 prerenderedStuff = prerenderStill();
279 ExtensionManager.getExtension().adjustForSize(width, height);
280 }
281
282 private void setEnablings(int w, int h, String version, InformationElement ic, Graphics2D g2d) {
283 showLeaf = true;
284 if (w > 0 && h > 0) {
285 //leaf stretch much better to wide then to high
286 if (h / w > 2 || w / h > 6) {
287 showLeaf = false;
288 }
289 }
290 showInfo = true;
291 if (version != null && g2d != null && ic != null && ic.getHeader() != null && ic.getHeader().size() > 0) {
292 String s = ic.getHeader().get(0);
293 FontMetrics fm = g2d.getFontMetrics(plainTextsFont);
294 int versionLength = fm.stringWidth(version);
295 int firsDescLineLengthg = fm.stringWidth(s);
296 if (firsDescLineLengthg > w - versionLength - 10) {
297 showInfo = false;
298 }
299 }
300 if (Math.min(h, w) < ScreenFinder.getCurrentScreenSizeWithoutBounds().getHeight() / 10) {
301 showNiceTexts = false;
302 } else {
303 showNiceTexts = true;
304 }
305 }
306
307 public final void startAnimationThreads() {
308 Thread tt = getMovingTextThread();
309 tt.start();
310 Thread t = getWaterLevelThread();
311 t.start();
312 }
313
314 private void prepareFonts(int w, int h) {
315 master.setSplashHeight(h);
316 master.setSplashWidth(w);
317 Map<TextAttribute, Object> teaFontAttributes = new HashMap<TextAttribute, Object>();
318 teaFontAttributes.put(TextAttribute.SIZE, new Integer(scaleMin(84)));
319 teaFontAttributes.put(TextAttribute.WIDTH, new Double((0.95)));
320 teaFontAttributes.put(TextAttribute.FAMILY, "Serif");
321 teaFont = new Font(teaFontAttributes);
322 Map<TextAttribute, Object> icedFontAttributes = new HashMap<TextAttribute, Object>();
323 icedFontAttributes.put(TextAttribute.SIZE, new Integer(scaleMin(82)));
324 icedFontAttributes.put(TextAttribute.WIDTH, new Double((0.80)));
325 icedFontAttributes.put(TextAttribute.FAMILY, "Serif");
326 icedFont = new Font(icedFontAttributes);
327 Map<TextAttribute, Object> webFontAttributes = new HashMap<TextAttribute, Object>();
328 webFontAttributes.put(TextAttribute.SIZE, new Integer(scaleMin(41)));
329 webFontAttributes.put(TextAttribute.WIDTH, new Double((2)));
330 webFontAttributes.put(TextAttribute.FAMILY, "Serif");
331 webFont = new Font(webFontAttributes);
332 Map<TextAttribute, Object> pluginFontAttributes = new HashMap<TextAttribute, Object>();
333 pluginFontAttributes.put(TextAttribute.SIZE, new Integer(scaleMin(32)));
334 pluginFontAttributes.put(TextAttribute.WEIGHT, new Double((5d)));
335 pluginFontAttributes.put(TextAttribute.WIDTH, new Double((0.9)));
336 pluginFontAttributes.put(TextAttribute.FAMILY, "Serif");
337 pluginFont = new Font(pluginFontAttributes);
338 Map<TextAttribute, Object> plainFontAttributes = new HashMap<TextAttribute, Object>();
339 plainFontAttributes.put(TextAttribute.SIZE, new Integer(12));
340 plainFontAttributes.put(TextAttribute.FAMILY, "Monospaced");
341 plainTextsFont = new Font(plainFontAttributes);
342 Map<TextAttribute, Object> alternativeTextFontAttributes = new HashMap<TextAttribute, Object>();
343 alternativeTextFontAttributes.put(TextAttribute.SIZE, Math.min(w, h) / 5);
344 alternativeTextFontAttributes.put(TextAttribute.WIDTH, new Double((0.7)));
345 alternativeTextFontAttributes.put(TextAttribute.FAMILY, "Monospaced");
346 alternativeTextFont = new Font(alternativeTextFontAttributes);
347
348 }
349
350 private void setColors() {
351
352 teaColor = TEA_LIVE_COLOR;
353 backgroundColor = BACKGROUND_LIVE_COLOR;
354 teaLeafsStalksColor = TEA_LEAFS_STALKS_LIVE_COLOR;
355 pluginColor = PLUGIN_LIVE_COLOR;
356 waterColor = WATER_LIVE_COLOR;
357 plainTextColor = PLAIN_TEXT_LIVE_COLOR;
358
359 }
360
361 protected BufferedImage prerenderStill() {
362 if (master.getSplashWidth() <= 0 || master.getSplashHeight() <= 0) {
363 return null;
364 }
365 BufferedImage bi = new BufferedImage(master.getSplashWidth(), master.getSplashHeight(), BufferedImage.TYPE_INT_ARGB);
366 paintStillTo(bi.createGraphics(), master.getInformationElement(), master.getVersion());
367 return bi;
368 }
369
370 protected void paintStillTo(Graphics2D g2d, InformationElement ic, String version) {
371 RenderingHints r = g2d.getRenderingHints();
372 drawBase(g2d, ic, version);
373 g2d.setRenderingHints(r);
374 }
375
376 protected void drawTextAroundCenter(Graphics2D g2d, double heightOffset, String msg) {
377
378 int y = (master.getSplashHeight() / 2) + (g2d.getFontMetrics().getHeight() / 2 + (int) (heightOffset * g2d.getFontMetrics().getHeight()));
379 int x = (master.getSplashWidth() / 2) - (g2d.getFontMetrics().stringWidth(msg) / 2);
380 g2d.drawString(msg, x, y);
381 }
382
383 private Thread getMovingTextThread() {
384 Thread tt = new Thread(new MovingTextRunner(this));
385 //tt.setDaemon(true);
386 return tt;
387 }
388
389 static String stripCommitFromVersion(String version) {
390 if (version.contains("pre+")) {
391 return version;
392 }
393 int i = version.indexOf("+");
394 if (i < 0) {
395 return version;
396 }
397 return version.substring(0, version.indexOf("+"));
398 }
399
400 private final class MovingTextRunner extends Observable implements Runnable {
401
402 private static final int MAX_ANIMATION_VALUE = 10000;
403 private static final int ANIMATION_RESTART_VALUE = 1;
404 private static final long MOOVING_TEXT_DELAY = 150;
405
406 public MovingTextRunner(Observer o) {
407 this.addObserver(o);
408 }
409
410 @Override
411 public void run() {
412 while (master.isAnimationRunning()) {
413 try {
414 animationsPosition += greyTextIncrment;
415 if (animationsPosition > MAX_ANIMATION_VALUE) {
416 animationsPosition = ANIMATION_RESTART_VALUE;
417 }
418 this.setChanged();
419 this.notifyObservers();
420 Thread.sleep(MOOVING_TEXT_DELAY);
421 } catch (Exception e) {
422 OutputController.getLogger().log(e);
423 }
424 }
425 }
426 };
427
428 private Thread getWaterLevelThread() {
429 Thread t = new Thread(new WaterLevelThread(this));
430 //t.setDaemon(true);
431 return t;
432 }
433
434 private final class WaterLevelThread extends Observable implements Runnable {
435
436 private static final int MAX_WATERLEVEL_VALUE = 120;
437 private static final int WATER_LEVEL_INCREMENT = 2;
438
439 private WaterLevelThread(BasePainter o) {
440 this.addObserver(o);
441 }
442
443 @Override
444 public void run() {
445 while (master.isAnimationRunning()) {
446 if (waterLevel > MAX_WATERLEVEL_VALUE) {
447 break;
448 }
449 try {
450 waterLevel += WATER_LEVEL_INCREMENT;
451 this.setChanged();
452 this.notifyObservers();
453 //it is risinfg slower and slower
454 Thread.sleep((waterLevel / 4) * 30);
455 } catch (Exception e) {
456 OutputController.getLogger().log(e);
457 }
458 }
459 }
460 };
461
462 private String getAlternativeProductName() {
463 if (SplashReason.JAVAWS.equals(master.getSplashReason())) {
464 return alternativeWeb;
465 } else if (SplashReason.APPLET.equals(master.getSplashReason())) {
466 return alternativeWeb + alternativePlugin;
467 } else {
468 return "....";
469 }
470 }
471
472 protected FontMetrics drawBase(Graphics2D g2d, InformationElement ic, String version) {
473 g2d.setColor(backgroundColor);
474 g2d.fillRect(0, 0, master.getSplashWidth() + 5, master.getSplashHeight() + 5);
475 if (showNiceTexts) {
476 //g2d.drawImage(tmpBackround, 0, 0, null);
477 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
478 g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
479 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
480 g2d.setFont(teaFont);
481 g2d.setColor(teaColor);
482 g2d.drawString(tea, scaleX(42) + g2d.getFontMetrics(icedFont).stringWidth(ICED), scaleY(278));
483 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
484 if (showLeaf) {
485 g2d.fillPolygon(SplinesDefs.getMainLeaf(getRatioX(), getRatioY()));
486 }
487 g2d.setColor(teaLeafsStalksColor);
488 if (showLeaf) {
489 g2d.fillPolygon(SplinesDefs.getSecondLeaf(getRatioX(), getRatioY()));
490 }
491 g2d.setFont(pluginFont);
492 g2d.setColor(pluginColor);
493 if (SplashReason.APPLET.equals(master.getSplashReason())) {
494 if (showLeaf) {
495 g2d.drawString(plugin, scaleX(420), scaleY(145));
496 } else {
497 FontMetrics wfm = g2d.getFontMetrics(webFont);
498 g2d.drawString(plugin, wfm.stringWidth(web) + scaleX(WEB_LEFT_ALIGMENT) + 10, scaleY(WEB_TOP_ALIGMENT));
499 }
500 }
501 g2d.setFont(plainTextsFont);
502 g2d.setColor(plainTextColor);
503 FontMetrics fm = g2d.getFontMetrics();
504 if (ic != null) {
505 InfoItem des = ic.getBestMatchingDescriptionForSplash();
506 List<String> head = ic.getHeader();
507 if (head != null && showInfo) {
508 for (int i = 0; i < head.size(); i++) {
509 String string = head.get(i);
510 g2d.drawString(string, 5, (i + 1) * fm.getHeight());
511 }
512 }
513 if (des != null && des.getValue() != null) {
514 g2d.drawString(des.getValue(), 5, master.getSplashHeight() - fm.getHeight());
515 }
516 }
517 }
518 g2d.setFont(plainTextsFont);
519 g2d.setColor(plainTextColor);
520 FontMetrics fm = g2d.getFontMetrics();
521 if (version != null) {
522 String aboutPrefix = Translator.R("AboutDialogueTabAbout") + ": ";
523 int aboutPrefixWidth = fm.stringWidth(aboutPrefix);
524 String niceVersion = stripCommitFromVersion(version);
525 int y = master.getSplashWidth() - fm.stringWidth(niceVersion + " ");
526 if (y < 0) {
527 y = 0;
528 }
529 if (y > aboutPrefixWidth) {
530 niceVersion = aboutPrefix + niceVersion;
531 y -= aboutPrefixWidth;
532 }
533 aboutOfset = new Point(y, fm.getHeight());
534 Stroke backup = g2d.getStroke();
535 g2d.setStroke(dashed);
536 g2d.drawRect(aboutOfset.x - 1, 1, master.getSplashWidth() - aboutOfset.x - 1, aboutOfset.y + 1);
537 g2d.setStroke(backup);
538 g2d.drawString(niceVersion, y, fm.getHeight());
539 }
540 return fm;
541 }
542
543 public int getWaterLevel() {
544 return waterLevel;
545 }
546
547 public void setWaterLevel(int level) {
548 this.waterLevel = level;
549 }
550
551 public int getAnimationsPosition() {
552 return animationsPosition;
553 }
554
555 public void clearCachedWaterTextImage() {
556 oldTwl = null;
557 }
558
559 @Override
560 public void update(Observable o, Object arg) {
561 SwingUtils.invokeAndWait(new Runnable() {
562
563 @Override
564 public void run() {
565 if (master.isAnimationRunning()) {
566 ExtensionManager.getExtension().animate();
567 master.repaint();
568 }
569 }
570 });
571 }
572
573 public BasicComponentSplashScreen getMaster() {
574 return master;
575 }
576
577 public Point getAboutOfset() {
578 return aboutOfset;
579 }
580
581 public Color getWaterColor() {
582 return waterColor;
583 }
584
585 public Color getBackgroundColor() {
586 return backgroundColor;
587 }
588
589 }