KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 /**
36  * Print view tab toolbar action
37  *
38  * @since 3.0
39  */

40 public class PrintTableRenderingAction extends Action
41 {
42     private AbstractBaseTableRendering fRendering;
43     private StructuredViewer fViewer;
44     
45     private static final String JavaDoc COLUMN_SEPERATOR = " "; //$NON-NLS-1$
46

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

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         // return line number after column labels are printed
76
lineNum = printColumnLabels(printGC, lineNum);
77
78         //for all items in the table
79
for (int i=0; i < itemList.length; i++) {
80             StringBuffer JavaDoc tableContents = new StringBuffer JavaDoc();
81             //print all columns for this row
82
for (int j=0; j < numColumns; j++) {
83                 String JavaDoc columnText = labelProvider.getColumnText(itemList[i].getData(), j);
84                 
85                 while (columnText.length() < fRendering.getBytesPerColumn() * charsPerByte)
86                 {
87                      columnText += " "; //$NON-NLS-1$
88
}
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 we've run over the end of a page, start a new one
96
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 JavaDoc tableContents = new StringBuffer JavaDoc();
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             // special for address column
120
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         //get the column headers
143
for (int k=0; k < numColumns; k++) {
144     
145             StringBuffer JavaDoc columnLabel = new StringBuffer JavaDoc(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(" "); //$NON-NLS-1$
160
}
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     /* (non-Javadoc)
172      * @see org.eclipse.jface.action.IAction#run()
173      */

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(); // pop up a system print dialog
181
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         // start the print job and assign it a title
187
printer.startJob(DebugUIMessages.PrintViewTabAction_jobtitle + fRendering.getLabel());
188         printer.startPage(); // start the first page
189
printTable(tableItems, gc, printer); // print all rows of the table
190
printer.endPage(); // end the last page
191
printer.endJob(); // end the print job
192
gc.dispose();
193         printer.dispose();
194         setChecked(false);
195     }
196 }
197
Popular Tags