KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.*;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.ResourceTraversal;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.team.core.diff.IDiff;
26 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
27 import org.eclipse.team.internal.ui.*;
28 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
29 import org.eclipse.ui.PlatformUI;
30
31 /**
32  * Remove the selected elements from the page
33  */

34 public class RemoveFromViewAction extends ResourceModelParticipantAction {
35
36     public RemoveFromViewAction(ISynchronizePageConfiguration configuration) {
37         super(null, configuration);
38         Utils.initAction(this, "action.removeFromView."); //$NON-NLS-1$
39
}
40
41     /* (non-Javadoc)
42      * @see org.eclipse.jface.action.Action#run()
43      */

44     public void run() {
45         if (confirmRemove()) {
46             try {
47                 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
48                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
49                             InterruptedException JavaDoc {
50                         try {
51                             performRemove(monitor);
52                         } catch (CoreException e) {
53                             throw new InvocationTargetException JavaDoc(e);
54                         }
55                     }
56                 });
57             } catch (InvocationTargetException JavaDoc e) {
58                 Utils.handle(e);
59             } catch (InterruptedException JavaDoc e) {
60                 // Ignore
61
}
62         }
63     }
64
65     private void performRemove(IProgressMonitor monitor) throws CoreException {
66         IResource[] resources = getVisibleResources(monitor);
67         if (resources.length == 0)
68             return;
69         ResourceDiffTree tree = (ResourceDiffTree)getSynchronizationContext().getDiffTree();
70         try {
71             tree.beginInput();
72             for (int i = 0; i < resources.length; i++) {
73                 IResource resource = resources[i];
74                 tree.remove(resource);
75             }
76         } finally {
77             tree.endInput(monitor);
78         }
79     }
80     
81     private IResource[] getVisibleResources(IProgressMonitor monitor) throws CoreException {
82         ResourceTraversal[] traversals = getResourceTraversals(getStructuredSelection(), monitor);
83         IDiff[] diffs = getSynchronizationContext().getDiffTree().getDiffs(traversals);
84         List result = new ArrayList();
85         for (int i = 0; i < diffs.length; i++) {
86             IDiff diff = diffs[i];
87             if (isVisible(diff)) {
88                 result.add(ResourceDiffTree.getResourceFor(diff));
89             }
90         }
91         return (IResource[]) result.toArray(new IResource[result.size()]);
92     }
93     
94     private boolean confirmRemove() {
95         IPreferenceStore store = TeamUIPlugin.getPlugin().getPreferenceStore();
96         if (store.getBoolean(IPreferenceIds.SYNCVIEW_REMOVE_FROM_VIEW_NO_PROMPT)) {
97             return true;
98         } else {
99             MessageDialogWithToggle dialog = MessageDialogWithToggle.openOkCancelConfirm(
100                     getConfiguration().getSite().getShell(),
101                     TeamUIMessages.RemoveFromView_warningTitle,
102                     TeamUIMessages.RemoveFromView_warningMessage,
103                     TeamUIMessages.RemoveFromView_warningDontShow,
104                     false,
105                     null,
106                     null);
107             store.setValue(IPreferenceIds.SYNCVIEW_REMOVE_FROM_VIEW_NO_PROMPT, dialog.getToggleState());
108             return dialog.getReturnCode() == Window.OK;
109         }
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.team.ui.synchronize.ModelParticipantAction#isEnabledForSelection(org.eclipse.jface.viewers.IStructuredSelection)
114      */

115     protected boolean isEnabledForSelection(IStructuredSelection selection) {
116         // Only enable if the selected elements adapt to IResource
117
if (selection.isEmpty())
118             return false;
119         for (Iterator iter = selection.iterator(); iter.hasNext();) {
120             Object JavaDoc element = iter.next();
121             if (Utils.getResource(element) == null) {
122                 return false;
123             }
124         }
125         return true;
126     }
127 }
128
Popular Tags