Coverage Report - org.jtheque.core.managers.view.impl.WindowsConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
WindowsConfiguration
0 %
0/52
0 %
0/24
2.5
 
 1  
 package org.jtheque.core.managers.view.impl;
 2  
 
 3  
 import org.jtheque.core.managers.Managers;
 4  
 import org.jtheque.core.managers.state.AbstractState;
 5  
 import org.jtheque.core.managers.state.NodeState;
 6  
 import org.jtheque.core.managers.state.NodeStateAttribute;
 7  
 import org.jtheque.core.managers.view.able.IView;
 8  
 import org.jtheque.core.managers.view.able.IViewManager;
 9  
 
 10  
 import java.util.ArrayList;
 11  
 import java.util.Collection;
 12  
 import java.util.HashMap;
 13  
 import java.util.Map;
 14  
 import java.util.Map.Entry;
 15  
 
 16  
 /*
 17  
  * This file is part of JTheque.
 18  
  *
 19  
  * JTheque is free software: you can redistribute it and/or modify
 20  
  * it under the terms of the GNU General Public License as published by
 21  
  * the Free Software Foundation, either version 3 of the License.
 22  
  *
 23  
  * JTheque is distributed in the hope that it will be useful,
 24  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  
  * GNU General Public License for more details.
 27  
  *
 28  
  * You should have received a copy of the GNU General Public License
 29  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 30  
  */
 31  
 
 32  
 /**
 33  
  * A state for persist different views configuration.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  0
 public final class WindowsConfiguration extends AbstractState {
 38  0
     private final Map<String, WindowConfiguration> configurations = new HashMap<String, WindowConfiguration>(10);
 39  
 
 40  
     @Override
 41  
     public boolean isDelegated() {
 42  0
         return true;
 43  
     }
 44  
 
 45  
     @Override
 46  
     public void delegateLoad(Collection<NodeState> nodes) {
 47  0
         for (NodeState node : nodes) {
 48  0
             if ("window".equals(node.getName())) {
 49  0
                 WindowConfiguration configuration = new WindowConfiguration();
 50  
 
 51  0
                 for (NodeState child : node.getChildrens()) {
 52  0
                     applyValueFromChild(configuration, child);
 53  
                 }
 54  
 
 55  0
                 add(node.getAttributeValue("name"), configuration);
 56  0
             }
 57  
         }
 58  0
     }
 59  
 
 60  
     /**
 61  
      * Apply the value from child.
 62  
      *
 63  
      * @param configuration The window configuration.
 64  
      * @param child         The child.
 65  
      */
 66  
     private static void applyValueFromChild(WindowConfiguration configuration, NodeState child) {
 67  0
         if ("width".equals(child.getName())) {
 68  0
             configuration.setWidth(Integer.parseInt(child.getText()));
 69  0
         } else if ("height".equals(child.getName())) {
 70  0
             configuration.setHeight(Integer.parseInt(child.getText()));
 71  0
         } else if ("posX".equals(child.getName())) {
 72  0
             configuration.setPositionX(Integer.parseInt(child.getText()));
 73  0
         } else if ("posY".equals(child.getName())) {
 74  0
             configuration.setPositionY(Integer.parseInt(child.getText()));
 75  
         }
 76  0
     }
 77  
 
 78  
     @Override
 79  
     public Collection<NodeState> delegateSave() {
 80  0
         Collection<NodeState> states = new ArrayList<NodeState>(10);
 81  
 
 82  0
         for (Entry<String, WindowConfiguration> configuration : configurations.entrySet()) {
 83  0
             NodeState state = new NodeState("window");
 84  
 
 85  0
             state.getAttributes().add(new NodeStateAttribute("name", configuration.getKey()));
 86  0
             state.getChildrens().add(new NodeState("width", Integer.toString(configuration.getValue().getWidth())));
 87  0
             state.getChildrens().add(new NodeState("height", Integer.toString(configuration.getValue().getHeight())));
 88  0
             state.getChildrens().add(new NodeState("posX", Integer.toString(configuration.getValue().getPositionX())));
 89  0
             state.getChildrens().add(new NodeState("posY", Integer.toString(configuration.getValue().getPositionY())));
 90  
 
 91  0
             states.add(state);
 92  0
         }
 93  
 
 94  0
         return states;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Add a window configuration for a view.
 99  
      *
 100  
      * @param name          The name of the view.
 101  
      * @param configuration The configuration to add.
 102  
      */
 103  
     private void add(String name, WindowConfiguration configuration) {
 104  0
         configurations.put(name, configuration);
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Update the configuration with the view.
 109  
      *
 110  
      * @param name The name of the view.
 111  
      * @param view The view.
 112  
      */
 113  
     public void update(String name, IView view) {
 114  0
         if (Managers.getCore().getConfiguration().retainSizeAndPositionOfWindow()) {
 115  0
             WindowConfiguration configuration = get(name);
 116  
 
 117  0
             if (configuration != null) {
 118  0
                 Managers.getManager(IViewManager.class).getViewDelegate().fill(configuration, view);
 119  
             }
 120  
         }
 121  0
     }
 122  
 
 123  
     /**
 124  
      * Return the window configuration for a view.
 125  
      *
 126  
      * @param name The name of the view.
 127  
      * @return The window configuration for the view.
 128  
      */
 129  
     private WindowConfiguration get(String name) {
 130  0
         return configurations.get(name);
 131  
     }
 132  
 
 133  
     /**
 134  
      * Configure the view.
 135  
      *
 136  
      * @param name          The name of the view.
 137  
      * @param view          The view.
 138  
      * @param defaultWidth  The default width of the view.
 139  
      * @param defaultHeight The default height of the view.
 140  
      */
 141  
     public void configure(String name, IView view, int defaultWidth, int defaultHeight) {
 142  0
         if (Managers.getCore().getConfiguration().retainSizeAndPositionOfWindow()) {
 143  0
             WindowConfiguration configuration = get(name);
 144  
 
 145  0
             if (configuration == null) {
 146  0
                 configuration = new WindowConfiguration();
 147  
 
 148  0
                 configuration.setWidth(defaultWidth);
 149  0
                 configuration.setHeight(defaultHeight);
 150  0
                 configuration.setPositionX(-1);
 151  0
                 configuration.setPositionY(-1);
 152  
 
 153  0
                 add(name, configuration);
 154  
             }
 155  
 
 156  0
             Managers.getManager(IViewManager.class).getViewDelegate().configure(configuration, view);
 157  0
         } else {
 158  0
             Managers.getManager(IViewManager.class).getViewDelegate().setSize(view, defaultWidth, defaultHeight);
 159  
         }
 160  0
     }
 161  
 }