]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/SortMembersAction.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / SortMembersAction.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.ui.actions;
12
13import java.lang.reflect.InvocationTargetException;
14import java.util.Iterator;
15
16import org.eclipse.swt.widgets.Shell;
17
18import org.eclipse.core.runtime.CoreException;
19
20import org.eclipse.jface.dialogs.IDialogConstants;
21import org.eclipse.jface.dialogs.MessageDialog;
22import org.eclipse.jface.viewers.IStructuredSelection;
23import org.eclipse.jface.window.Window;
24
25import org.eclipse.jface.text.ITextSelection;
26import org.eclipse.jface.text.source.Annotation;
27import org.eclipse.jface.text.source.IAnnotationModel;
28
29import org.eclipse.ui.IEditorPart;
30import org.eclipse.ui.IWorkbenchSite;
31import org.eclipse.ui.PlatformUI;
32
33import org.eclipse.jdt.core.ICompilationUnit;
34import org.eclipse.jdt.core.IJavaElement;
35import org.eclipse.jdt.core.IParent;
36import org.eclipse.jdt.core.IType;
37import org.eclipse.jdt.core.JavaModelException;
38
39import org.eclipse.jdt.internal.corext.codemanipulation.SortMembersOperation;
40
41import org.eclipse.jdt.ui.JavaUI;
42
43import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
44import org.eclipse.jdt.internal.ui.actions.ActionMessages;
45import org.eclipse.jdt.internal.ui.actions.ActionUtil;
46import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
47import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
48import org.eclipse.jdt.internal.ui.dialogs.SortMembersMessageDialog;
49import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
50import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation;
51import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
52import org.eclipse.jdt.internal.ui.util.ElementValidator;
53import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
54
55/**
56 * Sorts the members of a compilation unit with the sort order as specified in
57 * the Sort Order preference page.
58 * <p>
59 * The action will open the parent compilation unit in a Java editor. The result
60 * is unsaved, so the user can decide if the changes are acceptable.
61 * <p>
62 * The action is applicable to structured selections containing a single
63 * <code>ICompilationUnit</code> or top level <code>IType</code> in a
64 * compilation unit.
65 *
66 * <p>
67 * This class may be instantiated; it is not intended to be subclassed.
68 * </p>
69 *
70 * @since 2.1
71 *
72 * @noextend This class is not intended to be subclassed by clients.
73 */
74public class SortMembersAction extends SelectionDispatchAction {
75
76 public CompilationUnitEditor fEditor;
77 private final static String ID_OPTIONAL_DIALOG= "org.eclipse.jdt.ui.actions.SortMembersAction"; //$NON-NLS-1$
78
79 /**
80 * Creates a new <code>SortMembersAction</code>. The action requires
81 * that the selection provided by the site's selection provider is of type <code>
82 * org.eclipse.jface.viewers.IStructuredSelection</code>.
83 *
84 * @param site the site providing context information for this action
85 */
86 public SortMembersAction(IWorkbenchSite site) {
87 super(site);
88 setText(ActionMessages.SortMembersAction_label);
89 setDescription(ActionMessages.SortMembersAction_description);
90 setToolTipText(ActionMessages.SortMembersAction_tooltip);
91
92 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SORT_MEMBERS_ACTION);
93 }
94
95 /**
96 * Note: This constructor is for internal use only. Clients should not call this constructor.
97 * @param editor the compilation unit editor
98 *
99 * @noreference This constructor is not intended to be referenced by clients.
100 */
101 public SortMembersAction(CompilationUnitEditor editor) {
102 this(editor.getEditorSite());
103 fEditor= editor;
104 setEnabled(checkEnabledEditor());
105 }
106
107 private boolean checkEnabledEditor() {
108 return fEditor != null && SelectionConverter.canOperateOn(fEditor);
109 }
110
111 //---- Structured Viewer -----------------------------------------------------------
112
113 /* (non-Javadoc)
114 * Method declared on SelectionDispatchAction
115 */
116 @Override
117 public void selectionChanged(IStructuredSelection selection) {
118 boolean enabled= false;
119 enabled= getSelectedCompilationUnit(selection) != null;
120 setEnabled(enabled);
121 }
122
123 /* (non-Javadoc)
124 * Method declared on SelectionDispatchAction
125 */
126 @Override
127 public void run(IStructuredSelection selection) {
128 Shell shell= getShell();
129 try {
130 ICompilationUnit cu= getSelectedCompilationUnit(selection);
131 if (cu == null) {
132 return;
133 }
134 IType[] types= cu.getTypes();
135 if (!hasMembersToSort(types)) {
136 return;
137 }
138 if (!ActionUtil.isEditable(getShell(), cu)) {
139 return;
140 }
141
142 SortMembersMessageDialog dialog= new SortMembersMessageDialog(getShell());
143 if (dialog.open() != Window.OK) {
144 return;
145 }
146
147 if (!ElementValidator.check(cu, getShell(), getDialogTitle(), false)) {
148 return;
149 }
150
151 // open an editor and work on a working copy
152 IEditorPart editor= JavaUI.openInEditor(cu);
153 if (editor != null) {
154 run(shell, cu, editor, dialog.isNotSortingFieldsEnabled());
155 }
156 } catch (CoreException e) {
157 ExceptionHandler.handle(e, shell, getDialogTitle(), null);
158 }
159 }
160
161 private boolean hasMembersToSort(IJavaElement[] members) throws JavaModelException {
162 if (members.length > 1) {
163 return true;
164 }
165 if (members.length == 1) {
166 IJavaElement elem= members[0];
167 if (elem instanceof IParent) {
168 return hasMembersToSort(((IParent) elem).getChildren());
169 }
170 }
171 return false;
172 }
173
174 //---- Java Editor --------------------------------------------------------------
175
176 /* (non-Javadoc)
177 * Method declared on SelectionDispatchAction
178 */
179 @Override
180 public void selectionChanged(ITextSelection selection) {
181 }
182
183 /* (non-Javadoc)
184 * Method declared on SelectionDispatchAction
185 */
186 @Override
187 public void run(ITextSelection selection) {
188 fEditor.generated_3806800038962803857(this);
189 }
190
191
192
193 //---- Helpers -------------------------------------------------------------------
194
195 private boolean containsRelevantMarkers(IEditorPart editor) {
196 IAnnotationModel model= JavaUI.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
197 Iterator<Annotation> iterator= model.getAnnotationIterator();
198 while (iterator.hasNext()) {
199 Annotation element= iterator.next();
200 if (element instanceof IJavaAnnotation) {
201 IJavaAnnotation annot= (IJavaAnnotation) element;
202 if (!annot.isMarkedDeleted() && annot.isPersistent() && !annot.isProblem())
203 return true;
204 }
205 }
206 return false;
207 }
208
209 public void run(Shell shell, ICompilationUnit cu, IEditorPart editor, boolean isNotSortFields) {
210 if (containsRelevantMarkers(editor)) {
211 int returnCode= OptionalMessageDialog.open(ID_OPTIONAL_DIALOG,
212 getShell(),
213 getDialogTitle(),
214 null,
215 ActionMessages.SortMembersAction_containsmarkers,
216 MessageDialog.WARNING,
217 new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL},
218 0);
219 if (returnCode != OptionalMessageDialog.NOT_SHOWN &&
220 returnCode != Window.OK ) return;
221 }
222
223 SortMembersOperation op= new SortMembersOperation(cu, null, isNotSortFields);
224 try {
225 BusyIndicatorRunnableContext context= new BusyIndicatorRunnableContext();
226 op.generated_1317820492583791401(context);
227 } catch (InvocationTargetException e) {
228 ExceptionHandler.handle(e, shell, getDialogTitle(), null);
229 } catch (InterruptedException e) {
230 // Do nothing. Operation has been canceled by user.
231 }
232 }
233
234 private ICompilationUnit getSelectedCompilationUnit(IStructuredSelection selection) {
235 if (selection.size() == 1) {
236 Object element= selection.getFirstElement();
237 if (element instanceof ICompilationUnit) {
238 return (ICompilationUnit) element;
239 } else if (element instanceof IType) {
240 IType type= (IType) element;
241 if (type.getParent() instanceof ICompilationUnit) { // only top level types
242 return type.getCompilationUnit();
243 }
244 }
245 }
246 return null;
247 }
248
249 public String getDialogTitle() {
250 return ActionMessages.SortMembersAction_dialog_title;
251 }
252}