KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > memory > renderings > CopyTableRenderingToClipboardAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12
13 package org.eclipse.debug.internal.ui.views.memory.renderings;
14
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.model.IMemoryBlock;
17 import org.eclipse.debug.core.model.IMemoryBlockExtension;
18 import org.eclipse.debug.internal.ui.DebugPluginImages;
19 import org.eclipse.debug.internal.ui.DebugUIMessages;
20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
21 import org.eclipse.debug.ui.IDebugUIConstants;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.viewers.ITableLabelProvider;
24 import org.eclipse.jface.viewers.StructuredViewer;
25 import org.eclipse.swt.dnd.Clipboard;
26 import org.eclipse.swt.dnd.TextTransfer;
27 import org.eclipse.swt.dnd.Transfer;
28 import org.eclipse.swt.widgets.Table;
29 import org.eclipse.swt.widgets.TableColumn;
30 import org.eclipse.swt.widgets.TableItem;
31 import org.eclipse.ui.PlatformUI;
32
33
34 /**
35  * Toobar Copy View Tab to Clipboard action
36  *
37  * @since 3.0
38  */

39 public class CopyTableRenderingToClipboardAction extends Action
40 {
41     private final String JavaDoc COLUMN_SEPERATOR = " "; //$NON-NLS-1$
42

43     protected AbstractBaseTableRendering fRendering;
44     protected StructuredViewer fViewer;
45     
46     public CopyTableRenderingToClipboardAction(AbstractBaseTableRendering rendering, StructuredViewer viewer)
47     {
48         super();
49         fRendering = rendering;
50         fViewer = viewer;
51         setText(DebugUIMessages.CopyViewToClipboardAction_title);
52         setToolTipText(DebugUIMessages.CopyViewToClipboardAction_tooltip);
53         setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_COPY_VIEW_TO_CLIPBOARD));
54         setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_COPY_VIEW_TO_CLIPBOARD));
55         setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_COPY_VIEW_TO_CLIPBOARD));
56         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".PrintViewTabContextAction_context"); //$NON-NLS-1$
57
}
58
59     protected String JavaDoc concatenateTableAsString(TableItem[] itemList) {
60         if (itemList.length == 0) return null;
61
62         StringBuffer JavaDoc tableContents = new StringBuffer JavaDoc();
63         
64         Table table = (Table)fViewer.getControl();
65         int numColumns = table.getColumnCount();
66         ITableLabelProvider labelProvider = (ITableLabelProvider)fViewer.getLabelProvider();
67         TableColumn columns[] = table.getColumns();
68         
69         // get title of view tab
70
String JavaDoc label = fRendering.getLabel();
71         tableContents.append(label);
72         tableContents.append(System.getProperty("line.separator")); //$NON-NLS-1$
73
tableContents.append(COLUMN_SEPERATOR);
74         
75         int charsPerByte = fRendering.getNumCharsPerByte();
76         if (charsPerByte < 0)
77             charsPerByte = 4;
78         
79         //get the column headers and line them up properly
80
for (int k=0; k < numColumns; k++) {
81             
82             StringBuffer JavaDoc columnLabel = new StringBuffer JavaDoc(columns[k].getText());
83             int numBytes = 0;
84             int numChars = 0;
85             
86             if (k > 0)
87             {
88                 numBytes = fRendering.getBytesPerColumn();
89                 numChars = numBytes * charsPerByte;
90             }
91             else
92             {
93                 // special for address column
94
IMemoryBlock memBlock = fRendering.getMemoryBlock();
95                 if (memBlock instanceof IMemoryBlockExtension)
96                 {
97                     TableRenderingContentDescriptor descriptor = (TableRenderingContentDescriptor)fRendering.getAdapter(TableRenderingContentDescriptor.class);
98                     if (descriptor == null)
99                         {
100                         try {
101                             numBytes = ((IMemoryBlockExtension)memBlock).getAddressSize();
102                         } catch (DebugException e) {
103                             numBytes = 0;
104                         }
105                     }
106                     else
107                         numBytes = descriptor.getAddressSize();
108                     
109                     // check address size
110
if (numBytes <= 0)
111                         numBytes = 4;
112                 }
113                 else
114                 {
115                     numBytes = 4;
116                 }
117                 numChars = numBytes*2;
118                 
119             }
120             
121              while (columnLabel.length() < numChars)
122              {
123                  columnLabel.append(" "); //$NON-NLS-1$
124
}
125                 
126             tableContents.append(columnLabel);
127             tableContents.append(COLUMN_SEPERATOR);
128         }
129         
130         tableContents.append(System.getProperty("line.separator")); //$NON-NLS-1$
131
StringBuffer JavaDoc temp;
132             
133         //get the column contents from all the rows
134
for (int i=0; i < itemList.length; i++) {
135             for (int j=0; j < numColumns; j++) {
136                 tableContents.append(COLUMN_SEPERATOR);
137                 
138                 temp = new StringBuffer JavaDoc(labelProvider.getColumnText(itemList[i].getData(), j));
139                 
140                 if (j>0)
141                 {
142                     int numBytes = fRendering.getBytesPerColumn();
143                     int numChars = numBytes * charsPerByte;
144                     while (temp.length() < numChars)
145                     {
146                         temp.append(" "); //$NON-NLS-1$
147
}
148                 }
149                 
150                 tableContents.append(temp);
151             }
152             tableContents.append(System.getProperty("line.separator")); //$NON-NLS-1$
153
}
154         return tableContents.toString();
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.jface.action.IAction#run()
159      */

160     public void run() {
161         
162         if (fRendering == null)
163             return;
164         
165         if (! (fViewer.getControl() instanceof Table))
166             return;
167         
168         Table table = (Table)fViewer.getControl();
169         
170         if (table == null)
171             return;
172         Clipboard clip= null;
173         try {
174             clip = new Clipboard(table.getDisplay());
175             TableItem[] tableItems = table.getItems();
176             String JavaDoc tableAsString = new String JavaDoc();
177             tableAsString = concatenateTableAsString(tableItems);
178             if (!tableAsString.equals("")) { //$NON-NLS-1$
179
TextTransfer plainTextTransfer = TextTransfer.getInstance();
180                 clip.setContents(new Object JavaDoc[] {tableAsString}, new Transfer[] {plainTextTransfer});
181             }
182         } finally {
183             if (clip != null) {
184                 clip.dispose();
185             }
186         }
187     }
188 }
189
Popular Tags