Skip to main content
summaryrefslogtreecommitdiffstats
blob: fe8bc5d27d893f42646c2e4c5e104c7e32664edb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*****************************************************************************
 * Copyright (c) 2013 Cedric Dumoulin.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.core.sasheditor.di.sashmodel.query;

import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.core.sashwindows.di.AbstractPanel;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.sashwindows.di.Window;

/**
 * Class used to check and query (from tests) the DiContentProvider.
 * <br>
 * This class also provide a set of static constructor helping in writing query.
 * <br>
 * Examples:
 * <ul>
 *   <li>PanelTerm query = folder("a", page(), page() );</li>
 *   <li>query = hSash( folder( page("p1"), page() ), vSash("s2",  folder( page() ),  folder( page() )) );</li>
 *   <li>PanelTerm query = hSash( folder("f1", page("p1"), page("p2"), page("p3") ), folder("f2", page("p4") ) );</li>
 *   <li></li>
 * </ul>
 * @author cedric dumoulin
 *
 */
public class SashModelQuery {

	/**
	 * Model to query.
	 */
	protected SashModel sashModel;
	
	/**
	 * Static constructor for {@link Page}.
	 * @return
	 */
	static public Page page() {
		return new Page();
	}
	
	/**
	 * Static constructor for {@link Page}.
	 * @return
	 */
	static public Page page(String name) {
		return new Page(name);
	}
	
	/**
	 * Static constructor for {@link Folder}.
	 * @return
	 */
	static public Folder folder( Page ...pages ) {
		return new Folder(pages);
	}
	
	/**
	 * Static constructor for {@link Folder}.
	 * @return
	 */
	static public Folder folder( String name, Page ...pages ) {
		return new Folder(name, pages);
	}
	
	/**
	 * Static constructor for {@link VSash}.
	 * @return
	 */
	static public VSash vSash( String name, PanelTerm up, PanelTerm down) {
		return new VSash(name, up, down);
	}
	
	/**
	 * Static constructor for {@link VSash}.
	 * @return
	 */
	static public VSash vSash( PanelTerm up, PanelTerm down) {
		return new VSash(up, down);
	}
	
	/**
	 * Static constructor for {@link HSash}.
	 * @return
	 */
	static public HSash hSash( String name, PanelTerm left, PanelTerm right) {
		return new HSash(name, left, right);
	}
	
	/**
	 * Static constructor for {@link HSash}.
	 * @return
	 */
	static public HSash hSash( PanelTerm left, PanelTerm right) {
		return new HSash(left, right);
	}
	
	/**
	 * Constructor.
	 *
	 * @param modelMngr
	 */
	public SashModelQuery(SashWindowsMngr modelMngr) {
		this.sashModel = modelMngr.getSashModel();
	}

	/**
	 * Constructor.
	 *
	 * @param sashModel
	 */
	public SashModelQuery(SashModel sashModel) {
		this.sashModel = sashModel;
	}


	/**
	 * Check if the sashModel is conformed to the specified query.
	 * @param query
	 * @throws QueryException 
	 */
	public void assertConform(IQueryExp query) throws QueryException {
		
		EObject first;
		if( query instanceof WindowTerm) {
			first = getFirstWindowModel();
		}
		else if( query instanceof PanelTerm) {
			first = getFirstPanelModel();
		}
		else {
			throw new QueryException("Don't know how to get the model associated to the first term of the expression (" + query.toString() + ")");
		}
		
		CheckVisitor visitor = new CheckVisitor();
		query.accept(visitor, first);
	}

	/**
	 * Create an internal model conform to the specified query.
	 * Any previous model is disguarded.
	 * 
	 * @param query Should be a subtype PanelTerm (Folder, HSash, VSash)
	 * @throws QueryException 
	 */
	public void createModel(IQueryExp query) throws QueryException {
		
		Window first = sashModel.getWindows().get(0);
		if( query instanceof PanelTerm) {
			first = getFirstWindowModel();
		}
		else {
			throw new QueryException("Don't know how to get the model associated to the first term of the expression (" + query.toString() + ")");
		}
		
		// Create a surrounding WindowTerm
		WindowTerm windowTerm = new WindowTerm((PanelTerm)query);
		
		CreateModelVisitor visitor = new CreateModelVisitor();
		windowTerm.accept(visitor, first);
	}

	/**
	 * Get element in the model.
	 * Each model element whose corresponding model query part as a name is added to the result map.
	 * The element is then accessible with the name set in the query part.
	 * 
	 * @param query
	 * @return
	 * @throws QueryException
	 */
	public Map<String, Object> queryModel(IQueryExp query) throws QueryException {
		
		EObject first;
		if( query instanceof WindowTerm) {
			first = getFirstWindowModel();
		}
		else if( query instanceof PanelTerm) {
			first = getFirstPanelModel();
		}
		else {
			throw new QueryException("Don't know how to get the model associated to the first term of the expression (" + query.toString() + ")");
		}
		
		QueryVisitor visitor = new QueryVisitor();
		query.accept(visitor, first);
		
		return visitor.getResult();
	}

	/**
	 * Get the model of the first the first window (in actual implementation their is only one window).
	 * @return
	 */
	private Window getFirstWindowModel() {
		return sashModel.getWindows().get(0);
	}

	/**
	 * Get the panel of the first window (in actual implementation their is only one window).
	 * @return
	 */
	private AbstractPanel getFirstPanelModel() {
		return sashModel.getWindows().get(0).getPanel();
	}
	
}

Back to the top