KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > quickdiff > RestoreAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.texteditor.quickdiff;
12
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.source.ILineDiffInfo;
16 import org.eclipse.jface.text.source.ILineDiffer;
17
18 import org.eclipse.ui.internal.texteditor.NLSUtility;
19 import org.eclipse.ui.texteditor.ITextEditor;
20
21 /**
22  * Action that will restore a block of deleted lines at the current caret position in an editor.
23  *
24  * @since 3.0
25  */

26 public class RestoreAction extends QuickDiffRestoreAction {
27     /** Resource key prefix. */
28     private static final String JavaDoc PREFIX= "RestoreAction."; //$NON-NLS-1$
29

30     /** The line to be restored. Set in <code>update()</code>. */
31     private int fLine;
32
33     /**
34      * Creates a new instance.
35      *
36      * @param editor the editor this action belongs to
37      * @param isRulerAction <code>true</code> if this is a ruler action
38      */

39     public RestoreAction(ITextEditor editor, boolean isRulerAction) {
40         super(PREFIX, editor, isRulerAction);
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         fLine= getLastLine();
51         if (fLine == -1)
52             return false;
53         ILineDiffer differ= getDiffer();
54         if (differ == null)
55             return false;
56         ILineDiffInfo info= differ.getLineInfo(fLine);
57         if (info == null || (info.getRemovedLinesAbove() <= 0 && info.getRemovedLinesBelow() <= 0))
58             return false;
59
60         if (info.getRemovedLinesBelow() == 0) {
61             fLine--;
62         } else if (info.getRemovedLinesAbove() != 0) {
63             // take the line below
64
}
65         info= differ.getLineInfo(fLine);
66         if (info == null)
67             return false;
68         if (info.getRemovedLinesBelow() == 1)
69             setText(QuickDiffMessages.RestoreAction_label);
70         else
71             setText(NLSUtility.format(QuickDiffMessages.RestoreAction_multiple_label, String.valueOf(info.getRemovedLinesBelow())));
72         return true;
73     }
74
75     /*
76      * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
77      */

78     public void runCompoundChange() {
79         if (!isEnabled())
80             return;
81         ILineDiffer differ= getDiffer();
82         if (differ != null) {
83             try {
84                 differ.restoreAfterLine(fLine);
85             } catch (BadLocationException e) {
86                 setStatus(e.getMessage());
87             }
88         }
89     }
90 }
91
Popular Tags