1 /*******************************************************************************
2 * Copyright (c) 2000, 2010 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.actions;
13 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IProjectNature;
20 import org.eclipse.core.resources.IResource;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
25 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
29 import org.eclipse.ui.editors.text.EditorsUI;
31 import org.eclipse.jdt.core.IJavaElement;
32 import org.eclipse.jdt.core.IJavaProject;
33 import org.eclipse.jdt.core.IPackageFragment;
34 import org.eclipse.jdt.core.IPackageFragmentRoot;
35 import org.eclipse.jdt.core.JavaCore;
37 import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
38 import org.eclipse.jdt.internal.corext.util.Messages;
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
41 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
44 * http://dev.eclipse.org/bugs/show_bug.cgi?id=19104
46 public class ActionUtil {
51 //bug 31998 we will have to disable renaming of linked packages (and cus)
52 public static boolean mustDisableJavaModelAction(Shell shell, Object element) {
53 if (!(element instanceof IPackageFragment) && !(element instanceof IPackageFragmentRoot))
56 IResource resource= ResourceUtil.getResource(element);
57 if ((resource == null) || (! (resource instanceof IFolder)) || (! resource.isLinked()))
60 MessageDialog.openInformation(shell, ActionMessages.ActionUtil_not_possible, ActionMessages.ActionUtil_no_linked);
64 public static boolean isProcessable(JavaEditor editor) {
67 Shell shell= editor.getSite().getShell();
68 IJavaElement input= SelectionConverter.getInput(editor);
69 // if a Java editor doesn't have an input of type Java element
70 // then it is for sure not on the build path
72 MessageDialog.openInformation(shell,
73 ActionMessages.ActionUtil_notOnBuildPath_title,
74 ActionMessages.ActionUtil_notOnBuildPath_message);
77 return isProcessable(shell, input);
80 public static boolean isProcessable(Shell shell, IJavaElement element) {
83 if (isOnBuildPath(element))
85 MessageDialog.openInformation(shell,
86 ActionMessages.ActionUtil_notOnBuildPath_title,
87 ActionMessages.ActionUtil_notOnBuildPath_message);
91 public static boolean isOnBuildPath(IJavaElement element) {
92 //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
93 if (element.getElementType() == IJavaElement.JAVA_PROJECT)
95 IJavaProject project= element.getJavaProject();
97 if (!project.isOnClasspath(element))
99 IProject resourceProject= project.getProject();
100 if (resourceProject == null)
102 IProjectNature nature= resourceProject.getNature(JavaCore.NATURE_ID);
103 // We have a Java project
106 } catch (CoreException e) {
111 public static boolean areProcessable(Shell shell, IJavaElement[] elements) {
112 for (int i= 0; i < elements.length; i++) {
113 if (! isOnBuildPath(elements[i])) {
114 MessageDialog.openInformation(shell,
115 ActionMessages.ActionUtil_notOnBuildPath_title,
116 Messages.format(ActionMessages.ActionUtil_notOnBuildPath_resource_message, new Object[] {BasicElementLabels.getPathLabel(elements[i].getPath(), false)}));
124 * Check whether <code>editor</code> and <code>element</code> are
125 * processable and editable. If the editor edits the element, the validation
126 * is only performed once. If necessary, ask the user whether the file(s)
129 * @param editor an editor, or <code>null</code> iff the action was not
130 * executed from an editor
131 * @param shell a shell to serve as parent for a dialog
132 * @param element the element to check, cannot be <code>null</code>
133 * @return <code>true</code> if the element can be edited,
134 * <code>false</code> otherwise
136 public static boolean isEditable(JavaEditor editor, Shell shell, IJavaElement element) {
137 if (editor != null) {
138 IJavaElement input= SelectionConverter.getInput(editor);
139 if (input != null && input.equals(element.getAncestor(IJavaElement.COMPILATION_UNIT)))
140 return isEditable(editor);
142 return isEditable(editor) && isEditable(shell, element);
144 return isEditable(shell, element);
147 public static boolean isEditable(JavaEditor editor) {
148 if (! isProcessable(editor))
151 return editor.validateEditorInputState();
154 public static boolean isEditable(Shell shell, IJavaElement element) {
155 if (! isProcessable(shell, element))
158 IJavaElement cu= element.getAncestor(IJavaElement.COMPILATION_UNIT);
160 IResource resource= cu.getResource();
161 if (resource != null && resource.isDerived(IResource.CHECK_ANCESTORS)) {
163 // see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#validateEditorInputState()
164 final String warnKey= AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WARN_IF_INPUT_DERIVED;
165 IPreferenceStore store= EditorsUI.getPreferenceStore();
166 if (!store.getBoolean(warnKey))
169 MessageDialogWithToggle toggleDialog= MessageDialogWithToggle.openYesNoQuestion(
171 ActionMessages.ActionUtil_warning_derived_title,
172 Messages.format(ActionMessages.ActionUtil_warning_derived_message, BasicElementLabels.getPathLabel(resource.getFullPath(), false)),
173 ActionMessages.ActionUtil_warning_derived_dontShowAgain,
178 EditorsUI.getPreferenceStore().setValue(warnKey, !toggleDialog.getToggleState());
180 return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;