KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > CVSModelProviderAction


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.ccvs.ui.mappings;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.mapping.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.team.internal.ccvs.ui.Policy;
20 import org.eclipse.team.internal.ui.TeamUIPlugin;
21 import org.eclipse.team.internal.ui.Utils;
22 import org.eclipse.team.internal.ui.mapping.ResourceModelParticipantAction;
23 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
24 import org.eclipse.ui.ide.IDE;
25
26 public abstract class CVSModelProviderAction extends ResourceModelParticipantAction {
27
28     public CVSModelProviderAction(ISynchronizePageConfiguration configuration) {
29         super(null, configuration);
30         Utils.initAction(this, getBundleKeyPrefix(), Policy.getActionBundle());
31     }
32     
33     /**
34      * Return the key to the action text in the resource bundle.
35      * The default is the fully qualified class name followed by a dot (.).
36      * @return the bundle key prefix
37      */

38     protected String JavaDoc getBundleKeyPrefix() {
39         return getClass().getName() + "."; //$NON-NLS-1$
40
}
41
42     protected ResourceMapping[] getResourceMappings(IStructuredSelection selection) {
43         List mappings = new ArrayList();
44         for (Iterator iter = selection.iterator(); iter.hasNext();) {
45             Object JavaDoc element = (Object JavaDoc) iter.next();
46             ResourceMapping mapping = Utils.getResourceMapping(element);
47             if (mapping != null)
48                 mappings.add(mapping);
49         }
50         return (ResourceMapping[]) mappings.toArray(new ResourceMapping[mappings.size()]);
51     }
52     
53     /**
54      * Prompt to save all dirty editors and return whether to proceed
55      * or not.
56      * @return whether to proceed
57      * or not
58      */

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

103     public final boolean saveAllEditors(IResource[] resources, boolean confirm) {
104         return IDE.saveAllEditors(resources, confirm);
105     }
106
107     /**
108      * Return whether dirty editor should be saved before this action is run.
109      * Default is <code>true</code>.
110      *
111      * @return whether dirty editor should be saved before this action is run
112      */

113     protected boolean needsToSaveDirtyEditors() {
114         return true;
115     }
116     
117     /**
118      * Returns whether the user should be prompted to save dirty editors. The
119      * default is <code>true</code>.
120      *
121      * @return whether the user should be prompted to save dirty editors
122      */

123     protected boolean confirmSaveOfDirtyEditor() {
124         return true;
125     }
126     
127     public void run() {
128         if (saveDirtyEditors())
129             execute();
130     }
131
132     protected abstract void execute();
133 }
134
Popular Tags