Coverage Report - org.jtheque.core.managers.view.impl.components.JThequeStateBar
 
Classes in this File Line Coverage Branch Coverage Complexity
JThequeStateBar
0 %
0/55
0 %
0/20
1.909
JThequeStateBar$1
N/A
N/A
1.909
JThequeStateBar$LeftToRightComparator
0 %
0/2
N/A
1.909
 
 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.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.view.able.IViewManager;
 21  
 import org.jtheque.core.managers.view.able.components.StateBarComponent;
 22  
 import org.jtheque.core.managers.view.listeners.StateBarEvent;
 23  
 import org.jtheque.core.managers.view.listeners.StateBarListener;
 24  
 import org.jtheque.utils.ui.GridBagUtils;
 25  
 
 26  
 import javax.swing.Box;
 27  
 import javax.swing.JPanel;
 28  
 import javax.swing.JSeparator;
 29  
 import java.awt.Color;
 30  
 import java.awt.Component;
 31  
 import java.awt.Dimension;
 32  
 import java.awt.GridBagLayout;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Collections;
 35  
 import java.util.Comparator;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * A State Bar for JTheque.
 40  
  *
 41  
  * @author Baptiste Wicht
 42  
  */
 43  
 public final class JThequeStateBar extends JPanel implements StateBarListener {
 44  
     private int currentColumn;
 45  
 
 46  0
     private final GridBagUtils gbc = new GridBagUtils();
 47  
 
 48  
     private boolean leftFilled;
 49  
     private boolean centerFilled;
 50  
 
 51  
     /**
 52  
      * Construct a new JThequeStateBar.
 53  
      */
 54  
     public JThequeStateBar() {
 55  0
         super();
 56  
 
 57  0
         setLayout(new GridBagLayout());
 58  0
         setBackground(new Color(133, 144, 165));
 59  
 
 60  0
         build();
 61  0
     }
 62  
 
 63  
     /**
 64  
      * Build the state bar.
 65  
      */
 66  
     private void build() {
 67  0
         List<StateBarComponent> components = new ArrayList<StateBarComponent>(Managers.getManager(IViewManager.class).getStateBarComponents());
 68  
 
 69  0
         if (components.isEmpty()) {
 70  0
             setVisible(false);
 71  
         } else {
 72  0
             setVisible(true);
 73  
 
 74  0
             Collections.sort(components, new LeftToRightComparator());
 75  
 
 76  0
             currentColumn = 0;
 77  
 
 78  0
             addLeftComponents(components);
 79  0
             addCenterComponents(components);
 80  0
             addRightComponents(components);
 81  
         }
 82  
 
 83  0
         Managers.getManager(IViewManager.class).addStateBarListener(this);
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Add the components in the left.
 88  
      *
 89  
      * @param components The state bar components.
 90  
      */
 91  
     private void addLeftComponents(Iterable<StateBarComponent> components) {
 92  0
         for (StateBarComponent component : components) {
 93  0
             if (component.isLeft()) {
 94  0
                 addComponent(component.getComponent());
 95  0
                 leftFilled = true;
 96  
             }
 97  
         }
 98  
 
 99  0
         addGlue();
 100  0
     }
 101  
 
 102  
     /**
 103  
      * Add the components in the center.
 104  
      *
 105  
      * @param components The state bar components.
 106  
      */
 107  
     private void addCenterComponents(Iterable<StateBarComponent> components) {
 108  0
         addGlue();
 109  
 
 110  0
         if (leftFilled) {
 111  0
             addSeparator();
 112  
         }
 113  
 
 114  0
         for (StateBarComponent component : components) {
 115  0
             if (component.isCenter()) {
 116  0
                 addComponent(component.getComponent());
 117  0
                 centerFilled = true;
 118  
             }
 119  
         }
 120  
 
 121  0
         addGlue();
 122  0
     }
 123  
 
 124  
     /**
 125  
      * Add the components in the right.
 126  
      *
 127  
      * @param components The state bar components.
 128  
      */
 129  
     private void addRightComponents(Iterable<StateBarComponent> components) {
 130  0
         addGlue();
 131  
 
 132  0
         if (centerFilled) {
 133  0
             addSeparator();
 134  
         }
 135  
 
 136  0
         for (StateBarComponent component : components) {
 137  0
             if (component.isRight()) {
 138  0
                 addComponent(component.getComponent());
 139  
             }
 140  
         }
 141  0
     }
 142  
 
 143  
     /**
 144  
      * Add a glue.
 145  
      */
 146  
     private void addGlue() {
 147  0
         add(Box.createHorizontalGlue(), gbc.gbcSet(
 148  
                 currentColumn++, 1, GridBagUtils.HORIZONTAL, GridBagUtils.BASELINE_LEADING, 1.0, 1.0));
 149  0
     }
 150  
 
 151  
     /**
 152  
      * Add a separated component.
 153  
      *
 154  
      * @param component The component to add.
 155  
      */
 156  
     private void addComponent(Component component) {
 157  0
         add(component, gbc.gbcSet(currentColumn++, 1));
 158  0
     }
 159  
 
 160  
     /**
 161  
      * Add a separator.
 162  
      */
 163  
     private void addSeparator() {
 164  0
         Component separator = new JSeparator(JSeparator.VERTICAL);
 165  0
         separator.setPreferredSize(new Dimension(1, 10));
 166  
 
 167  0
         add(separator, gbc.gbcSet(currentColumn++, 1));
 168  0
     }
 169  
 
 170  
     @Override
 171  
     public void componentAdded() {
 172  0
         build();
 173  
 
 174  0
         Managers.getManager(IViewManager.class).refresh(this);
 175  0
     }
 176  
 
 177  
     @Override
 178  
     public void componentRemoved(StateBarEvent event) {
 179  0
         remove(event.getComponent().getComponent());
 180  
 
 181  0
         if (Managers.getManager(IViewManager.class).getStateBarComponents().isEmpty()) {
 182  0
             setVisible(false);
 183  
         }
 184  
 
 185  0
         Managers.getManager(IViewManager.class).refresh(this);
 186  0
     }
 187  
 
 188  
     /**
 189  
      * A comparator to sort the state bar component from left to right.
 190  
      *
 191  
      * @author Baptiste Wicht
 192  
      */
 193  0
     private static final class LeftToRightComparator implements Comparator<StateBarComponent> {
 194  
         @Override
 195  
         public int compare(StateBarComponent component, StateBarComponent other) {
 196  0
             return component.getPosition().compareTo(other.getPosition());
 197  
         }
 198  
     }
 199  
 }