Skip to content

Package: CreateDialog

CreateDialog

nameinstructionbranchcomplexitylinemethod
CreateDialog(Shell, EObject)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
configureShell(Shell)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createButtonsForButtonBar(Composite)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createDialogArea(Composite)
M: 1 C: 104
99%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 24
100%
M: 0 C: 1
100%
getCreatedInstance()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
okPressed()
M: 72 C: 16
18%
M: 5 C: 1
17%
M: 3 C: 1
25%
M: 15 C: 6
29%
M: 0 C: 1
100%
updateOKButtonEnablement()
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Clemens Elflein - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.ui;
15:
16: import java.util.Set;
17:
18: import org.eclipse.emf.common.util.Diagnostic;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecore.util.Diagnostician;
21: import org.eclipse.emf.ecp.ui.view.ECPRendererException;
22: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTView;
23: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTViewRenderer;
24: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
25: import org.eclipse.emf.ecp.view.spi.model.VViewModelProperties;
26: import org.eclipse.emf.ecp.view.spi.validation.ValidationService;
27: import org.eclipse.emf.ecp.view.spi.validation.ViewValidationListener;
28: import org.eclipse.emfforms.spi.editor.messages.Messages;
29: import org.eclipse.emfforms.swt.core.EMFFormsSWTConstants;
30: import org.eclipse.jface.dialogs.Dialog;
31: import org.eclipse.jface.dialogs.IDialogConstants;
32: import org.eclipse.jface.dialogs.MessageDialog;
33: import org.eclipse.swt.SWT;
34: import org.eclipse.swt.custom.ScrolledComposite;
35: import org.eclipse.swt.graphics.Color;
36: import org.eclipse.swt.layout.FillLayout;
37: import org.eclipse.swt.layout.GridData;
38: import org.eclipse.swt.layout.GridLayout;
39: import org.eclipse.swt.widgets.Button;
40: import org.eclipse.swt.widgets.Composite;
41: import org.eclipse.swt.widgets.Control;
42: import org.eclipse.swt.widgets.Shell;
43:
44: /**
45: * The Class CreateDialog allows initializing newly created EObjects.
46: * It also can be used to create an EObject and initialize it directly.
47: *
48: * @author Clemens Elflein
49: */
50: public class CreateDialog extends Dialog {
51:
52:         /** The new object. */
53:         private final EObject newObject;
54:         private ECPSWTView ecpswtView;
55:
56:         /**
57:          * Instantiates a new dialog.
58:          *
59:          * @param parent the parent
60:          * @param createdInstance an EObject to initialize
61:          */
62:         public CreateDialog(Shell parent, EObject createdInstance) {
63:                 super(parent);
64:                 newObject = createdInstance;
65:                 setShellStyle(getShellStyle() | SWT.RESIZE);
66:         }
67:
68:         @Override
69:         protected void configureShell(Shell newShell) {
70:                 super.configureShell(newShell);
71:                 newShell.setText(Messages.CreateDialog_CreateDialogTitle + newObject.eClass().getName());
72:                 newShell.setMinimumSize(300, 150);
73:                 newShell.setBackground(new Color(newShell.getDisplay(), 255, 255, 255));
74:                 newShell.setBackgroundMode(SWT.INHERIT_FORCE);
75:         }
76:
77:         @Override
78:         protected Control createDialogArea(Composite parent) {
79:
80:                 final GridData parentData = new GridData(SWT.FILL, SWT.FILL, true, true);
81:                 parent.setLayout(new GridLayout(1, true));
82:                 parent.setLayoutData(parentData);
83:
84:                 final ScrolledComposite wrapper = new ScrolledComposite(parent, SWT.V_SCROLL);
85:                 wrapper.setExpandHorizontal(true);
86:                 wrapper.setExpandVertical(true);
87:                 final FillLayout wrapperLayout = new FillLayout();
88:                 wrapperLayout.marginHeight = 10;
89:                 wrapperLayout.marginWidth = 10;
90:                 wrapper.setLayout(wrapperLayout);
91:                 wrapper.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
92:
93:                 final Composite emfFormsParent = new Composite(wrapper, SWT.NONE);
94:                 wrapper.setContent(emfFormsParent);
95:                 emfFormsParent.setLayout(new GridLayout());
96:
97:                 try {
98:                         final VViewModelProperties properties = VViewFactory.eINSTANCE.createViewModelLoadingProperties();
99:                         properties.addInheritableProperty(EMFFormsSWTConstants.USE_ON_MODIFY_DATABINDING_KEY,
100:                                 EMFFormsSWTConstants.USE_ON_MODIFY_DATABINDING_VALUE);
101:                         ecpswtView = ECPSWTViewRenderer.INSTANCE.render(emfFormsParent, newObject, properties);
102:                         final ValidationService validationService = ecpswtView.getViewModelContext()
103:                                 .getService(ValidationService.class);
104:                         validationService.registerValidationListener(new ViewValidationListener() {
105:                                 @Override
106:                                 public void onNewValidation(Set<Diagnostic> validationResults) {
107:                                         updateOKButtonEnablement();
108:                                 }
109:                         });
110:                 } catch (final ECPRendererException e) {
111:                 }
112:
113:                 wrapper.setMinSize(wrapper.computeSize(SWT.DEFAULT, SWT.DEFAULT));
114:
115:                 return parent;
116:         }
117:
118:         @Override
119:         protected void okPressed() {
120:                 getParentShell().forceFocus();
121:                 final Diagnostic result = Diagnostician.INSTANCE.validate(newObject);
122:•                if (result.getSeverity() == Diagnostic.OK) {
123:                         super.okPressed();
124:                 } else {
125:                         // Get the error count and create an appropriate Error message:
126:                         final int errorCount = result.getChildren().size();
127:
128:                         final StringBuilder sb = new StringBuilder();
129:
130:                         sb.append(errorCount);
131:                         sb.append(" "); //$NON-NLS-1$
132:•                        sb.append(errorCount == 1 ? Messages.CreateDialog_ErrorSingular : Messages.CreateDialog_ErrorPlural);
133:                         sb.append(Messages.CreateDialog_ErrorOccuredMessage);
134:
135:                         int messageCount = 1;
136:•                        for (final Diagnostic d : result.getChildren()) {
137:                                 sb.append("\r\n"); //$NON-NLS-1$
138:                                 sb.append(messageCount++);
139:                                 sb.append(". "); //$NON-NLS-1$
140:                                 sb.append(d.getMessage());
141:                         }
142:
143:                         final String errorMessage = sb.toString();
144:
145:                         MessageDialog.open(MessageDialog.ERROR, getParentShell(), Messages.CreateDialog_ErrorDialogTitle,
146:                                 errorMessage, SWT.NONE);
147:                 }
148:         }
149:
150:         /**
151:          * Gets the created instance or the updated one, if it was passed in the constructor.
152:          * All fields are initialized with user inputs
153:          *
154:          * @return the created instance
155:          */
156:         public EObject getCreatedInstance() {
157:                 return newObject;
158:         }
159:
160:         @Override
161:         protected void createButtonsForButtonBar(Composite parent) {
162:                 super.createButtonsForButtonBar(parent);
163:                 updateOKButtonEnablement();
164:         }
165:
166:         private void updateOKButtonEnablement() {
167:                 final Button button = getButton(IDialogConstants.OK_ID);
168:•                if (button == null) {
169:                         return;
170:                 }
171:                 if (ecpswtView.getViewModelContext().getViewModel().getDiagnostic()
172:•                        .getHighestSeverity() >= Diagnostic.ERROR) {
173:                         button.setEnabled(false);
174:                 } else {
175:                         button.setEnabled(true);
176:                 }
177:         }
178: }