summaryrefslogtreecommitdiff
path: root/src/main/java/org/cristalise/gui/lifecycle/desc/CompActDefOutcomeHandler.java
blob: eabc85b8cb765c7403b858a9710a168e302e0bd3 (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
package org.cristalise.gui.lifecycle.desc;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import org.cristalise.gui.ImageLoader;
import org.cristalise.gui.MainFrame;
import org.cristalise.gui.graph.view.EditorPanel;
import org.cristalise.gui.graph.view.VertexPropertyPanel;
import org.cristalise.gui.lifecycle.instance.FindActDefPanel;
import org.cristalise.gui.tabs.outcome.InvalidOutcomeException;
import org.cristalise.gui.tabs.outcome.InvalidSchemaException;
import org.cristalise.gui.tabs.outcome.OutcomeException;
import org.cristalise.gui.tabs.outcome.OutcomeHandler;
import org.cristalise.gui.tabs.outcome.OutcomeNotInitialisedException;
import org.cristalise.kernel.graph.layout.DefaultGraphLayoutGenerator;
import org.cristalise.kernel.lifecycle.CompositeActivityDef;
import org.cristalise.kernel.lifecycle.WfVertexDefOutlineCreator;
import org.cristalise.kernel.process.Gateway;
import org.cristalise.kernel.utils.FileStringUtility;
import org.cristalise.kernel.utils.Logger;


/**************************************************************************
 *
 * $Revision: 1.14 $
 * $Date: 2005/09/07 13:46:31 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

public class CompActDefOutcomeHandler
    extends JPanel
    implements OutcomeHandler {

    protected JButton mLoadButton = new JButton(ImageLoader.findImage("graph/load.png"));
    protected JButton mLayoutButton = new JButton(ImageLoader.findImage("graph/autolayout.png"));
    protected JButton[] mOtherToolBarButtons = { mLayoutButton, mLoadButton };

    protected CompositeActivityDef mCompActDef = null;
    protected WfEdgeDefFactory mWfEdgeDefFactory = new WfEdgeDefFactory();
    protected WfVertexDefFactory mWfVertexDefFactory = new WfVertexDefFactory();

    protected EditorPanel mEditorPanel;
    protected VertexPropertyPanel mPropertyPanel;
    protected JSplitPane mSplitPane;
    boolean unsaved;

    public CompActDefOutcomeHandler() {
        super();
        mPropertyPanel = loadPropertyPanel();
        mPropertyPanel.createLayout(new FindActDefPanel());
        mEditorPanel =
            new EditorPanel(
                mWfEdgeDefFactory,
                mWfVertexDefFactory,
                new WfVertexDefOutlineCreator(),
                true,
                mOtherToolBarButtons,
        		new WfDefGraphPanel(new WfDirectedEdgeDefRenderer(),
		new WfVertexDefRenderer()));
    }

    protected void createLayout()
    {
        mLoadButton.setToolTipText("Load from local file");
        mLayoutButton.setToolTipText("Auto-Layout");

        // Add the editor pane
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 2.0;
        c.weightx = 2.0;
        mSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, mEditorPanel, mPropertyPanel);
        mSplitPane.setDividerSize(5);
        gridbag.setConstraints(mSplitPane, c);
        add(mSplitPane);
        revalidate();
    }

    protected void createListeners()
    {
        mLoadButton.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(ActionEvent ae) {
                File selectedFile = null;

                int returnValue = MainFrame.xmlChooser.showOpenDialog(null);

                switch (returnValue)
                {
                    case JFileChooser.APPROVE_OPTION :
                        selectedFile = MainFrame.xmlChooser.getSelectedFile();
                        try {
                            String newWf = FileStringUtility.file2String(selectedFile);
                            setOutcome(newWf);
                            setUpGraphEditor();
                        } catch (Exception e) {
                        	MainFrame.exceptionDialog(e);
                        }
                    case JFileChooser.CANCEL_OPTION :
                    case JFileChooser.ERROR_OPTION :

                    default :
                }
            }
        });

        mLayoutButton.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(ActionEvent ae) {
                DefaultGraphLayoutGenerator.layoutGraph(mEditorPanel.mGraphModelManager.getModel());
            }
        });
    }

    public void setUpGraphEditor() {
        mEditorPanel.mGraphModelManager.setModel(mCompActDef.getChildrenGraphModel());
        // Give the editor panel the edge and vertex types
        mEditorPanel.updateVertexTypes(mCompActDef.getVertexTypeNameAndConstructionInfo());
        mEditorPanel.updateEdgeTypes(mCompActDef.getEdgeTypeNameAndConstructionInfo());
        mEditorPanel.enterSelectMode();
        mWfVertexDefFactory.setCreationContext(mCompActDef);
    }

    /**
     *
     */
    @Override
	public void setOutcome(String outcome) throws InvalidOutcomeException {
        try {
            CompositeActivityDef newAct = (CompositeActivityDef)Gateway.getMarshaller().unmarshall(outcome);
            if (mCompActDef != null)
                newAct.setName(mCompActDef.getName());
            mCompActDef = newAct;
        } catch (Exception ex) {
            Logger.error(ex);
            throw new InvalidOutcomeException(ex.getMessage());
        }
    }
    /**
     *
     */
    @Override
	public void setDescription(String description)
        throws InvalidSchemaException {
        // ignore - always the same
    }
    /**
     *
     */
    @Override
	public void setReadOnly(boolean readOnly) {
        mLayoutButton.setEnabled(!readOnly);
        mLoadButton.setEnabled(!readOnly);
        mEditorPanel.setEditable(!readOnly);
        mPropertyPanel.setEditable(!readOnly);
    }
    /**
     *
     */
    @Override
	public JPanel getPanel() throws OutcomeNotInitialisedException {
        return this;
    }
    /**
     *
     */
    @Override
	public String getOutcome() throws OutcomeException {
    	if (!mCompActDef.verify())
    		throw new OutcomeException(mCompActDef.getErrors());
        try {
            return Gateway.getMarshaller().marshall(mCompActDef);
        } catch (Exception ex) {
            throw new OutcomeException(ex.getMessage());
        }
    }
    /**
     *
     */
    @Override
	public void run() {
        Thread.currentThread().setName("Composite Act Def Viewer");
        createLayout();
        createListeners();
        mPropertyPanel.setGraphModelManager(mEditorPanel.mGraphModelManager);
        setUpGraphEditor();
    }

    public VertexPropertyPanel loadPropertyPanel()
    {
        String wfPanelClass = Gateway.getProperties().getString("WfPropertyPanel");
        if (wfPanelClass != null) {
            try {
                return (VertexPropertyPanel)Gateway.getProperties().getInstance("WfPropertyPanel");
            } catch (Exception ex) {
                Logger.error("Could not load wf props panel:"+wfPanelClass);
                Logger.error(ex);
            }
        }
        return new VertexPropertyPanel();
    }

    @Override
	public boolean isUnsaved() {
        return unsaved;
    }

    @Override
	public void saved() {
        unsaved = false;
    }

	@Override
	public void export(File targetFile) throws Exception {
		//Make sure module structure is present
		File parentDir = targetFile.getParentFile();
		FileStringUtility.createNewDir(parentDir.getAbsolutePath()+"/CA");
		FileStringUtility.createNewDir(parentDir.getAbsolutePath()+"/EA");
		FileStringUtility.createNewDir(parentDir.getAbsolutePath()+"/OD");
		FileStringUtility.createNewDir(parentDir.getAbsolutePath()+"/SC");
		BufferedWriter imports = new BufferedWriter(new FileWriter(new File(parentDir, mCompActDef.getActName()+"Imports.xml")));
		ElemActDefOutcomeHandler.exportAct(targetFile.getParentFile(), imports, mCompActDef);
		imports.close();
	}
}