1. Suppress ArrayIndexOutOfBoundsException occurring in a certain case of formatting...
[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)
40                                 && !(element instanceof IPackageFragmentRoot))
41                         return false;
42
43                 IResource resource = ResourceUtil.getResource(element);
44                 if ((resource == null) || (!(resource instanceof IFolder))
45                                 || (!resource.isLinked()))
46                         return false;
47
48                 MessageDialog
49                                 .openInformation(
50                                                 shell,
51                                                 ActionMessages.getString("ActionUtil.not_possible"), ActionMessages.getString("ActionUtil.no_linked")); //$NON-NLS-1$ //$NON-NLS-2$
52                 return true;
53         }
54
55         public static boolean isProcessable(Shell shell, PHPEditor editor) {
56                 if (editor == null)
57                         return true;
58                 IJavaElement input = SelectionConverter.getInput(editor);
59                 // if a Java editor doesn't have an input of type Java element
60                 // then it is for sure not on the build path
61                 if (input == null) {
62                         MessageDialog.openInformation(shell, ActionMessages
63                                         .getString("ActionUtil.notOnBuildPath.title"), //$NON-NLS-1$
64                                         ActionMessages
65                                                         .getString("ActionUtil.notOnBuildPath.message")); //$NON-NLS-1$
66                         return false;
67                 }
68                 return isProcessable(shell, input);
69         }
70
71         public static boolean isProcessable(Shell shell, Object element) {
72                 if (!(element instanceof IJavaElement))
73                         return true;
74
75                 if (isOnBuildPath((IJavaElement) element))
76                         return true;
77                 MessageDialog.openInformation(shell, ActionMessages
78                                 .getString("ActionUtil.notOnBuildPath.title"), //$NON-NLS-1$
79                                 ActionMessages.getString("ActionUtil.notOnBuildPath.message")); //$NON-NLS-1$
80                 return false;
81         }
82
83         public static boolean isOnBuildPath(IJavaElement element) {
84                 // fix for bug http://dev.eclipse.org/bugs/show_bug.cgi?id=20051
85                 if (element.getElementType() == IJavaElement.JAVA_PROJECT)
86                         return true;
87                 IJavaProject project = element.getJavaProject();
88                 try {
89                         // if (!project.isOnClasspath(element))
90                         // return false;
91                         IProject resourceProject = project.getProject();
92                         if (resourceProject == null)
93                                 return false;
94                         IProjectNature nature = resourceProject
95                                         .getNature(PHPeclipsePlugin.PHP_NATURE_ID);
96                         // We have a Java project
97                         if (nature != null)
98                                 return true;
99                 } catch (CoreException e) {
100                 }
101                 return false;
102         }
103 }