1 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 35 public class CopyViewTabToClipboardAction extends AbstractMemoryAction 36 { 37 private final String PREFIX = "CopyViewToClipboardAction."; private final String TITLE = PREFIX + "title"; private final String TOOLTIP = PREFIX + "tooltip"; 41 private final String COLUMN_SEPERATOR = " "; 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 concatenateTableAsString(TableItem[] itemList) { 55 if (itemList.length == 0) return null; 56 57 StringBuffer tableContents = new StringBuffer (); 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 String tabLabel = getViewTab().getTabLabel(); 66 tableContents.append(tabLabel); 67 tableContents.append(System.getProperty("line.separator")); 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 for (int k=0; k < numColumns; k++) { 82 83 StringBuffer columnLabel = new StringBuffer (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 ""; } 93 94 numBytes = ((ITableMemoryViewTab)getViewTab()).getColumnSize(); 95 numChars = numBytes * charPerByte; 96 } 97 else 98 { 99 IMemoryBlock memBlock = getViewTab().getMemoryBlock(); 101 102 103 if (memBlock instanceof IExtendedMemoryBlock) 104 { 105 numBytes = ((IExtendedMemoryBlock)memBlock).getAddressSize(); 106 107 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(" "); } 123 124 tableContents.append(columnLabel); 125 tableContents.append(COLUMN_SEPERATOR); 126 } 127 128 tableContents.append(System.getProperty("line.separator")); StringBuffer temp; 130 131 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 (labelProvider.getColumnText(itemList[i].getData(), j)); 137 138 if (j>0) 139 { 140 if (!(getViewTab() instanceof ITableMemoryViewTab)) 141 return ""; 143 int numBytes = ((ITableMemoryViewTab)getViewTab()).getColumnSize(); 144 int numChars = numBytes * charPerByte; 145 while (temp.length() < numChars) 146 { 147 temp.append(" "); } 149 } 150 151 tableContents.append(temp); 152 } 153 tableContents.append(System.getProperty("line.separator")); } 155 return tableContents.toString(); 156 } 157 158 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 tableAsString = new String (); 179 tableAsString = concatenateTableAsString(tableItems); 180 if (!tableAsString.equals("")) { TextTransfer plainTextTransfer = TextTransfer.getInstance(); 182 clip.setContents(new Object [] {tableAsString}, new Transfer[] {plainTextTransfer}); 183 } 184 } 185 186 189 IMemoryViewTab getViewTab() 190 { 191 return getTopViewTabFromView(IInternalDebugUIConstants.ID_MEMORY_VIEW); 192 } 193 } 194 | Popular Tags |