package org.simantics.xml.sax.ui.wizard; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.Arrays; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.simantics.xml.sax.OntologyCombinator; import org.simantics.xml.sax.SCLCombinator; public class SchemaCombinationPage extends WizardPage { private Composite composite; File outputPlugin; File srcDir; CheckboxTableViewer fileSelector; Text javaOutputText; Text sclOutputText; public SchemaCombinationPage() { super("XML Schema conversion","Combining schema versions", null); setPageComplete(false); } @Override public void setVisible(boolean visible) { super.setVisible(visible); if (visible) { if (outputPlugin == null) { Label label = new Label(composite, SWT.NONE); label.setText("Output plug-in has not been set, cannot create combination resources"); return; } String name = outputPlugin.getName(); name = name.replaceAll("\\.", "/"); srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name); fileSelector.setInput(srcDir); } } @Override public void createControl(Composite parent) { composite = new Composite(parent, SWT.NONE); composite.setLayout(new GridLayout(1,true)); setControl(composite); Label label = new Label(composite, SWT.NONE); label.setText("Ontology resource files"); fileSelector = CheckboxTableViewer.newCheckList(composite, SWT.BORDER); fileSelector.setContentProvider(new FileContentProvider()); fileSelector.setLabelProvider(new FileLabelProvider()); label = new Label(composite, SWT.NONE); label.setText("Java Output file"); javaOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE); label = new Label(composite, SWT.NONE); label.setText("SCL Output file"); sclOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE); Button button = new Button(composite, SWT.PUSH); button.setText("Combine"); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { doCombination(); } }); GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(fileSelector.getControl()); GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(javaOutputText); GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(sclOutputText); GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button); } public void setOutputPlugin(File outputPlugin) { this.outputPlugin = outputPlugin; } private void doCombination() { Object sel[] = fileSelector.getCheckedElements(); File inputFiles[] = new File[sel.length]; for (int i = 0; i < sel.length; i++) { inputFiles[i] = (File)sel[i]; } try { String javaOutputName = javaOutputText.getText(); if (javaOutputName != null && javaOutputName.length() > 2) { if (!javaOutputName.endsWith(".java")) javaOutputName+=".java"; File outputFile = new File(srcDir.getAbsolutePath()+"/"+javaOutputName); OntologyCombinator combinator = new OntologyCombinator(); combinator.combine(inputFiles, outputFile); } // TODO: SCL code depends on Java, allowing generating just SCL makes sense only for testing purposes. String sclOutputName = sclOutputText.getText(); if (sclOutputName != null && sclOutputName.length() > 2) { if (!sclOutputName.endsWith(".scl")) sclOutputName+=".scl"; File outputFile = new File(srcDir.getAbsolutePath()+"/"+sclOutputName); OntologyCombinator combinator = new SCLCombinator(); combinator.combine(inputFiles, outputFile); } setPageComplete(true); } catch (IOException e) { setErrorMessage(e.getMessage()); e.printStackTrace(); } } private static class FileContentProvider implements IStructuredContentProvider { @Override public Object[] getElements(Object inputElement) { File directory = (File)inputElement; if (!directory.isDirectory()) return new Object[0]; File[] files = directory.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith("java"); } }); return files; } @Override public void dispose() { } @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } } private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider { @Override public Image getColumnImage(Object element, int columnIndex) { return null; } @Override public String getColumnText(Object element, int columnIndex) { File file = (File)element; if (columnIndex == 0) return file.getName(); else return null; } } }