sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/smtwidgets/FilterWidget.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 // ${file_name}
       
    15 // 
       
    16 //
       
    17 
       
    18 package com.symbian.smt.gui.smtwidgets;
       
    19 
       
    20 import org.eclipse.jface.dialogs.MessageDialog;
       
    21 import org.eclipse.swt.SWT;
       
    22 import org.eclipse.swt.events.KeyEvent;
       
    23 import org.eclipse.swt.events.KeyListener;
       
    24 import org.eclipse.swt.events.SelectionAdapter;
       
    25 import org.eclipse.swt.events.SelectionEvent;
       
    26 import org.eclipse.swt.layout.FillLayout;
       
    27 import org.eclipse.swt.layout.GridData;
       
    28 import org.eclipse.swt.layout.GridLayout;
       
    29 import org.eclipse.swt.layout.RowData;
       
    30 import org.eclipse.swt.layout.RowLayout;
       
    31 import org.eclipse.swt.widgets.Button;
       
    32 import org.eclipse.swt.widgets.Composite;
       
    33 import org.eclipse.swt.widgets.Label;
       
    34 import org.eclipse.swt.widgets.List;
       
    35 import org.eclipse.swt.widgets.Text;
       
    36 
       
    37 public class FilterWidget extends Composite {
       
    38 	private Text text = null;
       
    39 	private List list;
       
    40 	private Button modifyButton;
       
    41 
       
    42 	/**
       
    43 	 * Creates a FilterWidget composite object
       
    44 	 * 
       
    45 	 * @return void
       
    46 	 */
       
    47 	public FilterWidget(final Composite parent, int style) {
       
    48 		super(parent, style);
       
    49 
       
    50 		this.setLayout(new FillLayout());
       
    51 
       
    52 		final Composite gridLayoutComposite = new Composite(this, SWT.NONE);
       
    53 		final GridLayout gridLayout = new GridLayout();
       
    54 		gridLayout.numColumns = 2;
       
    55 		gridLayoutComposite.setLayout(gridLayout);
       
    56 
       
    57 		list = new List(gridLayoutComposite, SWT.BORDER);
       
    58 		final GridData gd_list = new GridData(SWT.FILL, SWT.FILL, true, true,
       
    59 				2, 1);
       
    60 		gd_list.widthHint = 439;
       
    61 		list.setLayoutData(gd_list);
       
    62 		list.addSelectionListener(new SelectionAdapter() {
       
    63 			public void widgetSelected(final SelectionEvent e) {
       
    64 				int index = list.getSelectionIndex();
       
    65 
       
    66 				if (index != -1) {
       
    67 					String filterSelected = list.getItem(index);
       
    68 					text.setText(filterSelected);
       
    69 					text.setFocus();
       
    70 					modifyButton.setEnabled(true);
       
    71 				}
       
    72 			}
       
    73 		});
       
    74 
       
    75 		final Label filterLabel = new Label(gridLayoutComposite, SWT.NONE);
       
    76 		filterLabel.setText("Filter");
       
    77 
       
    78 		text = new Text(gridLayoutComposite, SWT.BORDER);
       
    79 		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
       
    80 		text.setFocus();
       
    81 		text.addKeyListener(new KeyListener() {
       
    82 			public void keyPressed(KeyEvent e) {
       
    83 			}
       
    84 
       
    85 			public void keyReleased(KeyEvent e) {
       
    86 				if (text.getText().length() == 0) {
       
    87 					modifyButton.setEnabled(false);
       
    88 				}
       
    89 			}
       
    90 		});
       
    91 
       
    92 		final Composite buttonsComposite = new Composite(gridLayoutComposite,
       
    93 				SWT.NONE);
       
    94 		final RowLayout rowLayout = new RowLayout();
       
    95 		rowLayout.spacing = 30;
       
    96 		rowLayout.justify = true;
       
    97 		buttonsComposite.setLayout(rowLayout);
       
    98 		final GridData gd_buttonsComposite = new GridData(SWT.CENTER, SWT.FILL,
       
    99 				true, false, 2, 1);
       
   100 		buttonsComposite.setLayoutData(gd_buttonsComposite);
       
   101 
       
   102 		final Button addFilterButton = new Button(buttonsComposite, SWT.NONE);
       
   103 		final RowData rd_addFilterButton = new RowData();
       
   104 		rd_addFilterButton.width = 75;
       
   105 		addFilterButton.setLayoutData(rd_addFilterButton);
       
   106 		addFilterButton.setText("Add");
       
   107 		addFilterButton.addSelectionListener(new SelectionAdapter() {
       
   108 			public void widgetSelected(final SelectionEvent e) {
       
   109 				// Adds the text entered in the filter text to the list of
       
   110 				// filter items
       
   111 				String filter = text.getText().trim();
       
   112 
       
   113 				if (filter.length() > 0) {
       
   114 					// Check that text has been entered and that it does not
       
   115 					// already exist in the table
       
   116 					Boolean doesExistInList = false;
       
   117 
       
   118 					for (String s : list.getItems()) {
       
   119 						if (s.equals(filter)) {
       
   120 							doesExistInList = true;
       
   121 						}
       
   122 					}
       
   123 
       
   124 					if (!doesExistInList) {
       
   125 						// If it doesn't exist in the table then add it and
       
   126 						// clear the text box
       
   127 						list.add(filter);
       
   128 						text.setText("");
       
   129 						modifyButton.setEnabled(false);
       
   130 					} else {
       
   131 						MessageDialog
       
   132 								.openError(
       
   133 										parent.getShell(),
       
   134 										"Error",
       
   135 										filter
       
   136 												+ " already exists in the list of filters.");
       
   137 					}
       
   138 				}
       
   139 
       
   140 				text.setFocus();
       
   141 			}
       
   142 		});
       
   143 
       
   144 		final Button removeFilterButton = new Button(buttonsComposite, SWT.NONE);
       
   145 		final RowData rd_removeFilterButton = new RowData();
       
   146 		rd_removeFilterButton.width = 75;
       
   147 		removeFilterButton.setLayoutData(rd_removeFilterButton);
       
   148 		removeFilterButton.setText("Remove");
       
   149 		removeFilterButton.addSelectionListener(new SelectionAdapter() {
       
   150 			public void widgetSelected(SelectionEvent e) {
       
   151 				int listIndex = list.getSelectionIndex();
       
   152 
       
   153 				if (listIndex != -1) {
       
   154 					// If an item has been selected in the table then remove it
       
   155 					list.remove(listIndex);
       
   156 					text.setText("");
       
   157 					modifyButton.setEnabled(false);
       
   158 				} else {
       
   159 					MessageDialog.openInformation(parent.getShell(), "Info",
       
   160 							"You need to select an item to remove.");
       
   161 				}
       
   162 
       
   163 				text.setFocus();
       
   164 			}
       
   165 		});
       
   166 
       
   167 		modifyButton = new Button(buttonsComposite, SWT.NONE);
       
   168 		final RowData rd_modifyButton = new RowData();
       
   169 		rd_modifyButton.width = 75;
       
   170 		modifyButton.setLayoutData(rd_modifyButton);
       
   171 		modifyButton.setText("Modify");
       
   172 		modifyButton.setEnabled(false);
       
   173 		modifyButton.addSelectionListener(new SelectionAdapter() {
       
   174 			public void widgetSelected(final SelectionEvent e) {
       
   175 				// Modifies a table entry in place
       
   176 				int index = list.getSelectionIndex();
       
   177 
       
   178 				String filter = text.getText().trim();
       
   179 
       
   180 				if (filter.length() > 0) {
       
   181 					// Check filter text has been entered
       
   182 					Boolean doesExistInList = false;
       
   183 
       
   184 					for (String s : list.getItems()) {
       
   185 						// Check that the entry doesn't already exist in the
       
   186 						// table
       
   187 						if (s.equals(filter)) {
       
   188 							doesExistInList = true;
       
   189 						}
       
   190 					}
       
   191 
       
   192 					if (!doesExistInList) {
       
   193 						// If it doesn't already exist in the table then add it
       
   194 						list.remove(index);
       
   195 						list.add(filter, index);
       
   196 						text.setText("");
       
   197 						modifyButton.setEnabled(false);
       
   198 					}
       
   199 				}
       
   200 
       
   201 				text.setFocus();
       
   202 			}
       
   203 		});
       
   204 	}
       
   205 
       
   206 	/**
       
   207 	 * Returns the filter items
       
   208 	 * 
       
   209 	 * @return String[]
       
   210 	 */
       
   211 	public String[] getFilterItems() {
       
   212 		return list.getItems();
       
   213 	}
       
   214 
       
   215 	/**
       
   216 	 * Sets the filter items
       
   217 	 * 
       
   218 	 * @param filterItems
       
   219 	 *            A list containing filters.
       
   220 	 * @return void
       
   221 	 */
       
   222 	public void setFilterItems(String[] filterItems) {
       
   223 		if (filterItems != null)
       
   224 			list.setItems(filterItems);
       
   225 	}
       
   226 }