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.ITextSelection;
14: import org.eclipse.jface.text.source.ILineDiffInfo;
15: import org.eclipse.jface.text.source.ILineDiffer;
16:
17: import org.eclipse.ui.texteditor.ITextEditor;
18:
19: /**
20: * Action that will revert the added, deleted and changes lines in the selection on the currently
21: * displayed document to the state in the reference document.
22: *
23: * @since 3.0
24: */
25: public class RevertSelectionAction extends QuickDiffRestoreAction {
26: /** The first line to be restored. Set in <code>update()</code>. */
27: private int fStartLine;
28: /** The last line to be restored. Set in <code>update()</code>. */
29: private int fEndLine;
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 RevertSelectionAction(ITextEditor editor,
38: boolean isRulerAction) {
39: super ("RevertSelectionAction.", editor, isRulerAction); //$NON-NLS-1$
40: }
41:
42: /*
43: * @see org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction#computeEnablement()
44: */
45: public boolean computeEnablement() {
46: if (!super .computeEnablement())
47: return false;
48:
49: ITextSelection selection = getSelection();
50: if (selection == null)
51: return false;
52: fStartLine = selection.getStartLine();
53: fEndLine = selection.getEndLine();
54: // only enable if mouse activity is inside line range
55: int activityLine = getLastLine();
56: if (activityLine == -1 || activityLine < fStartLine
57: || activityLine > fEndLine + 1)
58: // + 1 to cover the case where the selection goes to the offset of the next line
59: return false;
60: ILineDiffer differ = getDiffer();
61: if (differ == null)
62: return false;
63: // only enable if selection covers at least two lines
64: if (fEndLine > fStartLine) {
65: for (int i = fStartLine; i <= fEndLine; i++) {
66: ILineDiffInfo info = differ.getLineInfo(i);
67: if (info != null && info.hasChanges()) {
68: return true;
69: }
70: }
71: }
72: return false;
73: }
74:
75: /*
76: * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
77: */
78: public void runCompoundChange() {
79: // recheck if run without being enabled
80: if (!isEnabled())
81: return;
82:
83: ILineDiffer differ = getDiffer();
84: if (differ != null) {
85: try {
86: differ.revertSelection(fStartLine, fEndLine
87: - fStartLine + 1);
88: } catch (BadLocationException e) {
89: setStatus(e.getMessage());
90: }
91: }
92: }
93:
94: }
|