1
/*******************************************************************************
2
* Copyright (c) 2007 Symbian Software Limited 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
* Bala Torati (Symbian) - Initial API and implementation
11
*******************************************************************************/
12
package org.eclipse.cdt.ui.templateengine.pages;
14
import java.util.List;
16
import org.eclipse.jface.preference.PreferencePage;
17
import org.eclipse.jface.viewers.ColumnLayoutData;
18
import org.eclipse.jface.viewers.ColumnWeightData;
19
import org.eclipse.jface.viewers.TableLayout;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.events.SelectionAdapter;
22
import org.eclipse.swt.events.SelectionEvent;
23
import org.eclipse.swt.events.SelectionListener;
24
import org.eclipse.swt.layout.FillLayout;
25
import org.eclipse.swt.layout.GridData;
26
import org.eclipse.swt.layout.GridLayout;
27
import org.eclipse.swt.widgets.Button;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Control;
30
import org.eclipse.swt.widgets.Label;
31
import org.eclipse.swt.widgets.MessageBox;
32
import org.eclipse.swt.widgets.Table;
33
import org.eclipse.swt.widgets.TableColumn;
34
import org.eclipse.swt.widgets.TableItem;
35
import org.eclipse.ui.IWorkbench;
36
import org.eclipse.ui.IWorkbenchPreferencePage;
37
import org.eclipse.ui.PlatformUI;
38
import org.w3c.dom.Element;
40
import org.eclipse.cdt.core.templateengine.SharedDefaults;
41
import org.eclipse.cdt.core.templateengine.TemplateEngine;
42
import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
43
import org.eclipse.cdt.core.templateengine.TemplateEngineUtil;
44
import org.eclipse.cdt.ui.CUIPlugin;
48
* This class represents a preference page that is contributed to the
49
* Preferences dialog. By Provided GUI for SharedDefaults settings for the
50
* Templates present in the Template Engine
53
public class TemplatePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
56
* Preference Page buttons
59
private static final String EditButton = Messages.getString("TemplatePreferencePage.8"); //$NON-NLS-1$
60
private static final String DeleteButton = Messages.getString("TemplatePreferencePage.9"); //$NON-NLS-1$
61
private static final String PageDescription = Messages.getString("TemplatePreferencePage.0");//$NON-NLS-1$
62
public static final String Blank = "";//$NON-NLS-1$
67
public static final String Message = Messages.getString("TemplatePreferencePage.1");//$NON-NLS-1$
68
protected static final String DuplicateEntry = Messages.getString("TemplatePreferencePage.2");//$NON-NLS-1$
69
private static final String DeleteValidator = Messages.getString("TemplatePreferencePage.3");//$NON-NLS-1$
70
private static final String DeleteShellMessage = Messages.getString("TemplatePreferencePage.4");//$NON-NLS-1$
75
private static final String TableToolTip = Messages.getString("TemplatePreferencePage.5");//$NON-NLS-1$
76
private static final String EditToolTip = Messages.getString("TemplatePreferencePage.6");//$NON-NLS-1$
77
private static final String DeleteToolTip = Messages.getString("TemplatePreferencePage.7");//$NON-NLS-1$
82
private static TemplateInputDialog inputDialog;
83
private static SharedDefaults sharedDefaults = SharedDefaults.getInstance();
88
private int columnWidth = 100;
89
private int columnWeight = 50;
90
private String columnNames[];
91
private static List<Element> sharedElementList;
92
private int attrListSize;
94
private ColumnLayoutData columnLayouts[] = { new ColumnWeightData(columnWeight, columnWidth),
95
new ColumnWeightData(columnWeight, columnWidth) };
98
* InfoHelp for SharedDefault
100
private String pageID;
101
private String SharedContextHelpID = "shared_defaults_help";//$NON-NLS-1$
104
* Button instance for ADD/EDIT/DELETE
107
private Button editButton;
108
private Button deleteButton;
111
* Checks for row(s) deletion
113
private static boolean isDeleted;
116
* Checks for redundant data
118
private boolean isRedundant;
121
* Takes isRedundant reference to get reflected in different class scope See
122
* TemplateInputDialog class
124
public static boolean isDup;
125
private static String delItemNames[] = null;
126
private static Table table;
129
* Add/Edit option values
131
protected static final int OPTION_ADD = 0;
132
protected static final int OPTION_EDIT = 1;
135
* Dialog input values arriving from TemplateInputDialog class
138
private String value;
141
* Constructor to initialize defaults
144
public TemplatePreferencePage() {
146
noDefaultAndApplyButton();
147
initializeDefaults();
151
* Sets the values of the Message Dialog
156
public TemplatePreferencePage(String aName, String aValue) {
162
* Sets default settings and gathers attributes from the XML for Table
166
private void initializeDefaults() {
167
columnNames = new String[] { Messages.getString("TemplatePreferencePage.10"), Messages.getString("TemplatePreferencePage.11") }; //$NON-NLS-1$//$NON-NLS-2$
168
// Setting InfoPop help (plugin-id+ContextID).
169
pageID = CUIPlugin.getPluginId() + "." + //$NON-NLS-1$
172
setTableAttributes();
177
* Creates controls on the Preference Page Adds the created Table and Button
178
* composite to the parent composite.
181
* @return subComposite
185
protected Control createContents(Composite parent) {
187
Composite composite = new Composite(parent, SWT.NONE);
188
GridLayout layout = new GridLayout();
189
layout.marginHeight = 0;
190
layout.marginWidth = 0;
191
composite.setLayout(layout);
193
Label pageLabel = new Label(composite, SWT.NONE);
194
pageLabel.setText(PageDescription);
196
Composite subComposite = new Composite(parent, SWT.NONE);
197
GridLayout subLayout = new GridLayout(2, false);
198
GridData gridData = new GridData(GridData.FILL_BOTH);
199
subComposite.setLayout(subLayout);
200
subComposite.setLayoutData(gridData);
202
addFirstSection(subComposite);
203
addSecondSection(subComposite);
205
// Info help for SharedDefault is displayed when Functional Key (F1) is
207
PlatformUI.getWorkbench().getHelpSystem().setHelp(super.getControl(), pageID);
213
* Adds table into the first composite present under parent Updates the
214
* Table with backend persistence data.
219
private void addFirstSection(Composite parent) {
221
setTableAttributes();
222
addXMLDataIntoTable();
226
* Creates second composite for buttons present under the parent
231
private void addSecondSection(Composite parent) {
232
Composite composite = createDefaultComposite(parent);
233
addButtonControls(composite);
237
* Creates default composite area for the Buttons
243
private Composite createDefaultComposite(Composite parent) {
245
Composite composite = new Composite(parent, SWT.NULL);
246
FillLayout layout = new FillLayout(SWT.VERTICAL);
248
layout.marginWidth = 0;
249
layout.marginHeight = 0;
250
composite.setLayout(layout);
252
GridData gridData = new GridData();
253
gridData.verticalAlignment = GridData.BEGINNING;
254
composite.setLayoutData(gridData);
261
* Creates Table with XML properties as its settings
266
private void createTable(Composite composite) {
268
table = new Table(composite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER
269
| SWT.NO_REDRAW_RESIZE);
271
GridData gridData = new GridData(GridData.FILL_BOTH);
272
gridData.heightHint = convertHeightInCharsToPixels(10);
273
gridData.widthHint = convertWidthInCharsToPixels(10);
275
table.setLayoutData(gridData);
276
TableLayout layout = new TableLayout();
277
table.setLayout(layout);
278
table.setLinesVisible(true);
279
table.setHeaderVisible(true);
280
table.setToolTipText(TableToolTip);
282
// The attribute size becomes zero when no data
283
// remains in the table. To avoid fault creation of
284
// the table the attribute size to required columns
286
if (attrListSize == 0) {
290
for (int nCols = 0; nCols < attrListSize; nCols++) {
291
layout.addColumnData(columnLayouts[nCols]);
292
TableColumn tColumn = new TableColumn(table, SWT.LEFT, nCols);
293
tColumn.setWidth(columnWidth);
294
tColumn.setText(columnNames[nCols]);
301
* Table listener added to enable EDIT/DELETE functionality only when table
302
* listenes to an event.
304
private void addTableListener() {
306
SelectionListener sListener = new SelectionAdapter() {
308
public void widgetSelected(SelectionEvent e) {
309
boolean isSelected = table.isSelected(table.getSelectionIndex());
310
int selectionCount = table.getSelectionCount();
312
// Enable EDIT/DELETE button when row(s) get selected
314
editButton.setEnabled(true);
315
deleteButton.setEnabled(true);
318
// disable EDIT button if more than one row is selected
319
if (selectionCount > 1)
320
editButton.setEnabled(false);
323
table.addSelectionListener(sListener);
327
* Adds XML backend data into the table Supports for pesistency.
330
private void addXMLDataIntoTable() {
331
for (int i = 0, l = sharedElementList.size(); i < l; i++) {
332
Element xmlElement = sharedElementList.get(i);
333
String name = xmlElement.getAttribute(TemplateEngineHelper.ID);
334
String value = xmlElement.getAttribute(TemplateEngineHelper.VALUE);
336
if (name.equals(Blank) && value.equals(Blank))
339
String backEndData[] = new String[] { name, value };
340
TableItem backEndItem = new TableItem(table, SWT.NONE);
342
for (int data = 0; data < backEndData.length; data++) {
343
if (backEndData[data] != null)
344
backEndItem.setText(data, backEndData[data]);
350
* Creates button controls on the first composite present under parent
351
* composite. Its aligned at rightmost end of Table and top of the second
357
private void addButtonControls(Composite composite) {
359
editButton = new Button(composite, SWT.PUSH);
360
editButton.setText(EditButton);
361
editButton.setEnabled(false);
362
editButton.setToolTipText(EditToolTip);
363
addButtonListener(editButton);
365
deleteButton = new Button(composite, SWT.PUSH);
366
deleteButton.setText(DeleteButton);
367
deleteButton.setEnabled(false);
368
deleteButton.setToolTipText(DeleteToolTip);
369
addButtonListener(deleteButton);
374
* Constructs button listeners to trigger specific functionality
378
public void addButtonListener(final Button button) {
380
inputDialog = new TemplateInputDialog(getShell());
381
SelectionListener listener = new SelectionAdapter() {
384
public void widgetSelected(SelectionEvent e) {
385
if (e.getSource().equals(editButton)) {
386
String editItemString = getSelectedItemNameFromTable();
387
if (editItemString != null) {
388
if (editItemString != Blank) {
389
inputDialog.open(inputDialog, OPTION_EDIT);
394
if (e.getSource().equals(deleteButton)) {
396
editButton.setEnabled(false);
397
deleteButton.setEnabled(false);
401
button.addSelectionListener(listener);
405
* Adding new data into the table and adds the same data to the backend XML
406
* Checks for duplicate entries and null values
409
public void addNewDataIntoTable() {
411
String addData[] = new String[] { name, value };
413
TableItem duplicateItems[] = table.getItems();
414
TableItem duplicateItem = null;
416
for (TableItem duplicateItem2 : duplicateItems) {
417
duplicateItem = duplicateItem2;
418
String duplicateString = duplicateItem.getText();
420
if (duplicateString.equals(name)) {
421
int result = inputDialog.popDuplicate();
422
if (result == SWT.OK) {
430
// Check if not redundant
432
TableItem tableItem = new TableItem(table, SWT.NONE);
434
for (int data = 0; data < addData.length; data++) {
435
tableItem.setText(data, addData[data]);
442
sharedDefaults.addToBackEndStorage(name, value);
446
* Gets the size of the Attributes of an Element of XML file
448
* @param sharedElementList
449
* @return attrListSize
451
private int getAttributeSize() {
453
int listSize = sharedElementList.size();
455
while (i < listSize) {
456
Element xmlElement = sharedElementList.get(i++);
457
attrListSize = xmlElement.getAttributes().getLength();
459
} catch (Exception exp) {
460
TemplateEngineUtil.log(exp);
466
* Setting the table attributes with the XML properties Sets XML-document
467
* Element List as the number of table rows and XML-document Attribute List
468
* as the number of table columns
471
private void setTableAttributes() {
474
SharedDefaults sharedTemp = new SharedDefaults();
475
sharedElementList = TemplateEngine.getChildrenOfElement(sharedTemp.document.getDocumentElement());
476
attrListSize = getAttributeSize();
477
sharedDefaults.putAll(sharedTemp.getSharedDefaultsMap());
479
} catch (Exception exp) {
480
TemplateEngineUtil.log(exp);
484
public void init(IWorkbench workbench) {
489
* Updating data with the changed value for a given ID in the table.
492
public void updateDataInTheTable() {
495
int selectedItemIndex = table.getSelectionIndex();
496
TableItem selectedItem = table.getItem(selectedItemIndex);
497
String updateString[] = new String[] { name, value };
498
selectedItem.setText(updateString);
499
sharedDefaults.updateToBackEndStorage(name, value);
500
} catch (Exception exp) {
501
TemplateEngineUtil.log(exp);
506
* Gives the item for the selected row in the table
508
* @return selectedItemName
511
public static String getSelectedItemNameFromTable() {
513
String selectedItemName = null;
514
int selectedItemIndex = 0;
517
selectedItemIndex = table.getSelectionIndex();
520
catch (Exception exp) {
521
TemplateEngineUtil.log(exp);
524
TableItem selectedItem = table.getItem(selectedItemIndex);
525
selectedItemName = selectedItem.getText();
526
return selectedItemName;
531
* Deletes the data for the specified row Data also gets deleted at the
532
* backend with Key-name as an identifier.
534
private void deleteRow() {
537
String nonEmptyItemString = getSelectedItemNameFromTable();
539
if (nonEmptyItemString != null)
540
result = confirmdeleteContents(nonEmptyItemString);
542
if (result == SWT.OK) {
543
int itemSelected[] = table.getSelectionIndices();
544
table.remove(itemSelected);
546
if (delItemNames != null) {
547
sharedDefaults.deleteBackEndStorage(delItemNames);
551
else if (result == SWT.CANCEL)
556
* Sets confirmation for the data deletion at the fronend and backend
558
* @param selectedItems
562
private int confirmdeleteContents(String selectedItems) {
565
TableItem deleteItems[] = null;
567
if (selectedItems != Blank) {
569
deleteItems = table.getSelection();
570
delItemNames = new String[deleteItems.length];
572
for (int nDel = 0; nDel < deleteItems.length; nDel++) {
574
TableItem item = deleteItems[nDel];
575
delItemNames[nDel] = item.getText();
579
MessageBox mBox = new MessageBox(getShell(), SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
580
mBox.setText(DeleteShellMessage);
581
mBox.setMessage(DeleteValidator);
582
result = mBox.open();