Skip to main content
summaryrefslogtreecommitdiffstats
blob: ea9da263fa1499bda0443bf64cfb0b2fff135c47 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.itemcreation;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.commands.CreateItemCommand;
import org.eclipse.jst.pagedesigner.dom.DOMPositionHelper;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.tools.ExposeHelper;
import org.eclipse.jst.pagedesigner.validation.caret.ActionData;
import org.eclipse.jst.pagedesigner.validation.caret.DnDPositionValidator;
import org.eclipse.jst.pagedesigner.viewer.DesignPosition;
import org.eclipse.jst.pagedesigner.viewer.EditPartPositionHelper;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;

/**
 * @author mengbo
 */
public class ItemCreationEditPolicy extends GraphicalEditPolicy {
	private RectangleFigure _feedbackFigure;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#getCommand(org.eclipse.gef.Request)
	 */
	public Command getCommand(Request request) {
		if (request instanceof ItemCreationRequest) {
			ItemCreationRequest r = (ItemCreationRequest) request;
			DesignPosition position = EditPartPositionHelper
					.findEditPartPosition(getHost(), r.getLocation(),
							new DnDPositionValidator(new ActionData(
									ActionData.PALETTE_DND, request)));
			IDOMPosition domposition = null;
			if (position == null) {
				return null;
			} else {
				domposition = DOMPositionHelper.toDOMPosition(position);
			}
			if (domposition == null) {
				return null;
			}
			return new CreateItemCommand(
					PDPlugin
							.getResourceString("ItemCreationEditPolicy.CommandLabel.CreateItem"),//$NON-NLS-1$
					getViewer(getHost()), domposition, r.getItemDescriptor());
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#getTargetEditPart(org.eclipse.gef.Request)
	 */
	public EditPart getTargetEditPart(Request request) {
		if (request instanceof ItemCreationRequest) {
			ItemCreationRequest r = (ItemCreationRequest) request;
			DesignPosition position = EditPartPositionHelper
					.findEditPartPosition(getHost(), r.getLocation(),
							new DnDPositionValidator(new ActionData(
									ActionData.PALETTE_DND, request)));

			if (position == null) {
				return null;
			}

			EditPart container = position.getContainerPart();
			return container;
		}
		return null;
	}

	/**
	 * @param host
	 * @return
	 */
	private IHTMLGraphicalViewer getViewer(EditPart host) {
		return (IHTMLGraphicalViewer) ((GraphicalEditPart) host).getViewer();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#eraseTargetFeedback(org.eclipse.gef.Request)
	 */
	public void eraseTargetFeedback(Request request) {
		if (_feedbackFigure != null) {
			removeFeedback(_feedbackFigure);
			_feedbackFigure = null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#showTargetFeedback(org.eclipse.gef.Request)
	 */
	public void showTargetFeedback(Request request) {
		if (request instanceof ItemCreationRequest) {
			ItemCreationRequest r = (ItemCreationRequest) request;
			DesignPosition position = EditPartPositionHelper
					.findEditPartPosition(getHost(), r.getLocation(),
							new DnDPositionValidator(new ActionData(
									ActionData.PALETTE_DND, request)));

			if (position == null) {
				return;
			}
			Rectangle rect = EditPartPositionHelper
					.convertToAbsoluteCaretRect(position);
			showFeedbackRect(rect);
			if (getHost() instanceof GraphicalEditPart) {
				ExposeHelper exposeHelper = new ExposeHelper(
						getViewer(getHost()));
				exposeHelper.adjustVertical(r.getLocation());
			}
		}
	}

	protected RectangleFigure getFeedbackFigure() {
		if (_feedbackFigure == null) {
			_feedbackFigure = new RectangleFigure();
			_feedbackFigure.setFill(true);
			_feedbackFigure.setXOR(true);
			_feedbackFigure.setOutline(true);
			_feedbackFigure.setLineWidth(1);
			_feedbackFigure.setForegroundColor(ColorConstants.red);
			_feedbackFigure.setBounds(new Rectangle(0, 0, 0, 0));
			addFeedback(_feedbackFigure);
		}
		return _feedbackFigure;
	}

	protected void showFeedbackRect(Rectangle rect) {
		RectangleFigure pf = getFeedbackFigure();
		pf.translateToRelative(rect);
		pf.setBounds(rect);
	}
}

Back to the top