IDEA-109869 (diagnostic; cleanup)

This commit is contained in:
Roman Shevchenko
2013-07-03 15:34:39 +02:00
parent b3d260c9d6
commit 73b3d2d5a6
4 changed files with 117 additions and 112 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeEditor.printing;
import com.intellij.openapi.progress.ProgressIndicator;
import java.awt.print.Printable;
abstract class BasePainter implements Printable {
protected ProgressIndicator myProgress;
public void setProgress(ProgressIndicator progress) {
myProgress = progress;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeEditor.printing;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiFile;
@@ -28,44 +26,39 @@ import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.List;
class MultiFilePainter implements Printable{
class MultiFilePainter extends BasePainter {
private final List<Pair<PsiFile, Editor>> myFilesList;
private int myFileIndex = 0;
private int myStartPageIndex = 0;
private Printable myTextPainter = null;
private ProgressIndicator myProgress;
public MultiFilePainter(List<Pair<PsiFile, Editor>> filesList) {
myFilesList = filesList;
}
public void setProgress(ProgressIndicator progress) {
myProgress = progress;
}
@Override
public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (myProgress.isCanceled()) {
return Printable.NO_SUCH_PAGE;
}
while(myFileIndex < myFilesList.size()) {
if(myTextPainter == null) {
while (myFileIndex < myFilesList.size()) {
if (myTextPainter == null) {
Pair<PsiFile, Editor> pair = myFilesList.get(myFileIndex);
myTextPainter = PrintManager.initTextPainter(pair.first, pair.second);
}
if (myTextPainter != null) {
((TextPainter)myTextPainter).setProgress(myProgress);
int ret = 0;
try {
ret = myTextPainter.print(g, pageFormat, pageIndex - myStartPageIndex);
}
catch (ProcessCanceledException ignored) {
}
catch (ProcessCanceledException ignored) { }
if (myProgress.isCanceled()) {
return Printable.NO_SUCH_PAGE;
}
if(ret == Printable.PAGE_EXISTS) {
if (ret == Printable.PAGE_EXISTS) {
return Printable.PAGE_EXISTS;
}
myTextPainter = null;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeEditor.printing;
import com.intellij.CommonBundle;
import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
@@ -29,9 +31,9 @@ import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
@@ -40,7 +42,6 @@ import com.intellij.psi.util.PsiUtilBase;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.print.*;
import java.util.List;
@@ -49,59 +50,62 @@ class PrintManager {
public static void executePrint(DataContext dataContext) {
final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
if (project == null) return;
final PrinterJob printerJob = PrinterJob.getPrinterJob();
final PsiDirectory[] psiDirectory = new PsiDirectory[1];
PsiDirectory[] psiDirectory = new PsiDirectory[1];
PsiElement psiElement = LangDataKeys.PSI_ELEMENT.getData(dataContext);
if(psiElement instanceof PsiDirectory) {
if (psiElement instanceof PsiDirectory) {
psiDirectory[0] = (PsiDirectory)psiElement;
}
final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(dataContext);
final String[] shortFileName = new String[1];
final String[] directoryName = new String[1];
if(psiFile != null || psiDirectory[0] != null) {
if(psiFile != null) {
shortFileName[0] = psiFile.getVirtualFile().getName();
if(psiDirectory[0] == null) {
PsiFile psiFile = LangDataKeys.PSI_FILE.getData(dataContext);
String[] shortFileName = new String[1];
String[] directoryName = new String[1];
if (psiFile != null || psiDirectory[0] != null) {
if (psiFile != null) {
shortFileName[0] = psiFile.getName();
if (psiDirectory[0] == null) {
psiDirectory[0] = psiFile.getContainingDirectory();
}
}
if(psiDirectory[0] != null) {
if (psiDirectory[0] != null) {
directoryName[0] = psiDirectory[0].getVirtualFile().getPresentableUrl();
}
}
Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
String text = null;
if(editor != null) {
if(editor.getSelectionModel().hasSelection()) {
if (editor != null) {
if (editor.getSelectionModel().hasSelection()) {
text = CodeEditorBundle.message("print.selected.text.radio");
} else {
}
else {
text = psiFile == null ? "Console text" : null;
}
}
PrintDialog printDialog = new PrintDialog(shortFileName[0], directoryName[0], text, project);
printDialog.reset();
printDialog.show();
if(!printDialog.isOK()) {
if (!printDialog.isOK()) {
return;
}
printDialog.apply();
final PageFormat pageFormat = createPageFormat();
PrintSettings printSettings = PrintSettings.getInstance();
Printable painter;
final BasePainter painter;
if(printSettings.getPrintScope() != PrintSettings.PRINT_DIRECTORY) {
PrintSettings printSettings = PrintSettings.getInstance();
if (printSettings.getPrintScope() != PrintSettings.PRINT_DIRECTORY) {
if (psiFile == null && editor == null) return;
TextPainter textPainter = psiFile != null ? initTextPainter(psiFile, editor) : initTextPainter((DocumentEx)editor.getDocument(), project);
if (textPainter == null) return;
if(printSettings.getPrintScope() == PrintSettings.PRINT_SELECTED_TEXT && editor != null && editor.getSelectionModel().hasSelection()) {
if (printSettings.getPrintScope() == PrintSettings.PRINT_SELECTED_TEXT &&
editor != null &&
editor.getSelectionModel().hasSelection()) {
int firstLine = editor.getDocument().getLineNumber(editor.getSelectionModel().getSelectionStart());
textPainter.setSegment(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd(), firstLine+1);
textPainter.setSegment(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd(), firstLine + 1);
}
painter = textPainter;
}
@@ -109,71 +113,58 @@ class PrintManager {
List<Pair<PsiFile, Editor>> filesList = ContainerUtil.newArrayList();
boolean isRecursive = printSettings.isIncludeSubdirectories();
addToPsiFileList(psiDirectory[0], filesList, isRecursive);
painter = new MultiFilePainter(filesList);
}
final Printable painter0 = painter;
Pageable document = new Pageable(){
Pageable document = new Pageable() {
@Override
public int getNumberOfPages() {
return Pageable.UNKNOWN_NUMBER_OF_PAGES;
}
@Override
public PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException {
public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
return pageFormat;
}
@Override
public Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException {
return painter0;
public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
return painter;
}
};
final PrinterJob printerJob = PrinterJob.getPrinterJob();
try {
printerJob.setPageable(document);
printerJob.setPrintable(painter, pageFormat);
if(!printerJob.printDialog()) {
if (!printerJob.printDialog()) {
return;
}
} catch (Exception e) {
// In case print dialog is not supported on some platform. Strange thing but there was a checking
// for Windows only...
}
catch (Exception e) {
LOG.warn(e);
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
ProgressManager.getInstance().run(new Task.Backgroundable(project, CodeEditorBundle.message("print.progress"), true, PerformInBackgroundOption.ALWAYS_BACKGROUND) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
if (painter0 instanceof MultiFilePainter) {
((MultiFilePainter)painter0).setProgress(progress);
ProgressManager.getInstance()
.run(new Task.Backgroundable(project, CodeEditorBundle.message("print.progress"), true, PerformInBackgroundOption.ALWAYS_BACKGROUND) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
painter.setProgress(indicator);
printerJob.print();
}
else {
((TextPainter)painter0).setProgress(progress);
catch (ProcessCanceledException e) {
LOG.info("Cancelled");
printerJob.cancel();
}
catch (Exception e) {
Notifications.Bus.notify(new Notification("Print", CommonBundle.getErrorTitle(), e.getMessage(), NotificationType.ERROR));
LOG.warn(e);
}
printerJob.print();
}
catch(final PrinterException e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Messages.showErrorDialog(project, e.getMessage(), CommonBundle.getErrorTitle());
}
});
LOG.info(e);
}
catch(ProcessCanceledException e) {
printerJob.cancel();
}
}
});
});
}
private static void addToPsiFileList(PsiDirectory psiDirectory, List<Pair<PsiFile, Editor>> filesList, boolean isRecursive) {
@@ -190,27 +181,26 @@ class PrintManager {
}
}
private static PageFormat createPageFormat() {
PrintSettings printSettings = PrintSettings.getInstance();
PageFormat pageFormat = new PageFormat();
Paper paper = new Paper();
String paperSize = printSettings.PAPER_SIZE;
double paperWidth = PageSizes.getWidth(paperSize)*72;
double paperHeight = PageSizes.getHeight(paperSize)*72;
double leftMargin = printSettings.LEFT_MARGIN*72;
double rightMargin = printSettings.RIGHT_MARGIN*72;
double topMargin = printSettings.TOP_MARGIN*72;
double bottomMargin = printSettings.BOTTOM_MARGIN*72;
double paperWidth = PageSizes.getWidth(paperSize) * 72;
double paperHeight = PageSizes.getHeight(paperSize) * 72;
double leftMargin = printSettings.LEFT_MARGIN * 72;
double rightMargin = printSettings.RIGHT_MARGIN * 72;
double topMargin = printSettings.TOP_MARGIN * 72;
double bottomMargin = printSettings.BOTTOM_MARGIN * 72;
paper.setSize(paperWidth, paperHeight);
if(printSettings.PORTRAIT_LAYOUT) {
if (printSettings.PORTRAIT_LAYOUT) {
pageFormat.setOrientation(PageFormat.PORTRAIT);
paperWidth -= leftMargin + rightMargin;
paperHeight -= topMargin + bottomMargin;
paper.setImageableArea(leftMargin, topMargin, paperWidth, paperHeight);
}
else{
else {
pageFormat.setOrientation(PageFormat.LANDSCAPE);
paperWidth -= topMargin + bottomMargin;
paperHeight -= leftMargin + rightMargin;
@@ -220,7 +210,7 @@ class PrintManager {
return pageFormat;
}
public static TextPainter initTextPainter(final PsiFile psiFile, final Editor editor) {
static TextPainter initTextPainter(final PsiFile psiFile, final Editor editor) {
return ApplicationManager.getApplication().runReadAction(new Computable<TextPainter>() {
@Override
public TextPainter compute() {
@@ -230,15 +220,16 @@ class PrintManager {
}
private static TextPainter doInitTextPainter(final PsiFile psiFile, final Editor editor) {
final String fileName = psiFile.getVirtualFile().getPresentableUrl();
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) return null;
DocumentEx doc = (DocumentEx)PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile);
if (doc == null) return null;
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(psiFile.getProject(), psiFile.getVirtualFile());
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(psiFile.getProject(), virtualFile);
highlighter.setText(doc.getCharsSequence());
return new TextPainter(doc, highlighter, fileName, psiFile, psiFile.getFileType(), editor);
return new TextPainter(doc, highlighter, virtualFile.getPresentableUrl(), psiFile, psiFile.getFileType(), editor);
}
public static TextPainter initTextPainter(@NotNull final DocumentEx doc, final Project project) {
private static TextPainter initTextPainter(@NotNull final DocumentEx doc, final Project project) {
final TextPainter[] res = new TextPainter[1];
ApplicationManager.getApplication().runReadAction(
new Runnable() {
@@ -252,8 +243,8 @@ class PrintManager {
}
private static TextPainter doInitTextPainter(@NotNull final DocumentEx doc, Project project) {
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(project, "unknown");
highlighter.setText(doc.getCharsSequence());
return new TextPainter(doc, highlighter, "unknown", project, FileTypes.PLAIN_TEXT, null);
}
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(project, "unknown");
highlighter.setText(doc.getCharsSequence());
return new TextPainter(doc, highlighter, "unknown", project, FileTypes.PLAIN_TEXT, null);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeEditor.printing;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
@@ -25,7 +24,6 @@ import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
@@ -48,12 +46,10 @@ import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.List;
public class TextPainter implements Printable {
class TextPainter extends BasePainter {
private final DocumentEx myDocument;
private int myOffset = 0;
private int myLineNumber = 1;
private float myLineHeight = -1;
private float myDescent = -1;
private double myCharWidth = -1;
@@ -71,7 +67,7 @@ public class TextPainter implements Printable {
private int myCurrentMethodSeparator;
private final CodeStyleSettings myCodeStyleSettings;
private final FileType myFileType;
private ProgressIndicator myProgress;
@NonNls private static final String DEFAULT_MEASURE_HEIGHT_TEXT = "A";
@NonNls private static final String DEFAULT_MEASURE_WIDTH_TEXT = "w";
@NonNls private static final String HEADER_TOKEN_PAGE = "PAGE";
@@ -80,8 +76,9 @@ public class TextPainter implements Printable {
public TextPainter(DocumentEx editorDocument,
EditorHighlighter highlighter,
String fileName,
@NotNull final PsiFile psiFile,
final FileType fileType, final Editor editor) {
@NotNull PsiFile psiFile,
FileType fileType,
Editor editor) {
this(editorDocument, highlighter, fileName, psiFile.getProject(), fileType,
FileSeparatorProvider.getInstance().getFileSeparators(psiFile, editorDocument, editor));
}
@@ -89,8 +86,9 @@ public class TextPainter implements Printable {
public TextPainter(DocumentEx editorDocument,
EditorHighlighter highlighter,
String fileName,
final Project project,
final FileType fileType, final List<LineMarkerInfo> separators) {
Project project,
FileType fileType,
List<LineMarkerInfo> separators) {
myCodeStyleSettings = CodeStyleSettingsManager.getSettings(project);
myDocument = editorDocument;
myPrintSettings = PrintSettings.getInstance();
@@ -101,8 +99,7 @@ public class TextPainter implements Printable {
myItalicFont = new Font(fontName, Font.ITALIC, fontSize);
myBoldItalicFont = new Font(fontName, Font.BOLD | Font.ITALIC, fontSize);
myHighlighter = highlighter;
myHeaderFont = new Font(myPrintSettings.FOOTER_HEADER_FONT_NAME, Font.PLAIN,
myPrintSettings.FOOTER_HEADER_FONT_SIZE);
myHeaderFont = new Font(myPrintSettings.FOOTER_HEADER_FONT_NAME, Font.PLAIN, myPrintSettings.FOOTER_HEADER_FONT_SIZE);
myFileName = fileName;
mySegmentEnd = myDocument.getTextLength();
myFileType = fileType;
@@ -110,10 +107,6 @@ public class TextPainter implements Printable {
myCurrentMethodSeparator = 0;
}
public void setProgress(ProgressIndicator progress) {
myProgress = progress;
}
public void setSegment(int segmentStart, int segmentEnd, int firstLineNumber) {
myOffset = segmentStart;
mySegmentEnd = segmentEnd;