1
/*******************************************************************************
2
* Copyright (c) 2007, 2010 Intel 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
9
* Intel Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.ui.newui;
13
import java.util.HashMap;
14
import java.util.Iterator;
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.core.runtime.IConfigurationElement;
19
import org.eclipse.core.runtime.IExtension;
20
import org.eclipse.core.runtime.IExtensionPoint;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.core.runtime.NullProgressMonitor;
23
import org.eclipse.core.runtime.Platform;
24
import org.eclipse.jface.layout.PixelConverter;
25
import org.eclipse.jface.viewers.CheckStateChangedEvent;
26
import org.eclipse.jface.viewers.CheckboxTableViewer;
27
import org.eclipse.jface.viewers.ICheckStateListener;
28
import org.eclipse.jface.viewers.IStructuredContentProvider;
29
import org.eclipse.jface.viewers.LabelProvider;
30
import org.eclipse.jface.viewers.Viewer;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.custom.SashForm;
33
import org.eclipse.swt.events.SelectionAdapter;
34
import org.eclipse.swt.events.SelectionEvent;
35
import org.eclipse.swt.layout.GridData;
36
import org.eclipse.swt.layout.GridLayout;
37
import org.eclipse.swt.widgets.Composite;
38
import org.eclipse.swt.widgets.Table;
39
import org.eclipse.ui.PlatformUI;
41
import org.eclipse.cdt.core.CCorePlugin;
42
import org.eclipse.cdt.core.model.CoreModelUtil;
43
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
44
import org.eclipse.cdt.core.settings.model.ICMultiResourceDescription;
45
import org.eclipse.cdt.core.settings.model.ICResourceDescription;
46
import org.eclipse.cdt.core.settings.model.ICTargetPlatformSetting;
47
import org.eclipse.cdt.ui.CUIPlugin;
48
import org.eclipse.cdt.ui.dialogs.ICOptionPage;
49
import org.eclipse.cdt.utils.ui.controls.TabFolderLayout;
51
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
52
import org.eclipse.cdt.internal.ui.newui.Messages;
55
* @noextend This class is not intended to be subclassed by clients.
57
public class BinaryParsTab extends AbstractCPropertyTab {
59
/* Settings from binary parser pages are NOT saved by prior CDT version.
60
* So existing binary parsers _always_ use default values.
61
* Moreover, obsolete interface is used while attempting to save.
63
* We have to affect both parsers and pages
64
* to teach them to save data really.
66
* It will be done in next versions. Currently pages are disabled.
69
private static final int DEFAULT_HEIGHT = 160;
70
private static final String ATTR_FILTER = "filter"; //$NON-NLS-1$
71
private static final String ATTR_NAME = "name"; //$NON-NLS-1$
72
private static final String ATTR_NAME_VISIBILITY = "visibility"; //$NON-NLS-1$
73
private static final String ATTR_VALUE = "value"; //$NON-NLS-1$
74
private static final String ATTR_VALUE_PRIVATE = "private"; //$NON-NLS-1$
76
protected Map<String, BinaryParserConfiguration> configMap;
77
protected Map<String, BinaryParserPageConfiguration> fParserPageMap = null;
78
protected Table table;
79
protected CheckboxTableViewer tv;
80
protected Composite parserGroup;
81
protected SashForm sashForm;
83
private ICTargetPlatformSetting tps;
85
protected class BinaryParserConfiguration {
86
IExtension fExtension;
87
public BinaryParserConfiguration(IExtension extension) { fExtension = extension; }
88
public String getID() { return fExtension.getUniqueIdentifier();}
89
public String getName() { return fExtension.getLabel(); }
91
public String toString() { return fExtension.getUniqueIdentifier(); }
93
public boolean equals(Object obj) {
94
if (obj instanceof BinaryParserConfiguration) {
95
return this.getID().equals(((BinaryParserConfiguration) obj).getID());
97
return super.equals(obj);
101
protected class BinaryParserPageConfiguration {
102
ICOptionPage dynamicPage;
103
IConfigurationElement fElement;
104
public BinaryParserPageConfiguration(IConfigurationElement element) {
107
public ICOptionPage getPage() throws CoreException {
108
if (dynamicPage == null) {
109
dynamicPage = (ICOptionPage) fElement.createExecutableExtension("class"); //$NON-NLS-1$
115
protected String getParserId() {
116
return CCorePlugin.BINARY_PARSER_SIMPLE_ID;
120
public void createControls(Composite parent) {
121
super.createControls(parent);
122
PlatformUI.getWorkbench().getHelpSystem().setHelp(usercomp, ICHelpContextIds.BINARY_PARSER_PAGE);
124
usercomp.setLayout(new GridLayout(1, false));
126
sashForm = new SashForm(usercomp, SWT.NONE);
127
sashForm.setBackground(sashForm.getDisplay().getSystemColor(SWT.COLOR_GRAY));
128
sashForm.setOrientation(SWT.VERTICAL);
129
sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
131
GridLayout layout = new GridLayout(2, false);
132
layout.marginHeight = 5;
133
sashForm.setLayout(layout);
135
Composite c1 = new Composite(sashForm, SWT.NONE);
136
c1.setLayout(new GridLayout(2, false));
137
setupLabel(c1, Messages.BinaryParsTab_0, 2, GridData.FILL_HORIZONTAL);
138
table = new Table(c1, SWT.BORDER | SWT.CHECK | SWT.SINGLE);
139
table.setLayoutData(new GridData(GridData.FILL_BOTH));
140
table.addSelectionListener(new SelectionAdapter() {
142
public void widgetSelected(SelectionEvent e) {
143
handleBinaryParserChanged();
146
tv = new CheckboxTableViewer(table);
147
tv.setContentProvider(new IStructuredContentProvider() {
148
public Object[] getElements(Object inputElement) {
149
return (Object[])inputElement;
151
public void dispose() {}
152
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
154
tv.setLabelProvider(new LabelProvider() {
156
public String getText(Object element) {
157
String txt = (element != null) ? element.toString() : EMPTY_STR;
158
if (element instanceof BinaryParserConfiguration)
159
txt = ((BinaryParserConfiguration)element).getName();
164
tv.addCheckStateListener(new ICheckStateListener() {
165
public void checkStateChanged(CheckStateChangedEvent e) {
169
// get "standard" buttons on my own place
170
Composite c = new Composite(c1, SWT.NONE);
171
c.setLayoutData(new GridData(GridData.END));
172
initButtons(c, new String[] {MOVEUP_STR, MOVEDOWN_STR});
174
parserGroup = new Composite(sashForm, SWT.NULL);
175
GridData gd = new GridData();
176
parserGroup.setLayout(new TabFolderLayout());
178
PixelConverter converter = new PixelConverter(parent);
179
gd.heightHint = converter.convertHorizontalDLUsToPixels(DEFAULT_HEIGHT);
181
gd.horizontalAlignment = GridData.FILL;
182
gd.grabExcessHorizontalSpace = true;
183
gd.grabExcessVerticalSpace = true;
184
gd.horizontalSpan = 2;
185
parserGroup.setLayoutData(gd);
187
sashForm.setWeights(new int[] {100, 100});
188
initializeParserList();
189
initializeParserPageMap();
190
handleBinaryParserChanged();
194
public void setVisible(boolean _visible) {
195
super.setVisible(_visible);
196
page.enableConfigSelection(!_visible);
200
public void updateData(ICResourceDescription cfgd) {
202
if (page.isForPrefs()) { // prefs
203
if (cfgd != null && cfgd.getConfiguration() != null) {
204
tps = cfgd.getConfiguration().getTargetPlatformSetting();
206
ids = tps.getBinaryParserIds();
209
ids = new String[0]; // no selection
211
ICConfigurationDescription[] cfgs = page.getCfgsEditable();
212
ids = CoreModelUtil.getBinaryParserIds(cfgs);
214
Object[] data = new Object[configMap.size()];
215
HashMap<String, BinaryParserConfiguration> clone = new HashMap<String, BinaryParserConfiguration>(configMap);
216
// add checked elements
218
for (i=0; i<ids.length; i++) {
219
data[i] = clone.get(ids[i]);
220
clone.remove(ids[i]);
222
// add remaining parsers (unchecked)
223
Iterator<String> it = clone.keySet().iterator();
225
while (it.hasNext()) {
226
String s = it.next();
227
data[i++] = clone.get(s);
230
tv.setAllChecked(false);
232
for (i=0; i<ids.length; i++) {
233
if (configMap.containsKey(ids[i])) {
234
tv.setChecked(configMap.get(ids[i]), true);
240
private void initializeParserList() {
241
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(CCorePlugin.PLUGIN_ID, CCorePlugin.BINARY_PARSER_SIMPLE_ID);
243
IExtension[] exts = point.getExtensions();
244
configMap = new HashMap<String, BinaryParserConfiguration>(exts.length);
245
for (IExtension ext : exts) {
246
if (isExtensionVisible(ext)) {
247
configMap.put(ext.getUniqueIdentifier(), new BinaryParserConfiguration(ext));
253
private void initializeParserPageMap() {
254
fParserPageMap = new HashMap<String, BinaryParserPageConfiguration>(5);
256
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(CUIPlugin.PLUGIN_ID, "BinaryParserPage"); //$NON-NLS-1$
257
IConfigurationElement[] infos = extensionPoint.getConfigurationElements();
258
for (IConfigurationElement info : infos) {
259
if (info.getName().equals("parserPage")) { //$NON-NLS-1$
260
String id = info.getAttribute("parserID"); //$NON-NLS-1$
261
fParserPageMap.put(id, new BinaryParserPageConfiguration(info));
266
private boolean isExtensionVisible(IExtension ext) {
267
IConfigurationElement[] elements = ext.getConfigurationElements();
268
for (IConfigurationElement element : elements) {
269
IConfigurationElement[] children = element.getChildren(ATTR_FILTER);
270
for (IConfigurationElement element2 : children) {
271
String name = element2.getAttribute(ATTR_NAME);
272
if (name != null && name.equals(ATTR_NAME_VISIBILITY)) {
273
String value = element2.getAttribute(ATTR_VALUE);
274
if (value != null && value.equals(ATTR_VALUE_PRIVATE)) {
281
return false; // invalid extension definition (must have at least cextension elements)
285
public void updateButtons() {
286
int cnt = table.getItemCount();
287
int pos = table.getSelectionIndex();
288
buttonSetEnabled(0, pos > 0);
289
buttonSetEnabled(1, pos != -1 && pos < (cnt - 1));
291
protected void handleBinaryParserChanged() {
292
String[] enabled = getBinaryParserIDs();
293
ICOptionPage dynamicPage;
294
for (String element : enabled) { // create all enabled pages
295
dynamicPage = getBinaryParserPage(element);
297
if (dynamicPage != null) {
298
if (dynamicPage.getControl() == null) {
299
dynamicPage.setContainer(page);
300
dynamicPage.createControl(parserGroup);
302
dynamicPage.setVisible(false);
305
// Retrieve the dynamic UI for the current parser
306
String parserID = getCurrentBinaryParserID();
307
dynamicPage = getBinaryParserPage(parserID);
308
if (dynamicPage != null) { dynamicPage.setVisible(true); }
311
protected String[] getBinaryParserIDs() {
312
return configMap.keySet().toArray(new String[configMap.keySet().size()]);
315
protected ICOptionPage getBinaryParserPage(String parserID) {
316
BinaryParserPageConfiguration configElement = fParserPageMap.get(parserID);
317
if (configElement != null) {
319
return configElement.getPage();
320
} catch (CoreException e) {}
325
protected String getCurrentBinaryParserID() {
326
int x = table.getSelectionIndex();
327
if (x < 0) return null;
328
return ((BinaryParserConfiguration)table.getItem(x).getData()).getID();
332
protected void performApply(ICResourceDescription src, ICResourceDescription dst) {
333
if (page.isMultiCfg()) {
334
src = ((ICResourceDescription[])((ICMultiResourceDescription)src).getItems())[0];
335
dst = ((ICResourceDescription[])((ICMultiResourceDescription)dst).getItems())[0];
337
ICTargetPlatformSetting tps1 = src.getConfiguration().getTargetPlatformSetting();
338
ICTargetPlatformSetting tps2 = dst.getConfiguration().getTargetPlatformSetting();
339
if (tps1 != null && tps2 != null) {
340
tps2.setBinaryParserIds(tps1.getBinaryParserIds());
345
protected void performDefaults() {
346
if (page.isForProject())
347
CoreModelUtil.setBinaryParserIds(page.getCfgsEditable(), null);
350
tps.setBinaryParserIds(null);
352
updateData(getResDesc());
355
private void informPages(boolean apply) {
356
IProgressMonitor mon = new NullProgressMonitor();
357
Iterator<BinaryParserPageConfiguration> it = fParserPageMap.values().iterator();
359
while (it.hasNext()) {
361
ICOptionPage dynamicPage = (it.next()).getPage();
362
if (dynamicPage.isValid() && dynamicPage.getControl() != null) {
364
dynamicPage.performApply(mon);
366
dynamicPage.performDefaults();
368
} catch (CoreException e) {}
373
public void buttonPressed(int i) {
386
// Move item up / down
387
private void moveItem(boolean up) {
388
int n = table.getSelectionIndex();
391
(!up && n+1 == table.getItemCount()))
393
Object d = tv.getElementAt(n);
394
boolean checked = tv.getChecked(d);
396
n = up ? n - 1 : n + 1;
398
tv.setChecked(d, checked);
399
table.setSelection(n);
404
private void saveChecked() {
405
Object[] objs = tv.getCheckedElements();
408
ids = new String[objs.length];
409
for (int i=0; i<objs.length; i++) {
410
ids[i] = ((BinaryParserConfiguration)objs[i]).getID();
413
if (page.isForPrefs()) {
414
if (tps != null) tps.setBinaryParserIds(ids);
416
CoreModelUtil.setBinaryParserIds(page.getCfgsEditable(), ids);
419
// This page can be displayed for project only
421
public boolean canBeVisible() {
422
return page.isForProject() || page.isForPrefs();