Coverage Report - org.openpermis.editor.policy.view.AbstractEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEditor
0%
0/35
0%
0/14
1.583
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.editor.policy.view;
 11  
 
 12  
 import javax.swing.JComponent;
 13  
 import javax.swing.JOptionPane;
 14  
 import javax.swing.JPanel;
 15  
 import javax.swing.JScrollPane;
 16  
 
 17  
 import org.jdesktop.application.ApplicationContext;
 18  
 import org.jdesktop.beansbinding.BindingGroup;
 19  
 
 20  
 import com.jgoodies.forms.layout.FormLayout;
 21  
 
 22  
 import org.openpermis.editor.policy.adapter.AdapterTrader;
 23  
 import org.openpermis.editor.policy.beans.PropertyChange;
 24  
 import org.openpermis.editor.policy.beans.PropertyChangeDispatcher;
 25  
 import org.openpermis.editor.policy.gui.binding.ErrorBindingListener;
 26  
 import org.openpermis.editor.policy.presenter.PartPresenter;
 27  
 import org.openpermis.policy.bean.SerialNumber;
 28  
 
 29  
 
 30  
 /**
 31  
  * Abstract base class for editors of part presenters.
 32  
  * @param <P> the type of presenter bound to this view.
 33  
  * @since 0.1.0
 34  
  */
 35  
 public abstract class AbstractEditor<P extends PartPresenter<?>>
 36  
         extends AbstractView
 37  
         implements Editor
 38  
 {
 39  
 
 40  
         //---- State
 41  
 
 42  
         /**
 43  
          * The presenter of this view.
 44  
          * @since 0.1.0
 45  
          */
 46  
         private final P presenter;
 47  
 
 48  
         /**
 49  
          * Dispatcher attached to the presenter to update the title parameters.
 50  
          * @since 0.1.0
 51  
          */
 52  
         private final PropertyChangeDispatcher propertyChangeDispatcher;
 53  
 
 54  
         /**
 55  
          * The binding group used to bind the presenter to view components.
 56  
          * @since 0.1.0
 57  
          */
 58  
         private final BindingGroup bindingGroup;
 59  
 
 60  
         //---- Constructors
 61  
 
 62  
         /**
 63  
          * Creates an editor for the specified presenter.
 64  
          * @param context the application context.
 65  
          * @param presenter the presenter for this editor.
 66  
          * @since 0.1.0
 67  
          */
 68  
         protected AbstractEditor (ApplicationContext context, AdapterTrader trader, P presenter) {
 69  0
                 super(context, trader);
 70  0
                 if (presenter == null) {
 71  0
                         throw new IllegalArgumentException("Presenter must not be [null].");
 72  
                 }
 73  0
                 this.presenter = presenter;
 74  0
                 this.propertyChangeDispatcher = new PropertyChangeDispatcher(presenter, this);
 75  0
                 this.bindingGroup = new BindingGroup();
 76  0
         }
 77  
 
 78  
         //---- Methods
 79  
 
 80  
         /**
 81  
          * Returns the presenter of this view.
 82  
          * @return the presenter of this view.
 83  
          * @since 0.1.0
 84  
          */
 85  
         protected final P getPresenter () {
 86  0
                 return this.presenter;
 87  
         }
 88  
 
 89  
         /**
 90  
          * Instructs the view to update its title once the parameters change.
 91  
          * @since 0.1.0
 92  
          */
 93  
         @PropertyChange(bean = PartPresenter.class, property = "titleParameters")
 94  
         public void adjustTitleParameters () {
 95  0
                 updateTitle();
 96  0
         }
 97  
 
 98  
         /**
 99  
          * Binds all components to the presenter.
 100  
          * <p>This method is called after creation of the content pane.</p>
 101  
          * @param bindings the binding group to add bindings to.
 102  
          * @since 0.1.0
 103  
          */
 104  
         protected abstract void bind (BindingGroup bindings);
 105  
 
 106  
         /**
 107  
          * Show yes-no-dialog.
 108  
          * @param title title-text
 109  
          * @param body body-text
 110  
          * @since 0.1.0
 111  
          */
 112  
         public boolean showYesNoDialog (String title, String body, int expectedValue) {
 113  0
                 return JOptionPane.showConfirmDialog(
 114  
                         getContentPane(),
 115  
                         getResourceMap().getString(body, getTitle()),
 116  
                         getResourceMap().getString(title),
 117  
                         JOptionPane.YES_NO_OPTION
 118  
                 ) == expectedValue;
 119  
         }
 120  
 
 121  
         //---- Editor
 122  
 
 123  
         /**
 124  
          * @since 0.1.0
 125  
          */
 126  
         public final SerialNumber getModelSerialNumber () {
 127  0
                 return getPresenter().getModelSerialNumber();
 128  
         }
 129  
 
 130  
         /**
 131  
          * @since 0.1.0
 132  
          */
 133  
         public void dispose () {
 134  0
                 this.bindingGroup.unbind();
 135  0
                 this.propertyChangeDispatcher.dispose();
 136  0
                 getPresenter().dispose();
 137  0
         }
 138  
 
 139  
         //---- View
 140  
 
 141  
         /**
 142  
          * @since 0.1.0
 143  
          */
 144  
         @Override
 145  
         protected void contentPaneCreated () {
 146  0
                 bind(this.bindingGroup);
 147  0
                 this.bindingGroup.bind();
 148  0
         }
 149  
 
 150  
         /**
 151  
          * @since 0.1.0
 152  
          */
 153  
         @Override
 154  
         public Object[] getTitleParameters () {
 155  0
                 final String[] presenterParameters = getPresenter().getTitleParameters();
 156  0
                 final String[] parameters = new String[presenterParameters.length + 2];
 157  0
                 System.arraycopy(presenterParameters, 0, parameters, 2, presenterParameters.length);
 158  0
                 parameters[0] = getPresenter().getModelSerialNumber().toString();
 159  0
                 for (int i = 0 ; i < parameters.length ; i++) {
 160  0
                         if (parameters[i] == null || parameters[i].length() == 0) {
 161  0
                                 parameters[i] = getResourceMap().getString("emptyTitleParameter");
 162  
                         }
 163  
                 }
 164  0
                 return parameters;
 165  
         }
 166  
 
 167  
         /**
 168  
          * @since 0.1.0
 169  
          */
 170  
         @Override
 171  
         public boolean canClose () {
 172  0
                 if (!ErrorBindingListener.hasAnyError(getContentPane())) {
 173  0
                         return true;
 174  
                 }
 175  0
                 return JOptionPane.showConfirmDialog(
 176  
                         getContentPane(),
 177  
                         getResourceMap().getString("errorCloseMessage", getTitle()),
 178  
                         getResourceMap().getString("errorCloseTitle"),
 179  
                         JOptionPane.YES_NO_OPTION
 180  
                 ) == JOptionPane.YES_OPTION;
 181  
         }
 182  
 
 183  
         /**
 184  
          * @since 0.1.0
 185  
          */
 186  
         abstract void fillContentPane (JPanel panel);
 187  
 
 188  
         /**
 189  
          * @since 0.1.0
 190  
          */
 191  
         @Override
 192  
         protected JComponent createContentPane () {
 193  0
                 final FormLayout layout = new FormLayout(
 194  
                         "8dlu, fill:min:grow, 8dlu",
 195  
                         "8dlu, pref, 8dlu, fill:pref:grow, 8dlu"
 196  
                 );
 197  0
                 final JPanel panel = new JPanel();
 198  0
                 panel.setLayout(layout);
 199  0
                 fillContentPane(panel);
 200  0
                 return new JScrollPane(panel);
 201  
         }
 202  
 
 203  
 
 204  
 }