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

Last change on this file since 597 was 593, checked in by pharms, 12 years ago
  • added possibility to retrieve a parent GUI element
  • removed usage of linked list
File size: 5.5 KB
Line 
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;
10import java.util.List;
11
12/**
13 * <p>
14 * The goal of a GUI model is to correctly l
15 * </p>
16 *
17 * @version $Revision: $ $Date: 14.08.2012$
18 * @author 2012, last modified by $Author: pharms$
19 */
20public class GUIModel {
21   
22    /**
23     *
24     */
25    private TreeNode root = new TreeNode();
26   
27    /**
28     *
29     */
30    private List<TreeNode> allNodes = new ArrayList<TreeNode>();
31
32    /**
33     * TODO: comment
34     *
35     * @param currentGUIElementPath
36     * @return
37     * @throws GUIModelException
38     */
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>();
63               
64                if (node.children != null) {
65                    for (TreeNode child : node.children) {
66                      result.add(child.guiElement);
67                    }
68                }
69               
70                return result;
71            }
72        }
73       
74        return null;
75    }
76
77    /**
78     * <p>
79     * TODO: comment
80     * </p>
81     *
82     * @param guiElement
83     * @return
84     */
85    public IGUIElement getParent(IGUIElement guiElement) {
86        for (TreeNode node : allNodes) {
87            for (TreeNode child : node.children) {
88                if (child.guiElement.equals(guiElement)) {
89                    return node.guiElement;
90                }
91            }
92        }
93       
94        return null;
95    }
96
97    /**
98     * TODO: comment
99     *
100     * @return
101     */
102    public List<IGUIElement> getRootElements() {
103        List<IGUIElement> roots = new ArrayList<IGUIElement>();
104        for (TreeNode rootChild : root.children) {
105            roots.add(rootChild.guiElement);
106        }
107        return roots;
108    }
109
110    /**
111     * <p>
112     * TODO: comment
113     * </p>
114     *
115     * @param root2
116     * @param guiElementPath
117     * @param guiElementFactory
118     * @return
119     * @throws GUIModelException
120     */
121    private IGUIElement integratePath(TreeNode                        parentNode,
122                                      List<? extends IGUIElementSpec> remainingPath,
123                                      IGUIElementFactory              guiElementFactory)
124        throws GUIModelException
125    {
126        IGUIElementSpec specToIntegrateElementFor = remainingPath.remove(0);
127       
128        List<TreeNode> matchingChildren = new ArrayList<TreeNode>();
129       
130        if (parentNode.children != null) {
131            for (TreeNode child : parentNode.children) {
132                if (specToIntegrateElementFor.getSimilarity(child.guiElement.getSpecification())) {
133                    matchingChildren.add(child);
134                }
135            }
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            matchingChildren.get(0).guiElement.updateSpecification(specToIntegrateElementFor);
153            return integratePath(matchingChildren.get(0), remainingPath, guiElementFactory);
154        }
155        else {
156            return matchingChildren.get(0).guiElement;
157        }
158    }
159
160    /**
161     * <p>
162     * TODO comment
163     * </p>
164     *
165     * @version $Revision: $ $Date: 17.08.2012$
166     * @author 2012, last modified by $Author: pharms$
167     */
168    private class TreeNode
169    {
170        /** */
171        private IGUIElement guiElement;
172       
173        /** */
174        private List<TreeNode> children;
175       
176        /** */
177        //private TreeNode parent;
178       
179        /**
180         * <p>
181         * TODO: comment
182         * </p>
183         *
184         * @param guiElement
185         * @return
186         */
187        private TreeNode addChild(IGUIElement guiElement)
188        {
189            if (children == null)
190            {
191                children = new ArrayList<TreeNode>();
192            }
193           
194            TreeNode child = new TreeNode();
195            child.guiElement = guiElement;
196            //child.parent = this;
197            children.add(child);
198           
199            allNodes.add(child);
200           
201            return child;
202        }
203    }
204}
Note: See TracBrowser for help on using the repository browser.