01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 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.internal.editors.text.NLSUtility;
17: import org.eclipse.ui.texteditor.ITextEditor;
18:
19: /**
20: * Action that will restore a block of deleted lines at the current caret position in an editor.
21: *
22: * @since 3.1
23: */
24: public class RestoreAction extends QuickDiffRestoreAction {
25: /** Resource key prefix. */
26: private static final String PREFIX = "RestoreAction."; //$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 RestoreAction(ITextEditor editor, boolean isRulerAction) {
38: super (QuickDiffMessages.getResourceBundle(), PREFIX, editor,
39: isRulerAction);
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: fLine = getLastLine();
50: if (fLine == -1)
51: return false;
52: ILineDiffer differ = getDiffer();
53: if (differ == null)
54: return false;
55: ILineDiffInfo info = differ.getLineInfo(fLine);
56: if (info == null
57: || (info.getRemovedLinesAbove() <= 0 && info
58: .getRemovedLinesBelow() <= 0))
59: return false;
60:
61: if (info.getRemovedLinesBelow() == 0) {
62: fLine--;
63: } else if (info.getRemovedLinesAbove() != 0) {
64: // take the line below
65: }
66: info = differ.getLineInfo(fLine);
67: if (info == null)
68: return false;
69: if (info.getRemovedLinesBelow() == 1)
70: setText(QuickDiffMessages.getString("RestoreAction.label")); //$NON-NLS-1$
71: else
72: setText(NLSUtility
73: .format(
74: QuickDiffMessages
75: .getString("RestoreAction.multiple.label"), String.valueOf(info.getRemovedLinesBelow()))); //$NON-NLS-1$
76: return true;
77: }
78:
79: /*
80: * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
81: */
82: public void runCompoundChange() {
83: if (!isEnabled())
84: return;
85: ILineDiffer differ = getDiffer();
86: if (differ != null) {
87: try {
88: differ.restoreAfterLine(fLine);
89: } catch (BadLocationException e) {
90: setStatus(e.getMessage());
91: }
92: }
93: }
94: }
|