1 11 12 package org.eclipse.debug.internal.ui.views.memory; 13 14 import org.eclipse.debug.core.model.IMemoryBlock; 15 import org.eclipse.debug.internal.core.memory.IExtendedMemoryBlock; 16 import org.eclipse.debug.internal.ui.DebugPluginImages; 17 import org.eclipse.debug.internal.ui.DebugUIMessages; 18 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 19 import org.eclipse.jface.viewers.ITableLabelProvider; 20 import org.eclipse.jface.viewers.TableViewer; 21 import org.eclipse.swt.graphics.GC; 22 import org.eclipse.swt.printing.PrintDialog; 23 import org.eclipse.swt.printing.Printer; 24 import org.eclipse.swt.printing.PrinterData; 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 PrintViewTabAction extends AbstractMemoryAction 36 { 37 TableViewer fViewer; 38 39 private static final String PREFIX = "PrintViewTabAction."; private static final String TITLE = PREFIX + "title"; private static final String TOOLTIP = PREFIX + "tooltip"; private static final String JOB_TITLE = PREFIX + "jobtitle"; 44 private static final String COLUMN_SEPERATOR = " "; 46 public PrintViewTabAction() 47 { 48 super(DebugUIMessages.getString(TITLE)); 49 50 setToolTipText(DebugUIMessages.getString(TOOLTIP)); 51 setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_PRINT_TOP_VIEW_TAB)); 52 setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_PRINT_TOP_VIEW_TAB)); 53 setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_PRINT_TOP_VIEW_TAB)); 54 } 55 56 61 private void printTable(TableItem[] itemList, GC printGC, Printer printer) { 62 63 if (!(getViewTab() instanceof ITableMemoryViewTab)) 64 return; 65 66 String tableContents = new String (); 67 int numColumns = ((Table)fViewer.getControl()).getColumnCount(); 68 ITableLabelProvider labelProvider = (ITableLabelProvider)fViewer.getLabelProvider(); 69 int lineNum = 1; 70 71 int charPerByte = 4; 72 if (labelProvider instanceof AbstractTableViewTabLabelProvider) 73 { 74 AbstractMemoryRenderer renderer = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer(); 75 if (renderer instanceof IFixedLengthOutputRenderer) 76 { 77 charPerByte = ((IFixedLengthOutputRenderer)renderer).getNumCharPerByte(); 78 } 79 } 80 81 lineNum = printColumnLabels(printGC, lineNum); 83 84 for (int i=0; i < itemList.length; i++) { 86 tableContents = ""; for (int j=0; j < numColumns; j++) { 89 String columnText = labelProvider.getColumnText(itemList[i].getData(), j); 90 91 while (columnText.length() < ((ITableMemoryViewTab)getViewTab()).getColumnSize() * charPerByte) 92 { 93 columnText += " "; } 95 tableContents += COLUMN_SEPERATOR + columnText; 96 } 97 printGC.drawString(tableContents, 10, 10+(lineNum*printGC.getFontMetrics().getHeight())); 98 lineNum++; 99 100 if (20+lineNum*printGC.getFontMetrics().getHeight() > printer.getClientArea().height) { 102 lineNum=1; 103 printer.endPage(); 104 printer.startPage(); 105 lineNum = printColumnLabels(printGC, lineNum); 106 } 107 } 108 } 109 110 private int printColumnLabels(GC printGC, int lineNum) 111 { 112 if (!(getViewTab() instanceof ITableMemoryViewTab)) 113 return lineNum; 114 115 String tableContents = ""; int numColumns = ((Table)fViewer.getControl()).getColumnCount(); 117 ITableLabelProvider labelProvider = (ITableLabelProvider)fViewer.getLabelProvider(); 118 TableColumn columns[] = ((Table)fViewer.getControl()).getColumns(); 119 120 int charPerByte = 4; 121 if (labelProvider instanceof AbstractTableViewTabLabelProvider) 122 { 123 AbstractMemoryRenderer renderer = ((AbstractTableViewTabLabelProvider)labelProvider).getRenderer(); 124 if (renderer instanceof IFixedLengthOutputRenderer) 125 { 126 charPerByte = ((IFixedLengthOutputRenderer)renderer).getNumCharPerByte(); 127 } 128 } 129 130 for (int k=0; k < numColumns; k++) { 132 133 StringBuffer columnLabel = new StringBuffer (columns[k].getText()); 134 int numBytes = 0; 135 136 if (k > 0) 137 { 138 numBytes = ((ITableMemoryViewTab)getViewTab()).getColumnSize(); 139 } 140 else 141 { 142 IMemoryBlock memBlock = getViewTab().getMemoryBlock(); 144 145 146 if (memBlock instanceof IExtendedMemoryBlock) 147 { 148 numBytes = ((IExtendedMemoryBlock)memBlock).getAddressSize(); 149 150 if (numBytes <= 0) 151 numBytes = 4; 152 } 153 else 154 { 155 numBytes = 4; 156 } 157 158 } 159 160 while (columnLabel.length() < numBytes * charPerByte) 161 { 162 columnLabel.append(" "); } 164 165 tableContents += COLUMN_SEPERATOR + columnLabel; 166 } 167 printGC.drawString(tableContents, 10, 10+(lineNum*printGC.getFontMetrics().getHeight())); 168 lineNum++; 169 170 return lineNum; 171 } 172 173 176 public void run() { 177 178 fViewer = ((MemoryViewTab)getViewTab()).getTableViewer(); 179 180 if(fViewer == null) 181 return; 182 183 if (fViewer.getControl() == null) 184 return; 185 186 PrintDialog printDialog = new PrintDialog(fViewer.getControl().getDisplay().getActiveShell()); 187 PrinterData printerData = printDialog.open(); if (printerData == null) {setChecked(false); return;} 189 Printer printer = new Printer(printerData); 190 GC gc = new GC(printer); 191 TableItem[] tableItems = ((Table)fViewer.getControl()).getItems(); 192 193 printer.startJob(DebugUIMessages.getString(JOB_TITLE) + getViewTab().getTabLabel()); 195 printer.startPage(); printTable(tableItems, gc, printer); printer.endPage(); printer.endJob(); gc.dispose(); 200 printer.dispose(); 201 setChecked(false); 202 } 203 204 207 IMemoryViewTab getViewTab() 208 { 209 return getTopViewTabFromView(IInternalDebugUIConstants.ID_MEMORY_VIEW); 210 } 211 } 212 | Popular Tags |