Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c96b15e99d416d156114e95e1f92cb582a48aafe (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*****************************************************************************
 * Copyright (c) 2017 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:
 *  Mickaäl ADAM (ALL4TEC) mickael.adam@all4tec.net - Move from oep.uml.diagram.common, see bug 512343.
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.service.palette;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.gmfdiag.common.Activator;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.ChildConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.Configuration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.DrawerConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.LeafConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.PaletteConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.PaletteconfigurationFactory;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.SeparatorConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.StackConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.ToolConfiguration;
import org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.util.PaletteconfigurationSwitch;


/**
 * Utility class for {@link PaletteConfiguration} elements.
 */
public class PaletteConfigurationUtils {

	public static Collection<String> getRequiredProfiles(PaletteConfiguration paletteConfiguration) {
		if (paletteConfiguration == null) {
			return Collections.emptyList();
		}
		return paletteConfiguration.getRequiredProfiles();
	}

	/**
	 * Removes a configuration from its container
	 *
	 * @param configuration
	 *            the configuration to remove
	 * @return <code>true</code> if the configuration was removed from its container
	 */
	public static boolean removeConfiguration(Configuration configuration) {
		return new PaletteconfigurationSwitch<Boolean>() {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Boolean caseStackConfiguration(StackConfiguration configuration) {
				return removeStack(configuration);
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Boolean caseDrawerConfiguration(DrawerConfiguration configuration) {
				return removeDrawer(configuration);
			};

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Boolean caseToolConfiguration(ToolConfiguration configuration) {
				return removeTool(configuration);
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Boolean caseSeparatorConfiguration(SeparatorConfiguration configuration) {
				return removeSeparator(configuration);
			};
		}.doSwitch(configuration);
	}

	/**
	 * Removes a drawer from a palette configuration
	 *
	 * @param configuration
	 *            the drawer configuration to remove
	 * @return <code>true</code> if the drawer configuration has been removed
	 */
	public static boolean removeDrawer(DrawerConfiguration configuration) {
		EObject container = configuration.eContainer();
		if (!(container instanceof PaletteConfiguration)) {
			return false;
		}

		PaletteConfiguration paletteConfiguration = ((PaletteConfiguration) container);
		return paletteConfiguration.getDrawerConfigurations().remove(configuration);
	}

	/**
	 * Removes a stack from a palette configuration
	 *
	 * @param configuration
	 *            the stack configuration to remove
	 * @return <code>true</code> if the stack configuration has been removed
	 */
	public static boolean removeStack(StackConfiguration configuration) {
		EObject container = configuration.eContainer();
		if (!(container instanceof DrawerConfiguration)) {
			return false;
		}

		DrawerConfiguration drawerConfiguration = ((DrawerConfiguration) container);
		return drawerConfiguration.getOwnedConfigurations().remove(configuration);
	}

	/**
	 * Removes a tool from its container
	 *
	 * @param configuration
	 *            the tool configuration to remove
	 * @return <code>true</code> if the tool configuration has been removed
	 */
	public static boolean removeTool(ToolConfiguration configuration) {
		EObject container = configuration.eContainer();
		if ((container instanceof DrawerConfiguration)) {
			DrawerConfiguration drawerConfiguration = ((DrawerConfiguration) container);
			return drawerConfiguration.getOwnedConfigurations().remove(configuration);
		} else if ((container instanceof StackConfiguration)) {
			StackConfiguration stackConfiguration = ((StackConfiguration) container);
			return stackConfiguration.getOwnedConfigurations().remove(configuration);
		}
		return false;
	}

	/**
	 * Removes a separator from its container
	 *
	 * @param configuration
	 *            the separator configuration to remove
	 * @return <code>true</code> if the separator configuration has been removed
	 */
	public static boolean removeSeparator(SeparatorConfiguration configuration) {
		EObject container = configuration.eContainer();
		if ((container instanceof DrawerConfiguration)) {
			DrawerConfiguration drawerConfiguration = ((DrawerConfiguration) container);
			return drawerConfiguration.getOwnedConfigurations().remove(configuration);
		} else if ((container instanceof StackConfiguration)) {
			StackConfiguration stackConfiguration = ((StackConfiguration) container);
			return stackConfiguration.getOwnedConfigurations().remove(configuration);
		}
		return false;
	}

	/**
	 * Creates a new separator configuration and adds it to the specified parent configuration
	 *
	 * @param drawerConfiguration
	 *            parent configuration for the created separator
	 * @return the created separator configuration
	 */
	public static SeparatorConfiguration createSeparator(DrawerConfiguration drawerConfiguration) {
		String id = generateID("Separator");
		SeparatorConfiguration separatorConfiguration = PaletteconfigurationFactory.eINSTANCE.createSeparatorConfiguration();
		separatorConfiguration.setId(id);
		separatorConfiguration.setLabel(id);
		drawerConfiguration.getOwnedConfigurations().add(separatorConfiguration);
		return separatorConfiguration;
	}

	/**
	 * Creates a new separator configuration and adds it to the specified parent configuration
	 *
	 * @param leafConfiguration
	 *            configuration after which the created separator should be added
	 * @return the created separator configuration
	 */
	public static SeparatorConfiguration createSeparator(LeafConfiguration leafConfiguration) {
		String id = generateID("Separator");
		SeparatorConfiguration separatorConfiguration = PaletteconfigurationFactory.eINSTANCE.createSeparatorConfiguration();
		separatorConfiguration.setId(id);
		separatorConfiguration.setLabel(id);
		EObject parentContainer = leafConfiguration.eContainer();
		if (parentContainer instanceof DrawerConfiguration) {
			int i = ((DrawerConfiguration) parentContainer).getOwnedConfigurations().indexOf(leafConfiguration);
			if (i >= 0) {
				((DrawerConfiguration) parentContainer).getOwnedConfigurations().add(i + 1, separatorConfiguration);
			} else {
				throw new RuntimeException("Impossible to find the index of the configuration: " + leafConfiguration);
			}
		} else {
			throw new RuntimeException("Impossible to create a stack outside a DrawerConfiguration: " + leafConfiguration);
		}
		return separatorConfiguration;
	}

	/**
	 * Creates a new stack configuration and adds it to the specified parent configuration
	 *
	 * @param drawerConfiguration
	 *            parent configuration for the created separator
	 * @return the stack separator configuration
	 */
	public static StackConfiguration createStack(DrawerConfiguration drawerConfiguration) {
		String id = generateID("Stack");
		StackConfiguration stackConfiguration = PaletteconfigurationFactory.eINSTANCE.createStackConfiguration();
		stackConfiguration.setId(id);
		stackConfiguration.setLabel(id);
		drawerConfiguration.getOwnedConfigurations().add(stackConfiguration);
		return stackConfiguration;
	}

	/**
	 * Creates a new stack configuration and adds it after the given leaf configuration
	 *
	 * @param leafConfiguration
	 *            the configuration after which the created configuration is added
	 * @return the stack separator configuration
	 */
	public static StackConfiguration createStack(LeafConfiguration leafConfiguration) {
		String id = generateID("Stack");
		StackConfiguration stackConfiguration = PaletteconfigurationFactory.eINSTANCE.createStackConfiguration();
		stackConfiguration.setId(id);
		stackConfiguration.setLabel(id);
		EObject parentContainer = leafConfiguration.eContainer();
		if (parentContainer instanceof DrawerConfiguration) {
			int i = ((DrawerConfiguration) parentContainer).getOwnedConfigurations().indexOf(leafConfiguration);
			if (i > 0) {
				((DrawerConfiguration) parentContainer).getOwnedConfigurations().add(i + 1, stackConfiguration);
			} else {
				throw new RuntimeException("Impossible to find the index of the configuration: " + leafConfiguration);
			}
		} else {
			throw new RuntimeException("Impossible to create a stack outside a DrawerConfiguration: " + leafConfiguration);
		}
		return stackConfiguration;
	}

	/**
	 * Creates a new drawer configuration and adds it to the given paletteConfiguration
	 *
	 * @param paletteConfiguration
	 *            the container of the created configuration
	 * @return the drawer configuration
	 */
	public static void createDrawer(PaletteConfiguration paletteConfiguration) {
		boolean isFound = false;
		String newName = "";
		for (int i = 0; i < 100 && !isFound; i++) {
			isFound = false;
			boolean alreadyExists = false;
			String newLabel = "Drawer_" + i;
			Iterator<DrawerConfiguration> it = paletteConfiguration.getDrawerConfigurations().iterator();
			while (it.hasNext()) {
				DrawerConfiguration configuration = it.next();
				String label = configuration.getLabel();
				if (newLabel.equalsIgnoreCase(label)) {
					alreadyExists = true;
				}
			}

			if (!alreadyExists) {
				isFound = true;
				newName = newLabel;
			}
		}

		DrawerConfiguration drawerConfiguration = PaletteconfigurationFactory.eINSTANCE.createDrawerConfiguration();
		drawerConfiguration.setId(generateID("Drawer"));
		drawerConfiguration.setLabel(newName);
		paletteConfiguration.getDrawerConfigurations().add(drawerConfiguration);
	}

	/**
	 * Generates the ID for a local element
	 *
	 * @param base
	 *            the begining of the id
	 * @return the separator id
	 */
	protected static String generateID(String base) {
		StringBuffer id = new StringBuffer();
		id.append(base);
		id.append("_");
		id.append(System.currentTimeMillis());
		return id.toString();
	}

	/**
	 * Moves a drawer configuration before the specified one
	 *
	 * @param paletteConfiguration
	 *            the configuration containing the drawer configurations
	 * @param drawerConfigurationToMove
	 *            the drawer configuration that should be moved
	 * @param drawerConfiguration
	 *            the drawer configuration in front of which the drawer configuration to move should be placed.
	 */
	public static void moveDrawerConfiguration(PaletteConfiguration paletteConfiguration, DrawerConfiguration drawerConfigurationToMove, DrawerConfiguration drawerConfiguration) {
		int referenceIndex = paletteConfiguration.getDrawerConfigurations().indexOf(drawerConfiguration);
		int indexToMove = paletteConfiguration.getDrawerConfigurations().indexOf(drawerConfigurationToMove);

		if (referenceIndex == -1) {
			Activator.log.error("Impossible to find the drawer before which the drawer to move should be placed", null);
			return;
		}

		if (indexToMove == -1) {
			Activator.log.error("Impossible to find the drawer to move", null);
			return;
		}

		if (referenceIndex == indexToMove) {
			// nothing to do, as this should be the same element..
			return;
		} else if (referenceIndex < indexToMove) { // the element to move is after the reference element. Remove it and ad it to the right place
			paletteConfiguration.getDrawerConfigurations().remove(drawerConfigurationToMove);
			paletteConfiguration.getDrawerConfigurations().add(referenceIndex, drawerConfigurationToMove);
		} else {
			paletteConfiguration.getDrawerConfigurations().remove(drawerConfigurationToMove);
			paletteConfiguration.getDrawerConfigurations().add(referenceIndex - 1, drawerConfigurationToMove);
		}

	}

	/**
	 * Moves a separator configuration in a drawer
	 *
	 * @param drawerConfiguration
	 *            the configuration where to move the element. should be a {@link DrawerConfiguration}.
	 * @param separatorConfigurationToMove
	 *            the separator configuration to move
	 */
	public static void moveSeparatorConfiguration(DrawerConfiguration drawerConfiguration, SeparatorConfiguration separatorConfigurationToMove) {
		DrawerConfiguration oldContainer = (DrawerConfiguration) separatorConfigurationToMove.eContainer();
		if (oldContainer.equals(drawerConfiguration)) {
			drawerConfiguration.getOwnedConfigurations().remove(separatorConfigurationToMove);
		}
		drawerConfiguration.getOwnedConfigurations().add(separatorConfigurationToMove);
	}

	/**
	 * Moves the specified Separator configuration before the specified child configuration
	 *
	 * @param childConfiguration
	 *            the configuration before which the {@link SeparatorConfiguration} should be moved
	 * @param separatorConfigurationToMove
	 *            the {@link SeparatorConfiguration} to move
	 */
	public static void moveSeparatorConfiguration(ChildConfiguration childConfiguration, SeparatorConfiguration separatorConfigurationToMove) {
		DrawerConfiguration parentConfiguration = (DrawerConfiguration) childConfiguration.eContainer();

		// check if the container of the separator configuration is the new one. If yes, the separator Configuration should be removed from its parent container first
		Configuration oldContainer = (Configuration) separatorConfigurationToMove.eContainer();

		// check indexes
		int index = parentConfiguration.getOwnedConfigurations().indexOf(childConfiguration);
		if (index == -1) {
			Activator.log.error("Impossible to find the child configuration:" + childConfiguration, null);
			return;
		}

		// now add the element. remove from the container if target and source container are equals (to avoid duplication)
		if (oldContainer.equals(parentConfiguration)) {
			parentConfiguration.getOwnedConfigurations().remove(separatorConfigurationToMove);
		}

		parentConfiguration.getOwnedConfigurations().add(index, separatorConfigurationToMove);



	}

	/**
	 * Moves a stack configuration in a drawer
	 *
	 * @param drawerConfiguration
	 *            the configuration where to move the element. should be a {@link DrawerConfiguration}.
	 * @param stackConfigurationToMove
	 *            the stack configuration to move
	 */
	public static void moveStackConfiguration(DrawerConfiguration drawerConfiguration, StackConfiguration stackConfigurationToMove) {
		DrawerConfiguration oldContainer = (DrawerConfiguration) stackConfigurationToMove.eContainer();
		if (oldContainer.equals(drawerConfiguration)) {
			drawerConfiguration.getOwnedConfigurations().remove(stackConfigurationToMove);
		}
		drawerConfiguration.getOwnedConfigurations().add(stackConfigurationToMove);

	}

	/**
	 * Moves the specified Stack configuration before the specified child configuration
	 *
	 * @param childConfiguration
	 *            the configuration before which the {@link StackConfiguration} should be moved
	 * @param stackConfigurationToMove
	 *            the {@link StackConfiguration} to move
	 */
	public static void moveStackConfiguration(ChildConfiguration childConfiguration, StackConfiguration stackConfigurationToMove) {
		DrawerConfiguration parentConfiguration = (DrawerConfiguration) childConfiguration.eContainer();

		// check if the container of the stack configuration is the new one. If yes, the stack Configuration should be removed from its parent container first
		Configuration oldContainer = (Configuration) stackConfigurationToMove.eContainer();

		// check indexes
		int index = parentConfiguration.getOwnedConfigurations().indexOf(childConfiguration);
		if (index == -1) {
			Activator.log.error("Impossible to find the child configuration:" + childConfiguration, null);
			return;
		}

		// now add the element. remove from the container if target and source container are equals (to avoid duplication)
		if (oldContainer.equals(parentConfiguration)) {
			parentConfiguration.getOwnedConfigurations().remove(stackConfigurationToMove);
		}

		parentConfiguration.getOwnedConfigurations().add(index, stackConfigurationToMove);
	}

	/**
	 * Moves a tool configuration in a drawer
	 *
	 * @param drawerConfiguration
	 *            the configuration where to move the element. should be a {@link DrawerConfiguration}.
	 * @param toolConfigurationToMove
	 *            the tool configuration to move
	 */
	public static void moveToolConfiguration(DrawerConfiguration drawerConfiguration, ToolConfiguration toolConfigurationToMove) {
		Configuration oldContainer = (Configuration) toolConfigurationToMove.eContainer();
		if (oldContainer.equals(drawerConfiguration)) {
			drawerConfiguration.getOwnedConfigurations().remove(toolConfigurationToMove);
		}
		drawerConfiguration.getOwnedConfigurations().add(toolConfigurationToMove);
	}

	/**
	 * Moves the specified Tool configuration before the specified child configuration
	 *
	 * @param childConfiguration
	 *            the configuration before which the {@link ToolConfiguration} should be moved
	 * @param toolConfigurationToMove
	 *            the {@link ToolConfiguration} to move
	 */
	public static void moveToolConfiguration(ChildConfiguration childConfiguration, ToolConfiguration toolConfigurationToMove) {
		EObject parentConfiguration = childConfiguration.eContainer();

		// check if the container of the stack configuration is the new one. If yes, the tool Configuration should be removed from its parent container first
		Configuration oldContainer = (Configuration) toolConfigurationToMove.eContainer();

		if (parentConfiguration instanceof DrawerConfiguration) {
			// check indexes
			int index = ((DrawerConfiguration) parentConfiguration).getOwnedConfigurations().indexOf(childConfiguration);
			if (index == -1) {
				Activator.log.error("Impossible to find the child configuration:" + childConfiguration, null);
				return;
			}

			// now add the element. remove from the container if target and source container are equals (to avoid duplication)
			if (oldContainer.equals(parentConfiguration)) {
				((DrawerConfiguration) parentConfiguration).getOwnedConfigurations().remove(toolConfigurationToMove);
			}

			((DrawerConfiguration) parentConfiguration).getOwnedConfigurations().add(index, toolConfigurationToMove);
		} else if (parentConfiguration instanceof StackConfiguration) {
			// check indexes
			int index = ((StackConfiguration) parentConfiguration).getOwnedConfigurations().indexOf(childConfiguration);
			if (index == -1) {
				Activator.log.error("Impossible to find the child configuration:" + childConfiguration, null);
				return;
			}

			// now add the element. remove from the container if target and source container are equals (to avoid duplication)
			if (oldContainer.equals(parentConfiguration)) {
				((StackConfiguration) parentConfiguration).getOwnedConfigurations().remove(toolConfigurationToMove);
			}

			((StackConfiguration) parentConfiguration).getOwnedConfigurations().add(index, toolConfigurationToMove);
		} else {
			Activator.log.error("Not handled yet: parent of the tool is:" + parentConfiguration, null);
		}
	}

	/**
	 * Moves the specified Tool configuration before the specified child configuration
	 *
	 * @param stackConfiguration
	 *            the configuration before which the {@link ToolConfiguration} should be moved
	 * @param toolConfigurationToMove
	 *            the {@link ToolConfiguration} to move
	 */
	public static void moveToolConfiguration(StackConfiguration stackConfiguration, ToolConfiguration toolConfigurationToMove) {
		// check if the container of the stack configuration is the new one. If yes, the tool Configuration should be removed from its parent container first
		EObject oldContainer = toolConfigurationToMove.eContainer();

		// now add the element. remove from the container if target and source container are equals (to avoid duplication)
		if (oldContainer.equals(stackConfiguration)) {
			stackConfiguration.getOwnedConfigurations().remove(toolConfigurationToMove);
		}

		stackConfiguration.getOwnedConfigurations().add(toolConfigurationToMove);
	}

}

Back to the top