Coverage Report - org.jtheque.core.managers.view.impl.components.LayerTabbedPane
 
Classes in this File Line Coverage Branch Coverage Complexity
LayerTabbedPane
0 %
0/20
0 %
0/4
2.062
LayerTabbedPane$1
N/A
N/A
2.062
LayerTabbedPane$AnimationLayerUI
0 %
0/19
0 %
0/14
2.062
LayerTabbedPane$TabbedAnimatingChangeListener
0 %
0/22
0 %
0/8
2.062
LayerTabbedPane$TabbedAnimatingChangeListener$1
0 %
0/7
0 %
0/2
2.062
 
 1  
 package org.jtheque.core.managers.view.impl.components;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque 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 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jdesktop.jxlayer.JXLayer;
 20  
 import org.jdesktop.jxlayer.plaf.BufferedLayerUI;
 21  
 import org.jtheque.core.managers.Managers;
 22  
 import org.jtheque.core.managers.language.ILanguageManager;
 23  
 import org.jtheque.core.managers.view.ViewComponent;
 24  
 
 25  
 import javax.swing.JComponent;
 26  
 import javax.swing.JTabbedPane;
 27  
 import javax.swing.Timer;
 28  
 import javax.swing.event.ChangeEvent;
 29  
 import javax.swing.event.ChangeListener;
 30  
 import java.awt.Component;
 31  
 import java.awt.Graphics;
 32  
 import java.awt.Graphics2D;
 33  
 import java.awt.event.ActionEvent;
 34  
 import java.awt.event.ActionListener;
 35  
 import java.awt.image.BufferedImage;
 36  
 import java.util.ArrayList;
 37  
 import java.util.List;
 38  
 
 39  
 /**
 40  
  * A tabbed pane with different layer.
 41  
  *
 42  
  * @author Baptiste Wicht
 43  
  */
 44  0
 public class LayerTabbedPane extends JTabbedPane implements ViewComponent {
 45  
     private final List<JXLayer<JComponent>> components;
 46  
 
 47  
     /**
 48  
      * Construct a new LayerTabbedPane.
 49  
      */
 50  
     public LayerTabbedPane() {
 51  0
         super();
 52  
 
 53  0
         components = new ArrayList<JXLayer<JComponent>>(5);
 54  
 
 55  0
         addChangeListener(new TabbedAnimatingChangeListener());
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Add an internationalized tab.
 60  
      *
 61  
      * @param key       The internationalization key.
 62  
      * @param component The component to add.
 63  
      */
 64  
     public final void addInternationalizedTab(String key, JComponent component) {
 65  0
         addLayeredTab(Managers.getManager(ILanguageManager.class).getMessage(key), component);
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Add a tab.
 70  
      *
 71  
      * @param title     The title of the tab.
 72  
      * @param component The tab component.
 73  
      */
 74  
     public final void addLayeredTab(String title, JComponent component) {
 75  0
         JXLayer<JComponent> layerComponent = new JXLayer<JComponent>(component);
 76  
 
 77  0
         components.add(layerComponent);
 78  0
         addTab(title, layerComponent);
 79  0
     }
 80  
 
 81  
     @Override
 82  
     public final JComponent getSelectedComponent() {
 83  0
         if (getSelectedIndex() != -1) {
 84  0
             return components.get(getSelectedIndex()).getView();
 85  
         }
 86  
 
 87  0
         return null;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Return the selected layer.
 92  
      *
 93  
      * @return the selected layer or null if there is no selected layer.
 94  
      */
 95  
     final JXLayer<JComponent> getSelectedLayer() {
 96  0
         JXLayer<JComponent> layer = null;
 97  
 
 98  0
         if (getSelectedIndex() != -1) {
 99  0
             layer = components.get(getSelectedIndex());
 100  
         }
 101  
 
 102  0
         return layer;
 103  
     }
 104  
 
 105  
     /**
 106  
      * Return the layer at a certain index.
 107  
      *
 108  
      * @param index The index for which we search the layer.
 109  
      * @return the corresponding layer.
 110  
      */
 111  
     final JXLayer<JComponent> getLayerAt(int index) {
 112  0
         return components.get(index);
 113  
     }
 114  
 
 115  
     /**
 116  
      * A listener to animate the tabbed pane.
 117  
      *
 118  
      * @author Baptiste Wicht
 119  
      */
 120  0
     private static final class TabbedAnimatingChangeListener implements ChangeListener {
 121  
         private int index;
 122  
         private final Timer timer;
 123  
         private final AnimationLayerUI layerUI;
 124  
         private float delta;
 125  
 
 126  
         private static final int ANIMATION_DELAY = 50;
 127  
 
 128  
         /**
 129  
          * Construct a new <code>TabbedAnimatingChangeListener</code>.
 130  
          */
 131  
         private TabbedAnimatingChangeListener() {
 132  0
             super();
 133  
 
 134  0
             delta = .1f;
 135  0
             layerUI = new AnimationLayerUI();
 136  
 
 137  0
             timer = new Timer(ANIMATION_DELAY, new ActionListener() {
 138  
                 @Override
 139  
                 public void actionPerformed(ActionEvent e) {
 140  
 
 141  0
                     if (layerUI.getAlpha() <= delta) {
 142  0
                         layerUI.setAlpha(0);
 143  0
                         timer.stop();
 144  0
                         return;
 145  
                     }
 146  
 
 147  0
                     layerUI.setAlpha(layerUI.getAlpha() - delta);
 148  0
                 }
 149  
             });
 150  0
         }
 151  
 
 152  
         /**
 153  
          * Return the current delta for alpha.
 154  
          *
 155  
          * @return The delta for alpha value.
 156  
          */
 157  
         public float getDelta() {
 158  0
             return delta;
 159  
         }
 160  
 
 161  
         /**
 162  
          * Set the delta for alpha.
 163  
          *
 164  
          * @param delta The delta.
 165  
          */
 166  
         public void setDelta(float delta) {
 167  0
             if (delta <= 0 || delta > 1) {
 168  0
                 throw new IllegalArgumentException();
 169  
             }
 170  0
             this.delta = delta;
 171  0
         }
 172  
 
 173  
         @Override
 174  
         public void stateChanged(ChangeEvent e) {
 175  0
             LayerTabbedPane pane = (LayerTabbedPane) e.getSource();
 176  0
             JXLayer<JComponent> layer = pane.getSelectedLayer();
 177  0
             JXLayer<JComponent> oldLayer = pane.getLayerAt(index);
 178  
 
 179  0
             if (oldLayer != null && layer != null) {
 180  0
                 layerUI.setAlpha(1 - layerUI.getAlpha());
 181  
 
 182  0
                 layerUI.setComponent(oldLayer);
 183  
 
 184  0
                 oldLayer.setUI(layer.getUI());
 185  0
                 layer.setUI(layerUI);
 186  
 
 187  
                 //painter.update();            
 188  0
                 timer.start();
 189  0
                 index = pane.getSelectedIndex();
 190  
             }
 191  0
         }
 192  
     }
 193  
 
 194  
     @Override
 195  
     public Object getImpl() {
 196  0
         return this;
 197  
     }
 198  
 
 199  
     /**
 200  
      * A layer UI for the animation.
 201  
      *
 202  
      * @author Baptiste Wicht
 203  
      */
 204  0
     private static final class AnimationLayerUI extends BufferedLayerUI<JComponent> {
 205  
         private BufferedImage componentImage;
 206  
 
 207  
         /**
 208  
          * Construct a new <code>AnimationLayerUI</code>.
 209  
          */
 210  0
         private AnimationLayerUI() {
 211  0
             setAlpha(0);
 212  0
             setIncrementalUpdate(false);
 213  0
         }
 214  
 
 215  
         /**
 216  
          * Set the component to paint.
 217  
          *
 218  
          * @param component The component to paint.
 219  
          */
 220  
         public void setComponent(Component component) {
 221  0
             if (component == null
 222  
                     || component.getWidth() <= 0 || component.getHeight() <= 0) {
 223  0
                 componentImage = null;
 224  
             } else {
 225  0
                 if (componentImage == null
 226  
                         || componentImage.getWidth() != component.getWidth()
 227  
                         || componentImage.getHeight() != component.getHeight()) {
 228  0
                     componentImage = createBuffer(component.getWidth(), component.getHeight());
 229  
                 }
 230  0
                 Graphics g = componentImage.getGraphics();
 231  0
                 component.paint(g);
 232  0
                 g.dispose();
 233  
             }
 234  0
         }
 235  
 
 236  
         @Override
 237  
         public void paint(Graphics g, JComponent c) {
 238  
             // always paint the layer
 239  0
             c.paint(g);
 240  0
             super.paint(g, c);
 241  0
         }
 242  
 
 243  
         @Override
 244  
         protected void paintLayer(Graphics2D graphics2D, JXLayer<? extends JComponent> jxLayer) {
 245  0
             if (componentImage != null) {
 246  
                 // paint the old layer with diminishing alpha
 247  0
                 graphics2D.drawImage(componentImage, 0, 0, null);
 248  
             }
 249  0
         }
 250  
     }
 251  
 }