Changeset 576 for trunk/quest-core-events/src/main/java
- Timestamp:
- 08/17/12 16:15:24 (12 years ago)
- Location:
- trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/AbstractDefaultGUIElement.java
r565 r576 18 18 public static final long serialVersionUID = 1L; 19 19 20 /** the information about the original type before the mapping*/21 private String originalTypeInfo;20 /** the specification of the GUI element */ 21 private IGUIElementSpec specification; 22 22 23 /* (non-Javadoc) 24 * @see de.ugoe.cs.quest.eventcore.guimodel.IGUIElement#getParent() 23 /** 24 * <p> 25 * TODO: comment 26 * </p> 27 * 28 * @param specification 25 29 */ 26 @Override 27 public IGUIElement getParent() { 28 // TODO remove getParent Stuff 29 return null; 30 public AbstractDefaultGUIElement(IGUIElementSpec specification) { 31 this.specification = specification; 30 32 } 31 33 … … 33 35 * (non-Javadoc) 34 36 * 35 * @see de.ugoe.cs.tasktree.guimodel.GUIElement#get OriginalTypeInfo()37 * @see de.ugoe.cs.tasktree.guimodel.GUIElement#getSpecification() 36 38 */ 37 39 @Override 38 public String getOriginalTypeInfo() {39 return originalTypeInfo;40 public IGUIElementSpec getSpecification() { 41 return specification; 40 42 } 41 43 … … 43 45 * (non-Javadoc) 44 46 * 45 * @see GUIElement# setOriginalTypeInfo(String)47 * @see GUIElement#equals(GUIElement) 46 48 */ 47 void setOriginalTypeInfo(String originalTypeInfo) { 48 this.originalTypeInfo = originalTypeInfo; 49 public boolean equals(IGUIElement other) { 50 if (this == other) 51 { 52 return true; 53 } 54 55 if (!this.getClass().isInstance(other)) { 56 return false; 57 } 58 59 AbstractDefaultGUIElement otherElem = (AbstractDefaultGUIElement) other; 60 61 return 62 ((otherElem.specification == specification) || 63 ((specification != null) && specification.equals(otherElem.specification))); 49 64 } 50 65 -
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/AbstractDefaultGUIElementFactory.java
r564 r576 20 20 * @author 2012, last modified by $Author: patrick$ 21 21 */ 22 public abstract class AbstractDefaultGUIElementFactory {22 public abstract class AbstractDefaultGUIElementFactory implements IGUIElementFactory { 23 23 24 24 /** */ … … 53 53 * @throws GUIModelConfigurationException 54 54 */ 55 protected IGUIElement instantiateGUIElementFromConfiguredMappings(String type, 56 Object... parameters) 55 protected IGUIElement instantiateGUIElementFromConfiguredMappings(IGUIElementSpec specification) 57 56 throws GUIModelConfigurationException 58 57 { 59 58 Properties mappings = getMappingsFromConfiguration(); 60 59 61 String className = mappings.getProperty( type);60 String className = mappings.getProperty(specification.getType()); 62 61 if (className != null) { 63 62 try { … … 72 71 } 73 72 74 Class<?>[] parameterTypes = new Class<?>[parameters.length]; 75 76 for (int i = 0; i < parameters.length; i++) { 77 parameterTypes[i] = parameters[i].getClass(); 78 79 if (Boolean.class.equals(parameterTypes[i])) { 80 parameterTypes[i] = boolean.class; 81 } 82 else if (Integer.class.equals(parameterTypes[i])) { 83 parameterTypes[i] = int.class; 84 } 85 else if (Double.class.equals(parameterTypes[i])) { 86 parameterTypes[i] = double.class; 87 } 88 else if (Character.class.equals(parameterTypes[i])) { 89 parameterTypes[i] = char.class; 90 } 91 else if (Byte.class.equals(parameterTypes[i])) { 92 parameterTypes[i] = byte.class; 93 } 94 else if (Float.class.equals(parameterTypes[i])) { 95 parameterTypes[i] = float.class; 96 } 97 else if (Long.class.equals(parameterTypes[i])) { 98 parameterTypes[i] = long.class; 99 } 100 else if (Short.class.equals(parameterTypes[i])) { 101 parameterTypes[i] = short.class; 102 } 103 } 73 Class<?>[] parameterTypes = new Class<?>[1]; 74 parameterTypes[0] = specification.getClass(); 104 75 105 76 IGUIElement guiElement = 106 (IGUIElement) clazz.getConstructor(parameterTypes).newInstance(parameters); 107 108 if (guiElement instanceof AbstractDefaultGUIElement) { 109 ((AbstractDefaultGUIElement) guiElement).setOriginalTypeInfo(type); 110 } 77 (IGUIElement) clazz.getConstructor(parameterTypes).newInstance(specification); 111 78 112 79 return guiElement; -
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/GUIModel.java
r545 r576 8 8 9 9 import java.util.ArrayList; 10 import java.util.Collection;11 import java.util.HashMap;12 10 import java.util.List; 13 import java.util.Map;14 11 15 12 /** 16 * TODO comment 13 * <p> 14 * The goal of a GUI model is to correctly l 15 * </p> 17 16 * 18 17 * @version $Revision: $ $Date: 14.08.2012$ 19 18 * @author 2012, last modified by $Author: pharms$ 20 19 */ 21 public class GUIModel <T extends IGUIElement>{20 public class GUIModel { 22 21 23 22 /** 24 23 * 25 24 */ 26 private Collection<T> guiElements = new ArrayList<T>();27 25 private TreeNode root = new TreeNode(); 26 28 27 /** 29 28 * 30 29 */ 31 private Map<T, List<T>> childRelations = new HashMap<T, List<T>>();30 private List<TreeNode> allNodes = new ArrayList<TreeNode>(); 32 31 33 32 /** … … 38 37 * @throws GUIModelException 39 38 */ 40 public T checkAndIntegratePath(List<T> guiElementPath) throws GUIModelException { 41 T existingGuiElement = check(guiElementPath); 42 43 if (existingGuiElement == null) { 44 T terminalGuiElement = null; 45 T child = null; 46 boolean doBreak = false; 47 for (int i = guiElementPath.size() - 1; ((i >= 0) && (!doBreak)); i--) { 48 T newElement = guiElementPath.get(i); 49 existingGuiElement = null; 39 public IGUIElement integratePath(List<? extends IGUIElementSpec> guiElementPath, 40 IGUIElementFactory guiElementFactory) 41 throws GUIModelException 42 { 43 List<IGUIElementSpec> remainingPath = new ArrayList<IGUIElementSpec>(); 44 45 for (IGUIElementSpec spec : guiElementPath) 46 { 47 remainingPath.add(spec); 48 } 49 50 return integratePath(root, remainingPath, guiElementFactory); 51 } 52 53 /** 54 * TODO: comment 55 * 56 * @param root 57 * @return 58 */ 59 public List<IGUIElement> getChildren(IGUIElement guiElement) { 60 for (TreeNode node : allNodes) { 61 if (node.guiElement.equals(guiElement)) { 62 List<IGUIElement> result = new ArrayList<IGUIElement>(); 50 63 51 for (T candidate : guiElements) { 52 if (candidate.equals(newElement)) { 53 existingGuiElement = candidate; 54 break; 64 if (node.children != null) { 65 for (TreeNode child : node.children) { 66 result.add(child.guiElement); 55 67 } 56 68 } 57 69 58 // element not yet contained in model. Add it. 59 if (existingGuiElement == null) 60 { 61 guiElements.add(newElement); 62 existingGuiElement = newElement; 63 } 64 else 65 { 66 doBreak = true; 67 } 70 return result; 71 } 72 } 73 74 return null; 75 } 76 77 /** 78 * TODO: comment 79 * 80 * @return 81 */ 82 public List<IGUIElement> getRootElements() { 83 List<IGUIElement> roots = new ArrayList<IGUIElement>(); 84 for (TreeNode rootChild : root.children) { 85 roots.add(rootChild.guiElement); 86 } 87 return roots; 88 } 89 90 /** 91 * <p> 92 * TODO: comment 93 * </p> 94 * 95 * @param root2 96 * @param guiElementPath 97 * @param guiElementFactory 98 * @return 99 * @throws GUIModelException 100 */ 101 private IGUIElement integratePath(TreeNode parentNode, 102 List<? extends IGUIElementSpec> remainingPath, 103 IGUIElementFactory guiElementFactory) 104 throws GUIModelException 105 { 106 IGUIElementSpec specToIntegrateElementFor = remainingPath.remove(0); 107 108 List<TreeNode> matchingChildren = new ArrayList<TreeNode>(); 109 int maximumSimilarity = 0; 110 111 if (parentNode.children != null) { 112 for (TreeNode child : parentNode.children) { 113 int similarityLevel = getSimilarityLevel 114 (specToIntegrateElementFor, child.guiElement.getSpecification()); 68 115 69 if (terminalGuiElement == null) { 70 terminalGuiElement = existingGuiElement; 71 } 72 73 if (child != null) { 74 List<T> children = childRelations.get(existingGuiElement); 75 if (children == null) { 76 children = new ArrayList<T>(); 77 childRelations.put(existingGuiElement, children); 78 } 79 children.add(child); 80 } 81 child = existingGuiElement; 82 } 83 84 existingGuiElement = terminalGuiElement; 85 } 86 87 return existingGuiElement; 88 } 89 90 /** 91 * TODO: comment 92 * 93 * @param root 94 * @return 95 */ 96 public List<T> getChildren(T guiElement) { 97 return childRelations.get(guiElement); 98 } 99 100 /** 101 * TODO: comment 102 * 103 * @return 104 */ 105 public List<T> getRootElements() { 106 List<T> roots = new ArrayList<T>(); 107 for (T guiElement : guiElements) { 108 if (guiElement.getParent() == null) { 109 roots.add(guiElement); 110 } 111 } 112 return roots; 113 } 114 115 /** 116 * TODO: comment 117 * 118 * @param guiElementPath 119 * @throws GUIModelException 120 */ 121 private T check(List<T> guiElementPath) throws GUIModelException { 122 T guiElementInModel = null; 123 124 for (int i = 0; i < guiElementPath.size(); i++) { 125 guiElementInModel = null; 126 127 T newElement = guiElementPath.get(i); 128 T newParent = (i > 0 ? guiElementPath.get(i - 1) : null); 129 130 if ((newElement.getParent() != null) && 131 (!newElement.getParent().equals(newParent))) 132 { 133 throw new GUIModelException 134 ("new GUI element " + newElement + " denotes a parent element (" + 135 newElement.getParent() + ") which is not identical to the parent element " + 136 "denoted by the element path (" + newParent + ")"); 137 } 138 139 for (T existingElement : guiElements) { 140 if (existingElement.equals(newElement)) { 141 if (guiElementInModel != null) { 116 if (similarityLevel >= maximumSimilarity) { 117 if (maximumSimilarity == 100) { 142 118 throw new GUIModelException 143 ("several GUI elements already existing in the model pretend to "+144 "match the new element " + newElement);119 ("several children of gui element " + parentNode.guiElement + 120 " pretend to fully match specification " + specToIntegrateElementFor); 145 121 } 146 122 147 if ((existingElement.getParent() != newParent) && 148 ((existingElement.getParent() != null) && 149 (!existingElement.getParent().equals(newParent)))) 150 { 151 throw new GUIModelException 152 ("the new path denotes the GUI element " + newElement + 153 " with parent " + newElement.getParent() + " that already exists in " + 154 "the model with a distinct parent element (" + 155 existingElement.getParent() + ")"); 123 if (maximumSimilarity != similarityLevel) { 124 matchingChildren.clear(); 125 maximumSimilarity = similarityLevel; 156 126 } 157 127 158 guiElementInModel = existingElement;128 matchingChildren.add(child); 159 129 } 160 130 } 161 131 } 162 132 163 return guiElementInModel; 164 } 165 133 // we do not see something as matching if the similarity level is below 80% 134 if (maximumSimilarity < 80) { 135 matchingChildren.clear(); 136 } 137 138 // if we get here, the corresponding path does not exist yet. So create it 139 if (matchingChildren.size() == 0) { 140 matchingChildren.add 141 (parentNode.addChild 142 (guiElementFactory.instantiateGUIElement(specToIntegrateElementFor))); 143 } 144 else if (matchingChildren.size() > 1) { 145 throw new GUIModelException 146 ("several children of gui element " + parentNode.guiElement + 147 " match the specification " + specToIntegrateElementFor + " at the same level. " + 148 "Can not decide which is the right one."); 149 } 150 151 if (remainingPath.size() > 0) { 152 return integratePath(matchingChildren.get(0), remainingPath, guiElementFactory); 153 } 154 else { 155 return matchingChildren.get(0).guiElement; 156 } 157 } 158 159 /** 160 * <p> 161 * TODO: comment 162 * </p> 163 * 164 * @param spec1 165 * @param spec2 166 * @return 167 * @throws GUIModelException 168 */ 169 private int getSimilarityLevel(IGUIElementSpec spec1, IGUIElementSpec spec2) 170 throws GUIModelException 171 { 172 if (spec1 == spec2) { 173 return 100; 174 } 175 else if (spec1 != null) { 176 int level = spec1.getSimilarity(spec2); 177 178 if ((level < 0) || (100 < level)) { 179 throw new GUIModelException("invalid tree node similarity provided (" + level + 180 "). Must be between 0 and 100."); 181 } 182 183 return level; 184 } 185 else { 186 return 0; 187 } 188 } 189 190 /** 191 * <p> 192 * TODO comment 193 * </p> 194 * 195 * @version $Revision: $ $Date: 17.08.2012$ 196 * @author 2012, last modified by $Author: pharms$ 197 */ 198 private class TreeNode 199 { 200 /** */ 201 private IGUIElement guiElement; 202 203 /** */ 204 private List<TreeNode> children; 205 206 /** */ 207 //private TreeNode parent; 208 209 /** 210 * <p> 211 * TODO: comment 212 * </p> 213 * 214 * @param guiElement 215 * @return 216 */ 217 private TreeNode addChild(IGUIElement guiElement) 218 { 219 if (children == null) 220 { 221 children = new ArrayList<TreeNode>(); 222 } 223 224 TreeNode child = new TreeNode(); 225 child.guiElement = guiElement; 226 //child.parent = this; 227 children.add(child); 228 229 allNodes.add(child); 230 231 return child; 232 } 233 } 166 234 } -
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/IGUIElement.java
r565 r576 20 20 * 21 21 */ 22 public IGUIElement getParent(); 23 24 /** 25 * 26 */ 27 public String getOriginalTypeInfo(); 22 public IGUIElementSpec getSpecification(); 28 23 29 24 /** … … 33 28 public boolean equals(IGUIElement other); 34 29 30 /** 31 * 32 */ 33 public int hashCode(); 35 34 }
Note: See TracChangeset
for help on using the changeset viewer.