1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.CoreException; 17 18 import org.eclipse.swt.widgets.Shell; 19 20 import org.eclipse.jface.dialogs.IDialogConstants; 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 import org.eclipse.jface.window.Window; 24 25 import org.eclipse.jface.text.ITextSelection; 26 import org.eclipse.jface.text.source.IAnnotationModel; 27 28 import org.eclipse.ui.IEditorPart; 29 import org.eclipse.ui.IWorkbenchSite; 30 import org.eclipse.ui.PlatformUI; 31 32 import org.eclipse.jdt.core.ICompilationUnit; 33 import org.eclipse.jdt.core.IJavaElement; 34 import org.eclipse.jdt.core.IParent; 35 import org.eclipse.jdt.core.IType; 36 import org.eclipse.jdt.core.JavaModelException; 37 38 import org.eclipse.jdt.internal.corext.codemanipulation.SortMembersOperation; 39 40 import org.eclipse.jdt.ui.JavaUI; 41 42 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 43 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 44 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 45 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 46 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 47 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog; 48 import org.eclipse.jdt.internal.ui.dialogs.SortMembersMessageDialog; 49 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 50 import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation; 51 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; 52 import org.eclipse.jdt.internal.ui.util.ElementValidator; 53 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 54 55 72 public class SortMembersAction extends SelectionDispatchAction { 73 74 private CompilationUnitEditor fEditor; 75 private final static String ID_OPTIONAL_DIALOG= "org.eclipse.jdt.ui.actions.SortMembersAction"; 77 84 public SortMembersAction(IWorkbenchSite site) { 85 super(site); 86 setText(ActionMessages.SortMembersAction_label); 87 setDescription(ActionMessages.SortMembersAction_description); 88 setToolTipText(ActionMessages.SortMembersAction_tooltip); 89 90 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SORT_MEMBERS_ACTION); 91 } 92 93 97 public SortMembersAction(CompilationUnitEditor editor) { 98 this(editor.getEditorSite()); 99 fEditor= editor; 100 setEnabled(checkEnabledEditor()); 101 } 102 103 private boolean checkEnabledEditor() { 104 return fEditor != null && SelectionConverter.canOperateOn(fEditor); 105 } 106 107 109 112 public void selectionChanged(IStructuredSelection selection) { 113 boolean enabled= false; 114 enabled= getSelectedCompilationUnit(selection) != null; 115 setEnabled(enabled); 116 } 117 118 121 public void run(IStructuredSelection selection) { 122 Shell shell= getShell(); 123 try { 124 ICompilationUnit cu= getSelectedCompilationUnit(selection); 125 if (cu == null) { 126 return; 127 } 128 IType[] types= cu.getTypes(); 129 if (!hasMembersToSort(types)) { 130 return; 131 } 132 if (!ActionUtil.isEditable(getShell(), cu)) { 133 return; 134 } 135 136 SortMembersMessageDialog dialog= new SortMembersMessageDialog(getShell()); 137 if (dialog.open() != Window.OK) { 138 return; 139 } 140 141 if (!ElementValidator.check(cu, getShell(), getDialogTitle(), false)) { 142 return; 143 } 144 145 IEditorPart editor= JavaUI.openInEditor(cu); 147 if (editor != null) { 148 run(shell, cu, editor, dialog.isNotSortingFieldsEnabled()); 149 } 150 } catch (CoreException e) { 151 ExceptionHandler.handle(e, shell, getDialogTitle(), null); 152 } 153 } 154 155 private boolean hasMembersToSort(IJavaElement[] members) throws JavaModelException { 156 if (members.length > 1) { 157 return true; 158 } 159 if (members.length == 1) { 160 IJavaElement elem= members[0]; 161 if (elem instanceof IParent) { 162 return hasMembersToSort(((IParent) elem).getChildren()); 163 } 164 } 165 return false; 166 } 167 168 170 173 public void selectionChanged(ITextSelection selection) { 174 } 175 176 179 public void run(ITextSelection selection) { 180 Shell shell= getShell(); 181 IJavaElement input= SelectionConverter.getInput(fEditor); 182 if (input instanceof ICompilationUnit) { 183 if (!ActionUtil.isEditable(fEditor)) { 184 return; 185 } 186 SortMembersMessageDialog dialog= new SortMembersMessageDialog(getShell()); 187 if (dialog.open() != Window.OK) { 188 return; 189 } 190 if (!ElementValidator.check(input, getShell(), getDialogTitle(), true)) { 191 return; 192 } 193 run(shell, (ICompilationUnit) input, fEditor, dialog.isNotSortingFieldsEnabled()); 194 } else { 195 MessageDialog.openInformation(shell, getDialogTitle(), ActionMessages.SortMembersAction_not_applicable); 196 } 197 } 198 199 201 private boolean containsRelevantMarkers(IEditorPart editor) { 202 IAnnotationModel model= JavaUI.getDocumentProvider().getAnnotationModel(editor.getEditorInput()); 203 Iterator iterator= model.getAnnotationIterator(); 204 while (iterator.hasNext()) { 205 Object element= iterator.next(); 206 if (element instanceof IJavaAnnotation) { 207 IJavaAnnotation annot= (IJavaAnnotation) element; 208 if (!annot.isMarkedDeleted() && annot.isPersistent() && !annot.isProblem()) 209 return true; 210 } 211 } 212 return false; 213 } 214 215 private void run(Shell shell, ICompilationUnit cu, IEditorPart editor, boolean isNotSortFields) { 216 if (containsRelevantMarkers(editor)) { 217 int returnCode= OptionalMessageDialog.open(ID_OPTIONAL_DIALOG, 218 getShell(), 219 getDialogTitle(), 220 null, 221 ActionMessages.SortMembersAction_containsmarkers, 222 MessageDialog.WARNING, 223 new String [] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL}, 224 0); 225 if (returnCode != OptionalMessageDialog.NOT_SHOWN && 226 returnCode != Window.OK ) return; 227 } 228 229 SortMembersOperation op= new SortMembersOperation(cu, null, isNotSortFields); 230 try { 231 BusyIndicatorRunnableContext context= new BusyIndicatorRunnableContext(); 232 PlatformUI.getWorkbench().getProgressService().runInUI(context, 233 new WorkbenchRunnableAdapter(op, op.getScheduleRule()), 234 op.getScheduleRule()); 235 } catch (InvocationTargetException e) { 236 ExceptionHandler.handle(e, shell, getDialogTitle(), null); 237 } catch (InterruptedException e) { 238 } 240 } 241 242 243 private ICompilationUnit getSelectedCompilationUnit(IStructuredSelection selection) { 244 if (selection.size() == 1) { 245 Object element= selection.getFirstElement(); 246 if (element instanceof ICompilationUnit) { 247 return (ICompilationUnit) element; 248 } else if (element instanceof IType) { 249 IType type= (IType) element; 250 if (type.getParent() instanceof ICompilationUnit) { return type.getCompilationUnit(); 252 } 253 } 254 } 255 return null; 256 } 257 258 private String getDialogTitle() { 259 return ActionMessages.SortMembersAction_dialog_title; 260 } 261 } 262 | Popular Tags |