Skip to main content
summaryrefslogtreecommitdiffstats
blob: 653ba15e64f28d4d3fee436228345496a448f3ee (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
/*******************************************************************************
 * 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.parts;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.requests.LocationRequest;
import org.eclipse.jst.jsf.common.dom.TagIdentifier;
import org.eclipse.jst.jsf.core.internal.tld.TagIdentifierFactory;
import org.eclipse.jst.pagedesigner.dnd.LocalDropRequest;
import org.eclipse.jst.pagedesigner.dnd.internal.LocalDropEditPolicy;
import org.eclipse.jst.pagedesigner.editpolicies.DragMoveEditPolicy;
import org.eclipse.jst.pagedesigner.itemcreation.ItemCreationEditPolicy;
import org.eclipse.jst.pagedesigner.itemcreation.ItemCreationRequest;
import org.eclipse.jst.pagedesigner.tools.RangeDragTracker;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author mengbo
 */
public abstract class NodeEditPart extends AbstractGraphicalEditPart implements
		INodeAdapter {

	private IDOMDocument _destDocument;
	private boolean      _isDragActive;
    
	/**
	 * this method is called from the HTMLEditPartsFactory directly after the
	 * part's creation.
	 * 
	 * @param doc
	 */
	public void setDestDocumentForDesign(IDOMDocument doc) {
		this._destDocument = doc;
	}

	/**
	 * @return to owner document of this part's model node
	 */
	public IDOMDocument getDestDocumentForDesign() {
		if (this._destDocument == null) {
			return (IDOMDocument) this.getIDOMNode().getOwnerDocument();
		}
        return this._destDocument;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.NODE_ROLE, null);
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, null);
		installEditPolicy(LocalDropRequest.REQ_LOCAL_DROP,
				new LocalDropEditPolicy());
		installEditPolicy(ItemCreationRequest.REQ_ITEM_CREATION,
				new ItemCreationEditPolicy());
		installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE,
				new DragMoveEditPolicy());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ibm.sse.model.INodeAdapter#isAdapterForType(java.lang.Object)
	 */
	public boolean isAdapterForType(Object type) {
		return type == EditPart.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#addNotify()
	 */
	public void addNotify() {
		Object obj = getModel();
		if (obj instanceof INodeNotifier) {
			((INodeNotifier) obj).addAdapter(this);
		}
		super.addNotify();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#removeNotify()
	 */
	public void removeNotify() {
		super.removeNotify();
		Object obj = getModel();
		if (obj instanceof INodeNotifier) {
			((INodeNotifier) obj).removeAdapter(this);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.EditPart#deactivate()
	 */
	public void deactivate() {
		LocationRequest hoverRequest = new LocationRequest();
		hoverRequest.setType(RequestConstants.REQ_SELECTION_HOVER);
		this.eraseTargetFeedback(hoverRequest);
		super.deactivate();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class key) {
		Object obj = getModel();
		if (key == IPropertySource.class) {
			if (obj instanceof INodeNotifier) {
				return ((INodeNotifier) obj)
						.getAdapterFor(IPropertySource.class);
			}
		}

		if (obj instanceof IAdaptable) {
			Object ret = ((IAdaptable) obj).getAdapter(key);
			if (ret != null)
				return ret;
		}
		return super.getAdapter(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getDragTracker(org.eclipse.gef.Request)
	 */
	public DragTracker getDragTracker(Request request) {
		return new RangeDragTracker(this);
	}

	/**
	 * @return the model node as an IDOMNode
	 */
	public IDOMNode getIDOMNode() {
		return ((IDOMNode) getModel());
	}

    /**
     * @return the model node as a node
     */
    public Node getDOMNode()
    {
        return ((Node)getModel());
    }
    
    /**
     * @return if this edit part's model is an Element, then returns
     * the tag identifier. Otherwise, null.
     */
    public TagIdentifier getTagIdentifier()
    {
        Node node = getDOMNode();
        
        if (node instanceof Element)
        {
            return TagIdentifierFactory.createDocumentTagWrapper(((Element)node));
        }
        
        return null;
    }
    
	/**
	 * if a EditPart don't support caret inside it, and don't can't have child
	 * edit part, then we call it as a widget.
	 * 
	 * @return true if this part represents a widget
	 */
	public boolean isWidget() {
		return false; // child class must override.
	}

	/**
	 * whether this EditPart allow the selection range to have one edge in the
	 * edit part and one edge outside the edit part.
	 * 
	 * @return true if selection range across is allowed
	 */
	public boolean allowSelectionRangeAcross() {
		return true;
	}

	/**
	 * @return true if this part is resizable
	 */
	public boolean isResizable() {
		return false;
	}
    
    /**
     * @return true if drag is active on this part
     */
    public boolean isDragActive() {
        return _isDragActive;
    }
    
    /**
     * @param newValue
     */
    public void setDragActive(boolean newValue)
    {
        _isDragActive = newValue;
    }
    
    /**
     * @param mouseLocation
     * @return this edit part's cursor or null if this edit part
     * does not wish to specify a specific cursor (the default
     * should be used).  
     * Note that this is only called when no drag tracker is active,
     * when the mouse enters the edit part with the RangeSelectionTool
     * and the current state is INITIAL.  This allows the editpart to
     * specify a custom mouse-over tool cursor
     * TODO: perhaps getCursor is not a specific enough a name
     */
    public Cursor getCursor(Point mouseLocation)
    {
        return null;
    }
}

Back to the top