sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/exportwizards/ExportSelectionPage.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 package com.symbian.smt.gui.exportwizards;
       
    17 
       
    18 import java.io.File;
       
    19 import java.io.FileInputStream;
       
    20 import java.io.FileNotFoundException;
       
    21 import java.io.FileOutputStream;
       
    22 import java.io.IOException;
       
    23 import java.nio.channels.FileChannel;
       
    24 
       
    25 import org.eclipse.core.resources.IFile;
       
    26 import org.eclipse.core.resources.IProject;
       
    27 import org.eclipse.core.resources.IWorkspace;
       
    28 import org.eclipse.core.resources.IWorkspaceRoot;
       
    29 import org.eclipse.core.resources.ProjectScope;
       
    30 import org.eclipse.core.resources.ResourcesPlugin;
       
    31 import org.eclipse.core.runtime.CoreException;
       
    32 import org.eclipse.core.runtime.preferences.IScopeContext;
       
    33 import org.eclipse.jface.dialogs.MessageDialog;
       
    34 import org.eclipse.jface.viewers.IStructuredSelection;
       
    35 import org.eclipse.jface.wizard.WizardPage;
       
    36 import org.eclipse.swt.SWT;
       
    37 import org.eclipse.swt.events.ModifyEvent;
       
    38 import org.eclipse.swt.events.ModifyListener;
       
    39 import org.eclipse.swt.events.SelectionAdapter;
       
    40 import org.eclipse.swt.events.SelectionEvent;
       
    41 import org.eclipse.swt.events.SelectionListener;
       
    42 import org.eclipse.swt.layout.FillLayout;
       
    43 import org.eclipse.swt.layout.GridData;
       
    44 import org.eclipse.swt.layout.GridLayout;
       
    45 import org.eclipse.swt.widgets.Button;
       
    46 import org.eclipse.swt.widgets.Composite;
       
    47 import org.eclipse.swt.widgets.FileDialog;
       
    48 import org.eclipse.swt.widgets.List;
       
    49 import org.eclipse.swt.widgets.Text;
       
    50 
       
    51 import com.symbian.smt.gui.Activator;
       
    52 import com.symbian.smt.gui.Logger;
       
    53 import com.symbian.smt.gui.PersistentDataStore;
       
    54 import com.symbian.smt.gui.nature.Nature;
       
    55 
       
    56 public class ExportSelectionPage extends WizardPage {
       
    57 	private Text text;
       
    58 	private List list;
       
    59 
       
    60 	/**
       
    61 	 * @param pageName
       
    62 	 */
       
    63 	protected ExportSelectionPage(IStructuredSelection selection) {
       
    64 		super("wizardPage");
       
    65 		setTitle("System Model Manager Export Wizard");
       
    66 		setDescription("Select the project you wish to export the diagram from, select the location to export to and then click finish");
       
    67 		setPageComplete(false);
       
    68 	}
       
    69 
       
    70 	private void canFinish() {
       
    71 		if (text.getText().trim().length() > 0
       
    72 				&& list.getSelectionIndex() != -1) {
       
    73 			setPageComplete(true);
       
    74 		} else {
       
    75 			setPageComplete(false);
       
    76 		}
       
    77 	}
       
    78 
       
    79 	public boolean copyFile() {
       
    80 		IWorkspace workspace = ResourcesPlugin.getWorkspace();
       
    81 		IWorkspaceRoot workspaceRoot = workspace.getRoot();
       
    82 
       
    83 		IProject project = workspaceRoot.getProject(list.getItem(list
       
    84 				.getSelectionIndex()));
       
    85 
       
    86 		IScopeContext projectScope = new ProjectScope(project);
       
    87 		PersistentDataStore store = new PersistentDataStore(projectScope
       
    88 				.getNode(Activator.PLUGIN_ID));
       
    89 
       
    90 		IFile projectFile = project.getFile(store.getOutputFilename());
       
    91 
       
    92 		File toFile = new File(text.getText());
       
    93 
       
    94 		if (toFile.isDirectory()) {
       
    95 			MessageDialog.openError(getShell(), "Error", toFile.toString()
       
    96 					+ " is a directory");
       
    97 			Logger.log(toFile.toString() + " is a directory");
       
    98 			return false;
       
    99 		}
       
   100 
       
   101 		if (!projectFile.exists()) {
       
   102 			MessageDialog.openError(getShell(), "Error", "The project "
       
   103 					+ list.getItem(list.getSelectionIndex())
       
   104 					+ " does not contain a System Model Diagram");
       
   105 			return false;
       
   106 		}
       
   107 
       
   108 		if (toFile.exists()) {
       
   109 			if (!MessageDialog.openConfirm(getShell(), "Overwrite?",
       
   110 					"Do you wish to overwrite the file " + toFile.toString()
       
   111 							+ "?")) {
       
   112 				return false;
       
   113 			}
       
   114 		}
       
   115 
       
   116 		try {
       
   117 			FileChannel in = new FileInputStream(projectFile.getRawLocation()
       
   118 					.toOSString()).getChannel();
       
   119 			FileChannel out = new FileOutputStream(text.getText()).getChannel();
       
   120 
       
   121 			try {
       
   122 				in.transferTo(0, in.size(), out);
       
   123 				in.close();
       
   124 				out.close();
       
   125 			} catch (IOException e) {
       
   126 				MessageDialog.openError(getShell(), "Error", e.getMessage());
       
   127 				Logger.log(e.getMessage(), e);
       
   128 				return false;
       
   129 			}
       
   130 
       
   131 		} catch (FileNotFoundException e) {
       
   132 			MessageDialog.openError(getShell(), "Error", e.getMessage());
       
   133 			Logger.log(e.getMessage(), e);
       
   134 			return false;
       
   135 		}
       
   136 
       
   137 		return true;
       
   138 	}
       
   139 
       
   140 	/*
       
   141 	 * (non-Javadoc)
       
   142 	 * 
       
   143 	 * @see
       
   144 	 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
       
   145 	 * .Composite)
       
   146 	 */
       
   147 	public void createControl(Composite parent) {
       
   148 		Composite container = new Composite(parent, SWT.NULL);
       
   149 		container.setLayout(new FillLayout(SWT.VERTICAL));
       
   150 
       
   151 		setControl(container);
       
   152 
       
   153 		final Composite composite = new Composite(container, SWT.NONE);
       
   154 		final GridLayout gridLayout = new GridLayout();
       
   155 		gridLayout.numColumns = 2;
       
   156 		composite.setLayout(gridLayout);
       
   157 
       
   158 		list = new List(composite, SWT.BORDER);
       
   159 		final GridData gd_list = new GridData(SWT.FILL, SWT.FILL, true, true,
       
   160 				2, 1);
       
   161 		list.setLayoutData(gd_list);
       
   162 		list.addSelectionListener(new SelectionListener() {
       
   163 			public void widgetDefaultSelected(SelectionEvent e) {
       
   164 			}
       
   165 
       
   166 			public void widgetSelected(SelectionEvent e) {
       
   167 				canFinish();
       
   168 			}
       
   169 		});
       
   170 
       
   171 		IWorkspace workspace = ResourcesPlugin.getWorkspace();
       
   172 		IWorkspaceRoot workspaceRoot = workspace.getRoot();
       
   173 
       
   174 		for (IProject project : workspaceRoot.getProjects()) {
       
   175 			if (project.isOpen()) {
       
   176 				try {
       
   177 					if (project.hasNature(Nature.ID)) {
       
   178 						list.add(project.getName());
       
   179 					}
       
   180 				} catch (CoreException e) {
       
   181 					Logger.log(e.getMessage(), e);
       
   182 					MessageDialog
       
   183 							.openError(getShell(), "Error", e.getMessage());
       
   184 				}
       
   185 			}
       
   186 		}
       
   187 
       
   188 		final Button browseButton = new Button(composite, SWT.NONE);
       
   189 		browseButton.setText("Browse");
       
   190 		browseButton.addSelectionListener(new SelectionAdapter() {
       
   191 			public void widgetSelected(SelectionEvent e) {
       
   192 				// Create and open a file window
       
   193 				FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
       
   194 
       
   195 				String[] filterNames = new String[1];
       
   196 
       
   197 				filterNames[0] = "*.svg";
       
   198 
       
   199 				dialog.setFilterExtensions(filterNames);
       
   200 
       
   201 				String filename = dialog.open();
       
   202 
       
   203 				if (filename != null) {
       
   204 					text.setText(filename);
       
   205 				}
       
   206 			}
       
   207 		});
       
   208 
       
   209 		text = new Text(composite, SWT.BORDER);
       
   210 		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
       
   211 		text.addModifyListener(new ModifyListener() {
       
   212 			public void modifyText(ModifyEvent e) {
       
   213 				canFinish();
       
   214 			}
       
   215 		});
       
   216 	}
       
   217 }