Coverage Report - org.openpermis.editor.policy.view.RoleHierarchyListTool
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleHierarchyListTool
0%
0/55
0%
0/20
2.091
 
 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 java.beans.PropertyChangeEvent;
 13  
 import java.beans.PropertyChangeListener;
 14  
 import java.net.URI;
 15  
 
 16  
 import javax.swing.JComponent;
 17  
 import javax.swing.JList;
 18  
 import javax.swing.JOptionPane;
 19  
 import javax.swing.JScrollPane;
 20  
 
 21  
 import org.jdesktop.application.Action;
 22  
 import org.jdesktop.application.ApplicationContext;
 23  
 import org.jdesktop.beansbinding.BeanProperty;
 24  
 import org.jdesktop.beansbinding.BindingGroup;
 25  
 import org.jdesktop.beansbinding.Bindings;
 26  
 import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
 27  
 import org.jdesktop.swingbinding.SwingBindings;
 28  
 import org.slf4j.Logger;
 29  
 import org.slf4j.LoggerFactory;
 30  
 
 31  
 import bibliothek.gui.dock.common.CLocation;
 32  
 
 33  
 import org.openpermis.editor.policy.adapter.AdapterTrader;
 34  
 import org.openpermis.editor.policy.adapter.overview.Overview;
 35  
 import org.openpermis.editor.policy.gui.DoubleClickForwarder;
 36  
 import org.openpermis.editor.policy.presenter.PolicyContext;
 37  
 import org.openpermis.editor.policy.presenter.RoleHierarchyListPresenter;
 38  
 import org.openpermis.editor.policy.renderer.RendererFactory;
 39  
 import org.openpermis.policy.bean.PolicyBean;
 40  
 import org.openpermis.policy.bean.RoleHierarchyBean;
 41  
 
 42  
 /**
 43  
  * Tool view that displays all role hierarchies in a policy.
 44  
  * @since 0.3.0
 45  
  */
 46  0
 public class RoleHierarchyListTool
 47  
         extends AbstractToolView<RoleHierarchyListPresenter>
 48  
         implements PropertyChangeListener
 49  
 {
 50  
 
 51  
         //---- Static
 52  
 
 53  
         /**
 54  
          * The logger object of this class.
 55  
          * @since 0.3.0
 56  
          */
 57  0
         private static final Logger LOGGER =
 58  
                 LoggerFactory.getLogger(RoleHierarchyListTool.class);
 59  
 
 60  
         /**
 61  
          * The default location for the role hierarchy list tool.
 62  
          * @since 0.3.0
 63  
          */
 64  0
         private static final CLocation DEFAULT_ROLE_HIERARCHY_LIST_LOCATION =
 65  
                 DEFAULT_LOCATION.south(0.6);
 66  
 
 67  
         //---- State
 68  
 
 69  
         /**
 70  
          * The list used to render the role hierarchies.
 71  
          * @since 0.3.0
 72  
          */
 73  
         private JList list;
 74  
 
 75  
         //---- Constructors
 76  
 
 77  
         /**
 78  
          * Creates an role hierarchies tool view.
 79  
          * @param context the application context used to lookup the action and resource map.
 80  
          * @since 0.3.0
 81  
          */
 82  
         public RoleHierarchyListTool (ApplicationContext context, AdapterTrader trader) {
 83  0
                 super(context, trader);
 84  0
         }
 85  
 
 86  
         //---- Actions
 87  
 
 88  
         /**
 89  
          * Command to add a new role hierarchy.
 90  
          * @since 0.3.0
 91  
          */
 92  
         @Action
 93  
         public void addHierarchy () {
 94  0
                 if (this.getPresenter() == null) {
 95  0
                         return;
 96  
                 }
 97  0
                 LOGGER.debug("addHierarchy");
 98  
 
 99  0
                 String identifier = (String) JOptionPane.showInputDialog(
 100  
                         null,
 101  
                         getResourceMap().getString("addHierarchy.Dialog.text"),
 102  
                         getResourceMap().getString("addHierarchy.Dialog.title"),
 103  
                         JOptionPane.PLAIN_MESSAGE,
 104  
                         null,
 105  
                         null,
 106  
                         ""
 107  
                 );
 108  
 
 109  
                 // Get uri.
 110  
                 URI uri;
 111  
                 try {
 112  0
                         uri = URI.create(identifier);
 113  0
                 } catch (Exception e) {
 114  0
                         uri = null;
 115  0
                 }
 116  0
                 if (uri != null) {
 117  0
                         this.getPresenter().addRoleHierarchy(uri);
 118  
                 }
 119  0
         }
 120  
 
 121  
         /**
 122  
          * Command to remove the active role hierarchy.
 123  
          * @since 0.3.0
 124  
          */
 125  
         @Action
 126  
         public void removeHierarchy () {
 127  0
                 if (this.getPresenter() == null) {
 128  0
                         return;
 129  
                 }
 130  0
                 LOGGER.debug("removeHierarchy");
 131  0
                 final RoleHierarchyBean roleHierarchy = this.getPresenter().getActive();
 132  0
                 if (roleHierarchy != null) {
 133  0
                         this.getPresenter().removeRoleHierarchy(roleHierarchy);
 134  
                 }
 135  0
         }
 136  
 
 137  
         /**
 138  
          * Command to edit the active role hierarchy.
 139  
          * @since 0.3.0
 140  
          */
 141  
         @Action
 142  
         public void editHierarchy () {
 143  0
                 if (this.getPresenter() == null) {
 144  0
                         return;
 145  
                 }
 146  0
                 final RoleHierarchyBean roleHierarchy = this.getPresenter().getActive();
 147  0
                 if (roleHierarchy != null) {
 148  0
                         editPart(roleHierarchy);
 149  
                 }
 150  0
         }
 151  
 
 152  
         //---- Tool
 153  
 
 154  
         /**
 155  
          * @since 0.3.0
 156  
          */
 157  
         public void refresh (PolicyBean policy, PolicyContext context) {
 158  0
                 if (policy == null) {
 159  0
                         setPresenter(null);
 160  
                 } else {
 161  0
                         setPresenter(new RoleHierarchyListPresenter(policy, context));
 162  
                 }
 163  0
         }
 164  
 
 165  
         //---- AbstractToolView
 166  
 
 167  
         /**
 168  
          * @since 0.3.0
 169  
          */
 170  
         @Override
 171  
         protected void updateActions () {
 172  0
                 super.updateActions();
 173  0
                 final boolean hasPresenter = this.getPresenter() != null;
 174  0
                 final boolean hasActive = hasPresenter && this.getPresenter().getActive() != null;
 175  0
                 getActionMap().get("addHierarchy").setEnabled(hasPresenter);
 176  0
                 getActionMap().get("removeHierarchy").setEnabled(hasActive);
 177  0
                 getActionMap().get("editHierarchy").setEnabled(hasActive);
 178  0
         }
 179  
 
 180  
         /**
 181  
          * @since 0.3.0
 182  
          */
 183  
         @Override
 184  
         protected void detachPresenter (RoleHierarchyListPresenter presenter) {
 185  0
                 super.detachPresenter(presenter);
 186  0
                 presenter.removePropertyChangeListener(this);
 187  0
         }
 188  
 
 189  
         /**
 190  
          * @since 0.3.0
 191  
          */
 192  
         @Override
 193  
         protected void attachPresenter (
 194  
                 RoleHierarchyListPresenter presenter, BindingGroup bindings
 195  
         ) {
 196  0
                 super.attachPresenter(presenter, bindings);
 197  0
                 presenter.addPropertyChangeListener(this);
 198  0
                 bindings.addBinding(
 199  
                         SwingBindings.createJListBinding(
 200  
                                 UpdateStrategy.READ,
 201  
                                 this.getPresenter().getRoleHierarchies(),
 202  
                                 this.list
 203  
                         )
 204  
                 );
 205  0
                 bindings.addBinding(
 206  
                         Bindings.createAutoBinding(
 207  
                                 UpdateStrategy.READ_WRITE,
 208  
                                 this.getPresenter(),
 209  
                                 BeanProperty.create("active"),
 210  
                                 this.list,
 211  
                                 BeanProperty.create("selectedElement")
 212  
                         )
 213  
                 );
 214  0
         }
 215  
 
 216  
         /**
 217  
          * @since 0.3.0
 218  
          */
 219  
         @Override
 220  
         public CLocation getDefaultLocation () {
 221  0
                 return DEFAULT_ROLE_HIERARCHY_LIST_LOCATION;
 222  
         }
 223  
 
 224  
         //---- View
 225  
 
 226  
         /**
 227  
          * @since 0.3.0
 228  
          */
 229  
         @Override
 230  
         public JComponent createContentPane () {
 231  0
                 this.list = new JList(new Object[0]);
 232  0
                 this.list.setCellRenderer(
 233  
                         RendererFactory.createListCellRenderer(this.getAdapteeTrader(), Overview.class)
 234  
                 );
 235  0
                 DoubleClickForwarder.register(this.list, getActionMap().get("editHierarchy"));
 236  0
                 return new JScrollPane(this.list);
 237  
         }
 238  
 
 239  
         //---- PropertyChangeListener
 240  
 
 241  
         /**
 242  
          * @since 0.3.0
 243  
          */
 244  
         public void propertyChange (PropertyChangeEvent event) {
 245  0
                 updateActions();
 246  0
         }
 247  
 
 248  
 }