1 11 12 package org.eclipse.debug.internal.ui.views.memory.renderings; 13 14 import org.eclipse.debug.core.DebugException; 15 import org.eclipse.debug.core.model.IMemoryBlock; 16 import org.eclipse.debug.core.model.IMemoryBlockExtension; 17 import org.eclipse.debug.internal.ui.DebugPluginImages; 18 import org.eclipse.debug.internal.ui.DebugUIMessages; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 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.graphics.GC; 26 import org.eclipse.swt.printing.PrintDialog; 27 import org.eclipse.swt.printing.Printer; 28 import org.eclipse.swt.printing.PrinterData; 29 import org.eclipse.swt.widgets.Table; 30 import org.eclipse.swt.widgets.TableColumn; 31 import org.eclipse.swt.widgets.TableItem; 32 import org.eclipse.ui.PlatformUI; 33 34 35 40 public class PrintTableRenderingAction extends Action 41 { 42 private AbstractBaseTableRendering fRendering; 43 private StructuredViewer fViewer; 44 45 private static final String COLUMN_SEPERATOR = " "; 47 public PrintTableRenderingAction(AbstractBaseTableRendering rendering, StructuredViewer viewer) 48 { 49 super(DebugUIMessages.PrintViewTabAction_title); 50 fRendering = rendering; 51 fViewer = viewer; 52 setToolTipText(DebugUIMessages.PrintViewTabAction_tooltip); 53 setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_PRINT_TOP_VIEW_TAB)); 54 setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_PRINT_TOP_VIEW_TAB)); 55 setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_PRINT_TOP_VIEW_TAB)); 56 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".PrintViewTabContextAction_context"); } 58 59 64 protected void printTable(TableItem[] itemList, GC printGC, Printer printer) { 65 66 67 int numColumns = ((Table)fViewer.getControl()).getColumnCount(); 68 ITableLabelProvider labelProvider = (ITableLabelProvider)fViewer.getLabelProvider(); 69 int lineNum = 1; 70 71 int charsPerByte = fRendering.getNumCharsPerByte(); 72 if (charsPerByte < 0) 73 charsPerByte = 4; 74 75 lineNum = printColumnLabels(printGC, lineNum); 77 78 for (int i=0; i < itemList.length; i++) { 80 StringBuffer tableContents = new StringBuffer (); 81 for (int j=0; j < numColumns; j++) { 83 String columnText = labelProvider.getColumnText(itemList[i].getData(), j); 84 85 while (columnText.length() < fRendering.getBytesPerColumn() * charsPerByte) 86 { 87 columnText += " "; } 89 tableContents.append(COLUMN_SEPERATOR); 90 tableContents.append(columnText); 91 } 92 printGC.drawString(tableContents.toString(), 10, 10+(lineNum*printGC.getFontMetrics().getHeight())); 93 lineNum++; 94 95 if (20+lineNum*printGC.getFontMetrics().getHeight() > printer.getClientArea().height) { 97 lineNum=1; 98 printer.endPage(); 99 printer.startPage(); 100 lineNum = printColumnLabels(printGC, lineNum); 101 } 102 } 103 } 104 105 private int printColumnLabels(GC printGC, int lineNum) 106 { 107 StringBuffer tableContents = new StringBuffer (); 108 int numColumns = ((Table)fViewer.getControl()).getColumnCount(); 109 TableColumn columns[] = ((Table)fViewer.getControl()).getColumns(); 110 111 int charsPerByte = fRendering.getNumCharsPerByte(); 112 if (charsPerByte < 0) 113 charsPerByte = 4; 114 115 int addressSizeInBytes = 0; 116 TableRenderingContentDescriptor descriptor = (TableRenderingContentDescriptor)fRendering.getAdapter(TableRenderingContentDescriptor.class); 117 if (descriptor == null) 118 { 119 IMemoryBlock memBlock = fRendering.getMemoryBlock(); 121 if (memBlock instanceof IMemoryBlockExtension) 122 { 123 try { 124 addressSizeInBytes = ((IMemoryBlockExtension)memBlock).getAddressSize(); 125 } catch (DebugException e) { 126 addressSizeInBytes = 0; 127 } 128 129 if (addressSizeInBytes <= 0) 130 addressSizeInBytes = 4; 131 } 132 else 133 { 134 addressSizeInBytes = 4; 135 } 136 } 137 else 138 { 139 addressSizeInBytes = descriptor.getAddressSize(); 140 } 141 142 for (int k=0; k < numColumns; k++) { 144 145 StringBuffer columnLabel = new StringBuffer (columns[k].getText()); 146 int numBytes = 0; 147 148 if (k > 0) 149 { 150 numBytes = fRendering.getBytesPerColumn(); 151 } 152 else 153 { 154 numBytes = addressSizeInBytes; 155 } 156 157 while (columnLabel.length() < numBytes * charsPerByte) 158 { 159 columnLabel.append(" "); } 161 162 tableContents.append(COLUMN_SEPERATOR); 163 tableContents.append(columnLabel); 164 } 165 printGC.drawString(tableContents.toString(), 10, 10+(lineNum*printGC.getFontMetrics().getHeight())); 166 lineNum++; 167 168 return lineNum; 169 } 170 171 174 public void run() { 175 176 if (!(fViewer.getControl() instanceof Table)) 177 return; 178 179 PrintDialog printDialog = new PrintDialog(DebugUIPlugin.getShell()); 180 PrinterData printerData = printDialog.open(); if (printerData == null) {setChecked(false); return;} 182 Printer printer = new Printer(printerData); 183 GC gc = new GC(printer); 184 TableItem[] tableItems = ((Table)fViewer.getControl()).getItems(); 185 186 printer.startJob(DebugUIMessages.PrintViewTabAction_jobtitle + fRendering.getLabel()); 188 printer.startPage(); printTable(tableItems, gc, printer); printer.endPage(); printer.endJob(); gc.dispose(); 193 printer.dispose(); 194 setChecked(false); 195 } 196 } 197 | Popular Tags |