View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.wizards;
23  
24  import org.eclipse.jface.viewers.IStructuredSelection;
25  import org.eclipse.jface.wizard.Wizard;
26  import org.eclipse.ui.INewWizard;
27  import org.eclipse.ui.IWorkbench;
28  import org.eclipse.core.runtime.*;
29  import org.eclipse.jface.operation.*;
30  import java.lang.reflect.InvocationTargetException;
31  import org.eclipse.jface.dialogs.MessageDialog;
32  import org.eclipse.jface.viewers.ISelection;
33  import org.eclipse.core.resources.*;
34  import org.eclipse.core.runtime.CoreException;
35  import java.io.*;
36  import org.eclipse.ui.*;
37  import org.eclipse.ui.ide.IDE;
38  import org.fosstrak.llrp.commander.util.LLRPFactory;
39  import org.llrp.ltk.types.LLRPMessage;
40  
41  /**
42   * This wizard lets the user create a new LLRP message with all mandatory parameters set.
43   * 
44   * The implementation is based on Eclipse's "New File Wizard" template (see plug-in development environment).
45   *
46   * @author Ulrich Etter, ETHZ
47   *
48   */
49  public class NewLLRPMessageWizard extends Wizard implements INewWizard {
50  	
51  	private final String PLUGIN_ID = "llrp";
52  	
53  	private NewLLRPMessageWizardPage page;
54  	private ISelection selection;
55  
56  	/**
57  	 * Constructor for NewLLRPMessageWizard.
58  	 */
59  	public NewLLRPMessageWizard() {
60  		super();
61  		setNeedsProgressMonitor(true);
62  	}
63  	
64  	/**
65  	 * Adding the page to the wizard.
66  	 */
67  	public void addPages() {
68  		page = new NewLLRPMessageWizardPage(selection);
69  		addPage(page);
70  	}
71  
72  	/**
73  	 * This method is called when 'Finish' button is pressed in
74  	 * the wizard. We will create an operation and run it
75  	 * using wizard as execution context.
76  	 */
77  	public boolean performFinish() {
78  		final String folderName = page.getFolderName();
79  		final String fileName = page.getFileName();
80  		final String messageType = page.getLLRPMessageType();
81  		IRunnableWithProgress op = new IRunnableWithProgress() {
82  			public void run(IProgressMonitor monitor) throws InvocationTargetException {
83  				try {
84  					doFinish(folderName, fileName, messageType, monitor);
85  				} catch (CoreException e) {
86  					throw new InvocationTargetException(e);
87  				} finally {
88  					monitor.done();
89  				}
90  			}
91  		};
92  		try {
93  			getContainer().run(true, false, op);
94  		} catch (InterruptedException e) {
95  			return false;
96  		} catch (InvocationTargetException e) {
97  			Throwable realException = e.getTargetException();
98  			MessageDialog.openError(getShell(), "Error", realException.getMessage());
99  			return false;
100 		}
101 		return true;
102 	}
103 	
104 	/**
105 	 * The worker method. It will find the container, create the
106 	 * file if missing or just replace its contents, and open
107 	 * the editor on the newly created file.
108 	 */
109 	private void doFinish(
110 		String folderName,
111 		String fileName,
112 		String messageType, 
113 		IProgressMonitor monitor)
114 		throws CoreException {
115 		monitor.beginTask("Creating " + fileName, 2);
116 		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
117 		IResource resource = root.findMember(new Path(folderName));
118 		if (!resource.exists() || !(resource instanceof IContainer)) {
119 			throwCoreException("Container \"" + folderName + "\" does not exist.");
120 		}
121 		IContainer container = (IContainer) resource;
122 		final IFile file = container.getFile(new Path(fileName));
123 		try {
124 			InputStream stream = openContentStream(messageType);
125 			if (file.exists()) {
126 				file.setContents(stream, true, true, monitor);
127 			} else {
128 				file.create(stream, true, monitor);
129 			}
130 			stream.close();
131 		} catch (IOException e) {
132 		}
133 		monitor.worked(1);
134 		monitor.setTaskName("Opening file for editing...");
135 		getShell().getDisplay().asyncExec(new Runnable() {
136 			public void run() {
137 				IWorkbenchPage page =
138 					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
139 				try {
140 					IDE.openEditor(page, file, true);
141 				} catch (PartInitException e) {
142 				}
143 			}
144 		});
145 		monitor.worked(1);
146 	}
147 	
148 	/**
149 	 * Initializes file contents with a default LLRP message.
150 	 */
151 	private InputStream openContentStream(String messageType) {
152 		String contents = "";
153 		try {
154 			LLRPMessage message = LLRPFactory.createLLRPMessage(messageType);
155 			contents = message.toXMLString();
156 		} catch (Exception e) {
157 			e.printStackTrace();
158 		}
159 		return new ByteArrayInputStream(contents.getBytes());
160 	}
161 
162 	private void throwCoreException(String message) throws CoreException {
163 		IStatus status =
164 			new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, message, null);
165 		throw new CoreException(status);
166 	}
167 
168 	/**
169 	 * We will accept the selection in the workbench to see if
170 	 * we can initialize from it.
171 	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
172 	 */
173 	public void init(IWorkbench workbench, IStructuredSelection selection) {
174 		this.selection = selection;
175 	}
176 }