testdev/ite/src/com.nokia.testfw.codegen.ui/src/com/nokia/testfw/codegen/ui/wizard/MethodEditDialog.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2  * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.testfw.codegen.ui.wizard;
       
    19 
       
    20 import java.util.Set;
       
    21 
       
    22 import org.eclipse.jface.resource.JFaceResources;
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.custom.CLabel;
       
    25 import org.eclipse.swt.events.ModifyEvent;
       
    26 import org.eclipse.swt.events.ModifyListener;
       
    27 import org.eclipse.swt.events.SelectionAdapter;
       
    28 import org.eclipse.swt.events.SelectionEvent;
       
    29 import org.eclipse.swt.graphics.Image;
       
    30 import org.eclipse.swt.graphics.Point;
       
    31 import org.eclipse.swt.graphics.Rectangle;
       
    32 import org.eclipse.swt.layout.GridData;
       
    33 import org.eclipse.swt.layout.GridLayout;
       
    34 import org.eclipse.swt.widgets.Button;
       
    35 import org.eclipse.swt.widgets.Dialog;
       
    36 import org.eclipse.swt.widgets.Display;
       
    37 import org.eclipse.swt.widgets.Label;
       
    38 import org.eclipse.swt.widgets.Shell;
       
    39 import org.eclipse.swt.widgets.Text;
       
    40 
       
    41 import com.nokia.testfw.codegen.ui.Messages;
       
    42 import com.nokia.testfw.codegen.model.INode;
       
    43 import com.nokia.testfw.codegen.model.MethodNodeImpl;
       
    44 import com.nokia.testfw.codegen.model.ClassNodeImpl;
       
    45 
       
    46 public class MethodEditDialog extends Dialog {
       
    47 	private static final Image ERROR_IMG = JFaceResources
       
    48 			.getImage("dialog_message_error_image");
       
    49 
       
    50 	private boolean result = false;
       
    51 	private String iTitle;
       
    52 	private MethodNodeImpl iTestMethod;
       
    53 	private ClassNodeImpl iTestClass;
       
    54 	private Text iTestMethodNameText;
       
    55 	private Button iBtnOK;
       
    56 	private CLabel iMessageLabel;
       
    57 	private String iMethodName;
       
    58 
       
    59 	private static final String NEWMETHOD = "TestMethod";
       
    60 
       
    61 	public MethodEditDialog(Shell parent, String title,
       
    62 			MethodNodeImpl testmethod) {
       
    63 		super(parent, 0);
       
    64 		iTitle = title;
       
    65 		if (testmethod != null) {
       
    66 			iTestMethod = testmethod;
       
    67 			iTestClass = (ClassNodeImpl) iTestMethod.getParent();
       
    68 		} else {
       
    69 			throw new IllegalArgumentException(
       
    70 					"Parameter testmethod can not be null.");
       
    71 		}
       
    72 	}
       
    73 
       
    74 	public MethodEditDialog(Shell parent, String title,
       
    75 			ClassNodeImpl parentClass) {
       
    76 		super(parent, 0);
       
    77 		iTitle = title;
       
    78 		if (parentClass != null) {
       
    79 			iTestClass = parentClass;
       
    80 			int counter = 1;
       
    81 			while (isDuplicate(NEWMETHOD + Integer.toString(counter))) {
       
    82 				counter++;
       
    83 			}
       
    84 			iTestMethod = new MethodNodeImpl(NEWMETHOD
       
    85 					+ Integer.toString(counter), parentClass);
       
    86 		} else {
       
    87 			throw new IllegalArgumentException(
       
    88 					"Parameter parentClass can not be null.");
       
    89 		}
       
    90 	}
       
    91 
       
    92 	private void init(final Shell shell) {
       
    93 		shell.setText(iTitle);
       
    94 		shell.setLayout(new GridLayout(4, true));
       
    95 		Label label = new Label(shell, SWT.None);
       
    96 		GridData data = new GridData(GridData.FILL_HORIZONTAL);
       
    97 		data.horizontalSpan = 1;
       
    98 		label.setLayoutData(data);
       
    99 		label.setText(Messages.getString("MethodDialog.MethodName"));
       
   100 
       
   101 		iTestMethodNameText = new Text(shell, SWT.BORDER);
       
   102 		data = new GridData(GridData.FILL_HORIZONTAL);
       
   103 		data.horizontalSpan = 3;
       
   104 		iTestMethodNameText.setLayoutData(data);
       
   105 		iTestMethodNameText.setText(iTestMethod.getName());
       
   106 		iTestMethodNameText.addModifyListener(new ModifyListener() {
       
   107 			public void modifyText(ModifyEvent event) {
       
   108 				canComplete(validation());
       
   109 			}
       
   110 		});
       
   111 		iTestMethodNameText.selectAll();
       
   112 
       
   113 		// Message Label
       
   114 		iMessageLabel = new CLabel(shell, SWT.NONE);
       
   115 		GridData messageLabelData = new GridData(GridData.FILL_HORIZONTAL);
       
   116 		messageLabelData.horizontalSpan = 4;
       
   117 		iMessageLabel.setLayoutData(messageLabelData);
       
   118 
       
   119 		label = new Label(shell, SWT.None);
       
   120 		data = new GridData(GridData.FILL_HORIZONTAL);
       
   121 		data.horizontalSpan = 2;
       
   122 		label.setLayoutData(data);
       
   123 
       
   124 		iBtnOK = new Button(shell, SWT.PUSH);
       
   125 		iBtnOK.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   126 		iBtnOK.setText(Messages.getString("Dialog.OK"));
       
   127 		iBtnOK.addSelectionListener(new SelectionAdapter() {
       
   128 			public void widgetSelected(SelectionEvent event) {
       
   129 				result = true;
       
   130 				iTestMethod.setName(iMethodName);
       
   131 				((Button) event.widget).getShell().dispose();
       
   132 			}
       
   133 		});
       
   134 
       
   135 		Button lBtnCancel = new Button(shell, SWT.PUSH);
       
   136 		lBtnCancel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   137 		lBtnCancel.setText(Messages.getString("Dialog.Cancel"));
       
   138 		lBtnCancel.addSelectionListener(new SelectionAdapter() {
       
   139 			public void widgetSelected(SelectionEvent event) {
       
   140 				result = false;
       
   141 				((Button) event.widget).getShell().dispose();
       
   142 			}
       
   143 		});
       
   144 
       
   145 		canComplete(validation());
       
   146 	}
       
   147 
       
   148 	public boolean open() {
       
   149 		Shell parent = getParent();
       
   150 		Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
       
   151 
       
   152 		init(shell);
       
   153 		shell.pack();
       
   154 
       
   155 		Rectangle lParentBounds = parent.getBounds();
       
   156 		Point lDialogSize = shell.getSize();
       
   157 		shell.setLocation(lParentBounds.x
       
   158 				+ (lParentBounds.width - lDialogSize.x) / 2, lParentBounds.y
       
   159 				+ (lParentBounds.height - lDialogSize.y) / 2);
       
   160 
       
   161 		shell.open();
       
   162 		Display display = parent.getDisplay();
       
   163 		while (!shell.isDisposed()) {
       
   164 			if (!display.readAndDispatch())
       
   165 				display.sleep();
       
   166 		}
       
   167 		return result;
       
   168 	}
       
   169 
       
   170 	private boolean isDuplicate(String name) {
       
   171 		Set<INode> lMethodSet = (Set<INode>) iTestClass.getChildren();
       
   172 		for (INode item : lMethodSet) {
       
   173 			if (item != iTestMethod) {
       
   174 				if (item.getName().equals(name)) {
       
   175 					return true;
       
   176 				}
       
   177 			}
       
   178 		}
       
   179 		return false;
       
   180 	}
       
   181 
       
   182 	private boolean validation() {
       
   183 		String text = iTestMethodNameText.getText();
       
   184 		if (!text.matches("^[a-zA-Z~_]\\w*")) {
       
   185 			showMessage(Messages.getString("MethodDialog.InvalidMethodName",
       
   186 					text));
       
   187 			return false;
       
   188 		}
       
   189 		if (isDuplicate(text)) {
       
   190 			showMessage(Messages.getString("MethodDialog.DuplicateMethodName",
       
   191 					text));
       
   192 			return false;
       
   193 		}
       
   194 		iMethodName = text;
       
   195 		showMessage(null);
       
   196 		return true;
       
   197 	}
       
   198 
       
   199 	private void canComplete(boolean validate) {
       
   200 		if (validate) {
       
   201 			iBtnOK.setEnabled(true);
       
   202 		} else {
       
   203 			iBtnOK.setEnabled(false);
       
   204 		}
       
   205 	}
       
   206 
       
   207 	private void showMessage(String message) {
       
   208 		if (message == null) {
       
   209 			iMessageLabel.setImage(null);
       
   210 			iMessageLabel.setText(null);
       
   211 		} else {
       
   212 			iMessageLabel.setImage(ERROR_IMG);
       
   213 			iMessageLabel.setText(message);
       
   214 		}
       
   215 	}
       
   216 
       
   217 	public MethodNodeImpl getMethodItem() {
       
   218 		return iTestMethod;
       
   219 	}
       
   220 }