Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 Oakland Software Incorporated 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 |
* Francis Upton IV, Oakland Software - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.ui.internal.navigator.resources.actions; |
12 |
|
13 |
import org.eclipse.core.resources.IFile; |
14 |
import org.eclipse.core.runtime.IAdaptable; |
15 |
import org.eclipse.jface.text.ITextViewerExtension8; |
16 |
import org.eclipse.jface.viewers.IStructuredSelection; |
17 |
import org.eclipse.swt.custom.StyledTextPrintOptions; |
18 |
import org.eclipse.ui.IEditorInput; |
19 |
import org.eclipse.ui.IEditorPart; |
20 |
import org.eclipse.ui.IWorkbenchCommandConstants; |
21 |
import org.eclipse.ui.PlatformUI; |
22 |
import org.eclipse.ui.actions.SelectionListenerAction; |
23 |
import org.eclipse.ui.navigator.ICommonActionExtensionSite; |
24 |
import org.eclipse.ui.part.FileEditorInput; |
25 |
import org.eclipse.ui.texteditor.AbstractTextEditor; |
26 |
import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds; |
27 |
|
28 |
/** |
29 |
* |
30 |
*/ |
31 |
/* package */class PrintAction extends SelectionListenerAction { |
32 |
|
33 |
private ITextViewerExtension8 textViewer; |
34 |
private AbstractTextEditor textEditor; |
35 |
|
36 |
/** |
37 |
* @param aSite |
38 |
*/ |
39 |
public PrintAction(ICommonActionExtensionSite aSite) { |
40 |
// Text does not matter since this is just for the handler |
41 |
super(""); //$NON-NLS-1$ |
42 |
setActionDefinitionId(IWorkbenchCommandConstants.FILE_PRINT); |
43 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAbstractTextEditorHelpContextIds.PRINT_ACTION); |
44 |
} |
45 |
|
46 |
public void run() { |
47 |
// FINISHME - is this right? |
48 |
StyledTextPrintOptions options= new StyledTextPrintOptions(); |
49 |
options.printTextFontStyle= true; |
50 |
options.printTextForeground= true; |
51 |
options.printTextBackground= true; |
52 |
options.jobName= textEditor.getTitle(); |
53 |
textViewer.print(options); |
54 |
} |
55 |
|
56 |
protected boolean updateSelection(IStructuredSelection sel) { |
57 |
if (!super.updateSelection(sel)) { |
58 |
return false; |
59 |
} |
60 |
|
61 |
Object obj = sel.getFirstElement(); |
62 |
|
63 |
IFile file = null; |
64 |
if (obj instanceof IFile) { |
65 |
file = (IFile) obj; |
66 |
} else if (obj instanceof IAdaptable) { |
67 |
file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class); |
68 |
} |
69 |
|
70 |
if (file != null) { |
71 |
IEditorInput fileInput = new FileEditorInput(file); |
72 |
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findEditor( |
73 |
fileInput); |
74 |
if (editor == null) |
75 |
return false; |
76 |
if (editor instanceof AbstractTextEditor) { |
77 |
textEditor = (AbstractTextEditor) editor; |
78 |
// FINISHME - need to know how to get the text viewer |
79 |
Object sv = null; |
80 |
if ((sv instanceof ITextViewerExtension8)) |
81 |
return false; |
82 |
textViewer = (ITextViewerExtension8) sv; |
83 |
return true; |
84 |
} |
85 |
} |
86 |
return false; |
87 |
} |
88 |
} |