KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > ActionUtil


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.jdt.internal.ui.actions;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.core.resources.IFolder;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectNature;
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.swt.widgets.Shell;
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
42 /*
43  * http://dev.eclipse.org/bugs/show_bug.cgi?id=19104
44  */

45 public class ActionUtil {
46     
47     private ActionUtil(){
48     }
49
50     //bug 31998 we will have to disable renaming of linked packages (and cus)
51
public static boolean mustDisableJavaModelAction(Shell shell, Object JavaDoc element) {
52         if (!(element instanceof IPackageFragment) && !(element instanceof IPackageFragmentRoot))
53             return false;
54         
55         IResource resource= ResourceUtil.getResource(element);
56         if ((resource == null) || (! (resource instanceof IFolder)) || (! resource.isLinked()))
57             return false;
58             
59         MessageDialog.openInformation(shell, ActionMessages.ActionUtil_not_possible, ActionMessages.ActionUtil_no_linked);
60         return true;
61     }
62     
63     public static boolean isProcessable(JavaEditor editor) {
64         if (editor == null)
65             return true;
66         Shell shell= editor.getSite().getShell();
67         IJavaElement input= SelectionConverter.getInput(editor);
68         // if a Java editor doesn't have an input of type Java element
69
// then it is for sure not on the build path
70
if (input == null) {
71             MessageDialog.openInformation(shell,
72                 ActionMessages.ActionUtil_notOnBuildPath_title,
73                 ActionMessages.ActionUtil_notOnBuildPath_message);
74             return false;
75         }
76         return isProcessable(shell, input);
77     }
78     
79     public static boolean isProcessable(Shell shell, IJavaElement element) {
80         if (element == null)
81             return true;
82         if (isOnBuildPath(element))
83             return true;
84         MessageDialog.openInformation(shell,
85             ActionMessages.ActionUtil_notOnBuildPath_title,
86             ActionMessages.ActionUtil_notOnBuildPath_message);
87         return false;
88     }
89
90     public static boolean isOnBuildPath(IJavaElement element) {
91         //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
92
if (element.getElementType() == IJavaElement.JAVA_PROJECT)
93             return true;
94         IJavaProject project= element.getJavaProject();
95         try {
96             if (!project.isOnClasspath(element))
97                 return false;
98             IProject resourceProject= project.getProject();
99             if (resourceProject == null)
100                 return false;
101             IProjectNature nature= resourceProject.getNature(JavaCore.NATURE_ID);
102             // We have a Java project
103
if (nature != null)
104                 return true;
105         } catch (CoreException e) {
106         }
107         return false;
108     }
109
110     public static boolean areProcessable(Shell shell, IJavaElement[] elements) {
111         for (int i= 0; i < elements.length; i++) {
112             if (! isOnBuildPath(elements[i])) {
113                 MessageDialog.openInformation(shell,
114                         ActionMessages.ActionUtil_notOnBuildPath_title,
115                         Messages.format(ActionMessages.ActionUtil_notOnBuildPath_resource_message, new Object JavaDoc[] {elements[i].getPath()}));
116                 return false;
117             }
118         }
119         return true;
120     }
121     
122     /**
123      * Check whether <code>editor</code> and <code>element</code> are
124      * processable and editable. If the editor edits the element, the validation
125      * is only performed once. If necessary, ask the user whether the file(s)
126      * should be edited.
127      *
128      * @param editor an editor, or <code>null</code> iff the action was not
129      * executed from an editor
130      * @param shell a shell to serve as parent for a dialog
131      * @param element the element to check, cannot be <code>null</code>
132      * @return <code>true</code> if the element can be edited,
133      * <code>false</code> otherwise
134      */

135     public static boolean isEditable(JavaEditor editor, Shell shell, IJavaElement element) {
136         if (editor != null) {
137             IJavaElement input= SelectionConverter.getInput(editor);
138             if (input != null && input.equals(element.getAncestor(IJavaElement.COMPILATION_UNIT)))
139                 return isEditable(editor);
140             else
141                 return isEditable(editor) && isEditable(shell, element);
142         }
143         return isEditable(shell, element);
144     }
145     
146     public static boolean isEditable(JavaEditor editor) {
147         if (! isProcessable(editor))
148             return false;
149         
150         return editor.validateEditorInputState();
151     }
152             
153     public static boolean isEditable(Shell shell, IJavaElement element) {
154         if (! isProcessable(shell, element))
155             return false;
156         
157         IJavaElement cu= element.getAncestor(IJavaElement.COMPILATION_UNIT);
158         if (cu != null) {
159             IResource resource= cu.getResource();
160             if (resource != null && resource.isDerived()) {
161                 
162                 // see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#validateEditorInputState()
163
final String JavaDoc warnKey= AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WARN_IF_INPUT_DERIVED;
164                 IPreferenceStore store= EditorsUI.getPreferenceStore();
165                 if (!store.getBoolean(warnKey))
166                     return true;
167                 
168                 MessageDialogWithToggle toggleDialog= MessageDialogWithToggle.openYesNoQuestion(
169                         shell,
170                         ActionMessages.ActionUtil_warning_derived_title,
171                         Messages.format(ActionMessages.ActionUtil_warning_derived_message, resource.getFullPath().toString()),
172                         ActionMessages.ActionUtil_warning_derived_dontShowAgain,
173                         false,
174                         null,
175                         null);
176                 
177                 EditorsUI.getPreferenceStore().setValue(warnKey, !toggleDialog.getToggleState());
178                 
179                 return toggleDialog.getReturnCode() == IDialogConstants.YES_ID;
180             }
181         }
182         return true;
183     }
184
185 }
186
187
Popular Tags