Coverage Report - org.openpermis.editor.policy.view.RoleAssignmentRuleListTool
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleAssignmentRuleListTool
0%
0/50
0%
0/18
1.857
RoleAssignmentRuleListTool$1
N/A
N/A
1.857
RoleAssignmentRuleListTool$Renderer
0%
0/21
0%
0/4
1.857
 
 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.awt.Component;
 13  
 import java.beans.PropertyChangeEvent;
 14  
 import java.beans.PropertyChangeListener;
 15  
 
 16  
 import javax.swing.DefaultListCellRenderer;
 17  
 import javax.swing.Icon;
 18  
 import javax.swing.JComponent;
 19  
 import javax.swing.JList;
 20  
 import javax.swing.JScrollPane;
 21  
 import javax.swing.ListSelectionModel;
 22  
 import javax.swing.SwingConstants;
 23  
 
 24  
 import org.jdesktop.application.Action;
 25  
 import org.jdesktop.application.ApplicationContext;
 26  
 import org.jdesktop.beansbinding.BeanProperty;
 27  
 import org.jdesktop.beansbinding.BindingGroup;
 28  
 import org.jdesktop.beansbinding.Bindings;
 29  
 import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
 30  
 import org.jdesktop.swingbinding.SwingBindings;
 31  
 import org.slf4j.Logger;
 32  
 import org.slf4j.LoggerFactory;
 33  
 
 34  
 import bibliothek.gui.dock.common.CLocation;
 35  
 
 36  
 import org.openpermis.editor.policy.adapter.AdapterTrader;
 37  
 import org.openpermis.editor.policy.gui.DoubleClickForwarder;
 38  
 import org.openpermis.editor.policy.presenter.PolicyContext;
 39  
 import org.openpermis.editor.policy.presenter.RoleAssignmentRuleListPresenter;
 40  
 import org.openpermis.policy.bean.PolicyBean;
 41  
 import org.openpermis.policy.bean.RoleAssignmentRuleBean;
 42  
 
 43  
 /**
 44  
  * Tool view that displays all role assignment rules in a policy.
 45  
  * @since 0.3.0
 46  
  */
 47  0
 public class RoleAssignmentRuleListTool
 48  
         extends AbstractToolView<RoleAssignmentRuleListPresenter>
 49  
         implements PropertyChangeListener
 50  
 {
 51  
 
 52  
         //---- Static
 53  
 
 54  
         /**
 55  
          * The logger object of this class.
 56  
          * @since 0.3.0
 57  
          */
 58  0
         private static final Logger LOGGER =
 59  
                 LoggerFactory.getLogger(RoleAssignmentRuleListTool.class);
 60  
 
 61  
         /**
 62  
          * The default location for this tool.
 63  
          * @since 0.3.0
 64  
          */
 65  0
         private static final CLocation DEFAULT_ROLE_ASSIGNMENT_RULE_LIST_LOCATION =
 66  
                 DEFAULT_LOCATION.south(0.4);
 67  
 
 68  
         //---- State
 69  
 
 70  
         /**
 71  
          * The list used to render the role assignment rules.
 72  
          * @since 0.3.0
 73  
          */
 74  
         private JList list;
 75  
 
 76  
         //---- Constructors
 77  
 
 78  
         /**
 79  
          * Creates an role assignment rules tool view.
 80  
          * @param context the application context used to lookup the action and resource map.
 81  
          * @since 0.3.0
 82  
          */
 83  
         public RoleAssignmentRuleListTool (ApplicationContext context, AdapterTrader trader) {
 84  0
                 super(context, trader);
 85  0
         }
 86  
 
 87  
         //---- Actions
 88  
 
 89  
         /**
 90  
          * Command to add a new role assignment rule.
 91  
          * @since 0.3.0
 92  
          */
 93  
         @Action
 94  
         public void addRule () {
 95  0
                 if (this.getPresenter() == null) {
 96  0
                         return;
 97  
                 }
 98  0
                 LOGGER.debug("addRule");
 99  0
                 this.getPresenter().addRoleAssignmentRule();
 100  0
         }
 101  
 
 102  
 
 103  
         /**
 104  
          * Command to remove the active role assignment rule.
 105  
          * @since 0.3.0
 106  
          */
 107  
         @Action
 108  
         public void removeRule () {
 109  0
                 if (this.getPresenter() == null) {
 110  0
                         return;
 111  
                 }
 112  0
                 LOGGER.debug("removeRule");
 113  0
                 final RoleAssignmentRuleBean roleAssignmentRule = this.getPresenter().getActive();
 114  0
                 if (roleAssignmentRule != null) {
 115  0
                         this.getPresenter().removeRoleAssignmentRule(roleAssignmentRule);
 116  
                 }
 117  0
         }
 118  
 
 119  
         /**
 120  
          * Command to edit the active role assignment rule.
 121  
          * @since 0.3.0
 122  
          */
 123  
         @Action
 124  
         public void editRule () {
 125  0
                 if (this.getPresenter() == null) {
 126  0
                         return;
 127  
                 }
 128  0
                 final RoleAssignmentRuleBean roleAssignmentRule = this.getPresenter().getActive();
 129  0
                 if (roleAssignmentRule != null) {
 130  0
                         editPart(roleAssignmentRule);
 131  
                 }
 132  0
         }
 133  
 
 134  
         //---- Tool
 135  
 
 136  
         /**
 137  
          * @since 0.3.0
 138  
          */
 139  
         public void refresh (PolicyBean policy, PolicyContext context) {
 140  0
                 if (policy == null) {
 141  0
                         setPresenter(null);
 142  
                 } else {
 143  0
                         setPresenter(new RoleAssignmentRuleListPresenter(policy, context));
 144  
                 }
 145  0
         }
 146  
 
 147  
         //---- AbstractToolView
 148  
 
 149  
         /**
 150  
          * @since 0.3.0
 151  
          */
 152  
         @Override
 153  
         protected void updateActions () {
 154  0
                 super.updateActions();
 155  0
                 final boolean hasPresenter = this.getPresenter() != null;
 156  0
                 final boolean hasActive = hasPresenter && this.getPresenter().getActive() != null;
 157  0
                 getActionMap().get("addRule").setEnabled(hasPresenter);
 158  0
                 getActionMap().get("removeRule").setEnabled(hasActive);
 159  0
                 getActionMap().get("editRule").setEnabled(hasActive);
 160  0
         }
 161  
 
 162  
         /**
 163  
          * @since 0.3.0
 164  
          */
 165  
         @Override
 166  
         protected void detachPresenter (RoleAssignmentRuleListPresenter presenter) {
 167  0
                 super.detachPresenter(presenter);
 168  0
                 presenter.removePropertyChangeListener(this);
 169  0
         }
 170  
 
 171  
         /**
 172  
          * @since 0.3.0
 173  
          */
 174  
         @Override
 175  
         protected void attachPresenter (
 176  
                 RoleAssignmentRuleListPresenter presenter, BindingGroup bindings
 177  
         ) {
 178  0
                 super.attachPresenter(presenter, bindings);
 179  0
                 presenter.addPropertyChangeListener(this);
 180  0
                 bindings.addBinding(
 181  
                         SwingBindings.createJListBinding(
 182  
                                 UpdateStrategy.READ,
 183  
                                 this.getPresenter().getRoleAssignmentRules(),
 184  
                                 this.list
 185  
                         )
 186  
                 );
 187  0
                 bindings.addBinding(
 188  
                         Bindings.createAutoBinding(
 189  
                                 UpdateStrategy.READ_WRITE,
 190  
                                 this.getPresenter(),
 191  
                                 BeanProperty.create("active"),
 192  
                                 this.list,
 193  
                                 BeanProperty.create("selectedElement")
 194  
                         )
 195  
                 );
 196  0
         }
 197  
 
 198  
         /**
 199  
          * @since 0.3.0
 200  
          */
 201  
         @Override
 202  
         public CLocation getDefaultLocation () {
 203  0
                 return DEFAULT_ROLE_ASSIGNMENT_RULE_LIST_LOCATION;
 204  
         }
 205  
 
 206  
         //---- View
 207  
 
 208  
         /**
 209  
          * @since 0.3.0
 210  
          */
 211  
         @Override
 212  
         public JComponent createContentPane () {
 213  0
                 this.list = new JList(new Object[0]);
 214  0
                 this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 215  0
                 this.list.setCellRenderer(new Renderer());
 216  0
                 DoubleClickForwarder.register(this.list, getActionMap().get("editRule"));
 217  0
                 return new JScrollPane(this.list);
 218  
         }
 219  
 
 220  
         //---- PropertyChangeListener
 221  
 
 222  
         /**
 223  
          * @since 0.3.0
 224  
          */
 225  
         public void propertyChange (PropertyChangeEvent event) {
 226  0
                 updateActions();
 227  0
         }
 228  
 
 229  
         //---- Renderer
 230  
 
 231  
         /**
 232  
          * Renderer for role assignment rules.
 233  
          * @since 0.3.0
 234  
          */
 235  0
         private final class Renderer
 236  
                 extends DefaultListCellRenderer
 237  
         {
 238  
 
 239  
                 //---- Static
 240  
                 
 241  
                 /**
 242  
                  * @since 0.3.0
 243  
                  */
 244  
                 private static final long serialVersionUID = 5561279544864812361L;
 245  
 
 246  
                 //---- State
 247  
 
 248  
                 /**
 249  
                  * @since 0.3.0
 250  
                  */
 251  
                 private final Icon icon;
 252  
 
 253  
                 //---- Constructors
 254  
 
 255  
                 /**
 256  
                  * @since 0.3.0
 257  
                  */
 258  0
                 private Renderer () {
 259  0
                         this.icon = RoleAssignmentRuleListTool.this.getIcon();
 260  0
                         setVerticalTextPosition(SwingConstants.TOP);
 261  0
                 }
 262  
 
 263  
                 //---- ListCellRenderer
 264  
 
 265  
                 /**
 266  
                  * @since 0.3.0
 267  
                  */
 268  
                 @Override
 269  
                 public Component getListCellRendererComponent (
 270  
                         JList source, Object value, int index, boolean selected, boolean focussed
 271  
                 ) {
 272  0
                         final RoleAssignmentRuleBean role = (RoleAssignmentRuleBean) value;
 273  
 
 274  0
                         final StringBuilder accessHierarchyLines = new StringBuilder();
 275  0
                         accessHierarchyLines.append(
 276  
                                 tag(
 277  
                                         getResourceMap().getString("roleAssignmentRule")
 278  
                                                 + " \"" + role.getSerialNumber() + "\"",
 279  
                                                 "tr",
 280  
                                                 "td colspan= \"2\"", "i", "u", "b"
 281  
                                 )
 282  
                         );
 283  
 
 284  0
                         final StringBuilder outerMostLines = new StringBuilder();
 285  0
                         outerMostLines.append(
 286  
                                 tag(accessHierarchyLines.toString(), "html", "table border=\"0\"")
 287  
                         );
 288  
 
 289  0
                         setToolTipText(role.toString());
 290  0
                         super.getListCellRendererComponent(
 291  
                                 source, outerMostLines.toString(), index, selected, focussed
 292  
                         );
 293  0
                         setIcon(this.icon);
 294  0
                         return this;
 295  
                 }
 296  
 
 297  
                 /**
 298  
                  * @since 0.3.0
 299  
                  */
 300  
                 private String tag (final String line, final String... tags) {
 301  0
                         final StringBuilder sb = new StringBuilder();
 302  0
                         for (String s : tags) {
 303  0
                                 sb.append("<" + s + ">");
 304  
                         }
 305  0
                         sb.append(line);
 306  0
                         for (int i = tags.length - 1; i >= 0; i--) {
 307  0
                                 sb.append("</" + tags[i] + ">");
 308  
                         }
 309  0
                         return sb.toString();
 310  
                 }
 311  
 
 312  
         }
 313  
 
 314  
 }