Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a24442d9fcabae028d8b39a28acf27fe995bb7d0 (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
/*******************************************************************************
 * <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:
 *    Stefan Dimov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.internal.util;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants.ShapeType;


public class GraphicsUpdater {

	private static final int PRIMARY_SHAPE_INDEX = 3;
	private static final int RELATION_SHAPE_CONSTANT = 4;
	private static final int BASIC_SHAPE_INDEX = 5;

	public static int increaseCompartmentHeigth(ContainerShape containerShape,
			int height) {
		for(Shape shape : containerShape.getChildren()){
			Iterator<GraphicsAlgorithm> iterator = shape.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().iterator();
			while(iterator.hasNext()){
			    if(iterator.next() instanceof Text)
			        height = height + JPAEditorConstants.ATTRIBUTES_PLACEMENT_STEP;
			}
		}
		return height;
	}
	
	public static void updateContainer(ContainerShape containerShape, 
			int childrenSizeBefore, int containerHeightBefore){
		int childrenSizeAfter = containerShape.getChildren().size();
		int containerHeightAfter = 0;
		if(childrenSizeBefore == 2){
				containerHeightBefore = JPAEditorConstants.COMPARTMENT_MIN_HEIGHT + JPAEditorConstants.COMPARTMENT_BUTTOM_OFFSET;
		}
		containerHeightAfter = containerHeightBefore + (((childrenSizeAfter - childrenSizeBefore)/2)*JPAEditorConstants.ATTRIBUTES_PLACEMENT_STEP);
		
		if(!isCollapsed(containerShape))
			containerShape.getGraphicsAlgorithm().setHeight(containerHeightAfter);
		else 
			containerShape.getGraphicsAlgorithm().setHeight(JPAEditorConstants.COMPARTMENT_MIN_HEIGHT);
	}
	
	
	public static void updateEntityShape(ContainerShape entityShape){
		ContainerShape primaryShape = getPrimaryShape(entityShape);
		ContainerShape relationShape = getRelationShape(entityShape);
		ContainerShape basicShape = getBasicShape(entityShape);
		
		primaryShape.setVisible(!isEmptyCompartment(primaryShape));
		basicShape.setVisible(!isEmptyCompartment(basicShape));
		relationShape.setVisible(!isEmptyCompartment(relationShape));

        relationShape.getGraphicsAlgorithm().setY(getNextCompartmentY(primaryShape));
        basicShape.getGraphicsAlgorithm().setY(getNextCompartmentY(relationShape));
	}

	private static boolean isEmptyCompartment(ContainerShape primaryShape) {
		return primaryShape.getChildren().size() <= 2;
	}
	
	public static void updateEntityHeigth(ContainerShape entityShape){
		ContainerShape primaryShape = GraphicsUpdater.getPrimaryShape(entityShape);
		ContainerShape relationShape = GraphicsUpdater.getRelationShape(entityShape);
		ContainerShape basicShape = GraphicsUpdater.getBasicShape(entityShape);
		
        entityShape.getGraphicsAlgorithm().setHeight(JPAEditorConstants.ENTITY_MIN_HEIGHT + 
        		primaryShape.getGraphicsAlgorithm().getHeight() + relationShape.getGraphicsAlgorithm().getHeight()
        		+ basicShape.getGraphicsAlgorithm().getHeight() + 2);
	}
	
	public static int getNextCompartmentY(ContainerShape compartmentShape){
		if(isEmptyCompartment(compartmentShape))
			return compartmentShape.getGraphicsAlgorithm().getY();
		return compartmentShape.getGraphicsAlgorithm().getY() + compartmentShape.getGraphicsAlgorithm().getHeight();
	}

	public static ContainerShape getPrimaryShape(ContainerShape entityShape){
		List<Shape> children = entityShape.getChildren();
		if(children.size() < PRIMARY_SHAPE_INDEX+1)
			return null;
		GraphicsAlgorithm alg = children.get(PRIMARY_SHAPE_INDEX).getGraphicsAlgorithm();
		ContainerShape primaryShape = (ContainerShape)Graphiti.getPeService().getActiveContainerPe(alg);
		return primaryShape;
	}
	
	public static ContainerShape getCompartmentSeparatorShape(ContainerShape compartmentShape){
		GraphicsAlgorithm alg = compartmentShape.getChildren().get(0).getGraphicsAlgorithm();
		ContainerShape primarySeparatorShape = (ContainerShape) Graphiti.getPeService().getActiveContainerPe(alg);
		return primarySeparatorShape;
	}
	
	public static ContainerShape getRelationShape(ContainerShape entityShape){
		List<Shape> children = entityShape.getChildren();
		if(children.size()<RELATION_SHAPE_CONSTANT+1)
			return null;
		GraphicsAlgorithm alg = children.get(RELATION_SHAPE_CONSTANT).getGraphicsAlgorithm();
		ContainerShape relationShape = (ContainerShape) Graphiti.getPeService().getActiveContainerPe(alg);
		return relationShape;
	}
	
	public static ContainerShape getBasicShape(ContainerShape entityShape){
		List<Shape> children = entityShape.getChildren();
		if(children.size() < BASIC_SHAPE_INDEX+1)
			return null;
		GraphicsAlgorithm alg = children.get(BASIC_SHAPE_INDEX).getGraphicsAlgorithm();
		ContainerShape basicShape = (ContainerShape) Graphiti.getPeService().getActiveContainerPe(alg);
		return basicShape;
	}
	
	public static boolean isCollapsed(ContainerShape compartmentShape) {
		ContainerShape container = compartmentShape.getContainer();
		String result = null;
		if(compartmentShape.equals(GraphicsUpdater.getPrimaryShape(container)))
			result = Graphiti.getPeService().getPropertyValue(container, JPAEditorConstants.PRIMARY_COLLAPSED);
		else if(compartmentShape.equals(GraphicsUpdater.getBasicShape(container)))
			result = Graphiti.getPeService().getPropertyValue(container, JPAEditorConstants.BASIC_COLLAPSED);
		else if(compartmentShape.equals(GraphicsUpdater.getRelationShape(container)))
			result = Graphiti.getPeService().getPropertyValue(container, JPAEditorConstants.RELATION_COLLAPSED);
		if(Boolean.toString(true).equals(result)) 
			return true;
		return false;
	}
	
	/**
	 * @param compartmentShape
	 * @param collapsed 
	 * @return true if successful
	 */
	public static boolean setCollapsed(ContainerShape compartmentShape, boolean collapsed) {
		ContainerShape container = compartmentShape.getContainer();
		if(compartmentShape.equals(GraphicsUpdater.getPrimaryShape(container))) {
			Graphiti.getPeService().setPropertyValue(container, JPAEditorConstants.PRIMARY_COLLAPSED, Boolean.toString(collapsed));
			return true;
		}
		else if(compartmentShape.equals(GraphicsUpdater.getBasicShape(container))) {
			Graphiti.getPeService().setPropertyValue(container, JPAEditorConstants.BASIC_COLLAPSED, Boolean.toString(collapsed));
			return true;
		}
		else if(compartmentShape.equals(GraphicsUpdater.getRelationShape(container))) {
			Graphiti.getPeService().setPropertyValue(container, JPAEditorConstants.RELATION_COLLAPSED, Boolean.toString(collapsed));
			return true;
		}
		return false;
	}
	
	public static void updateHeader(ContainerShape entityShape, final String newHeader) {
		final Text txt = getHeaderText(entityShape);
		if (txt == null)
			return;
		//if (!JPAEditorUtil.areHeadersEqual(txt.getValue(), newHeader)) {

		if (!txt.getValue().equals(newHeader)) {
			TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(txt);
			RecordingCommand rc = new RecordingCommand(ted) {
				@Override
				protected void doExecute() {
					txt.setValue(newHeader);		
				}		
			};			
			ted.getCommandStack().execute(rc);
		}
	}
	
	private static Text getHeaderText(ContainerShape entityShape) {
		if(entityShape == null)
			return null;		
		List<Shape> shapes = entityShape.getChildren();
		Iterator<Shape> shIt = shapes.iterator();
		Shape headerShape = null;
		while (shIt.hasNext()) {
			headerShape = shIt.next();
			String shapeType = Graphiti.getPeService().getPropertyValue(headerShape, JPAEditorConstants.PROP_SHAPE_TYPE);
			if (ShapeType.HEADER.toString().equals(shapeType)) 
				break;
			headerShape = null;
		}
		if (headerShape == null)
			return null;
		GraphicsAlgorithm ga = headerShape.getGraphicsAlgorithm();
		if (ga == null)
			return null;
		if (ga.getGraphicsAlgorithmChildren().size() == 0)
			return null;
		Text txt = (Text)ga.getGraphicsAlgorithmChildren().get(0);
		return txt;
	}
}

Back to the top