4166947ce23915815936090101e45f6b627281d6
[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 BookmarkClipboard bookmarkClipboard;
34     
35         /**
36          * @param BookmarkView
37          */
38         public CopyAction(IViewPart view, BookmarkClipboard bookmarkClipboard, 
39         ISelectionProvider selectionProvider) {
40         super(Messages.getString(CopyAction.class.getName() + ".text"));
41         setImageDescriptor(QuantumPlugin.getImageDescriptor("copy.gif"));
42         this.bookmarkClipboard = bookmarkClipboard;
43         selectionProvider.addSelectionChangedListener(this);
44         }
45         public void run() {
46         
47         List list = getSelectedNonResources();
48         Object selection = list.get(0);
49         
50                 if (selection instanceof BookmarkNode) {
51             Bookmark bookmark = ((BookmarkNode) selection).getBookmark();
52                         this.bookmarkClipboard.setBookmark(bookmark);
53                         QuantumPlugin.getDefault().getSysClip().setContents(
54                                 new Object[] { bookmark.getName()},
55                                 new Transfer[] { TextTransfer.getInstance()});
56                         
57                 } else if (selection instanceof EntityNode) {
58             try {
59                         Document doc = XMLHelper.createEmptyDocument();
60                 Element root = doc.createElement("metadata");
61                 doc.appendChild(root);
62                         ModelToXMLConverter.getInstance().convert(root, ((EntityNode) selection).getEntity());
63                         StringWriter text = new StringWriter();
64                         try {
65                     XMLHelper.write(text, doc);
66                         } catch (IOException e) {
67                                 e.printStackTrace();
68                         }
69                         String textXml = new String(text.getBuffer());
70                 QuantumPlugin.getDefault().getSysClip().setContents(
71                                 new Object[] { textXml },
72                                 new Transfer[] { TextTransfer.getInstance()});
73             } catch (ParserConfigurationException e) {
74                 e.printStackTrace();
75             }
76                 } else if (selection instanceof ColumnNode) {
77                         String copyText = ""; //$NON-NLS-1$
78                         Iterator iter = list.iterator();
79                         IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
80         
81                         while (iter.hasNext()) {
82                                 Object current = iter.next();
83                                 if (current instanceof ColumnNode) {
84                                         ColumnNode column = (ColumnNode) current;
85                                         if (column != null) {
86                                                 copyText += column.getName();
87                                                 if (iter.hasNext()) copyText += QuantumUtil.trasposeEscape(store.getString("copyColumnSeparator")); //$NON-NLS-1$
88                                         }
89                                 }
90                         }
91                         
92             QuantumPlugin.getDefault().getSysClip().setContents(
93                                 new Object[] { copyText },
94                                 new Transfer[] { TextTransfer.getInstance()});                          
95                 }
96                 
97         }
98     /* (non-Javadoc)
99      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
100      */
101     protected boolean updateSelection(IStructuredSelection selection) {
102         boolean enabled = super.updateSelection(selection);
103         
104         return enabled; 
105     }
106
107 }