debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/AddEditFileToTransferDialog.java
changeset 0 fb279309251b
child 664 18b0c24adafe
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 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 package com.nokia.cdt.internal.debug.launch.ui;
       
    18 
       
    19 import java.io.File;
       
    20 
       
    21 import org.eclipse.core.runtime.IPath;
       
    22 import org.eclipse.core.runtime.Path;
       
    23 import org.eclipse.jface.dialogs.StatusDialog;
       
    24 import org.eclipse.swt.SWT;
       
    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.Point;
       
    30 import org.eclipse.swt.layout.GridData;
       
    31 import org.eclipse.swt.layout.GridLayout;
       
    32 import org.eclipse.swt.widgets.Button;
       
    33 import org.eclipse.swt.widgets.Composite;
       
    34 import org.eclipse.swt.widgets.Control;
       
    35 import org.eclipse.swt.widgets.FileDialog;
       
    36 import org.eclipse.swt.widgets.Label;
       
    37 import org.eclipse.swt.widgets.Shell;
       
    38 import org.eclipse.swt.widgets.Text;
       
    39 import org.eclipse.ui.PlatformUI;
       
    40 
       
    41 public class AddEditFileToTransferDialog extends StatusDialog {
       
    42 
       
    43 	private FileToTransfer fFile;
       
    44 	private Text hostPath;
       
    45 	private Button hostBrowse;
       
    46 	private Text targetPath;
       
    47 
       
    48 	/**
       
    49 	 * Create the dialog
       
    50 	 * @param parent
       
    51 	 * @param file
       
    52 	 */
       
    53 	public AddEditFileToTransferDialog(Shell parent, FileToTransfer file) {
       
    54 		super(parent);
       
    55 		setShellStyle(getShellStyle() | SWT.RESIZE);
       
    56 		fFile = file;
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * @see Windows#configureShell
       
    61 	 */
       
    62 	protected void configureShell(Shell newShell) {
       
    63 		super.configureShell(newShell);
       
    64 		PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, LaunchTabHelpIds.RUN_MODE_FILE_TRANSFER_DIALOG);
       
    65 	}		
       
    66 
       
    67 	/* (non-Javadoc)
       
    68 	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
       
    69 	 */
       
    70 	protected Control createDialogArea(Composite parent) {
       
    71 		initializeDialogUnits(parent);
       
    72 		Composite composite= (Composite) super.createDialogArea(parent);
       
    73 		
       
    74 		GridLayout layout = (GridLayout) composite.getLayout();
       
    75 		layout.numColumns = 2;
       
    76 		
       
    77 		final Label hostLabel = new Label(composite, SWT.NONE);
       
    78 		hostLabel.setText(Messages.getString("AddEditFileToTransferDialog.1")); //$NON-NLS-1$
       
    79 		GridData data = new GridData(GridData.FILL, GridData.CENTER, true, false);
       
    80 		data.horizontalSpan = 2;
       
    81 		hostLabel.setLayoutData(data);
       
    82 		hostLabel.setToolTipText(Messages.getString("AddEditFileToTransferDialog.2")); //$NON-NLS-1$
       
    83 
       
    84 		hostPath = new Text(composite, SWT.BORDER);
       
    85 		hostPath.setText(fFile.getHostPath());
       
    86 		data = new GridData(GridData.FILL, GridData.CENTER, true, false);
       
    87 		hostPath.setLayoutData(data);
       
    88 		hostPath.setToolTipText(Messages.getString("AddEditFileToTransferDialog.2")); //$NON-NLS-1$
       
    89 		hostPath.addModifyListener(new ModifyListener() {
       
    90 			public void modifyText(ModifyEvent e) {
       
    91 				checkValues();
       
    92 			}
       
    93 		});
       
    94 
       
    95 		hostBrowse = new Button(composite, SWT.PUSH);
       
    96 		hostBrowse.setFont(parent.getFont());
       
    97 		hostBrowse.setText(Messages.getString("AddEditFileToTransferDialog.3")); //$NON-NLS-1$
       
    98 		data = new GridData();
       
    99 		hostBrowse.setLayoutData(data);	
       
   100 		hostBrowse.addSelectionListener(new SelectionAdapter() {
       
   101 
       
   102 			public void widgetSelected(SelectionEvent evt) {
       
   103 				FileDialog dialog = new FileDialog(getShell(), SWT.NONE);
       
   104 
       
   105 				dialog.setText(Messages.getString("AddEditFileToTransferDialog.4")); //$NON-NLS-1$
       
   106 				dialog.setFilterExtensions(new String[] {"*"}); //$NON-NLS-1$
       
   107 				dialog.setFilterNames(new String[] {Messages.getString("AddEditFileToTransferDialog.11")}); //$NON-NLS-1$
       
   108 
       
   109 				String result = dialog.open();
       
   110 				if (result != null) {
       
   111 					IPath path = new Path(result);
       
   112 					if (path.toFile().exists()) {
       
   113 						hostPath.setText(result);
       
   114 						
       
   115 						// if target path is empty, set it to something useful here.
       
   116 						if (targetPath.getText().trim().length() < 1) {
       
   117 							String tp = "C:\\"; //$NON-NLS-1$
       
   118 							
       
   119 							if (path.getFileExtension().compareToIgnoreCase("app") == 0) { //$NON-NLS-1$
       
   120 								// this is an EKA1 app
       
   121 								tp += "system\\apps\\" + path.removeFirstSegments(path.segmentCount()-2).setDevice(null).toOSString(); //$NON-NLS-1$
       
   122 							} else if (path.toOSString().toLowerCase().indexOf("\\epoc32\\release\\") >= 0) { //$NON-NLS-1$
       
   123 								// this is a binary
       
   124 								tp += "sys\\bin\\" + path.lastSegment(); //$NON-NLS-1$
       
   125 							} else {
       
   126 								// see if this is a resource
       
   127 								final String dataZDir = "\\epoc32\\data\\z\\"; //$NON-NLS-1$
       
   128 								int index = path.toOSString().toLowerCase().indexOf(dataZDir);
       
   129 								if (index >= 0) {
       
   130 									tp += path.toOSString().substring(index + dataZDir.length());
       
   131 								} else {
       
   132 									// fallback - just add filename
       
   133 									tp += path.lastSegment();
       
   134 								}
       
   135 							}
       
   136 
       
   137 							targetPath.setText(tp);
       
   138 						}
       
   139 					}
       
   140 				}
       
   141 			}
       
   142 		});
       
   143 
       
   144 		final Label targetLabel = new Label(composite, SWT.NONE);
       
   145 		targetLabel.setText(Messages.getString("AddEditFileToTransferDialog.5")); //$NON-NLS-1$
       
   146 		data = new GridData(GridData.FILL, GridData.CENTER, true, false);
       
   147 		data.horizontalSpan = 2;
       
   148 		targetLabel.setLayoutData(data);
       
   149 		targetLabel.setToolTipText(Messages.getString("AddEditFileToTransferDialog.6")); //$NON-NLS-1$
       
   150 
       
   151 		targetPath = new Text(composite, SWT.BORDER);
       
   152 		targetPath.setText(fFile.getTargetPath());
       
   153 		data = new GridData(GridData.FILL, GridData.CENTER, true, false);
       
   154 		targetPath.setLayoutData(data);
       
   155 		targetPath.setToolTipText(Messages.getString("AddEditFileToTransferDialog.6")); //$NON-NLS-1$
       
   156 		targetPath.addModifyListener(new ModifyListener() {
       
   157 			public void modifyText(ModifyEvent e) {
       
   158 				checkValues();
       
   159 			}
       
   160 		});
       
   161 
       
   162 		applyDialogFont(composite);		
       
   163 
       
   164 		return composite;
       
   165 	}
       
   166 
       
   167 	public void create() {
       
   168 		super.create();
       
   169 		checkValues();
       
   170 	}
       
   171 
       
   172 	protected void okPressed() {
       
   173 		fFile.setHostPath(hostPath.getText());
       
   174 		fFile.setTargetPath(targetPath.getText());
       
   175 
       
   176 		super.okPressed();
       
   177 	}
       
   178 
       
   179 	/* (non-Javadoc)
       
   180 	 * @see org.eclipse.jface.window.Window#getInitialSize()
       
   181 	 */
       
   182 	protected Point getInitialSize() {
       
   183 		Point size = super.getInitialSize();
       
   184 		
       
   185 		// set the initial size for the add dialog.  note that when editing it
       
   186 		// automatically sizes it based on the length of the text in the edit boxes
       
   187 		if (hostPath.getText().trim().length() == 0) {
       
   188 			size.x *= 3;
       
   189 		}
       
   190 
       
   191 		return size;
       
   192 	}
       
   193 
       
   194 	protected void checkValues() {
       
   195 		StatusInfo status= new StatusInfo();
       
   196 		status.setOK();
       
   197 		
       
   198 		String hp = hostPath.getText().trim();
       
   199 		if (hp.length() > 0) {
       
   200 			if (!new File(hp).exists()) {
       
   201 				status.setError(Messages.getString("AddEditFileToTransferDialog.7")); //$NON-NLS-1$
       
   202 			}
       
   203 		}
       
   204 		else {
       
   205 			status.setError(Messages.getString("AddEditFileToTransferDialog.8")); //$NON-NLS-1$
       
   206 		}
       
   207 
       
   208 		// if we already have an error then no need to check the rest of the stuff
       
   209 		if (status.isOK()) {
       
   210 			String tp = targetPath.getText().trim();
       
   211 			if (tp.length() > 0) {
       
   212 				if (tp.length() < 3) {
       
   213 					status.setError(Messages.getString("AddEditFileToTransferDialog.9")); //$NON-NLS-1$					
       
   214 				} else {
       
   215 					char drive = tp.charAt(0);
       
   216 					char colon = tp.charAt(1);
       
   217 					if (!Character.isLetter(drive) || colon != ':') { 
       
   218 						status.setError(Messages.getString("AddEditFileToTransferDialog.9")); //$NON-NLS-1$
       
   219 					}
       
   220 					
       
   221 					// we really want the target file path, not just a directory.  but we do allow directories for
       
   222 					// backwards compatibility, but only when they end in a path delimiter.  so if the target path
       
   223 					// does not end in a path delimiter, and there is no file extension, warn them that we think
       
   224 					// this is just a file without an extension and not a directory.
       
   225 					IPath path = new Path(tp);
       
   226 					if (!path.hasTrailingSeparator() && path.getFileExtension() == null) {
       
   227 						status.setWarning(Messages.getString("AddEditFileToTransferDialog.12")); //$NON-NLS-1$
       
   228 					}
       
   229 				}
       
   230 			}
       
   231 			else {
       
   232 				status.setError(Messages.getString("AddEditFileToTransferDialog.10")); //$NON-NLS-1$
       
   233 			}
       
   234 		}
       
   235 
       
   236 		updateStatus(status);		
       
   237 	}
       
   238 
       
   239 }