1 package javafxmlpackage.wizards;
3 import org.eclipse.jface.viewers.IStructuredSelection;
4 import org.eclipse.jface.wizard.Wizard;
5 import org.eclipse.ui.INewWizard;
6 import org.eclipse.ui.IWorkbench;
7 import org.eclipse.core.runtime.*;
8 import org.eclipse.jface.operation.*;
9 import java.lang.reflect.InvocationTargetException;
10 import java.util.Calendar;
12 import org.eclipse.jface.dialogs.MessageDialog;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.core.resources.*;
17 import org.eclipse.ui.*;
18 import org.eclipse.ui.ide.IDE;
34 setNeedsProgressMonitor(
true);
58 IRunnableWithProgress op =
new IRunnableWithProgress() {
60 public void run(IProgressMonitor monitor)
throws InvocationTargetException {
62 doFinish(containerName, packageName, fileName, monitor);
63 }
catch (CoreException e) {
64 throw new InvocationTargetException(e);
71 getContainer().run(
true,
false, op);
72 }
catch (InterruptedException e) {
74 }
catch (InvocationTargetException e) {
75 Throwable realException = e.getTargetException();
76 MessageDialog.openError(getShell(),
"Error", realException.getMessage());
97 IProgressMonitor monitor)
98 throws CoreException {
101 String parsePackageName = packageName.replace(
'.',
'/');
103 String parseFileName = fileName;
105 String vaihdaKirjain =
"ÅÄÖåäö";
106 String uusiKirjain =
"AAOaao";
110 for (
int i = 0;
i < vaihdaKirjain.length();
i++) {
111 parsePackageName = parsePackageName.replace(vaihdaKirjain.charAt(
i), uusiKirjain.charAt(
i));
112 parseFileName = parseFileName.replace(vaihdaKirjain.charAt(
i), uusiKirjain.charAt(
i));
116 StringBuilder sb =
new StringBuilder(parsePackageName);
118 while (sb.indexOf(
"//") != -1) {
119 sb.deleteCharAt(sb.indexOf(
"//"));
122 parsePackageName = sb.toString();
126 if (Character.isLowerCase(parseFileName.charAt(0)))
127 parseFileName = Character.toUpperCase(parseFileName.charAt(0)) + parseFileName.substring(1);
129 monitor.beginTask(
"Creating files...", 2);
130 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
131 IResource resource = root.findMember(
new Path(containerName));
132 if (!resource.exists() || !(resource instanceof IContainer)) {
136 if (resource.getProjectRelativePath().isEmpty()) {
141 IContainer container = (IContainer) resource;
142 final IFile javaFile = container.getFile(
new Path(parsePackageName +
"/" +parseFileName +
"Main.java"));
143 final IFile controllerFile = container.getFile(
new Path(parsePackageName +
"/" +parseFileName+
"Controller.java"));
144 final IFile cssFile = container.getFile(
new Path(parsePackageName +
"/" +parseFileName.toLowerCase()+
".css"));
145 final IFile viewFile = container.getFile(
new Path(parsePackageName +
"/" +parseFileName+
"View.fxml"));
148 if (parsePackageName.lastIndexOf(
'/') == -1) {
149 final IFolder packageF = container.getFolder(
new Path(parsePackageName));
150 if (!packageF.exists())
151 packageF.create(
false,
false, monitor);
155 String[] splitti = parsePackageName.split(
"/");
157 for (
int i = 0;
i < splitti.length;
i++) {
158 if (splitti[
i].length() == 0)
continue;
159 pathi += splitti[
i] +
"/";
160 final IFolder packageF = container.getFolder(
new Path(pathi));
161 if (!packageF.exists())
162 packageF.create(
false,
false, monitor);
167 try (InputStream stream =
openContentStream(1, parsePackageName, parseFileName, fileName)){
168 cssFile.create(stream,
true, monitor);
170 }
catch (IOException e) {
174 try (InputStream stream =
openContentStream(2, parsePackageName, parseFileName, fileName)){
175 controllerFile.create(stream,
true, monitor);
177 }
catch (IOException e) {
181 try (InputStream stream =
openContentStream(3, parsePackageName, parseFileName, fileName)){
182 javaFile.create(stream,
true, monitor);
184 }
catch (IOException e) {
188 try (InputStream stream =
openContentStream(4, parsePackageName, parseFileName, fileName)){
189 viewFile.create(stream,
true, monitor);
191 }
catch (IOException e) {
199 monitor.setTaskName(
"Opening file for editing...");
200 getShell().getDisplay().asyncExec(
new Runnable() {
203 IWorkbenchPage sivu =
204 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
206 IDE.openEditor(sivu, javaFile,
true);
207 }
catch (PartInitException e) {
215 private void tarkistaProjekti(IContainer cont, IProgressMonitor monitor)
throws CoreException {
217 if (!cont.getProjectRelativePath().isEmpty())
return;
218 final IFile fxbuildFile = cont.getFile(
new Path(
"/build.fxbuild"));
219 if (fxbuildFile.exists()) {
224 fxbuildFile.create(stream,
true, monitor);
226 }
catch (IOException e) {
232 final IFile projectFile = cont.getFile(
new Path(
".project"));
233 if (!fxbuildFile.exists()) {
238 try (InputStream stream = projectFile.getContents()) {
242 String mihin =
"</buildSpec>";
243 int index = sisalto.lastIndexOf(mihin);
244 if (index == -1)
return;
246 " <buildCommand>"+System.lineSeparator()+
247 " <name>org.eclipse.xtext.ui.shared.xtextBuilder</name>"+System.lineSeparator()+
248 " <arguments>"+System.lineSeparator()+
249 " </arguments>"+System.lineSeparator()+
250 " </buildCommand>"+System.lineSeparator()+
"\t";
251 sisalto.insert(index, lisattava);
254 index = sisalto.lastIndexOf(mihin);
255 if (index == -1)
return;
257 "<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>"+System.lineSeparator()+
"\t\t";
258 sisalto.insert(index, lisattava);
259 projectFile.setContents(
new ByteArrayInputStream(sisalto.toString().getBytes()),
true,
true, monitor);
260 }
catch (IOException e) {
265 final IFile classpathFile = cont.getFile(
new Path(
".classpath"));
266 if (!fxbuildFile.exists()) {
270 try (InputStream stream = classpathFile.getContents()) {
274 String mihin =
"<classpathentry";
275 int index = sisalto.lastIndexOf(mihin);
276 if (index == -1)
return;
278 "<classpathentry kind=\"con\" path=\"org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER\"/>"+System.lineSeparator()+
"\t";
279 sisalto.insert(index, lisattava);
281 classpathFile.setContents(
new ByteArrayInputStream(sisalto.toString().getBytes()),
true,
true, monitor);
282 }
catch (IOException e) {
296 StringBuilder sb =
new StringBuilder();
298 try (BufferedReader br =
new BufferedReader(
new InputStreamReader(stream))) {
299 while ((s = br.readLine()) != null) {
301 sb.append(s + System.lineSeparator());
314 private InputStream
openContentStream(
int monesko, String packageName, String fileName, String alkupFileName) {
316 Calendar aika = Calendar.getInstance();
319 String contents =
"";
320 String parsePackageName = packageName.replace(
'/',
'.');
323 case 1: contents =
"/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */";
327 "package "+parsePackageName+
";"+System.lineSeparator()+
328 System.lineSeparator()+
329 "/**"+System.lineSeparator()+
330 " * @author "+System.getProperty(
"user.name").toString()+System.lineSeparator()+
331 " * @version "+aika.get(Calendar.DAY_OF_MONTH)+
"."+(aika.get(Calendar.MONTH) + 1)+
"."+aika.get(Calendar.YEAR)+System.lineSeparator()+
332 " *"+System.lineSeparator()+
333 " */"+System.lineSeparator()+
334 "public class "+fileName+
"Controller {"+System.lineSeparator()+
335 " // TODO"+System.lineSeparator()+
340 "package "+parsePackageName+
";"+System.lineSeparator()+System.lineSeparator()+
342 "import javafx.application.Application;" +System.lineSeparator()+
343 "import javafx.stage.Stage;"+System.lineSeparator()+
344 "import javafx.scene.Scene;"+System.lineSeparator()+
345 "import javafx.scene.layout.Pane;"+System.lineSeparator()+
346 "import javafx.fxml.FXMLLoader;"+System.lineSeparator()+System.lineSeparator()+System.lineSeparator()+
349 "/**"+System.lineSeparator()+
350 " * @author "+System.getProperty(
"user.name").toString()+System.lineSeparator()+
351 " * @version "+aika.get(Calendar.DAY_OF_MONTH)+
"."+(aika.get(Calendar.MONTH) + 1)+
"."+aika.get(Calendar.YEAR)+System.lineSeparator()+
352 " *"+System.lineSeparator()+
353 " */"+System.lineSeparator()+
354 "public class "+fileName+
"Main extends Application {"+System.lineSeparator()+
355 " @Override"+System.lineSeparator()+
356 " public void start(Stage primaryStage) {"+System.lineSeparator()+
357 " try {"+System.lineSeparator()+
358 " FXMLLoader ldr = new FXMLLoader(getClass().getResource(\""+fileName+
"View.fxml\"));"+System.lineSeparator()+
359 " final Pane root = ldr.load();"+System.lineSeparator()+
360 " //final "+fileName+
"Controller "+fileName.toLowerCase()+
"Ctrl = ("+fileName+
"Controller) ldr.getController();"+System.lineSeparator()+
361 " Scene scene = new Scene(root);"+System.lineSeparator()+
362 " scene.getStylesheets().add(getClass().getResource(\""+fileName.toLowerCase()+
".css\").toExternalForm());"+System.lineSeparator()+
363 " primaryStage.setScene(scene);"+System.lineSeparator()+
364 " primaryStage.setTitle(\""+alkupFileName+
"\");"+System.lineSeparator()+
365 " primaryStage.show();"+System.lineSeparator()+
366 " } catch(Exception e) {"+System.lineSeparator()+
367 " e.printStackTrace();"+System.lineSeparator()+
368 " }"+System.lineSeparator()+
369 " }"+System.lineSeparator()+System.lineSeparator()+
371 " /**"+System.lineSeparator()+
372 " * @param args Ei käytössä"+System.lineSeparator()+
373 " */"+System.lineSeparator()+
374 " public static void main(String[] args) {"+System.lineSeparator()+
375 " launch(args);"+System.lineSeparator()+
376 " }"+System.lineSeparator()+
382 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +System.lineSeparator()+System.lineSeparator()+
384 "<?import javafx.scene.layout.BorderPane?>"+System.lineSeparator()+System.lineSeparator()+
386 "<BorderPane xmlns:fx=\"http://javafx.com/fxml/1\" fx:controller=\""+parsePackageName+
"."+fileName+
"Controller\">"+System.lineSeparator()+
387 " <!-- TODO Add Nodes -->"+System.lineSeparator()+
388 "</BorderPane>"+System.lineSeparator();
393 return new ByteArrayInputStream(contents.getBytes());
398 "<?xml version=\"1.0\" encoding=\"ASCII\"?>"+System.lineSeparator()+
399 "<anttasks:AntTask xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:anttasks=\"http://org.eclipse.fx.ide.jdt/1.0\" buildDirectory=\"${project}/build\">"+System.lineSeparator()+
400 " <deploy>"+System.lineSeparator()+
401 " <application name=\""+cont.getFullPath().toString().replace(
"/",
"")+
"\"/>"+System.lineSeparator()+
402 " <info/>"+System.lineSeparator()+
403 " </deploy>"+System.lineSeparator()+
404 " <signjar/>"+System.lineSeparator()+
405 "</anttasks:AntTask>"+System.lineSeparator();
406 return new ByteArrayInputStream(contents.getBytes());
411 new Status(IStatus.ERROR,
"javaFXpackage", IStatus.OK, message, null);
412 throw new CoreException(status);
421 public void init(IWorkbench workbench, IStructuredSelection select) {