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.osgi.util.NLS;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Table;
34 import org.eclipse.swt.widgets.TableColumn;
35 import org.eclipse.swt.widgets.TableItem;
37 import org.eclipse.cdt.core.model.util.CDTListComparator;
38 import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
39 import org.eclipse.cdt.core.settings.model.ICProjectDescription;
40 import org.eclipse.cdt.ui.CUIPlugin;
42 import org.eclipse.cdt.internal.ui.newui.Messages;
45 * @noextend This class is not intended to be subclassed by clients.
47 public class ManageConfigDialog extends Dialog {
48 private static final String EXTENSION_POINT_ID = "org.eclipse.cdt.ui.newCfgDialog"; //$NON-NLS-1$
49 public static final String ELEMENT_NAME = "dialog"; //$NON-NLS-1$
50 public static final String CLASS_NAME = "class"; //$NON-NLS-1$
51 public static final String TITLE_NAME = "title"; //$NON-NLS-1$
52 public static final String ID_NAME = "mbs_id"; //$NON-NLS-1$
54 // The list of configurations to delete
55 private ICProjectDescription des;
58 private String mbs_id;
59 protected Table table;
61 protected Button actBtn;
62 protected Button newBtn;
63 protected Button renBtn;
64 protected Button delBtn;
69 ManageConfigDialog(Shell parentShell, String _title, IProject _prj) {
71 setShellStyle(getShellStyle() | SWT.RESIZE);
77 protected void configureShell(Shell shell) {
78 super.configureShell(shell);
79 if (title != null) shell.setText(title);
83 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
86 protected Control createDialogArea(Composite parent) {
87 Composite composite = new Composite(parent, SWT.NULL);
88 composite.setFont(parent.getFont());
89 composite.setLayout(new GridLayout(4, true));
90 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
92 // Create the current config table
93 table = new Table(composite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
94 GridData gd = new GridData(GridData.FILL_BOTH);
95 gd.horizontalSpan = 4;
96 table.setLayoutData(gd);
97 table.setHeaderVisible(true);
98 table.setLinesVisible(true);
100 table.addSelectionListener(new SelectionAdapter() {
102 public void widgetSelected(SelectionEvent e) {
106 TableColumn col = new TableColumn(table, SWT.NONE);
107 col.setText(Messages.ManageConfigDialog_1);
109 col = new TableColumn(table, SWT.NONE);
110 col.setText(Messages.ManageConfigDialog_2);
112 col = new TableColumn(table, SWT.NONE);
113 col.setText(Messages.ManageConfigDialog_3);
116 actBtn = new Button(composite, SWT.PUSH);
117 actBtn.setText(Messages.ManageConfigDialog_4);
118 actBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
119 actBtn.addSelectionListener(new SelectionAdapter() {
121 public void widgetSelected(SelectionEvent e) {
122 TableItem[] tis = table.getSelection();
123 if (tis == null || tis.length != 1) return;
124 ICConfigurationDescription cfgd = (ICConfigurationDescription)tis[0].getData();
126 des.setActiveConfiguration(cfgd);
130 newBtn = new Button(composite, SWT.PUSH);
131 newBtn.setText(Messages.BuildPropertyCommon_label_new);
132 newBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
133 newBtn.addSelectionListener(new SelectionAdapter() {
135 public void widgetSelected(SelectionEvent e) {
139 delBtn = new Button(composite, SWT.PUSH);
140 delBtn.setText(Messages.BuildPropertyCommon_label_remove);
141 delBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
142 delBtn.addSelectionListener(new SelectionAdapter() {
144 public void widgetSelected(SelectionEvent e) {
145 handleRemovePressed();
148 renBtn = new Button(composite, SWT.PUSH);
149 renBtn.setText(Messages.ManageConfig_label_rename);
150 renBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
151 renBtn.addSelectionListener(new SelectionAdapter() {
153 public void widgetSelected(SelectionEvent e) {
154 handleRenamePressed();
157 des = CDTPropertyManager.getProjectDescription(composite, prj);
164 * Event handler for the add button
166 protected void handleNewPressed() {
167 INewCfgDialog dialog = handleSpecificMBS(mbs_id);
168 if (dialog == null) { // default (core) implementation.
169 dialog = new NewConfigurationDialog(getShell());
170 dialog.setTitle(Messages.ManageConfig_label_new_config_dialog);
172 dialog.setProject(des);
173 if (dialog.open() == OK) updateData();
177 * Tries to load MBS-specific creation dialog a
178 * @return false if there's no such feature
180 protected INewCfgDialog handleSpecificMBS(String id) {
181 IExtensionPoint extensionPoint = Platform.getExtensionRegistry()
182 .getExtensionPoint(EXTENSION_POINT_ID);
183 if (extensionPoint == null) return null;
184 IExtension[] extensions = extensionPoint.getExtensions();
185 if (extensions == null) return null;
186 for (int i = 0; i < extensions.length; ++i) {
187 IConfigurationElement[] elements = extensions[i].getConfigurationElements();
188 for (int k = 0; k < elements.length; k++) {
189 if (elements[k].getName().equals(ELEMENT_NAME)) {
190 if (! id.equals(elements[k].getAttribute(ID_NAME)))
192 INewCfgDialog dialog = null;
194 dialog = (INewCfgDialog) elements[k].createExecutableExtension(CLASS_NAME);
195 dialog.setTitle(elements[k].getAttribute(TITLE_NAME));
196 dialog.setShell(getShell());
198 } catch (CoreException e) {
199 System.out.println("Cannot create dialog: " + e.getLocalizedMessage()); //$NON-NLS-1$
209 * (non-javadoc) Event handler for the rename button
211 protected void handleRenamePressed() {
212 int sel = table.getSelectionIndex();
214 ICConfigurationDescription cfgd = (ICConfigurationDescription) table.getItem(sel).getData();
215 RenameConfigurationDialog dialog = new RenameConfigurationDialog(
216 getShell(), cfgd, des.getConfigurations(),
217 Messages.ManageConfig_label_rename_config_dialog);
218 if (dialog.open() == OK) {
219 cfgd.setName(dialog.getNewName());
220 cfgd.setDescription(dialog.getNewDescription());
227 * (non-javadoc) Event handler for the remove button
229 protected void handleRemovePressed() {
230 TableItem[] tis = table.getSelection();
231 if (tis == null || tis.length < 1) return;
232 String[] names = new String[tis.length];
233 for (int i=0; i<tis.length; i++)
234 names[i] = tis[i].getText(0);
235 // Get the confirmation from user before deleting the configuration
236 Shell shell = CUIPlugin.getActiveWorkbenchShell();
237 boolean shouldDelete = MessageDialog.openQuestion(shell,
238 Messages.ManageConfig_deletedialog_title,
239 NLS.bind(Messages.ManageConfig_deletedialog_message, names));
241 boolean wasActive = false;
242 for (int j=0; j<tis.length; j++) {
243 ICConfigurationDescription cfgd = (ICConfigurationDescription)tis[j].getData();
244 if (cfgd.isActive()) wasActive = true;
245 des.removeConfiguration(cfgd);
248 ICConfigurationDescription[] cfgds = des.getConfigurations();
249 if (wasActive && cfgds.length > 0) {
250 cfgds[0].setActive();
251 des.setActiveConfiguration(cfgds[0]);
257 private void updateButtons() {
258 int sel = table.getSelectionCount();
259 delBtn.setEnabled(sel > 0 & sel < table.getItemCount());
260 renBtn.setEnabled(sel == 1);
262 ICConfigurationDescription c = (ICConfigurationDescription)table.getSelection()[0].getData();
263 actBtn.setEnabled(c != null && !c.isActive());
265 actBtn.setEnabled(false);
269 * refresh configs table after changes
271 private void updateData() {
273 ICConfigurationDescription[] cfgds = des.getConfigurations();
274 mbs_id = cfgds[0].getBuildSystemId();
275 Arrays.sort(cfgds, CDTListComparator.getInstance());
276 for (int i=0; i<cfgds.length; i++ ) {
277 TableItem t = new TableItem(table, 0);
278 t.setText(0, cfgds[i].getName());
279 t.setText(1, cfgds[i].getDescription());
280 t.setText(2, cfgds[i].isActive() ? Messages.ManageConfigDialog_5 : ""); //$NON-NLS-1$
283 if (table.getItemCount() > 0) table.select(0);
288 ICProjectDescription getProjectDescription(){