Change variable view behaviour.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / CopyAction.java
1 /*
2  * Created on 28-jul-2003
3  *
4  */
5 package com.quantum.view.tableview;
6
7 import com.quantum.ImageStore;
8 import com.quantum.Messages;
9 import com.quantum.QuantumPlugin;
10
11 import org.eclipse.jface.action.Action;
12 import org.eclipse.swt.dnd.TextTransfer;
13 import org.eclipse.swt.dnd.Transfer;
14 import org.eclipse.swt.widgets.Table;
15 import org.eclipse.swt.widgets.TableItem;
16
17
18 public final class CopyAction extends Action {
19         
20         private static final String LINE_SEPARATOR = System.getProperty("line.separator");
21         
22         private final TableView view;
23         
24         public CopyAction(TableView view) {
25                 super();
26                 this.view = view;
27                 setText(Messages.getString(getClass(), "text"));
28                 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.COPY));
29         }
30         
31         public void run() {
32                 Table table = getTable();
33                 if (table != null) {
34                         TableItem items[] = table.getSelection();
35                         StringBuffer text = new StringBuffer();
36                         for (int i = 0; i < items.length; i++) {
37                                 int columns = table.getColumnCount();
38                                 for (int col = 0; col < columns; col++) {
39                                         text.append(items[i].getText(col));
40                                         text.append('\t');
41                                 }
42                                 text.append(LINE_SEPARATOR);
43                         }
44                         QuantumPlugin.getDefault().getSysClip().setContents(
45                                 new Object[] { text.toString()},
46                                 new Transfer[] { TextTransfer.getInstance()});
47                 }
48         }
49         
50         private Table getTable() {
51                 ResultSetViewer viewer = this.view.getSelectedResultSetViewer();
52                 return viewer == null ? null : viewer.getTable();
53         }
54 }