View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui.gui;
7   
8   import org.glassbox.Theme;
9   import org.glassbox.gui.Visible;
10  
11  import net.kwfgrid.gwui.SwingFactory2;
12  import net.kwfgrid.gwui.GWUI;
13  import net.kwfgrid.gworkflowdl.structure.Workflow;
14  import net.kwfgrid.gworkflowdl.structure.JdomString;
15  import net.kwfgrid.gworkflowdl.protocol.structure.ProtocolWorkflow;
16  
17  import java.awt.*;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.io.*;
21  import javax.swing.*;
22  import javax.swing.border.TitledBorder;
23  
24  import org.apache.log4j.Logger;
25  
26  /***
27     A view for the workflow document.
28     This widget has the following properties which can be configured in the application's theme:<br>
29     <ul>
30     <li>kwfgrid.WorkflowDocumentView.textarea.size</li>
31     </ul>
32   */
33  public class WorkflowDocumentView extends AbstractWorkflowElementView {
34      private static final Logger logger = Logger.getLogger(WorkflowDocumentView.class);
35  
36      private static final String FRAME_SIZE_KEY = "kwfgrid.WorkflowDocumentView.textarea.size";
37      private static final Dimension FRAME_SIZE = Theme.getSize(FRAME_SIZE_KEY);
38  
39      private JComponent _view;    
40      private JTextArea _documentarea;
41      private JButton _updatebutton;
42      
43      public WorkflowDocumentView(ProtocolWorkflow wf) {
44  	super(wf, wf);
45  	_view = null;
46      }
47  
48      protected void updateState() {
49  	// _documentarea.setEnabled(isEnabled());
50  	// _updatebutton.setEnabled(isEnabled());
51      }
52  
53      protected void updateView() {
54  	updateState();
55  	_view.invalidate();
56  
57  	_view.setSize(_view.getLayout().preferredLayoutSize(_view));
58  	_view.doLayout();
59  	_view.validate();	
60      }
61  
62      public void dispose() {
63  	//
64      }
65  
66      public void updateWorkflowDocument() {
67  	try {
68  	    _documentarea.setText(JdomString.workflow2string((Workflow)getElement()));
69  	} catch (Exception x) {
70  	    // FIXME
71  	    logger.error("Could not serialize workflow description.", x);
72  	    _documentarea.setText("Could not serialize workflow description: "+x.getClass().getName()+" "+x.getMessage());
73  	}
74      }
75  
76      public JComponent getView() {
77  	if (_view == null) {
78  	    _documentarea = new JTextArea(FRAME_SIZE.height, FRAME_SIZE.width);
79  	    _documentarea.setEditable(false);
80  	    JScrollPane documentscroller = new JScrollPane(_documentarea);
81  	    documentscroller.setBorder(null);
82  
83  	    _updatebutton = new JButton("update");
84  	    _updatebutton.addActionListener(new ActionListener() {
85  	    	    public void actionPerformed(ActionEvent e) {
86  	    		updateWorkflowDocument();
87  	    	    }
88  	    	});	    
89  	    
90  	    _view = new JPanel();
91  	    _view.setBorder(createBorder("XML Document"));
92  	    _view.setLayout(new GridBagLayout());
93  	    GridBagConstraints c = new GridBagConstraints();
94  
95  	    c.fill = GridBagConstraints.BOTH;
96  	    c.anchor = GridBagConstraints.CENTER;
97  	    c.insets = new Insets(5, 0, 5, 0);
98  	    c.weightx = 1.0;
99  	    c.weighty = 0.0;
100 	    c.gridx = 0;
101 	    c.gridy = 0;
102 	    c.gridwidth = GridBagConstraints.REMAINDER;
103 	    c.weighty = 1.0;
104 	    c.gridy = 0;
105 	    
106 	    _view.add(documentscroller, c);
107 
108 	    c.weightx = 0.0;
109 	    c.weighty = 0.0;
110 	    c.gridy = 1;	    
111 	    c.fill = GridBagConstraints.NONE;
112 	    c.anchor = GridBagConstraints.EAST;
113 	    c.gridwidth = GridBagConstraints.REMAINDER;
114 
115 	    _view.add(_updatebutton, c);
116 
117 	    updateView();
118 	}
119 	return _view;
120     }
121 }