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.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.1
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 (QuickDiffMessages.getResourceBundle(),
40: "RevertSelectionAction.", editor, isRulerAction); //$NON-NLS-1$
41: }
42:
43: /*
44: * @see org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction#computeEnablement()
45: */
46: public boolean computeEnablement() {
47: if (!super .computeEnablement())
48: return false;
49:
50: ITextSelection selection = getSelection();
51: if (selection == null)
52: return false;
53: fStartLine = selection.getStartLine();
54: fEndLine = selection.getEndLine();
55: // only enable if mouse activity is inside line range
56: int activityLine = getLastLine();
57: if (activityLine == -1 || activityLine < fStartLine
58: || activityLine > fEndLine + 1)
59: // + 1 to cover the case where the selection goes to the offset of the next line
60: return false;
61: ILineDiffer differ = getDiffer();
62: if (differ == null)
63: return false;
64: // only enable if selection covers at least two lines
65: if (fEndLine > fStartLine) {
66: for (int i = fStartLine; i <= fEndLine; i++) {
67: ILineDiffInfo info = differ.getLineInfo(i);
68: if (info != null && info.hasChanges()) {
69: return true;
70: }
71: }
72: }
73: return false;
74: }
75:
76: /*
77: * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
78: */
79: public void runCompoundChange() {
80: // recheck if run without being enabled
81: if (!isEnabled())
82: return;
83:
84: ILineDiffer differ = getDiffer();
85: if (differ != null) {
86: try {
87: differ.revertSelection(fStartLine, fEndLine
88: - fStartLine + 1);
89: } catch (BadLocationException e) {
90: setStatus(e.getMessage());
91: }
92: }
93: }
94:
95: }
|