org.symbian.tools.mtw.ui/src/org/symbian/tools/mtw/internal/deployment/targets/LocalFileSystemPane.java
changeset 460 c0bff5ed874c
equal deleted inserted replaced
459:c278f0c8917f 460:c0bff5ed874c
       
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation 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  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.mtw.internal.deployment.targets;
       
    20 
       
    21 import java.io.File;
       
    22 
       
    23 import org.eclipse.core.resources.IResource;
       
    24 import org.eclipse.core.resources.ResourcesPlugin;
       
    25 import org.eclipse.core.runtime.IPath;
       
    26 import org.eclipse.core.runtime.IStatus;
       
    27 import org.eclipse.core.runtime.Path;
       
    28 import org.eclipse.core.runtime.Status;
       
    29 import org.eclipse.jface.layout.GridDataFactory;
       
    30 import org.eclipse.swt.SWT;
       
    31 import org.eclipse.swt.events.ModifyEvent;
       
    32 import org.eclipse.swt.events.ModifyListener;
       
    33 import org.eclipse.swt.events.SelectionAdapter;
       
    34 import org.eclipse.swt.events.SelectionEvent;
       
    35 import org.eclipse.swt.layout.GridData;
       
    36 import org.eclipse.swt.layout.GridLayout;
       
    37 import org.eclipse.swt.widgets.Button;
       
    38 import org.eclipse.swt.widgets.Composite;
       
    39 import org.eclipse.swt.widgets.Control;
       
    40 import org.eclipse.swt.widgets.FileDialog;
       
    41 import org.eclipse.swt.widgets.Label;
       
    42 import org.eclipse.swt.widgets.Text;
       
    43 import org.symbian.tools.mtw.ui.MTWCoreUI;
       
    44 import org.symbian.tools.mtw.ui.deployment.IDeploymentTarget;
       
    45 import org.symbian.tools.mtw.ui.deployment.ITargetDetailsPane;
       
    46 
       
    47 public class LocalFileSystemPane implements ITargetDetailsPane {
       
    48     private Composite root;
       
    49     private Text location;
       
    50     private FilesystemDeploymentTarget target;
       
    51     private Context page;
       
    52 
       
    53     public void init(Context page) {
       
    54         this.page = page;
       
    55     }
       
    56 
       
    57     public void setTarget(IDeploymentTarget target) {
       
    58         this.target = (FilesystemDeploymentTarget) target;
       
    59         IPath path = this.target.getPath();
       
    60         String string;
       
    61         if (path == null) {
       
    62             string = "";
       
    63         } else {
       
    64             string = path.toOSString();
       
    65         }
       
    66         location.setText(string);
       
    67         page.revalidate();
       
    68     }
       
    69 
       
    70     public void createControl(Composite parent) {
       
    71         root = new Composite(parent, SWT.NONE);
       
    72         root.setLayout(new GridLayout(2, false));
       
    73         Label label = new Label(root, SWT.NONE);
       
    74         label.setText("File name:");
       
    75         GridDataFactory.generate(label, 2, 1);
       
    76         location = new Text(root, SWT.BORDER);
       
    77         location.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    78         location.addModifyListener(new ModifyListener() {
       
    79             public void modifyText(ModifyEvent e) {
       
    80                 page.revalidate();
       
    81             }
       
    82         });
       
    83         final Button button = new Button(root, SWT.NONE);
       
    84         button.setText("Browse...");
       
    85         button.addSelectionListener(new SelectionAdapter() {
       
    86             @Override
       
    87             public void widgetSelected(SelectionEvent e) {
       
    88                 browse();
       
    89             }
       
    90         });
       
    91     }
       
    92 
       
    93     protected void browse() {
       
    94         final FileDialog dialog = new FileDialog(location.getShell(), SWT.SAVE);
       
    95         dialog.setText("Select Application Package Location");
       
    96         dialog.setFileName(target.getDefaultName());
       
    97         final IPath path = target.getPath();
       
    98         if (path != null) {
       
    99             dialog.setFilterPath(path.removeLastSegments(1).toOSString());
       
   100         }
       
   101         String newPath = dialog.open();
       
   102         if (newPath != null) {
       
   103             location.setText(newPath);
       
   104             page.revalidate();
       
   105         }
       
   106     }
       
   107 
       
   108     public IStatus validate() {
       
   109         target.setPath(null);
       
   110         String path = location.getText();
       
   111         if (path.trim().length() == 0) {
       
   112             return new Status(IStatus.ERROR, MTWCoreUI.PLUGIN_ID, "File name must be specified");
       
   113         } else {
       
   114             final File file = new File(path);
       
   115             if (!file.getParentFile().isDirectory()) {
       
   116                 return new Status(IStatus.ERROR, MTWCoreUI.PLUGIN_ID, String.format("%s is not a valid folder",
       
   117                         file.getParent()));
       
   118             } else {
       
   119                 IStatus status = ResourcesPlugin.getWorkspace().validateName(file.getName(), IResource.FILE);
       
   120                 if (status.getSeverity() == IStatus.ERROR) {
       
   121                     return status;
       
   122                 } else {
       
   123                     Path p = new Path(path.trim());
       
   124                     target.setPath(p);
       
   125                     if (p.toFile().exists()) {
       
   126                         return new Status(IStatus.WARNING, MTWCoreUI.PLUGIN_ID,
       
   127                                 "Target file already exists. It will be overwritten.");
       
   128                     } else {
       
   129                         return status;
       
   130                     }
       
   131                 }
       
   132             }
       
   133         }
       
   134     }
       
   135 
       
   136     public Control getControl() {
       
   137         return root;
       
   138     }
       
   139 
       
   140 }