KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > quickdiff > RevertLineAction


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.editors.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 line in the currently displayed document to the state in the
22  * reference document.
23  *
24  * @since 3.1
25  */

26 public class RevertLineAction extends QuickDiffRestoreAction {
27     /** Resource key prefix. */
28     private static final String JavaDoc PREFIX= "RevertLineAction."; //$NON-NLS-1$
29
/** Resource key for added lines - they will be deleted. */
30     private static final String JavaDoc DELETE_KEY= PREFIX + "delete.label"; //$NON-NLS-1$
31
/** Resource key for changed lines - they will be reverted. */
32     private static final String JavaDoc REVERT_KEY= PREFIX + "label"; //$NON-NLS-1$
33

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

43     public RevertLineAction(ITextEditor editor, boolean isRulerAction) {
44         super(QuickDiffMessages.getResourceBundle(), PREFIX, editor, isRulerAction);
45     }
46
47     /*
48      * @see org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction#computeEnablement()
49      */

50     public boolean computeEnablement() {
51         if (!super.computeEnablement())
52             return false;
53
54         fLine= getLastLine();
55         if (fLine == -1)
56             return false;
57         ILineDiffer differ= getDiffer();
58         if (differ == null)
59             return false;
60         ILineDiffInfo info= differ.getLineInfo(fLine);
61         if (info == null || info.getChangeType() == ILineDiffInfo.UNCHANGED)
62             return false;
63
64         if (info.getChangeType() == ILineDiffInfo.ADDED)
65             setText(QuickDiffMessages.getString(DELETE_KEY));
66         else
67             setText(QuickDiffMessages.getString(REVERT_KEY));
68         return true;
69     }
70
71     /*
72      * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
73      */

74     public void runCompoundChange() {
75         if (!isEnabled())
76             return;
77         ILineDiffer differ= getDiffer();
78         if (differ != null) {
79             try {
80                 differ.revertLine(fLine);
81             } catch (BadLocationException e) {
82                 setStatus(e.getMessage());
83             }
84         }
85     }
86 }
87
Popular Tags