2 * Created on 17/02/2004
5 package com.quantum.extensions;
7 import java.io.IOException;
8 import java.io.StringWriter;
9 import java.util.Iterator;
11 import javax.xml.parsers.ParserConfigurationException;
13 import org.eclipse.jface.viewers.StructuredSelection;
14 import org.eclipse.swt.dnd.TextTransfer;
15 import org.eclipse.swt.dnd.Transfer;
16 import org.eclipse.ui.IViewPart;
17 import org.eclipse.ui.actions.SelectionListenerAction;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.Element;
21 import com.quantum.Messages;
22 import com.quantum.QuantumPlugin;
23 import com.quantum.model.xml.ModelToXMLConverter;
24 import com.quantum.sql.TableRow;
25 import com.quantum.sql.metadata.MetaDataXMLInterface;
26 import com.quantum.util.xml.XMLHelper;
27 import com.quantum.view.bookmark.BookmarkView;
28 import com.quantum.view.bookmark.EntityNode;
29 import com.quantum.view.bookmark.TreeNode;
30 import com.quantum.view.tableview.TableView;
35 * Defines an Action Class to implement extension Actions.
36 * An external plug-in that wants to extend quantum will
37 * generate one ExtensionAction object for each action it
40 public class ExtensionAction extends SelectionListenerAction {
41 private IViewPart view;
42 private IQuantumExtension extension;
43 private TableRow tableRow;
45 public ExtensionAction(IViewPart view, String label, String toolTip, IQuantumExtension extension) {
48 this.extension = extension;
51 setToolTipText(toolTip); //$NON-NLS-1$
56 if (extension instanceof IMetadataExtension && view instanceof BookmarkView)
57 runMetadataExtension();
58 else if (extension instanceof IDataExtension && view instanceof TableView)
65 private void runDataExtension() {
66 IDataExtension dataExtension = (IDataExtension) extension;
67 if (tableRow == null) return;
70 doc = XMLHelper.createEmptyDocument();
71 } catch (ParserConfigurationException e) {
75 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Metadata"))); //$NON-NLS-1$
76 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Author"), //$NON-NLS-1$
77 Messages.getString("ExportXMLAction.Quantum")); //$NON-NLS-1$
78 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Version"), //$NON-NLS-1$
79 Messages.getString("ExportXMLAction.XMLVersionNumber")); //$NON-NLS-1$
80 ModelToXMLConverter.getInstance().convert(root, tableRow.getEntity() );
81 MetaDataXMLInterface.stringMatrixToXML(tableRow.getRowTableData(), doc, root, "DataRow"); //$NON-NLS-1$
83 dataExtension.run(doc);
86 private void runMetadataExtension() {
87 IMetadataExtension metadataExtension = (IMetadataExtension) extension;
88 BookmarkView bookmarkView = (BookmarkView) view;
89 StructuredSelection selection = bookmarkView.getSelection();
90 if (selection == null) return;
93 doc = XMLHelper.createEmptyDocument();
94 } catch (ParserConfigurationException e) {
95 // TODO Auto-generated catch block
99 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Metadata"))); //$NON-NLS-1$
100 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Author"), //$NON-NLS-1$
101 Messages.getString("ExportXMLAction.Quantum")); //$NON-NLS-1$
102 MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Version"), //$NON-NLS-1$
103 Messages.getString("ExportXMLAction.XMLVersionNumber")); //$NON-NLS-1$
104 Iterator iter = selection.iterator();
105 while (iter.hasNext()) {
106 TreeNode current = (TreeNode) iter.next();
107 EntityNode entityNode = (EntityNode) current;
108 ModelToXMLConverter.getInstance().convert(root, entityNode.getEntity() );
111 StringWriter text = new StringWriter();
113 XMLHelper.createDOMSerializer(text).serialize(doc);
114 } catch (IOException e) {
117 String textXml = new String(text.getBuffer());
118 QuantumPlugin.getDefault().getSysClip().setContents(
119 new Object[] { textXml },
120 new Transfer[] { TextTransfer.getInstance()});
122 metadataExtension.run(doc);
128 public void addRowData(TableRow row) {