misc changes
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / CopyAction.java
1 /*
2  * Created on 11/08/2003
3  *
4  */
5 package com.quantum.view.bookmark;
6
7 import java.io.IOException;
8 import java.io.StringWriter;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import com.quantum.Messages;
15 import com.quantum.QuantumPlugin;
16 import com.quantum.model.Bookmark;
17 import com.quantum.model.xml.ModelToXMLConverter;
18 import com.quantum.util.QuantumUtil;
19 import com.quantum.util.xml.XMLHelper;
20
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.swt.dnd.TextTransfer;
25 import org.eclipse.swt.dnd.Transfer;
26 import org.eclipse.ui.IViewPart;
27 import org.eclipse.ui.actions.SelectionListenerAction;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30
31
32 final class CopyAction extends SelectionListenerAction {
33         private IViewPart view;
34     private BookmarkClipboard bookmarkClipboard;
35     
36         /**
37          * @param BookmarkView
38          */
39         public CopyAction(IViewPart view, BookmarkClipboard bookmarkClipboard, 
40         ISelectionProvider selectionProvider) {
41         super(Messages.getString(CopyAction.class.getName() + ".text"));
42         setImageDescriptor(QuantumPlugin.getImageDescriptor("copy.gif"));
43                 this.view = view;
44         this.bookmarkClipboard = bookmarkClipboard;
45         selectionProvider.addSelectionChangedListener(this);
46         }
47         public void run() {
48         
49         List list = getSelectedNonResources();
50         Object selection = list.get(0);
51         
52                 if (selection instanceof BookmarkNode) {
53             Bookmark bookmark = ((BookmarkNode) selection).getBookmark();
54                         this.bookmarkClipboard.setBookmark(bookmark);
55                         QuantumPlugin.getDefault().getSysClip().setContents(
56                                 new Object[] { bookmark.getName()},
57                                 new Transfer[] { TextTransfer.getInstance()});
58                         
59                 } else if (selection instanceof EntityNode) {
60             try {
61                         Document doc = XMLHelper.createEmptyDocument();
62                 Element root = doc.createElement("metadata");
63                 doc.appendChild(root);
64                         ModelToXMLConverter.getInstance().convert(root, ((EntityNode) selection).getEntity());
65                         StringWriter text = new StringWriter();
66                         try {
67                     XMLHelper.createDOMSerializer(text).serialize(doc);
68                         } catch (IOException e) {
69                                 e.printStackTrace();
70                         }
71                         String textXml = new String(text.getBuffer());
72                 QuantumPlugin.getDefault().getSysClip().setContents(
73                                 new Object[] { textXml },
74                                 new Transfer[] { TextTransfer.getInstance()});
75             } catch (ParserConfigurationException e) {
76                 e.printStackTrace();
77             }
78                 } else if (selection instanceof ColumnNode) {
79                         String copyText = ""; //$NON-NLS-1$
80                         Iterator iter = list.iterator();
81                         IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
82         
83                         while (iter.hasNext()) {
84                                 Object current = iter.next();
85                                 if (current instanceof ColumnNode) {
86                                         ColumnNode column = (ColumnNode) current;
87                                         if (column != null) {
88                                                 copyText += column.getName();
89                                                 if (iter.hasNext()) copyText += QuantumUtil.trasposeEscape(store.getString("copyColumnSeparator")); //$NON-NLS-1$
90                                         }
91                                 }
92                         }
93                         
94             QuantumPlugin.getDefault().getSysClip().setContents(
95                                 new Object[] { copyText },
96                                 new Transfer[] { TextTransfer.getInstance()});                          
97                 }
98                 
99         }
100     /* (non-Javadoc)
101      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
102      */
103     protected boolean updateSelection(IStructuredSelection selection) {
104         boolean enabled = super.updateSelection(selection);
105         
106         return enabled; 
107     }
108
109 }