]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/actions/ActionUtil.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / actions / ActionUtil.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.core.runtime.CoreException;
16
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;
21
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;
26
27 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
28
29 import org.eclipse.ui.editors.text.EditorsUI;
30
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;
36
37 import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
38 import org.eclipse.jdt.internal.corext.util.Messages;
39
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
41 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
42
43 /*
44  * http://dev.eclipse.org/bugs/show_bug.cgi?id=19104
45  */
46 public class ActionUtil {
47
48         private ActionUtil(){
49         }
50
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))
54                         return false;
55
56                 IResource resource= ResourceUtil.getResource(element);
57                 if ((resource == null) || (! (resource instanceof IFolder)) || (! resource.isLinked()))
58                         return false;
59
60                 MessageDialog.openInformation(shell, ActionMessages.ActionUtil_not_possible, ActionMessages.ActionUtil_no_linked);
61                 return true;
62         }
63
64         public static boolean isProcessable(JavaEditor editor) {
65                 if (editor == null)
66                         return true;
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
71                 if (input == null) {
72                         MessageDialog.openInformation(shell,
73                                 ActionMessages.ActionUtil_notOnBuildPath_title,
74                                 ActionMessages.ActionUtil_notOnBuildPath_message);
75                         return false;
76                 }
77                 return isProcessable(shell, input);
78         }
79
80         public static boolean isProcessable(Shell shell, IJavaElement element) {
81                 if (element == null)
82                         return true;
83                 if (isOnBuildPath(element))
84                         return true;
85                 MessageDialog.openInformation(shell,
86                         ActionMessages.ActionUtil_notOnBuildPath_title,
87                         ActionMessages.ActionUtil_notOnBuildPath_message);
88                 return false;
89         }
90
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)
94             return true;
95                 IJavaProject project= element.getJavaProject();
96                 try {
97                         if (!project.isOnClasspath(element))
98                                 return false;
99                         IProject resourceProject= project.getProject();
100                         if (resourceProject == null)
101                                 return false;
102                         IProjectNature nature= resourceProject.getNature(JavaCore.NATURE_ID);
103                         // We have a Java project
104                         if (nature != null)
105                                 return true;
106                 } catch (CoreException e) {
107                 }
108                 return false;
109         }
110
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)}));
117                                 return false;
118                         }
119                 }
120                 return true;
121         }
122
123         /**
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)
127          * should be edited.
128          *
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
135          */
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);
141                         else
142                                 return isEditable(editor) && isEditable(shell, element);
143                 }
144                 return isEditable(shell, element);
145         }
146
147         public static boolean isEditable(JavaEditor editor) {
148                 if (! isProcessable(editor))
149                         return false;
150
151                 return editor.validateEditorInputState();
152         }
153
154         public static boolean isEditable(Shell shell, IJavaElement element) {
155                 if (! isProcessable(shell, element))
156                         return false;
157
158                 IJavaElement cu= element.getAncestor(IJavaElement.COMPILATION_UNIT);
159                 if (cu != null) {
160                         IResource resource= cu.getResource();
161                         if (resource != null && resource.isDerived(IResource.CHECK_ANCESTORS)) {
162
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))
167                                         return true;
168
169                                 MessageDialogWithToggle toggleDialog= MessageDialogWithToggle.openYesNoQuestion(
170                                                 shell,
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,
174                                                 false,
175                                                 null,
176                                                 null);
177
178                                 EditorsUI.getPreferenceStore().setValue(warnKey, !toggleDialog.getToggleState());
179
180                                 return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;
181                         }
182                 }
183                 return true;
184         }
185
186 }
187