Skip to content

Package: CreateNewChildDialog$2

CreateNewChildDialog$2

nameinstructionbranchcomplexitylinemethod
open(OpenEvent)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 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-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: * Clemens Elflein - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.ui;
15:
16: import java.util.ArrayList;
17: import java.util.Collection;
18: import java.util.List;
19:
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecp.common.spi.ChildrenDescriptorCollector;
22: import org.eclipse.emf.edit.command.CommandParameter;
23: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
24: import org.eclipse.emf.edit.domain.EditingDomain;
25: import org.eclipse.emfforms.spi.editor.InitializeChildCallback;
26: import org.eclipse.emfforms.spi.swt.treemasterdetail.util.CreateChildAction;
27: import org.eclipse.jface.action.Action;
28: import org.eclipse.jface.dialogs.Dialog;
29: import org.eclipse.jface.viewers.ArrayContentProvider;
30: import org.eclipse.jface.viewers.IOpenListener;
31: import org.eclipse.jface.viewers.ISelectionProvider;
32: import org.eclipse.jface.viewers.LabelProvider;
33: import org.eclipse.jface.viewers.OpenEvent;
34: import org.eclipse.jface.viewers.StructuredSelection;
35: import org.eclipse.jface.viewers.TableViewer;
36: import org.eclipse.swt.SWT;
37: import org.eclipse.swt.events.KeyEvent;
38: import org.eclipse.swt.events.KeyListener;
39: import org.eclipse.swt.graphics.Image;
40: import org.eclipse.swt.layout.GridData;
41: import org.eclipse.swt.layout.GridLayout;
42: import org.eclipse.swt.widgets.Button;
43: import org.eclipse.swt.widgets.Composite;
44: import org.eclipse.swt.widgets.Control;
45: import org.eclipse.swt.widgets.Shell;
46:
47: /**
48: * The Class CreateNewChildDialog.
49: * It shows the small dialog asking the user to select the type of the newly created element
50: */
51: public class CreateNewChildDialog extends Dialog {
52:
53:         /** The title of the dialog. */
54:         private final String title;
55:
56:         /** The selection provider. It is used to select the created elements afterwards. */
57:         private final ISelectionProvider selectionProvider;
58:
59:         /** The parent. It is used to obtain which children can be created. */
60:         private final EObject parent;
61:
62:         /**
63:          * Instantiates a new creates the new child dialog.
64:          *
65:          * @param parentShell the parent shell
66:          * @param title the title
67:          * @param parent the parent
68:          * @param selectionProvider the selection provider
69:          */
70:         public CreateNewChildDialog(Shell parentShell, String title, EObject parent, ISelectionProvider selectionProvider) {
71:                 super(parentShell);
72:                 this.title = title;
73:                 this.parent = parent;
74:                 this.selectionProvider = selectionProvider;
75:         }
76:
77:         /**
78:          * Returns the selection provider.
79:          *
80:          * @return the selection provider
81:          */
82:         protected final ISelectionProvider getSelectionProvider() {
83:                 return selectionProvider;
84:         }
85:
86:         @Override
87:         protected void setShellStyle(int newShellStyle) {
88:                 super.setShellStyle(SWT.TITLE);
89:         }
90:
91:         @Override
92:         protected Button createButton(Composite parent, int id,
93:                 String label, boolean defaultButton) {
94:                 return null;
95:         }
96:
97:         @Override
98:         protected void createButtonsForButtonBar(Composite parent) {
99:                 final GridLayout layout = (GridLayout) parent.getLayout();
100:                 layout.marginHeight = 0;
101:         }
102:
103:         @Override
104:         protected void configureShell(Shell newShell) {
105:                 super.configureShell(newShell);
106:                 newShell.setText(title);
107:         }
108:
109:         @Override
110:         protected Control createDialogArea(Composite parentComposite) {
111:                 final ChildrenDescriptorCollector childrenDescriptorCollector = new ChildrenDescriptorCollector();
112:                 final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(parent);
113:
114:                 final Dialog currentDialog = this;
115:                 final List<Action> actions = getNewChildActions(
116:                         childrenDescriptorCollector.getDescriptors(parent),
117:                         editingDomain, parent);
118:
119:                 final TableViewer list = new TableViewer(parentComposite);
120:                 list.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
121:                 list.setContentProvider(new ArrayContentProvider());
122:                 list.setLabelProvider(new LabelProvider() {
123:                         @Override
124:                         public String getText(Object element) {
125:                                 final Action action = (Action) element;
126:                                 final StringBuilder builder = new StringBuilder(action
127:                                         .getText());
128:                                 if (action.getAccelerator() > 0) {
129:                                         builder.append(" ["); //$NON-NLS-1$
130:                                         builder.append(Character.toUpperCase((char) action
131:                                                 .getAccelerator()));
132:                                         builder.append("]"); //$NON-NLS-1$
133:                                 }
134:                                 return builder.toString();
135:                         }
136:
137:                         @Override
138:                         public Image getImage(Object element) {
139:                                 return ((Action) element).getImageDescriptor()
140:                                         .createImage();
141:                         }
142:                 });
143:                 list.setInput(actions.toArray());
144:                 list.addOpenListener(new IOpenListener() {
145:
146:                         @Override
147:                         public void open(OpenEvent event) {
148:                                 final Action action = (Action) ((StructuredSelection) event
149:                                         .getSelection()).getFirstElement();
150:                                 action.run();
151:                                 currentDialog.close();
152:                         }
153:                 });
154:                 list.getControl().addKeyListener(new KeyListener() {
155:
156:                         @Override
157:                         public void keyReleased(KeyEvent e) {
158:                                 // NOP
159:                         }
160:
161:                         @Override
162:                         public void keyPressed(KeyEvent e) {
163:                                 for (final Action a : actions) {
164:                                         if (a.getAccelerator() == e.keyCode) {
165:                                                 a.run();
166:                                                 currentDialog.close();
167:                                                 break;
168:                                         }
169:                                 }
170:                         }
171:                 });
172:                 return parentComposite;
173:         }
174:
175:         /**
176:          * Creates all new child actions.
177:          *
178:          * @param descriptors the descriptors
179:          * @param domain the domain
180:          * @param eObject the e object
181:          * @return the list
182:          */
183:         protected List<Action> getNewChildActions(Collection<?> descriptors,
184:                 final EditingDomain domain, final EObject eObject) {
185:                 final List<Action> result = new ArrayList<Action>();
186:
187:                 for (final Object descriptor : descriptors) {
188:
189:                         final CommandParameter cp = (CommandParameter) descriptor;
190:                         if (!CommandParameter.class.isInstance(descriptor)) {
191:                                 continue;
192:                         }
193:                         if (cp.getEReference() == null) {
194:                                 continue;
195:                         }
196:                         if (!cp.getEReference().isMany()
197:                                 && eObject.eIsSet(cp.getEStructuralFeature())) {
198:                                 continue;
199:                         } else if (cp.getEReference().isMany()
200:                                 && cp.getEReference().getUpperBound() != -1
201:                                 && cp.getEReference().getUpperBound() <= ((List<?>) eObject
202:                                         .eGet(cp.getEReference())).size()) {
203:                                 continue;
204:                         }
205:                         result.add(new CreateChildAction(eObject, domain, getSelectionProvider(), cp,
206:                                 new InitializeChildCallback()));
207:                 }
208:                 return result;
209:         }
210:
211: }