Fixed: malfunctioned "Remove trailing spaces on editor save"
[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.ImageStore;
15 import com.quantum.Messages;
16 import com.quantum.QuantumPlugin;
17 import com.quantum.model.Bookmark;
18 import com.quantum.model.xml.ModelToXMLConverter;
19 import com.quantum.util.QuantumUtil;
20 import com.quantum.util.xml.XMLHelper;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.swt.dnd.TextTransfer;
26 import org.eclipse.swt.dnd.Transfer;
27 import org.eclipse.ui.IViewPart;
28 import org.eclipse.ui.actions.SelectionListenerAction;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31
32
33 final class CopyAction extends SelectionListenerAction {
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(ImageStore.getImageDescriptor(ImageStore.COPY));
43         this.bookmarkClipboard = bookmarkClipboard;
44         selectionProvider.addSelectionChangedListener(this);
45         }
46         public void run() {
47         
48         List list = getSelectedNonResources();
49         Object selection = list.get(0);
50         
51                 if (selection instanceof BookmarkNode) {
52             Bookmark bookmark = ((BookmarkNode) selection).getBookmark();
53                         this.bookmarkClipboard.setBookmark(bookmark);
54                         QuantumPlugin.getDefault().getSysClip().setContents(
55                                 new Object[] { bookmark.getName()},
56                                 new Transfer[] { TextTransfer.getInstance()});
57                         
58                 } else if (selection instanceof EntityNode) {
59             try {
60                         Document doc = XMLHelper.createEmptyDocument();
61                 Element root = doc.createElement("metadata");
62                 doc.appendChild(root);
63                         ModelToXMLConverter.getInstance().convert(root, ((EntityNode) selection).getEntity());
64                         StringWriter text = new StringWriter();
65                         try {
66                     XMLHelper.write(text, doc);
67                         } catch (IOException e) {
68                                 e.printStackTrace();
69                         }
70                         String textXml = new String(text.getBuffer());
71                 QuantumPlugin.getDefault().getSysClip().setContents(
72                                 new Object[] { textXml },
73                                 new Transfer[] { TextTransfer.getInstance()});
74             } catch (ParserConfigurationException e) {
75                 e.printStackTrace();
76             }
77                 } else if (selection instanceof ColumnNode) {
78                         String copyText = ""; //$NON-NLS-1$
79                         Iterator iter = list.iterator();
80                         IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
81         
82                         while (iter.hasNext()) {
83                                 Object current = iter.next();
84                                 if (current instanceof ColumnNode) {
85                                         ColumnNode column = (ColumnNode) current;
86                                         if (column != null) {
87                                                 copyText += column.getName();
88                                                 if (iter.hasNext()) copyText += QuantumUtil.trasposeEscape(store.getString("copyColumnSeparator")); //$NON-NLS-1$
89                                         }
90                                 }
91                         }
92                         
93             QuantumPlugin.getDefault().getSysClip().setContents(
94                                 new Object[] { copyText },
95                                 new Transfer[] { TextTransfer.getInstance()});                          
96                 }
97                 
98         }
99     /* (non-Javadoc)
100      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
101      */
102     protected boolean updateSelection(IStructuredSelection selection) {
103         boolean enabled = super.updateSelection(selection);
104         
105         return enabled; 
106     }
107
108 }