Coverage Report - org.jtheque.core.managers.view.impl.components.filthy.java2d.SplashScreenPane
 
Classes in this File Line Coverage Branch Coverage Complexity
SplashScreenPane
0 %
0/70
0 %
0/16
1.615
 
 1  
 package org.jtheque.core.managers.view.impl.components.filthy.java2d;
 2  
 
 3  
 import org.jdesktop.animation.timing.Animator;
 4  
 import org.jdesktop.animation.timing.triggers.TimingTrigger;
 5  
 import org.jdesktop.animation.timing.triggers.TimingTriggerEvent;
 6  
 import org.jdesktop.jxlayer.JXLayer;
 7  
 import org.jdesktop.jxlayer.plaf.BufferedLayerUI;
 8  
 import org.jdesktop.jxlayer.plaf.LayerUI;
 9  
 import org.jtheque.core.managers.Managers;
 10  
 import org.jtheque.core.managers.core.Core;
 11  
 import org.jtheque.core.managers.core.application.Application;
 12  
 import org.jtheque.core.managers.view.able.components.ISplashView;
 13  
 import org.jtheque.core.utils.ui.AnimationUtils;
 14  
 import org.jtheque.utils.StringUtils;
 15  
 import org.jtheque.utils.ui.ImageUtils;
 16  
 import org.jtheque.utils.ui.PaintUtils;
 17  
 
 18  
 import javax.swing.JComponent;
 19  
 import java.awt.Color;
 20  
 import java.awt.Component;
 21  
 import java.awt.Font;
 22  
 import java.awt.Graphics;
 23  
 import java.awt.Graphics2D;
 24  
 import java.awt.Point;
 25  
 import java.awt.image.BufferedImage;
 26  
 
 27  
 /*
 28  
  * This file is part of JTheque.
 29  
  *
 30  
  * JTheque is free software: you can redistribute it and/or modify
 31  
  * it under the terms of the GNU General Public License as published by
 32  
  * the Free Software Foundation, either version 3 of the License.
 33  
  *
 34  
  * JTheque is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * You should have received a copy of the GNU General Public License
 40  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 41  
  */
 42  
 
 43  
 /**
 44  
  * @author Baptiste Wicht
 45  
  */
 46  
 public final class SplashScreenPane extends BufferedLayerUI<JComponent> implements ISplashView {
 47  
     private boolean inited;
 48  
 
 49  
     private BufferedImage bImage;
 50  
 
 51  
     private Font fontName;
 52  
     private Font fontLoading;
 53  
 
 54  
     private final Point pImage;
 55  
     private final Point pTextName;
 56  
     private final Point pTextLoading;
 57  
 
 58  0
     private final String[] loadings = {"Loading ", "Loading .", "Loading ..", "Loading ...", "Loading ..."};
 59  
     private int current;
 60  
 
 61  
     private static final int LOADING_FONT_SIZE = 20;
 62  
     private static final int TITLE_FONT_SIZE = 24;
 63  
 
 64  
     private static final int ANIMATION_DURATION = 2000;
 65  
 
 66  
     private Animator waitingAnimation;
 67  
     private Animator fadeAnimation;
 68  
 
 69  
     /**
 70  
      * Construct a new SplashScreenPane.
 71  
      */
 72  
     public SplashScreenPane() {
 73  0
         super();
 74  
 
 75  0
         pImage = new Point();
 76  0
         pTextName = new Point();
 77  0
         pTextLoading = new Point();
 78  
 
 79  0
         init();
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Init the view.
 84  
      */
 85  
     public void init() {
 86  0
         Application application = Managers.getCore().getApplication();
 87  
 
 88  0
         if (!StringUtils.isEmpty(application.getLogo())) {
 89  0
             bImage = ImageUtils.openCompatibleImageFromFileSystem(application.getLogo() + '.' + application.getLogoType().getExtension());
 90  
         }
 91  0
     }
 92  
 
 93  
     @Override
 94  
     public void paintLayer(Graphics2D g2, JXLayer<? extends JComponent> layer) {
 95  0
         super.paintLayer(g2, layer);
 96  
 
 97  0
         PaintUtils.initHints(g2);
 98  
 
 99  0
         g2.setColor(Color.BLACK);
 100  
 
 101  0
         g2.fillRect(0, 0, layer.getWidth(), layer.getHeight());
 102  
 
 103  0
         if (!inited) {
 104  0
             initFonts(g2);
 105  
 
 106  0
             computeSizes(layer);
 107  
 
 108  0
             inited = true;
 109  
         }
 110  
 
 111  0
         Color color = new Color(255, 255, 255, (int) (getAlpha() * 255));
 112  
 
 113  0
         paintName(g2, color);
 114  0
         paintImage(g2);
 115  0
         paintLoading(g2, color);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Compute the sizes.
 120  
      *
 121  
      * @param layer The component to compute the sizes.
 122  
      */
 123  
     private void computeSizes(Component layer) {
 124  0
         int nameWidth = layer.getFontMetrics(fontName).stringWidth(Core.getInstance().getApplication().getName());
 125  0
         int loadingWidth = layer.getFontMetrics(fontLoading).stringWidth("Loading ...");
 126  
 
 127  0
         int height = layer.getFontMetrics(fontName).getHeight() + 10 + bImage.getHeight() + layer.getFontMetrics(fontLoading).getHeight();
 128  
 
 129  0
         pTextName.y = (int) ((layer.getSize().getHeight() - height) / 2);
 130  0
         pImage.y = pTextName.y + layer.getFontMetrics(fontName).getHeight() + 10;
 131  0
         pTextLoading.y = pImage.y + bImage.getHeight() + 20;
 132  
 
 133  0
         pTextName.x = (int) ((layer.getSize().getWidth() - nameWidth) / 2);
 134  0
         pImage.x = (int) ((layer.getSize().getWidth() - bImage.getWidth()) / 2);
 135  0
         pTextLoading.x = (int) ((layer.getSize().getWidth() - loadingWidth) / 2);
 136  0
     }
 137  
 
 138  
     /**
 139  
      * Init the fonts.
 140  
      *
 141  
      * @param g2 The Graphics2D element.
 142  
      */
 143  
     private void initFonts(Graphics g2) {
 144  0
         fontName = g2.getFont().deriveFont(Font.BOLD, TITLE_FONT_SIZE);
 145  0
         fontLoading = g2.getFont().deriveFont(Font.BOLD, LOADING_FONT_SIZE);
 146  0
     }
 147  
 
 148  
     /**
 149  
      * Paint the loading text.
 150  
      *
 151  
      * @param g2    The Graphics2D element.
 152  
      * @param color The color of the loading text.
 153  
      */
 154  
     private void paintLoading(Graphics g2, Color color) {
 155  0
         g2.translate(pTextLoading.x, pTextLoading.y);
 156  
 
 157  0
         PaintUtils.drawString(g2, loadings[current], 0, 0, fontLoading, color);
 158  0
     }
 159  
 
 160  
     /**
 161  
      * Paint the image.
 162  
      *
 163  
      * @param g2 The Graphics2D element.
 164  
      */
 165  
     private void paintImage(Graphics2D g2) {
 166  0
         g2.translate(pImage.x, pImage.y);
 167  
 
 168  0
         PaintUtils.drawAlphaImage(g2, bImage, 0, 0, getAlpha());
 169  
 
 170  0
         g2.translate(-pImage.x, -pImage.y);
 171  0
     }
 172  
 
 173  
     /**
 174  
      * Paint the name of the application.
 175  
      *
 176  
      * @param g2    The Graphics2D element.
 177  
      * @param color The color of the application name.
 178  
      */
 179  
     private void paintName(Graphics g2, Color color) {
 180  0
         g2.translate(pTextName.x, pTextName.y);
 181  
 
 182  0
         PaintUtils.drawString(g2, Core.getInstance().getApplication().getName(), 0, 0, fontName, color);
 183  
 
 184  0
         g2.translate(-pTextName.x, -pTextName.y);
 185  0
     }
 186  
 
 187  
     /**
 188  
      * Set the current loading index.
 189  
      *
 190  
      * @param current The current loading index.
 191  
      */
 192  
     public void setCurrent(int current) {
 193  0
         int oldCurrent = this.current;
 194  
 
 195  0
         this.current = current;
 196  
 
 197  0
         if (current != oldCurrent) {
 198  0
             setDirty(true);
 199  
         }
 200  0
     }
 201  
 
 202  
     /**
 203  
      * Return the current loading index.
 204  
      *
 205  
      * @return The current loading index.
 206  
      */
 207  
     public int getCurrent() {
 208  0
         return current;
 209  
     }
 210  
 
 211  
     @Override
 212  
     public void appearsAndAnimate() {
 213  0
         if (fadeAnimation != null && fadeAnimation.isRunning()) {
 214  0
             fadeAnimation.stop();
 215  
         }
 216  
 
 217  0
         if (waitingAnimation == null) {
 218  0
             waitingAnimation = AnimationUtils.createLoopEffect(this, ANIMATION_DURATION, "current", 4);
 219  0
             waitingAnimation.setResolution(ANIMATION_DURATION / 4);
 220  
         }
 221  
 
 222  0
         fadeAnimation = AnimationUtils.createFadeInAnimator(this);
 223  
 
 224  0
         TimingTrigger.addTrigger(fadeAnimation, waitingAnimation, TimingTriggerEvent.STOP);
 225  
 
 226  0
         fadeAnimation.start();
 227  0
     }
 228  
 
 229  
     @Override
 230  
     public void disappear() {
 231  0
         if (fadeAnimation != null && fadeAnimation.isRunning()) {
 232  0
             fadeAnimation.stop();
 233  
         }
 234  
 
 235  0
         waitingAnimation.stop();
 236  
 
 237  0
         fadeAnimation = AnimationUtils.createFadeOutAnimator(this);
 238  0
         fadeAnimation.start();
 239  0
     }
 240  
 
 241  
     @Override
 242  
     public LayerUI<JComponent> getImpl() {
 243  0
         return this;
 244  
     }
 245  
 }