Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 66c130333a6c9bb4d0ab824ed0d37fb0d37dc0cb (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*******************************************************************************
 * Copyright (c) 2013 Atos
 * 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:
 *     Arthur Daussy - initial implementation
 *******************************************************************************/
package org.eclipse.papyrus.team.collaborative.strategy.ui.dialogs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForWorkbenchPage;
import org.eclipse.papyrus.uml.tools.providers.SemanticUMLContentProvider;
import org.eclipse.papyrus.views.modelexplorer.CustomCommonViewer;
import org.eclipse.papyrus.views.modelexplorer.matching.IMatchingItem;
import org.eclipse.papyrus.views.modelexplorer.matching.ModelElementItemMatchingItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;


/**
 * The Class PreviewDialog.
 * The preview dialog is a generic dialog use to display feedback to the user using specific {@link ILabelProvider}
 */
public class PreviewDialog extends TitleAreaDialog {


	/** The title. */
	private String title;

	/** The message. */
	private String message;

	/** The label provider. */
	private ILabelProvider labelProvider;

	/** The tree viewer. */
	private TreeViewer treeViewer;

	/** The objects to reveal. */
	private Collection<EObject> objectsToReveal;



	/**
	 * Sets the objects to reveal.
	 * 
	 * @param objectToReveal
	 *        the new objects to reveal
	 */
	public void setObjectsToReveal(Collection<EObject> objectToReveal) {
		this.objectsToReveal = objectToReveal;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 *        the parent
	 * @return the control
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage(message);
		setTitle(title);
		Composite area = (Composite)super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		treeViewer = new CustomCommonViewer("org.eclipse.papyrus.modelexplorer.modelexplorer", container, SWT.BORDER);
		treeViewer.setContentProvider(new SemanticUMLContentProvider() {

//			@Override
//			protected static EObject[] getRoots(ResourceSet root) {
//				if(root instanceof ModelSet) {
//					ModelSet modelSet = (ModelSet)root;
//					UmlModel umlModel = (UmlUtils.getUmlModel(modelSet));
//					
//					if(umlModel == null)
//						return null;
//					
//					EList<EObject> contents = umlModel.getResource().getContents();
//					ArrayList<EObject> result = new ArrayList<EObject>();
//					Iterator<EObject> iterator = contents.iterator();
//					while(iterator.hasNext()) {
//						EObject eObject = (EObject)iterator.next();
//						//Shall be improved
//						if(eObject.eClass().getEPackage().getNsURI().contains("uml")) {
//							result.add(eObject);
//						}
//					}
//				}
//				return result.toArray(new EObject[result.size()]);
//			}

			@Override
			public Object[] getChildren(Object parentElement) {
				//Remove duplicate
				Object[] superChildren = super.getChildren(parentElement);

				List<Object> result = new ArrayList<Object>();
				for(int i = 0; i < superChildren.length; i++) {
					Object c = superChildren[i];
					if(!result.contains(c)) {
						result.add(c);
					}
				}
				return result.toArray(new Object[result.size()]);
			}
		});
		treeViewer.setLabelProvider(labelProvider);

		try {
			IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			ServicesRegistry serviceRegistry = ServiceUtilsForWorkbenchPage.getInstance().getServiceRegistry(activePage);
			treeViewer.setInput(serviceRegistry);
			if(objectsToReveal != null) {
				reveal(objectsToReveal);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return area;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 *        the parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 * 
	 * @return the initial size
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(600, 600);
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 *        the parent shell
	 * @param labelProvider
	 *        the label provider
	 * @param title
	 *        the title
	 * @param message
	 *        the message
	 */
	public PreviewDialog(Shell parentShell, ILabelProvider labelProvider, String title, String message) {
		super(parentShell);
		setShellStyle(SWT.SHELL_TRIM);
		setHelpAvailable(false);
		this.title = title;
		this.message = message;
		this.labelProvider = labelProvider;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.window.Window#open()
	 */
	@Override
	public int open() {
		int result = super.open();
		return result;
	}



	/**
	 * Reveal.
	 * 
	 * @param elementList
	 *        the element list
	 */
	public void reveal(Iterable<?> elementList) {
		ArrayList<IMatchingItem> matchingItemsToSelect = new ArrayList<IMatchingItem>();
		// filter out non EMF objects
		Iterable<EObject> list = Iterables.transform(Iterables.filter(elementList, EObject.class), new Function<Object, EObject>() {

			public EObject apply(Object from) {
				return (EObject)from;
			}
		});

		for(EObject currentEObject : list) {
			//TODO The custom ModelExplorer Revealer feature has not yet been backported. 
			//See bug 426360. For now, we only reveal the given EObjects.
			//EObject itemToSelect = ModelExplorerRevealerManager.getTargetObjectToRegveal(currentEObject);
			matchingItemsToSelect.add(new ModelElementItemMatchingItem(currentEObject));

			// Does the content provider exist?
			// if(treeViewer.getContentProvider() != null) {

			/*
			 * reveal the ancestors tree using expandToLevel on each of them
			 * in the good order. This is a lot faster than going through the whole tree
			 * using getChildren of the ContentProvider since our Viewer uses a Hashtable
			 * to keep track of the revealed elements.
			 * 
			 * However we need to use a dedicated MatchingItem to do the matching,
			 * and a specific comparer in our viewer so than the equals of MatchingItem is
			 * used in priority.
			 * 
			 * Please refer to MatchingItem for more infos.
			 */
			//for(IMatchingItem item : ModelExplorerRevealerManager.getChainToReveal(itemToSelect)) {
			//	treeViewer.expandToLevel(item, 1);
			//}
			//}
		}
		treeViewer.expandToLevel(new StructuredSelection(matchingItemsToSelect), TreeViewer.ALL_LEVELS);
	}

	/**
	 * Select reveal.
	 * 
	 * @param selection
	 *        the selection
	 */
	public void selectReveal(ISelection selection) {
		if(treeViewer != null) {
			treeViewer.setSelection(selection, true);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.core.ui.IRevealSemanticElement#revealSemanticElement(java.util.List)
	 */
	/**
	 * Reveal semantic element.
	 * 
	 * @param elementList
	 *        the element list
	 */
	public void revealSemanticElement(List<?> elementList) {
		reveal(elementList);
	}

}

Back to the top