]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java
Create version neutral wrapper class for converted XML schema ontology classes
[simantics/interop.git] / org.simantics.xml.sax.ui / src / org / simantics / xml / sax / ui / wizard / SchemaCombinationPage.java
diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java
new file mode 100644 (file)
index 0000000..1539f50
--- /dev/null
@@ -0,0 +1,158 @@
+package org.simantics.xml.sax.ui.wizard;\r
+\r
+import java.io.File;\r
+import java.io.FilenameFilter;\r
+import java.io.IOException;\r
+import java.util.Arrays;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.viewers.CheckboxTableViewer;\r
+import org.eclipse.jface.viewers.IStructuredContentProvider;\r
+import org.eclipse.jface.viewers.ITableLabelProvider;\r
+import org.eclipse.jface.viewers.LabelProvider;\r
+import org.eclipse.jface.viewers.Viewer;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.xml.sax.OntologyCombinator;\r
+\r
+public class SchemaCombinationPage extends WizardPage {\r
+       private Composite composite;\r
+       \r
+       File outputPlugin;\r
+       File srcDir;\r
+       \r
+       CheckboxTableViewer fileSelector;\r
+       Text outputText;\r
+       \r
+       public SchemaCombinationPage() {\r
+               super("XML Schema conversion","Combining schema versions", null);\r
+               setPageComplete(false);\r
+       }\r
+       \r
+       @Override\r
+       public void setVisible(boolean visible) {\r
+               super.setVisible(visible);\r
+               if (visible) {\r
+                       if (outputPlugin == null) {\r
+                               Label label = new Label(composite, SWT.NONE);\r
+                               label.setText("Output plug-in has not been set, cannot create combination resources");\r
+                               return;\r
+                       }\r
+                       String name = outputPlugin.getName();\r
+                       name = name.replaceAll("\\.", "/");\r
+                       srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name);\r
+                       fileSelector.setInput(srcDir);\r
+               }\r
+       }\r
+       \r
+       \r
+       @Override\r
+       public void createControl(Composite parent) {\r
+               composite = new Composite(parent, SWT.NONE);\r
+               composite.setLayout(new GridLayout(1,true));\r
+               setControl(composite);  \r
+\r
+               Label label = new Label(composite, SWT.NONE);\r
+               label.setText("Ontology resource files");\r
+               fileSelector = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);\r
+               fileSelector.setContentProvider(new FileContentProvider());\r
+               fileSelector.setLabelProvider(new FileLabelProvider());\r
+\r
+               label = new Label(composite, SWT.NONE);\r
+               label.setText("Output file");\r
+               outputText = new Text(composite, SWT.BORDER|SWT.SINGLE);\r
+               Button button = new Button(composite, SWT.PUSH);\r
+               button.setText("Combine");\r
+               button.addSelectionListener(new SelectionAdapter() {\r
+                       @Override\r
+                       public void widgetSelected(SelectionEvent e) {\r
+                               doCombination();\r
+                       }\r
+               });\r
+               \r
+               GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(fileSelector.getControl());\r
+               GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(outputText);\r
+               GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button);\r
+       }\r
+       \r
+       public void setOutputPlugin(File outputPlugin) {\r
+               this.outputPlugin = outputPlugin;\r
+       }\r
+       \r
+       private void doCombination() {\r
+               String outputName = outputText.getText();\r
+               if (outputName == null || outputName.length() < 2)\r
+                       return;\r
+               if (!outputName.endsWith(".java"))\r
+                       outputName+=".java";\r
+               \r
+               File outputFile = new File(srcDir.getAbsolutePath()+"/"+outputName);\r
+               \r
+               Object sel[] = fileSelector.getCheckedElements();\r
+               File inputFiles[] = new File[sel.length];\r
+               for (int i = 0; i < sel.length; i++) {\r
+                       inputFiles[i] = (File)sel[i];\r
+               }\r
+               \r
+               try {\r
+                       OntologyCombinator.combine(inputFiles, outputFile);\r
+                       setPageComplete(true);\r
+               } catch (IOException e) {\r
+                       setErrorMessage(e.getMessage());\r
+                       e.printStackTrace();\r
+               }\r
+               \r
+       }\r
+       \r
+       private static class FileContentProvider implements IStructuredContentProvider {\r
+               @Override\r
+               public Object[] getElements(Object inputElement) {\r
+                       File directory = (File)inputElement;\r
+                       if (!directory.isDirectory())\r
+                               return new Object[0];\r
+                       File[] files = directory.listFiles(new FilenameFilter() {\r
+                               \r
+                               @Override\r
+                               public boolean accept(File dir, String name) {\r
+                                       return name.endsWith("java");\r
+                               }\r
+                       });\r
+                       return files;\r
+               }\r
+               \r
+               @Override\r
+               public void dispose() {\r
+                       \r
+               }\r
+               \r
+               @Override\r
+               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {\r
+                       \r
+               }\r
+       }\r
+       \r
+       private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider {\r
+               @Override\r
+               public Image getColumnImage(Object element, int columnIndex) {\r
+                       return null;\r
+               }\r
+               \r
+               @Override\r
+               public String getColumnText(Object element, int columnIndex) {\r
+                       File file = (File)element;\r
+                       if (columnIndex == 0)\r
+                               return file.getName();\r
+                       else\r
+                               return null;\r
+               }\r
+       }\r
+\r
+}\r