Coverage Report - org.openpermis.editor.policy.view.TargetEditor
 
Classes in this File Line Coverage Branch Coverage Complexity
TargetEditor
0%
0/80
0%
0/20
2.083
 
 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.util.List;
 13  
 
 14  
 import javax.swing.JComboBox;
 15  
 import javax.swing.JComponent;
 16  
 import javax.swing.JOptionPane;
 17  
 import javax.swing.JPanel;
 18  
 import javax.swing.JScrollPane;
 19  
 
 20  
 import org.jdesktop.application.Action;
 21  
 import org.jdesktop.application.ApplicationContext;
 22  
 import org.jdesktop.beansbinding.BeanProperty;
 23  
 import org.jdesktop.beansbinding.BindingGroup;
 24  
 import org.jdesktop.beansbinding.Bindings;
 25  
 import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
 26  
 import org.jdesktop.swingbinding.SwingBindings;
 27  
 import org.slf4j.Logger;
 28  
 import org.slf4j.LoggerFactory;
 29  
 
 30  
 import com.jgoodies.forms.layout.CellConstraints;
 31  
 import com.jgoodies.forms.layout.FormLayout;
 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.checklist.CheckList;
 36  
 import org.openpermis.editor.policy.presenter.TargetPresenter;
 37  
 import org.openpermis.editor.policy.renderer.RendererFactory;
 38  
 import org.openpermis.policy.bean.ActionBean;
 39  
 import org.openpermis.policy.bean.DomainBean;
 40  
 import org.openpermis.policy.bean.PolicyBean;
 41  
 import org.openpermis.policy.bean.TargetBean;
 42  
 
 43  
 /**
 44  
  * Editor for a {@link TargetPresenter}.
 45  
  * @since 0.1.0
 46  
  */
 47  
 public class TargetEditor
 48  
         extends AbstractEditor<TargetPresenter>
 49  
 {
 50  
 
 51  
         //---- Static
 52  
 
 53  
         /**
 54  
          * The logger object of this class.
 55  
          * @since 0.1.0
 56  
          */
 57  0
         private static final Logger LOGGER =
 58  
                 LoggerFactory.getLogger(TargetEditor.class);
 59  
 
 60  
         //---- State
 61  
 
 62  
         /**
 63  
          * Chooser for the resource domain of the target.
 64  
          * @since 0.1.0
 65  
          */
 66  
         private JComboBox resourceDomain;
 67  
 
 68  
         /**
 69  
          * The list of actions in the target.
 70  
          * @since 0.1.0
 71  
          */
 72  
         private CheckList<ActionBean> actionList;
 73  
 
 74  
         //---- Constructors
 75  
 
 76  
         /**
 77  
          * Creates a new editor view.
 78  
          * @param context the application context used to lookup the action and resource map.
 79  
          * @param presenter the presenter of this view.
 80  
          * @since 0.1.0
 81  
          */
 82  
         public TargetEditor (
 83  
                 ApplicationContext context, AdapterTrader trader, TargetPresenter presenter
 84  
         ) {
 85  0
                 super(context, trader, presenter);
 86  0
         }
 87  
 
 88  
         //---- Actions
 89  
 
 90  
         /**
 91  
          * @since 0.1.0
 92  
          */
 93  
         @Action
 94  
         public void addAction () {
 95  0
                 LOGGER.debug("addAction");
 96  
 
 97  
                 // first check if a action with empty name exists
 98  0
                 List<ActionBean> list = this.getPresenter().getActionPool().getPoolList();
 99  0
                 for (ActionBean action : list) {
 100  0
                         if (action.getName().equals("")) {
 101  0
                                 JOptionPane.showMessageDialog(
 102  
                                         null,
 103  
                                         getResourceMap().getString("defineActionNameMessage"),
 104  
                                         getResourceMap().getString("defineActionNameTitle"),
 105  
                                         JOptionPane.WARNING_MESSAGE);
 106  0
                                 return;
 107  
                         }
 108  
                 }
 109  
 
 110  
                 // new action
 111  0
                 getPresenter().addAction(
 112  
                         new EditPartCommand<ActionBean>(getViewContext()));
 113  0
         }
 114  
 
 115  
         /**
 116  
          * @since 0.1.0
 117  
          */
 118  
         @Action
 119  
         public void addResourceDomain () {
 120  0
                 LOGGER.debug("addResourceDomain");
 121  0
                 getPresenter().addResourceDomain(new EditPartCommand<DomainBean>(getViewContext()));
 122  0
         }
 123  
 
 124  
         /**
 125  
          * @since 0.1.0
 126  
          */
 127  
         @Action
 128  
         public void removeResourceDomain () {
 129  0
                 LOGGER.debug("removeResourceDomain");
 130  0
                 DomainBean activeResourceDomain = this.getPresenter().getActiveResourceDomain();
 131  0
                 if (activeResourceDomain != null) {
 132  0
                         PolicyBean policy = this.getPresenter().getContext().getPolicyBean();
 133  0
                         List<TargetBean> parents = policy.getParentTargets(activeResourceDomain);
 134  0
                         if ((parents.size() > 1)
 135  
                                 && showYesNoDialog("nRefsTitle", "nRefs", JOptionPane.NO_OPTION)) {
 136  0
                                 return;
 137  
                         }
 138  0
                         getPresenter().removeActiveResourceDomain(activeResourceDomain);
 139  0
                 } else {
 140  0
                         LOGGER.debug("no part selected");
 141  
                 }
 142  0
         }
 143  
 
 144  
         /**
 145  
          * @since 0.1.0
 146  
          */
 147  
         @Action
 148  
         public void removeAction () {
 149  0
                 LOGGER.debug("removeAction");
 150  0
                 ActionBean activeAction = this.actionList.getActivePart();
 151  0
                 if (activeAction != null) {
 152  0
                         PolicyBean policy = this.getPresenter().getContext().getPolicyBean();
 153  0
                         List<TargetBean> parents = policy.getParentTargets(this.actionList.getActivePart());
 154  0
                         if ((parents.size() > 1)
 155  
                                 && showYesNoDialog("nRefsTitle", "nRefs", JOptionPane.NO_OPTION)) {
 156  0
                                 return;
 157  
                         }
 158  0
                         getPresenter().removeActiveAction(activeAction);
 159  0
                 } else {
 160  0
                         LOGGER.debug("no part selected");
 161  
                 }
 162  0
         }
 163  
 
 164  
         /**
 165  
          * @since 0.1.0
 166  
          */
 167  
         @Action
 168  
         public void editResourceDomain () {
 169  0
                 LOGGER.debug("editResourceDomain");
 170  0
                 final DomainBean active = getPresenter().getActiveResourceDomain();
 171  0
                 if (active != null) {
 172  0
                         editPart(active);
 173  
                 }
 174  0
         }
 175  
 
 176  
         /**
 177  
          * @since 0.1.0
 178  
          */
 179  
         @Action
 180  
         public void editAction () {
 181  0
                 LOGGER.debug("editAction");
 182  0
                 final ActionBean active =
 183  
                         (ActionBean) this.actionList.getValueAt(
 184  
                                 this.actionList.getSelectionModel().getAnchorSelectionIndex(), 1
 185  
                         );
 186  0
                 if (active != null) {
 187  0
                         editPart(active);
 188  
                 }
 189  0
         }
 190  
 
 191  
         /**
 192  
          * @since 0.1.0
 193  
          */
 194  
         @Action
 195  
         public void close () {
 196  0
                 LOGGER.debug("close");
 197  0
                 closeView(this, false);
 198  0
         }
 199  
 
 200  
         //---- AbstractEditor
 201  
 
 202  
         /**
 203  
          * @since 0.1.0
 204  
          */
 205  
         @Override
 206  
         protected void bind (BindingGroup bindings) {
 207  0
                 bindings.addBinding(
 208  
                         SwingBindings.createJComboBoxBinding(
 209  
                                 UpdateStrategy.READ,
 210  
                                 getPresenter().getResourceDomainPool().getPoolList(),
 211  
                                 this.resourceDomain
 212  
                         )
 213  
                 );
 214  0
                 bindings.addBinding(
 215  
                         Bindings.createAutoBinding(
 216  
                                 UpdateStrategy.READ_WRITE,
 217  
                                 getPresenter(),
 218  
                                 BeanProperty.create("activeResourceDomain"),
 219  
                                 this.resourceDomain,
 220  
                                 BeanProperty.create("selectedItem")
 221  
                         )
 222  
                 );
 223  0
                 this.actionList.bind(
 224  
                         getPresenter().getActionPool().getPoolList(),
 225  
                         getPresenter().getActions()
 226  
                 );
 227  0
         }
 228  
 
 229  
         //---- View
 230  
 
 231  
         /**
 232  
          * @since 0.1.0
 233  
          */
 234  
         private JComponent resourceDomainChooser () {
 235  0
                 final FormLayout layout = new FormLayout(
 236  
                         "pref, 2dlu, fill:50dlu:grow, 2dlu, pref",
 237  
                         "pref"
 238  
                 );
 239  0
                 final JPanel panel = new JPanel();
 240  0
                 panel.setLayout(layout);
 241  0
                 final CellConstraints cc = new CellConstraints();
 242  0
                 panel.add(label("resourceDomain"), cc.xy(1, 1));
 243  0
                 panel.add(this.resourceDomain, cc.xy(3, 1));
 244  0
                 panel.add(toolBar("resourceDomainToolBar"), cc.xy(5, 1));
 245  0
                 return panel;
 246  
         }
 247  
 
 248  
         /**
 249  
          * @since 0.1.0
 250  
          */
 251  
         private JComponent actionList () {
 252  0
                 final FormLayout layout = new FormLayout(
 253  
                         "pref, fill:pref:grow",
 254  
                         "pref, fill:pref:grow"
 255  
                 );
 256  0
                 final JPanel panel = new JPanel();
 257  0
                 panel.setLayout(layout);
 258  0
                 final CellConstraints cc = new CellConstraints();
 259  0
                 panel.add(label("actions"), cc.xy(1, 1));
 260  0
                 panel.add(toolBar("actionsToolBar"), cc.xy(2, 1));
 261  0
                 this.actionList.setVisibleRowCount(5);
 262  0
                 panel.add(new JScrollPane(this.actionList), cc.xyw(1, 2, 2));
 263  0
                 return panel;
 264  
         }
 265  
 
 266  
         /**
 267  
          * @since 0.1.0
 268  
          */
 269  
         public void fillContentPane (JPanel panel) {
 270  0
                 final CellConstraints cc = new CellConstraints();
 271  0
                 this.resourceDomain = new JComboBox();
 272  0
                 this.resourceDomain.setRenderer(
 273  
                         RendererFactory.createListCellRenderer(this.getAdapteeTrader(), Overview.class));
 274  
                 
 275  0
                 this.actionList = new CheckList<ActionBean>();
 276  0
                 this.actionList.setItemRenderer(
 277  
                         RendererFactory.createTableCellRenderer(this.getAdapteeTrader(), Overview.class)
 278  
                 );
 279  0
                 this.actionList.setDoubleClickAction(getActionMap().get("editAction"));
 280  0
                 this.actionList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
 281  
         
 282  0
                 panel.add(resourceDomainChooser(), cc.xy(2, 2));
 283  0
                 panel.add(actionList(), cc.xy(2, 4));
 284  0
         }
 285  
 }
 286  
 
 287