Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df969d7c266f5dc9442fabe357fae58945ce19c1 (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2010 SAP AG.
 * 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:
 *    Petya Sabeva - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.internal.feature;

import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IContext;
import org.eclipse.graphiti.features.context.ICustomContext;
import org.eclipse.graphiti.features.custom.AbstractCustomFeature;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.GraphicsUpdater;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;


public class CollapseCompartmentShapeFeature extends AbstractCustomFeature{

	public CollapseCompartmentShapeFeature(IFeatureProvider fp) {
		super(fp);
	}

	public void execute(ICustomContext context) {
		PictogramElement el = context.getPictogramElements()[0];
		
		final ContainerShape containerShape = (ContainerShape) el;
		final ContainerShape entityShape = containerShape.getContainer();
		
		final GraphicsAlgorithm algo = el.getGraphicsAlgorithm();
		TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(algo);
		ted.getCommandStack().execute(new RecordingCommand(ted) {
			@Override
			protected void doExecute() {
			    algo.setHeight(JPAEditorConstants.COMPARTMENT_MIN_HEIGHT);
				GraphicsUpdater.updateEntityShape(entityShape);
				GraphicsUpdater.setCollapsed(containerShape, true);
				JPAEditorUtil.rearrangeAllConnections(entityShape, getFeatureProvider(), false);				
			}
		});
	}

	@Override
	public boolean canExecute(ICustomContext context) {
		return true;
	}

	@Override
	public IJPAEditorFeatureProvider getFeatureProvider() {
		return  (IJPAEditorFeatureProvider)super.getFeatureProvider();
	}
	
    @Override
	public boolean isAvailable(IContext context) {
		boolean ret = false;
		if (context instanceof ICustomContext) {
			PictogramElement[] pes = ((ICustomContext) context).getPictogramElements();
			if (pes != null && pes.length > 0) {
				boolean collapsePossible = false;
				for (int i = 0; i < pes.length; i++) {
					PictogramElement pe = pes[i];
					Object bo = getBusinessObjectForPictogramElement(pe);
					if (bo != null) {
						return false;
					}
					if (!collapsePossible) {
						if(!GraphicsUpdater.isCollapsed((ContainerShape)pe)) {
							collapsePossible = true;
						}
					}
				}
				if (collapsePossible) {
					return true;
				}
			}
		}
		return ret;
	}

}

Back to the top