Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b222ad7eb32910d7c6601cadc15d408a44155d0 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 *
 * 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:
 *  Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.revision.tool.ui;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.ResourceSetListener;
import org.eclipse.emf.transaction.ResourceSetListenerImpl;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.revision.tool.core.ReviewResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;

/**
 * the review editor that display reviews or comments 
 *
 */
public class ReviewsEditor extends ViewPart implements ITabbedPropertySheetPageContributor{

	protected static final String ORG_ECLIPSE_PAPYRUS_REVISIONTOOL = "org.eclipse.papyrus.revisiontool";
	protected ReviewResourceManager reviewResourceManager= new ReviewResourceManager();
	private TreeViewer viewer;
	protected List<IPropertySheetPage> propertySheetPages = new ArrayList<IPropertySheetPage>();
	protected ResourceSetListenerImpl reviewEditorResourceListener=null;

	private static FontData[] getModifiedFontData(FontData[] originalData, int additionalStyle) {
		FontData[] styleData = new FontData[originalData.length];
		for (int i = 0; i < styleData.length; i++) {
			FontData base = originalData[i];
			styleData[i] = new FontData(base.getName(), base.getHeight(), base.getStyle() | additionalStyle);
		}
		return styleData;
	}
	/**
	 * only his class can create it
	 * @return review resource manger
	 */
	public ReviewResourceManager getReviewResourceManager(){
		return reviewResourceManager;
	}


	@Override
	public void dispose() {
		reviewResourceManager.getDomain().removeResourceSetListener(getResourceListener());
		reviewResourceManager.dispose();
		reviewResourceManager=null;
		super.dispose();

	}
	/**
	 * create the contextual menu
	 */
	protected  void createContextMenu() {
		// Create menu manager.
		MenuManager menuMgr = new MenuManager("#PopMenu");
		menuMgr.setRemoveAllWhenShown(true);
		// Register menu for extension.
		getSite().registerContextMenu(menuMgr, viewer);
		menuMgr.addMenuListener(new IMenuListener() {
			@Override
			public void menuAboutToShow(IMenuManager manager) {
				filterContextMenu(manager);
			}
		});

		// Create menu.
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().setSelectionProvider(viewer);


	}
	/**
	 * filter contextual menu
	 * @param mgr the menumanager
	 */
	protected void filterContextMenu(IMenuManager mgr) {
		IContributionItem[] contributionItems=mgr.getItems();
		for (int i = 0; i < contributionItems.length; i++) {
			if(!(contributionItems[i].getId().startsWith(ORG_ECLIPSE_PAPYRUS_REVISIONTOOL))){
				mgr.remove(contributionItems[i]);
			}
		}

	}

