Coverage Report - org.openpermis.editor.policy.gui.ComponentFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentFactory
0%
0/29
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.gui;
 11  
 
 12  
 import java.util.ArrayList;
 13  
 import java.util.List;
 14  
 
 15  
 import javax.swing.Action;
 16  
 import javax.swing.ActionMap;
 17  
 
 18  
 import org.jdesktop.application.ResourceMap;
 19  
 
 20  
 /**
 21  
  * Abstract factory for user interface components based on resource and action map keys.
 22  
  * @since 0.1.0
 23  
  */
 24  
 public abstract class ComponentFactory {
 25  
 
 26  
         //---- Static
 27  
         
 28  
         /**
 29  
          * Suffix for collection entries.
 30  
          * @since 0.1.0
 31  
          */
 32  
         private static final String ENTRIES = ".Entries";
 33  
 
 34  
         /**
 35  
          * Resource entry value for a tool separator entry.
 36  
          * @since 0.1.0
 37  
          */
 38  
         private static final String SEPARATOR = "-";
 39  
 
 40  
         /**
 41  
          * Resource entry value for a tool glue entry.
 42  
          * @since 0.1.0
 43  
          */
 44  
         private static final String GLUE = "~";
 45  
         
 46  
         /**
 47  
          * Resource entry start and stop characters for placeholders.
 48  
          * @since 0.1.0
 49  
          */
 50  0
         private static final char[] PLACEHOLDER = { '<', '>' };
 51  
 
 52  
         /**
 53  
          * Resource entry start and stop characters for composite entries.
 54  
          * @since 0.1.0
 55  
          */
 56  0
         private static final char[] COMPOSITE = { '[', ']' };
 57  
 
 58  
         //---- State
 59  
         
 60  
         /**
 61  
          * The action map attached to this factory instance.
 62  
          * @since 0.1.0
 63  
          */
 64  
         private final ActionMap actionMap;
 65  
         
 66  
         /**
 67  
          * The resource map attached to this factory instance.
 68  
          * @since 0.1.0
 69  
          */
 70  
         private final ResourceMap resourceMap;
 71  
         
 72  
         //---- Constructors
 73  
         
 74  
         /**
 75  
          * Creates an abstract factory that operates on the specified action and resource map.
 76  
          * @param actionMap the action map this factory operates on.
 77  
          * @param resourceMap the resource map this factory operates on.
 78  
          * @since 0.1.0
 79  
          */
 80  0
         protected ComponentFactory (ActionMap actionMap, ResourceMap resourceMap) {
 81  0
                 this.actionMap = actionMap;
 82  0
                 this.resourceMap = resourceMap;
 83  0
         }
 84  
         
 85  
         //---- Methods
 86  
         
 87  
         /**
 88  
          * Returns the action map this factory operates on.
 89  
          * @return the action map this factory operates on.
 90  
          * @since 0.1.0
 91  
          */
 92  
         protected ActionMap getActionMap () {
 93  0
                 return this.actionMap;
 94  
         }
 95  
         
 96  
         /**
 97  
          * Returns the resource map this factory operates on.
 98  
          * @return the resource map this factory operates on.
 99  
          * @since 0.1.0
 100  
          */
 101  
         protected ResourceMap getResourceMap () {
 102  0
                 return this.resourceMap;
 103  
         }
 104  
         
 105  
         /**
 106  
          * Returns a swing action with the specified name.
 107  
          * @param name the name of the swing action as it appears in the resource map.
 108  
          * @return the swing action requested.
 109  
          * @throws IllegalStateException if the action cannot be found.
 110  
          * @since 0.1.0
 111  
          */
 112  
         protected Action getAction (String name) {
 113  0
                 final Action action = getActionMap().get(name);
 114  0
                 if (action == null) {
 115  0
                         throw new IllegalStateException(
 116  
                                 "No action defined for resource key [" + name + "]."
 117  
                         );
 118  
                 }
 119  0
                 return action;
 120  
         }
 121  
 
 122  
         /**
 123  
          * Returns the entries of a collection resource key.
 124  
          * <p>Collection resource keys always have the suffix {@code .Entries}.</p>
 125  
          * @param name the name of the collection resource.
 126  
          * @return the entries requested, never <code>null</code>.
 127  
          * @since 0.1.0
 128  
          */
 129  
         protected String[] getEntries (String name) {
 130  0
                 final String entryList = getResourceMap().getString(unmaskComposite(name) + ENTRIES);
 131  0
                 if (entryList == null || entryList.length() == 0) {
 132  0
                         return new String[0];
 133  
                 }
 134  0
                 final List<String> entries = new ArrayList<String>();
 135  0
                 for (String entry : entryList.split(",")) {
 136  0
                         final String value = entry.trim();
 137  0
                         if (value.length() != 0) {
 138  0
                                 entries.add(value);
 139  
                         }
 140  
                 }
 141  0
                 return entries.toArray(new String[entries.size()]);
 142  
         }
 143  
 
 144  
         /**
 145  
          * Checks if the name specified is surrounded by the mask characters given.
 146  
          * @param name the name to check.
 147  
          * @param mask the mask.
 148  
          * @return {@code true} if the name is surrounded by the mask characters.
 149  
          * @since 0.1.0
 150  
          */
 151  
         private boolean isMaskedBy (String name, char[] mask) {
 152  0
                 return
 153  
                         mask.length == 2 &&
 154  
                         name.length() > mask.length && 
 155  
                         mask[0] == name.charAt(0) && 
 156  
                         mask[1] == name.charAt(name.length() - 1); 
 157  
         }
 158  
         
 159  
         /**
 160  
          * Checks if the resource entry is a special placeholder entry.
 161  
          * <p>Special entries are placeholder replaced at a later stage.</p>
 162  
          * @param name the name to check.
 163  
          * @return {@code true} if the entry is a placeholder.
 164  
          * @since 0.1.0
 165  
          */
 166  
         protected boolean isPlaceholder (String name) {
 167  0
                 return isMaskedBy(name, PLACEHOLDER);
 168  
         }
 169  
         
 170  
         /**
 171  
          * Unmasks a name composite name.
 172  
          * @param name the name to unmask if it is a {@link #isComposite(String)} name.
 173  
          * @return the unmasked name if it was a composite name, the unmodified name otherwise.
 174  
          * @since 0.1.0
 175  
          */
 176  
         protected String unmaskComposite (String name) {
 177  0
                 if (isComposite(name)) {
 178  0
                         return name.substring(1, name.length() - 1);
 179  
                 }
 180  0
                 return name;
 181  
         }
 182  
         
 183  
         /**
 184  
          * Checks if the resource entry is a special composite entry.
 185  
          * <p>Special entries are placeholder replaced at a later stage.</p>
 186  
          * @param name the name to check.
 187  
          * @return {@code true} if the entry is a placeholder.
 188  
          * @since 0.1.0
 189  
          */
 190  
         protected boolean isComposite (String name) {
 191  0
                 return isMaskedBy(name, COMPOSITE);
 192  
         }
 193  
         
 194  
         /**
 195  
          * Checks if a resource entry is a glue entry.
 196  
          * @param name the resource entry to check.
 197  
          * @return <code>true</code> if the entry denotes a glue entry,
 198  
          * <code>false</code> otherwise.
 199  
          * @since 0.1.0
 200  
          */
 201  
         protected boolean isGlue (String name) {
 202  0
                 return GLUE.equals(name);
 203  
         }
 204  
 
 205  
         /**
 206  
          * Checks if a resource entry is a separator entry.
 207  
          * @param name the resource entry to check.
 208  
          * @return <code>true</code> if the entry denotes a separator,
 209  
          * <code>false</code> otherwise.
 210  
          * @since 0.1.0
 211  
          */
 212  
         protected boolean isSeparator (String name) {
 213  0
                 return SEPARATOR.equals(name);
 214  
         }
 215  
         
 216  
 }