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