Newer
Older
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package uk.ac.sanger.artemis.components;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import uk.ac.sanger.artemis.Options;
import uk.ac.sanger.artemis.editor.ScrollPanel;
/**
*
* Use to print images from Artemis
*
*/
public class PrintArtemis extends ScrollPanel implements Printable
private EntryEdit entry;
private JCheckBox selectDisplay = new JCheckBox("Show Selection Header",true);
private JCheckBox featDisplay = new JCheckBox("Show Feature Display",true);
private JCheckBox groupsDisplay = new JCheckBox("Show Entries Loaded",true);
private JCheckBox plotsDisplay = new JCheckBox("Show Graphs",true);
private JCheckBox jamDisplay = new JCheckBox("Show Read Alignment",true);
private JCheckBox onelineDisplay = new JCheckBox("Show One Line Display",true);
private JCheckBox baseDisplay = new JCheckBox("Show Bases Display",true);
private JCheckBox featListDisplay = new JCheckBox("Show Feature List",true);
private int width;
private int height;
public PrintArtemis(EntryEdit entry)
{
{
// let UI delegate paint first (incl. background filling)
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
super.paintComponent(g2d);
// feature list
if(featListDisplay.isSelected())
{
FeatureList flist = entry.getFeatureList();
Point ploc = flist.getViewport().getViewPosition();
try
{
int translateX = 0;
if(selectDisplay.isSelected())
translateX += entry.getSelectionInfoDisplay().getHeight();
if(groupsDisplay.isSelected())
translateX += entry.getEntryGroupDisplay().getHeight();
if(plotsDisplay.isSelected())
translateX += entry.getBasePlotGroup().getHeight();
if(jamDisplay.isSelected() && entry.getBamPanel() != null && entry.getBamPanel().isVisible())
translateX += entry.getBamPanel().getHeight()-1;
if(vcfDisplay.isSelected() && entry.getVcfView() != null && entry.getVcfView().isVisible())
translateX += entry.getVcfPanel().getHeight();
if(onelineDisplay.isSelected())
translateX += entry.getOneLinePerEntryDisplay().getHeight();
if(featDisplay.isSelected())
translateX += entry.getFeatureDisplay().getHeight();
if(baseDisplay.isSelected())
translateX += entry.getBaseDisplay().getHeight();
translateX-=2+ploc.y;
g2d.translate(0,translateX);
flist.paintComponent(g2d);
g2d.translate(0,-translateX);
}
catch(IllegalArgumentException e){} // thrown if the list is not visible
}
// selection info
if(selectDisplay.isSelected())
{
entry.getSelectionInfoDisplay().paintComponent(g2d);
g2d.translate(0,entry.getSelectionInfoDisplay().getHeight());
}
// entry groups
if(groupsDisplay.isSelected())
{
entry.getEntryGroupDisplay().printComponent(g2d);
g2d.translate(0,entry.getEntryGroupDisplay().getHeight());
}
// plots
if(plotsDisplay.isSelected())
entry.getBasePlotGroup().printComponent(g2d);
// g2d.translate(0,entry.getBasePlotGroup().getHeight());
if(jamDisplay.isSelected() && entry.getBamPanel() != null && entry.getBamPanel().isVisible())
entry.getBamPanel().paintComponents(g2d);
if(vcfDisplay.isSelected() && entry.getVcfView() != null && entry.getVcfView().isVisible())
{
entry.getVcfPanel().paintComponents(g2d);
g2d.translate(0,entry.getVcfPanel().getHeight());
}
// one line per entry
if(onelineDisplay.isSelected())
{
entry.getOneLinePerEntryDisplay().paintComponent(g2d);
g2d.translate(0,entry.getOneLinePerEntryDisplay().getHeight());
}
// feature display
if(featDisplay.isSelected())
{
FeatureDisplay fd = entry.getFeatureDisplay();
fd.paintComponent(g2d);
g2d.translate(0,entry.getFeatureDisplay().getHeight());
}
// base display
if(baseDisplay.isSelected())
{
entry.getBaseDisplay().paintComponent(g2d);
g2d.translate(0,entry.getBaseDisplay().getHeight());
}
}
{
height = 0;
width = entry.getFeatureDisplay().getDisplayWidth();
if(selectDisplay.isSelected())
height += entry.getSelectionInfoDisplay().getHeight();
if(groupsDisplay.isSelected())
height += entry.getEntryGroupDisplay().getHeight();
if(jamDisplay.isSelected() &&
entry.getJamView() != null && entry.getJamView().isVisible())
height += entry.getBamPanel().getHeight();
entry.getVcfView() != null && entry.getVcfView().isVisible())
if(plotsDisplay.isSelected())
height += entry.getBasePlotGroup().getHeight();
if(onelineDisplay.isSelected())
height += entry.getOneLinePerEntryDisplay().getHeight();
if(baseDisplay.isSelected())
height += entry.getBaseDisplay().getHeight();
if(featDisplay.isSelected())
height += entry.getFeatureDisplay().getHeight();
if(featListDisplay.isSelected())
height += entry.getFeatureList().getViewport().getExtentSize().height;
private void setImageSize()
{
setPreferredSize(getImageSize());
}
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
*
* Display a print preview page
*
*/
protected void printPreview()
{
final JFrame f = new JFrame("Print Preview");
JPanel jpane = (JPanel)f.getContentPane();
JScrollPane scrollPane = new JScrollPane(this);
jpane.setLayout(new BorderLayout());
jpane.add(scrollPane,BorderLayout.CENTER);
final Dimension dScreen = f.getToolkit().getScreenSize();
Dimension d = new Dimension((int)(3*dScreen.getWidth()/4),
(int)(dScreen.getHeight()/2));
f.setSize(d);
setImageSize();
JMenuBar menuBar = new JMenuBar();
JMenu filemenu = new JMenu("File");
menuBar.add(filemenu);
// print png/jpeg
JMenuItem printImage = new JMenuItem("Save As Image Files (png/jpeg)...");
printImage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
print();
}
});
filemenu.add(printImage);
// print PostScript
JMenuItem printPS = new JMenuItem("Print...");
printPS.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doPrintActions();
}
});
filemenu.add(printPS);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// close
filemenu.add(new JSeparator());
JMenuItem menuClose = new JMenuItem("Close");
menuClose.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_E, ActionEvent.CTRL_MASK));
filemenu.add(menuClose);
menuClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f.dispose();
}
});
JMenu optionsmenu = new JMenu("Options");
menuBar.add(optionsmenu);
// draw selection info
JCheckBoxMenuItem showSelection = new JCheckBoxMenuItem("Show Selection Header",
selectDisplay.isSelected());
showSelection.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
selectDisplay.setSelected(!selectDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showSelection);
// draw entry groups
JCheckBoxMenuItem showEntryGroups = new JCheckBoxMenuItem("Show Entries Loaded",
groupsDisplay.isSelected());
showEntryGroups.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
groupsDisplay.setSelected(!groupsDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showEntryGroups);
// draw graphs
JCheckBoxMenuItem showPlots = new JCheckBoxMenuItem("Show Graphs",
plotsDisplay.isSelected());
// only enable if graphs displayed
if(entry.getBasePlotGroup().getNumberBasePlots() == 0)
showPlots.setEnabled(false);
showPlots.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
plotsDisplay.setSelected(!plotsDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showPlots);
// draw read alignment viewer
JCheckBoxMenuItem showJam = new JCheckBoxMenuItem("Show Read Alignment View",
jamDisplay.isSelected());
if(entry.getJamView() == null || !entry.getJamView().isVisible())
showJam.setEnabled(false);
showJam.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jamDisplay.setSelected(!jamDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showJam);
// draw vcf viewer
JCheckBoxMenuItem showVcf = new JCheckBoxMenuItem("Show VCF View",
vcfDisplay.isSelected());
if(entry.getVcfPanel() == null || !entry.getVcfPanel().isVisible())
showJam.setEnabled(false);
showVcf.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
vcfDisplay.setSelected(!vcfDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showVcf);
// draw one line
JCheckBoxMenuItem showOneLine = new JCheckBoxMenuItem("Show One Line Display",
onelineDisplay.isSelected());
if(!entry.getOneLinePerEntryDisplay().isVisible())
showOneLine.setEnabled(false);
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
showOneLine.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
onelineDisplay.setSelected(!onelineDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showOneLine);
// draw features
JCheckBoxMenuItem showFeatures = new JCheckBoxMenuItem("Show Feature Display",
featDisplay.isSelected());
showFeatures.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
featDisplay.setSelected(!featDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showFeatures);
// draw base display
JCheckBoxMenuItem showBases = new JCheckBoxMenuItem("Show Bases Display",
baseDisplay.isSelected());
showBases.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
baseDisplay.setSelected(!baseDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showBases);
// draw feature list
JCheckBoxMenuItem showFeatureList = new JCheckBoxMenuItem("Show Feature List",
featListDisplay.isSelected());
showFeatureList.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
featListDisplay.setSelected(!featListDisplay.isSelected());
repaint();
}
});
optionsmenu.add(showFeatureList);
f.setJMenuBar(menuBar);
f.setVisible(true);
}
protected static String[] getImageFormats()
{
final String fmts[] = javax.imageio.ImageIO.getWriterFormatNames();
final String tmpFmts[] = new String[fmts.length+1];
System.arraycopy(fmts, 0, tmpFmts, 0, fmts.length);
tmpFmts[tmpFmts.length-1] = "svg";
return tmpFmts;
}
/**
*
* Print to a jpeg or png file
*
*/
public void print()
{
// file chooser
System.getProperty("file.separator")+
"artemis.png");
fc.setSelectedFile(fselect);
// file name prefix
Box YBox = Box.createVerticalBox();
JLabel labFormat = new JLabel("Select Format:");
Font font = labFormat.getFont();
labFormat.setFont(font.deriveFont(Font.BOLD));
YBox.add(labFormat);
Box bacross = Box.createHorizontalBox();
final JComboBox formatSelect = new JComboBox(getImageFormats());
formatSelect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
String selected;
if(fc.getSelectedFile() != null)
{
selected = fc.getSelectedFile().getAbsolutePath();
String fmts[] = getImageFormats();
for(int i=0; i<fmts.length; i++)
selected = selected.replaceAll("."+fmts[i]+"$", "");
}
else
selected = "artemis";
fc.setSelectedFile(new File(selected+"."+
formatSelect.getSelectedItem()));
}
});
Dimension d = formatSelect.getPreferredSize();
formatSelect.setMaximumSize(d);
bacross.add(Box.createHorizontalGlue());
bacross.add(formatSelect);
YBox.add(bacross);
bacross = Box.createHorizontalBox();
bacross.add(selectDisplay);
bacross.add(Box.createHorizontalGlue());
YBox.add(bacross);
bacross = Box.createHorizontalBox();
bacross.add(Box.createHorizontalGlue());
YBox.add(bacross);
if(entry.getBasePlotGroup().getNumberBasePlots() > 0)
{
bacross = Box.createHorizontalBox();
bacross.add(plotsDisplay);
bacross.add(Box.createHorizontalGlue());
YBox.add(bacross);
}
if(entry.getBamPanel() != null && entry.getBamPanel().isVisible())
{
bacross = Box.createHorizontalBox();
bacross.add(jamDisplay);
bacross.add(Box.createHorizontalGlue());
YBox.add(bacross);
}
if(entry.getVcfView() != null && entry.getVcfView().isVisible())
{
bacross = Box.createHorizontalBox();
bacross.add(vcfDisplay);
bacross.add(Box.createHorizontalGlue());
YBox.add(bacross);
}
if(!entry.getOneLinePerEntryDisplay().isVisible())
{
bacross = Box.createHorizontalBox();
bacross.add(onelineDisplay);
bacross.add(Box.createHorizontalGlue());
YBox.add(bacross);
}
bacross = Box.createHorizontalBox();
bacross.add(featDisplay);
YBox.add(bacross);
bacross = Box.createHorizontalBox();
bacross.add(baseDisplay);
YBox.add(bacross);
bacross = Box.createHorizontalBox();
bacross.add(featListDisplay);
YBox.add(bacross);
// file prefix & format options
fc.setAccessory(YBox);
int n = fc.showSaveDialog(null);
if(n == JFileChooser.CANCEL_OPTION)
return;
// remove file extension
String fsave = fc.getSelectedFile().getAbsolutePath().toLowerCase();
if(fsave.endsWith(".svg"))
{
createSVG(fc.getSelectedFile());
return;
}
if(fsave.endsWith(".png") ||
fsave.endsWith(".jpg") ||
fsave.endsWith(".jpeg") )
{
int ind = fsave.lastIndexOf(".");
fsave = fc.getSelectedFile().getAbsolutePath();
fsave = fsave.substring(0,ind);
}
else
fsave = fc.getSelectedFile().getAbsolutePath();
// image type
String ftype = (String)formatSelect.getSelectedItem();
try
{
RenderedImage rendImage = createImage();
writeImageToFile(rendImage, new File(fsave+"."+ftype),
ftype);
}
catch(NoClassDefFoundError ex)
{
JOptionPane.showMessageDialog(this,
"This option requires Java 1.4 or higher.");
}
}
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
private void createSVG(final File fout)
{
final DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
final Document doc = domImpl.createDocument(
"http://www.w3.org/2000/svg", "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
ctx.setComment("Generated by Artemis with Batik SVG Generator");
final SVGGraphics2D svgG = new SVGGraphics2D(ctx, true);
svgG.setFont(Options.getOptions().getFont());
final FontMetrics fm = svgG.getFontMetrics();
final Dimension d = getImageSize();
svgG.setSVGCanvasSize( new Dimension(
d.width+fm.stringWidth(" "), d.height+fm.getHeight()) );
paintComponent(svgG);
try
{
final Writer out = new OutputStreamWriter(
new FileOutputStream(fout), "UTF-8");
svgG.stream(out, true);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (SVGGraphics2DIOException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return;
}
protected void doPrintActions()
{
final PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(PrintArtemis.this);
pj.printDialog();
try
{
pj.print();
}
catch (Exception PrintException) {}
}
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/**
* Returns a generated image
* @param pageIndex page number
* @return image
*/
private RenderedImage createImage()
{
setImageSize();
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(
width,height,
BufferedImage.TYPE_INT_RGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
paintComponent(g2d);
return bufferedImage;
}
/**
* Write out the image
* @param image image
* @param file file to write image to
* @param type type of image
*/
private void writeImageToFile(RenderedImage image,
File file, String type)
{
try
{
javax.imageio.ImageIO.write(image,type,file);
}
catch ( IOException e )
{
System.out.println("Java 1.4+ is required");
e.printStackTrace();
}
}
/**
*
* The method @print@ must be implemented for @Printable@ interface.
* Parameters are supplied by system.
*
*/
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
setImageSize();
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
if(panelWidth == 0)
{
d = this.getPreferredSize();
panelWidth = d.width;
panelHeight = d.height;
}
double pageHeight = pf.getImageableHeight(); //height of printer page
double pageWidth = pf.getImageableWidth(); //width of printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
// Make sure not print empty pages
if(pageIndex >= totalNumPages)
return Printable.NO_SUCH_PAGE;
// Shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageableX(), pf.getImageableY());
// Shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex*pageHeight);
// Scale the page so the width fits...
g2.scale(scale, scale);
paintComponent(g2);
return Printable.PAGE_EXISTS;
}