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 BookmarkClipboard bookmarkClipboard;
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);
47 List list = getSelectedNonResources();
48 Object selection = list.get(0);
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()});
57 } else if (selection instanceof EntityNode) {
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();
65 XMLHelper.write(text, doc);
66 } catch (IOException e) {
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) {
76 } else if (selection instanceof ColumnNode) {
77 String copyText = ""; //$NON-NLS-1$
78 Iterator iter = list.iterator();
79 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
81 while (iter.hasNext()) {
82 Object current = iter.next();
83 if (current instanceof ColumnNode) {
84 ColumnNode column = (ColumnNode) current;
86 copyText += column.getName();
87 if (iter.hasNext()) copyText += QuantumUtil.trasposeEscape(store.getString("copyColumnSeparator")); //$NON-NLS-1$
92 QuantumPlugin.getDefault().getSysClip().setContents(
93 new Object[] { copyText },
94 new Transfer[] { TextTransfer.getInstance()});
99 * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
101 protected boolean updateSelection(IStructuredSelection selection) {
102 boolean enabled = super.updateSelection(selection);