Class ExtendedPrintable

  • Direct Known Subclasses:
    ExtendedPrintable.Wrapper

    public abstract class ExtendedPrintable
    extends java.lang.Object
    An alternative to Printable which provides additional information and options which also provides useful information when saving to an image. May be supplied by a DocumentViewer

    The following example shows how to construct an ExtendedPrintable as an anonymous inner class. Note that this code comes from the example in DocumentViewerFactory and assumes that there is a global String variable called residues present.

    
     final Font font = new Font("monospaced",Font.PLAIN,48);
     ExtendedPrintable ep = new ExtendedPrintable(){
    
        //This Options appears at the top of the print options dialog that is opened when
        //the user selects "Print"
        public Options getOptions(boolean isSavingToFile) {
            Options options = new Options(this.getClass());
            options.addLabel("It is ludicrous to print this");
            return options;
        }
    
        //we need to be aware of what page we are on, and print the correct section of the sequence.
        //in a more robust implementation we would deal with characters being split between two pages.
        public int print(Graphics2D g, Dimension d, int pageIndex, Options options) throws PrinterException {
    
            //clear the background
            g.setColor(Color.white);
            g.fillRect(0,0,d.width,d.height);
    
            //set a clip to make sure that the graphics does not draw outside the printable area
            g.setClip(0,0,d.width,d.height);
    
            //move the graphics so that the correct section of the sequence is drawn to the page
            g.translate(-d.width*pageIndex,0);
    
            //paint the page
            g.setColor(Color.black);
            g.setFont(font);
            g.drawString(residues,10,50);
    
            //tell the printing subsystem whether the page requested was a valid page or not.
            return (getPagesRequired(d, null) >= pageIndex)? Printable.PAGE_EXISTS : Printable.NO_SUCH_PAGE;
        }
    
        //these two methods are used by the save to image feature
        public int getRequiredWidth(Options options) {
            FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
            Rectangle2D fontBounds = font.getStringBounds(residues, frc);
            return (int)fontBounds.getWidth()+20;
        }
    
        //these two methods are used by the save to image feature
        public int getRequiredHeight(Options options, int width) {
            FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
            Rectangle2D fontBounds = font.getStringBounds(residues, frc);
            return (int)fontBounds.getHeight()+60;
        }
    
        //returns the number of pages required to print the entire document.
        public int getPagesRequired(Dimension dimensions, Options options) {
            return (int)Math.ceil(((double)getRequiredWidth(null))/dimensions.width);
        }
    };
     
    • Field Detail

      • IS_HIGH_RESOLUTION_GRAPHICS_OPTION_NAME

        public static final java.lang.String IS_HIGH_RESOLUTION_GRAPHICS_OPTION_NAME
        The options returned from getOptions(boolean) may include a hidden Options.BooleanOption with this name in which case the printing system will set this to true when the format the user has selected is a Vector format (e.g. PDF) or greater than 100% resolution for raster formats. This is useful since Graphics2D.drawString(String, float, float) positions text slightly differently in these 2 cases.
        Since:
        API 4.910 (Geneious 9.1.0)
        See Also:
        Constant Field Values
      • SUPPORTS_GRADIENT_PAINTING_OPTION_NAME

        public static final java.lang.String SUPPORTS_GRADIENT_PAINTING_OPTION_NAME
        The options returned from getOptions(boolean) may include a hidden Options.BooleanOption with this name in which case the printing system will set this to true when the format the user has selected is a format (e.g. anything except EMF) that does supports gradient painting
        Since:
        API 4.913 (Geneious 9.1.3)
        See Also:
        Constant Field Values
      • IS_PREVIEW_OPTION_NAME

        public static final java.lang.String IS_PREVIEW_OPTION_NAME
        The options returned from getOptions(boolean) may include a hidden Options.BooleanOption with this name in which case the printing system will set this to true when generating an image for the PrintPreview dialog rather than actually saving the image or printing
        Since:
        API 4.910 (Geneious 9.1.0)
        See Also:
        Constant Field Values
    • Constructor Detail

      • ExtendedPrintable

        public ExtendedPrintable()
    • Method Detail

      • getOptions

        public Options getOptions​(boolean isSavingToFile)
        Optionally provide additional printing and/or saving image to file options.
        Parameters:
        isSavingToFile - true when saving to a file, false when printing
        Returns:
        a panel of options specific to this printable component or null if it has no options. The default implementation returns null.
      • getDocumentName

        public java.lang.String getDocumentName()
        Returns the name of the document that we are printing (if applicable)
        Returns:
        the name of the document we're printing, or null if there isn't one
      • print

        public abstract int print​(java.awt.Graphics2D graphics,
                                  java.awt.Dimension dimensions,
                                  int pageIndex,
                                  Options options)
                           throws java.awt.print.PrinterException
        print a single page of this component.
        Parameters:
        graphics - the graphics to render to.
        dimensions - the dimensions of the page to be printed to.
        pageIndex - index of the page to be printed or saved as an image.
        options - an options panel returned by getOptions(boolean)
        Returns:
        Printable.PAGE_EXISTS or Printable.NO_SUCH_PAGE
        Throws:
        java.awt.print.PrinterException - the printerexception
      • getMaximumWidth

        public int getMaximumWidth​(Options options)
        Get the maximum width (inclusive) of the component when saved as an image.
        Parameters:
        options - an options panel returned by getOptions(boolean)
        Returns:
        the maximum width (inclusive) of the component when saved as an image.
      • getMinimumWidth

        public int getMinimumWidth​(Options options)
        Get the minimum width (inclusive) of the component when saved as an image.
        Parameters:
        options - an options panel returned by getOptions(boolean)
        Returns:
        the minimum width (inclusive) of the component when saved as an image.
      • getMaximumHeight

        public int getMaximumHeight​(int width,
                                    Options options)
        Get the maximum height (inclusive) of the component when saved as an image.
        Parameters:
        width - a width chosen either by the user or returned from getDefaultWidth(Options)
        options - an options panel returned by getOptions(boolean)
        Returns:
        the maximum height (inclusive) of the component when saved as an image.
      • getMinimumHeight

        public int getMinimumHeight​(int width,
                                    Options options)
        Get the minimum height (inclusive) of the component when saved as an image.
        Parameters:
        width - a width chosen either by the user or returned from getDefaultWidth(Options)
        options - an options panel returned by getOptions(boolean)
        Returns:
        the minimum height (inclusive) of the component when saved as an image.
      • getPagesRequired

        public abstract int getPagesRequired​(java.awt.Dimension dimensions,
                                             Options options)
        Parameters:
        dimensions - the dimensions
        options - an options panel returned by getOptions(boolean)
        Returns:
        the number of pages required to print this document with the specified dimensions and options