Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df746e5145c7fa071d66013aca6ef17bae7c5ae9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2004, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.debug.internal.ui.views.memory.renderings;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IMemoryBlockExtension;
import org.eclipse.debug.internal.ui.DebugPluginImages;
import org.eclipse.debug.internal.ui.DebugUIMessages;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.PlatformUI;


/**
 * Print view tab toolbar action
 *
 * @since 3.0
 */
public class PrintTableRenderingAction extends Action
{
	private AbstractBaseTableRendering fRendering;
	private StructuredViewer fViewer;

	private static final String COLUMN_SEPERATOR = "  "; //$NON-NLS-1$

	public PrintTableRenderingAction(AbstractBaseTableRendering rendering, StructuredViewer viewer)
	{
		super(DebugUIMessages.PrintViewTabAction_title);
		fRendering = rendering;
		fViewer = viewer;
		setToolTipText(DebugUIMessages.PrintViewTabAction_tooltip);
		setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_PRINT_TOP_VIEW_TAB));
		setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_PRINT_TOP_VIEW_TAB));
		setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_PRINT_TOP_VIEW_TAB));
		PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".PrintViewTabContextAction_context"); //$NON-NLS-1$
	}

	/*
	 * draws a Table to a GC which has been initialized with a Printer.
	 * startJob() and startPage() must be called before printTable(...),
	 * and endPage() and endJob() must be called after printTable(...).
	 */
	protected void printTable(TableItem[] itemList, GC printGC, Printer printer) {


		int numColumns = ((Table)fViewer.getControl()).getColumnCount();
		ITableLabelProvider labelProvider = (ITableLabelProvider)fViewer.getLabelProvider();
		int lineNum = 1;

		int charsPerByte = fRendering.getNumCharsPerByte();
		if (charsPerByte < 0) {
			charsPerByte = 4;
		}

		// return line number after column labels are printed
		lineNum = printColumnLabels(printGC, lineNum);

		//for all items in the table
		for (int i=0; i < itemList.length; i++) {
			StringBuffer tableContents = new StringBuffer();
			//print all columns for this row
			for (int j=0; j < numColumns; j++) {
				String columnText = labelProvider.getColumnText(itemList[i].getData(), j);

				while (columnText.length() < fRendering.getBytesPerColumn() * charsPerByte)
				{
					 columnText += " "; //$NON-NLS-1$
				}
				tableContents.append(COLUMN_SEPERATOR);
				tableContents.append(columnText);
			}
			printGC.drawString(tableContents.toString(), 10, 10+(lineNum*printGC.getFontMetrics().getHeight()));
			lineNum++;

			// if we've run over the end of a page, start a new one
			if (20+lineNum*printGC.getFontMetrics().getHeight() > printer.getClientArea().height) {
				lineNum=1;
				printer.endPage();
				printer.startPage();
				lineNum = printColumnLabels(printGC, lineNum);
			}
		}
	}

	private int printColumnLabels(GC printGC, int lineNum)
	{
		StringBuffer tableContents = new StringBuffer();
		int numColumns = ((Table)fViewer.getControl()).getColumnCount();
		TableColumn columns[] = ((Table)fViewer.getControl()).getColumns();

		int charsPerByte = fRendering.getNumCharsPerByte();
		if (charsPerByte < 0) {
			charsPerByte = 4;
		}

		int addressSizeInBytes = 0;
		TableRenderingContentDescriptor descriptor = fRendering.getAdapter(TableRenderingContentDescriptor.class);
		if (descriptor == null)
		{
			// special for address column
			IMemoryBlock memBlock = fRendering.getMemoryBlock();
			if (memBlock instanceof IMemoryBlockExtension)
			{
				try {
					addressSizeInBytes = ((IMemoryBlockExtension)memBlock).getAddressSize();
				} catch (DebugException e) {
					addressSizeInBytes = 0;
				}

				if (addressSizeInBytes <= 0) {
					addressSizeInBytes = 4;
				}
			}
			else
			{
				addressSizeInBytes = 4;
			}
		}
		else
		{
			addressSizeInBytes = descriptor.getAddressSize();
		}

		//get the column headers
		for (int k=0; k < numColumns; k++) {

			StringBuffer columnLabel = new StringBuffer(columns[k].getText());
			int numBytes = 0;

			if (k > 0)
			{
				numBytes = fRendering.getBytesPerColumn();
			}
			else
			{
				numBytes = addressSizeInBytes;
			}

			 while (columnLabel.length() < numBytes * charsPerByte)
			 {
				 columnLabel.append(" "); //$NON-NLS-1$
			 }

			tableContents.append(COLUMN_SEPERATOR);
			tableContents.append(columnLabel);
		}
		printGC.drawString(tableContents.toString(), 10, 10+(lineNum*printGC.getFontMetrics().getHeight()));
		lineNum++;

		return lineNum;
	}

	@Override
	public void run() {

		if (!(fViewer.getControl() instanceof Table)) {
			return;
		}

		PrintDialog printDialog = new PrintDialog(DebugUIPlugin.getShell());
		PrinterData printerData = printDialog.open();	// pop up a system print dialog
		if (printerData == null) {setChecked(false); return;}
		Printer printer = new Printer(printerData);
		GC gc = new GC(printer);
		TableItem[] tableItems = ((Table)fViewer.getControl()).getItems();

		// start the print job and assign it a title
		printer.startJob(DebugUIMessages.PrintViewTabAction_jobtitle + fRendering.getLabel());
		printer.startPage();					// start the first page
		printTable(tableItems, gc, printer);	// print all rows of the table
		printer.endPage();						// end the last page
		printer.endJob();						// end the print job
		gc.dispose();
		printer.dispose();
		setChecked(false);
	}
}

Back to the top