	/**
	 * 
	 * @return listener to refresh the editor
	 */
	public ResourceSetListener getResourceListener(){
		if(reviewEditorResourceListener==null){
			reviewEditorResourceListener= new ResourceSetListenerImpl(){

				@Override
				public void resourceSetChanged(ResourceSetChangeEvent event) {
					HashSet<Resource> notifiers=new HashSet<Resource>();
					boolean closeSignal=false;

					for (Notification notification : event.getNotifications()) {
						if(notification.getNotifier() instanceof EObject){
							notifiers.add(((EObject)notification.getNotifier()).eResource());
						}
						if((notification.getNotifier()) instanceof Resource){
							if(((reviewResourceManager.getWorkingModel().eResource()==null)&&notification.getEventType()==Notification.REMOVE_MANY)){
								closeSignal=true;
							}
						}
					}
					for ( Resource currentResource : notifiers) {
						if(reviewResourceManager.getCurrentReviewModel().eResource().equals(currentResource)){
							viewer.setInput(reviewResourceManager.getCurrentReviewModel());
						}
					}
					if (closeSignal){
						IViewPart part=(IViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.papyrus.revisiontool.commentview");
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(part);
					}
				}
				@Override
				public boolean isPostcommitOnly() {
					return true;
				}

			};
		}
		return reviewEditorResourceListener;
	}

	/**
	 * add a review
	 * @param container
	 */
	public void addAReview(Element container) {
		reviewResourceManager.addAReview(container);
		viewer.setInput(reviewResourceManager.getCurrentReviewModel());
		reviewResourceManager.getDomain().addResourceSetListener(getResourceListener());
	}
	/**
	 * create action in the view
	 */
	public void createActions() {
		Action	loadReview = new Action("Load a review model") {
			@Override
			public void run() {
				Model reviewModel=reviewResourceManager.loadReviewModel();
				viewer.setInput(reviewModel);
				reviewResourceManager.getDomain().addResourceSetListener(getResourceListener());
			}
			@Override
			public String getDescription() {
				return "Load a review Model";
			}
		};

		Action	addReview = new Action("Add a review") {
			@Override
			public void run() {
				addAReview(null);	
			}

			@Override
			public String getDescription() {
				return "Add a review";
			}
		};

		try {
			loadReview.setImageDescriptor( ImageDescriptor.createFromURL(new URL("platform:/plugin/org.eclipse.papyrus.revisiontool/img/load.png")));
			addReview.setImageDescriptor( ImageDescriptor.createFromURL(new URL("platform:/plugin/org.eclipse.papyrus.revisiontool/img/Add.png")));

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
		mgr.add(loadReview);
		mgr.add(addReview);
	}


	@Override
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent,  SWT.MULTI | SWT.FULL_SELECTION);
		viewer.setContentProvider(new ReviewsTreeContentProvider());
		FontData[] boldFontData= getModifiedFontData(viewer.getTree().getFont().getFontData(), SWT.BOLD);
		FontData[] italicFontData= getModifiedFontData(viewer.getTree().getFont().getFontData(), SWT.ITALIC);
		Font boldFont = new Font(Display.getCurrent(), boldFontData);
		Font italicFont = new Font(Display.getCurrent(), italicFontData);
		viewer.setLabelProvider(new ReviewsTreeLabelProvider(boldFont,  italicFont));
		createContextMenu();
		createActions();


		// this code comes from eclipse  in oder to display multi line in a tree
		Listener paintListener = new Listener() {
			public void handleEvent(Event event) {
				switch(event.type) {       
				case SWT.MeasureItem: {
					TreeItem item = (TreeItem)event.item;
					String text = item.getText();
					Point size = event.gc.textExtent(text);
					event.width = size.x+20;
					event.height = Math.max(event.height, size.y);
					break;
				}
				case SWT.PaintItem: {
					TreeItem item = (TreeItem)event.item;
					String text = item.getText();
					Point size = event.gc.textExtent(text);                
					int offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
					event.gc.drawText(text, event.x, event.y + offset2, true);
					break;
				}
				case SWT.EraseItem: {  
					event.detail &= ~SWT.FOREGROUND;
					break;
				}
				}
			}
		};
		viewer.getTree().addListener(SWT.MeasureItem, paintListener);
		viewer.getTree().addListener(SWT.EraseItem, paintListener);
		getSite().setSelectionProvider(viewer);



	}
	/**
	 * launch the model revision and load review model in the editor
	 */
	public void startModeRevision(){
		viewer.setInput(reviewResourceManager.getCurrentReviewModel());
		reviewResourceManager.getDomain().addResourceSetListener(getResourceListener());
		reviewResourceManager.startModeRevision();
	}
	/**
	 * stop the model revision and load review model in the editor
	 */
	public void stopModelRevision(){
		viewer.setInput(reviewResourceManager.getCurrentReviewModel());
		reviewResourceManager.getDomain().addResourceSetListener(getResourceListener());
		reviewResourceManager.stopModelRevision();
		
	}
	/**
	 * Retrieves the {@link IPropertySheetPage} that his Model Explorer uses.
	 *
	 * @return
	 */
	private IPropertySheetPage getPropertySheetPage() {
		try {
			final IMultiDiagramEditor multiDiagramEditor = getReviewResourceManager().getServiceRegistry().getService(IMultiDiagramEditor.class);

			if (multiDiagramEditor != null) {
				if (multiDiagramEditor instanceof ITabbedPropertySheetPageContributor) {
					ITabbedPropertySheetPageContributor contributor = (ITabbedPropertySheetPageContributor) multiDiagramEditor;
					IPropertySheetPage propertySheetPage = new TabbedPropertySheetPage(contributor);
					this.propertySheetPages.add(propertySheetPage);
					return propertySheetPage;
				}
			}
		} catch (ServiceException ex) {
			System.err.println(ex);
		}
		return null;
	}

	public Object getAdapter(Class adapter) {
		if ((IPropertySheetPage.class.equals(adapter))) {
			return getPropertySheetPage();
		}

		else {
			return super.getAdapter(adapter);
		}
	}
	
	@Override
	public void setFocus() {


	}

	@Override
	public String getContributorId() {
		return "TreeOutlinePage";
	}

}

Back to the top