source: trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/GUIModel.java @ 603

Last change on this file since 603 was 603, checked in by pharms, 12 years ago
  • added reference to parent nodes to GUI elements
File size: 5.5 KB
RevLine 
[545]1// Module    : $RCSfile: GUIModel.java,v $
2// Version   : $Revision: 0.0 $  $Author: pharms $  $Date: 14.08.2012 $
3// Project   : quest-core-events
4// Creation  : 2012 by pharms
5// Copyright : Patrick Harms, 2012
6
7package de.ugoe.cs.quest.eventcore.guimodel;
8
9import java.util.ArrayList;
[603]10import java.util.LinkedList;
[545]11import java.util.List;
12
13/**
[576]14 * <p>
15 * The goal of a GUI model is to correctly l
16 * </p>
[545]17 *
18 * @version $Revision: $ $Date: 14.08.2012$
19 * @author 2012, last modified by $Author: pharms$
20 */
[576]21public class GUIModel {
[545]22   
23    /**
24     *
25     */
[576]26    private TreeNode root = new TreeNode();
27   
[545]28    /**
29     *
30     */
[576]31    private List<TreeNode> allNodes = new ArrayList<TreeNode>();
[545]32
33    /**
34     * TODO: comment
35     *
36     * @param currentGUIElementPath
37     * @return
38     * @throws GUIModelException
39     */
[576]40    public IGUIElement integratePath(List<? extends IGUIElementSpec> guiElementPath,
41                                     IGUIElementFactory              guiElementFactory)
42        throws GUIModelException
43    {
[603]44        List<IGUIElementSpec> remainingPath = new LinkedList<IGUIElementSpec>();
[545]45       
[576]46        for (IGUIElementSpec spec : guiElementPath)
47        {
48            remainingPath.add(spec);
[545]49        }
50       
[576]51        return integratePath(root, remainingPath, guiElementFactory);
[545]52    }
53
54    /**
55     * TODO: comment
56     *
57     * @param root
58     * @return
59     */
[576]60    public List<IGUIElement> getChildren(IGUIElement guiElement) {
61        for (TreeNode node : allNodes) {
62            if (node.guiElement.equals(guiElement)) {
63                List<IGUIElement> result = new ArrayList<IGUIElement>();
64               
65                if (node.children != null) {
66                    for (TreeNode child : node.children) {
67                      result.add(child.guiElement);
68                    }
69                }
70               
71                return result;
72            }
73        }
74       
75        return null;
[545]76    }
77
78    /**
[593]79     * <p>
[545]80     * TODO: comment
[593]81     * </p>
[545]82     *
[593]83     * @param guiElement
[545]84     * @return
85     */
[593]86    public IGUIElement getParent(IGUIElement guiElement) {
87        for (TreeNode node : allNodes) {
88            for (TreeNode child : node.children) {
89                if (child.guiElement.equals(guiElement)) {
90                    return node.guiElement;
91                }
92            }
93        }
94       
95        return null;
96    }
97
98    /**
99     * TODO: comment
100     *
101     * @return
102     */
[576]103    public List<IGUIElement> getRootElements() {
104        List<IGUIElement> roots = new ArrayList<IGUIElement>();
105        for (TreeNode rootChild : root.children) {
106            roots.add(rootChild.guiElement);
[545]107        }
108        return roots;
109    }
110
111    /**
[576]112     * <p>
[545]113     * TODO: comment
[576]114     * </p>
[545]115     *
[576]116     * @param root2
[545]117     * @param guiElementPath
[576]118     * @param guiElementFactory
119     * @return
[545]120     * @throws GUIModelException
121     */
[576]122    private IGUIElement integratePath(TreeNode                        parentNode,
123                                      List<? extends IGUIElementSpec> remainingPath,
124                                      IGUIElementFactory              guiElementFactory)
125        throws GUIModelException
126    {
127        IGUIElementSpec specToIntegrateElementFor = remainingPath.remove(0);
[545]128       
[593]129        List<TreeNode> matchingChildren = new ArrayList<TreeNode>();
[576]130       
131        if (parentNode.children != null) {
132            for (TreeNode child : parentNode.children) {
[593]133                if (specToIntegrateElementFor.getSimilarity(child.guiElement.getSpecification())) {
[576]134                    matchingChildren.add(child);
[545]135                }
136            }
137        }
138       
[576]139        // if we get here, the corresponding path does not exist yet. So create it
140        if (matchingChildren.size() == 0) {
[603]141            IGUIElement newElement = guiElementFactory.instantiateGUIElement
142                (specToIntegrateElementFor, parentNode.guiElement);
143           
144            matchingChildren.add(parentNode.addChild(newElement));
[576]145        }
146        else if (matchingChildren.size() > 1) {
147            throw new GUIModelException
148              ("several children of gui element " + parentNode.guiElement +
149               " match the specification " + specToIntegrateElementFor + " at the same level. " +
150               "Can not decide which is the right one.");
151        }
152       
153        if (remainingPath.size() > 0) {
[589]154            matchingChildren.get(0).guiElement.updateSpecification(specToIntegrateElementFor);
[576]155            return integratePath(matchingChildren.get(0), remainingPath, guiElementFactory);
156        }
157        else {
158            return matchingChildren.get(0).guiElement;
159        }
[545]160    }
161
[576]162    /**
163     * <p>
164     * TODO comment
165     * </p>
166     *
167     * @version $Revision: $ $Date: 17.08.2012$
168     * @author 2012, last modified by $Author: pharms$
169     */
170    private class TreeNode
171    {
172        /** */
173        private IGUIElement guiElement;
174       
175        /** */
176        private List<TreeNode> children;
177       
178        /** */
179        //private TreeNode parent;
180       
181        /**
182         * <p>
183         * TODO: comment
184         * </p>
185         *
186         * @param guiElement
187         * @return
188         */
189        private TreeNode addChild(IGUIElement guiElement)
190        {
191            if (children == null)
192            {
193                children = new ArrayList<TreeNode>();
194            }
195           
196            TreeNode child = new TreeNode();
197            child.guiElement = guiElement;
198            //child.parent = this;
199            children.add(child);
200           
201            allNodes.add(child);
202           
203            return child;
204        }
205    }
[545]206}
Note: See TracBrowser for help on using the repository browser.