KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

35 public class CopyViewTabToClipboardAction extends AbstractMemoryAction
36 {
37     private final String JavaDoc PREFIX = "CopyViewToClipboardAction."; //$NON-NLS-1$
38
private final String JavaDoc TITLE = PREFIX + "title"; //$NON-NLS-1$
39
private final String JavaDoc TOOLTIP = PREFIX + "tooltip"; //$NON-NLS-1$
40

41     private final String JavaDoc COLUMN_SEPERATOR = " "; //$NON-NLS-1$
42

43     public CopyViewTabToClipboardAction()
44     {
45         super();
46         
47         setText(DebugUIMessages.getString(TITLE));
48         setToolTipText(DebugUIMessages.getString(TOOLTIP));
49         setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_COPY_VIEW_TO_CLIPBOARD));
50         setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_COPY_VIEW_TO_CLIPBOARD));
51         setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_COPY_VIEW_TO_CLIPBOARD));
52     }
53
54     private String JavaDoc concatenateTableAsString(TableItem[] itemList) {
55         if (itemList.length == 0) return null;
56
57         StringBuffer JavaDoc tableContents = new StringBuffer JavaDoc();
58         TableViewer viewer = ((MemoryViewTab)getViewTab()).getTableViewer();
59         Table table = viewer.getTable();
60         int numColumns = table.getColumnCount();
61         ITableLabelProvider labelProvider = (ITableLabelProvider)viewer.getLabelProvider();
62         TableColumn columns[] = table.getColumns();
63         
64         // get title of view tab
65
String JavaDoc tabLabel = getViewTab().getTabLabel();
66         tableContents.append(tabLabel);
67         tableContents.append(System.getProperty("line.separator")); //$NON-NLS-1$
68
tableContents.append(COLUMN_SEPERATOR);
69         
70         int charPerByte = 4;
71         if (labelProvider instanceof AbstractTableViewTabLabelProvider)
72         {
73             AbstractMemoryRenderer renderer = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer();
74             if (renderer instanceof IFixedLengthOutputRenderer)
75             {
76                 charPerByte = ((IFixedLengthOutputRenderer)renderer).getNumCharPerByte();
77             }
78         }
79         
80         //get the column headers and line them up properly
81
for (int k=0; k < numColumns; k++) {
82             
83             StringBuffer JavaDoc columnLabel = new StringBuffer JavaDoc(columns[k].getText());
84             int numBytes = 0;
85             int numChars = 0;
86             
87             if (k > 0)
88             {
89                 if (!(getViewTab() instanceof ITableMemoryViewTab))
90                 {
91                     return ""; //$NON-NLS-1$
92
}
93                 
94                 numBytes = ((ITableMemoryViewTab)getViewTab()).getColumnSize();
95                 numChars = numBytes * charPerByte;
96             }
97             else
98             {
99                 // special for address column
100
IMemoryBlock memBlock = getViewTab().getMemoryBlock();
101                 
102                 
103                 if (memBlock instanceof IExtendedMemoryBlock)
104                 {
105                     numBytes = ((IExtendedMemoryBlock)memBlock).getAddressSize();
106                     
107                     // check address size
108
if (numBytes <= 0)
109                         numBytes = 4;
110                 }
111                 else
112                 {
113                     numBytes = 4;
114                 }
115                 numChars = numBytes*2;
116                 
117             }
118             
119              while (columnLabel.length() < numChars)
120              {
121                  columnLabel.append(" "); //$NON-NLS-1$
122
}
123                 
124             tableContents.append(columnLabel);
125             tableContents.append(COLUMN_SEPERATOR);
126         }
127         
128         tableContents.append(System.getProperty("line.separator")); //$NON-NLS-1$
129
StringBuffer JavaDoc temp;
130             
131         //get the column contents from all the rows
132
for (int i=0; i < itemList.length; i++) {
133             for (int j=0; j < numColumns; j++) {
134                 tableContents.append(COLUMN_SEPERATOR);
135                 
136                 temp = new StringBuffer JavaDoc(labelProvider.getColumnText(itemList[i].getData(), j));
137                 
138                 if (j>0)
139                 {
140                     if (!(getViewTab() instanceof ITableMemoryViewTab))
141                         return ""; //$NON-NLS-1$
142

143                     int numBytes = ((ITableMemoryViewTab)getViewTab()).getColumnSize();
144                     int numChars = numBytes * charPerByte;
145                     while (temp.length() < numChars)
146                     {
147                         temp.append(" "); //$NON-NLS-1$
148
}
149                 }
150                 
151                 tableContents.append(temp);
152             }
153             tableContents.append(System.getProperty("line.separator")); //$NON-NLS-1$
154
}
155         return tableContents.toString();
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.jface.action.IAction#run()
160      */

161     public void run() {
162         
163         if (getViewTab() == null)
164             return;
165         
166         TableViewer viewer = ((MemoryViewTab)getViewTab()).getTableViewer();
167         
168         if (viewer == null)
169             return;
170         
171         Table table = viewer.getTable();
172         
173         if (table == null)
174             return;
175         
176         Clipboard clip = new Clipboard(table.getDisplay());
177         TableItem[] tableItems = table.getItems();
178         String JavaDoc tableAsString = new String JavaDoc();
179         tableAsString = concatenateTableAsString(tableItems);
180         if (!tableAsString.equals("")) { //$NON-NLS-1$
181
TextTransfer plainTextTransfer = TextTransfer.getInstance();
182             clip.setContents(new Object JavaDoc[] {tableAsString}, new Transfer[] {plainTextTransfer});
183         }
184     }
185
186     /* (non-Javadoc)
187      * @see org.eclipse.debug.ui.internal.actions.AbstractMemoryAction#getViewTab()
188      */

189     IMemoryViewTab getViewTab()
190     {
191         return getTopViewTabFromView(IInternalDebugUIConstants.ID_MEMORY_VIEW);
192     }
193 }
194
Popular Tags