001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import org.eclipse.core.runtime.CoreException;
013:
014: import org.eclipse.core.resources.IFolder;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IProjectNature;
017: import org.eclipse.core.resources.IResource;
018:
019: import org.eclipse.swt.widgets.Shell;
020:
021: import org.eclipse.jface.dialogs.IDialogConstants;
022: import org.eclipse.jface.dialogs.MessageDialog;
023: import org.eclipse.jface.dialogs.MessageDialogWithToggle;
024: import org.eclipse.jface.preference.IPreferenceStore;
025:
026: import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
027:
028: import org.eclipse.ui.editors.text.EditorsUI;
029:
030: import org.eclipse.jdt.core.IJavaElement;
031: import org.eclipse.jdt.core.IJavaProject;
032: import org.eclipse.jdt.core.IPackageFragment;
033: import org.eclipse.jdt.core.IPackageFragmentRoot;
034: import org.eclipse.jdt.core.JavaCore;
035:
036: import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
037: import org.eclipse.jdt.internal.corext.util.Messages;
038:
039: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
040:
041: /*
042: * http://dev.eclipse.org/bugs/show_bug.cgi?id=19104
043: */
044: public class ActionUtil {
045:
046: private ActionUtil() {
047: }
048:
049: //bug 31998 we will have to disable renaming of linked packages (and cus)
050: public static boolean mustDisableJavaModelAction(Shell shell,
051: Object element) {
052: if (!(element instanceof IPackageFragment)
053: && !(element instanceof IPackageFragmentRoot))
054: return false;
055:
056: IResource resource = ResourceUtil.getResource(element);
057: if ((resource == null) || (!(resource instanceof IFolder))
058: || (!resource.isLinked()))
059: return false;
060:
061: MessageDialog.openInformation(shell,
062: ActionMessages.ActionUtil_not_possible,
063: ActionMessages.ActionUtil_no_linked);
064: return true;
065: }
066:
067: public static boolean isProcessable(JavaEditor editor) {
068: if (editor == null)
069: return true;
070: Shell shell = editor.getSite().getShell();
071: IJavaElement input = SelectionConverter.getInput(editor);
072: // if a Java editor doesn't have an input of type Java element
073: // then it is for sure not on the build path
074: if (input == null) {
075: MessageDialog.openInformation(shell,
076: ActionMessages.ActionUtil_notOnBuildPath_title,
077: ActionMessages.ActionUtil_notOnBuildPath_message);
078: return false;
079: }
080: return isProcessable(shell, input);
081: }
082:
083: public static boolean isProcessable(Shell shell,
084: IJavaElement element) {
085: if (element == null)
086: return true;
087: if (isOnBuildPath(element))
088: return true;
089: MessageDialog.openInformation(shell,
090: ActionMessages.ActionUtil_notOnBuildPath_title,
091: ActionMessages.ActionUtil_notOnBuildPath_message);
092: return false;
093: }
094:
095: public static boolean isOnBuildPath(IJavaElement element) {
096: //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
097: if (element.getElementType() == IJavaElement.JAVA_PROJECT)
098: return true;
099: IJavaProject project = element.getJavaProject();
100: try {
101: if (!project.isOnClasspath(element))
102: return false;
103: IProject resourceProject = project.getProject();
104: if (resourceProject == null)
105: return false;
106: IProjectNature nature = resourceProject
107: .getNature(JavaCore.NATURE_ID);
108: // We have a Java project
109: if (nature != null)
110: return true;
111: } catch (CoreException e) {
112: }
113: return false;
114: }
115:
116: public static boolean areProcessable(Shell shell,
117: IJavaElement[] elements) {
118: for (int i = 0; i < elements.length; i++) {
119: if (!isOnBuildPath(elements[i])) {
120: MessageDialog
121: .openInformation(
122: shell,
123: ActionMessages.ActionUtil_notOnBuildPath_title,
124: Messages
125: .format(
126: ActionMessages.ActionUtil_notOnBuildPath_resource_message,
127: new Object[] { elements[i]
128: .getPath() }));
129: return false;
130: }
131: }
132: return true;
133: }
134:
135: /**
136: * Check whether <code>editor</code> and <code>element</code> are
137: * processable and editable. If the editor edits the element, the validation
138: * is only performed once. If necessary, ask the user whether the file(s)
139: * should be edited.
140: *
141: * @param editor an editor, or <code>null</code> iff the action was not
142: * executed from an editor
143: * @param shell a shell to serve as parent for a dialog
144: * @param element the element to check, cannot be <code>null</code>
145: * @return <code>true</code> if the element can be edited,
146: * <code>false</code> otherwise
147: */
148: public static boolean isEditable(JavaEditor editor, Shell shell,
149: IJavaElement element) {
150: if (editor != null) {
151: IJavaElement input = SelectionConverter.getInput(editor);
152: if (input != null
153: && input
154: .equals(element
155: .getAncestor(IJavaElement.COMPILATION_UNIT)))
156: return isEditable(editor);
157: else
158: return isEditable(editor) && isEditable(shell, element);
159: }
160: return isEditable(shell, element);
161: }
162:
163: public static boolean isEditable(JavaEditor editor) {
164: if (!isProcessable(editor))
165: return false;
166:
167: return editor.validateEditorInputState();
168: }
169:
170: public static boolean isEditable(Shell shell, IJavaElement element) {
171: if (!isProcessable(shell, element))
172: return false;
173:
174: IJavaElement cu = element
175: .getAncestor(IJavaElement.COMPILATION_UNIT);
176: if (cu != null) {
177: IResource resource = cu.getResource();
178: if (resource != null && resource.isDerived()) {
179:
180: // see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#validateEditorInputState()
181: final String warnKey = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WARN_IF_INPUT_DERIVED;
182: IPreferenceStore store = EditorsUI.getPreferenceStore();
183: if (!store.getBoolean(warnKey))
184: return true;
185:
186: MessageDialogWithToggle toggleDialog = MessageDialogWithToggle
187: .openYesNoQuestion(
188: shell,
189: ActionMessages.ActionUtil_warning_derived_title,
190: Messages
191: .format(
192: ActionMessages.ActionUtil_warning_derived_message,
193: resource.getFullPath()
194: .toString()),
195: ActionMessages.ActionUtil_warning_derived_dontShowAgain,
196: false, null, null);
197:
198: EditorsUI.getPreferenceStore().setValue(warnKey,
199: !toggleDialog.getToggleState());
200:
201: return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;
202: }
203: }
204: return true;
205: }
206:
207: }
|