Coverage Report - org.openpermis.editor.policy.presenter.RecentFilesPresenter
 
Classes in this File Line Coverage Branch Coverage Complexity
RecentFilesPresenter
0%
0/29
0%
0/4
1.222
 
 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.util.ArrayList;
 13  
 import java.util.Arrays;
 14  
 import java.util.List;
 15  
 
 16  
 import org.jdesktop.observablecollections.ObservableCollections;
 17  
 import org.jdesktop.observablecollections.ObservableList;
 18  
 
 19  
 import org.openpermis.editor.policy.ApplicationState;
 20  
 import org.openpermis.editor.policy.beans.PropertyChange;
 21  
 import org.openpermis.editor.policy.beans.PropertyChangeDispatcher;
 22  
 
 23  
 /**
 24  
  * Presenter class for the application recent files list.
 25  
  * @since 0.1.0
 26  
  */
 27  
 public class RecentFilesPresenter
 28  
         extends Presenter<ApplicationState>
 29  
 {
 30  
 
 31  
         //---- State
 32  
 
 33  
         /**
 34  
          * The property change dispatcher for the model.
 35  
          * @since 0.1.0
 36  
          */
 37  
         private final PropertyChangeDispatcher changeDispatcher;
 38  
 
 39  
         /**
 40  
          * The recent files list as observable list.
 41  
          * @since 0.1.0
 42  
          */
 43  
         private final ObservableList<String> recentFiles;
 44  
 
 45  
         /**
 46  
          * The currently active file.
 47  
          * @since 0.1.0
 48  
          */
 49  
         private String active;
 50  
 
 51  
         //---- Constructors
 52  
 
 53  
         /**
 54  
          * Creates a recent files presenter that uses the global application state.
 55  
          * @see org.openpermis.editor.policy.Application#getApplicationState()
 56  
          * @since 0.1.0
 57  
          */
 58  
         public RecentFilesPresenter (ApplicationState applicationState) {
 59  0
                 super(applicationState);
 60  0
                 this.changeDispatcher = new PropertyChangeDispatcher(applicationState, this);
 61  0
                 this.recentFiles = ObservableCollections.observableList(
 62  
                         new ArrayList<String>(Arrays.asList(getModel().getRecentFiles()))
 63  
                 );
 64  0
         }
 65  
 
 66  
         //---- Methods
 67  
 
 68  
         /**
 69  
          * Returns the recent files list as observable list.
 70  
          * @return the recent files list as observable list.
 71  
          * @since 0.1.0
 72  
          */
 73  
         public ObservableList<String> getRecentFiles () {
 74  0
                 return this.recentFiles;
 75  
         }
 76  
 
 77  
         /**
 78  
          * Moves the active element to the front of the list.
 79  
          * @since 0.1.0
 80  
          */
 81  
         public void activeToFront () {
 82  0
                 final int index = this.recentFiles.indexOf(this.active);
 83  0
                 if (index != -1) {
 84  0
                         final List<String> newList = new ArrayList<String>(this.recentFiles);
 85  0
                         newList.add(0, newList.remove(index));
 86  0
                         getModel().setRecentFiles(newList.toArray(new String[newList.size()]));
 87  
                 }
 88  0
         }
 89  
 
 90  
         /**
 91  
          * Removes the active file.
 92  
          * @since 0.1.0
 93  
          */
 94  
         public void removeActive () {
 95  0
                 final int index = this.recentFiles.indexOf(this.active);
 96  0
                 if (index != -1) {
 97  0
                         final List<String> newList = new ArrayList<String>(this.recentFiles);
 98  0
                         newList.remove(index);
 99  0
                         getModel().setRecentFiles(newList.toArray(new String[newList.size()]));
 100  
                 }
 101  0
         }
 102  
 
 103  
         /**
 104  
          * Clears the list of recent files.
 105  
          * @since 0.1.0
 106  
          */
 107  
         public void clearRecentFiles () {
 108  0
                 getModel().setRecentFiles(null);
 109  0
         }
 110  
 
 111  
         /**
 112  
          * Returns the currently active file.
 113  
          * @return the currently active file, may be <code>null</code>.
 114  
          * @since 0.1.0
 115  
          */
 116  
         public String getActive () {
 117  0
                 return this.active;
 118  
         }
 119  
 
 120  
         /**
 121  
          * Sets the currently active file.
 122  
          * @param active the file to set active.
 123  
          * @since 0.1.0
 124  
          */
 125  
         public void setActive (String active) {
 126  0
                 final String oldValue = this.active;
 127  0
                 this.active = active;
 128  0
                 firePropertyChange("active", oldValue, this.active);
 129  0
         }
 130  
 
 131  
         //---- Presenter
 132  
 
 133  
         /**
 134  
          * @since 0.1.0
 135  
          */
 136  
         @Override
 137  
         public void dispose () {
 138  0
                 this.changeDispatcher.dispose();
 139  0
         }
 140  
 
 141  
         //---- PropertyChanges
 142  
 
 143  
         /**
 144  
          * Called whenever the recent files list of the application state model changes.
 145  
          * @since 0.1.0
 146  
          */
 147  
         @PropertyChange(bean = ApplicationState.class, property = "recentFiles")
 148  
         public void recentFilesChanged () {
 149  0
                 this.recentFiles.clear();
 150  0
                 this.recentFiles.addAll(Arrays.asList(getModel().getRecentFiles()));
 151  0
         }
 152  
 
 153  
 }