]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java
dd5d97f5d64fafeafa59adbf1904430f36d383fc
[simantics/interop.git] / org.simantics.xml.sax.ui / src / org / simantics / xml / sax / ui / wizard / SchemaCombinationPage.java
1 package org.simantics.xml.sax.ui.wizard;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6 import java.util.Arrays;
7
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;
26
27 public class SchemaCombinationPage extends WizardPage {
28         private Composite composite;
29         
30         File outputPlugin;
31         File srcDir;
32         
33         CheckboxTableViewer fileSelector;
34         Text javaOutputText;
35         Text sclOutputText;
36         
37         public SchemaCombinationPage() {
38                 super("XML Schema conversion","Combining schema versions", null);
39                 setPageComplete(false);
40         }
41         
42         @Override
43         public void setVisible(boolean visible) {
44                 super.setVisible(visible);
45                 if (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");
49                                 return;
50                         }
51                         String name = outputPlugin.getName();
52                         name = name.replaceAll("\\.", "/");
53                         srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name);
54                         fileSelector.setInput(srcDir);
55                 }
56         }
57         
58         
59         @Override
60         public void createControl(Composite parent) {
61                 composite = new Composite(parent, SWT.NONE);
62                 composite.setLayout(new GridLayout(1,true));
63                 setControl(composite);  
64
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());
70
71                 label = new Label(composite, SWT.NONE);
72                 label.setText("Java Output file");
73                 javaOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE);
74                 
75                 label = new Label(composite, SWT.NONE);
76                 label.setText("SCL Output file");
77                 sclOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE);
78                 
79                 Button button = new Button(composite, SWT.PUSH);
80                 button.setText("Combine");
81                 button.addSelectionListener(new SelectionAdapter() {
82                         @Override
83                         public void widgetSelected(SelectionEvent e) {
84                                 doCombination();
85                         }
86                 });
87                 
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);
92         }
93         
94         public void setOutputPlugin(File outputPlugin) {
95                 this.outputPlugin = outputPlugin;
96         }
97         
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];
103                 }
104                 
105                 try {
106                         String javaOutputName = javaOutputText.getText();
107                         if (javaOutputName != null && javaOutputName.length() > 2) {
108                                 if (!javaOutputName.endsWith(".java"))
109                                         javaOutputName+=".java";
110                                 
111                                 File outputFile = new File(srcDir.getAbsolutePath()+"/"+javaOutputName);
112                                 OntologyCombinator combinator = new OntologyCombinator();
113                                 combinator.combine(inputFiles, outputFile);
114                                 
115                         }
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";
121                                 
122                                 File outputFile = new File(srcDir.getAbsolutePath()+"/"+sclOutputName);
123                                 OntologyCombinator combinator = new SCLCombinator();
124                                 combinator.combine(inputFiles, outputFile);                     
125                         }
126                         
127                         setPageComplete(true);
128                 } catch (IOException e) {
129                         setErrorMessage(e.getMessage());
130                         e.printStackTrace();
131                 }
132                 
133         }
134         
135         private static class FileContentProvider implements IStructuredContentProvider {
136                 @Override
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() {
142                                 
143                                 @Override
144                                 public boolean accept(File dir, String name) {
145                                         return name.endsWith("java");
146                                 }
147                         });
148                         return files;
149                 }
150                 
151                 @Override
152                 public void dispose() {
153                         
154                 }
155                 
156                 @Override
157                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
158                         
159                 }
160         }
161         
162         private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider {
163                 @Override
164                 public Image getColumnImage(Object element, int columnIndex) {
165                         return null;
166                 }
167                 
168                 @Override
169                 public String getColumnText(Object element, int columnIndex) {
170                         File file = (File)element;
171                         if (columnIndex == 0)
172                                 return file.getName();
173                         else
174                                 return null;
175                 }
176         }
177
178 }