]>
Commit | Line | Data |
---|---|---|
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 | * 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; | |
15 | import java.util.Map; | |
16 | ||
17 | import org.eclipse.jface.dialogs.MessageDialog; | |
18 | import org.eclipse.jface.window.Window; | |
19 | ||
20 | import org.eclipse.ui.IWorkbenchSite; | |
21 | import org.eclipse.ui.PlatformUI; | |
22 | ||
23 | import org.eclipse.jdt.core.ICompilationUnit; | |
24 | import org.eclipse.jdt.core.IJavaElement; | |
25 | import org.eclipse.jdt.core.IParent; | |
26 | import org.eclipse.jdt.core.JavaModelException; | |
27 | ||
28 | import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; | |
29 | ||
30 | import org.eclipse.jdt.ui.cleanup.CleanUpOptions; | |
31 | import org.eclipse.jdt.ui.cleanup.ICleanUp; | |
32 | ||
33 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
34 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
35 | import org.eclipse.jdt.internal.ui.dialogs.SortMembersMessageDialog; | |
36 | import org.eclipse.jdt.internal.ui.fix.SortMembersCleanUp; | |
37 | import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; | |
38 | ||
39 | public class MultiSortMembersAction extends CleanUpAction { | |
40 | ||
41 | public MultiSortMembersAction(IWorkbenchSite site) { | |
42 | super(site); | |
43 | ||
44 | setText(ActionMessages.SortMembersAction_label); | |
45 | setDescription(ActionMessages.SortMembersAction_description); | |
46 | setToolTipText(ActionMessages.SortMembersAction_tooltip); | |
47 | ||
48 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SORT_MEMBERS_ACTION); | |
49 | } | |
50 | ||
51 | public MultiSortMembersAction(JavaEditor editor) { | |
52 | super(editor); | |
53 | ||
54 | setText(ActionMessages.SortMembersAction_label); | |
55 | setDescription(ActionMessages.SortMembersAction_description); | |
56 | setToolTipText(ActionMessages.SortMembersAction_tooltip); | |
57 | ||
58 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SORT_MEMBERS_ACTION); | |
59 | } | |
60 | ||
61 | /** | |
62 | * {@inheritDoc} | |
63 | */ | |
64 | @Override | |
65 | protected ICleanUp[] getCleanUps(ICompilationUnit[] units) { | |
66 | try { | |
67 | if (!hasMembersToSort(units)) { | |
68 | MessageDialog.openInformation(getShell(), ActionMessages.MultiSortMembersAction_noElementsToSortDialog_title, ActionMessages.MultiSortMembersAction_noElementsToSortDialog_message); | |
69 | return null; | |
70 | } | |
71 | } catch (JavaModelException e) { | |
72 | JavaPlugin.log(e); | |
73 | return null; | |
74 | } | |
75 | ||
76 | Map<String, String> settings= getSettings(); | |
77 | if (settings == null) | |
78 | return null; | |
79 | ||
80 | return new ICleanUp[] { | |
81 | new SortMembersCleanUp(settings) | |
82 | }; | |
83 | } | |
84 | ||
85 | protected Map<String, String> getSettings() { | |
86 | SortMembersMessageDialog dialog= new SortMembersMessageDialog(getShell()); | |
87 | if (dialog.open() != Window.OK) | |
88 | return null; | |
89 | ||
90 | Hashtable<String, String> settings= new Hashtable<String, String>(); | |
91 | settings.put(CleanUpConstants.SORT_MEMBERS, CleanUpOptions.TRUE); | |
92 | settings.put(CleanUpConstants.SORT_MEMBERS_ALL, !dialog.isNotSortingFieldsEnabled() ? CleanUpOptions.TRUE : CleanUpOptions.FALSE); | |
93 | return settings; | |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritDoc} | |
98 | */ | |
99 | @Override | |
100 | protected String getActionName() { | |
101 | return ActionMessages.SortMembersAction_dialog_title; | |
102 | } | |
103 | ||
104 | private boolean hasMembersToSort(ICompilationUnit[] units) throws JavaModelException { | |
105 | for (int i= 0; i < units.length; i++) { | |
106 | if (hasMembersToSort(units[i].getTypes())) | |
107 | return true; | |
108 | } | |
109 | ||
110 | return false; | |
111 | } | |
112 | ||
113 | private boolean hasMembersToSort(IJavaElement[] members) throws JavaModelException { | |
114 | if (members.length > 1) | |
115 | return true; | |
116 | ||
117 | if (members.length == 0) | |
118 | return false; | |
119 | ||
120 | IJavaElement elem= members[0]; | |
121 | if (!(elem instanceof IParent)) | |
122 | return false; | |
123 | ||
124 | return hasMembersToSort(((IParent)elem).getChildren()); | |
125 | } | |
126 | ||
127 | } |