KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > memory > PrintViewTabAction


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
31  * Print view tab toolbar action
32  *
33  * @since 3.0
34  */

35 public class PrintViewTabAction extends AbstractMemoryAction
36 {
37     TableViewer fViewer;
38     
39     private static final String JavaDoc PREFIX = "PrintViewTabAction."; //$NON-NLS-1$
40
private static final String JavaDoc TITLE = PREFIX + "title"; //$NON-NLS-1$
41
private static final String JavaDoc TOOLTIP = PREFIX + "tooltip"; //$NON-NLS-1$
42
private static final String JavaDoc JOB_TITLE = PREFIX + "jobtitle"; //$NON-NLS-1$
43

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

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     /*
57      * draws a Table to a GC which has been initialized with a Printer.
58      * startJob() and startPage() must be called before printTable(...),
59      * and endPage() and endJob() must be called after printTable(...).
60      */

61     private void printTable(TableItem[] itemList, GC printGC, Printer printer) {
62
63         if (!(getViewTab() instanceof ITableMemoryViewTab))
64             return;
65         
66         String JavaDoc tableContents = new String JavaDoc();
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         // return line number after column labels are printed
82
lineNum = printColumnLabels(printGC, lineNum);
83
84         //for all items in the table
85
for (int i=0; i < itemList.length; i++) {
86             tableContents = ""; //$NON-NLS-1$
87
//print all columns for this row
88
for (int j=0; j < numColumns; j++) {
89                 String JavaDoc columnText = labelProvider.getColumnText(itemList[i].getData(), j);
90                 
91                 while (columnText.length() < ((ITableMemoryViewTab)getViewTab()).getColumnSize() * charPerByte)
92                 {
93                      columnText += " "; //$NON-NLS-1$
94
}
95                 tableContents += COLUMN_SEPERATOR + columnText;
96             }
97             printGC.drawString(tableContents, 10, 10+(lineNum*printGC.getFontMetrics().getHeight()));
98             lineNum++;
99
100             // if we've run over the end of a page, start a new one
101
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 JavaDoc tableContents = ""; //$NON-NLS-1$
116
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         //get the column headers
131
for (int k=0; k < numColumns; k++) {
132     
133             StringBuffer JavaDoc columnLabel = new StringBuffer JavaDoc(columns[k].getText());
134             int numBytes = 0;
135     
136             if (k > 0)
137             {
138                 numBytes = ((ITableMemoryViewTab)getViewTab()).getColumnSize();
139             }
140             else
141             {
142                 // special for address column
143
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(" "); //$NON-NLS-1$
163
}
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     /* (non-Javadoc)
174      * @see org.eclipse.jface.action.IAction#run()
175      */

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(); // pop up a system print dialog
188
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         // start the print job and assign it a title
194
printer.startJob(DebugUIMessages.getString(JOB_TITLE) + getViewTab().getTabLabel());
195         printer.startPage(); // start the first page
196
printTable(tableItems, gc, printer); // print all rows of the table
197
printer.endPage(); // end the last page
198
printer.endJob(); // end the print job
199
gc.dispose();
200         printer.dispose();
201         setChecked(false);
202     }
203
204     /* (non-Javadoc)
205      * @see org.eclipse.debug.ui.internal.actions.AbstractMemoryAction#getViewTab()
206      */

207     IMemoryViewTab getViewTab()
208     {
209         return getTopViewTabFromView(IInternalDebugUIConstants.ID_MEMORY_VIEW);
210     }
211 }
212
Popular Tags