Skip to content

Package: GenerateControlsHandler$1

GenerateControlsHandler$1

nameinstructionbranchcomplexitylinemethod
get(VView)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * EclipseSource Muenchen - initial API and implementation
13: *
14: *******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.editor.handler;
16:
17: import java.util.Collections;
18: import java.util.Set;
19:
20: import org.eclipse.core.commands.ExecutionEvent;
21: import org.eclipse.core.commands.ExecutionException;
22: import org.eclipse.emf.ecore.EClass;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EStructuralFeature;
25: import org.eclipse.emf.ecp.view.spi.model.VContainer;
26: import org.eclipse.emf.ecp.view.spi.model.VElement;
27: import org.eclipse.emf.ecp.view.spi.model.VView;
28: import org.eclipse.emf.ecp.view.spi.treemasterdetail.ui.swt.MasterDetailAction;
29: import org.eclipse.emf.edit.command.ChangeCommand;
30: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
31: import org.eclipse.jface.viewers.IStructuredSelection;
32: import org.eclipse.jface.window.Window;
33: import org.eclipse.jface.wizard.WizardDialog;
34: import org.eclipse.swt.widgets.Shell;
35: import org.eclipse.ui.PlatformUI;
36: import org.eclipse.ui.handlers.HandlerUtil;
37:
38: /**
39: * Handler for generating controls on a {@link org.eclipse.emf.ecp.view.spi.model.VContainedContainer VContainer} or
40: * {@link org.eclipse.emf.ecp.view.spi.model.VView VView}.
41: *
42: * @author Eugen Neufeld
43: *
44: */
45: public class GenerateControlsHandler extends MasterDetailAction {
46:
47:         @Override
48:         public void execute(final EObject object) {
49:                 final CallbackFeatureSupplier callBack = new CallbackFeatureSupplier() {
50:
51:                         @Override
52:                         public Set<EStructuralFeature> get(VView view) {
53:                                 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
54:                                 final SelectAttributesWizard selectAttributesWizard = new SelectAttributesWizard(view);
55:                                 final WizardDialog sad = new WizardDialog(shell, selectAttributesWizard);
56:                                 final int result = sad.open();
57:•                                if (result == Window.OK) {
58:                                         return selectAttributesWizard.getSelectedFeatures();
59:                                 }
60:                                 return Collections.emptySet();
61:                         }
62:                 };
63:                 execute(object, callBack);
64:
65:         }
66:
67:         /**
68:          * Creates controls based on the features supplied by the {@link CallbackFeatureSupplier}.
69:          *
70:          * @param vElement The element the action was executed on
71:          * @param featureSupplier The Supplier which provides the features to create controls for
72:          */
73:         void execute(final EObject vElement, CallbackFeatureSupplier featureSupplier) {
74:                 if (!(VView.class.isInstance(vElement) || VContainer.class.isInstance(vElement))) {
75:                         return;
76:                 }
77:
78:                 final VView view = getView(vElement);
79:                 if (view == null) {
80:                         return;
81:                 }
82:
83:                 final EClass rootEClass = view.getRootEClass();
84:
85:                 final Set<EStructuralFeature> featuresToAdd = featureSupplier.get(view);
86:                 if (featuresToAdd.isEmpty()) {
87:                         return;
88:                 }
89:                 AdapterFactoryEditingDomain.getEditingDomainFor(view).getCommandStack()
90:                         .execute(new ChangeCommand(view) {
91:                                 @Override
92:                                 protected void doExecute() {
93:                                         ControlGenerator.addControls(rootEClass, (VElement) vElement, featuresToAdd);
94:                                 }
95:                         });
96:         }
97:
98:         private VView getView(EObject object) {
99:
100:                 while (!(object instanceof VView)) {
101:                         object = object.eContainer();
102:                         if (object == null) {
103:                                 return null;
104:                         }
105:                 }
106:                 return (VView) object;
107:
108:         }
109:
110:         @Override
111:         public boolean shouldShow(EObject object) {
112:                 if (object == null) {
113:                         return false;
114:                 }
115:                 return object instanceof VView || object instanceof VContainer;
116:         }
117:
118:         @Override
119:         public Object execute(ExecutionEvent event) throws ExecutionException {
120:                 final Object selection = ((IStructuredSelection) HandlerUtil.getActiveMenuSelection(event)).getFirstElement();
121:                 if (selection == null || !(selection instanceof EObject)) {
122:                         return null;
123:                 }
124:                 execute((EObject) selection);
125:                 return null;
126:         }
127:
128:         /**
129:          * Internal Interface defined for testability. It is used to get the list of features which should be used to create
130:          * controls.
131:          */
132:         interface CallbackFeatureSupplier {
133:                 /**
134:                  * Gets the result based on the provided view.
135:                  *
136:                  * @param view The {@link VView} which is the root
137:                  * @return The Set of {@link EStructuralFeature EStructuralFeatures} supplied by this supplier
138:                  */
139:                 Set<EStructuralFeature> get(VView view);
140:         }
141: }