[919] | 1 | /**
|
---|
| 2 | * AttributeUtils.java
|
---|
| 3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[4] | 19 | package org.expeditee.gui;
|
---|
| 20 |
|
---|
[1415] | 21 | import java.lang.reflect.Field;
|
---|
[4] | 22 | import java.lang.reflect.InvocationTargetException;
|
---|
| 23 | import java.lang.reflect.Method;
|
---|
| 24 | import java.util.HashMap;
|
---|
| 25 | import java.util.LinkedList;
|
---|
| 26 | import java.util.List;
|
---|
| 27 |
|
---|
[1102] | 28 | import org.expeditee.core.Colour;
|
---|
| 29 | import org.expeditee.core.Font;
|
---|
| 30 | import org.expeditee.core.Point;
|
---|
[1374] | 31 | import org.expeditee.encryption.items.EncryptionPermissionTriple;
|
---|
[4] | 32 | import org.expeditee.io.Conversion;
|
---|
[427] | 33 | import org.expeditee.items.DotType;
|
---|
[4] | 34 | import org.expeditee.items.Item;
|
---|
[97] | 35 | import org.expeditee.items.Justification;
|
---|
[1402] | 36 | import org.expeditee.items.PermissionTriple;
|
---|
[4] | 37 | import org.expeditee.items.Text;
|
---|
[583] | 38 | import org.expeditee.simple.IncorrectTypeException;
|
---|
[4] | 39 |
|
---|
| 40 | /**
|
---|
| 41 | * This class provides the methods to extract and set attributes of Items and
|
---|
| 42 | * Frames. These methods are called when a user merges a text item with
|
---|
| 43 | * <code>Attribute: Value</code> pairs.
|
---|
| 44 | *
|
---|
| 45 | * @author jdm18
|
---|
| 46 | *
|
---|
| 47 | */
|
---|
| 48 | public class AttributeUtils {
|
---|
| 49 |
|
---|
[583] | 50 | public static final class Attribute {
|
---|
| 51 | public final String displayName;
|
---|
| 52 | public final Method getter;
|
---|
| 53 | public final Method setter;
|
---|
| 54 |
|
---|
| 55 | public Attribute(String displayName, Method getter, Method setter) {
|
---|
| 56 | this.displayName = displayName;
|
---|
| 57 | this.getter = getter;
|
---|
| 58 | this.setter = setter;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public static final class AttributeSet {
|
---|
| 63 |
|
---|
| 64 | // the internal hashmap
|
---|
[601] | 65 | private final HashMap<String, Attribute> map;
|
---|
[583] | 66 | // a list of keys in the order they were added (used to make attribute extraction consistent)
|
---|
[601] | 67 | public final List<String> keys;
|
---|
[583] | 68 |
|
---|
[601] | 69 | public AttributeSet(int size) {
|
---|
| 70 | map = new HashMap<String, Attribute>(size);
|
---|
| 71 | keys = new LinkedList<String>();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[583] | 74 | public void put(String attributeName, Method getter, Method setter) {
|
---|
| 75 | if(map.containsKey(attributeName.toLowerCase())) {
|
---|
| 76 | System.err.println(this + " already contains key '" + attributeName + "', overwriting value!");
|
---|
| 77 | } else {
|
---|
| 78 | // we keep an ordered list of attributes for extraction
|
---|
| 79 | keys.add(attributeName.toLowerCase());
|
---|
| 80 | }
|
---|
| 81 | map.put(attributeName.toLowerCase(), new Attribute(attributeName, getter, setter));
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[601] | 84 | // Create a second reference the the same Attribute, using a different name
|
---|
| 85 | // Does not modify the list of keys
|
---|
[583] | 86 | public void alias(String alias, String name) {
|
---|
| 87 | if(map.containsKey(name.toLowerCase())) {
|
---|
| 88 | map.put(alias.toLowerCase(), map.get(name.toLowerCase()));
|
---|
| 89 | } else {
|
---|
| 90 | System.err.println("Cannot add alias '" + alias + "', because key '" + name + "' does not exist!");
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public boolean containsKey(String key) {
|
---|
| 95 | return map.containsKey(key);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public Attribute get(String key) {
|
---|
| 99 | return map.get(key);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[601] | 103 | public static final AttributeSet _Attrib = new AttributeSet(128);
|
---|
| 104 | public static final AttributeSet _FrameAttrib = new AttributeSet(16);
|
---|
[4] | 105 |
|
---|
| 106 |
|
---|
[583] | 107 | // List of attributes which are ignored when extracting attributes
|
---|
| 108 | private static List<String> _IgnoreGet = null;
|
---|
| 109 | // List of attributes which are ignored when setting attributes,
|
---|
| 110 | // if multiple attributes are being set at once
|
---|
| 111 | private static List<String> _IgnoreSet = null;
|
---|
[4] | 112 |
|
---|
[214] | 113 | /***************************************************************************
|
---|
| 114 | * List of method names to show in extraced lists even when they return null
|
---|
| 115 | * (Null is often used to indicate the default value is used)
|
---|
| 116 | **************************************************************************/
|
---|
[4] | 117 | private static List<Method> _AllowNull = null;
|
---|
| 118 |
|
---|
| 119 | // private static HashMap<String, String> _Abbreviations = null;
|
---|
[583] | 120 |
|
---|
| 121 | public static void ensureReady() {
|
---|
| 122 | if(_IgnoreSet == null) {
|
---|
| 123 | initLists();
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[4] | 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Initialises the _Ignore and _AllowNull lists.
|
---|
| 129 | */
|
---|
| 130 | private static void initLists() {
|
---|
| 131 |
|
---|
| 132 | try {
|
---|
[583] | 133 |
|
---|
| 134 | Class<?>[] pPoint = { Point.class };
|
---|
| 135 | Class<?>[] pString = { String.class };
|
---|
| 136 | Class<?>[] pInt = { int.class };
|
---|
| 137 | Class<?>[] pIntO = { Integer.class };
|
---|
| 138 | Class<?>[] pFloat = { float.class };
|
---|
| 139 | Class<?>[] pFloatO = { Float.class };
|
---|
[1102] | 140 | Class<?>[] pDouble = {double.class };
|
---|
| 141 | Class<?>[] pColor = { Colour.class };
|
---|
[583] | 142 | Class<?>[] pBool = { boolean.class };
|
---|
| 143 | //Class[] pDouble = { double.class };
|
---|
| 144 | //Class[] pDoubleO = { Double.class };
|
---|
| 145 | Class<?>[] pArrow = { float.class, double.class, double.class };
|
---|
| 146 | Class<?>[] pList = { List.class };
|
---|
| 147 | Class<?>[] pIntArray = { int[].class };
|
---|
| 148 | Class<?>[] pJustification = { Justification.class };
|
---|
[1402] | 149 | Class<?>[] pPermission = { PermissionTriple.class };
|
---|
[583] | 150 | Class<?>[] pDotType = { DotType.class };
|
---|
[1374] | 151 | Class<?>[] pEncPermission = { EncryptionPermissionTriple.class };
|
---|
[583] | 152 |
|
---|
| 153 | _IgnoreSet = new LinkedList<String>();
|
---|
| 154 | _IgnoreGet = new LinkedList<String>();
|
---|
| 155 | _AllowNull = new LinkedList<Method>();
|
---|
| 156 |
|
---|
[97] | 157 | // TODO load these in with reflection...
|
---|
| 158 | // Set the shortcuts with annotation tags on the methods
|
---|
[583] | 159 | _IgnoreSet.add("date");
|
---|
| 160 | _IgnoreSet.add("datecreated");
|
---|
| 161 | _IgnoreSet.add("d");
|
---|
| 162 | _IgnoreSet.add("link");
|
---|
| 163 | _IgnoreSet.add("l");
|
---|
| 164 | _IgnoreSet.add("action");
|
---|
| 165 | _IgnoreSet.add("a");
|
---|
| 166 | _IgnoreSet.add("position");
|
---|
| 167 | _IgnoreSet.add("pos");
|
---|
| 168 | _IgnoreSet.add("p");
|
---|
| 169 | _IgnoreSet.add("x");
|
---|
| 170 | _IgnoreSet.add("y");
|
---|
| 171 |
|
---|
| 172 | _IgnoreGet.add("x");
|
---|
| 173 | _IgnoreGet.add("y");
|
---|
| 174 | _IgnoreGet.add("text");
|
---|
| 175 | _IgnoreGet.add("gradientangle");
|
---|
[4] | 176 |
|
---|
[583] | 177 | _AllowNull.add(Item.class.getMethod("getColor"));
|
---|
| 178 | _AllowNull.add(Item.class.getMethod("getBackgroundColor"));
|
---|
[71] | 179 |
|
---|
[583] | 180 | _AllowNull.add(Frame.class.getMethod("getBackgroundColor"));
|
---|
| 181 | _AllowNull.add(Frame.class.getMethod("getForegroundColor"));
|
---|
[4] | 182 |
|
---|
[583] | 183 | /*
|
---|
| 184 | * Populate the backing lists of attributes
|
---|
| 185 | */
|
---|
[427] | 186 |
|
---|
[1505] | 187 | // Standard Frame attributes
|
---|
[583] | 188 | _FrameAttrib.put("Permission", Frame.class.getMethod("getPermission"),
|
---|
| 189 | Frame.class.getMethod("setPermission", pPermission));
|
---|
| 190 | _FrameAttrib.put("Owner", Frame.class.getMethod("getOwner"),
|
---|
| 191 | Frame.class.getMethod("setOwner", pString));
|
---|
| 192 | _FrameAttrib.put("DateCreated", Frame.class.getMethod("getDateCreated"),
|
---|
[601] | 193 | null);
|
---|
[583] | 194 | _FrameAttrib.put("LastModifyUser", Frame.class.getMethod("getLastModifyUser"),
|
---|
[601] | 195 | null);
|
---|
[583] | 196 | _FrameAttrib.put("LastModifyDate", Frame.class.getMethod("getLastModifyDate"),
|
---|
[601] | 197 | null);
|
---|
[583] | 198 | _FrameAttrib.put("ForegroundColor", Frame.class.getMethod("getForegroundColor"),
|
---|
| 199 | Frame.class.getMethod("setForegroundColor", pColor));
|
---|
| 200 | _FrameAttrib.put("BackgroundColor", Frame.class.getMethod("getBackgroundColor"),
|
---|
| 201 | Frame.class.getMethod("setBackgroundColor", pColor));
|
---|
[1405] | 202 | _FrameAttrib.put("Group", Frame.class.getMethod("getGroup"),
|
---|
| 203 | Frame.class.getMethod("setGroup", pString));
|
---|
[427] | 204 |
|
---|
[1505] | 205 | // Encryption Frame attributes
|
---|
[1506] | 206 | _FrameAttrib.put("FrameEncryptionLabel", Frame.class.getMethod("getFrameEncryptionLabel"),
|
---|
| 207 | Frame.class.getMethod("setFrameEncryptionLabel", pString));
|
---|
| 208 | _FrameAttrib.put("EncryptionFramePermission", Frame.class.getMethod("getFrameEncryptionPermission"),
|
---|
| 209 | Frame.class.getMethod("setFrameEncryptionPermission", pEncPermission));
|
---|
| 210 | _FrameAttrib.put("HomogeneousEncryptionLabel", Frame.class.getMethod("getHomogeneousEncryptionLabel"),
|
---|
| 211 | Frame.class.getMethod("setHomogeneousEncryptionLabel", pString));
|
---|
[1509] | 212 | _FrameAttrib.put("ItemEncryptionPermission", Frame.class.getMethod("getEncryptionPermission"),
|
---|
[1506] | 213 | Frame.class.getMethod("setEncryptionPermission", pEncPermission));
|
---|
| 214 | _FrameAttrib.put("HetrogeneousEncryptionLabels", Frame.class.getMethod("getHetrogeneousFrameOwnerLabels"),
|
---|
[1508] | 215 | null);
|
---|
[1227] | 216 |
|
---|
[1505] | 217 | // aliases for Standard frame attribute settings
|
---|
[1227] | 218 | _FrameAttrib.alias("fgc", "foregroundcolor");
|
---|
| 219 | _FrameAttrib.alias("bgc", "backgroundcolor");
|
---|
| 220 | _FrameAttrib.alias("p", "permission");
|
---|
[427] | 221 |
|
---|
[1505] | 222 | // aliases for Encryption frame attribute settings
|
---|
| 223 | _FrameAttrib.alias("encframelabel", "FrameEncryptionLabel");
|
---|
[1506] | 224 | _FrameAttrib.alias("EncryptionFrameLabel", "FrameEncryptionLabel");
|
---|
[1505] | 225 | _FrameAttrib.alias("encframeperm", "EncryptionFramePermission");
|
---|
| 226 | _FrameAttrib.alias("homoenclabel", "HomogeneousEncryptionLabel");
|
---|
[1509] | 227 | _FrameAttrib.alias("encperm", "ItemEncryptionPermission");
|
---|
[427] | 228 |
|
---|
[583] | 229 | // Generic Items
|
---|
| 230 | _Attrib.put("DateCreated", Item.class.getMethod("getDateCreated"),
|
---|
| 231 | Item.class.getMethod("setDateCreated", pString));
|
---|
| 232 | _Attrib.put("Color", Item.class.getMethod("getColor"),
|
---|
| 233 | Item.class.getMethod("setColor", pColor));
|
---|
| 234 | _Attrib.put("BackgroundColor", Item.class.getMethod("getBackgroundColor"),
|
---|
| 235 | Item.class.getMethod("setBackgroundColor", pColor));
|
---|
| 236 | _Attrib.put("BorderColor", Item.class.getMethod("getBorderColor"),
|
---|
| 237 | Item.class.getMethod("setBorderColor", pColor));
|
---|
[720] | 238 | _Attrib.put("AnchorLeft", Item.class.getMethod("getAnchorLeft"),
|
---|
[1102] | 239 | Item.class.getMethod("setAnchorLeft", pIntO));
|
---|
[583] | 240 | _Attrib.put("AnchorRight", Item.class.getMethod("getAnchorRight"),
|
---|
[1102] | 241 | Item.class.getMethod("setAnchorRight", pIntO));
|
---|
[720] | 242 | _Attrib.put("AnchorTop", Item.class.getMethod("getAnchorTop"),
|
---|
[1102] | 243 | Item.class.getMethod("setAnchorTop", pIntO));
|
---|
[583] | 244 | _Attrib.put("AnchorBottom", Item.class.getMethod("getAnchorBottom"),
|
---|
[1102] | 245 | Item.class.getMethod("setAnchorBottom", pIntO));
|
---|
[583] | 246 | _Attrib.put("Position", Item.class.getMethod("getPosition"),
|
---|
| 247 | Item.class.getMethod("setPosition", pPoint));
|
---|
| 248 | _Attrib.put("Link", Item.class.getMethod("getLink"),
|
---|
| 249 | Item.class.getMethod("setLink", pString));
|
---|
[676] | 250 | _Attrib.put("AddToHistory", Item.class.getMethod("getLinkHistory"),
|
---|
| 251 | Item.class.getMethod("setLinkHistory", pBool));
|
---|
[583] | 252 | _Attrib.put("Action", Item.class.getMethod("getAction"),
|
---|
| 253 | Item.class.getMethod("setActions", pList));
|
---|
| 254 | _Attrib.put("ActionMark", Item.class.getMethod("getActionMark"),
|
---|
| 255 | Item.class.getMethod("setActionMark", pBool));
|
---|
| 256 | _Attrib.put("ActionCursorEnter", Item.class.getMethod("getActionCursorEnter"),
|
---|
| 257 | Item.class.getMethod("setActionCursorEnter", pList));
|
---|
| 258 | _Attrib.put("ActionCursorLeave", Item.class.getMethod("getActionCursorLeave"),
|
---|
| 259 | Item.class.getMethod("setActionCursorLeave", pList));
|
---|
| 260 | _Attrib.put("ActionEnterFrame", Item.class.getMethod("getActionEnterFrame"),
|
---|
| 261 | Item.class.getMethod("setActionEnterFrame", pList));
|
---|
| 262 | _Attrib.put("ActionLeaveFrame", Item.class.getMethod("getActionLeaveFrame"),
|
---|
| 263 | Item.class.getMethod("setActionLeaveFrame", pList));
|
---|
| 264 | _Attrib.put("Data", Item.class.getMethod("getData"),
|
---|
| 265 | Item.class.getMethod("setData", pList));
|
---|
| 266 | _Attrib.put("Highlight", Item.class.getMethod("getHighlight"),
|
---|
| 267 | Item.class.getMethod("setHighlight", pBool));
|
---|
| 268 | _Attrib.put("FillColor", Item.class.getMethod("getFillColor"),
|
---|
| 269 | Item.class.getMethod("setFillColor", pColor));
|
---|
| 270 | _Attrib.put("GradientColor", Item.class.getMethod("getGradientColor"),
|
---|
| 271 | Item.class.getMethod("setGradientColor", pColor));
|
---|
| 272 | _Attrib.put("GradientAngle", Item.class.getMethod("getGradientAngle"),
|
---|
[1102] | 273 | Item.class.getMethod("setGradientAngle", pDouble));
|
---|
[583] | 274 | _Attrib.put("FillPattern", Item.class.getMethod("getFillPattern"),
|
---|
| 275 | Item.class.getMethod("setFillPattern", pString));
|
---|
| 276 | _Attrib.put("Owner", Item.class.getMethod("getOwner"),
|
---|
| 277 | Item.class.getMethod("setOwner", pString));
|
---|
| 278 | _Attrib.put("LinkMark", Item.class.getMethod("getLinkMark"),
|
---|
| 279 | Item.class.getMethod("setLinkMark", pBool));
|
---|
| 280 | _Attrib.put("LinkFrameset", Item.class.getMethod("getLinkFrameset"),
|
---|
| 281 | Item.class.getMethod("setLinkFrameset", pString));
|
---|
| 282 | _Attrib.put("LinkTemplate", Item.class.getMethod("getLinkTemplate"),
|
---|
| 283 | Item.class.getMethod("setLinkTemplate", pString));
|
---|
| 284 | _Attrib.put("LinePattern", Item.class.getMethod("getLinePattern"),
|
---|
| 285 | Item.class.getMethod("setLinePattern", pIntArray));
|
---|
| 286 | _Attrib.put("Arrow", Item.class.getMethod("getArrow"),
|
---|
| 287 | Item.class.getMethod("setArrow", pArrow));
|
---|
| 288 | _Attrib.put("DotType", Item.class.getMethod("getDotType"),
|
---|
| 289 | Item.class.getMethod("setDotType", pDotType));
|
---|
| 290 | _Attrib.put("Filled", Item.class.getMethod("getFilled"),
|
---|
| 291 | Item.class.getMethod("setFilled", pBool));
|
---|
| 292 | _Attrib.put("Formula", Item.class.getMethod("getFormula"),
|
---|
| 293 | Item.class.getMethod("setFormula", pString));
|
---|
| 294 | _Attrib.put("Thickness", Item.class.getMethod("getThickness"),
|
---|
| 295 | Item.class.getMethod("setThickness", pFloat));
|
---|
[625] | 296 | // _Attrib.put("LineIDs", Item.class.getMethod("getLineIDs"),
|
---|
| 297 | // Item.class.getMethod("setLineIDs", pString));
|
---|
| 298 | // _Attrib.put("ConstraintIDs", Item.class.getMethod("getConstraintIDs"),
|
---|
| 299 | // Item.class.getMethod("setConstraintIDs", pString));
|
---|
[583] | 300 | _Attrib.put("Size", Item.class.getMethod("getSize"),
|
---|
| 301 | Item.class.getMethod("setSize", pFloat));
|
---|
| 302 | _Attrib.put("Save", Item.class.getMethod("getSave"),
|
---|
| 303 | Item.class.getMethod("setSave", pBool));
|
---|
| 304 | _Attrib.put("AutoStamp", Item.class.getMethod("getAutoStamp"),
|
---|
| 305 | Item.class.getMethod("setAutoStamp", pFloatO));
|
---|
| 306 | _Attrib.put("Width", Item.class.getMethod("getWidthToSave"),
|
---|
| 307 | Item.class.getMethod("setWidth", pIntO));
|
---|
[1200] | 308 | _Attrib.put("MinWidth", Item.class.getMethod("getMinWidthToSave"),
|
---|
| 309 | Item.class.getMethod("setMinWidth", pIntO));
|
---|
[601] | 310 | _Attrib.put("X", null,
|
---|
| 311 | Item.class.getMethod("setX", pFloat));
|
---|
| 312 | _Attrib.put("Y", null,
|
---|
| 313 | Item.class.getMethod("setY", pFloat));
|
---|
[656] | 314 | _Attrib.put("Tooltip", Item.class.getMethod("getTooltip"),
|
---|
[707] | 315 | Item.class.getMethod("setTooltips", pList));
|
---|
[737] | 316 | _Attrib.put("Permission", Item.class.getMethod("getPermission"),
|
---|
| 317 | Item.class.getMethod("setPermission", pPermission));
|
---|
[1398] | 318 | _Attrib.put("EncryptionLabel", Item.class.getMethod("getEncryptionLabel"),
|
---|
| 319 | Item.class.getMethod("setEncryptionLabel", pString));
|
---|
[1430] | 320 | _Attrib.put("ID", Item.class.getMethod("getID"),
|
---|
| 321 | Item.class.getMethod("setIDFail", pInt));
|
---|
[427] | 322 |
|
---|
[583] | 323 | // Text Items
|
---|
| 324 | _Attrib.put("Family", Text.class.getMethod("getFamily"),
|
---|
| 325 | Text.class.getMethod("setFamily", pString));
|
---|
| 326 | _Attrib.put("FontStyle", Text.class.getMethod("getFontStyle"),
|
---|
| 327 | Text.class.getMethod("setFontStyle", pString));
|
---|
| 328 | _Attrib.put("Justification", Text.class.getMethod("getJustification"),
|
---|
| 329 | Text.class.getMethod("setJustification", pJustification));
|
---|
[613] | 330 | _Attrib.put("AutoWrap", Text.class.getMethod("getAutoWrapToSave"),
|
---|
| 331 | Text.class.getMethod("setAutoWrap", pBool));
|
---|
[427] | 332 |
|
---|
[871] | 333 | _Attrib.put("LineSpacing", Text.class.getMethod("getSpacing"),
|
---|
| 334 | Text.class.getMethod("setSpacing", pFloat));
|
---|
| 335 |
|
---|
| 336 | _Attrib.put("LetterSpacing", Text.class.getMethod("getLetterSpacing"),
|
---|
| 337 | Text.class.getMethod("setLetterSpacing", pFloat));
|
---|
[1200] | 338 | _Attrib.put("Mask", Text.class.getMethod("getMask"),
|
---|
| 339 | Text.class.getMethod("setMask", pIntO));
|
---|
[1296] | 340 | _Attrib.put("Placeholder", Text.class.getMethod("getPlaceholder"),
|
---|
| 341 | Text.class.getMethod("setPlaceholder", pString));
|
---|
[1379] | 342 | _Attrib.put("SingleLineOnly", Text.class.getMethod("isSingleLineOnly"),
|
---|
| 343 | Text.class.getMethod("setSingleLineOnly", pBool));
|
---|
[1381] | 344 | _Attrib.put("TabIndex", Text.class.getMethod("getTabIndex"),
|
---|
| 345 | Text.class.getMethod("setTabIndex", pInt));
|
---|
[1415] | 346 | _Attrib.put("EnterClick", Item.class.getMethod("acceptsKeyboardEnter"),
|
---|
| 347 | Item.class.getMethod("setAcceptsEnter", pBool));
|
---|
[1477] | 348 | _Attrib.put("KeyImage", Item.class.getMethod("keyType"),
|
---|
| 349 | Item.class.getMethod("setKeyType", pString));
|
---|
[871] | 350 |
|
---|
[601] | 351 | // Aliases for attribute setting
|
---|
| 352 | _Attrib.alias("pos", "position");
|
---|
| 353 | _Attrib.alias("p", "position");
|
---|
| 354 | _Attrib.alias("xy", "position");
|
---|
| 355 | _Attrib.alias("a", "action");
|
---|
| 356 | _Attrib.alias("d", "data");
|
---|
| 357 | _Attrib.alias("f", "formula");
|
---|
[707] | 358 | _Attrib.alias("font", "family");
|
---|
[601] | 359 | _Attrib.alias("s", "size");
|
---|
| 360 | _Attrib.alias("l", "link");
|
---|
[720] | 361 | _Attrib.alias("at", "anchortop");
|
---|
[601] | 362 | _Attrib.alias("ab", "anchorbottom");
|
---|
[720] | 363 | _Attrib.alias("al", "anchorleft");
|
---|
[601] | 364 | _Attrib.alias("ar", "anchorright");
|
---|
| 365 | _Attrib.alias("t", "thickness");
|
---|
| 366 | // _Attrib.alias("c", "color"); // breaks circle creation
|
---|
| 367 | _Attrib.alias("bgc", "backgroundcolor");
|
---|
| 368 | _Attrib.alias("bc", "bordercolor");
|
---|
| 369 | _Attrib.alias("fc", "fillcolor");
|
---|
| 370 | _Attrib.alias("gc", "gradientcolor");
|
---|
| 371 | _Attrib.alias("ga", "gradientangle");
|
---|
| 372 | _Attrib.alias("fp", "fillpattern");
|
---|
| 373 | _Attrib.alias("lm", "linkmark");
|
---|
| 374 | _Attrib.alias("am", "actionmark");
|
---|
| 375 | _Attrib.alias("dt", "dottype");
|
---|
| 376 | _Attrib.alias("fill", "filled");
|
---|
| 377 | _Attrib.alias("lp", "linepattern");
|
---|
| 378 | _Attrib.alias("lf", "linkframeset");
|
---|
| 379 | _Attrib.alias("lt", "linktemplate");
|
---|
| 380 | _Attrib.alias("face", "fontstyle");
|
---|
| 381 | _Attrib.alias("j", "justification");
|
---|
| 382 | _Attrib.alias("w", "width");
|
---|
[1200] | 383 | _Attrib.alias("mw", "minwidth");
|
---|
[601] | 384 | _Attrib.alias("as", "autostamp");
|
---|
[4] | 385 | } catch (SecurityException e) {
|
---|
| 386 | // TODO Auto-generated catch block
|
---|
| 387 | e.printStackTrace();
|
---|
| 388 | } catch (NoSuchMethodException e) {
|
---|
| 389 | // TODO Auto-generated catch block
|
---|
| 390 | e.printStackTrace();
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
[583] | 393 |
|
---|
[4] | 394 | /**
|
---|
[583] | 395 | * Extracts a list of attributes from the given Item. Any method that
|
---|
[4] | 396 | * starts with <code>get</code>, takes no arguments and is not found in
|
---|
| 397 | * the Ignore list will be run, All the attributes are then put into a Text
|
---|
| 398 | * Item of the form <Name>:<Value> If the value returned by the get method
|
---|
| 399 | * is null, then the attribute will not be included, unless the name of the
|
---|
| 400 | * method is found in the AllowNull list.
|
---|
| 401 | *
|
---|
| 402 | * @param toExtract
|
---|
| 403 | * The Object from which to extract the attributes
|
---|
| 404 | * @return A Text Item containing the extracted Attributes.
|
---|
| 405 | */
|
---|
[97] | 406 | public static Item extractAttributes(Object toExtract) {
|
---|
[583] | 407 |
|
---|
| 408 | // System.out.println(toExtract);
|
---|
| 409 |
|
---|
| 410 | if (toExtract == null) {
|
---|
[4] | 411 | return null;
|
---|
[583] | 412 | }
|
---|
| 413 |
|
---|
| 414 | // Ensure the lists are populated
|
---|
| 415 | ensureReady();
|
---|
[4] | 416 |
|
---|
[671] | 417 | AttributeSet attribSet = null;
|
---|
[583] | 418 | if(toExtract instanceof Frame) {
|
---|
[671] | 419 | attribSet = _FrameAttrib;
|
---|
[583] | 420 | } else if(toExtract instanceof Item) {
|
---|
[671] | 421 | attribSet = _Attrib;
|
---|
[583] | 422 | } else {
|
---|
| 423 | throw new IncorrectTypeException("toExtract", "Item | Frame");
|
---|
| 424 | }
|
---|
[4] | 425 |
|
---|
| 426 | // StringBuffer to store all the extracted Attribute:Value pairs
|
---|
| 427 | StringBuffer attributes = new StringBuffer();
|
---|
| 428 |
|
---|
| 429 | // iterate through the list of methods
|
---|
[671] | 430 | for (String prop : attribSet.keys) {
|
---|
[71] | 431 |
|
---|
[671] | 432 | Attribute a = attribSet.get(prop);
|
---|
[71] | 433 | // Make sure the classes of the methods match the item
|
---|
[1188] | 434 | if (a != null && a.getter != null && a.getter.getDeclaringClass().isAssignableFrom(toExtract.getClass())) {
|
---|
[4] | 435 | try {
|
---|
[583] | 436 | String s = getValue(prop, a, toExtract, true);
|
---|
[1188] | 437 | if (s == null) {
|
---|
[80] | 438 | continue;
|
---|
[1188] | 439 | }
|
---|
[97] | 440 | // Append the attributes
|
---|
[583] | 441 | attributes.append(a.displayName)
|
---|
[235] | 442 | .append(AttributeValuePair.SEPARATOR_STRING)
|
---|
[376] | 443 | .append(s).append('\n');
|
---|
[80] | 444 | } catch (Exception e) {
|
---|
[4] | 445 | // TODO Auto-generated catch block
|
---|
| 446 | e.printStackTrace();
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | // if no attributes were extracted
|
---|
[1188] | 452 | if (attributes.length() <= 0) {
|
---|
[4] | 453 | return null;
|
---|
[1188] | 454 | }
|
---|
[4] | 455 |
|
---|
[1188] | 456 | while (attributes.charAt(attributes.length() - 1) == '\n') {
|
---|
[4] | 457 | attributes.delete(attributes.length() - 1, attributes.length());
|
---|
[1188] | 458 | }
|
---|
[4] | 459 |
|
---|
| 460 | // create the text Item
|
---|
[1102] | 461 | Frame current = DisplayController.getCurrentFrame();
|
---|
[78] | 462 | Item attribs = current.getStatsTextItem(attributes.toString());
|
---|
[4] | 463 | return attribs;
|
---|
| 464 | }
|
---|
[214] | 465 |
|
---|
[4] | 466 | /**
|
---|
[376] | 467 | * Gets a string form of the value for a given item get method.
|
---|
| 468 | * @param method
|
---|
| 469 | * @param item
|
---|
| 470 | * @param ignore true if the attributes in the IGNORE list should be ignored
|
---|
| 471 | * @return
|
---|
| 472 | */
|
---|
[583] | 473 | private static String getValue(String name, Attribute a, Object item, boolean ignore) {
|
---|
| 474 | // assert(method.getName().startsWith("get"));
|
---|
[376] | 475 |
|
---|
| 476 | Object o = null;
|
---|
| 477 | try {
|
---|
[583] | 478 | o = a.getter.invoke(item, (Object[]) null);
|
---|
[376] | 479 | } catch (IllegalArgumentException e) {
|
---|
| 480 | e.printStackTrace();
|
---|
| 481 | return null;
|
---|
| 482 | } catch (IllegalAccessException e) {
|
---|
| 483 | e.printStackTrace();
|
---|
| 484 | return null;
|
---|
| 485 | } catch (InvocationTargetException e) {
|
---|
| 486 | e.printStackTrace();
|
---|
| 487 | return null;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | if (o == null) {
|
---|
| 491 | // methods that return null are only included if they
|
---|
| 492 | // are in the AllowNull list
|
---|
[583] | 493 | if (_AllowNull.contains(a.getter)) {
|
---|
[1188] | 494 | if (name.equals("color")) {
|
---|
[376] | 495 | o = "default";
|
---|
[1188] | 496 | } else if (name.equals("backgroundcolor")) {
|
---|
[376] | 497 | o = "transparent";
|
---|
[1188] | 498 | } else if (name.equals("foregroundcolor")) {
|
---|
[376] | 499 | o = "auto";
|
---|
[1188] | 500 | } else {
|
---|
[376] | 501 | o = "";
|
---|
[1188] | 502 | }
|
---|
[376] | 503 | } else {
|
---|
| 504 | return null;
|
---|
| 505 | }
|
---|
| 506 | }
|
---|
| 507 | // skip methods that are in the ignore lists
|
---|
[583] | 508 | if (ignore && _IgnoreGet.contains(name)) {
|
---|
[376] | 509 | return null;
|
---|
| 510 | }
|
---|
[583] | 511 |
|
---|
[376] | 512 | if (o instanceof Integer) {
|
---|
| 513 | Integer i = (Integer) o;
|
---|
[1188] | 514 | if (i == Item.DEFAULT_INTEGER) {
|
---|
[376] | 515 | return null;
|
---|
[1188] | 516 | }
|
---|
[583] | 517 | if (a.getter.getName().endsWith("Justification")
|
---|
[1188] | 518 | && ((Justification) o).toString() != null) {
|
---|
[376] | 519 | o = ((Justification) o).toString();
|
---|
| 520 | // -1 indicates default value
|
---|
[1188] | 521 | } else {
|
---|
[376] | 522 | o = i;
|
---|
[1188] | 523 | }
|
---|
[376] | 524 | } else if (o instanceof Float) {
|
---|
| 525 | if (((Float) o) < -0.0001)
|
---|
[1188] | 526 | {
|
---|
[376] | 527 | return null;
|
---|
| 528 | // Null indicates default
|
---|
[583] | 529 | // o = Math.round((Float) o);
|
---|
[1188] | 530 | }
|
---|
[376] | 531 | } else if (o instanceof Double) {
|
---|
| 532 | // -1 indicates default value
|
---|
[1188] | 533 | if (((Double) o) < 0.0001) {
|
---|
[376] | 534 | return null;
|
---|
[1188] | 535 | }
|
---|
[1102] | 536 | } else if (o instanceof Colour) {
|
---|
[376] | 537 | // converts the color to the Expeditee code
|
---|
[1102] | 538 | o = Conversion.getExpediteeColorCode((Colour) o);
|
---|
[1188] | 539 | if (o == null) {
|
---|
[376] | 540 | return null;
|
---|
[1188] | 541 | }
|
---|
[376] | 542 | } else if (o instanceof Point) {
|
---|
| 543 | Point p = (Point) o;
|
---|
| 544 | o = Math.round(p.getX()) + " " + Math.round(p.getY());
|
---|
| 545 | } else if (o instanceof Font) {
|
---|
| 546 | Font f = (Font) o;
|
---|
| 547 |
|
---|
[1102] | 548 | String s = f.getFamilyName() + "-";
|
---|
[1188] | 549 | if (f.isPlain()) {
|
---|
[376] | 550 | s += "Plain";
|
---|
[1188] | 551 | }
|
---|
[376] | 552 |
|
---|
[1188] | 553 | if (f.isBold()) {
|
---|
[376] | 554 | s += "Bold";
|
---|
[1188] | 555 | }
|
---|
[376] | 556 |
|
---|
[1188] | 557 | if (f.isItalic()) {
|
---|
[376] | 558 | s += "Italic";
|
---|
[1188] | 559 | }
|
---|
[376] | 560 |
|
---|
| 561 | s += "-" + f.getSize();
|
---|
| 562 | o = s;
|
---|
| 563 | } else if (o instanceof Text) {
|
---|
| 564 | o = ((Text) o).getFirstLine();
|
---|
| 565 | } else if (o instanceof List) {
|
---|
| 566 | List list = (List) o;
|
---|
| 567 | StringBuffer sb = new StringBuffer();
|
---|
[1188] | 568 | for (Object ob : list) {
|
---|
[376] | 569 | // TODO check that this works ok
|
---|
| 570 | if (sb.length() == 0) {
|
---|
| 571 | sb.append(ob);
|
---|
| 572 | } else {
|
---|
[583] | 573 | sb.append('\n').append(a.displayName).append(AttributeValuePair.SEPARATOR_STRING).append(ob);
|
---|
[376] | 574 | }
|
---|
[1188] | 575 | }
|
---|
[376] | 576 | return sb.toString();
|
---|
| 577 | } else if (o instanceof int[]) {
|
---|
| 578 | StringBuffer sb = new StringBuffer();
|
---|
| 579 | int[] values = (int[]) o;
|
---|
| 580 | for (int i = 0; i < values.length; i++) {
|
---|
| 581 | sb.append(values[i]).append(' ');
|
---|
| 582 | }
|
---|
| 583 | sb.deleteCharAt(sb.length() - 1);
|
---|
| 584 | o = sb.toString();
|
---|
[1415] | 585 | } else if (o instanceof Boolean) {
|
---|
| 586 | try {
|
---|
| 587 | Class<?> parentClass = item.getClass();
|
---|
| 588 | Field defaultValueField = parentClass.getField(a.getter.getName() + "Default");
|
---|
| 589 | boolean defaultValue = defaultValueField.getBoolean(null);
|
---|
| 590 | if (defaultValue == (boolean) o) {
|
---|
| 591 | return null;
|
---|
| 592 | }
|
---|
| 593 | } catch (IllegalArgumentException e) {
|
---|
| 594 | e.printStackTrace();
|
---|
| 595 | } catch (SecurityException e) {
|
---|
| 596 | e.printStackTrace();
|
---|
| 597 | } catch (NoSuchFieldException e) {
|
---|
| 598 | // true is the default for boolean values when no other default is provided
|
---|
| 599 | if ((boolean) o) {
|
---|
| 600 | return null;
|
---|
| 601 | }
|
---|
| 602 | } catch (IllegalAccessException e) {
|
---|
| 603 | e.printStackTrace();
|
---|
[1188] | 604 | }
|
---|
[376] | 605 | }
|
---|
| 606 | return o.toString();
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | /**
|
---|
[4] | 610 | * Attempts to set the attribute in the given attribute: value pair. The
|
---|
| 611 | * value string should be formatted as follows:
|
---|
| 612 | * <code> Attribute: Value </code> Multiple values can be used if they are
|
---|
| 613 | * separated by spaces
|
---|
| 614 | *
|
---|
| 615 | * @param toSet
|
---|
[97] | 616 | * The Item or Frame to set the attribute of
|
---|
[4] | 617 | * @param attribs
|
---|
| 618 | * The Text item that contains the list of attributes to set
|
---|
| 619 | * @return True if the attribute(s) were sucessfully set, false otherwise
|
---|
| 620 | */
|
---|
[97] | 621 | public static boolean setAttribute(Object toSet, Text attribs) {
|
---|
[242] | 622 | return setAttribute(toSet, attribs, 1);
|
---|
| 623 | }
|
---|
[348] | 624 |
|
---|
| 625 | public static boolean setAttribute(Object toSet, Text attribs,
|
---|
| 626 | int minAttributeLength) {
|
---|
[4] | 627 | // error checking
|
---|
[1188] | 628 | if (toSet == null || attribs == null) {
|
---|
[4] | 629 | return false;
|
---|
[1188] | 630 | }
|
---|
[4] | 631 |
|
---|
[583] | 632 | ensureReady();
|
---|
[671] | 633 |
|
---|
| 634 | AttributeSet attribSet = null;
|
---|
| 635 | if(toSet instanceof Frame) {
|
---|
| 636 | attribSet = _FrameAttrib;
|
---|
| 637 | } else if(toSet instanceof Item) {
|
---|
| 638 | attribSet = _Attrib;
|
---|
| 639 | } else {
|
---|
| 640 | throw new IncorrectTypeException("toExtract", "Item | Frame");
|
---|
| 641 | }
|
---|
[4] | 642 |
|
---|
[348] | 643 | // if(attribs.isAnnotation())
|
---|
| 644 | // return false;
|
---|
| 645 |
|
---|
[4] | 646 | // get the list of attribute: value pairs
|
---|
[80] | 647 | List<String> values = attribs.getTextList();
|
---|
[97] | 648 | // if no pairs exist, we are done
|
---|
[235] | 649 | if (values == null || values.size() == 0) {
|
---|
[4] | 650 | return false;
|
---|
[86] | 651 | }
|
---|
[4] | 652 |
|
---|
| 653 | // loop through all attribute: value pairs
|
---|
| 654 | for (int i = 0; i < values.size(); i++) {
|
---|
[348] | 655 | AttributeValuePair avp = new AttributeValuePair(values.get(i),
|
---|
| 656 | false);
|
---|
[4] | 657 |
|
---|
[477] | 658 | // If the first is not an attribute value pair then don't do
|
---|
| 659 | // attribute merging
|
---|
[348] | 660 | if (!avp.hasAttribute()
|
---|
[1188] | 661 | || avp.getAttribute().length() < minAttributeLength) {
|
---|
[235] | 662 | return false;
|
---|
[1188] | 663 | }
|
---|
[4] | 664 |
|
---|
| 665 | // check if the next string is another attribute to merge or a
|
---|
| 666 | // continuation
|
---|
[235] | 667 | for (; i < values.size() - 1; i++) {
|
---|
| 668 | AttributeValuePair nextAvp = new AttributeValuePair(values
|
---|
[247] | 669 | .get(i + 1), false);
|
---|
[4] | 670 |
|
---|
| 671 | // if the next String has a colon, then it may be another
|
---|
| 672 | // attribute
|
---|
[235] | 673 | if (nextAvp.hasAttribute()) {
|
---|
[4] | 674 | // if the attribute is the same as v, then it is a
|
---|
| 675 | // continuation
|
---|
[235] | 676 | if (nextAvp.getAttribute().equals(avp.getAttribute())) {
|
---|
[4] | 677 | // strip the attribute from next
|
---|
[707] | 678 | avp.appendValue(nextAvp.getValue() + "\n");
|
---|
[235] | 679 |
|
---|
[4] | 680 | // if the attribute is not the same, then it may be a
|
---|
| 681 | // new method
|
---|
| 682 | } else {
|
---|
| 683 | break;
|
---|
| 684 | }
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[235] | 687 | // v.append("\n").append(next);
|
---|
[4] | 688 | }
|
---|
| 689 |
|
---|
[427] | 690 | try {
|
---|
| 691 | if (!setAttribute(toSet, avp, values.size() > 1)) {
|
---|
[235] | 692 |
|
---|
[427] | 693 | String stripped = avp.getAttribute();
|
---|
| 694 | if (!avp.hasPair()) {
|
---|
| 695 | // This happens when there is an attribute at the start
|
---|
| 696 | // Then a bunch of plain text
|
---|
| 697 | return false;
|
---|
[583] | 698 | } else if (_IgnoreSet.contains(stripped)) {
|
---|
[427] | 699 | return false;
|
---|
| 700 | } else {
|
---|
[671] | 701 | Attribute a = attribSet.get(stripped);
|
---|
[601] | 702 | if(a == null || a.setter == null) {
|
---|
| 703 | return false;
|
---|
| 704 | }
|
---|
[427] | 705 | String types = "";
|
---|
[601] | 706 | for (Class<?> c : a.setter.getParameterTypes()) {
|
---|
[427] | 707 | types += c.getSimpleName() + " ";
|
---|
[583] | 708 | }
|
---|
[427] | 709 | MessageBay.warningMessage("Wrong arguments for: '"
|
---|
| 710 | + avp.getAttribute() + "' expecting "
|
---|
| 711 | + types.trim() + " found '" + avp.getValue() + "'");
|
---|
| 712 | }
|
---|
[4] | 713 | }
|
---|
[427] | 714 | } catch (AttributeException e) {
|
---|
| 715 | MessageBay.errorMessage(e.getMessage());
|
---|
[235] | 716 | }
|
---|
[4] | 717 | }
|
---|
| 718 |
|
---|
| 719 | return true;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
[235] | 722 | /**
|
---|
[1048] | 723 | * Sets a single attribute of a frame or item.
|
---|
[235] | 724 | *
|
---|
| 725 | * @param toSet
|
---|
| 726 | * @param avp
|
---|
| 727 | * @param isAttributeList
|
---|
| 728 | * some properties are ignored when attribute list are injected
|
---|
| 729 | * into an item. These properties are ignored if this param is
|
---|
| 730 | * true
|
---|
| 731 | * @return
|
---|
[427] | 732 | * @throws NoSuchAttributeException
|
---|
[235] | 733 | */
|
---|
| 734 | private static boolean setAttribute(Object toSet, AttributeValuePair avp,
|
---|
[427] | 735 | boolean isAttributeList) throws AttributeException {
|
---|
[235] | 736 |
|
---|
| 737 | assert (avp.hasAttribute());
|
---|
| 738 |
|
---|
[4] | 739 | // separate attribute and value from string
|
---|
[235] | 740 | String attribute = avp.getAttribute().toLowerCase();
|
---|
[214] | 741 |
|
---|
[235] | 742 | String value = avp.getValue();
|
---|
[214] | 743 | assert (value != null);
|
---|
[671] | 744 |
|
---|
| 745 | AttributeSet attribSet = null;
|
---|
| 746 | if(toSet instanceof Frame) {
|
---|
| 747 | attribSet = _FrameAttrib;
|
---|
| 748 | } else if(toSet instanceof Item) {
|
---|
| 749 | attribSet = _Attrib;
|
---|
| 750 | } else {
|
---|
| 751 | throw new IncorrectTypeException("toExtract", "Item | Frame");
|
---|
| 752 | }
|
---|
[4] | 753 |
|
---|
| 754 | // Some properties are ignored when multiple attributes are being set on
|
---|
| 755 | // an item at the same time
|
---|
[583] | 756 | if (isAttributeList && _IgnoreSet.contains(attribute)) {
|
---|
[4] | 757 | // System.out.println("Attribute ignored: " + attribute);
|
---|
| 758 | return true;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
[97] | 761 | // Separate multiple values if required
|
---|
[583] | 762 |
|
---|
[671] | 763 | Attribute a = attribSet.get(attribute);
|
---|
[4] | 764 | // if this is not the name of a method, it may be the name of an agent
|
---|
[601] | 765 | if (a == null || a.setter == null) {
|
---|
[4] | 766 | // System.out.println("Attrib not found for: " + attribute);
|
---|
| 767 | return false;
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 | // if there are duplicate methods with the same name
|
---|
| 771 | List<Method> possibles = new LinkedList<Method>();
|
---|
[1188] | 772 | if (a.setter.getDeclaringClass().isInstance(toSet)) {
|
---|
[583] | 773 | possibles.add(a.setter);
|
---|
[1188] | 774 | }
|
---|
[4] | 775 | int i = 0;
|
---|
[671] | 776 | while (attribSet.containsKey(attribute + i)) {
|
---|
| 777 | Method m = attribSet.get(attribute + i).setter;
|
---|
[601] | 778 | if(m == null) {
|
---|
| 779 | break;
|
---|
| 780 | }
|
---|
[583] | 781 | if (m.getDeclaringClass().isAssignableFrom(toSet.getClass())) {
|
---|
| 782 | possibles.add(m);
|
---|
| 783 | }
|
---|
[4] | 784 | i++;
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | for (Method possible : possibles) {
|
---|
[427] | 788 | Object current = invokeAttributeGetMethod(avp.getAttribute(), toSet);
|
---|
[4] | 789 | // find the corresponding get method for this set method
|
---|
[80] | 790 | // and get the current value of the attribute
|
---|
[4] | 791 |
|
---|
| 792 | try {
|
---|
[50] | 793 | Object[] params = Conversion.Convert(possible, value, current);
|
---|
[4] | 794 |
|
---|
| 795 | try {
|
---|
| 796 | possible.invoke(toSet, params);
|
---|
| 797 | return true;
|
---|
| 798 | } catch (IllegalArgumentException e) {
|
---|
| 799 | // TODO Auto-generated catch block
|
---|
| 800 | e.printStackTrace();
|
---|
| 801 | } catch (IllegalAccessException e) {
|
---|
| 802 | // TODO Auto-generated catch block
|
---|
| 803 | e.printStackTrace();
|
---|
| 804 | } catch (InvocationTargetException e) {
|
---|
[214] | 805 | MessageBay.displayMessage(toSet.getClass().getSimpleName()
|
---|
[50] | 806 | + " type does not support that attribute.");
|
---|
| 807 | // e.printStackTrace();
|
---|
[4] | 808 | }
|
---|
| 809 | } catch (NumberFormatException e) {
|
---|
| 810 |
|
---|
| 811 | }
|
---|
| 812 | }
|
---|
| 813 |
|
---|
[427] | 814 | if(possibles.size() == 0){
|
---|
[1188] | 815 | if(invokeAttributeGetMethod(avp.getAttribute(), toSet) == null) {
|
---|
[427] | 816 | throw new NoSuchAttributeException(avp.getAttribute(), toSet.getClass().getSimpleName());
|
---|
[1188] | 817 | }
|
---|
[427] | 818 | throw new ReadOnlyAttributeException(avp.getAttribute(), toSet.getClass().getSimpleName());
|
---|
| 819 | }
|
---|
| 820 |
|
---|
[4] | 821 | return false;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
[427] | 824 | private static Object invokeAttributeGetMethod(String name, Object toSet) {
|
---|
| 825 |
|
---|
[671] | 826 | AttributeSet attribSet = null;
|
---|
| 827 | if(toSet instanceof Frame) {
|
---|
| 828 | attribSet = _FrameAttrib;
|
---|
| 829 | } else if(toSet instanceof Item) {
|
---|
| 830 | attribSet = _Attrib;
|
---|
| 831 | } else {
|
---|
| 832 | throw new IncorrectTypeException("toExtract", "Item | Frame");
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | Attribute a = attribSet.get(name.toLowerCase());
|
---|
[583] | 836 | if(a == null) {
|
---|
| 837 | return null;
|
---|
[427] | 838 | }
|
---|
[583] | 839 | try {
|
---|
| 840 | return a.getter.invoke(toSet);
|
---|
| 841 | } catch (Exception e) {
|
---|
| 842 | e.printStackTrace();
|
---|
| 843 | }
|
---|
[427] | 844 | return null;
|
---|
| 845 | }
|
---|
| 846 |
|
---|
[4] | 847 | /**
|
---|
[97] | 848 | * Replaces the current value for the text item with the new value.
|
---|
[95] | 849 | *
|
---|
| 850 | * @param text
|
---|
[97] | 851 | * the item whos value is to be changed
|
---|
| 852 | * @param newValue
|
---|
| 853 | * the new value for the item
|
---|
[95] | 854 | */
|
---|
[97] | 855 | public static void replaceValue(Text text, String newValue) {
|
---|
| 856 | assert (newValue != null);
|
---|
[50] | 857 |
|
---|
[235] | 858 | AttributeValuePair avp = new AttributeValuePair(text.getText());
|
---|
[4] | 859 |
|
---|
[235] | 860 | if (avp.getAttribute() == null) {
|
---|
| 861 | avp.setAttribute(avp.getValue());
|
---|
[4] | 862 | }
|
---|
[235] | 863 | avp.setValue(newValue);
|
---|
| 864 | text.setText(avp.toString());
|
---|
[4] | 865 | }
|
---|
[376] | 866 |
|
---|
| 867 | public static String getAttribute(Item item, String attribute) {
|
---|
| 868 |
|
---|
| 869 | // ensure the lists are populated
|
---|
[583] | 870 | ensureReady();
|
---|
[376] | 871 |
|
---|
| 872 | // separate attribute and value from string
|
---|
| 873 | String lowerAttribute = attribute.trim().toLowerCase();
|
---|
| 874 |
|
---|
[583] | 875 | Attribute a = _Attrib.get(lowerAttribute);
|
---|
| 876 | if(a == null) {
|
---|
| 877 | MessageBay.errorMessage("Could no extract unknown attribute value: " + attribute);
|
---|
| 878 | return null;
|
---|
[376] | 879 | }
|
---|
[583] | 880 | return a.displayName + AttributeValuePair.SEPARATOR_STRING + getValue(lowerAttribute, a, item, false);
|
---|
[376] | 881 | }
|
---|
[4] | 882 | }
|
---|