Coverage Report - org.openpermis.editor.policy.presenter.Presenter
 
Classes in this File Line Coverage Branch Coverage Complexity
Presenter
52%
11/21
25%
1/4
1.375
 
 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.presenter;
 11  
 
 12  
 import java.beans.PropertyChangeListener;
 13  
 import java.beans.PropertyChangeSupport;
 14  
 
 15  
 /**
 16  
  * Abstract base class for presenter beans.
 17  
  * <p>All abstract presenters have built-in property change support for bound properties.</p>
 18  
  * @param <M> the model of this presenter.
 19  
  * @since 0.1.0
 20  
  */
 21  
 public abstract class Presenter<M> {
 22  
 
 23  
         //---- State
 24  
 
 25  
         /**
 26  
          * Beans property change support.
 27  
          * @since 0.1.0
 28  
          */
 29  
         private final PropertyChangeSupport changeSupport;
 30  
 
 31  
         /**
 32  
          * The model this presenter is bound to.
 33  
          * @since 0.1.0
 34  
          */
 35  
         private final M model;
 36  
 
 37  
         /**
 38  
          * An array of title parameters for suitable display in a view title.
 39  
          * @since 0.1.0
 40  
          */
 41  
         private String[] titleParameters;
 42  
 
 43  
         //---- Constructors
 44  
 
 45  
         /**
 46  
          * Creates an abstract presenter for the specified domain object.
 47  
          * @param model the domain object this presenter represents, must not be {@code null}.
 48  
          * @since 0.1.0
 49  
          */
 50  9
         protected Presenter (M model) {
 51  9
                 if (model == null) {
 52  0
                         throw new IllegalArgumentException("Presenter model must not be [null].");
 53  
                 }
 54  9
                 this.model = model;
 55  9
                 this.titleParameters = new String[0];
 56  9
                 this.changeSupport = new PropertyChangeSupport(this);
 57  9
         }
 58  
 
 59  
         //---- Methods
 60  
 
 61  
         /**
 62  
          * Returns the model of this presenter.
 63  
          * @return the model of this presenter.
 64  
          * @since 0.1.0
 65  
          */
 66  
         protected final M getModel () {
 67  133
                 return this.model;
 68  
         }
 69  
 
 70  
         /**
 71  
          * Returns an array of title parameters for suitable display in a view title.
 72  
          * @return the request array of title parameters, never {@code null}.
 73  
          * @since 0.1.0
 74  
          */
 75  
         public String[] getTitleParameters () {
 76  0
                 return this.titleParameters;
 77  
         }
 78  
 
 79  
         /**
 80  
          * Sets the array of title parameters which are used in view titles.
 81  
          * @param titleParameters the new array to set.
 82  
          * @since 0.1.0
 83  
          */
 84  
         public void setTitleParameters (String... titleParameters) {
 85  0
                 if (titleParameters == null) {
 86  0
                         titleParameters = new String[0];
 87  
                 }
 88  0
                 final String[] oldParameters = this.titleParameters;
 89  0
                 this.titleParameters = titleParameters;
 90  0
                 firePropertyChange("titleParameters", oldParameters, this.titleParameters);
 91  0
         }
 92  
 
 93  
         /**
 94  
          * Disposes this presenter.
 95  
          * <p>The presenter has to remove all listeners registered.</p>
 96  
          * @since 0.1.0
 97  
          */
 98  
         public abstract void dispose ();
 99  
 
 100  
         //---- JavaBean
 101  
 
 102  
         /**
 103  
          * Registers the specified property change listener.
 104  
          * @param listener the listener to register.
 105  
          * @since 0.1.0
 106  
          */
 107  
         public void addPropertyChangeListener (PropertyChangeListener listener) {
 108  9
                 this.changeSupport.addPropertyChangeListener(listener);
 109  9
         }
 110  
 
 111  
         /**
 112  
          * Removes the specified property change listener.
 113  
          * @param listener the listener to remove.
 114  
          * @since 0.1.0
 115  
          */
 116  
         public void removePropertyChangeListener (PropertyChangeListener listener) {
 117  0
                 this.changeSupport.removePropertyChangeListener(listener);
 118  0
         }
 119  
 
 120  
         /**
 121  
          * Support for reporting bound property changes for Object properties.
 122  
          * <p>Use this method to inform registered property change listeners of a change in
 123  
          * a bound property.</p>
 124  
          * @param property the property whose value has changed.
 125  
          * @param oldValue the previous value of the property.
 126  
          * @param newValue the new value of the property.
 127  
          * @since 0.1.0
 128  
          */
 129  
         protected void firePropertyChange (String property, Object oldValue, Object newValue) {
 130  175
                 this.changeSupport.firePropertyChange(property, oldValue, newValue);
 131  175
         }
 132  
 
 133  
 }