Coverage Report - org.jtheque.core.managers.view.ViewManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ViewManager
0 %
0/130
0 %
0/38
1.392
 
 1  
 package org.jtheque.core.managers.view;
 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.AbstractManager;
 20  
 import org.jtheque.core.managers.error.JThequeError;
 21  
 import org.jtheque.core.managers.view.able.IAboutView;
 22  
 import org.jtheque.core.managers.view.able.ICollectionView;
 23  
 import org.jtheque.core.managers.view.able.ISplashManager;
 24  
 import org.jtheque.core.managers.view.able.IView;
 25  
 import org.jtheque.core.managers.view.able.IViewManager;
 26  
 import org.jtheque.core.managers.view.able.ViewDefaults;
 27  
 import org.jtheque.core.managers.view.able.ViewDelegate;
 28  
 import org.jtheque.core.managers.view.able.components.StateBarComponent;
 29  
 import org.jtheque.core.managers.view.able.components.TabComponent;
 30  
 import org.jtheque.core.managers.view.edt.SimpleTask;
 31  
 import org.jtheque.core.managers.view.edt.Task;
 32  
 import org.jtheque.core.managers.view.impl.WindowsConfiguration;
 33  
 import org.jtheque.core.managers.view.impl.components.config.ConfigTabComponent;
 34  
 import org.jtheque.core.managers.view.listeners.ConfigTabEvent;
 35  
 import org.jtheque.core.managers.view.listeners.ConfigTabListener;
 36  
 import org.jtheque.core.managers.view.listeners.StateBarEvent;
 37  
 import org.jtheque.core.managers.view.listeners.StateBarListener;
 38  
 import org.jtheque.core.managers.view.listeners.TabEvent;
 39  
 import org.jtheque.core.managers.view.listeners.TabListener;
 40  
 import org.jtheque.utils.collections.CollectionUtils;
 41  
 import org.jtheque.utils.io.SimpleFilter;
 42  
 
 43  
 import javax.annotation.Resource;
 44  
 import java.util.ArrayList;
 45  
 import java.util.Collection;
 46  
 
 47  
 /**
 48  
  * A view manager implementation.
 49  
  *
 50  
  * @author Baptiste Wicht
 51  
  */
 52  0
 public final class ViewManager extends AbstractManager implements IViewManager {
 53  
     /* Components */
 54  0
     private final Collection<StateBarComponent> stateBarComponents = new ArrayList<StateBarComponent>(5);
 55  0
     private final Collection<TabComponent> tabComponents = new ArrayList<TabComponent>(5);
 56  
     private Collection<ConfigTabComponent> configPanels;
 57  
 
 58  
     private ViewComponent mainComponent;
 59  
 
 60  
     private WindowsConfiguration configuration;
 61  
 
 62  
     private ViewDelegate viewDelegate;
 63  
 
 64  
     private ViewDefaults viewDefaults;
 65  
 
 66  
     @Resource
 67  
     private Views windowManager;
 68  
 
 69  
     @Resource
 70  
     private ICollectionView collectionPane;
 71  
 
 72  
     @Resource
 73  
     private IAboutView aboutPane;
 74  
 
 75  
     @Override
 76  
     public void displayAboutView() {
 77  0
         viewDelegate.applyGlassPane(aboutPane.getImpl());
 78  0
         aboutPane.appear();
 79  0
     }
 80  
 
 81  
     @Override
 82  
     public void displayCollectionView() {
 83  0
         viewDelegate.applyGlassPane(collectionPane.getImpl());
 84  0
         collectionPane.appear();
 85  0
     }
 86  
 
 87  
     @Override
 88  
     public void saveState(IView window, String name) {
 89  0
         if (configuration != null) {
 90  0
             configuration.update(name, window);
 91  
         }
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public void configureView(IView view, String name, int defaultWidth, int defaultHeight) {
 96  0
         configuration.configure(name, view, defaultWidth, defaultHeight);
 97  0
     }
 98  
 
 99  
     @Override
 100  
     public void setMainComponent(ViewComponent component) {
 101  0
         mainComponent = component;
 102  0
     }
 103  
 
 104  
     @Override
 105  
     public void removeMainComponent(ViewComponent component) {
 106  0
         if (mainComponent == component) {
 107  0
             mainComponent = null;
 108  
         }
 109  0
     }
 110  
 
 111  
     @Override
 112  
     public boolean isTabMainComponent() {
 113  0
         return mainComponent == null;
 114  
     }
 115  
 
 116  
     @Override
 117  
     public ViewComponent getMainComponent() {
 118  0
         if (isTabMainComponent()) {
 119  0
             return windowManager.getMainView().getTabbedPane();
 120  
         }
 121  
 
 122  0
         return mainComponent;
 123  
     }
 124  
 
 125  
     @Override
 126  
     public void close() {
 127  0
         if (windowManager != null && windowManager.getMainView() != null) {
 128  0
             windowManager.getMainView().closeDown();
 129  
         }
 130  0
     }
 131  
 
 132  
     @Override
 133  
     public void init() {
 134  0
         MacOSXConfiguration.configureForMac();
 135  
 
 136  0
         configuration = getStates().getState(WindowsConfiguration.class);
 137  
 
 138  0
         if (configuration == null) {
 139  
             try {
 140  0
                 configuration = getStates().createState(WindowsConfiguration.class);
 141  0
             } catch (Exception e) {
 142  0
                 getLogger().error(e, "Unable to retrieve configuration");
 143  0
                 configuration = new WindowsConfiguration();
 144  0
                 getErrorManager().addInternationalizedError("error.loading.configuration");
 145  0
             }
 146  
         }
 147  0
     }
 148  
 
 149  
     @Override
 150  
     public WindowsConfiguration getConfigurations() {
 151  0
         return configuration;
 152  
     }
 153  
 
 154  
     @Override
 155  
     public Views getViews() {
 156  0
         return windowManager;
 157  
     }
 158  
 
 159  
     @Override
 160  
     public ISplashManager getSplashManager() {
 161  0
         return SplashManager.getInstance();
 162  
     }
 163  
 
 164  
     @Override
 165  
     public void addStateBarComponent(StateBarComponent component) {
 166  0
         stateBarComponents.add(component);
 167  
 
 168  0
         if (component != null && component.getComponent() != null) {
 169  0
             fireStateBarComponentAdded();
 170  
         }
 171  0
     }
 172  
 
 173  
     @Override
 174  
     public void removeStateBarComponent(StateBarComponent component) {
 175  0
         stateBarComponents.remove(component);
 176  
 
 177  0
         if (component != null && component.getComponent() != null) {
 178  0
             fireStateBarComponentRemoved(component);
 179  
         }
 180  0
     }
 181  
 
 182  
     @Override
 183  
     public Collection<StateBarComponent> getStateBarComponents() {
 184  0
         return CollectionUtils.copyOf(stateBarComponents);
 185  
     }
 186  
 
 187  
     @Override
 188  
     public void addTabComponent(TabComponent component) {
 189  0
         if (isTabMainComponent()) {
 190  0
             tabComponents.add(component);
 191  
 
 192  0
             fireTabComponentAdded();
 193  
         }
 194  0
     }
 195  
 
 196  
     @Override
 197  
     public void removeTabComponent(TabComponent component) {
 198  0
         if (isTabMainComponent()) {
 199  0
             tabComponents.remove(component);
 200  
 
 201  0
             fireTabComponentRemoved(component);
 202  
         }
 203  0
     }
 204  
 
 205  
     @Override
 206  
     public Collection<TabComponent> getTabComponents() {
 207  0
         return CollectionUtils.copyOf(tabComponents);
 208  
     }
 209  
 
 210  
     @Override
 211  
     public void addConfigTabComponent(ConfigTabComponent component) {
 212  0
         configPanels.add(component);
 213  
 
 214  0
         fireConfigTabComponentAdded(component);
 215  0
     }
 216  
 
 217  
     @Override
 218  
     public void removeConfigTabComponent(ConfigTabComponent component) {
 219  0
         configPanels.remove(component);
 220  
 
 221  0
         fireConfigTabComponentRemoved(component);
 222  0
     }
 223  
 
 224  
     @Override
 225  
     public Collection<ConfigTabComponent> getConfigTabComponents() {
 226  0
         return CollectionUtils.copyOf(configPanels);
 227  
     }
 228  
 
 229  
     @Override
 230  
     public boolean askUserForConfirmation(String text, String title) {
 231  0
         return viewDelegate.askYesOrNo(text, title);
 232  
     }
 233  
 
 234  
     @Override
 235  
     public boolean askI18nUserForConfirmation(String textKey, String titleKey) {
 236  0
         return viewDelegate.askYesOrNo(getMessage(textKey), getMessage(titleKey));
 237  
     }
 238  
 
 239  
     @Override
 240  
     public String askUserForText(String text) {
 241  0
         return viewDelegate.askText(text);
 242  
     }
 243  
 
 244  
     @Override
 245  
     public void displayText(String text) {
 246  0
         viewDelegate.displayText(text);
 247  0
     }
 248  
 
 249  
     @Override
 250  
     public String chooseFile(SimpleFilter filter) {
 251  0
         return viewDelegate.chooseFile(filter);
 252  
     }
 253  
 
 254  
     @Override
 255  
     public String chooseDirectory() {
 256  0
         return viewDelegate.chooseDirectory();
 257  
     }
 258  
 
 259  
     @Override
 260  
     public void displayI18nText(String key) {
 261  0
         displayText(getMessage(key));
 262  0
     }
 263  
 
 264  
     @Override
 265  
     public void addStateBarListener(StateBarListener listener) {
 266  0
         getListeners().add(StateBarListener.class, listener);
 267  0
     }
 268  
 
 269  
     @Override
 270  
     public void removeStateBarListener(StateBarListener listener) {
 271  0
         getListeners().remove(StateBarListener.class, listener);
 272  0
     }
 273  
 
 274  
     @Override
 275  
     public void addConfigTabListener(ConfigTabListener listener) {
 276  0
         getListeners().add(ConfigTabListener.class, listener);
 277  0
     }
 278  
 
 279  
     @Override
 280  
     public void removeConfigTabListener(ConfigTabListener listener) {
 281  0
         getListeners().remove(ConfigTabListener.class, listener);
 282  0
     }
 283  
 
 284  
     @Override
 285  
     public void addTabListener(TabListener listener) {
 286  0
         getListeners().add(TabListener.class, listener);
 287  0
     }
 288  
 
 289  
     @Override
 290  
     public void removeTabListener(TabListener listener) {
 291  0
         getListeners().remove(TabListener.class, listener);
 292  0
     }
 293  
 
 294  
     @Override
 295  
     public void displayError(JThequeError error) {
 296  0
         viewDelegate.displayError(error);
 297  0
     }
 298  
 
 299  
     @Override
 300  
     public ViewDelegate getViewDelegate() {
 301  0
         return viewDelegate;
 302  
     }
 303  
 
 304  
     /**
 305  
      * Fire a state bar component added event.
 306  
      */
 307  
     private static void fireStateBarComponentAdded() {
 308  0
         StateBarListener[] l = getListeners().getListeners(StateBarListener.class);
 309  
 
 310  0
         for (StateBarListener listener : l) {
 311  0
             listener.componentAdded();
 312  
         }
 313  0
     }
 314  
 
 315  
     /**
 316  
      * Fire a state bar component removed event.
 317  
      *
 318  
      * @param component The state bar component who's been removed.
 319  
      */
 320  
     private void fireStateBarComponentRemoved(StateBarComponent component) {
 321  0
         StateBarListener[] l = getListeners().getListeners(StateBarListener.class);
 322  
 
 323  0
         StateBarEvent event = new StateBarEvent(this, component);
 324  
 
 325  0
         for (StateBarListener listener : l) {
 326  0
             listener.componentRemoved(event);
 327  
         }
 328  0
     }
 329  
 
 330  
     /**
 331  
      * Fire a config tab component added event.
 332  
      *
 333  
      * @param component The config tab component who's been added.
 334  
      */
 335  
     private void fireConfigTabComponentAdded(ConfigTabComponent component) {
 336  0
         ConfigTabListener[] l = getListeners().getListeners(ConfigTabListener.class);
 337  
 
 338  0
         ConfigTabEvent event = new ConfigTabEvent(this, component);
 339  
 
 340  0
         for (ConfigTabListener listener : l) {
 341  0
             listener.tabAdded(event);
 342  
         }
 343  0
     }
 344  
 
 345  
     /**
 346  
      * Fire a config tab component removed event.
 347  
      *
 348  
      * @param component The config tab component who's been removed.
 349  
      */
 350  
     private void fireConfigTabComponentRemoved(ConfigTabComponent component) {
 351  0
         ConfigTabListener[] l = getListeners().getListeners(ConfigTabListener.class);
 352  
 
 353  0
         ConfigTabEvent event = new ConfigTabEvent(this, component);
 354  
 
 355  0
         for (ConfigTabListener listener : l) {
 356  0
             listener.tabRemoved(event);
 357  
         }
 358  0
     }
 359  
 
 360  
     /**
 361  
      * Fire a tab component added event.
 362  
      */
 363  
     private static void fireTabComponentAdded() {
 364  0
         TabListener[] l = getListeners().getListeners(TabListener.class);
 365  
 
 366  0
         for (TabListener listener : l) {
 367  0
             listener.tabAdded();
 368  
         }
 369  0
     }
 370  
 
 371  
     /**
 372  
      * Fire a tab component removed event.
 373  
      *
 374  
      * @param component The tab component who's been removed.
 375  
      */
 376  
     private void fireTabComponentRemoved(TabComponent component) {
 377  0
         TabListener[] l = getListeners().getListeners(TabListener.class);
 378  
 
 379  0
         TabEvent event = new TabEvent(this, component);
 380  
 
 381  0
         for (TabListener listener : l) {
 382  0
             listener.tabRemoved(event);
 383  
         }
 384  0
     }
 385  
 
 386  
     @Override
 387  
     public void execute(SimpleTask task) {
 388  0
         viewDelegate.run(task.asRunnable());
 389  0
     }
 390  
 
 391  
     @Override
 392  
     public <T> T execute(Task<T> task) {
 393  0
         viewDelegate.run(task.asRunnable());
 394  
 
 395  0
         return task.getResult();
 396  
     }
 397  
 
 398  
     @Override
 399  
     public void refresh(Object c) {
 400  0
         viewDelegate.refresh(c);
 401  0
     }
 402  
 
 403  
     @Override
 404  
     public ICollectionView getCollectionView() {
 405  0
         return collectionPane;
 406  
     }
 407  
 
 408  
     @Override
 409  
     public ViewDefaults getViewDefaults() {
 410  0
         return viewDefaults;
 411  
     }
 412  
 
 413  
     /**
 414  
      * Set the config panels. This is not for use, this is only for Spring Injection.
 415  
      *
 416  
      * @param configPanels A collection containing all the config tab components.
 417  
      */
 418  
     public void setConfigPanels(Collection<ConfigTabComponent> configPanels) {
 419  0
         this.configPanels = CollectionUtils.copyOf(configPanels);
 420  0
     }
 421  
 
 422  
     /**
 423  
      * Set the view delegate. This is not for use, this is only for Spring Injection.
 424  
      *
 425  
      * @param viewDelegate The view delegate implementation.
 426  
      */
 427  
     public void setViewDelegate(ViewDelegate viewDelegate) {
 428  0
         this.viewDelegate = viewDelegate;
 429  0
     }
 430  
 
 431  
     /**
 432  
      * Set the view defaults. This is not for use, this is only for Spring Injection.
 433  
      *
 434  
      * @param viewDefaults The view defaults.
 435  
      */
 436  
     public void setViewDefaults(ViewDefaults viewDefaults) {
 437  0
         this.viewDefaults = viewDefaults;
 438  0
     }
 439  
 }