KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > SortMembersAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Iterator JavaDoc;
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 /**
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 public class SortMembersAction extends SelectionDispatchAction {
73
74     private CompilationUnitEditor fEditor;
75     private final static String JavaDoc ID_OPTIONAL_DIALOG= "org.eclipse.jdt.ui.actions.SortMembersAction"; //$NON-NLS-1$
76

77     /**
78      * Creates a new <code>SortMembersAction</code>. The action requires
79      * that the selection provided by the site's selection provider is of type <code>
80      * org.eclipse.jface.viewers.IStructuredSelection</code>.
81      *
82      * @param site the site providing context information for this action
83      */

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     /**
94      * Note: This constructor is for internal use only. Clients should not call this constructor.
95      * @param editor the compilation unit editor
96      */

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     //---- Structured Viewer -----------------------------------------------------------
108

109     /* (non-Javadoc)
110      * Method declared on SelectionDispatchAction
111      */

112     public void selectionChanged(IStructuredSelection selection) {
113         boolean enabled= false;
114         enabled= getSelectedCompilationUnit(selection) != null;
115         setEnabled(enabled);
116     }
117     
118     /* (non-Javadoc)
119      * Method declared on SelectionDispatchAction
120      */

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             // open an editor and work on a working copy
146
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     //---- Java Editor --------------------------------------------------------------
169

170     /* (non-Javadoc)
171      * Method declared on SelectionDispatchAction
172      */

173     public void selectionChanged(ITextSelection selection) {
174     }
175     
176     /* (non-Javadoc)
177      * Method declared on SelectionDispatchAction
178      */

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     //---- Helpers -------------------------------------------------------------------
200

201     private boolean containsRelevantMarkers(IEditorPart editor) {
202         IAnnotationModel model= JavaUI.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
203         Iterator JavaDoc iterator= model.getAnnotationIterator();
204         while (iterator.hasNext()) {
205             Object JavaDoc 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 JavaDoc[] {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 JavaDoc e) {
236             ExceptionHandler.handle(e, shell, getDialogTitle(), null);
237         } catch (InterruptedException JavaDoc e) {
238             // Do nothing. Operation has been canceled by user.
239
}
240     }
241
242         
243     private ICompilationUnit getSelectedCompilationUnit(IStructuredSelection selection) {
244         if (selection.size() == 1) {
245             Object JavaDoc 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) { // only top level types
251
return type.getCompilationUnit();
252                 }
253             }
254         }
255         return null;
256     }
257     
258     private String JavaDoc getDialogTitle() {
259         return ActionMessages.SortMembersAction_dialog_title;
260     }
261 }
262
Popular Tags