core/com.nokia.carbide.cpp.sdk.ui/src/com/nokia/carbide/cpp/sdk/ui/shared/AddSDKDialog.java
changeset 0 fb279309251b
child 677 d13345e153e8
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2007-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 the License "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.carbide.cpp.sdk.ui.shared;
       
    19 
       
    20 import java.io.File;
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.jface.dialogs.IDialogConstants;
       
    25 import org.eclipse.jface.dialogs.MessageDialog;
       
    26 import org.eclipse.jface.dialogs.TrayDialog;
       
    27 import org.eclipse.swt.SWT;
       
    28 import org.eclipse.swt.events.SelectionAdapter;
       
    29 import org.eclipse.swt.events.SelectionEvent;
       
    30 import org.eclipse.swt.events.SelectionListener;
       
    31 import org.eclipse.swt.graphics.Point;
       
    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.Combo;
       
    36 import org.eclipse.swt.widgets.Composite;
       
    37 import org.eclipse.swt.widgets.Control;
       
    38 import org.eclipse.swt.widgets.DirectoryDialog;
       
    39 import org.eclipse.swt.widgets.Label;
       
    40 import org.eclipse.swt.widgets.Shell;
       
    41 import org.eclipse.swt.widgets.Text;
       
    42 import org.eclipse.ui.PlatformUI;
       
    43 import org.osgi.framework.Version;
       
    44 
       
    45 import com.nokia.carbide.cpp.internal.sdk.ui.Messages;
       
    46 import com.nokia.carbide.cpp.internal.sdk.ui.SDKUIHelpIds;
       
    47 import com.nokia.carbide.cpp.sdk.core.ISDKManager;
       
    48 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    49 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    50 import com.nokia.carbide.cpp.sdk.core.SymbianSDKFactory;
       
    51 
       
    52 /**
       
    53  * Dialog that allows the user to add a new Symbian OS SDK or custkit
       
    54  * to the devices.xml file.
       
    55  */
       
    56 public class AddSDKDialog extends TrayDialog {
       
    57 
       
    58 	private Combo sdkVersionCombo;
       
    59 	private Combo osVersionCombo;
       
    60 	private Combo isDefaultCombo;
       
    61 	private Combo vendorCombo;
       
    62 	private Text deviceIDtext;
       
    63 	private Text epocRootText;
       
    64 	private Button browseEPOCROOTButton;
       
    65 	
       
    66 	public static final String DEFAULT_YES = "yes"; //$NON-NLS-1$
       
    67 	public static final String DEFAULT_NO  = "no"; //$NON-NLS-1$
       
    68 	public static final String AUTO_DETECT  = "(Auto-Detect)"; //$NON-NLS-1$
       
    69 	
       
    70 	/**
       
    71 	 * Create the dialog
       
    72 	 * @param parentShell
       
    73 	 */
       
    74 	public AddSDKDialog(Shell parentShell) {
       
    75 		super(parentShell);
       
    76 	}
       
    77 	
       
    78 	/* (non-Javadoc)
       
    79 	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
       
    80 	 */
       
    81 	protected void configureShell(Shell shell) {
       
    82 		super.configureShell(shell);
       
    83 		shell.setText(Messages.getString("AddSDKDialog.Add_New_SDK")); //$NON-NLS-1$
       
    84 	}
       
    85 	
       
    86 	@Override
       
    87 	protected void okPressed() {
       
    88 		
       
    89 		if (deviceIDtext.getText().length() > 0){
       
    90 			if (!isValidIDName(deviceIDtext.getText())){
       
    91 				return;
       
    92 			}
       
    93 			
       
    94 			if (vendorCombo.getText().length() > 0){
       
    95 				if (!isValidVendorName(vendorCombo.getText())){
       
    96 					return;
       
    97 				}
       
    98 			} else {
       
    99 				MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Missing_Vendor_Name"), Messages.getString("AddSDKDialog.Enter_Vendor_Name")); //$NON-NLS-1$ //$NON-NLS-2$
       
   100 				return;
       
   101 			}
       
   102 		} else {
       
   103 			MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Missing_SDK_ID"), Messages.getString("AddSDKDialog.Enter_SDK_ID")); //$NON-NLS-1$ //$NON-NLS-2$
       
   104 			return;
       
   105 		}
       
   106 		
       
   107 		if (epocRootText.getText().length() > 1){
       
   108 			if (epocRootText.getText().charAt(1) != ':'){
       
   109 				MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Malformed_EPOCROOT"), Messages.getString("AddSDKDialog.EPOCROOT_drive_spec")); //$NON-NLS-1$ //$NON-NLS-2$
       
   110 				return;
       
   111 			}
       
   112 			
       
   113 			if (epocRootText.getText().toUpperCase().endsWith("EPOC32") || epocRootText.getText().toUpperCase().endsWith("EPOC32\\")){ //$NON-NLS-1$ //$NON-NLS-2$
       
   114 				if (!MessageDialog.openConfirm(getShell(), Messages.getString("AddSDKDialog.Possible_wrong_epocroot"), Messages.getString("AddSDKDialog.EPOCROOT_Should_Point"))){ //$NON-NLS-1$ //$NON-NLS-2$
       
   115 					return;
       
   116 				}
       
   117 			} else {
       
   118 				File epocRootFile = new File(epocRootText.getText());
       
   119 				if (!epocRootFile.exists()){
       
   120 					if (!MessageDialog.openConfirm(getShell(), Messages.getString("AddSDKDialog.EPOCROOT_does_not_exist"), Messages.getString("AddSDKDialog.EPOCROOT_does_not_exist_msg"))){ //$NON-NLS-1$ //$NON-NLS-2$
       
   121 						return;
       
   122 					}
       
   123 				}
       
   124 				if (epocRootFile.toString().indexOf(" ") != -1){
       
   125 					// Paths should not have spaces...
       
   126 					MessageDialog.openError(getShell(), "EPOC Root Contains Spaces", Messages.getString("AddSDKDialog.EPOCROOT_contains_spaces")); //$NON-NLS-1$ //$NON-NLS-2$
       
   127 					return;
       
   128 					
       
   129 				}
       
   130 			}
       
   131 		} else {
       
   132 			if (epocRootText.getText().length() == 1 && epocRootText.getText().charAt(0) == '\\'){
       
   133 				// OK - Currently accept only a '\'. May need to reconsider side-effects
       
   134 			} else {
       
   135 				MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Missing_EPOCROOT"), Messages.getString("AddSDKDialog.Please_enter_an_EPOCROOT")); //$NON-NLS-1$ //$NON-NLS-2$
       
   136 				return;
       
   137 			}
       
   138 		}
       
   139 		
       
   140 		// No objections raised, write the new device entry
       
   141 		String osVersionText = osVersionCombo.getText();
       
   142 		Version osVersion;
       
   143 		String osBranch = ""; //$NON-NLS-1$
       
   144 		if (osVersionText.equals(AUTO_DETECT)){
       
   145 			osVersion = new Version("0.0"); //$NON-NLS-1$
       
   146 		} else {
       
   147 			int len = osVersionText.length();
       
   148 			if (Character.isLetter(osVersionText.charAt(len-1))){
       
   149 				osBranch = osVersionText.substring(len-1);
       
   150 				osVersionText = osVersionText.substring(0, len-1);
       
   151 				osVersion = new Version(osVersionText);
       
   152 			} else {
       
   153 				osVersion = new Version(osVersionText);
       
   154 			}
       
   155 		}
       
   156 		
       
   157 		Version sdkVersion = new Version("0.0"); //$NON-NLS-1$
       
   158 		if (!sdkVersionCombo.getText().equals(AUTO_DETECT)){
       
   159 			sdkVersion = new Version(sdkVersionCombo.getText());
       
   160 		}
       
   161 		
       
   162 		ISymbianSDK sdk = SymbianSDKFactory.createInstance(deviceIDtext.getText(), 
       
   163 														   epocRootText.getText(), 
       
   164 														   vendorCombo.getText(), 
       
   165 														   osVersion,
       
   166 														   osBranch,
       
   167 														   sdkVersion, 
       
   168 														   false);
       
   169 		
       
   170 		SDKCorePlugin.getSDKManager().addSDK(sdk);
       
   171 		
       
   172 		List<ISymbianSDK> sdkList = SDKCorePlugin.getSDKManager().getSDKList();
       
   173 		if (sdkList != null){
       
   174 			sdkList.add(sdk);
       
   175 		}
       
   176 		
       
   177 		if (isDefaultCombo.getText().equals(DEFAULT_YES)){
       
   178 			sdk.setIsDefaultSDK(true);
       
   179 			SDKCorePlugin.getSDKManager().setDefaultSDK(sdk);
       
   180 		} else {
       
   181 			sdk.setIsDefaultSDK(false);
       
   182 		}
       
   183 		
       
   184 		super.okPressed();
       
   185 	}
       
   186 	
       
   187 	private boolean isValidIDName(String sdkID){
       
   188 		boolean isValid = true;
       
   189 		
       
   190 		
       
   191 		// check for spaces in ID
       
   192 		if (sdkID.contains(" ")){ //$NON-NLS-1$
       
   193 			MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Invalid_SDK_ID"), Messages.getString("AddSDKDialog.SDK_ID_No_Spaces")); //$NON-NLS-1$ //$NON-NLS-2$
       
   194 			return false;
       
   195 		}
       
   196 		
       
   197 		// check that the ID is unique...
       
   198 		for (ISymbianSDK sdk : SDKCorePlugin.getSDKManager().getSDKList()){
       
   199 			if (sdk.getUniqueId().equalsIgnoreCase(sdkID)){
       
   200 				// id already exists, choose a different one...
       
   201 				MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Duplicate_ID"), Messages.getString("AddSDKDialog.Duplicate_ID_Msg")); //$NON-NLS-1$ //$NON-NLS-2$
       
   202 				return false;
       
   203 			}
       
   204 		}
       
   205 		
       
   206 		return isValid;
       
   207 	}
       
   208 	
       
   209 	private boolean isValidVendorName(String vendor){
       
   210 		boolean isValid = true;
       
   211 		
       
   212 		String[] vendorSplit = vendor.split("[.]"); //$NON-NLS-1$
       
   213 		if (vendorSplit.length == 3){ 
       
   214 			if (!vendorSplit[0].toLowerCase().startsWith("com")){ //$NON-NLS-1$
       
   215 			isValid = false;
       
   216 			}
       
   217 		} else {
       
   218 			isValid = false;
       
   219 		}
       
   220 		
       
   221 		if (isValid == false){
       
   222 			MessageDialog.openError(getShell(), Messages.getString("AddSDKDialog.Invalid_name_attrib"), Messages.getString("AddSDKDialog.Invalid_name_attrib_msg")); //$NON-NLS-1$ //$NON-NLS-2$
       
   223 		}
       
   224 			
       
   225 		return isValid;
       
   226 	}
       
   227 
       
   228 	/**
       
   229 	 * Create contents of the dialog
       
   230 	 * @param parent
       
   231 	 */
       
   232 	@Override
       
   233 	protected Control createDialogArea(Composite parent) {
       
   234 		Composite container = (Composite) super.createDialogArea(parent);
       
   235 		final GridLayout gridLayout = new GridLayout();
       
   236 		gridLayout.numColumns = 3;
       
   237 		container.setLayout(gridLayout);
       
   238 
       
   239 		final Label deviceIdLabel = new Label(container, SWT.NONE);
       
   240 		deviceIdLabel.setToolTipText(Messages.getString("AddSDKDialog.Enter_unique_id_for_sdk")); //$NON-NLS-1$
       
   241 		deviceIdLabel.setText(Messages.getString("AddSDKDialog.SDK_ID")); //$NON-NLS-1$
       
   242 
       
   243 		deviceIDtext = new Text(container, SWT.BORDER);
       
   244 		deviceIDtext.setLayoutData(new GridData(263, SWT.DEFAULT));
       
   245 		new Label(container, SWT.NONE);
       
   246 
       
   247 		final Label epocrootLabel = new Label(container, SWT.NONE);
       
   248 		epocrootLabel.setToolTipText(Messages.getString("AddSDKDialog.Enter_location_where_epoc32_folder")); //$NON-NLS-1$
       
   249 		epocrootLabel.setText("EPOCROOT:"); //$NON-NLS-1$
       
   250 
       
   251 		epocRootText = new Text(container, SWT.BORDER);
       
   252 		epocRootText.setLayoutData(new GridData(263, SWT.DEFAULT));
       
   253 
       
   254 		browseEPOCROOTButton = new Button(container, SWT.NONE);
       
   255 		browseEPOCROOTButton.setToolTipText(Messages.getString("AddSDKDialog.Choose_folder_where_epoc32_exists")); //$NON-NLS-1$
       
   256 		browseEPOCROOTButton.setText(Messages.getString("AddSDKDialog.Browse")); //$NON-NLS-1$
       
   257 		addButtonListener(browseEPOCROOTButton);
       
   258 		
       
   259 		final Label vendorLabel = new Label(container, SWT.NONE);
       
   260 		vendorLabel.setToolTipText(Messages.getString("AddSDKDialog.Enter_name_of_sdk")); //$NON-NLS-1$
       
   261 		vendorLabel.setText(Messages.getString("AddSDKDialog.Vendor")); //$NON-NLS-1$
       
   262 
       
   263 		vendorCombo = new Combo(container, SWT.NONE);
       
   264 		vendorCombo.setLayoutData(new GridData(250, SWT.DEFAULT));
       
   265 		new Label(container, SWT.NONE);
       
   266 		vendorCombo.add(ISymbianSDK.S60_SDK_NAME);
       
   267 		vendorCombo.add(ISymbianSDK.S80_SDK_NAME);
       
   268 		vendorCombo.add(ISymbianSDK.UIQ_SDK_NAME);
       
   269 		vendorCombo.add(ISymbianSDK.TECHVIEW_SDK_NAME);
       
   270 
       
   271 		final Label isDefaultLabel = new Label(container, SWT.NONE);
       
   272 		isDefaultLabel.setToolTipText(Messages.getString("AddSDKDialog.default_label_tooltip")); //$NON-NLS-1$
       
   273 		isDefaultLabel.setText(Messages.getString("AddSDKDialog.is_default")); //$NON-NLS-1$
       
   274 
       
   275 		isDefaultCombo = new Combo(container, SWT.READ_ONLY);
       
   276 		isDefaultCombo.setLayoutData(new GridData(250, SWT.DEFAULT));
       
   277 		new Label(container, SWT.NONE);
       
   278 		isDefaultCombo.add(DEFAULT_YES);
       
   279 		isDefaultCombo.add(DEFAULT_NO);
       
   280 		isDefaultCombo.select(1);
       
   281 
       
   282 		final Label osVersionLabel = new Label(container, SWT.NONE);
       
   283 		osVersionLabel.setToolTipText(Messages.getString("AddSDKDialog.os_version_tooltip")); //$NON-NLS-1$
       
   284 		osVersionLabel.setText(Messages.getString("AddSDKDialog.os_version")); //$NON-NLS-1$
       
   285 		//ISymbianMacroStore sms = 
       
   286 		List<String> osVersions = new ArrayList<String>();
       
   287 		ISDKManager sdkMgr = SDKCorePlugin.getSDKManager();
       
   288 		osVersions = sdkMgr.getSymbianMacroStore().getSupportedOSVersions();
       
   289 
       
   290 		osVersionCombo = new Combo(container, SWT.READ_ONLY);
       
   291 		osVersionCombo.setLayoutData(new GridData(250, SWT.DEFAULT));
       
   292 		new Label(container, SWT.NONE);
       
   293 		osVersionCombo.add(AUTO_DETECT);
       
   294 		
       
   295 		for (String currVer : osVersions){
       
   296 			osVersionCombo.add(currVer);
       
   297 		}
       
   298 		osVersionCombo.select(0);
       
   299 
       
   300 		final Label sdkVersionLabel = new Label(container, SWT.NONE);
       
   301 		sdkVersionLabel.setToolTipText(Messages.getString("AddSDKDialog.select_sdk_tooltip")); //$NON-NLS-1$
       
   302 		sdkVersionLabel.setText(Messages.getString("AddSDKDialog.sdk_version")); //$NON-NLS-1$
       
   303 
       
   304 		sdkVersionCombo = new Combo(container, SWT.READ_ONLY);
       
   305 		sdkVersionCombo.setLayoutData(new GridData(250, SWT.DEFAULT));
       
   306 		sdkVersionCombo.add(AUTO_DETECT);
       
   307 		sdkVersionCombo.select(0);
       
   308 		
       
   309 		List<String> sdkVerList = new ArrayList<String>();
       
   310 		sdkVerList = sdkMgr.getSymbianMacroStore().getSDKVersions();
       
   311 		for (String currVer : sdkVerList){
       
   312 			sdkVersionCombo.add(currVer);
       
   313 		}
       
   314 		
       
   315 		new Label(container, SWT.NONE);
       
   316 		
       
   317 		PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, SDKUIHelpIds.SDK_ADD_DIALOG);
       
   318 
       
   319 		
       
   320 		return container;
       
   321 	}
       
   322 
       
   323 	/**
       
   324 	 * Create contents of the button bar
       
   325 	 * @param parent
       
   326 	 */
       
   327 	@Override
       
   328 	protected void createButtonsForButtonBar(Composite parent) {
       
   329 		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
       
   330 				true);
       
   331 		createButton(parent, IDialogConstants.CANCEL_ID,
       
   332 				IDialogConstants.CANCEL_LABEL, false);
       
   333 	}
       
   334 
       
   335 	/**
       
   336 	 * Return the initial size of the dialog
       
   337 	 */
       
   338 	@Override
       
   339 	protected Point getInitialSize() {
       
   340 		//return new Point(428, 256);
       
   341 		return super.getInitialSize();
       
   342 	}
       
   343 	
       
   344 	/**
       
   345 	 * Sets the listener event to a button.
       
   346 	 * 
       
   347 	 * @param aButton
       
   348 	 */
       
   349 	private void addButtonListener( final Button aButton ) {
       
   350 		SelectionListener listener = new SelectionAdapter() {
       
   351 			public void widgetSelected( SelectionEvent e )  {
       
   352 				if (e.getSource().equals(browseEPOCROOTButton)) {
       
   353 					browseEPOCROOT();
       
   354 				}
       
   355 			}
       
   356 		};
       
   357 		aButton.addSelectionListener(listener);
       
   358 	}
       
   359 	
       
   360 	private void browseEPOCROOT(){
       
   361         DirectoryDialog browseDir = new DirectoryDialog(getShell(), SWT.OPEN);
       
   362         browseDir.setMessage(Messages.getString("AddSDKDialog.Choose_location_for_EPOCROOT")); //$NON-NLS-1$
       
   363         File symbianFolder = new File("C:/Symbian/"); //$NON-NLS-1$
       
   364         if (symbianFolder.exists()){
       
   365         	browseDir.setFilterPath(symbianFolder.toString());
       
   366         }
       
   367         
       
   368         String dirText = browseDir.open();
       
   369         if (dirText != null){
       
   370         	epocRootText.setText(dirText);
       
   371         }
       
   372 	}
       
   373 
       
   374 }