synchronized from quantum plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / extensions / ExtensionAction.java
1 /*
2  * Created on 17/02/2004
3  *
4  */
5 package com.quantum.extensions;
6
7 import java.io.IOException;
8 import java.io.StringWriter;
9 import java.util.Iterator;
10
11 import javax.xml.parsers.ParserConfigurationException;
12
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;
20
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;
31
32 /**
33  * @author panic
34  *
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
38  * wants to perform.
39  */
40 public class ExtensionAction extends SelectionListenerAction {
41         private IViewPart view;
42         private IQuantumExtension extension;
43         private TableRow tableRow;
44         
45         public ExtensionAction(IViewPart view, String label, String toolTip, IQuantumExtension extension) {
46                 super(label);
47                 this.view = view;
48                 this.extension = extension;
49
50                 setText(label);
51                 setToolTipText(toolTip); //$NON-NLS-1$
52                 tableRow = null;
53         }
54         
55         public void run() {
56                 if (extension instanceof IMetadataExtension &&  view instanceof BookmarkView)
57                         runMetadataExtension();
58                 else if (extension instanceof IDataExtension && view instanceof TableView)
59                         runDataExtension();
60         }
61
62         /**
63          * 
64          */
65         private void runDataExtension() {
66                 IDataExtension dataExtension = (IDataExtension) extension;
67                 if (tableRow == null) return;
68                 Document doc;
69                 try {
70                         doc = XMLHelper.createEmptyDocument();
71                 } catch (ParserConfigurationException e) {
72                         e.printStackTrace();
73                         return;
74                 }
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$
82
83                 dataExtension.run(doc);
84         }
85
86         private void runMetadataExtension() {
87                 IMetadataExtension metadataExtension = (IMetadataExtension) extension;
88                 BookmarkView bookmarkView = (BookmarkView) view;
89                 StructuredSelection selection = bookmarkView.getSelection();
90                 if (selection == null) return;
91                 Document doc;
92                 try {
93                         doc = XMLHelper.createEmptyDocument();
94                 } catch (ParserConfigurationException e) {
95                         // TODO Auto-generated catch block
96                         e.printStackTrace();
97                         return;
98                 }
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() );                  
109                 }
110                 
111                 StringWriter text = new StringWriter();
112                 try {
113                         XMLHelper.write(text, doc);
114                 } catch (IOException e) {
115                         e.printStackTrace();
116                 }
117                 String textXml = new String(text.getBuffer());
118                 QuantumPlugin.getDefault().getSysClip().setContents(
119                         new Object[] { textXml },
120                         new Transfer[] { TextTransfer.getInstance()});
121                 
122                 metadataExtension.run(doc);
123         }
124
125         /**
126          * @param row
127          */
128         public void addRowData(TableRow row) {
129                 tableRow = row;
130         }
131
132 }