Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 Bartosz Michalik 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 |
* Bartosz Michalik (bartosz.michalik@gmail.com) |
11 |
*******************************************************************************/ |
12 |
package org.eclipse.pde.internal.ui.editor.product; |
13 |
|
14 |
import java.util.ArrayList; |
15 |
import org.eclipse.jface.util.Util; |
16 |
import org.eclipse.jface.viewers.*; |
17 |
import org.eclipse.jface.window.Window; |
18 |
import org.eclipse.pde.core.IModelChangedEvent; |
19 |
import org.eclipse.pde.core.plugin.IFragmentModel; |
20 |
import org.eclipse.pde.core.plugin.PluginRegistry; |
21 |
import org.eclipse.pde.internal.core.iproduct.*; |
22 |
import org.eclipse.pde.internal.ui.*; |
23 |
import org.eclipse.pde.internal.ui.editor.PDEFormPage; |
24 |
import org.eclipse.pde.internal.ui.editor.TableSection; |
25 |
import org.eclipse.pde.internal.ui.parts.TablePart; |
26 |
import org.eclipse.swt.SWT; |
27 |
import org.eclipse.swt.custom.CCombo; |
28 |
import org.eclipse.swt.custom.TableEditor; |
29 |
import org.eclipse.swt.events.*; |
30 |
import org.eclipse.swt.graphics.Image; |
31 |
import org.eclipse.swt.layout.GridData; |
32 |
import org.eclipse.swt.widgets.*; |
33 |
import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
34 |
import org.eclipse.ui.forms.widgets.FormToolkit; |
35 |
import org.eclipse.ui.forms.widgets.Section; |
36 |
|
37 |
public class PluginConfigurationSection extends TableSection { |
38 |
|
39 |
private class ContentProvider implements IStructuredContentProvider { |
40 |
|
41 |
private IProduct fProduct; |
42 |
|
43 |
ContentProvider() { |
44 |
} |
45 |
|
46 |
/* (non-Javadoc) |
47 |
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) |
48 |
*/ |
49 |
public Object[] getElements(Object inputElement) { |
50 |
return fProduct.getPluginConfigurations(); |
51 |
} |
52 |
|
53 |
/* (non-Javadoc) |
54 |
* @see org.eclipse.jface.viewers.IContentProvider#dispose() |
55 |
*/ |
56 |
public void dispose() { |
57 |
|
58 |
} |
59 |
|
60 |
/* (non-Javadoc) |
61 |
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) |
62 |
*/ |
63 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
64 |
if (oldInput == newInput) |
65 |
return; |
66 |
fProduct = (IProduct) newInput; |
67 |
//TODO refresh |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
private class LabelProvider extends PDELabelProvider { |
73 |
|
74 |
public Image getColumnImage(Object obj, int index) { |
75 |
if (index == 0) |
76 |
return super.getColumnImage(PluginRegistry.findModel(((IPluginConfiguration) obj).getId()), index); |
77 |
return null; |
78 |
} |
79 |
|
80 |
public String getColumnText(Object obj, int index) { |
81 |
IPluginConfiguration configuration = (IPluginConfiguration) obj; |
82 |
switch (index) { |
83 |
case 0 : |
84 |
return configuration.getId(); |
85 |
//return super.getColumnText(PluginRegistry.findModel(configuration.getId()), index); |
86 |
case 1 : |
87 |
return Integer.toString(configuration.getStartLevel()); |
88 |
case 2 : |
89 |
return Boolean.toString(configuration.isAutoStart()); |
90 |
} |
91 |
return null; |
92 |
} |
93 |
|
94 |
} |
95 |
|
96 |
private TableViewer fConfigurationsTable; |
97 |
private TableEditor fLevelColumnEditor; |
98 |
private TableEditor fAutoColumnEditor; |
99 |
|
100 |
private IStructuredSelection fLastSelection = null; |
101 |
|
102 |
/** |
103 |
* @param page |
104 |
* @param parent |
105 |
*/ |
106 |
public PluginConfigurationSection(PDEFormPage page, Composite parent) { |
107 |
super(page, parent, Section.DESCRIPTION, getButtonLabels()); |
108 |
} |
109 |
|
110 |
private static String[] getButtonLabels() { |
111 |
String[] labels = new String[3]; |
112 |
labels[0] = PDEUIMessages.Product_PluginSection_add; |
113 |
labels[1] = PDEUIMessages.PluginSection_remove; |
114 |
labels[2] = PDEUIMessages.Product_PluginSection_removeAll; |
115 |
return labels; |
116 |
} |
117 |
|
118 |
/* (non-Javadoc) |
119 |
* @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit) |
120 |
*/ |
121 |
protected void createClient(Section section, FormToolkit toolkit) { |
122 |
section.setText(PDEUIMessages.ConfigurationPageMock_sectionTitle); |
123 |
section.setDescription(PDEUIMessages.ConfigurationPageMock_sectionDesc); |
124 |
GridData sectionData = new GridData(SWT.FILL, SWT.FILL, true, true); |
125 |
sectionData.horizontalSpan = 2; |
126 |
section.setLayoutData(sectionData); |
127 |
Composite container = createClientContainer(section, 3, toolkit); |
128 |
createViewerPartControl(container, SWT.SINGLE, 3, toolkit); |
129 |
fConfigurationsTable = getTablePart().getTableViewer(); |
130 |
|
131 |
final Table table = fConfigurationsTable.getTable(); |
132 |
final TableColumn column1 = new TableColumn(table, SWT.LEFT); |
133 |
column1.setText(PDEUIMessages.ConfigurationPageMock_sectionTitle); |
134 |
column1.setWidth(300); |
135 |
|
136 |
final TableColumn column2 = new TableColumn(table, SWT.CENTER); |
137 |
column2.setText(PDEUIMessages.EquinoxPluginBlock_levelColumn); |
138 |
|
139 |
final TableColumn column3 = new TableColumn(table, SWT.CENTER); |
140 |
column3.setText(PDEUIMessages.EquinoxPluginBlock_autoColumn); |
141 |
|
142 |
table.addControlListener(new ControlListener() { |
143 |
|
144 |
public void controlMoved(ControlEvent e) { |
145 |
} |
146 |
|
147 |
public void controlResized(ControlEvent e) { |
148 |
int size = table.getSize().x; |
149 |
column1.setWidth(size / 7 * 4); |
150 |
column2.setWidth(size / 7 * 2); |
151 |
column3.setWidth(size / 7 * 1); |
152 |
} |
153 |
|
154 |
}); |
155 |
|
156 |
table.setHeaderVisible(true); |
157 |
toolkit.paintBordersFor(container); |
158 |
fConfigurationsTable.setLabelProvider(getLabelProvider()); |
159 |
fConfigurationsTable.setContentProvider(new ContentProvider()); |
160 |
fConfigurationsTable.setInput(getProduct()); |
161 |
createEditors(); |
162 |
|
163 |
section.setClient(container); |
164 |
getModel().addModelChangedListener(this); |
165 |
updateRemoveButtons(true, true); |
166 |
} |
167 |
|
168 |
protected void buttonSelected(int index) { |
169 |
switch (index) { |
170 |
case 0 : |
171 |
handleAdd(); |
172 |
break; |
173 |
case 1 : |
174 |
handleRemove(); |
175 |
break; |
176 |
case 2 : |
177 |
handleRemoveAll(); |
178 |
break; |
179 |
} |
180 |
} |
181 |
|
182 |
private void handleAdd() { |
183 |
ElementListSelectionDialog dialog = new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), PDEPlugin.getDefault().getLabelProvider()); |
184 |
IProductPlugin[] allPlugins = getProduct().getPlugins(); |
185 |
IPluginConfiguration[] configs = getProduct().getPluginConfigurations(); |
186 |
ArrayList filtered = new ArrayList(); |
187 |
for (int i = 0; i < allPlugins.length; ++i) { |
188 |
boolean match = false; |
189 |
for (int j = 0; j < configs.length; ++j) { |
190 |
String id = allPlugins[i].getId(); |
191 |
if (PluginRegistry.findModel(id) instanceof IFragmentModel || id.equals(configs[j].getId())) { |
192 |
match = true; |
193 |
break; |
194 |
} |
195 |
} |
196 |
if (!match) { |
197 |
filtered.add(allPlugins[i]); |
198 |
} |
199 |
} |
200 |
dialog.setElements(filtered.toArray(new IProductPlugin[filtered.size()])); |
201 |
dialog.setTitle(PDEUIMessages.PluginSelectionDialog_title); |
202 |
dialog.setMessage(PDEUIMessages.PluginSelectionDialog_message); |
203 |
dialog.setMultipleSelection(true); |
204 |
if (dialog.open() == Window.OK) { |
205 |
Object[] plugins = dialog.getResult(); |
206 |
for (int i = 0; i < plugins.length; i++) { |
207 |
addPlugin(((IProductPlugin) plugins[i]).getId()); |
208 |
} |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
private void handleRemove() { |
214 |
IStructuredSelection ssel = (IStructuredSelection) fConfigurationsTable.getSelection(); |
215 |
if (ssel.size() > 0) { |
216 |
Object[] objects = ssel.toArray(); |
217 |
IPluginConfiguration[] configurations = new IPluginConfiguration[objects.length]; |
218 |
System.arraycopy(objects, 0, configurations, 0, objects.length); |
219 |
getProduct().removePluginConfigurations(configurations); |
220 |
} |
221 |
} |
222 |
|
223 |
private void handleRemoveAll() { |
224 |
IProduct product = getProduct(); |
225 |
product.removePluginConfigurations(product.getPluginConfigurations()); |
226 |
|
227 |
} |
228 |
|
229 |
protected void handleDoubleClick(IStructuredSelection selection) { |
230 |
// Clean up any previous editor control |
231 |
clearEditors(); |
232 |
|
233 |
if (!selection.isEmpty()) { |
234 |
Table table = fConfigurationsTable.getTable(); |
235 |
final IPluginConfiguration ppc = (IPluginConfiguration) selection.getFirstElement(); |
236 |
final TableItem item = table.getSelection()[0]; |
237 |
final Spinner spinner = new Spinner(table, SWT.BORDER); |
238 |
|
239 |
spinner.setMinimum(0); |
240 |
String level = item.getText(1); |
241 |
int defaultLevel = level.length() == 0 || "default".equals(level) ? 0 : Integer.parseInt(level); //$NON-NLS-1$ |
242 |
spinner.setSelection(defaultLevel); |
243 |
spinner.addModifyListener(new ModifyListener() { |
244 |
public void modifyText(ModifyEvent e) { |
245 |
int selection = spinner.getSelection(); |
246 |
item.setText(1, selection == 0 ? "default" //$NON-NLS-1$ |
247 |
: Integer.toString(selection)); |
248 |
ppc.setStartLevel(selection); |
249 |
} |
250 |
}); |
251 |
fLevelColumnEditor.setEditor(spinner, item, 1); |
252 |
|
253 |
final CCombo combo = new CCombo(table, SWT.BORDER | SWT.READ_ONLY); |
254 |
//TODO is there need for the default options ?? |
255 |
combo.setItems(new String[] {Boolean.toString(true), Boolean.toString(false)}); |
256 |
combo.setText(item.getText(2)); |
257 |
combo.pack(); |
258 |
combo.addSelectionListener(new SelectionAdapter() { |
259 |
public void widgetSelected(SelectionEvent e) { |
260 |
item.setText(2, combo.getText()); |
261 |
ppc.setAutoStart(Boolean.getBoolean(combo.getText())); |
262 |
} |
263 |
}); |
264 |
fAutoColumnEditor.setEditor(combo, item, 2); |
265 |
} |
266 |
} |
267 |
|
268 |
protected void selectionChanged(IStructuredSelection selection) { |
269 |
if (selection == null || !selection.equals(fLastSelection)) { |
270 |
clearEditors(); |
271 |
} |
272 |
updateRemoveButtons(true, false); |
273 |
|
274 |
} |
275 |
|
276 |
private void addPlugin(String id) { |
277 |
IProduct product = getProduct(); |
278 |
IProductModelFactory factory = product.getModel().getFactory(); |
279 |
IPluginConfiguration configuration = factory.createPluginConfiguration(); |
280 |
configuration.setId(id); |
281 |
product.addPluginConfigurations(new IPluginConfiguration[] {configuration}); |
282 |
fConfigurationsTable.setSelection(new StructuredSelection(configuration)); |
283 |
} |
284 |
|
285 |
private ILabelProvider getLabelProvider() { |
286 |
return new LabelProvider(); |
287 |
} |
288 |
|
289 |
private void clearEditors() { |
290 |
Control oldEditor = fLevelColumnEditor.getEditor(); |
291 |
if (oldEditor != null && !oldEditor.isDisposed()) |
292 |
oldEditor.dispose(); |
293 |
|
294 |
oldEditor = fAutoColumnEditor.getEditor(); |
295 |
if (oldEditor != null && !oldEditor.isDisposed()) |
296 |
oldEditor.dispose(); |
297 |
} |
298 |
|
299 |
private void createEditors() { |
300 |
final Table table = fConfigurationsTable.getTable(); |
301 |
|
302 |
fLevelColumnEditor = new TableEditor(table); |
303 |
fLevelColumnEditor.horizontalAlignment = SWT.CENTER; |
304 |
fLevelColumnEditor.minimumWidth = 40; |
305 |
if (Util.isMac()) |
306 |
fLevelColumnEditor.minimumHeight = 27; |
307 |
|
308 |
fAutoColumnEditor = new TableEditor(table); |
309 |
fAutoColumnEditor.horizontalAlignment = SWT.CENTER; |
310 |
fAutoColumnEditor.grabHorizontal = true; |
311 |
fAutoColumnEditor.minimumWidth = 50; |
312 |
} |
313 |
|
314 |
private IProduct getProduct() { |
315 |
return getModel().getProduct(); |
316 |
} |
317 |
|
318 |
private IProductModel getModel() { |
319 |
return (IProductModel) getPage().getPDEEditor().getAggregateModel(); |
320 |
} |
321 |
|
322 |
/* (non-Javadoc) |
323 |
* @see org.eclipse.pde.internal.core.IPluginModelListener#modelsChanged(org.eclipse.pde.internal.core.PluginModelDelta) |
324 |
*/ |
325 |
public void modelChanged(IModelChangedEvent e) { |
326 |
//TODO update modelChanged method |
327 |
// No need to call super, handling world changed event here |
328 |
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { |
329 |
handleGlobalRefresh(); |
330 |
return; |
331 |
} |
332 |
Table table = fConfigurationsTable.getTable(); |
333 |
int count = table.getItemCount(); |
334 |
Object[] objects = e.getChangedObjects(); |
335 |
boolean refreshRemove = false; |
336 |
boolean refreshRemoveAll = false; |
337 |
if (e.getChangeType() == IModelChangedEvent.INSERT) { |
338 |
if (count == 0) { |
339 |
refreshRemoveAll = true; |
340 |
} |
341 |
for (int i = 0; i < objects.length; i++) { |
342 |
if (objects[i] instanceof IPluginConfiguration) |
343 |
fConfigurationsTable.add(objects[i]); |
344 |
} |
345 |
} else if (e.getChangeType() == IModelChangedEvent.REMOVE) { |
346 |
refreshRemove = refreshRemoveAll = true; |
347 |
int index = table.getSelectionIndex(); |
348 |
boolean global = false; |
349 |
for (int i = 0; i < objects.length; i++) { |
350 |
if (objects[i] instanceof IPluginConfiguration) |
351 |
fConfigurationsTable.remove(objects[i]); |
352 |
else if (objects[i] instanceof IProductPlugin) { |
353 |
global = true; |
354 |
break; |
355 |
} |
356 |
} |
357 |
|
358 |
if (global) |
359 |
handleGlobalRefresh(); |
360 |
|
361 |
// Update Selection |
362 |
|
363 |
if (count == 0) { |
364 |
table.deselectAll(); |
365 |
clearEditors(); |
366 |
} else if (index < count) { |
367 |
table.setSelection(index); |
368 |
} else { |
369 |
table.setSelection(count - 1); |
370 |
} |
371 |
|
372 |
} |
373 |
updateRemoveButtons(refreshRemove, refreshRemoveAll); |
374 |
|
375 |
} |
376 |
|
377 |
private void handleGlobalRefresh() { |
378 |
fConfigurationsTable.setInput(getProduct()); |
379 |
fConfigurationsTable.refresh(); |
380 |
} |
381 |
|
382 |
private void updateRemoveButtons(boolean updateRemove, boolean updateRemoveAll) { |
383 |
TablePart tablePart = getTablePart(); |
384 |
if (updateRemove) { |
385 |
ISelection selection = getViewerSelection(); |
386 |
tablePart.setButtonEnabled(1, isEditable() && !selection.isEmpty()); |
387 |
} |
388 |
int count = fConfigurationsTable.getTable().getItemCount(); |
389 |
if (updateRemoveAll) |
390 |
tablePart.setButtonEnabled(2, isEditable() && count > 0); |
391 |
} |
392 |
|
393 |
} |