Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: caea634a59f212d5122202b188a6baf6498a2d0c (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
/*******************************************************************************
 * <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.feature;

import java.util.HashSet;

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.IAddContext;
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature;
import org.eclipse.graphiti.mm.algorithms.Image;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.jpt.jpa.core.context.java.JavaSpecifiedPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.JPAEditorImageProvider;
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;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.Wrp;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants.ShapeType;


public class GraphicalAddAttributeFeature extends AbstractAddShapeFeature {

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

	public PictogramElement add(final IAddContext context) {
		final ContainerShape entityShape = context.getTargetContainer();
		final Wrp wrp = new Wrp();
		TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(entityShape);
		ted.getCommandStack().execute(new RecordingCommand(ted) {
			@Override
			protected void doExecute() {
				JavaSpecifiedPersistentAttribute newAttr = (JavaSpecifiedPersistentAttribute) context.getNewObject();
				String txt = JPAEditorUtil.getText(newAttr);
				HashSet<String> annots = JpaArtifactFactory.instance().getAnnotationNames(newAttr);
				ContainerShape textShape = null;
				ContainerShape primaryShape = GraphicsUpdater.getPrimaryShape(entityShape);
				ContainerShape relationShape = GraphicsUpdater.getRelationShape(entityShape);
				ContainerShape basicShape = GraphicsUpdater.getBasicShape(entityShape);
				textShape = addAttributeToProperlyShape(entityShape, txt, annots, primaryShape, relationShape, basicShape);
				link(textShape, newAttr);
				layoutPictogramElement(entityShape);
				wrp.setObj(textShape);
			}
			
		});
		return (PictogramElement)wrp.getObj();
	}

	private ContainerShape addAttributeToProperlyShape(ContainerShape entityShape, String txt, HashSet<String> annots,
			ContainerShape primaryShape, ContainerShape relationShape, ContainerShape basicShape) {
		ContainerShape textShape = null;
		if (annots.contains(JPAEditorConstants.ANNOTATION_ID) || annots.contains(JPAEditorConstants.ANNOTATION_EMBEDDED_ID)
				|| annots.contains(JPAEditorConstants.ANNOTATION_MAPS_ID)) {
			textShape = addAttributeToShape(entityShape, txt, annots, primaryShape);
		} else if (!annots.contains(JPAEditorConstants.ANNOTATION_ID) && !annots.contains(JPAEditorConstants.ANNOTATION_MAPS_ID) 
				 && (annots.contains(JPAEditorConstants.ANNOTATION_MANY_TO_MANY)
				|| annots.contains(JPAEditorConstants.ANNOTATION_MANY_TO_ONE)
				|| annots.contains(JPAEditorConstants.ANNOTATION_ONE_TO_MANY)
				|| annots.contains(JPAEditorConstants.ANNOTATION_ONE_TO_ONE))) {
			textShape = addAttributeToShape(entityShape, txt, annots, relationShape);
		} else {
			textShape = addAttributeToShape(entityShape, txt, annots, basicShape);
		}
		return textShape;
	}

	private ContainerShape addAttributeToShape(ContainerShape entityShape, String txt, HashSet<String> annots,
			ContainerShape containerShape) {
		ContainerShape textShape = null;
		int childrenSizeBefore = containerShape.getChildren().size();
		int containerHeightBefore = containerShape.getGraphicsAlgorithm().getHeight();
		textShape = addAttribute(getFeatureProvider(), containerShape, txt, annots);

		GraphicsUpdater.updateContainer(containerShape, childrenSizeBefore, containerHeightBefore);
		GraphicsUpdater.updateEntityShape(entityShape);

		return textShape;
	}

	private static ContainerShape addAttribute(IJPAEditorFeatureProvider fp, ContainerShape containerShape,
			String attribTxt, HashSet<String> annotations) {

		int width = containerShape.getContainer().getGraphicsAlgorithm().getWidth();
		ContainerShape iconShape = Graphiti.getPeService().createContainerShape(containerShape, false);
		Graphiti.getPeService().setPropertyValue(iconShape, JPAEditorConstants.PROP_SHAPE_TYPE, ShapeType.ICON.toString());
		int attribIndex = fp.getAttribsNum(containerShape);
		Rectangle iconRect = UpdateAttributeFeature.addRectangleForIcon(iconShape, attribIndex);
		Image icon = null;
		Object ob = fp.getBusinessObjectForPictogramElement(containerShape.getContainer());
		JavaPersistentType jpt = (JavaPersistentType)ob;
		boolean isMethodAnnotated = JpaArtifactFactory.instance().isMethodAnnotated(jpt); 
		boolean isCollection = isMethodAnnotated ? 
				JpaArtifactFactory.instance().isGetterMethodReturnTypeCollection(containerShape.getContainer(), fp, attribTxt) :
				JpaArtifactFactory.instance().isCollection(containerShape.getContainer(), fp, attribTxt); 
		if (isCollection && annotations.isEmpty()) {
			icon = Graphiti.getGaService().createImage(iconRect, JPAEditorImageProvider.ICON_UNMAPPED);
		} else{
		    icon = JPAEditorUtil.createAttributeIcon(iconRect, annotations);
		}
		Graphiti.getGaService().setLocationAndSize(icon, JPAEditorConstants.ICON_X, JPAEditorConstants.ICON_Y,
				JPAEditorConstants.ICON_WIDTH, JPAEditorConstants.ICON_HEIGHT);
		ContainerShape textShape = Graphiti.getPeService().createContainerShape(containerShape, false);
		Graphiti.getPeService().setPropertyValue(textShape, JPAEditorConstants.PROP_SHAPE_TYPE, ShapeType.ATTRIBUTE.toString());
		Rectangle textRectangle = UpdateAttributeFeature.addRectangleForText(textShape, attribIndex, width);
		textShape.setActive(true);
		Text text = UpdateAttributeFeature.addText(fp, textRectangle, attribTxt);
		Graphiti.getGaService().setWidth(text, width - JPAEditorConstants.ATTRIBUTE_TEXT_RECT_WIDTH_REDUCER);
		Graphiti.getGaService().setLocationAndSize(text, 1, -2, width
				- JPAEditorConstants.ATTRIBUTE_TEXT_RECT_WIDTH_REDUCER, JPAEditorConstants.ATTRIBUTE_RECT_HEIGHT);
		fp.increaseAttribsNum(containerShape);
		return textShape;
	}

	@Override
	public IJPAEditorFeatureProvider getFeatureProvider() {
		return (IJPAEditorFeatureProvider) super.getFeatureProvider();
	}

	public boolean canAdd(IAddContext context) {
		return false;
	}
}

Back to the top