1 package org.simantics.xml.sax.ui.wizard;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6 import java.util.Arrays;
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.jface.viewers.CheckboxTableViewer;
10 import org.eclipse.jface.viewers.IStructuredContentProvider;
11 import org.eclipse.jface.viewers.ITableLabelProvider;
12 import org.eclipse.jface.viewers.LabelProvider;
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.wizard.WizardPage;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
24 import org.simantics.xml.sax.OntologyCombinator;
25 import org.simantics.xml.sax.SCLCombinator;
27 public class SchemaCombinationPage extends WizardPage {
28 private Composite composite;
33 CheckboxTableViewer fileSelector;
37 public SchemaCombinationPage() {
38 super("XML Schema conversion","Combining schema versions", null);
39 setPageComplete(false);
43 public void setVisible(boolean visible) {
44 super.setVisible(visible);
46 if (outputPlugin == null) {
47 Label label = new Label(composite, SWT.NONE);
48 label.setText("Output plug-in has not been set, cannot create combination resources");
51 String name = outputPlugin.getName();
52 name = name.replaceAll("\\.", "/");
53 srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name);
54 fileSelector.setInput(srcDir);
60 public void createControl(Composite parent) {
61 composite = new Composite(parent, SWT.NONE);
62 composite.setLayout(new GridLayout(1,true));
63 setControl(composite);
65 Label label = new Label(composite, SWT.NONE);
66 label.setText("Ontology resource files");
67 fileSelector = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
68 fileSelector.setContentProvider(new FileContentProvider());
69 fileSelector.setLabelProvider(new FileLabelProvider());
71 label = new Label(composite, SWT.NONE);
72 label.setText("Java Output file");
73 javaOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE);
75 label = new Label(composite, SWT.NONE);
76 label.setText("SCL Output file");
77 sclOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE);
79 Button button = new Button(composite, SWT.PUSH);
80 button.setText("Combine");
81 button.addSelectionListener(new SelectionAdapter() {
83 public void widgetSelected(SelectionEvent e) {
88 GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(fileSelector.getControl());
89 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(javaOutputText);
90 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(sclOutputText);
91 GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button);
94 public void setOutputPlugin(File outputPlugin) {
95 this.outputPlugin = outputPlugin;
98 private void doCombination() {
99 Object sel[] = fileSelector.getCheckedElements();
100 File inputFiles[] = new File[sel.length];
101 for (int i = 0; i < sel.length; i++) {
102 inputFiles[i] = (File)sel[i];
106 String javaOutputName = javaOutputText.getText();
107 if (javaOutputName != null && javaOutputName.length() > 2) {
108 if (!javaOutputName.endsWith(".java"))
109 javaOutputName+=".java";
111 File outputFile = new File(srcDir.getAbsolutePath()+"/"+javaOutputName);
112 OntologyCombinator combinator = new OntologyCombinator();
113 combinator.combine(inputFiles, outputFile);
116 // TODO: SCL code depends on Java, allowing generating just SCL makes sense only for testing purposes.
117 String sclOutputName = sclOutputText.getText();
118 if (sclOutputName != null && sclOutputName.length() > 2) {
119 if (!sclOutputName.endsWith(".scl"))
120 sclOutputName+=".scl";
122 File outputFile = new File(srcDir.getAbsolutePath()+"/"+sclOutputName);
123 OntologyCombinator combinator = new SCLCombinator();
124 combinator.combine(inputFiles, outputFile);
127 setPageComplete(true);
128 } catch (IOException e) {
129 setErrorMessage(e.getMessage());
135 private static class FileContentProvider implements IStructuredContentProvider {
137 public Object[] getElements(Object inputElement) {
138 File directory = (File)inputElement;
139 if (!directory.isDirectory())
140 return new Object[0];
141 File[] files = directory.listFiles(new FilenameFilter() {
144 public boolean accept(File dir, String name) {
145 return name.endsWith("java");
152 public void dispose() {
157 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
162 private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider {
164 public Image getColumnImage(Object element, int columnIndex) {
169 public String getColumnText(Object element, int columnIndex) {
170 File file = (File)element;
171 if (columnIndex == 0)
172 return file.getName();