Coverage Report - org.jtheque.core.managers.view.impl.frame.abstraction.SwingDialogView
 
Classes in this File Line Coverage Branch Coverage Complexity
SwingDialogView
0 %
0/68
0 %
0/10
1.207
 
 1  
 package org.jtheque.core.managers.view.impl.frame.abstraction;
 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.beans.IBeansManager;
 21  
 import org.jtheque.core.managers.error.IErrorManager;
 22  
 import org.jtheque.core.managers.error.JThequeError;
 23  
 import org.jtheque.core.managers.language.ILanguageManager;
 24  
 import org.jtheque.core.managers.language.Internationalizable;
 25  
 import org.jtheque.core.managers.resource.IResourceManager;
 26  
 import org.jtheque.core.managers.view.able.IViewManager;
 27  
 import org.jtheque.core.managers.view.able.IWindowView;
 28  
 import org.jtheque.core.managers.view.able.components.IModel;
 29  
 import org.jtheque.core.managers.view.impl.actions.ActionFactory;
 30  
 import org.jtheque.core.managers.view.impl.components.ExtendedGlassPane;
 31  
 import org.jtheque.core.managers.view.impl.components.InfiniteWaitFigure;
 32  
 import org.jtheque.core.managers.view.impl.components.WaitFigure;
 33  
 import org.jtheque.utils.collections.ArrayUtils;
 34  
 
 35  
 import javax.swing.Action;
 36  
 import javax.swing.JDialog;
 37  
 import javax.swing.JFrame;
 38  
 import java.awt.Component;
 39  
 import java.awt.Frame;
 40  
 import java.awt.Image;
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 
 44  
 /**
 45  
  * A swing dialog view.
 46  
  *
 47  
  * @author Baptiste Wicht
 48  
  */
 49  0
 public abstract class SwingDialogView extends JDialog implements IWindowView, Internationalizable {
 50  
     private String titleKey;
 51  
     private Object[] titleReplaces;
 52  
 
 53  
     private IModel model;
 54  
 
 55  
     private boolean glassPaneInstalled;
 56  
     private boolean waitFigureInstalled;
 57  
 
 58  
     /**
 59  
      * Construct a SwingDialogView modal to the main view.
 60  
      */
 61  
     protected SwingDialogView(){
 62  0
         this((Frame) Managers.getManager(IViewManager.class).getViews().getMainView().getImpl());
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Construct a SwingDialogView.
 67  
      *
 68  
      * @param frame The parent frame.
 69  
      */
 70  
     protected SwingDialogView(Frame frame) {
 71  0
         super(frame);
 72  
 
 73  0
         setModal(true);
 74  0
         setResizable(true);
 75  
 
 76  0
         Managers.getManager(ILanguageManager.class).addInternationalizable(this);
 77  
 
 78  0
         setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
 79  0
         setIconImage(getDefaultWindowIcon());
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Install the default glass pane. If you need to install another glass pane, simply override
 84  
      * this method.
 85  
      */
 86  
     void installGlassPane() {
 87  0
         Component glassPane = new ExtendedGlassPane(this);
 88  0
         setGlassPane(glassPane);
 89  0
         glassPane.setVisible(false);
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Install the default wait figure. If you need to use another wait figure, simply override
 94  
      * this method.
 95  
      */
 96  
     void installWaitFigure() {
 97  0
         setWaitFigure(new InfiniteWaitFigure());
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Return The extendedGlassPane of this frame.
 102  
      *
 103  
      * @return The glass pane
 104  
      */
 105  
     final ExtendedGlassPane getExtendedGlassPane() {
 106  0
         return (ExtendedGlassPane) getGlassPane();
 107  
     }
 108  
 
 109  
     @Override
 110  
     public Component getGlassPane() {
 111  0
         installGlassPaneIfNecessary();
 112  
 
 113  0
         return super.getGlassPane();
 114  
     }
 115  
 
 116  
     /**
 117  
      * Start the wait. The glass pane will start the wait animation.
 118  
      */
 119  
     public final void startWait() {
 120  0
         installWaitFigureIfNecessary();
 121  
 
 122  0
         getExtendedGlassPane().startWait();
 123  0
     }
 124  
 
 125  
     /**
 126  
      * Stop the wait. The glass pane will stop the wait animation.
 127  
      */
 128  
     public final void stopWait() {
 129  0
         installWaitFigureIfNecessary();
 130  
 
 131  0
         getExtendedGlassPane().stopWait();
 132  0
     }
 133  
 
 134  
     /**
 135  
      * Return the default window icon.
 136  
      *
 137  
      * @return The default window icon.
 138  
      */
 139  
     protected static Image getDefaultWindowIcon() {
 140  0
         return Managers.getManager(IResourceManager.class).getImage(
 141  
                 "file:" + Managers.getCore().getApplication().getWindowIcon(),
 142  
                 Managers.getCore().getApplication().getWindowIconType());
 143  
     }
 144  
 
 145  
     /**
 146  
      * Set the wait figure of the dialog view. Must be called in the installWaitFigure() method.
 147  
      *
 148  
      * @param waitFigure The wait figure.
 149  
      */
 150  
     protected final void setWaitFigure(WaitFigure waitFigure) {
 151  0
         getExtendedGlassPane().setWaitFigure(waitFigure);
 152  0
     }
 153  
 
 154  
     /**
 155  
      * Return the view manager.
 156  
      *
 157  
      * @return The view manager.
 158  
      */
 159  
     public static IViewManager getManager(){
 160  0
         return Managers.getManager(IViewManager.class);
 161  
     }
 162  
 
 163  
     /**
 164  
      * Return the bean with a specific name.
 165  
      *
 166  
      * @param name The name of the bean.
 167  
      * @return The bean.
 168  
      */
 169  
     public static <T> T getBean(String name){
 170  0
         return Managers.getManager(IBeansManager.class).<T>getBean(name);
 171  
     }
 172  
 
 173  
     /**
 174  
      * Return an action to close this view.
 175  
      *
 176  
      * @param key The i18n key.
 177  
      *
 178  
      * @return An action to close this view.
 179  
      */
 180  
     public Action getCloseAction(String key){
 181  0
         return ActionFactory.createCloseViewAction(key, this);
 182  
     }
 183  
 
 184  
     @Override
 185  
     public void closeDown() {
 186  0
         setVisible(false);
 187  0
     }
 188  
 
 189  
     @Override
 190  
     public void display() {
 191  0
         setVisible(true);
 192  0
     }
 193  
 
 194  
     @Override
 195  
     public final void toFirstPlan() {
 196  0
         toFront();
 197  0
     }
 198  
 
 199  
     @Override
 200  
     public void sendMessage(String message, Object value) {
 201  0
         throw new UnsupportedOperationException();
 202  
     }
 203  
 
 204  
     @Override
 205  
     public final void refresh() {
 206  0
         Managers.getManager(IViewManager.class).refresh(this);
 207  0
     }
 208  
 
 209  
     /**
 210  
      * Set the title key.
 211  
      *
 212  
      * @param key The internationalization key.
 213  
      * @param replaces The replacements objects for the i18n methods. 
 214  
      */
 215  
     protected final void setTitleKey(String key, Object... replaces) {
 216  0
         titleKey = key;
 217  
 
 218  0
         if(!ArrayUtils.isEmpty(replaces)){
 219  0
             titleReplaces = ArrayUtils.copyOf(replaces);
 220  
         }
 221  
 
 222  0
         setTitle(getMessage(key, replaces));
 223  0
     }
 224  
 
 225  
     @Override
 226  
     public void refreshText() {
 227  0
         if (titleKey != null) {
 228  0
             setTitleKey(titleKey, titleReplaces);
 229  
         }
 230  0
     }
 231  
 
 232  
     /**
 233  
      * Set the model of the view.
 234  
      *
 235  
      * @param model The model of the view.
 236  
      */
 237  
     public void setModel(IModel model) {
 238  0
         this.model = model;
 239  0
     }
 240  
 
 241  
     @Override
 242  
     public IModel getModel() {
 243  0
         return model;
 244  
     }
 245  
 
 246  
     @Override
 247  
     public final JDialog getImpl() {
 248  0
         return this;
 249  
     }
 250  
 
 251  
     @Override
 252  
     public final boolean validateContent() {
 253  0
         Collection<JThequeError> errors = new ArrayList<JThequeError>(5);
 254  
 
 255  0
         validate(errors);
 256  
 
 257  0
         for (JThequeError error : errors) {
 258  0
             Managers.getManager(IErrorManager.class).addError(error);
 259  
         }
 260  
 
 261  0
         return errors.isEmpty();
 262  
     }
 263  
 
 264  
     /**
 265  
      * Return the internationalized message.
 266  
      *
 267  
      * @param key The internationalization key.
 268  
      * @return The internationalized message.
 269  
      */
 270  
     protected static String getMessage(String key) {
 271  0
         return Managers.getManager(ILanguageManager.class).getMessage(key);
 272  
     }
 273  
 
 274  
     /**
 275  
      * Return the internationalized message.
 276  
      *
 277  
      * @param key      The internationalization key.
 278  
      * @param replaces The replacement objects to use.
 279  
      * @return the internationalized message.
 280  
      */
 281  
     protected static String getMessage(String key, Object... replaces) {
 282  0
         return Managers.getManager(ILanguageManager.class).getMessage(key, replaces);
 283  
     }
 284  
 
 285  
     /**
 286  
      * Validate the view and save all the validation's errors in the list.
 287  
      *
 288  
      * @param errors The error's list.
 289  
      */
 290  
     protected abstract void validate(Collection<JThequeError> errors);
 291  
 
 292  
     /**
 293  
      * Install the glass pane of the view if necessary.
 294  
      */
 295  
     private void installGlassPaneIfNecessary() {
 296  0
         if (!glassPaneInstalled) {
 297  0
             installGlassPane();
 298  0
             glassPaneInstalled = true;
 299  
         }
 300  0
     }
 301  
 
 302  
     /**
 303  
      * Install the wait figure if necessary. 
 304  
      */
 305  
     private void installWaitFigureIfNecessary() {
 306  0
         installGlassPaneIfNecessary();
 307  
 
 308  0
         if (!waitFigureInstalled) {
 309  0
             installWaitFigure();
 310  0
             waitFigureInstalled = true;
 311  
         }
 312  0
     }
 313  
 }