summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/tabs/outcome/form/OutcomeEditor.java
blob: 10c3542166d1b24de8ccd77bdafe70bcff7fbbf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.c2kernel.gui.tabs.outcome.form;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.c2kernel.persistency.outcome.OutcomeValidator;
import com.c2kernel.persistency.outcome.Schema;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Logger;


class OutcomeEditor extends JFrame implements ActionListener {

    boolean readOnly = false;
    File schemaFile = null;
    File instanceFile = null;
    JFileChooser chooser;
    OutcomePanel outcome;
    OutcomeValidator thisValid;
    
    public OutcomeEditor(File schema, File instance, boolean readOnly) {
        URL schemaURL = null;
        URL instanceURL = null;
        schemaFile = schema;
        instanceFile = instance;
        this.readOnly = readOnly;
        
        try {
            chooser = new JFileChooser();
            chooser.setCurrentDirectory(new File(new File(".").getCanonicalPath()));
        } catch (IOException e) {
           System.out.println("Could not initialise file dialog");
           System.exit(0);
        }


        this.setTitle("Outcome Editor");
        GridBagLayout gridbag = new GridBagLayout();
        getContentPane().setLayout(gridbag);
        
        addWindowListener(
            new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {        
                    System.exit(0);
                }                
            }
        );
        // select files if url is empty

        if (schemaFile == null) { // prompt for schema
            schemaFile = getFile("Choose Schema File", "xsd");
            if (schemaFile == null) {
                System.out.println("Cannot function without a schema");
                System.exit(1);
            }
        }
        
        try {
            schemaURL = schemaFile.toURL();
        } catch (Exception e) { 
            System.out.println("Invalid schema URL");
            System.exit(1);        
            }

        if (instanceFile == null) { // prompt for schema
            instanceFile = getFile("Choose Instance File", "xml");
        }

        try {
            instanceURL = instanceFile.toURL();
        } catch (Exception e) { }
            
        try {
            if (instanceFile != null && instanceFile.exists())
                outcome = new OutcomePanel(schemaURL, instanceURL, readOnly);
            else
                outcome = new OutcomePanel(schemaURL, readOnly);

            Schema thisSchema = new Schema();
            thisSchema.docType = schemaURL.getFile();
            thisSchema.docVersion = -1;
            thisSchema.schema = FileStringUtility.url2String(schemaURL);
            thisValid = OutcomeValidator.getValidator(thisSchema);

        } catch (Exception e) { e.printStackTrace(); System.exit(0);}
        
        
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0; c.weighty = 1.0;
        c.gridwidth = 2; c.ipadx = 5; c.ipady = 5; 
        gridbag.setConstraints(outcome, c);
        this.getContentPane().add(outcome);

        JButton saveButton = new JButton("Save");
        saveButton.setActionCommand("save");
        saveButton.addActionListener(this);
        c.gridy++; c.weighty = 0; c.gridwidth = 1;
        gridbag.setConstraints(saveButton, c);
        this.getContentPane().add(saveButton);
        if (readOnly) saveButton.setEnabled(false);
        
        JButton saveAsButton = new JButton("Save As");
        saveAsButton.setActionCommand("saveas");
        saveAsButton.addActionListener(this);
        c.gridx++; c.weighty = 0;
        gridbag.setConstraints(saveAsButton, c);
        this.getContentPane().add(saveAsButton);
        if (readOnly) saveAsButton.setEnabled(false);
        System.out.println("Building Outcome Panel. Please wait . . .");
        outcome.run();
        pack();
        setVisible(true);
        super.toFront();

    }

    public File getFile(String title, String fileType) {
        File targetFile = null;
        chooser.setFileFilter(new SimpleFilter(fileType));
        chooser.setDialogTitle(title);
        int returnVal = chooser.showDialog(this, "Select");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                 targetFile = chooser.getSelectedFile();
             }
        try {
           System.out.println(fileType+"="+targetFile.toURL());
        } catch (Exception ex) { }
        return targetFile;
    }

    public static void usage() {
        System.out.println("-schema file:///schema.xsd");
        System.out.println("-inst file:///instance.xml");
        System.out.println("Leave one out to get a file open box.");
        System.exit(0);
    }
    public static void main( String[] argv ) {
        Logger.addLogStream(System.out, 6);
        File instance = null;
        File schema = null;
        boolean readOnly = false;
        for (int i = 0; i < argv.length; i++) {
            if (argv[i].equals("-schema"))
                schema = new File(argv[++i]);
            if (argv[i].equals("-inst"))
                instance = new File(argv[++i]);
            if (argv[i].equals("-readOnly"))
                readOnly = true;
            if (argv[i].equals("-help") || argv[i].equals("-h"))
                usage();
        }
        new OutcomeEditor(schema, instance, readOnly);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().indexOf("save") == 0) {
            String output;
            output = outcome.getOutcome();
            
            String errors = thisValid.validate(output);
            if (errors != null && errors.length() > 0) {
                int choice = JOptionPane.showConfirmDialog(null, errors+"\n\nSave anyway?", "Errors validating document", JOptionPane.YES_NO_OPTION);
                if (choice != JOptionPane.YES_OPTION)
                    return;
            }
            
            if (instanceFile == null || e.getActionCommand().equals("saveas")) {
                instanceFile = getFile("Choose Instance File", "xml");
                if (instanceFile == null) {
                    System.out.println(output);
                    return;
                }
            }
            try {
                FileOutputStream targetStream = new FileOutputStream(instanceFile);
                targetStream.write(output.getBytes());
                targetStream.close();
            } catch (Exception ex) {ex.printStackTrace();}
        }
    }

  private class SimpleFilter extends javax.swing.filechooser.FileFilter {
    String extension;

    public SimpleFilter(String extension) {
        super();
        this.extension = extension;
    }

    public String getDescription() {
        return extension.toUpperCase()+" Files";
    }

    public boolean accept(File f) {
        if ((f.isFile() && f.getName().endsWith(extension.toLowerCase())) || f.isDirectory()) {
            return true;
        }
        return false;
        }
    }
}