2 * Created on 11/08/2003
5 package com.quantum.view.bookmark;
7 import java.io.IOException;
8 import java.io.StringWriter;
9 import java.util.Iterator;
10 import java.util.List;
12 import javax.xml.parsers.ParserConfigurationException;
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;
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;
33 final class CopyAction extends SelectionListenerAction {
34 private BookmarkClipboard bookmarkClipboard;
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);
48 List list = getSelectedNonResources();
49 Object selection = list.get(0);
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()});
58 } else if (selection instanceof EntityNode) {
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();
66 XMLHelper.write(text, doc);
67 } catch (IOException e) {
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) {
77 } else if (selection instanceof ColumnNode) {
78 String copyText = ""; //$NON-NLS-1$
79 Iterator iter = list.iterator();
80 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
82 while (iter.hasNext()) {
83 Object current = iter.next();
84 if (current instanceof ColumnNode) {
85 ColumnNode column = (ColumnNode) current;
87 copyText += column.getName();
88 if (iter.hasNext()) copyText += QuantumUtil.trasposeEscape(store.getString("copyColumnSeparator")); //$NON-NLS-1$
93 QuantumPlugin.getDefault().getSysClip().setContents(
94 new Object[] { copyText },
95 new Transfer[] { TextTransfer.getInstance()});
100 * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
102 protected boolean updateSelection(IStructuredSelection selection) {
103 boolean enabled = super.updateSelection(selection);