1 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 39 public class CopyTableRenderingToClipboardAction extends Action 40 { 41 private final String COLUMN_SEPERATOR = " "; 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"); } 58 59 protected String concatenateTableAsString(TableItem[] itemList) { 60 if (itemList.length == 0) return null; 61 62 StringBuffer tableContents = new StringBuffer (); 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 String label = fRendering.getLabel(); 71 tableContents.append(label); 72 tableContents.append(System.getProperty("line.separator")); tableContents.append(COLUMN_SEPERATOR); 74 75 int charsPerByte = fRendering.getNumCharsPerByte(); 76 if (charsPerByte < 0) 77 charsPerByte = 4; 78 79 for (int k=0; k < numColumns; k++) { 81 82 StringBuffer columnLabel = new StringBuffer (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 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 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(" "); } 125 126 tableContents.append(columnLabel); 127 tableContents.append(COLUMN_SEPERATOR); 128 } 129 130 tableContents.append(System.getProperty("line.separator")); StringBuffer temp; 132 133 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 (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(" "); } 148 } 149 150 tableContents.append(temp); 151 } 152 tableContents.append(System.getProperty("line.separator")); } 154 return tableContents.toString(); 155 } 156 157 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 tableAsString = new String (); 177 tableAsString = concatenateTableAsString(tableItems); 178 if (!tableAsString.equals("")) { TextTransfer plainTextTransfer = TextTransfer.getInstance(); 180 clip.setContents(new Object [] {tableAsString}, new Transfer[] {plainTextTransfer}); 181 } 182 } finally { 183 if (clip != null) { 184 clip.dispose(); 185 } 186 } 187 } 188 } 189 | Popular Tags |