summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/tabs/outcome/form/OutcomePanel.java
blob: 4bb73470b224dec41b64cc9ddaef49c47837a2bd (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.c2kernel.gui.tabs.outcome.form;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.Enumeration;

import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.exolab.castor.xml.schema.ElementDecl;
import org.exolab.castor.xml.schema.Schema;
import org.exolab.castor.xml.schema.reader.SchemaReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.c2kernel.gui.MainFrame;
import com.c2kernel.gui.tabs.outcome.InvalidOutcomeException;
import com.c2kernel.gui.tabs.outcome.InvalidSchemaException;
import com.c2kernel.gui.tabs.outcome.OutcomeException;
import com.c2kernel.gui.tabs.outcome.OutcomeHandler;
import com.c2kernel.gui.tabs.outcome.OutcomeNotInitialisedException;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Logger;

// will load the outcome as instructed by other bits of the gui
// provides the 'save' button and creates the trees of objects to feed to the outcome form

public class OutcomePanel extends JPanel implements OutcomeHandler
{

    Schema schemaSOM;
    //ASModel schemaASModel;
    Document outcomeDOM;
    OutcomeStructure documentRoot;
    DocumentBuilder parser;
    boolean readOnly;
    boolean useForm = true;
    boolean panelBuilt = false;
    boolean unsaved = false;
    JScrollPane scrollpane = new JScrollPane();
    HelpPane help = new HelpPane();

    JTextArea basicView;

    public OutcomePanel()
    {
        GridBagLayout gridbag = new java.awt.GridBagLayout();
        setLayout(gridbag);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        dbf.setNamespaceAware(false);
        try
        {
            parser = dbf.newDocumentBuilder();
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }

        // Set up panel

        JComponent pane;
        if (!MainFrame.getPref("ShowHelp", "true").equals("true"))
        	pane = scrollpane;
        else {
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, scrollpane, help);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerSize(9);
        pane = splitPane;
        }
        
        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.ipadx = 5;
        c.ipady = 5;

        gridbag.setConstraints(pane, c);
        this.add(pane);
    }

    public OutcomePanel(boolean readOnly)
    {
        this();
        setReadOnly(readOnly);
    }

    public OutcomePanel(String schema, boolean readOnly) throws OutcomeException
    {
        this(readOnly);
        this.setDescription(schema);
    }

    public OutcomePanel(String schema, String outcome, boolean readOnly) throws OutcomeException
    {
        this(readOnly);
        this.setDescription(schema);
        this.setOutcome(outcome);
    }

    // Parse from URLS
    public void setOutcome(URL outcomeURL) throws InvalidOutcomeException
    {

        try
        {
            setOutcome(new InputSource(outcomeURL.openStream()));
        }
        catch (IOException ex)
        {
            throw new InvalidOutcomeException("Error creating instance DOM tree: " + ex);
        }
    }

    public void setDescription(URL schemaURL) throws InvalidSchemaException
    {
        Logger.msg(7, "OutcomePanel.setDescription() - schemaURL:" + schemaURL.toString());
        try
        {
            setDescription(new InputSource(schemaURL.openStream()));
        }
        catch (IOException ex)
        {
            throw new InvalidSchemaException("Error creating exolab schema object: " + ex);
        }

    }

    public OutcomePanel(URL schemaURL, boolean readOnly) throws OutcomeException
    {
        this(readOnly);
        this.setDescription(schemaURL);
    }

    public OutcomePanel(URL schemaURL, URL outcomeURL, boolean readOnly) throws OutcomeException
    {
        this(readOnly);
        this.setDescription(schemaURL);
        this.setOutcome(outcomeURL);
    }

    // Parse from Strings    
    public void setOutcome(String outcome) throws InvalidOutcomeException
    {

        try
        {
            setOutcome(new InputSource(new StringReader(outcome)));
        }
        catch (IOException ex)
        {
            throw new InvalidOutcomeException("Error creating instance DOM tree: " + ex);
        }
    }

    public void setDescription(String schema) throws InvalidSchemaException
    {
        if (schema == null)
            throw new InvalidSchemaException("Null schema supplied");
        try
        {
            setDescription(new InputSource(new StringReader(schema)));
        }
        catch (Exception ex)
        {
            Logger.error(ex);
        }

    }

    public void setReadOnly(boolean readOnly)
    {
        this.readOnly = readOnly;
    }

    public void setDescription(InputSource schemaSource) throws InvalidSchemaException, IOException
    {

        SchemaReader mySchemaReader = new SchemaReader(schemaSource);
        this.schemaSOM = mySchemaReader.read();
    }

    public void setOutcome(InputSource outcomeSource) throws InvalidOutcomeException, IOException
    {
        try
        {
            outcomeDOM = parser.parse(outcomeSource);
        }
        catch (SAXException ex)
        {
            throw new InvalidOutcomeException("Sax error parsing Outcome " + ex);
        }
    }

    public void run()
    {
        Thread.currentThread().setName("Outcome Panel Builder");
        try
        {
            makeDisplay();
        }
        catch (Exception oe)
        {
            scrollpane.setViewportView(new JLabel("Outcome View Generation Failed: " + oe.getMessage()));
            Logger.error(oe);
        }
    }

    public void makeDisplay()
    {
        try
        {
            initPanel();
        }
        catch (OutcomeException ex)
        {
            // something went wrong
            useForm = false;
            Box textPanel = Box.createVerticalBox();
            JLabel errorMsg = new JLabel("Could not create outcome view: " + ex.getMessage());
            errorMsg.setHorizontalAlignment(SwingConstants.LEFT);
            textPanel.add(errorMsg);
            textPanel.add(Box.createVerticalGlue());
            if (outcomeDOM!=null) {
                basicView = new JTextArea(serialize(outcomeDOM, true));
                basicView.setEnabled(!readOnly);
                textPanel.add(basicView);
            }
            scrollpane.setViewportView(textPanel);
        }
    }

    public void initPanel() throws OutcomeException
    {
        Element docElement;
        /*if (panelBuilt)
            return;*/
        Logger.msg(5, "Initialising Panel..");
        scrollpane.setViewportView(new JLabel("Building outcome. Please hang on two ticks . . ."));
        if (schemaSOM == null)
            throw new InvalidSchemaException("A valid schema has not been supplied.");
        // create root panel with element declaration and maybe root document element node

        //find the root element declaration in the schema - may need to look for annotation??
        ElementDecl rootElementDecl = null;
        docElement = (outcomeDOM == null) ? null : outcomeDOM.getDocumentElement();

        for (Enumeration globalElements = schemaSOM.getElementDecls(); globalElements.hasMoreElements();)
        {
            rootElementDecl = (ElementDecl) globalElements.nextElement();
            // REVISIT: We don't detect which is the most likely root element if there is more than one root decl
            // xmlspy looks for an element not referenced elsewhere. simple but hard
            // if we already have a document then use its root element to find the right decl
            if (docElement != null && docElement.getTagName().equals(rootElementDecl.getName()))
                break;
        }

        if (rootElementDecl == null)
            throw new InvalidSchemaException("No root elements defined");
        documentRoot = new DataRecord(rootElementDecl, readOnly, help, false);
        
        Logger.msg(5, "Finished structure. Populating...");
        if (docElement == null)
        {
            outcomeDOM = parser.newDocument();
            docElement = documentRoot.initNew(outcomeDOM);
            outcomeDOM.appendChild(docElement);
        }
        else
            documentRoot.addInstance(docElement, outcomeDOM);

        // got a fully rendered Outcome! put it in the scrollpane
        // initialise container panel

        JTabbedPane outcomeTab = new JTabbedPane();
        outcomeTab.addTab(rootElementDecl.getName(), documentRoot);
        outcomeTab.setSelectedIndex(0);

        scrollpane.setViewportView(outcomeTab);        
        panelBuilt = true;

        revalidate();
        doLayout();
        if (!readOnly)
            documentRoot.grabFocus();
    }

    public JPanel getPanel() throws OutcomeNotInitialisedException
    {
        return this;
    }

    public String getOutcome()
    {
        if (useForm)
        {
            documentRoot.validateStructure();
            return serialize(outcomeDOM, false);
        }
        else
        {
            return basicView.getText();
        }
    }

    static public String serialize(Document doc, boolean prettyPrint)
    {
        String serializedDoc = null;
        OutputFormat format = new OutputFormat(Method.XML, null, prettyPrint);
        StringWriter stringOut = new StringWriter();
        XMLSerializer serial = new XMLSerializer(stringOut, format);
        try
        {
            serial.asDOMSerializer();
            serial.serialize(doc);
        }
        catch (java.io.IOException ex)
        {
            Logger.error(ex.toString());
        }
        serializedDoc = stringOut.toString();
        return serializedDoc;
    }

    public boolean isUnsaved() {
        return unsaved;
    }
    
    public void saved() {
        unsaved = false;
    }
    
	public void export(File targetFile) throws Exception {
		FileStringUtility.string2File(targetFile, getOutcome());
	}
}