1 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 45 public class ActionUtil { 46 47 private ActionUtil(){ 48 } 49 50 public static boolean mustDisableJavaModelAction(Shell shell, Object 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 (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 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 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 [] {elements[i].getPath()})); 116 return false; 117 } 118 } 119 return true; 120 } 121 122 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 final String 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 |