sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/smtwidgets/IgnoreWidget.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 package com.symbian.smt.gui.smtwidgets;
       
    17 
       
    18 import java.util.ArrayList;
       
    19 import java.util.List;
       
    20 
       
    21 import org.eclipse.jface.dialogs.MessageDialog;
       
    22 import org.eclipse.swt.SWT;
       
    23 import org.eclipse.swt.events.KeyEvent;
       
    24 import org.eclipse.swt.events.KeyListener;
       
    25 import org.eclipse.swt.events.SelectionAdapter;
       
    26 import org.eclipse.swt.events.SelectionEvent;
       
    27 import org.eclipse.swt.layout.FillLayout;
       
    28 import org.eclipse.swt.layout.GridData;
       
    29 import org.eclipse.swt.layout.GridLayout;
       
    30 import org.eclipse.swt.layout.RowData;
       
    31 import org.eclipse.swt.layout.RowLayout;
       
    32 import org.eclipse.swt.widgets.Button;
       
    33 import org.eclipse.swt.widgets.Combo;
       
    34 import org.eclipse.swt.widgets.Composite;
       
    35 import org.eclipse.swt.widgets.Label;
       
    36 import org.eclipse.swt.widgets.Table;
       
    37 import org.eclipse.swt.widgets.TableColumn;
       
    38 import org.eclipse.swt.widgets.TableItem;
       
    39 import org.eclipse.swt.widgets.Text;
       
    40 
       
    41 public class IgnoreWidget extends Composite {
       
    42 	private Combo itemTypeCombo;
       
    43 	private Text itemNameText;
       
    44 	private Table ignoreItemsTable;
       
    45 	private Button modifyButton;
       
    46 
       
    47 	/**
       
    48 	 * Creates an IgnoreWidget composite object
       
    49 	 * 
       
    50 	 * @return void
       
    51 	 */
       
    52 	public IgnoreWidget(final Composite parent, int style) {
       
    53 		super(parent, style);
       
    54 
       
    55 		this.setLayout(new FillLayout());
       
    56 
       
    57 		final Composite gridLayoutComposite = new Composite(this, SWT.NONE);
       
    58 		final GridLayout gridLayout = new GridLayout();
       
    59 		gridLayout.numColumns = 4;
       
    60 		gridLayoutComposite.setLayout(gridLayout);
       
    61 
       
    62 		ignoreItemsTable = new Table(gridLayoutComposite, SWT.FULL_SELECTION
       
    63 				| SWT.BORDER);
       
    64 		ignoreItemsTable.setHeaderVisible(true);
       
    65 		ignoreItemsTable.setLinesVisible(true);
       
    66 		final GridData gd_ignoreItemsTable = new GridData(SWT.FILL, SWT.FILL,
       
    67 				true, true, 4, 1);
       
    68 		ignoreItemsTable.setLayoutData(gd_ignoreItemsTable);
       
    69 		ignoreItemsTable.addSelectionListener(new SelectionAdapter() {
       
    70 			public void widgetSelected(final SelectionEvent e) {
       
    71 				// When an item in the table is selected populate the item type
       
    72 				// combo and the
       
    73 				// item name text with the relevant information
       
    74 				TableItem itemSelected = ignoreItemsTable
       
    75 						.getItem(ignoreItemsTable.getSelectionIndex());
       
    76 
       
    77 				int index = 0;
       
    78 
       
    79 				for (String item : itemTypeCombo.getItems()) {
       
    80 					if (item.equals(itemSelected.getText(0))) {
       
    81 						itemTypeCombo.select(index);
       
    82 					}
       
    83 					index++;
       
    84 				}
       
    85 
       
    86 				itemNameText.setText(itemSelected.getText(1));
       
    87 				itemNameText.setFocus();
       
    88 				modifyButton.setEnabled(true);
       
    89 			}
       
    90 		});
       
    91 
       
    92 		final TableColumn itemTypeTableColumn = new TableColumn(
       
    93 				ignoreItemsTable, SWT.NONE);
       
    94 		itemTypeTableColumn.setWidth(100);
       
    95 		itemTypeTableColumn.setText("Item Type");
       
    96 
       
    97 		final TableColumn itemNameTableColumn = new TableColumn(
       
    98 				ignoreItemsTable, SWT.NONE);
       
    99 		itemNameTableColumn.setWidth(269);
       
   100 		itemNameTableColumn.setText("Item Name");
       
   101 
       
   102 		final Label itemTypeLabel = new Label(gridLayoutComposite, SWT.NONE);
       
   103 		itemTypeLabel.setText("Item Type");
       
   104 
       
   105 		itemTypeCombo = new Combo(gridLayoutComposite, SWT.READ_ONLY);
       
   106 		itemTypeCombo.setLayoutData(new GridData());
       
   107 		itemTypeCombo.setItems(new String[] { "layer", "block", "subblock",
       
   108 				"collection", "component" });
       
   109 		itemTypeCombo.select(0);
       
   110 
       
   111 		final Label itemNameLabel = new Label(gridLayoutComposite, SWT.NONE);
       
   112 		itemNameLabel.setLayoutData(new GridData());
       
   113 		itemNameLabel.setText("Item Name");
       
   114 
       
   115 		itemNameText = new Text(gridLayoutComposite, SWT.BORDER);
       
   116 		final GridData gd_itemNameText = new GridData(SWT.FILL, SWT.CENTER,
       
   117 				true, false);
       
   118 		itemNameText.setLayoutData(gd_itemNameText);
       
   119 		itemNameText.addKeyListener(new KeyListener() {
       
   120 			public void keyPressed(KeyEvent e) {
       
   121 			}
       
   122 
       
   123 			public void keyReleased(KeyEvent e) {
       
   124 				if (itemNameText.getText().length() == 0) {
       
   125 					modifyButton.setEnabled(false);
       
   126 				}
       
   127 			}
       
   128 		});
       
   129 
       
   130 		final Composite buttonsComposite = new Composite(gridLayoutComposite,
       
   131 				SWT.NONE);
       
   132 		final GridData gd_buttonsComposite = new GridData(SWT.CENTER,
       
   133 				SWT.CENTER, false, false, 4, 1);
       
   134 		buttonsComposite.setLayoutData(gd_buttonsComposite);
       
   135 		final RowLayout rowLayout = new RowLayout();
       
   136 		rowLayout.spacing = 30;
       
   137 		buttonsComposite.setLayout(rowLayout);
       
   138 
       
   139 		final Button addButton = new Button(buttonsComposite, SWT.NONE);
       
   140 		addButton.addSelectionListener(new SelectionAdapter() {
       
   141 			public void widgetSelected(final SelectionEvent e) {
       
   142 				// Check to see if an item had been selected and item text has
       
   143 				// been entered
       
   144 				if (itemTypeCombo.getText().length() > 0
       
   145 						&& itemNameText.getText().trim().length() > 0) {
       
   146 
       
   147 					// Check that combination of type and text doesn't already
       
   148 					// exist in the table
       
   149 					Boolean alreadyExistsInTable = false;
       
   150 
       
   151 					for (TableItem item : ignoreItemsTable.getItems()) {
       
   152 						if (item.getText(0).equals(itemTypeCombo.getText())
       
   153 								&& item.getText(1).equalsIgnoreCase(
       
   154 										itemNameText.getText().trim())) {
       
   155 							alreadyExistsInTable = true;
       
   156 						}
       
   157 					}
       
   158 
       
   159 					// Add to the table if does not already exist
       
   160 					if (!alreadyExistsInTable) {
       
   161 						final TableItem newItemTableItem = new TableItem(
       
   162 								ignoreItemsTable, SWT.BORDER);
       
   163 						newItemTableItem.setText(0, itemTypeCombo.getText());
       
   164 						newItemTableItem.setText(1, itemNameText.getText());
       
   165 
       
   166 						// Clear the text entry
       
   167 						itemNameText.setText("");
       
   168 						modifyButton.setEnabled(false);
       
   169 					} else {
       
   170 						MessageDialog
       
   171 								.openError(
       
   172 										parent.getShell(),
       
   173 										"Error",
       
   174 										itemTypeCombo.getText()
       
   175 												+ " "
       
   176 												+ itemNameText.getText()
       
   177 												+ " already exists in the list of items to ignore.");
       
   178 					}
       
   179 				}
       
   180 
       
   181 				itemNameText.setFocus();
       
   182 			}
       
   183 		});
       
   184 
       
   185 		final RowData rd_addButton = new RowData();
       
   186 		rd_addButton.width = 75;
       
   187 		addButton.setLayoutData(rd_addButton);
       
   188 		addButton.setText("Add");
       
   189 
       
   190 		final Button removeButton = new Button(buttonsComposite, SWT.NONE);
       
   191 		removeButton.addSelectionListener(new SelectionAdapter() {
       
   192 			public void widgetSelected(final SelectionEvent e) {
       
   193 				if (ignoreItemsTable.getSelectionIndex() >= 0) {
       
   194 					ignoreItemsTable.remove(ignoreItemsTable
       
   195 							.getSelectionIndex());
       
   196 
       
   197 					// Clear the text entry
       
   198 					itemNameText.setText("");
       
   199 					modifyButton.setEnabled(false);
       
   200 				} else {
       
   201 					MessageDialog
       
   202 							.openInformation(parent.getShell(), "Info",
       
   203 									"You need to select an item from the list before you can remove it.");
       
   204 				}
       
   205 
       
   206 				itemNameText.setFocus();
       
   207 			}
       
   208 		});
       
   209 
       
   210 		final RowData rd_removeButton = new RowData();
       
   211 		rd_removeButton.width = 75;
       
   212 		removeButton.setLayoutData(rd_removeButton);
       
   213 		removeButton.setText("Remove");
       
   214 
       
   215 		modifyButton = new Button(buttonsComposite, SWT.NONE);
       
   216 		modifyButton.addSelectionListener(new SelectionAdapter() {
       
   217 			public void widgetSelected(final SelectionEvent e) {
       
   218 				// Check to see if an item had been selected and item text has
       
   219 				// been entered
       
   220 				if (itemNameText.getText().length() > 0) {
       
   221 
       
   222 					// Check that combination of type and text doesn't already
       
   223 					// exist in the table
       
   224 					Boolean alreadyExistsInTable = false;
       
   225 
       
   226 					for (TableItem item : ignoreItemsTable.getItems()) {
       
   227 						if (item.getText(0).equals(itemTypeCombo.getText())
       
   228 								&& item.getText(1).equals(
       
   229 										itemNameText.getText())) {
       
   230 							alreadyExistsInTable = true;
       
   231 						}
       
   232 					}
       
   233 
       
   234 					// Add to the table if does not already exist
       
   235 					if (!alreadyExistsInTable) {
       
   236 						TableItem itemSelected = ignoreItemsTable
       
   237 								.getItem(ignoreItemsTable.getSelectionIndex());
       
   238 
       
   239 						itemSelected.setText(0, itemTypeCombo.getText());
       
   240 						itemSelected.setText(1, itemNameText.getText());
       
   241 
       
   242 						// Clear the text entry
       
   243 						itemNameText.setText("");
       
   244 						modifyButton.setEnabled(false);
       
   245 					}
       
   246 				}
       
   247 
       
   248 				itemNameText.setFocus();
       
   249 			}
       
   250 		});
       
   251 
       
   252 		final RowData rd_modifyButton = new RowData();
       
   253 		rd_modifyButton.width = 75;
       
   254 		modifyButton.setLayoutData(rd_modifyButton);
       
   255 		modifyButton.setText("Modify");
       
   256 		modifyButton.setEnabled(false);
       
   257 	}
       
   258 
       
   259 	/**
       
   260 	 * Returns a list of the ignore items
       
   261 	 * 
       
   262 	 * @return List<String>
       
   263 	 */
       
   264 	public List<String[]> getIgnoreItems() {
       
   265 		ArrayList<String[]> ignoreItems = new ArrayList<String[]>();
       
   266 
       
   267 		for (TableItem item : ignoreItemsTable.getItems()) {
       
   268 			String[] itemData = { item.getText(0), item.getText(1) };
       
   269 			ignoreItems.add(itemData);
       
   270 		}
       
   271 
       
   272 		return ignoreItems;
       
   273 	}
       
   274 
       
   275 	/**
       
   276 	 * Sets the ignore items
       
   277 	 * 
       
   278 	 * @param ignoreItems
       
   279 	 *            An ArrayList containing 2 element lists. The first element
       
   280 	 *            contains the item type and the second element contains the
       
   281 	 *            item text.
       
   282 	 * @return void
       
   283 	 */
       
   284 	public void setIgnoreItems(List<String[]> ignoreItems) {
       
   285 		ignoreItemsTable.removeAll();
       
   286 		for (String[] ignoreItem : ignoreItems) {
       
   287 			final TableItem newItemTableItem = new TableItem(ignoreItemsTable,
       
   288 					SWT.BORDER);
       
   289 			newItemTableItem.setText(0, ignoreItem[0]);
       
   290 			newItemTableItem.setText(1, ignoreItem[1]);
       
   291 		}
       
   292 	}
       
   293 }