avoid NullPointerException in debug
[phpeclipse.git] / net.sourceforge.phpeclipse / 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.phpeditor.PHPEditor;
20
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IProjectNature;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.swt.widgets.Shell;
28
29 /*
30  * http://dev.eclipse.org/bugs/show_bug.cgi?id=19104
31  */
32 public class ActionUtil {
33         
34         private ActionUtil(){
35         }
36
37         //bug 31998     we will have to disable renaming of linked packages (and cus)
38         public static boolean mustDisableJavaModelAction(Shell shell, Object element) {
39                 if (!(element instanceof IPackageFragment) && !(element instanceof IPackageFragmentRoot))
40                         return false;
41                 
42                 IResource resource= ResourceUtil.getResource(element);
43                 if ((resource == null) || (! (resource instanceof IFolder)) || (! resource.isLinked()))
44                         return false;
45                         
46                 MessageDialog.openInformation(shell, ActionMessages.getString("ActionUtil.not_possible"), ActionMessages.getString("ActionUtil.no_linked")); //$NON-NLS-1$ //$NON-NLS-2$
47                 return true;
48         }
49         
50         public static boolean isProcessable(Shell shell, PHPEditor editor) {
51                 if (editor == null)
52                         return true;
53                 IJavaElement input= SelectionConverter.getInput(editor);
54                 // if a Java editor doesn't have an input of type Java element
55                 // then it is for sure not on the build path
56                 if (input == null) {
57                         MessageDialog.openInformation(shell, 
58                                 ActionMessages.getString("ActionUtil.notOnBuildPath.title"),  //$NON-NLS-1$
59                                 ActionMessages.getString("ActionUtil.notOnBuildPath.message")); //$NON-NLS-1$
60                         return false;
61                 }
62                 return isProcessable(shell, input);
63         }
64         
65         public static boolean isProcessable(Shell shell, Object element) {
66                 if (!(element instanceof IJavaElement))
67                         return true;
68                         
69                 if (isOnBuildPath((IJavaElement)element))
70                         return true;
71                 MessageDialog.openInformation(shell, 
72                         ActionMessages.getString("ActionUtil.notOnBuildPath.title"),  //$NON-NLS-1$
73                         ActionMessages.getString("ActionUtil.notOnBuildPath.message")); //$NON-NLS-1$
74                 return false;
75         }
76
77         public static boolean isOnBuildPath(IJavaElement element) {     
78         //fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
79         if (element.getElementType() == IJavaElement.JAVA_PROJECT)
80             return true;
81                 IJavaProject project= element.getJavaProject();
82                 try {
83 //                      if (!project.isOnClasspath(element))
84 //                              return false;
85                         IProject resourceProject= project.getProject();
86                         if (resourceProject == null)
87                                 return false;
88                         IProjectNature nature= resourceProject.getNature(PHPeclipsePlugin.PHP_NATURE_ID);
89                         // We have a Java project
90                         if (nature != null)
91                                 return true;
92                 } catch (CoreException e) {
93                 }
94                 return false;
95         }
96 }
97