KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredViewer;
23 import org.eclipse.team.core.diff.*;
24 import org.eclipse.team.core.mapping.ISynchronizationContext;
25 import org.eclipse.team.internal.ui.TeamUIPlugin;
26 import org.eclipse.team.internal.ui.Utils;
27 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
28 import org.eclipse.team.ui.mapping.MergeActionHandler;
29 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
30 import org.eclipse.ui.ide.IDE;
31
32 public abstract class ResourceMergeActionHandler extends MergeActionHandler implements IDiffChangeListener, IPropertyChangeListener {
33
34     public ResourceMergeActionHandler(ISynchronizePageConfiguration configuration) {
35         super(configuration);
36         getSynchronizationContext().getDiffTree().addDiffChangeListener(this);
37         configuration.addPropertyChangeListener(this);
38     }
39
40     /**
41      * Prompt to save all dirty editors and return whether to proceed
42      * or not.
43      * @return whether to proceed
44      * or not
45      */

46     public final boolean saveDirtyEditors() {
47         if(needsToSaveDirtyEditors()) {
48             if(!saveAllEditors(getTargetResources(), confirmSaveOfDirtyEditor())) {
49                 return false;
50             }
51         }
52         return true;
53     }
54     
55     private IResource[] getTargetResources() {
56         IStructuredSelection selection = getStructuredSelection();
57         Object JavaDoc[] objects = selection.toArray();
58         Set JavaDoc roots = new HashSet JavaDoc();
59         for (int i = 0; i < objects.length; i++) {
60             Object JavaDoc object = objects[i];
61             ResourceMapping mapping = Utils.getResourceMapping(object);
62             if (mapping != null) {
63                 try {
64                     ResourceTraversal[] traversals = mapping.getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null);
65                     for (int j = 0; j < traversals.length; j++) {
66                         ResourceTraversal traversal = traversals[j];
67                         IResource[] resources = traversal.getResources();
68                         for (int k = 0; k < resources.length; k++) {
69                             IResource resource = resources[k];
70                             roots.add(resource);
71                         }
72                     }
73                 } catch (CoreException e) {
74                     TeamUIPlugin.log(e);
75                 }
76             }
77         }
78         return (IResource[]) roots.toArray(new IResource[roots.size()]);
79     }
80
81     /**
82      * Save all dirty editors in the workbench that are open on files that may
83      * be affected by this operation. Opens a dialog to prompt the user if
84      * <code>confirm</code> is true. Return true if successful. Return false
85      * if the user has canceled the command. Must be called from the UI thread.
86      * @param resources the root resources being operated on
87      * @param confirm prompt the user if true
88      * @return boolean false if the operation was canceled.
89      */

90     public final boolean saveAllEditors(IResource[] resources, boolean confirm) {
91         return IDE.saveAllEditors(resources, confirm);
92     }
93
94     /**
95      * Return whether dirty editor should be saved before this action is run.
96      * Default is <code>true</code>.
97      *
98      * @return whether dirty editor should be saved before this action is run
99      */

100     protected boolean needsToSaveDirtyEditors() {
101         return true;
102     }
103     
104     /**
105      * Returns whether the user should be prompted to save dirty editors. The
106      * default is <code>true</code>.
107      *
108      * @return whether the user should be prompted to save dirty editors
109      */

110     protected boolean confirmSaveOfDirtyEditor() {
111         return true;
112     }
113     
114     protected ISynchronizationContext getSynchronizationContext() {
115         return (ISynchronizationContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT);
116     }
117     
118     public void propertyChanged(IDiffTree tree, int property, IPath[] paths) {
119         // Nothing to do
120
}
121     
122     public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
123         Utils.syncExec(new Runnable JavaDoc() {
124             public void run() {
125                 updateEnablement(getStructuredSelection());
126             }
127         }, (StructuredViewer)getConfiguration().getPage().getViewer());
128         
129     }
130     
131     public void propertyChange(PropertyChangeEvent event) {
132         if (event.getProperty() == ISynchronizePageConfiguration.P_MODE) {
133             Utils.syncExec(new Runnable JavaDoc() {
134                 public void run() {
135                     updateEnablement(getStructuredSelection());
136                 }
137             }, (StructuredViewer)getConfiguration().getPage().getViewer());
138         }
139     }
140
141 }
142
Popular Tags