Skip to content

Package: DiffDialog$2

DiffDialog$2

nameinstructionbranchcomplexitylinemethod
widgetSelected(SelectionEvent)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.diffmerge.swt;
15:
16: import java.util.ArrayList;
17: import java.util.Collections;
18: import java.util.List;
19:
20: import org.eclipse.emf.common.command.BasicCommandStack;
21: import org.eclipse.emf.common.notify.AdapterFactory;
22: import org.eclipse.emf.common.util.URI;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.resource.Resource;
25: import org.eclipse.emf.ecore.resource.ResourceSet;
26: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
27: import org.eclipse.emf.ecore.util.EcoreUtil;
28: import org.eclipse.emf.ecp.diffmerge.spi.context.DefaultMergeUtil;
29: import org.eclipse.emf.ecp.diffmerge.spi.context.DiffMergeModelContext;
30: import org.eclipse.emf.ecp.ui.view.ECPRendererException;
31: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTViewRenderer;
32: import org.eclipse.emf.ecp.view.spi.model.LabelAlignment;
33: import org.eclipse.emf.ecp.view.spi.model.VContainedElement;
34: import org.eclipse.emf.ecp.view.spi.model.VControl;
35: import org.eclipse.emf.ecp.view.spi.model.VElement;
36: import org.eclipse.emf.ecp.view.spi.model.VView;
37: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
38: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
39: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
40: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
41: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
42: import org.eclipse.jface.layout.GridDataFactory;
43: import org.eclipse.jface.layout.GridLayoutFactory;
44: import org.eclipse.swt.SWT;
45: import org.eclipse.swt.custom.ScrolledComposite;
46: import org.eclipse.swt.events.SelectionAdapter;
47: import org.eclipse.swt.events.SelectionEvent;
48: import org.eclipse.swt.graphics.Point;
49: import org.eclipse.swt.layout.FillLayout;
50: import org.eclipse.swt.widgets.Button;
51: import org.eclipse.swt.widgets.Composite;
52: import org.eclipse.swt.widgets.Control;
53: import org.eclipse.swt.widgets.Group;
54: import org.eclipse.swt.widgets.Label;
55: import org.eclipse.swt.widgets.Shell;
56:
57: /**
58: * Class creating a diff Dialog.
59: *
60: * @author Eugen Neufeld
61: *
62: */
63: public class DiffDialog {
64:
65:         /**
66:          * Variant constant for indicating RAP controls.
67:          */
68:         private static final String CUSTOM_VARIANT = "org.eclipse.rap.rwt.customVariant"; //$NON-NLS-1$
69:
70:         private final VControl left;
71:         private final VControl right;
72:         private final VControl main;
73:         private final String diffAttribute;
74:         private final DiffMergeModelContext viewModelContext;
75:
76:         private VControl mergeControl;
77:         private EObject mergeDomainObject;
78:         private boolean diffConfirmed = true;
79:
80:         /**
81:          * Constructor for the diff dialog.
82:          *
83:          * @param viewModelContext the {@link org.eclipse.emf.ecp.view.spi.context.ViewModelContext ViewModelContext}
84:          * @param diffAttribute the display name of the attribute
85:          * @param left the left {@link VControl}
86:          * @param right the right {@link VControl}
87:          * @param main the main {@link VControl}
88:          */
89:         public DiffDialog(DiffMergeModelContext viewModelContext, String diffAttribute, VControl left,
90:                 VControl right, VControl main) {
91:                 this.viewModelContext = viewModelContext;
92:                 this.diffAttribute = diffAttribute;
93:                 this.left = left;
94:                 this.right = right;
95:                 this.main = main;
96:         }
97:
98:         /**
99:          * Creates the dialog content.
100:          *
101:          * @param parent the parent {@link Composite}
102:          */
103:         public void create(Composite parent) {
104:                 final Composite mainComposite = new Composite(parent, SWT.NONE);
105:                 GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(10, 10, 10, 10)
106:                         .applyTo(mainComposite);
107:                 final ScrolledComposite scrolledComposite = new ScrolledComposite(mainComposite, SWT.H_SCROLL
108:                         | SWT.V_SCROLL);
109:                 {
110:                         scrolledComposite.setExpandHorizontal(true);
111:                         scrolledComposite.setExpandVertical(true);
112:                         scrolledComposite.setShowFocusedControl(true);
113:                         GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).applyTo(scrolledComposite);
114:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(scrolledComposite);
115:
116:                         final Composite composite = new Composite(scrolledComposite, SWT.NONE);
117:                         GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false)
118:                                 .applyTo(composite);
119:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(composite);
120:
121:                         final Control title = createTitleLabel(composite);
122:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(title);
123:
124:                         final Control diff = createDiff(composite);
125:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(false, false).applyTo(diff);
126:
127:                         final Control merge = createTarget(composite);
128:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(false, false).applyTo(merge);
129:
130:                         scrolledComposite.setContent(composite);
131:                         composite.layout();
132:                         final Point point = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
133:                         scrolledComposite.setMinSize(point);
134:                 }
135:                 final Control nextPrevious = createButtonRow(mainComposite);
136:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(nextPrevious);
137:
138:         }
139:
140:         private Control createButtonRow(Composite parent) {
141:                 final Composite buttonRowComposite = new Composite(parent, SWT.NONE);
142:                 buttonRowComposite.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_buttonRow"); //$NON-NLS-1$
143:                 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(true).applyTo(buttonRowComposite);
144:
145:                 {
146:                         final Composite diffConfirmedComposite = new Composite(buttonRowComposite, SWT.NONE);
147:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).span(2, 1)
148:                                 .applyTo(diffConfirmedComposite);
149:                         diffConfirmedComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
150:                         final Button buttonDiffConfirmed = new Button(diffConfirmedComposite, SWT.CHECK);
151:                         buttonDiffConfirmed.setText(LocalizationServiceHelper.getString(DiffDialog.class,
152:                                 MessageKeys.DiffDialog_DiffConfirmed));
153:                         buttonDiffConfirmed.setSelection(diffConfirmed);
154:                         buttonDiffConfirmed.addSelectionListener(new SelectionAdapter() {
155:
156:                                 /**
157:                                  * {@inheritDoc}
158:                                  *
159:                                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
160:                                  */
161:                                 @Override
162:                                 public void widgetSelected(SelectionEvent e) {
163:                                         super.widgetSelected(e);
164:                                         diffConfirmed = buttonDiffConfirmed.getSelection();
165:                                 }
166:
167:                         });
168:                 }
169:
170:                 {
171:                         final int index = viewModelContext.getIndexOf(main);
172:
173:                         final Composite nextPreviousComposite = new Composite(buttonRowComposite, SWT.NONE);
174:                         nextPreviousComposite.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_nextPrevious"); //$NON-NLS-1$
175:                         GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(true).applyTo(nextPreviousComposite);
176:                         GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.BEGINNING).grab(true, false)
177:                                 .applyTo(nextPreviousComposite);
178:
179:                         final Button previous = new Button(nextPreviousComposite, SWT.PUSH);
180:                         previous.setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_Previous));
181:                         previous.setImage(Activator.getImage("icons/arrow_left.png")); //$NON-NLS-1$
182:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(false, false).applyTo(previous);
183:                         previous.addSelectionListener(new SelectionAdapter() {
184:
185:                                 /**
186:                                  * {@inheritDoc}
187:                                  *
188:                                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
189:                                  */
190:                                 @Override
191:                                 public void widgetSelected(SelectionEvent e) {
192:                                         super.widgetSelected(e);
193:                                         saveAndCloseDialog(previous.getShell());
194:                                         DiffDialogHelper.showDialog(viewModelContext, index - 1);
195:                                 }
196:
197:                         });
198:                         final Button next = new Button(nextPreviousComposite, SWT.PUSH);
199:                         next.setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_Next));
200:                         next.setImage(Activator.getImage("icons/arrow_right.png")); //$NON-NLS-1$
201:                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(false, false).applyTo(next);
202:                         next.addSelectionListener(new SelectionAdapter() {
203:
204:                                 /**
205:                                  * {@inheritDoc}
206:                                  *
207:                                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
208:                                  */
209:                                 @Override
210:                                 public void widgetSelected(SelectionEvent e) {
211:                                         super.widgetSelected(e);
212:                                         saveAndCloseDialog(next.getShell());
213:                                         DiffDialogHelper.showDialog(viewModelContext, index + 1);
214:                                 }
215:
216:                         });
217:
218:                         if (index == 0) {
219:                                 previous.setEnabled(false);
220:                         }
221:                         if (index + 1 == viewModelContext.getTotalNumberOfDiffs()) {
222:                                 next.setEnabled(false);
223:                         }
224:                 }
225:
226:                 {
227:                         final Button bConfirm = new Button(buttonRowComposite, SWT.PUSH);
228:                         bConfirm.setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_Confirm));
229:                         bConfirm.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_merge_confirm"); //$NON-NLS-1$
230:                         GridDataFactory.fillDefaults().align(SWT.END, SWT.BEGINNING).grab(true, false).applyTo(bConfirm);
231:                         bConfirm.addSelectionListener(new SelectionAdapter() {
232:
233:                                 /**
234:                                  * {@inheritDoc}
235:                                  *
236:                                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
237:                                  */
238:                                 @Override
239:                                 public void widgetSelected(SelectionEvent e) {
240:                                         super.widgetSelected(e);
241:                                         saveAndCloseDialog(bConfirm.getShell());
242:                                 }
243:
244:                         });
245:                 }
246:
247:                 return buttonRowComposite;
248:         }
249:
250:         /**
251:          * Creates the Merge content.
252:          *
253:          * @param parent the {@link Composite}
254:          * @return the control showing the merge
255:          */
256:         private Control createTarget(final Composite parent) {
257:                 final Group targetGroup = new Group(parent, SWT.NONE);
258:                 targetGroup.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_target"); //$NON-NLS-1$
259:                 targetGroup.setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_targetObject));
260:                 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(targetGroup);
261:
262:                 // final Label mainObject = new Label(targetGroup, SWT.NONE);
263:                 // mainObject.setText(Messages.DiffDialog_targetObject);
264:                 // mainObject.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_merge_label"); //$NON-NLS-1$
265:                 // GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).applyTo(mainObject);
266:
267:                 mergeControl = EcoreUtil.copy(main);
268:                 final ResourceSet resourceSet = new ResourceSetImpl();
269:                 final AdapterFactoryEditingDomain domain = new AdapterFactoryEditingDomain(
270:                         new ComposedAdapterFactory(new AdapterFactory[] {
271:                                 new ReflectiveItemProviderAdapterFactory(),
272:                                 new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) }),
273:                         new BasicCommandStack(), resourceSet);
274:                 mergeDomainObject = EcoreUtil.copy(viewModelContext.getDomainModel());
275:                 resourceSet.eAdapters().add(new AdapterFactoryEditingDomain.EditingDomainProvider(domain));
276:                 final Resource resource = resourceSet.createResource(URI.createURI("VIRTUAL_URI_TEMP")); //$NON-NLS-1$
277:                 resource.getContents().add(mergeDomainObject);
278:                 createControl(targetGroup, mergeControl, mergeDomainObject, false);
279:
280:                 return targetGroup;
281:         }
282:
283:         /**
284:          * Creates the Diff content.
285:          *
286:          * @param parent the {@link Composite}
287:          * @return the control showing the diff
288:          */
289:         private Control createDiff(final Composite parent) {
290:                 final Group group = new Group(parent, SWT.NONE);
291:                 group.setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_DifferenceGroup));
292:                 group.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_diff"); //$NON-NLS-1$
293:                 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(group);
294:
295:                 final Label leftObject = new Label(group, SWT.NONE);
296:                 leftObject
297:                         .setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_leftObject + ":")); //$NON-NLS-1$
298:                 leftObject.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_diff_left"); //$NON-NLS-1$
299:                 GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).span(2, 1).applyTo(leftObject);
300:
301:                 createControl(group, EcoreUtil.copy(left), EcoreUtil.copy(viewModelContext.getLeftModel()), true);
302:
303:                 final Button bReplaceWithLeft = new Button(group, SWT.PUSH);
304:                 bReplaceWithLeft.setText(LocalizationServiceHelper
305:                         .getString(DiffDialog.class, MessageKeys.DiffDialog_replaceWithLeft));
306:                 bReplaceWithLeft.setImage(Activator.getImage("icons/arrow_down.png")); //$NON-NLS-1$
307:                 bReplaceWithLeft.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_diff_leftReplace"); //$NON-NLS-1$
308:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(false, false).applyTo(bReplaceWithLeft);
309:                 bReplaceWithLeft.addSelectionListener(new SelectionAdapter() {
310:
311:                         /**
312:                          * {@inheritDoc}
313:                          *
314:                          * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
315:                          */
316:                         @Override
317:                         public void widgetSelected(SelectionEvent e) {
318:                                 super.widgetSelected(e);
319:                                 replaceMainWith(left, viewModelContext.getLeftModel());
320:                         }
321:
322:                 });
323:
324:                 final Label rightObject = new Label(group, SWT.NONE);
325:                 rightObject
326:                         .setText(LocalizationServiceHelper.getString(DiffDialog.class, MessageKeys.DiffDialog_rightObject + ":")); //$NON-NLS-1$
327:                 rightObject.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_diff_right"); //$NON-NLS-1$
328:                 GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).span(2, 1)
329:                         .applyTo(rightObject);
330:
331:                 createControl(group, EcoreUtil.copy(right), EcoreUtil.copy(viewModelContext.getRightModel()), true);
332:
333:                 final Button bReplaceWithRight = new Button(group, SWT.PUSH);
334:                 bReplaceWithRight.setText(LocalizationServiceHelper.getString(DiffDialog.class,
335:                         MessageKeys.DiffDialog_replaceWithRight));
336:                 bReplaceWithRight.setImage(Activator.getImage("icons/arrow_down.png")); //$NON-NLS-1$
337:                 bReplaceWithLeft.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_diff_rightReplace"); //$NON-NLS-1$
338:                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.BEGINNING).grab(false, false).applyTo(bReplaceWithRight);
339:                 bReplaceWithRight.addSelectionListener(new SelectionAdapter() {
340:
341:                         /**
342:                          * {@inheritDoc}
343:                          *
344:                          * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
345:                          */
346:                         @Override
347:                         public void widgetSelected(SelectionEvent e) {
348:                                 super.widgetSelected(e);
349:                                 replaceMainWith(right, viewModelContext.getRightModel());
350:                         }
351:
352:                 });
353:
354:                 return group;
355:         }
356:
357:         private void createControl(Composite parent, VControl control, EObject domainObject, boolean readonly) {
358:                 final VView leftView = VViewFactory.eINSTANCE.createView();
359:                 leftView.setRootEClass(domainObject.eClass());
360:                 final VControl leftControl = control;
361:                 leftControl.setReadonly(readonly);
362:                 leftControl.setLabelAlignment(LabelAlignment.NONE);
363:                 leftView.getChildren().add(leftControl);
364:                 try {
365:                         ECPSWTViewRenderer.INSTANCE.render(parent, domainObject, leftView);
366:                 } catch (final ECPRendererException ex) {
367:                         ex.printStackTrace();
368:                 }
369:         }
370:
371:         private void replaceMainWith(VControl replaceControl, EObject replaceDomainModel) {
372:                 DefaultMergeUtil.copyValues(replaceControl, replaceDomainModel, mergeControl, mergeDomainObject);
373:         }
374:
375:         /**
376:          * Creates the title {@link Label}.
377:          *
378:          * @param parent the {@link Composite}
379:          * @return the control showing the title
380:          */
381:         private Control createTitleLabel(final Composite parent) {
382:                 final Label title = new Label(parent, SWT.NONE);
383:                 final List<String> breadCrumb = new ArrayList<String>();
384:                 breadCrumb.add(diffAttribute);
385:                 EObject parentEObject = main.eContainer();
386:                 while (!VView.class.isInstance(parentEObject)) {
387:                         final VElement vElement = (VElement) parentEObject;
388:                         // FIXME hack -> will filter out groups
389:                         if (!VContainedElement.class.isInstance(vElement)) {
390:                                 breadCrumb.add(vElement.getName());
391:                         }
392:                         parentEObject = parentEObject.eContainer();
393:                 }
394:
395:                 Collections.reverse(breadCrumb);
396:                 final StringBuilder sb = new StringBuilder();
397:                 for (final String bc : breadCrumb) {
398:                         if (sb.length() != 0) {
399:                                 sb.append(">"); //$NON-NLS-1$
400:                         }
401:                         sb.append(bc);
402:                 }
403:                 title.setText(sb.toString());
404:                 title.setData(CUSTOM_VARIANT, "org_eclipse_emf_ecp_compare_dialog_title"); //$NON-NLS-1$
405:                 return title;
406:         }
407:
408:         private void saveAndCloseDialog(final Shell shell) {
409:                 DefaultMergeUtil.copyValues(mergeControl, mergeDomainObject, main, viewModelContext.getDomainModel());
410:
411:                 viewModelContext.markControl(main, diffConfirmed);
412:
413:                 shell.dispose();
414:         }
415: }