org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/dialogs/AptanaProjectSelectionDialog.java
changeset 51 a0e2f1f4e212
parent 50 0560e98b9bf6
parent 49 e64c52f5ee56
child 53 ba7c1ce1cc4a
equal deleted inserted replaced
50:0560e98b9bf6 51:a0e2f1f4e212
     1 package org.symbian.tools.wrttools.dialogs;
       
     2 
       
     3 import java.io.File;
       
     4 import java.text.MessageFormat;
       
     5 
       
     6 import org.eclipse.jface.dialogs.IDialogConstants;
       
     7 import org.eclipse.jface.dialogs.TitleAreaDialog;
       
     8 import org.eclipse.swt.SWT;
       
     9 import org.eclipse.swt.events.ModifyEvent;
       
    10 import org.eclipse.swt.events.ModifyListener;
       
    11 import org.eclipse.swt.events.SelectionAdapter;
       
    12 import org.eclipse.swt.events.SelectionEvent;
       
    13 import org.eclipse.swt.layout.GridData;
       
    14 import org.eclipse.swt.layout.GridLayout;
       
    15 import org.eclipse.swt.widgets.Button;
       
    16 import org.eclipse.swt.widgets.Composite;
       
    17 import org.eclipse.swt.widgets.Control;
       
    18 import org.eclipse.swt.widgets.DirectoryDialog;
       
    19 import org.eclipse.swt.widgets.Label;
       
    20 import org.eclipse.swt.widgets.Shell;
       
    21 import org.eclipse.swt.widgets.Text;
       
    22 import org.symbian.tools.wrttools.util.ProjectUtils;
       
    23 
       
    24 public class AptanaProjectSelectionDialog extends TitleAreaDialog {
       
    25 	private File project;
       
    26 	private Text location;
       
    27 
       
    28 	public AptanaProjectSelectionDialog(Shell parentShell) {
       
    29 		super(parentShell);
       
    30 	}
       
    31 
       
    32 	@Override
       
    33 	protected void configureShell(Shell newShell) {
       
    34 		super.configureShell(newShell);
       
    35 		newShell.setText("WebRuntime Tools");
       
    36 	}
       
    37 	
       
    38 	@Override
       
    39 	protected Control createDialogArea(Composite parent) {
       
    40 		setTitle("Select Aptana WRT Project");
       
    41 		setMessage("Select an Aptana WRT project or Aptana workspace containing one");
       
    42 		Composite root = (Composite) super.createDialogArea(parent);
       
    43 
       
    44 		Composite workingArea = new Composite(root, SWT.NONE);
       
    45 		workingArea.setLayoutData(new GridData(GridData.FILL_BOTH));
       
    46 
       
    47 		workingArea.setLayout(new GridLayout(2, false));
       
    48 		Label label = new Label(workingArea, SWT.NONE);
       
    49 		label.setLayoutData(new GridData(GridData.BEGINNING,
       
    50 				GridData.BEGINNING, false, false, 2, 1));
       
    51 		label.setText("Specify Aptana project or Aptana workspace location:");
       
    52 		location = new Text(workingArea, SWT.BORDER);
       
    53 		location.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    54 		location.addModifyListener(new ModifyListener() {
       
    55 			@Override
       
    56 			public void modifyText(ModifyEvent e) {
       
    57 				validate();
       
    58 			}
       
    59 		});
       
    60 
       
    61 		Button browse = new Button(workingArea, SWT.NONE);
       
    62 		browse.setText("Browse...");
       
    63 		browse.addSelectionListener(new SelectionAdapter() {
       
    64 			@Override
       
    65 			public void widgetSelected(SelectionEvent e) {
       
    66 				browse();
       
    67 			}
       
    68 		});
       
    69 
       
    70 		location.setText(ProjectUtils.getDefaultAptanaLocation());
       
    71 
       
    72 		return root;
       
    73 	}
       
    74 
       
    75 	@Override
       
    76 	protected void createButtonsForButtonBar(Composite parent) {
       
    77 		super.createButtonsForButtonBar(parent);
       
    78 		validate();
       
    79 		setErrorMessage(null);
       
    80 	}
       
    81 	
       
    82 	protected void browse() {
       
    83 		DirectoryDialog dialog = new DirectoryDialog(getShell());
       
    84 		dialog.setFilterPath(location.getText());
       
    85 		dialog.setMessage("Select Aptana WRT project or workspace");
       
    86 		dialog.setText("Web Runtime Tools");
       
    87 
       
    88 		String string = dialog.open();
       
    89 		if (string != null) {
       
    90 			location.setText(string);
       
    91 		}
       
    92 	}
       
    93 
       
    94 	protected void validate() {
       
    95 		String error = null;
       
    96 		File f = new File(location.getText());
       
    97 		if (f.isDirectory()) {
       
    98 			if (!ProjectUtils.isAptanaProject(f)) {
       
    99 				File[] files = f.listFiles();
       
   100 				f = null;
       
   101 				for (File file : files) {
       
   102 					if (ProjectUtils.isAptanaProject(file)) {
       
   103 						f = file;
       
   104 						break;
       
   105 					}
       
   106 				}
       
   107 				if (f == null) {
       
   108 					error = MessageFormat.format("{0} is not a WRT project or an Aptana workspace containing WRT projects", location.getText());
       
   109 				}
       
   110 			}
       
   111 		} else {
       
   112 			error = "Specified folder does not exist";
       
   113 		}
       
   114 		project = f;
       
   115 		setErrorMessage(error);
       
   116 		Button button = getButton(IDialogConstants.OK_ID);
       
   117 		if (button != null) {
       
   118 			button.setEnabled(error == null);
       
   119 		}
       
   120 	}
       
   121 
       
   122 	@Override
       
   123 	protected void cancelPressed() {
       
   124 		project = null;
       
   125 		super.cancelPressed();
       
   126 	}
       
   127 
       
   128 	public File getProject() {
       
   129 		return project;
       
   130 	}
       
   131 }