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.texteditor.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 contiguous block of added, deleted and changes lines in the currently
20: * displayed document to the state in the reference document.
21: *
22: * @since 3.0
23: */
24: public class RevertBlockAction extends QuickDiffRestoreAction {
25: /** Resource key prefix. */
26: private static final String PREFIX = "RevertBlockAction."; //$NON-NLS-1$
27:
28: /** The line to be restored. Set in <code>update()</code>. */
29: private int fLine;
30:
31: /**
32: * Creates a new instance.
33: *
34: * @param editor the editor this action belongs to
35: * @param isRulerAction <code>true</code> if this is a ruler action
36: */
37: public RevertBlockAction(ITextEditor editor, boolean isRulerAction) {
38: super (PREFIX, editor, isRulerAction);
39: }
40:
41: /*
42: * @see org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction#computeEnablement()
43: */
44: public boolean computeEnablement() {
45: if (!super .computeEnablement())
46: return false;
47:
48: fLine = getLastLine();
49: if (fLine == -1)
50: return false;
51: ILineDiffer differ = getDiffer();
52: if (differ == null)
53: return false;
54: ILineDiffInfo info = differ.getLineInfo(fLine);
55: if (info == null
56: || info.getChangeType() == ILineDiffInfo.UNCHANGED)
57: return false;
58:
59: boolean hasBlock = false;
60: if (fLine > 0) {
61: info = differ.getLineInfo(fLine - 1);
62: hasBlock = info != null && info.hasChanges();
63: }
64: if (!hasBlock) {
65: info = differ.getLineInfo(fLine + 1);
66: hasBlock = info != null && info.hasChanges();
67: }
68: if (hasBlock)
69: return true;
70:
71: return false;
72: }
73:
74: /*
75: * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
76: */
77: public void runCompoundChange() {
78: if (!isEnabled())
79: return;
80: ILineDiffer differ = getDiffer();
81: if (differ != null) {
82: try {
83: differ.revertBlock(fLine);
84: } catch (BadLocationException e) {
85: setStatus(e.getMessage());
86: }
87: }
88: }
89: }
|