Refactory
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / actions / ActionUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
12
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaProject;
15 import net.sourceforge.phpdt.core.IPackageFragment;
16 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
17 import net.sourceforge.phpdt.internal.corext.refactoring.util.ResourceUtil;
18 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
21 //import net.sourceforge.phpeclipse.ui.WebUI;
22
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IProjectNature;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.swt.widgets.Shell;
30
31 /*
32  * http://dev.eclipse.org/bugs/show_bug.cgi?id=19104
33  */
34 public class ActionUtil {
35
36         private ActionUtil() {
37         }
38
39         // bug 31998 we will have to disable renaming of linked packages (and cus)
40         public static boolean mustDisableJavaModelAction(Shell shell, Object element) {
41                 if (!(element instanceof IPackageFragment)
42                                 && !(element instanceof IPackageFragmentRoot))
43                         return false;
44
45                 IResource resource = ResourceUtil.getResource(element);
46                 if ((resource == null) || (!(resource instanceof IFolder))
47                                 || (!resource.isLinked()))
48                         return false;
49
50                 MessageDialog
51                                 .openInformation(
52                                                 shell,
53                                                 ActionMessages.getString("ActionUtil.not_possible"), ActionMessages.getString("ActionUtil.no_linked")); //$NON-NLS-1$ //$NON-NLS-2$
54                 return true;
55         }
56
57         public static boolean isProcessable(Shell shell, PHPEditor editor) {
58                 if (editor == null)
59                         return true;
60                 IJavaElement input = SelectionConverter.getInput(editor);
61                 // if a Java editor doesn't have an input of type Java element
62                 // then it is for sure not on the build path
63                 if (input == null) {
64                         MessageDialog.openInformation(shell, ActionMessages
65                                         .getString("ActionUtil.notOnBuildPath.title"), //$NON-NLS-1$
66                                         ActionMessages
67                                                         .getString("ActionUtil.notOnBuildPath.message")); //$NON-NLS-1$
68                         return false;
69                 }
70                 return isProcessable(shell, input);
71         }
72
73         public static boolean isProcessable(Shell shell, Object element) {
74                 if (!(element instanceof IJavaElement))
75                         return true;
76
77                 if (isOnBuildPath((IJavaElement) element))
78                         return true;
79                 MessageDialog.openInformation(shell, ActionMessages
80                                 .getString("ActionUtil.notOnBuildPath.title"), //$NON-NLS-1$
81                                 ActionMessages.getString("ActionUtil.notOnBuildPath.message")); //$NON-NLS-1$
82                 return false;
83         }
84
85         public static boolean isOnBuildPath(IJavaElement element) {
86                 // fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
87                 if (element.getElementType() == IJavaElement.JAVA_PROJECT)
88                         return true;
89                 IJavaProject project = element.getJavaProject();
90                 try {
91                         // if (!project.isOnClasspath(element))
92                         // return false;
93                         IProject resourceProject = project.getProject();
94                         if (resourceProject == null)
95                                 return false;
96                         IProjectNature nature = resourceProject
97                                         .getNature(/*WebUI*/PHPeclipsePlugin.PHP_NATURE_ID);
98                         // We have a Java project
99                         if (nature != null)
100                                 return true;
101                 } catch (CoreException e) {
102                 }
103                 return false;
104         }
105 }