Skip to content

Package: GenModelGroupExpansionViewModelService

GenModelGroupExpansionViewModelService

nameinstructionbranchcomplexitylinemethod
GenModelGroupExpansionViewModelService()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 9
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
getPriority()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
instantiate(ViewModelContext)
M: 1 C: 47
98%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 1 C: 12
92%
M: 0 C: 1
100%
static {...}
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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: * Martin Fleck - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.editor.genmodel.service;
15:
16: import java.util.HashMap;
17: import java.util.Map;
18:
19: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
20: import org.eclipse.emf.ecp.view.spi.context.ViewModelService;
21: import org.eclipse.emf.ecp.view.spi.group.model.VGroup;
22: import org.eclipse.emf.ecp.view.spi.group.model.VGroupPackage;
23: import org.eclipse.emf.ecp.view.spi.model.ModelChangeListener;
24: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
25: import org.eclipse.emf.ecp.view.spi.model.VContainedElement;
26: import org.eclipse.emf.ecp.view.spi.model.VElement;
27: import org.eclipse.emf.ecp.view.spi.model.VView;
28:
29: /**
30: * This {@link ViewModelService} tracks the collapsed state of the groups used in {@link ViewModelContext#getViewModel()
31: * view model} of the {@link org.eclipse.emfforms.internal.editor.genmodel.GenModelEditor GenModelEditor}. The group
32: * state is stored statically so it is the same across all input
33: * models.
34: *
35: * @author Martin Fleck
36: *
37: */
38: public class GenModelGroupExpansionViewModelService implements ViewModelService {
39:
40:         // known group names of the Ecore visible through the GenModel editor
41:         private static final String ECORE_STANDARD = "Standard"; //$NON-NLS-1$
42:         private static final String ECORE_ADVANCED = "Advanced"; //$NON-NLS-1$
43:
44:         // known group names of the GenModel visible through the GenModel editor
45:         private static final String GENMODEL_ALL = "All"; //$NON-NLS-1$
46:         private static final String GENMODEL_EDIT = "Edit"; //$NON-NLS-1$
47:         private static final String GENMODEL_EDITOR = "Editor"; //$NON-NLS-1$
48:         private static final String GENMODEL_MODEL = "Model"; //$NON-NLS-1$
49:         private static final String GENMODEL_MODEL_CLASS_DEFAULTS = "Model Class Defaults"; //$NON-NLS-1$
50:         private static final String GENMODEL_MODEL_FEATURE_DEFAULTS = "Model Feature Defaults"; //$NON-NLS-1$
51:         private static final String GENMODEL_PACKAGE_SUFFIXES = "Package Suffixes"; //$NON-NLS-1$
52:         private static final String GENMODEL_TEMPLATES_MERGE = "Templates & Merge"; //$NON-NLS-1$
53:         private static final String GENMODEL_TESTS = "Tests"; //$NON-NLS-1$
54:
55:         /**
56:          * Map storing the collapsed group states by group name.
57:          */
58:         private static final Map<String, Boolean> GROUP_COLLAPSED_STATES = new HashMap<String, Boolean>();
59:         static {
60:                 // initialize default collapsed states
61:                 GROUP_COLLAPSED_STATES.put(ECORE_STANDARD, Boolean.FALSE);
62:                 GROUP_COLLAPSED_STATES.put(ECORE_ADVANCED, Boolean.FALSE);
63:
64:                 GROUP_COLLAPSED_STATES.put(GENMODEL_ALL, Boolean.FALSE);
65:                 GROUP_COLLAPSED_STATES.put(GENMODEL_EDIT, Boolean.TRUE);
66:                 GROUP_COLLAPSED_STATES.put(GENMODEL_EDITOR, Boolean.TRUE);
67:                 GROUP_COLLAPSED_STATES.put(GENMODEL_MODEL, Boolean.TRUE);
68:                 GROUP_COLLAPSED_STATES.put(GENMODEL_MODEL_CLASS_DEFAULTS, Boolean.TRUE);
69:                 GROUP_COLLAPSED_STATES.put(GENMODEL_MODEL_FEATURE_DEFAULTS, Boolean.TRUE);
70:                 GROUP_COLLAPSED_STATES.put(GENMODEL_PACKAGE_SUFFIXES, Boolean.TRUE);
71:                 GROUP_COLLAPSED_STATES.put(GENMODEL_TEMPLATES_MERGE, Boolean.TRUE);
72:                 GROUP_COLLAPSED_STATES.put(GENMODEL_TESTS, Boolean.TRUE);
73:         }
74:
75:         /**
76:          * Context.
77:          */
78:         private ViewModelContext viewModelContext;
79:
80:         /**
81:          * Listener updating the stored group states.
82:          */
83:         private final ModelChangeListener collapsedStateListener = new ModelChangeListener() {
84:                 @Override
85:                 public void notifyChange(ModelChangeNotification notification) {
86:                         if (notification.getNotifier() instanceof VGroup
87:                                 && notification.getStructuralFeature().equals(VGroupPackage.Literals.GROUP__COLLAPSED)) {
88:                                 // update collapsed state
89:                                 final VGroup group = (VGroup) notification.getNotifier();
90:                                 GROUP_COLLAPSED_STATES.put(group.getName(), group.isCollapsed());
91:                         }
92:                 }
93:         };
94:
95:         /**
96:          * {@inheritDoc}
97:          *
98:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#instantiate(org.eclipse.emf.ecp.view.spi.context.ViewModelContext)
99:          */
100:         @Override
101:         public void instantiate(ViewModelContext context) {
102:                 final VElement viewModel = context.getViewModel();
103:•                if (!(viewModel instanceof VView)) {
104:                         return; // this service only works on views
105:                 }
106:
107:                 final VView view = (VView) viewModel;
108:                 viewModelContext = context;
109:
110:                 // Set the groups' collapsed states to the stored states (default or set by the user)
111:•                for (final VContainedElement element : view.getChildren()) {
112:•                        if (element instanceof VGroup) {
113:                                 final VGroup group = (VGroup) element;
114:                                 final Boolean groupCollapsedState = GROUP_COLLAPSED_STATES.get(group.getName());
115:•                                if (groupCollapsedState != null) {
116:                                         group.setCollapsed(groupCollapsedState);
117:                                 }
118:                         }
119:                 }
120:
121:                 // register listener
122:                 context.registerViewChangeListener(collapsedStateListener);
123:         }
124:
125:         /**
126:          * {@inheritDoc}
127:          *
128:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#dispose()
129:          */
130:         @Override
131:         public void dispose() {
132:•                if (viewModelContext != null) {
133:                         viewModelContext.unregisterViewChangeListener(collapsedStateListener);
134:                 }
135:         }
136:
137:         /**
138:          * {@inheritDoc}
139:          *
140:          * @see org.eclipse.emf.ecp.view.spi.context.ViewModelService#getPriority()
141:          */
142:         @Override
143:         public int getPriority() {
144:                 return 0;
145:         }
146: }