connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/wizard/ImportPage.java
changeset 0 fb279309251b
child 674 20c7966a3405
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2008 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 
       
    19 package com.nokia.carbide.remoteconnections.wizard;
       
    20 
       
    21 
       
    22 import com.nokia.carbide.remoteconnections.Messages;
       
    23 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    24 import com.nokia.carbide.remoteconnections.internal.registry.Reader;
       
    25 
       
    26 import org.eclipse.core.runtime.IPath;
       
    27 import org.eclipse.core.runtime.Path;
       
    28 import org.eclipse.jface.viewers.CheckStateChangedEvent;
       
    29 import org.eclipse.jface.viewers.ICheckStateListener;
       
    30 import org.eclipse.swt.SWT;
       
    31 import org.eclipse.swt.events.SelectionAdapter;
       
    32 import org.eclipse.swt.events.SelectionEvent;
       
    33 import org.eclipse.swt.widgets.*;
       
    34 
       
    35 import java.io.*;
       
    36 
       
    37 /**
       
    38  * Page in import wizard
       
    39  */
       
    40 public class ImportPage extends AbstractImportExportPage {
       
    41 
       
    42 	// the following two arrays need to correspond
       
    43 	static final String[] FILTER_EXTS  = { 
       
    44 		"*.xml", //$NON-NLS-1$
       
    45 		"*.*" //$NON-NLS-1$
       
    46 	};
       
    47 	static final String[] FILTER_EXT_NAMES  = { 
       
    48 		Messages.getString("ImportPage.XMLFilesLabel"), //$NON-NLS-1$
       
    49 		Messages.getString("ImportPage.AllFilesLabel") //$NON-NLS-1$
       
    50 	};
       
    51 	
       
    52 
       
    53 	protected ImportPage() {
       
    54 		super("ImportPage"); //$NON-NLS-1$
       
    55 		setTitle(Messages.getString("ImportPage.Title")); //$NON-NLS-1$
       
    56 		setDescription(Messages.getString("ImportPage.Description")); //$NON-NLS-1$
       
    57 	}
       
    58 
       
    59 	public void createControl(Composite parent) {
       
    60 		super.createControl(parent);
       
    61 		Composite composite = (Composite) getControl();
       
    62 
       
    63         createBrowseGroup(composite, Messages.getString("ImportPage.BrowseGroupLabel")); //$NON-NLS-1$
       
    64 	    browseButton.addSelectionListener(new SelectionAdapter() {
       
    65 			@Override
       
    66 			public void widgetSelected(SelectionEvent e) {
       
    67 				FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
       
    68 				fileDialog.setText(Messages.getString("ImportPage.FileDialogTitle")); //$NON-NLS-1$
       
    69 				fileDialog.setFilterExtensions(FILTER_EXTS);
       
    70 				fileDialog.setFilterNames(FILTER_EXT_NAMES);
       
    71 				String pathstr = fileDialog.open();
       
    72 				if (pathstr != null) {
       
    73 					pathText.setText(pathstr);
       
    74 				}
       
    75 			}
       
    76 	    });
       
    77         createViewerGroup(composite, Messages.getString("ImportPage.ViewerGroupLabel")); //$NON-NLS-1$
       
    78 		viewer.addCheckStateListener(new ICheckStateListener() {
       
    79 			public void checkStateChanged(CheckStateChangedEvent event) {
       
    80 				setPageComplete(validatePage(false));
       
    81 			}
       
    82 		});
       
    83 
       
    84         setPageComplete(validatePage(true));
       
    85 		RemoteConnectionsActivator.setHelp(composite, ".import_wizard"); //$NON-NLS-1$
       
    86 	}
       
    87 
       
    88 	protected boolean validatePage(boolean validateFile) {
       
    89 		setErrorMessage(null);
       
    90 		boolean valid = !validateFile;
       
    91 		if (validateFile) {
       
    92 			connections = null;
       
    93 			viewer.setInput(connections);
       
    94 			IPath path = new Path(pathText.getText());
       
    95 			File file = path.toFile();
       
    96 			if (file.exists()) {
       
    97 				try {
       
    98 					InputStream is = new FileInputStream(file); 
       
    99 					connections = Reader.readFromXML(RemoteConnectionsActivator.getConnectionTypeProvider(), is);
       
   100 					viewer.setInput(connections);
       
   101 					TableColumn[] columns = viewer.getTable().getColumns();
       
   102 					for (TableColumn tableColumn : columns) {
       
   103 						tableColumn.pack();
       
   104 					}
       
   105 					viewer.setAllChecked(true);
       
   106 					valid = true;
       
   107 				}
       
   108 				catch (Throwable t) {
       
   109 					// if we get an exception, it just means this was not a valid file
       
   110 				}
       
   111 			}
       
   112 		}
       
   113 		
       
   114 		if (!valid) {
       
   115 			setErrorMessage(Messages.getString("ImportPage.NoValidFileSelectedError")); //$NON-NLS-1$
       
   116 		}
       
   117 		else if (connections.isEmpty()) {
       
   118 			setErrorMessage(Messages.getString("ImportPage.NoConnectionsInFileError")); //$NON-NLS-1$
       
   119 			valid = false;
       
   120 		}
       
   121 		else if (viewer.getCheckedElements().length == 0) {
       
   122 			setErrorMessage(Messages.getString("ImportPage.NoConnectionsSelectedError")); //$NON-NLS-1$
       
   123 			valid = false;
       
   124 		}
       
   125 		
       
   126 		return valid;
       
   127 	}
       
   128 
       
   129 }