1
/*******************************************************************************
2
* Copyright (c) 2002, 2009 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
9
* IBM Rational Software - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.ui.newui;
13
import java.util.Arrays;
15
import org.eclipse.core.resources.IProject;
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IConfigurationElement;
18
import org.eclipse.core.runtime.IExtension;
19
import org.eclipse.core.runtime.IExtensionPoint;
20
import org.eclipse.core.runtime.Platform;
21
import org.eclipse.jface.dialogs.Dialog;
22
import org.eclipse.jface.dialogs.MessageDialog;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.SelectionAdapter;
25
import org.eclipse.swt.events.SelectionEvent;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.Control;
31
import org.eclipse.swt.widgets.Shell;
32
import org.eclipse.swt.widgets.Table;
33
import org.eclipse.swt.widgets.TableColumn;
34
import org.eclipse.swt.widgets.TableItem;
36
import org.eclipse.cdt.core.model.util.CDTListComparator;
37
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
38
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
39
import org.eclipse.cdt.ui.CUIPlugin;
42
* @noextend This class is not intended to be subclassed by clients.
44
public class ManageConfigDialog extends Dialog {
45
private static final String EXTENSION_POINT_ID = "org.eclipse.cdt.ui.newCfgDialog"; //$NON-NLS-1$
46
public static final String ELEMENT_NAME = "dialog"; //$NON-NLS-1$
47
public static final String CLASS_NAME = "class"; //$NON-NLS-1$
48
public static final String TITLE_NAME = "title"; //$NON-NLS-1$
49
public static final String ID_NAME = "mbs_id"; //$NON-NLS-1$
52
private static final String CMN_PREFIX = "BuildPropertyCommon"; //$NON-NLS-1$
53
private static final String CMN_LABEL = CMN_PREFIX + ".label"; //$NON-NLS-1$
54
private static final String NEW = CMN_LABEL + ".new"; //$NON-NLS-1$
55
private static final String REMOVE = CMN_LABEL + ".remove"; //$NON-NLS-1$
56
private static final String PREFIX = "ManageConfig"; //$NON-NLS-1$
57
private static final String LABEL = PREFIX + ".label"; //$NON-NLS-1$
58
private static final String RENAME = LABEL + ".rename"; //$NON-NLS-1$
59
private static final String NEW_CONF_DLG = LABEL + ".new.config.dialog"; //$NON-NLS-1$
60
private static final String RENAME_CONF_DLG = LABEL + ".rename.config.dialog"; //$NON-NLS-1$
62
// The list of configurations to delete
63
private ICProjectDescription des;
66
private String mbs_id;
67
protected Table table;
69
protected Button actBtn;
70
protected Button newBtn;
71
protected Button renBtn;
72
protected Button delBtn;
77
ManageConfigDialog(Shell parentShell, String _title, IProject _prj) {
79
setShellStyle(getShellStyle() | SWT.RESIZE);
85
protected void configureShell(Shell shell) {
86
super.configureShell(shell);
87
if (title != null) shell.setText(title);
91
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
94
protected Control createDialogArea(Composite parent) {
95
Composite composite = new Composite(parent, SWT.NULL);
96
composite.setFont(parent.getFont());
97
composite.setLayout(new GridLayout(4, true));
98
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
100
// Create the current config table
101
table = new Table(composite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
102
GridData gd = new GridData(GridData.FILL_BOTH);
103
gd.horizontalSpan = 4;
104
table.setLayoutData(gd);
105
table.setHeaderVisible(true);
106
table.setLinesVisible(true);
108
table.addSelectionListener(new SelectionAdapter() {
110
public void widgetSelected(SelectionEvent e) {
114
TableColumn col = new TableColumn(table, SWT.NONE);
115
col.setText(UIMessages.getString("ManageConfigDialog.1")); //$NON-NLS-1$
117
col = new TableColumn(table, SWT.NONE);
118
col.setText(UIMessages.getString("ManageConfigDialog.2")); //$NON-NLS-1$
120
col = new TableColumn(table, SWT.NONE);
121
col.setText(UIMessages.getString("ManageConfigDialog.3")); //$NON-NLS-1$
124
actBtn = new Button(composite, SWT.PUSH);
125
actBtn.setText(UIMessages.getString("ManageConfigDialog.4")); //$NON-NLS-1$
126
actBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
127
actBtn.addSelectionListener(new SelectionAdapter() {
129
public void widgetSelected(SelectionEvent e) {
130
TableItem[] tis = table.getSelection();
131
if (tis == null || tis.length != 1) return;
132
ICConfigurationDescription cfgd = (ICConfigurationDescription)tis[0].getData();
134
des.setActiveConfiguration(cfgd);
138
newBtn = new Button(composite, SWT.PUSH);
139
newBtn.setText(UIMessages.getString(NEW));
140
newBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
141
newBtn.addSelectionListener(new SelectionAdapter() {
143
public void widgetSelected(SelectionEvent e) {
147
delBtn = new Button(composite, SWT.PUSH);
148
delBtn.setText(UIMessages.getString(REMOVE));
149
delBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
150
delBtn.addSelectionListener(new SelectionAdapter() {
152
public void widgetSelected(SelectionEvent e) {
153
handleRemovePressed();
156
renBtn = new Button(composite, SWT.PUSH);
157
renBtn.setText(UIMessages.getString(RENAME));
158
renBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
159
renBtn.addSelectionListener(new SelectionAdapter() {
161
public void widgetSelected(SelectionEvent e) {
162
handleRenamePressed();
165
des = CDTPropertyManager.getProjectDescription(composite, prj);
172
* Event handler for the add button
174
protected void handleNewPressed() {
175
INewCfgDialog dialog = handleSpecificMBS(mbs_id);
176
if (dialog == null) { // default (core) implementation.
177
dialog = new NewConfigurationDialog(getShell());
178
dialog.setTitle(UIMessages.getString(NEW_CONF_DLG));
180
dialog.setProject(des);
181
if (dialog.open() == OK) updateData();
185
* Tries to load MBS-specific creation dialog a
186
* @return false if there's no such feature
188
protected INewCfgDialog handleSpecificMBS(String id) {
189
IExtensionPoint extensionPoint = Platform.getExtensionRegistry()
190
.getExtensionPoint(EXTENSION_POINT_ID);
191
if (extensionPoint == null) return null;
192
IExtension[] extensions = extensionPoint.getExtensions();
193
if (extensions == null) return null;
194
for (int i = 0; i < extensions.length; ++i) {
195
IConfigurationElement[] elements = extensions[i].getConfigurationElements();
196
for (int k = 0; k < elements.length; k++) {
197
if (elements[k].getName().equals(ELEMENT_NAME)) {
198
if (! id.equals(elements[k].getAttribute(ID_NAME)))
200
INewCfgDialog dialog = null;
202
dialog = (INewCfgDialog) elements[k].createExecutableExtension(CLASS_NAME);
203
dialog.setTitle(elements[k].getAttribute(TITLE_NAME));
204
dialog.setShell(getShell());
206
} catch (CoreException e) {
207
System.out.println("Cannot create dialog: " + e.getLocalizedMessage()); //$NON-NLS-1$
217
* (non-javadoc) Event handler for the rename button
219
protected void handleRenamePressed() {
220
int sel = table.getSelectionIndex();
222
ICConfigurationDescription cfgd = (ICConfigurationDescription) table.getItem(sel).getData();
223
RenameConfigurationDialog dialog = new RenameConfigurationDialog(
224
getShell(), cfgd, des.getConfigurations(),
225
UIMessages.getString(RENAME_CONF_DLG));
226
if (dialog.open() == OK) {
227
cfgd.setName(dialog.getNewName());
228
cfgd.setDescription(dialog.getNewDescription());
235
* (non-javadoc) Event handler for the remove button
237
protected void handleRemovePressed() {
238
TableItem[] tis = table.getSelection();
239
if (tis == null || tis.length < 1) return;
240
String[] names = new String[tis.length];
241
for (int i=0; i<tis.length; i++)
242
names[i] = tis[i].getText(0);
243
// Get the confirmation from user before deleting the configuration
244
Shell shell = CUIPlugin.getActiveWorkbenchShell();
245
boolean shouldDelete = MessageDialog.openQuestion(shell,
246
UIMessages.getString("ManageConfig.deletedialog.title"), //$NON-NLS-1$
247
UIMessages.getFormattedString("ManageConfig.deletedialog.message", //$NON-NLS-1$
250
boolean wasActive = false;
251
for (int j=0; j<tis.length; j++) {
252
ICConfigurationDescription cfgd = (ICConfigurationDescription)tis[j].getData();
253
if (cfgd.isActive()) wasActive = true;
254
des.removeConfiguration(cfgd);
257
ICConfigurationDescription[] cfgds = des.getConfigurations();
258
if (wasActive && cfgds.length > 0) {
259
cfgds[0].setActive();
260
des.setActiveConfiguration(cfgds[0]);
266
private void updateButtons() {
267
int sel = table.getSelectionCount();
268
delBtn.setEnabled(sel > 0 & sel < table.getItemCount());
269
renBtn.setEnabled(sel == 1);
271
ICConfigurationDescription c = (ICConfigurationDescription)table.getSelection()[0].getData();
272
actBtn.setEnabled(c != null && !c.isActive());
274
actBtn.setEnabled(false);
278
* refresh configs table after changes
280
private void updateData() {
282
ICConfigurationDescription[] cfgds = des.getConfigurations();
283
mbs_id = cfgds[0].getBuildSystemId();
284
Arrays.sort(cfgds, CDTListComparator.getInstance());
285
for (int i=0; i<cfgds.length; i++ ) {
286
TableItem t = new TableItem(table, 0);
287
t.setText(0, cfgds[i].getName());
288
t.setText(1, cfgds[i].getDescription());
289
t.setText(2, cfgds[i].isActive() ? UIMessages.getString("ManageConfigDialog.5") : ""); //$NON-NLS-1$ //$NON-NLS-2$
292
if (table.getItemCount() > 0) table.select(0);
297
ICProjectDescription getProjectDescription(){