Project

General

Profile

Download (4.65 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.gef.editpolicies;
12

    
13
import org.eclipse.gef.Request;
14
import org.eclipse.gef.RequestConstants;
15
import org.eclipse.gef.commands.Command;
16
import org.eclipse.gef.requests.DirectEditRequest;
17

    
18
/**
19
 * Shows DirectEdit feedback and creates the Command to perform a "direct edit".
20
 * <I>Direct Edit</I> is when the User is editing a property of an EditPart
21
 * directly (as opposed to in the Properties View) in the Viewer using a
22
 * {@link org.eclipse.jface.viewers.CellEditor}. This EditPolicy is typically
23
 * installed using {@link org.eclipse.gef.EditPolicy#DIRECT_EDIT_ROLE}.
24
 * 
25
 * @author hudsonr
26
 * @since 2.0
27
 */
28
public abstract class DirectEditPolicy extends GraphicalEditPolicy {
29

    
30
	private boolean showing;
31

    
32
	/**
33
	 * @see org.eclipse.gef.EditPolicy#eraseSourceFeedback(Request)
34
	 */
35
	public void eraseSourceFeedback(Request request) {
36
		if (RequestConstants.REQ_DIRECT_EDIT == request.getType())
37
			eraseDirectEditFeedback((DirectEditRequest) request);
38
	}
39

    
40
	/**
41
	 * If feedback is being shown, this method calls
42
	 * {@link #revertOldEditValue(DirectEditRequest)}.
43
	 * 
44
	 * @param request
45
	 *            the DirectEditRequest
46
	 */
47
	protected void eraseDirectEditFeedback(DirectEditRequest request) {
48
		if (showing) {
49
			revertOldEditValue(request);
50
			showing = false;
51
		}
52
	}
53

    
54
	/**
55
	 * @see org.eclipse.gef.EditPolicy#getCommand(Request)
56
	 */
57
	public Command getCommand(Request request) {
58
		if (RequestConstants.REQ_DIRECT_EDIT == request.getType())
59
			return getDirectEditCommand((DirectEditRequest) request);
60
		return null;
61
	}
62

    
63
	/**
64
	 * Returns the <code>Command</code> to perform the direct edit.
65
	 * 
66
	 * @param request
67
	 *            the DirectEditRequest
68
	 * @return the command to perform the direct edit
69
	 */
70
	protected abstract Command getDirectEditCommand(DirectEditRequest request);
71

    
72
	/**
73
	 * Helps erase feedback by reverting the original edit value. The rule when
74
	 * using GEF is that all feedback is removed before changes to the model are
75
	 * made. By default, the host is sent
76
	 * {@link org.eclipse.gef.EditPart#refresh()}, which should cause it to
77
	 * refresh all properties.
78
	 * 
79
	 * Subclasses can override this method to perform a more specific revert.
80
	 * 
81
	 * @see #storeOldEditValue(DirectEditRequest)
82
	 * @param request
83
	 *            the DirectEditRequest
84
	 */
85
	protected void revertOldEditValue(DirectEditRequest request) {
86
		getHost().refresh();
87
	}
88

    
89
	/**
90
	 * @see org.eclipse.gef.EditPolicy#showSourceFeedback(Request)
91
	 */
92
	public void showSourceFeedback(Request request) {
93
		if (RequestConstants.REQ_DIRECT_EDIT == request.getType())
94
			showDirectEditFeedback((DirectEditRequest) request);
95
	}
96

    
97
	/**
98
	 * Pushes the original edit value if necessary, and shows feedback.
99
	 * 
100
	 * @param request
101
	 *            the DirectEditRequest
102
	 */
103
	protected void showDirectEditFeedback(DirectEditRequest request) {
104
		if (!showing) {
105
			storeOldEditValue(request);
106
			showing = true;
107
		}
108
		showCurrentEditValue(request);
109
	}
110

    
111
	/**
112
	 * Override to show the current direct edit value in the host's Figure.
113
	 * Although the CellEditor will probably cover the figure's display of this
114
	 * value, updating the figure will cause its preferred size to reflect the
115
	 * new value.
116
	 * 
117
	 * @param request
118
	 *            the DirectEditRequest
119
	 */
120
	protected abstract void showCurrentEditValue(DirectEditRequest request);
121

    
122
	/**
123
	 * Called to remember the old value before direct edit feedback begins.
124
	 * After feedback is over, {@link #revertOldEditValue(DirectEditRequest)} is
125
	 * called to undo the changes done by feedback. By default, this method
126
	 * nothing.
127
	 * 
128
	 * @param request
129
	 *            the DirectEditRequest
130
	 */
131
	protected void storeOldEditValue(DirectEditRequest request) {
132
	}
133

    
134
	/**
135
	 * Returns <code>true</code> for {@link RequestConstants#REQ_DIRECT_EDIT}.
136
	 * {@link org.eclipse.gef.ui.actions.DirectEditAction} will determine
137
	 * enablement based on whether the selected EditPart understands
138
	 * "direct edit".
139
	 * 
140
	 * @see org.eclipse.gef.EditPolicy#understandsRequest(Request)
141
	 */
142
	public boolean understandsRequest(Request request) {
143
		if (RequestConstants.REQ_DIRECT_EDIT.equals(request.getType()))
144
			return true;
145
		return super.understandsRequest(request);
146
	}
147

    
148
}
(9-9/26)