KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > MultiSortMembersAction


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  * Alex Blewitt - Bug 133277 Allow Sort Members to be performed on package and project levels
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.actions;
13
14 import java.util.Hashtable JavaDoc;
15
16 import org.eclipse.jface.window.Window;
17
18 import org.eclipse.ui.IWorkbenchSite;
19 import org.eclipse.ui.PlatformUI;
20
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IParent;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
27
28 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30 import org.eclipse.jdt.internal.ui.dialogs.SortMembersMessageDialog;
31 import org.eclipse.jdt.internal.ui.fix.ICleanUp;
32 import org.eclipse.jdt.internal.ui.fix.SortMembersCleanUp;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
34
35 public class MultiSortMembersAction extends CleanUpAction {
36
37     public MultiSortMembersAction(IWorkbenchSite site) {
38         super(site);
39         
40         setText(ActionMessages.SortMembersAction_label);
41         setDescription(ActionMessages.SortMembersAction_description);
42         setToolTipText(ActionMessages.SortMembersAction_tooltip);
43         
44         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SORT_MEMBERS_ACTION);
45     }
46
47     public MultiSortMembersAction(JavaEditor editor) {
48         super(editor);
49         
50         setText(ActionMessages.SortMembersAction_label);
51         setDescription(ActionMessages.SortMembersAction_description);
52         setToolTipText(ActionMessages.SortMembersAction_tooltip);
53         
54         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SORT_MEMBERS_ACTION);
55     }
56
57     /**
58      * {@inheritDoc}
59      */

60     protected ICleanUp[] createCleanUps(ICompilationUnit[] units) {
61         try {
62             if (!hasMembersToSort(units))
63                 return null;
64         } catch (JavaModelException e) {
65             JavaPlugin.log(e);
66             return null;
67         }
68
69         SortMembersMessageDialog dialog= new SortMembersMessageDialog(getShell());
70         if (dialog.open() != Window.OK)
71             return null;
72
73         Hashtable JavaDoc settings= new Hashtable JavaDoc();
74         settings.put(CleanUpConstants.SORT_MEMBERS, CleanUpConstants.TRUE);
75         settings.put(CleanUpConstants.SORT_MEMBERS_ALL, !dialog.isNotSortingFieldsEnabled() ? CleanUpConstants.TRUE : CleanUpConstants.FALSE);
76
77         return new ICleanUp[] {
78             new SortMembersCleanUp(settings)
79         };
80     }
81
82     /**
83      * {@inheritDoc}
84      */

85     protected String JavaDoc getActionName() {
86         return ActionMessages.SortMembersAction_dialog_title;
87     }
88
89     private boolean hasMembersToSort(ICompilationUnit[] units) throws JavaModelException {
90         for (int i= 0; i < units.length; i++) {
91             if (hasMembersToSort(units[i].getTypes()))
92                 return true;
93         }
94
95         return false;
96     }
97
98     private boolean hasMembersToSort(IJavaElement[] members) throws JavaModelException {
99         if (members.length > 1)
100             return true;
101
102         if (members.length == 0)
103             return false;
104
105         IJavaElement elem= members[0];
106         if (!(elem instanceof IParent))
107             return false;
108
109         return hasMembersToSort(((IParent)elem).getChildren());
110     }
111
112 }
113
Popular Tags