Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 520e1c317b1a427d430f8a1165d7525280be946a (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*******************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 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:
 *     Cedric Dumoulin - cedric.dumoulin@lifl.fr
 ******************************************************************************/
/**
 */
package org.eclipse.papyrus.layers.stackmodel.layers.impl;

import org.eclipse.papyrus.layers.stackmodel.LayersException;
import org.eclipse.papyrus.layers.stackmodel.layers.AbstractLayerOperator;
import org.eclipse.papyrus.layers.stackmodel.layers.Layer;
import org.eclipse.papyrus.layers.stackmodel.layers.LayerExpression;
import org.eclipse.papyrus.layers.stackmodel.layers.LayerOperator;
import org.eclipse.papyrus.layers.stackmodel.layers.LayerOperatorDescriptor;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersContainer;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersFactory;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersFactoryForStack;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStack;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication;
import org.eclipse.papyrus.layers.stackmodel.layers.RegExpLayer;
import org.eclipse.papyrus.layers.stackmodel.layers.StackedLayerOperator;
import org.eclipse.papyrus.layers.stackmodel.layers.TopLayerOperator;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model <b>Factory</b>.
 * This factory allows to create Layers dedicated to a Application and {@link LayersStack} <!-- end-user-doc -->
 * 
 * @generated
 */
public class LayersFactoryForStackImpl implements LayersFactoryForStack {

	/**
	 * Index used to postfix the layer name.
	 */
	protected int newIndex = 0;
	
	final protected String LAYER_NAME_PREFIX = "layer";
	
	/**
	 * Creates the default factory implementation.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public static LayersFactoryForStack init() {
		return new LayersFactoryForStackImpl();
	}

	/**
	 * Creates an instance of the factory.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public LayersFactoryForStackImpl() {
		super();
	}

	public String findNewLayerName( LayersContainer parent ) {
		
		String proposedName = LAYER_NAME_PREFIX + newIndex++;
	
		// Check if the name is available in the targetted container
		if( parent instanceof LayerOperator ) {
			LayerOperator parentOperator = (LayerOperator)parent;
			while( getLayerByName(parentOperator, proposedName) != null ) {
				proposedName = LAYER_NAME_PREFIX + newIndex++;
			}
		}
		return proposedName;
	}
	
	/**
	 * Get a layer by its name.
	 * @param parentOperator The container containing layers
	 * @param name Name of the requested layer.
	 * @return The requested layer, or null.
	 */
	private LayerExpression getLayerByName(LayerOperator parentOperator, String name) {
		

		for( LayerExpression layer : parentOperator.getLayers() ) {
			if( name.equals(layer.getName() ) ) {
				return layer;
			}
		}
		// Not found
		return null;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	public LayersStack createLayersStack() {
		throw new UnsupportedOperationException("Not yet implemented");
	}


	/**
	 * 
	 * @param layer
	 *        The layer to init
	 * @param parentLayer
	 *        The parent Layer to which layer is added
	 * @param owningStack
	 *        The {@link LayersStack} owning the tree of layers
	 * @param application
	 *        The application required by layer.
	 * 
	 * @return
	 * @throws LayersException
	 */
	@Override
	public LayerExpression initLayer(LayerExpression layer, LayersContainer parentLayer, LayersStack owningStack, LayersStackApplication application) throws LayersException {
		// Init the created layer
		layer.setApplication(application);
		layer.setName(findNewLayerName(parentLayer));
		parentLayer.addLayer(layer);
		layer.setOwningLayersStack(owningStack);
		// Start the layer
		layer.attach();

		return layer;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * 
	 * @throws LayersException
	 * @generated NOT
	 */
	@Override
	public Layer createLayer(LayersContainer parent, LayersStack owningStack, LayersStackApplication application) throws LayersException {
		Layer layer = LayersFactory.eINSTANCE.createLayer();
		// Init the created layer
		initLayer(layer, parent, owningStack, application);

		return layer;
	}

	@Override
	public RegExpLayer createRegExpLayer(LayersContainer parent, LayersStack owningStack, LayersStackApplication application) throws LayersException {
		RegExpLayer layer = LayersFactory.eINSTANCE.createRegExpLayer();
		// Init the created layer
		initLayer(layer, parent, owningStack, application);

		return layer;
	}

	@Override
	public TopLayerOperator createTopLayerOperator(LayersContainer parent, LayersStack owningStack, LayersStackApplication application) throws LayersException {
		TopLayerOperator layer = LayersFactory.eINSTANCE.createTopLayerOperator();
		// Init the created layer
		initLayer(layer, parent, owningStack, application);

		return layer;
	}

	@Override
	public StackedLayerOperator createStackedLayerOperator(LayersContainer parent, LayersStack owningStack, LayersStackApplication application) throws LayersException {
		StackedLayerOperator layer = LayersFactory.eINSTANCE.createStackedLayerOperator();
		// Init the created layer
		initLayer(layer, parent, owningStack, application);

		return layer;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.layers.stackmodel.layers.LayersFactoryForStack#createLayerOperator(java.lang.String, org.eclipse.papyrus.layers.stackmodel.layers.LayersContainer, org.eclipse.papyrus.layers.stackmodel.layers.LayersStack, org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication)
	 *
	 * @param layerOperatorID
	 * @param parent
	 * @param owningStack
	 * @param application
	 * @return
	 * @throws LayersException
	 */
	@Override
	public AbstractLayerOperator createLayerOperator(String layerOperatorID, LayersContainer parent, LayersStack owningStack, LayersStackApplication application) throws LayersException {
		// Create a layer !
		 AbstractLayerOperator layerOperator = application.getLayerOperatorDescriptorRegistry().createLayerOperator(layerOperatorID);
		// Init the created layer
		initLayer(layerOperator, parent, owningStack, application);

		return layerOperator;
	}




} //LayersFactoryImpl

Back to the top