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
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.LinkedList;
11import java.util.List;
12
13/**
14 * <p>
15 * The goal of a GUI model is to correctly l
16 * </p>
17 *
18 * @version $Revision: $ $Date: 14.08.2012$
19 * @author 2012, last modified by $Author: pharms$
20 */
21public class GUIModel {
22   
23    /**
24     *
25     */
26    private TreeNode root = new TreeNode();
27   
28    /**
29     *
30     */
31    private List<TreeNode> allNodes = new ArrayList<TreeNode>();
32
33    /**
34     * TODO: comment
35     *
36     * @param currentGUIElementPath
37     * @return
38     * @throws GUIModelException
39     */
40    public IGUIElement integratePath(List<? extends IGUIElementSpec> guiElementPath,
41                                     IGUIElementFactory              guiElementFactory)
42        throws GUIModelException
43    {
44        List<IGUIElementSpec> remainingPath = new LinkedList<IGUIElementSpec>();
45       
46        for (IGUIElementSpec spec : guiElementPath)
47        {
48            remainingPath.add(spec);
49        }
50       
51        return integratePath(root, remainingPath, guiElementFactory);
52    }
53
54    /**
55     * TODO: comment
56     *
57     * @param root
58     * @return
59     */
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;
76    }
77
78    /**
79     * <p>
80     * TODO: comment
81     * </p>
82     *
83     * @param guiElement
84     * @return
85     */
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     */
103    public List<IGUIElement> getRootElements() {
104        List<IGUIElement> roots = new ArrayList<IGUIElement>();
105        for (TreeNode rootChild : root.children) {
106            roots.add(rootChild.guiElement);
107        }
108        return roots;
109    }
110
111    /**
112     * <p>
113     * TODO: comment
114     * </p>
115     *
116     * @param root2
117     * @param guiElementPath
118     * @param guiElementFactory
119     * @return
120     * @throws GUIModelException
121     */
122    private IGUIElement integratePath(TreeNode                        parentNode,
123                                      List<? extends IGUIElementSpec> remainingPath,
124                                      IGUIElementFactory              guiElementFactory)
125        throws GUIModelException
126    {
127        IGUIElementSpec specToIntegrateElementFor = remainingPath.remove(0);
128       
129        List<TreeNode> matchingChildren = new ArrayList<TreeNode>();
130       
131        if (parentNode.children != null) {
132            for (TreeNode child : parentNode.children) {
133                if (specToIntegrateElementFor.getSimilarity(child.guiElement.getSpecification())) {
134                    matchingChildren.add(child);
135                }
136            }
137        }
138       
139        // if we get here, the corresponding path does not exist yet. So create it
140        if (matchingChildren.size() == 0) {
141            IGUIElement newElement = guiElementFactory.instantiateGUIElement
142                (specToIntegrateElementFor, parentNode.guiElement);
143           
144            matchingChildren.add(parentNode.addChild(newElement));
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) {
154            matchingChildren.get(0).guiElement.updateSpecification(specToIntegrateElementFor);
155            return integratePath(matchingChildren.get(0), remainingPath, guiElementFactory);
156        }
157        else {
158            return matchingChildren.get(0).guiElement;
159        }
160    }
161
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    }
206}
Note: See TracBrowser for help on using the repository browser.