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