KickJava   Java API By Example, From Geeks To Geeks.

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


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.texteditor.ITextEditor;
19
20 /**
21  * Action that will revert a contiguous block of added, deleted and changes lines in the currently
22  * displayed document to the state in the reference document.
23  *
24  * @since 3.0
25  */

26 public class RevertBlockAction extends QuickDiffRestoreAction {
27     /** Resource key prefix. */
28     private static final String JavaDoc PREFIX= "RevertBlockAction."; //$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 RevertBlockAction(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.getChangeType() == ILineDiffInfo.UNCHANGED)
58             return false;
59
60         boolean hasBlock= false;
61         if (fLine > 0) {
62             info= differ.getLineInfo(fLine - 1);
63             hasBlock= info != null && info.hasChanges();
64         }
65         if (!hasBlock) {
66             info= differ.getLineInfo(fLine + 1);
67             hasBlock= info != null && info.hasChanges();
68         }
69         if (hasBlock)
70             return true;
71
72         return false;
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.revertBlock(fLine);
85             } catch (BadLocationException e) {
86                 setStatus(e.getMessage());
87             }
88         }
89     }
90 }
91
Popular Tags