1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import java.util.List;
19 import com.ibm.icu.text.MessageFormat;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.ISharedImages;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchSite;
35 import org.eclipse.ui.IWorkingSet;
36 import org.eclipse.ui.IWorkingSetManager;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.actions.DeleteResourceAction;
40 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
41 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
42 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
44 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
46 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
47 import org.eclipse.jdt.internal.ui.JavaPlugin;
48 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
49 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
50 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
51 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
52 import org.eclipse.jdt.internal.ui.workingsets.IWorkingSetIDs;
53 import org.eclipse.jdt.internal.ui.workingsets.WorkingSetModel;
56 public class DeleteAction extends SelectionDispatchAction {
59 * 'Hide' button index.
63 private static final int HIDE_BUTTON= 0;
66 * 'Remove' button index.
70 private static final int REMOVE_BUTTON= 1;
73 public DeleteAction(IWorkbenchSite site) {
75 setText(ReorgMessages.DeleteAction_3);
76 setDescription(ReorgMessages.DeleteAction_4);
77 ISharedImages workbenchImages= JavaPlugin.getDefault().getWorkbench().getSharedImages();
78 setDisabledImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
79 setImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
80 setHoverImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
82 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.DELETE_ACTION);
86 * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
89 public void selectionChanged(IStructuredSelection selection) {
90 if (ReorgUtils.containsOnlyProjects(selection.toList())) {
91 setEnabled(createWorkbenchAction(selection).isEnabled());
94 setEnabled(RefactoringAvailabilityTester.isDeleteAvailable(selection.toArray()));
97 private IAction createWorkbenchAction(IStructuredSelection selection) {
98 DeleteResourceAction action= new DeleteResourceAction(getSite());
99 action.selectionChanged(selection);
104 * Removes or hides the selected working sets.
106 * @param selection the selected working sets
109 private void deleteWorkingSets(IStructuredSelection selection) {
110 MessageDialog dialog;
111 if (selection.size() == 1) {
112 IWorkingSet workingSet= (IWorkingSet)selection.getFirstElement();
113 final String workingSetID= workingSet.getId();
114 String dialogMessage;
115 if (isDefaultWorkingSet(workingSetID))
116 dialogMessage= MessageFormat.format(ReorgMessages.DeleteWorkingSet_hideworkingset_single, new Object[] { workingSet.getLabel() });
118 dialogMessage= MessageFormat.format(ReorgMessages.DeleteWorkingSet_removeorhideworkingset_single, new Object[] { workingSet.getLabel() });
120 dialog= new MessageDialog(getShell(), ReorgMessages.DeleteWorkingSet_single, null, dialogMessage, MessageDialog.QUESTION, new String[] { ReorgMessages.DeleteWorkingSet_Hide,
121 ReorgMessages.DeleteWorkingSet_Remove,
122 IDialogConstants.CANCEL_LABEL }, 0) {
124 * @see org.eclipse.jface.dialogs.MessageDialog#createButton(org.eclipse.swt.widgets.Composite, int, java.lang.String, boolean)
128 protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
129 Button button= super.createButton(parent, id, label, defaultButton);
130 if (id == REMOVE_BUTTON && IWorkingSetIDs.OTHERS.equals(workingSetID))
131 button.setEnabled(false);
136 dialog= new MessageDialog(getShell(), ReorgMessages.DeleteWorkingSet_multiple, null, MessageFormat.format(ReorgMessages.DeleteWorkingSet_removeorhideworkingset_multiple,
137 new Object[] { new Integer(selection.size()) }),
138 MessageDialog.QUESTION, new String[] { ReorgMessages.DeleteWorkingSet_Hide, ReorgMessages.DeleteWorkingSet_Remove,
139 IDialogConstants.CANCEL_LABEL }, 0);
142 int dialogResponse= dialog.open();
143 if (dialogResponse == REMOVE_BUTTON) {
144 Iterator<?> iter= selection.iterator();
145 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
146 while (iter.hasNext()) {
147 IWorkingSet workingSet= (IWorkingSet) iter.next();
148 if (isDefaultWorkingSet(workingSet.getId()))
149 hideWorkingSets(Collections.singletonList(workingSet));
151 manager.removeWorkingSet(workingSet);
153 } else if (dialogResponse == HIDE_BUTTON) {
154 hideWorkingSets((List<IWorkingSet>) SelectionUtil.toList(selection));
159 * Hides all the working sets in the list from the Package Explorer.
161 * @param selection the selection of working sets
164 private void hideWorkingSets(List<IWorkingSet> selection) {
165 IWorkbenchPage page= JavaPlugin.getActivePage();
167 IWorkbenchPart activePart= page.getActivePart();
168 if (activePart instanceof PackageExplorerPart) {
169 PackageExplorerPart packagePart= (PackageExplorerPart) activePart;
170 WorkingSetModel model= packagePart.getWorkingSetModel();
171 List<IWorkingSet> activeWorkingSets= new ArrayList<IWorkingSet>(Arrays.asList(model.getActiveWorkingSets()));
172 activeWorkingSets.removeAll(selection);
173 model.setActiveWorkingSets(activeWorkingSets.toArray(new IWorkingSet[activeWorkingSets.size()]));
179 * Checks if the working set is the default working set.
181 * @param workingSetID the working set id, can be <code>null</code>
182 * @return <code>true</code> if default working set, <code>false</code> otherwise
185 private boolean isDefaultWorkingSet(String workingSetID) {
186 return IWorkingSetIDs.OTHERS.equals(workingSetID);
190 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
193 public void run(IStructuredSelection selection) {
194 if (ReorgUtils.containsOnlyProjects(selection.toList())) {
195 createWorkbenchAction(selection).run();
198 if (ReorgUtils.containsOnlyWorkingSets(selection.toList())){
199 deleteWorkingSets(selection);
203 RefactoringExecutionStarter.startDeleteRefactoring(selection.toArray(), getShell());
204 } catch (CoreException e) {
205 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);