Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b553cf3c96c21d49452251c1573b80914d2a7e1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2011 Formal Mind GmbH and University of Dusseldorf.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Michael Jastram - initial API and implementation
 ******************************************************************************/
package org.eclipse.rmf.pror.reqif10.presentation.ui;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.rmf.pror.reqif10.configuration.ConfigPackage;
import org.eclipse.rmf.pror.reqif10.configuration.ProrPresentationConfiguration;
import org.eclipse.rmf.pror.reqif10.configuration.ProrPresentationConfigurations;
import org.eclipse.rmf.pror.reqif10.configuration.ProrToolExtension;
import org.eclipse.rmf.pror.reqif10.presentation.id.IdConfiguration;
import org.eclipse.rmf.pror.reqif10.presentation.id.IdFactory;
import org.eclipse.rmf.pror.reqif10.presentation.service.AbstractPresentationService;
import org.eclipse.rmf.pror.reqif10.presentation.service.IProrCellRenderer;
import org.eclipse.rmf.pror.reqif10.presentation.service.PresentationService;
import org.eclipse.rmf.pror.reqif10.util.ConfigurationUtil;
import org.eclipse.rmf.reqif10.AttributeValue;
import org.eclipse.rmf.reqif10.AttributeValueString;
import org.eclipse.rmf.reqif10.ReqIf;
import org.eclipse.rmf.reqif10.util.Reqif10Util;

public class IDPresentationService extends AbstractPresentationService implements PresentationService {

	private final IProrCellRenderer idLabelCellRenderer;
	private final HashMap<IdConfiguration, EContentAdapter> adapters = new HashMap<IdConfiguration, EContentAdapter>();

	public IDPresentationService() {
		idLabelCellRenderer = new IdLabelCellRenderer();
	}
	/**
	 * Upon opening, attaches a Listener that sets the ID if the Datatype
	 * matches.
	 */
	@Override
	public void openReqif(final ReqIf rif) {

		ensureAllConfigsHaveAdapters(rif);

		// Make sure that IdConfig additions and removals are handled
		
		ProrPresentationConfigurations presentationConfigurations = getPresentationConfigurations(rif);
		if (presentationConfigurations == null) return;
		
		presentationConfigurations.eAdapters().add(new AdapterImpl() {
			@Override
			public void notifyChanged(Notification msg) {
				ensureAllConfigsHaveAdapters(rif);
			}
		});
	}

	private void ensureAllConfigsHaveAdapters(final ReqIf rif) {
		Set<IdConfiguration> configs = getConfigurationElements(rif);
		if (configs == null) return;

		// Process all existing IdConfigurations
		for (final IdConfiguration config : configs) {
			if (!adapters.containsKey(config)) {
				EContentAdapter adapter = buildAdapter(config);
				rif.eAdapters().add(adapter);
				adapters.put(config, adapter);
			}
		}
	}

	private EContentAdapter buildAdapter(final IdConfiguration config) {
		config.eAdapters().add(new AdapterImpl() {
			@Override
			public void notifyChanged(Notification msg) {
				if (ConfigPackage.Literals.PROR_PRESENTATION_CONFIGURATION__DATATYPE
						.equals(msg.getFeature())) {
					EContentAdapter adapter = adapters.get(config);
					if (adapter != null) {
						ReqIf rif = Reqif10Util.getReqIf(config);
						rif.eAdapters().remove(adapter);
						adapter = buildAdapter(config);
						rif.eAdapters().add(adapter);
						adapters.put(config, adapter);
					}
				}
			}
		});

		EContentAdapter adapter = new EContentAdapter() {
			@Override
			public void setTarget(Notifier target) {
				super.setTarget(target);
				if (target instanceof AttributeValueString) {
					AttributeValueString value = (AttributeValueString) target;
					if (value.getDefinition() != null
							&& value.getDefinition().getType() != null
							&& value.getDefinition().getType()
									.equals(config.getDatatype())) {
						if (value.getTheValue() == null
								|| value.getTheValue().length() == 0) {
							int newCount = config.getCount() + 1;
							value.setTheValue(config.getPrefix() + newCount);
							config.setCount(newCount);
						}
					}
				}
			}
		};
		return adapter;
	}

	/**
	 * Get the {@link IdConfiguration}s for the given ReqIF
	 */
	private Set<IdConfiguration> getConfigurationElements(ReqIf rif) {
		HashSet<IdConfiguration> idConfigs = new HashSet<IdConfiguration>();
		ProrPresentationConfigurations configsElement = getPresentationConfigurations(rif);
		if (configsElement == null) return null;
		EList<ProrPresentationConfiguration> configs = configsElement.getPresentationConfigurations();
		for (ProrPresentationConfiguration config : configs) {
			if (config instanceof IdConfiguration)
				idConfigs.add((IdConfiguration) config);
		}
		return idConfigs;
	}

	private ProrPresentationConfigurations getPresentationConfigurations(ReqIf rif) {
		ProrToolExtension uiExtension = ConfigurationUtil.getProrToolExtension(rif);
		ProrPresentationConfigurations configs = uiExtension
				.getPresentationConfigurations();
		return configs;
	}

	@Override
	public ProrPresentationConfiguration getConfigurationInstance() {
		return IdFactory.eINSTANCE.createIdConfiguration();
	}

	@Override
	public IProrCellRenderer getCellRenderer(AttributeValue av) {
		return idLabelCellRenderer;
	}

}

Back to the top