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.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;
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;
32 final class CopyAction extends SelectionListenerAction {
33 private IViewPart view;
34 private BookmarkClipboard bookmarkClipboard;
39 public CopyAction(IViewPart view, BookmarkClipboard bookmarkClipboard,
40 ISelectionProvider selectionProvider) {
41 super(Messages.getString(CopyAction.class.getName() + ".text"));
42 setImageDescriptor(QuantumPlugin.getImageDescriptor("copy.gif"));
44 this.bookmarkClipboard = bookmarkClipboard;
45 selectionProvider.addSelectionChangedListener(this);
49 List list = getSelectedNonResources();
50 Object selection = list.get(0);
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()});
59 } else if (selection instanceof EntityNode) {
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();
67 XMLHelper.createDOMSerializer(text).serialize(doc);
68 } catch (IOException e) {
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) {
78 } else if (selection instanceof ColumnNode) {
79 String copyText = ""; //$NON-NLS-1$
80 Iterator iter = list.iterator();
81 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
83 while (iter.hasNext()) {
84 Object current = iter.next();
85 if (current instanceof ColumnNode) {
86 ColumnNode column = (ColumnNode) current;
88 copyText += column.getName();
89 if (iter.hasNext()) copyText += QuantumUtil.trasposeEscape(store.getString("copyColumnSeparator")); //$NON-NLS-1$
94 QuantumPlugin.getDefault().getSysClip().setContents(
95 new Object[] { copyText },
96 new Transfer[] { TextTransfer.getInstance()});
101 * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
103 protected boolean updateSelection(IStructuredSelection selection) {
104 boolean enabled = super.updateSelection(selection);