1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.rcp.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IViewReference;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.views.IViewDescriptor;
29 import org.seasar.uruma.core.UrumaConstants;
30
31
32
33
34
35
36 public class ViewPartUtil implements UrumaConstants {
37 private ViewPartUtil() {
38
39 }
40
41
42
43
44
45
46
47
48
49 public static String getPrimaryId(final String id) {
50 if (id != null) {
51 int pos = id.lastIndexOf(COLON);
52 if (pos > -1) {
53 return id.substring(0, pos);
54 }
55 }
56 return id;
57 }
58
59
60
61
62
63
64
65
66
67 public static String getSecondaryId(final String id) {
68 if (id != null) {
69 int pos = id.lastIndexOf(COLON);
70 if (pos > -1) {
71 return id.substring(pos + 1);
72 }
73 }
74 return null;
75 }
76
77
78
79
80
81
82
83
84
85
86 public static String createFullId(final String primaryId,
87 final String secondaryId) {
88 if (secondaryId != null) {
89 return primaryId + COLON + secondaryId;
90 } else {
91 return primaryId;
92 }
93 }
94
95
96
97
98
99
100
101
102 public static IViewDescriptor findViewDescriptor(final String viewId) {
103 if (viewId == null) {
104 return null;
105 }
106
107 String pId = getPrimaryId(viewId);
108 IViewDescriptor[] descs = PlatformUI.getWorkbench().getViewRegistry()
109 .getViews();
110 for (int i = 0; i < descs.length; i++) {
111 IViewDescriptor viewDescriptor = descs[i];
112 if (pId.equals(viewDescriptor.getId())) {
113 return viewDescriptor;
114 }
115 }
116 return null;
117 }
118
119
120
121
122
123
124
125 public static List<IViewDescriptor> findUrumaAppViewDescs() {
126 List<IViewDescriptor> result = new ArrayList<IViewDescriptor>();
127
128 String prefix = UrumaServiceUtil.getService().getPluginId() + PERIOD;
129 IViewDescriptor[] descs = PlatformUI.getWorkbench().getViewRegistry()
130 .getViews();
131 for (int i = 0; i < descs.length; i++) {
132 IViewDescriptor desc = descs[i];
133 if (desc.getId().startsWith(prefix)) {
134 result.add(desc);
135 }
136 }
137 return result;
138 }
139
140
141
142
143
144
145 public static List<IViewReference> findUrumaAppViewRefs() {
146 List<IViewReference> result = new ArrayList<IViewReference>();
147
148 String prefix = UrumaServiceUtil.getService().getPluginId() + PERIOD;
149 IWorkbenchPage page = getWorkbenchPage();
150 if (page != null) {
151 IViewReference[] refs = page.getViewReferences();
152 for (int i = 0; i < refs.length; i++) {
153 IViewReference ref = refs[i];
154 if (ref.getId().startsWith(prefix)) {
155 result.add(ref);
156 }
157 }
158 }
159
160 return result;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174 public static IViewReference findViewReference(final String primaryId,
175 final String secondaryId) {
176 IWorkbenchPage page = getWorkbenchPage();
177 if (page != null) {
178 return page.findViewReference(primaryId, secondaryId);
179 } else {
180 return null;
181 }
182
183 }
184
185
186
187
188
189
190
191
192 public static void acitivateView(final String id) {
193 acitivateView(id, null);
194 }
195
196
197
198
199
200
201
202
203
204
205
206 public static void acitivateView(final String primaryId,
207 final String secondaryId) {
208 IWorkbenchPage page = getWorkbenchPage();
209 if (page == null) {
210 return;
211 }
212
213 try {
214 int mode = IWorkbenchPage.VIEW_ACTIVATE;
215 IViewReference ref = findViewReference(primaryId, secondaryId);
216 if (ref == null) {
217 mode = IWorkbenchPage.VIEW_CREATE;
218 }
219 page.showView(primaryId, secondaryId, mode);
220 } catch (PartInitException ex) {
221
222 }
223
224 }
225
226
227
228
229
230
231 public static IWorkbenchPage getWorkbenchPage() {
232 IWorkbench workbench = PlatformUI.getWorkbench();
233 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
234 if (window != null) {
235 return window.getActivePage();
236 }
237 return null;
238 }
239 }