KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > MergeAllActionHandler


1 /*******************************************************************************
2  * Copyright (c) 2006 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.team.internal.ui.mapping;
12
13 import org.eclipse.core.commands.ExecutionEvent;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.mapping.ResourceMapping;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.diff.*;
22 import org.eclipse.team.core.mapping.*;
23 import org.eclipse.team.internal.ui.*;
24 import org.eclipse.team.internal.ui.synchronize.SynchronizeView;
25 import org.eclipse.team.ui.mapping.*;
26 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
27 import org.eclipse.ui.ide.IDE;
28
29 public class MergeAllActionHandler extends MergeActionHandler implements IDiffChangeListener {
30
31     private MergeAllOperation operation;
32
33     public MergeAllActionHandler(ISynchronizePageConfiguration configuration) {
34         super(configuration);
35         getContext().getDiffTree().addDiffChangeListener(this);
36     }
37
38     /* (non-Javadoc)
39      * @see org.eclipse.team.ui.mapping.MergeActionHandler#getOperation()
40      */

41     protected synchronized SynchronizationOperation getOperation() {
42         if (operation == null) {
43             operation = createOperation();
44         }
45         return operation;
46     }
47
48     protected MergeAllOperation createOperation() {
49         return new MergeAllOperation(getJobName(), getConfiguration(), getMappings(), getContext());
50     }
51
52     private IMergeContext getContext() {
53         return ((IMergeContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT));
54     }
55
56     private ResourceMapping[] getMappings() {
57         return ((ISynchronizationScope)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_SCOPE)).getMappings();
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.team.core.diff.IDiffChangeListener#diffsChanged(org.eclipse.team.core.diff.IDiffChangeEvent, org.eclipse.core.runtime.IProgressMonitor)
62      */

63     public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
64         synchronized (this) {
65             operation = null;
66         }
67         setEnabled(event.getTree().countFor(IThreeWayDiff.INCOMING, IThreeWayDiff.DIRECTION_MASK) > 0
68                 || event.getTree().countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK) > 0);
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.team.core.diff.IDiffChangeListener#propertyChanged(org.eclipse.team.core.diff.IDiffTree, int, org.eclipse.core.runtime.IPath[])
73      */

74     public void propertyChanged(IDiffTree tree, int property, IPath[] paths) {
75         // Nothing to do
76
}
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.team.ui.mapping.MergeActionHandler#dispose()
80      */

81     public void dispose() {
82         getContext().getDiffTree().removeDiffChangeListener(this);
83         super.dispose();
84     }
85
86     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
87         if (saveDirtyEditors() && promptToUpdate())
88             return super.execute(event);
89         return null;
90     }
91     
92     /**
93      * Prompt to save all dirty editors and return whether to proceed
94      * or not.
95      * @return whether to proceed
96      * or not
97      */

98     public final boolean saveDirtyEditors() {
99         if(needsToSaveDirtyEditors()) {
100             if(!saveAllEditors(getTargetResources(), confirmSaveOfDirtyEditor())) {
101                 return false;
102             }
103         }
104         return true;
105     }
106     
107     private IResource[] getTargetResources() {
108         return getContext().getDiffTree().getAffectedResources();
109     }
110
111     /**
112      * Save all dirty editors in the workbench that are open on files that may
113      * be affected by this operation. Opens a dialog to prompt the user if
114      * <code>confirm</code> is true. Return true if successful. Return false
115      * if the user has canceled the command. Must be called from the UI thread.
116      * @param resources the root resources being operated on
117      * @param confirm prompt the user if true
118      * @return boolean false if the operation was canceled.
119      */

120     public final boolean saveAllEditors(IResource[] resources, boolean confirm) {
121         return IDE.saveAllEditors(resources, confirm);
122     }
123
124     /**
125      * Return whether dirty editor should be saved before this action is run.
126      * Default is <code>true</code>.
127      *
128      * @return whether dirty editor should be saved before this action is run
129      */

130     protected boolean needsToSaveDirtyEditors() {
131         return true;
132     }
133     
134     /**
135      * Returns whether the user should be prompted to save dirty editors. The
136      * default is <code>true</code>.
137      *
138      * @return whether the user should be prompted to save dirty editors
139      */

140     protected boolean confirmSaveOfDirtyEditor() {
141         return true;
142     }
143     
144     protected String JavaDoc getJobName() {
145         String JavaDoc name = getConfiguration().getParticipant().getName();
146         return NLS.bind(TeamUIMessages.MergeAllActionHandler_0, Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, name));
147     }
148     
149     protected boolean promptToUpdate() {
150         final IResourceDiffTree tree = getContext().getDiffTree();
151         if (tree.isEmpty()) {
152             return false;
153         }
154         final long count = tree.countFor(IThreeWayDiff.INCOMING, IThreeWayDiff.DIRECTION_MASK) + tree.countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK);
155         if (count == 0)
156             return false;
157         final boolean[] result = new boolean[] {true};
158         TeamUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
159             public void run() {
160                 String JavaDoc sizeString = Long.toString(count);
161                 String JavaDoc message = tree.size() > 1 ? NLS.bind(TeamUIMessages.MergeAllActionHandler_1, new String JavaDoc[] { sizeString }) :
162                     NLS.bind(TeamUIMessages.MergeAllActionHandler_2, new String JavaDoc[] { sizeString });
163                 result[0] = MessageDialog.openQuestion(getConfiguration().getSite().getShell(),
164                         NLS.bind(TeamUIMessages.MergeAllActionHandler_3, new String JavaDoc[] { sizeString }), message);
165             }
166         });
167         return result[0];
168     }
169 }
170
Popular Tags