01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.editors.quickdiff;
11:
12: import org.eclipse.jface.text.BadLocationException;
13: import org.eclipse.jface.text.source.ILineDiffInfo;
14: import org.eclipse.jface.text.source.ILineDiffer;
15:
16: import org.eclipse.ui.texteditor.ITextEditor;
17:
18: /**
19: * Action that will revert a line in the currently displayed document to the state in the
20: * reference document.
21: *
22: * @since 3.1
23: */
24: public class RevertLineAction extends QuickDiffRestoreAction {
25: /** Resource key prefix. */
26: private static final String PREFIX = "RevertLineAction."; //$NON-NLS-1$
27: /** Resource key for added lines - they will be deleted. */
28: private static final String DELETE_KEY = PREFIX + "delete.label"; //$NON-NLS-1$
29: /** Resource key for changed lines - they will be reverted. */
30: private static final String REVERT_KEY = PREFIX + "label"; //$NON-NLS-1$
31:
32: /** The line to be restored. Set in <code>update()</code>. */
33: private int fLine;
34:
35: /**
36: * Creates a new instance.
37: *
38: * @param editor the editor this action belongs to
39: * @param isRulerAction <code>true</code> if this is a ruler action
40: */
41: public RevertLineAction(ITextEditor editor, boolean isRulerAction) {
42: super (QuickDiffMessages.getResourceBundle(), PREFIX, editor,
43: isRulerAction);
44: }
45:
46: /*
47: * @see org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction#computeEnablement()
48: */
49: public boolean computeEnablement() {
50: if (!super .computeEnablement())
51: return false;
52:
53: fLine = getLastLine();
54: if (fLine == -1)
55: return false;
56: ILineDiffer differ = getDiffer();
57: if (differ == null)
58: return false;
59: ILineDiffInfo info = differ.getLineInfo(fLine);
60: if (info == null
61: || info.getChangeType() == ILineDiffInfo.UNCHANGED)
62: return false;
63:
64: if (info.getChangeType() == ILineDiffInfo.ADDED)
65: setText(QuickDiffMessages.getString(DELETE_KEY));
66: else
67: setText(QuickDiffMessages.getString(REVERT_KEY));
68: return true;
69: }
70:
71: /*
72: * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
73: */
74: public void runCompoundChange() {
75: if (!isEnabled())
76: return;
77: ILineDiffer differ = getDiffer();
78: if (differ != null) {
79: try {
80: differ.revertLine(fLine);
81: } catch (BadLocationException e) {
82: setStatus(e.getMessage());
83: }
84: }
85: }
86: }
|