1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
43
44
45
46
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
58
59 public NewLLRPMessageWizard() {
60 super();
61 setNeedsProgressMonitor(true);
62 }
63
64
65
66
67 public void addPages() {
68 page = new NewLLRPMessageWizardPage(selection);
69 addPage(page);
70 }
71
72
73
74
75
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
106
107
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
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
170
171
172
173 public void init(IWorkbench workbench, IStructuredSelection selection) {
174 this.selection = selection;
175 }
176 }