upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / newui / ManageConfigDialog.java
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
7  *
8  * Contributors:
9  * IBM Rational Software - Initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.ui.newui;
12
13 import java.util.Arrays;
14
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;
36
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;
41
42 import org.eclipse.cdt.internal.ui.newui.Messages;
43
44 /**
45  * @noextend This class is not intended to be subclassed by clients.
46  */
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$
53
54         // The list of configurations to delete
55         private ICProjectDescription des;
56         private IProject prj;
57         private String title;
58         private String mbs_id;
59         protected Table table;
60         
61         protected Button actBtn;
62         protected Button newBtn;
63         protected Button renBtn;
64         protected Button delBtn;
65
66         /**
67          * @param parentShell
68          */
69         ManageConfigDialog(Shell parentShell, String _title, IProject _prj) {
70                 super(parentShell);
71                 setShellStyle(getShellStyle() | SWT.RESIZE);
72                 title = _title;
73                 prj = _prj;
74         }
75
76         @Override
77         protected void configureShell(Shell shell) {
78                 super.configureShell(shell);
79                 if (title != null) shell.setText(title);
80         }
81         
82         /* (non-Javadoc)
83          * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
84          */
85         @Override
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));
91         
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);
99                 
100                 table.addSelectionListener(new SelectionAdapter() {
101                         @Override
102                         public void widgetSelected(SelectionEvent e) {
103                                 updateButtons();
104                         }});
105                 
106                 TableColumn col = new TableColumn(table, SWT.NONE);
107                 col.setText(Messages.ManageConfigDialog_1); 
108                 col.setWidth(100);
109                 col = new TableColumn(table, SWT.NONE);
110                 col.setText(Messages.ManageConfigDialog_2); 
111                 col.setWidth(120);
112                 col = new TableColumn(table, SWT.NONE);
113                 col.setText(Messages.ManageConfigDialog_3); 
114                 col.setWidth(80);
115
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() {
120                         @Override
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();
125 //                              cfgd.setActive();
126                                 des.setActiveConfiguration(cfgd);
127                                 updateData();
128                         }} ); 
129
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() {
134                         @Override
135                         public void widgetSelected(SelectionEvent e) {
136                                 handleNewPressed();
137                         }} ); 
138
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() {
143                         @Override
144                         public void widgetSelected(SelectionEvent e) {
145                                 handleRemovePressed();
146                         }} ); 
147
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() {
152                         @Override
153                         public void widgetSelected(SelectionEvent e) {
154                                 handleRenamePressed();
155                         }} ); 
156
157                 des = CDTPropertyManager.getProjectDescription(composite, prj);
158 //              comp = composite;
159                 
160                 updateData();
161         return composite;
162         }
163         /*
164          * Event handler for the add button
165          */
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);
171                 }
172                 dialog.setProject(des); 
173                 if (dialog.open() == OK) updateData();
174         }
175         
176         /**
177          * Tries to load MBS-specific creation dialog a
178          * @return false if there's no such feature 
179          */
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)))
191                                                 continue;
192                                         INewCfgDialog dialog = null;
193                                         try {
194                                                 dialog = (INewCfgDialog) elements[k].createExecutableExtension(CLASS_NAME);
195                                                 dialog.setTitle(elements[k].getAttribute(TITLE_NAME));
196                                                 dialog.setShell(getShell());
197                                                 return dialog;
198                                         } catch (CoreException e) {
199                                                 System.out.println("Cannot create dialog: " + e.getLocalizedMessage()); //$NON-NLS-1$
200                                                 return null; 
201                                         }
202                                 }                                       
203                         }
204                 }
205                 return null;
206         }
207         
208         /*
209          * (non-javadoc) Event handler for the rename button
210          */
211         protected void handleRenamePressed() {
212                 int sel = table.getSelectionIndex();
213                 if (sel != -1) {
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());
221                                 updateData();
222                         }
223                 }
224         }
225
226         /*
227          * (non-javadoc) Event handler for the remove button
228          */
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));
240                 if (shouldDelete) {
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);
246                                 
247                         }
248                         ICConfigurationDescription[] cfgds = des.getConfigurations(); 
249                         if (wasActive && cfgds.length > 0) {
250                                 cfgds[0].setActive();
251                                 des.setActiveConfiguration(cfgds[0]);
252                         }
253                         updateData();
254                 }
255         }
256
257         private void updateButtons() {
258                 int sel = table.getSelectionCount();
259                 delBtn.setEnabled(sel > 0 & sel < table.getItemCount());
260                 renBtn.setEnabled(sel == 1);
261                 if (sel == 1) {
262                         ICConfigurationDescription c = (ICConfigurationDescription)table.getSelection()[0].getData();
263                         actBtn.setEnabled(c != null && !c.isActive());
264                 } else
265                         actBtn.setEnabled(false);
266         }
267
268         /**
269          * refresh configs table after changes
270          */
271         private void updateData() {
272                 table.removeAll();
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$ 
281                         t.setData(cfgds[i]);
282                 }
283                 if (table.getItemCount() > 0) table.select(0);
284                 table.setFocus();
285                 updateButtons();
286         }
287         
288         ICProjectDescription getProjectDescription(){
289                 return des;
290         }
291 }