applied 2009 GPL license
[jalview.git] / src / jalview / gui / AlignmentPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4.0.b2)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.beans.*;
22 import java.io.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26 import java.awt.print.*;
27 import javax.swing.*;
28
29 import jalview.datamodel.*;
30 import jalview.jbgui.*;
31 import jalview.schemes.*;
32
33 /**
34  * DOCUMENT ME!
35  * 
36  * @author $author$
37  * @version $Revision$
38  */
39 public class AlignmentPanel extends GAlignmentPanel implements
40         AdjustmentListener, Printable
41 {
42   public AlignViewport av;
43
44   OverviewPanel overviewPanel;
45
46   SeqPanel seqPanel;
47
48   IdPanel idPanel;
49
50   IdwidthAdjuster idwidthAdjuster;
51
52   /** DOCUMENT ME!! */
53   public AlignFrame alignFrame;
54
55   ScalePanel scalePanel;
56
57   AnnotationPanel annotationPanel;
58
59   AnnotationLabels alabels;
60
61   // this value is set false when selection area being dragged
62   boolean fastPaint = true;
63
64   int hextent = 0;
65
66   int vextent = 0;
67
68   /**
69    * Creates a new AlignmentPanel object.
70    * 
71    * @param af
72    *                DOCUMENT ME!
73    * @param av
74    *                DOCUMENT ME!
75    */
76   public AlignmentPanel(AlignFrame af, final AlignViewport av)
77   {
78     alignFrame = af;
79     this.av = av;
80     seqPanel = new SeqPanel(av, this);
81     idPanel = new IdPanel(av, this);
82
83     scalePanel = new ScalePanel(av, this);
84
85     idPanelHolder.add(idPanel, BorderLayout.CENTER);
86     idwidthAdjuster = new IdwidthAdjuster(this);
87     idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
88
89     annotationPanel = new AnnotationPanel(this);
90     alabels = new AnnotationLabels(this);
91
92     annotationScroller.setViewportView(annotationPanel);
93     annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
94
95     scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
96     seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
97
98     setScrollValues(0, 0);
99
100     setAnnotationVisible(av.getShowAnnotation());
101
102     hscroll.addAdjustmentListener(this);
103     vscroll.addAdjustmentListener(this);
104
105     final AlignmentPanel ap = this;
106     av.addPropertyChangeListener(new PropertyChangeListener()
107     {
108       public void propertyChange(PropertyChangeEvent evt)
109       {
110         if (evt.getPropertyName().equals("alignment"))
111         {
112           PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
113           alignmentChanged();
114         }
115       }
116     });
117
118     fontChanged();
119     adjustAnnotationHeight();
120
121   }
122
123   public void alignmentChanged()
124   {
125     av.alignmentChanged(this);
126
127     alignFrame.updateEditMenuBar();
128
129     paintAlignment(true);
130
131   }
132
133   /**
134    * DOCUMENT ME!
135    */
136   public void fontChanged()
137   {
138     // set idCanvas bufferedImage to null
139     // to prevent drawing old image
140     FontMetrics fm = getFontMetrics(av.getFont());
141
142     scalePanelHolder.setPreferredSize(new Dimension(10, av.charHeight
143             + fm.getDescent()));
144     idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av.charHeight
145             + fm.getDescent()));
146
147     idPanel.idCanvas.gg = null;
148     seqPanel.seqCanvas.img = null;
149     annotationPanel.adjustPanelHeight();
150
151     Dimension d = calculateIdWidth();
152     d.setSize(d.width + 4, d.height);
153     idPanel.idCanvas.setPreferredSize(d);
154     hscrollFillerPanel.setPreferredSize(d);
155
156     if (overviewPanel != null)
157     {
158       overviewPanel.setBoxPosition();
159     }
160
161     repaint();
162   }
163
164   /**
165    * Calculate the width of the alignment labels based on the displayed names
166    * and any bounds on label width set in preferences.
167    * 
168    * @return Dimension giving the maximum width of the alignment label panel
169    *         that should be used.
170    */
171   public Dimension calculateIdWidth()
172   {
173     Container c = new Container();
174
175     FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
176             Font.ITALIC, av.font.getSize()));
177
178     AlignmentI al = av.getAlignment();
179     int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
180     int maxwidth = Math.max(20, Math.min(afwidth - 200, (int) 2 * afwidth
181             / 3));
182     int i = 0;
183     int idWidth = 0;
184     String id;
185
186     while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
187     {
188       SequenceI s = al.getSequenceAt(i);
189
190       id = s.getDisplayId(av.getShowJVSuffix());
191
192       if (fm.stringWidth(id) > idWidth)
193       {
194         idWidth = fm.stringWidth(id);
195       }
196
197       i++;
198     }
199
200     // Also check annotation label widths
201     i = 0;
202
203     if (al.getAlignmentAnnotation() != null)
204     {
205       fm = c.getFontMetrics(alabels.getFont());
206
207       while (i < al.getAlignmentAnnotation().length)
208       {
209         String label = al.getAlignmentAnnotation()[i].label;
210
211         if (fm.stringWidth(label) > idWidth)
212         {
213           idWidth = fm.stringWidth(label);
214         }
215
216         i++;
217       }
218     }
219
220     return new Dimension(Math.min(maxwidth, idWidth), 12);
221   }
222
223   /**
224    * DOCUMENT ME!
225    * 
226    * @param results
227    *                DOCUMENT ME!
228    */
229   public void highlightSearchResults(SearchResults results)
230   {
231     seqPanel.seqCanvas.highlightSearchResults(results);
232
233     // do we need to scroll the panel?
234     if (results != null)
235     {
236       SequenceI seq = results.getResultSequence(0);
237       int seqIndex = av.alignment.findIndex(seq);
238       int start = seq.findIndex(results.getResultStart(0)) - 1;
239       int end = seq.findIndex(results.getResultEnd(0)) - 1;
240
241       if (!av.wrapAlignment)
242       {
243         if ((av.getStartRes() > end)
244                 || (av.getEndRes() < start)
245                 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
246         {
247           setScrollValues(start, seqIndex);
248         }
249       }
250       else
251       {
252         scrollToWrappedVisible(start);
253       }
254     }
255
256     paintAlignment(true);
257   }
258
259   void scrollToWrappedVisible(int res)
260   {
261     int cwidth = seqPanel.seqCanvas
262             .getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth());
263     if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
264     {
265       vscroll.setValue(res / cwidth);
266       av.startRes = vscroll.getValue() * cwidth;
267     }
268   }
269
270   /**
271    * DOCUMENT ME!
272    * 
273    * @return DOCUMENT ME!
274    */
275   public OverviewPanel getOverviewPanel()
276   {
277     return overviewPanel;
278   }
279
280   /**
281    * DOCUMENT ME!
282    * 
283    * @param op
284    *                DOCUMENT ME!
285    */
286   public void setOverviewPanel(OverviewPanel op)
287   {
288     overviewPanel = op;
289   }
290
291   /**
292    * DOCUMENT ME!
293    * 
294    * @param b
295    *                DOCUMENT ME!
296    */
297   public void setAnnotationVisible(boolean b)
298   {
299     if (!av.wrapAlignment)
300     {
301       annotationSpaceFillerHolder.setVisible(b);
302       annotationScroller.setVisible(b);
303     }
304     repaint();
305   }
306
307   public void adjustAnnotationHeight()
308   {
309     // TODO: display vertical annotation scrollbar if necessary
310     // this is called after loading new annotation onto alignment
311     if (alignFrame.getHeight() == 0)
312     {
313       System.out.println("NEEDS FIXING");
314     }
315
316     int height = annotationPanel.adjustPanelHeight();
317
318     if (hscroll.isVisible())
319     {
320       height += hscroll.getPreferredSize().height;
321     }
322     if (height > alignFrame.getHeight() / 2)
323     {
324       height = alignFrame.getHeight() / 2;
325     }
326
327     hscroll.addNotify();
328
329     annotationScroller.setPreferredSize(new Dimension(annotationScroller
330             .getWidth(), height));
331
332     annotationSpaceFillerHolder.setPreferredSize(new Dimension(
333             annotationSpaceFillerHolder.getWidth(), height));
334     annotationScroller.validate();// repaint();
335     repaint();
336   }
337
338   /**
339    * DOCUMENT ME!
340    * 
341    * @param wrap
342    *                DOCUMENT ME!
343    */
344   public void setWrapAlignment(boolean wrap)
345   {
346     av.startSeq = 0;
347     scalePanelHolder.setVisible(!wrap);
348     hscroll.setVisible(!wrap);
349     idwidthAdjuster.setVisible(!wrap);
350
351     if (wrap)
352     {
353       annotationScroller.setVisible(false);
354       annotationSpaceFillerHolder.setVisible(false);
355     }
356     else if (av.showAnnotation)
357     {
358       annotationScroller.setVisible(true);
359       annotationSpaceFillerHolder.setVisible(true);
360     }
361
362     idSpaceFillerPanel1.setVisible(!wrap);
363
364     repaint();
365   }
366
367   // return value is true if the scroll is valid
368   public boolean scrollUp(boolean up)
369   {
370     if (up)
371     {
372       if (vscroll.getValue() < 1)
373       {
374         return false;
375       }
376
377       fastPaint = false;
378       vscroll.setValue(vscroll.getValue() - 1);
379     }
380     else
381     {
382       if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
383       {
384         return false;
385       }
386
387       fastPaint = false;
388       vscroll.setValue(vscroll.getValue() + 1);
389     }
390
391     fastPaint = true;
392
393     return true;
394   }
395
396   /**
397    * DOCUMENT ME!
398    * 
399    * @param right
400    *                DOCUMENT ME!
401    * 
402    * @return DOCUMENT ME!
403    */
404   public boolean scrollRight(boolean right)
405   {
406     if (!right)
407     {
408       if (hscroll.getValue() < 1)
409       {
410         return false;
411       }
412
413       fastPaint = false;
414       hscroll.setValue(hscroll.getValue() - 1);
415     }
416     else
417     {
418       if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
419       {
420         return false;
421       }
422
423       fastPaint = false;
424       hscroll.setValue(hscroll.getValue() + 1);
425     }
426
427     fastPaint = true;
428
429     return true;
430   }
431
432   /**
433    * DOCUMENT ME!
434    * 
435    * @param x
436    *                DOCUMENT ME!
437    * @param y
438    *                DOCUMENT ME!
439    */
440   public void setScrollValues(int x, int y)
441   {
442
443     int width = av.alignment.getWidth();
444     int height = av.alignment.getHeight();
445
446     if (av.hasHiddenColumns)
447     {
448       width = av.getColumnSelection().findColumnPosition(width);
449     }
450
451     av.setEndRes((x + (seqPanel.seqCanvas.getWidth() / av.charWidth)) - 1);
452
453     hextent = seqPanel.seqCanvas.getWidth() / av.charWidth;
454     vextent = seqPanel.seqCanvas.getHeight() / av.charHeight;
455
456     if (hextent > width)
457     {
458       hextent = width;
459     }
460
461     if (vextent > height)
462     {
463       vextent = height;
464     }
465
466     if ((hextent + x) > width)
467     {
468       x = width - hextent;
469     }
470
471     if ((vextent + y) > height)
472     {
473       y = height - vextent;
474     }
475
476     if (y < 0)
477     {
478       y = 0;
479     }
480
481     if (x < 0)
482     {
483       x = 0;
484     }
485
486     hscroll.setValues(x, hextent, 0, width);
487     vscroll.setValues(y, vextent, 0, height);
488   }
489
490   /**
491    * DOCUMENT ME!
492    * 
493    * @param evt
494    *                DOCUMENT ME!
495    */
496   public void adjustmentValueChanged(AdjustmentEvent evt)
497   {
498
499     int oldX = av.getStartRes();
500     int oldY = av.getStartSeq();
501
502     if (evt.getSource() == hscroll)
503     {
504       int x = hscroll.getValue();
505       av.setStartRes(x);
506       av
507               .setEndRes((x + (seqPanel.seqCanvas.getWidth() / av
508                       .getCharWidth())) - 1);
509     }
510
511     if (evt.getSource() == vscroll)
512     {
513       int offy = vscroll.getValue();
514
515       if (av.getWrapAlignment())
516       {
517         if (offy > -1)
518         {
519           int rowSize = seqPanel.seqCanvas
520                   .getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth());
521           av.setStartRes(offy * rowSize);
522           av.setEndRes((offy + 1) * rowSize);
523         }
524         else
525         {
526           // This is only called if file loaded is a jar file that
527           // was wrapped when saved and user has wrap alignment true
528           // as preference setting
529           SwingUtilities.invokeLater(new Runnable()
530           {
531             public void run()
532             {
533               setScrollValues(av.getStartRes(), av.getStartSeq());
534             }
535           });
536         }
537       }
538       else
539       {
540         av.setStartSeq(offy);
541         av.setEndSeq(offy
542                 + (seqPanel.seqCanvas.getHeight() / av.getCharHeight()));
543       }
544     }
545
546     if (overviewPanel != null)
547     {
548       overviewPanel.setBoxPosition();
549     }
550
551     int scrollX = av.startRes - oldX;
552     int scrollY = av.startSeq - oldY;
553
554     if (av.getWrapAlignment() || !fastPaint)
555     {
556       repaint();
557     }
558     else
559     {
560       // Make sure we're not trying to draw a panel
561       // larger than the visible window
562       if (scrollX > av.endRes - av.startRes)
563       {
564         scrollX = av.endRes - av.startRes;
565       }
566       else if (scrollX < av.startRes - av.endRes)
567       {
568         scrollX = av.startRes - av.endRes;
569       }
570
571       if (scrollX != 0 || scrollY != 0)
572       {
573         idPanel.idCanvas.fastPaint(scrollY);
574         seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
575         scalePanel.repaint();
576
577         if (av.getShowAnnotation())
578         {
579           annotationPanel.fastPaint(scrollX);
580         }
581       }
582     }
583   }
584
585   public void paintAlignment(boolean updateOverview)
586   {
587     repaint();
588
589     if (updateOverview)
590     {
591       jalview.structure.StructureSelectionManager
592               .getStructureSelectionManager().sequenceColoursChanged(this);
593
594       if (overviewPanel != null)
595       {
596         overviewPanel.updateOverviewImage();
597       }
598     }
599   }
600
601   /**
602    * DOCUMENT ME!
603    * 
604    * @param g
605    *                DOCUMENT ME!
606    */
607   public void paintComponent(Graphics g)
608   {
609     invalidate();
610
611     Dimension d = idPanel.idCanvas.getPreferredSize();
612     idPanelHolder.setPreferredSize(d);
613     hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
614     validate();
615
616     if (av.getWrapAlignment())
617     {
618       int maxwidth = av.alignment.getWidth();
619
620       if (av.hasHiddenColumns)
621       {
622         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
623       }
624
625       int canvasWidth = seqPanel.seqCanvas
626               .getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth());
627       if (canvasWidth > 0)
628       {
629         int max = maxwidth
630                 / seqPanel.seqCanvas
631                         .getWrappedCanvasWidth(seqPanel.seqCanvas
632                                 .getWidth()) + 1;
633         vscroll.setMaximum(max);
634         vscroll.setUnitIncrement(1);
635         vscroll.setVisibleAmount(1);
636       }
637     }
638     else
639     {
640       setScrollValues(av.getStartRes(), av.getStartSeq());
641     }
642   }
643
644   /**
645    * DOCUMENT ME!
646    * 
647    * @param pg
648    *                DOCUMENT ME!
649    * @param pf
650    *                DOCUMENT ME!
651    * @param pi
652    *                DOCUMENT ME!
653    * 
654    * @return DOCUMENT ME!
655    * 
656    * @throws PrinterException
657    *                 DOCUMENT ME!
658    */
659   public int print(Graphics pg, PageFormat pf, int pi)
660           throws PrinterException
661   {
662     pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
663
664     int pwidth = (int) pf.getImageableWidth();
665     int pheight = (int) pf.getImageableHeight();
666
667     if (av.getWrapAlignment())
668     {
669       return printWrappedAlignment(pg, pwidth, pheight, pi);
670     }
671     else
672     {
673       return printUnwrapped(pg, pwidth, pheight, pi);
674     }
675   }
676
677   /**
678    * DOCUMENT ME!
679    * 
680    * @param pg
681    *                DOCUMENT ME!
682    * @param pwidth
683    *                DOCUMENT ME!
684    * @param pheight
685    *                DOCUMENT ME!
686    * @param pi
687    *                DOCUMENT ME!
688    * 
689    * @return DOCUMENT ME!
690    * 
691    * @throws PrinterException
692    *                 DOCUMENT ME!
693    */
694   public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
695           throws PrinterException
696   {
697     int idWidth = getVisibleIdWidth();
698     FontMetrics fm = getFontMetrics(av.getFont());
699     int scaleHeight = av.charHeight + fm.getDescent();
700
701     pg.setColor(Color.white);
702     pg.fillRect(0, 0, pwidth, pheight);
703     pg.setFont(av.getFont());
704
705     // //////////////////////////////////
706     // / How many sequences and residues can we fit on a printable page?
707     int totalRes = (pwidth - idWidth) / av.getCharWidth();
708
709     int totalSeq = (int) ((pheight - scaleHeight) / av.getCharHeight()) - 1;
710
711     int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
712
713     // ///////////////////////////
714     // / Only print these sequences and residues on this page
715     int startRes;
716
717     // ///////////////////////////
718     // / Only print these sequences and residues on this page
719     int endRes;
720
721     // ///////////////////////////
722     // / Only print these sequences and residues on this page
723     int startSeq;
724
725     // ///////////////////////////
726     // / Only print these sequences and residues on this page
727     int endSeq;
728     startRes = (pi % pagesWide) * totalRes;
729     endRes = (startRes + totalRes) - 1;
730
731     if (endRes > (av.getAlignment().getWidth() - 1))
732     {
733       endRes = av.getAlignment().getWidth() - 1;
734     }
735
736     startSeq = (pi / pagesWide) * totalSeq;
737     endSeq = startSeq + totalSeq;
738
739     if (endSeq > av.getAlignment().getHeight())
740     {
741       endSeq = av.getAlignment().getHeight();
742     }
743
744     int pagesHigh = ((av.alignment.getHeight() / totalSeq) + 1) * pheight;
745
746     if (av.showAnnotation)
747     {
748       pagesHigh += annotationPanel.adjustPanelHeight() + 3;
749     }
750
751     pagesHigh /= pheight;
752
753     if (pi >= (pagesWide * pagesHigh))
754     {
755       return Printable.NO_SUCH_PAGE;
756     }
757
758     // draw Scale
759     pg.translate(idWidth, 0);
760     scalePanel.drawScale(pg, startRes, endRes, pwidth - idWidth,
761             scaleHeight);
762     pg.translate(-idWidth, scaleHeight);
763
764     // //////////////
765     // Draw the ids
766     Color currentColor = null;
767     Color currentTextColor = null;
768
769     pg.setFont(idPanel.idCanvas.idfont);
770
771     SequenceI seq;
772     for (int i = startSeq; i < endSeq; i++)
773     {
774       seq = av.getAlignment().getSequenceAt(i);
775       if ((av.getSelectionGroup() != null)
776               && av.getSelectionGroup().getSequences(null).contains(seq))
777       {
778         currentColor = Color.gray;
779         currentTextColor = Color.black;
780       }
781       else
782       {
783         currentColor = av.getSequenceColour(seq);
784         currentTextColor = Color.black;
785       }
786
787       pg.setColor(currentColor);
788       pg.fillRect(0, (i - startSeq) * av.charHeight, idWidth, av
789               .getCharHeight());
790
791       pg.setColor(currentTextColor);
792
793       int xPos = 0;
794       if (av.rightAlignIds)
795       {
796         fm = pg.getFontMetrics();
797         xPos = idWidth
798                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
799                 - 4;
800       }
801
802       pg.drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
803               (((i - startSeq) * av.charHeight) + av.getCharHeight())
804                       - (av.getCharHeight() / 5));
805     }
806
807     pg.setFont(av.getFont());
808
809     // draw main sequence panel
810     pg.translate(idWidth, 0);
811     seqPanel.seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
812
813     if (av.showAnnotation && (endSeq == av.alignment.getHeight()))
814     {
815       pg.translate(-idWidth - 3, (endSeq - startSeq) * av.charHeight + 3);
816       alabels.drawComponent((Graphics2D) pg, idWidth);
817       pg.translate(idWidth + 3, 0);
818       annotationPanel.drawComponent((Graphics2D) pg, startRes, endRes + 1);
819     }
820
821     return Printable.PAGE_EXISTS;
822   }
823
824   /**
825    * DOCUMENT ME!
826    * 
827    * @param pg
828    *                DOCUMENT ME!
829    * @param pwidth
830    *                DOCUMENT ME!
831    * @param pheight
832    *                DOCUMENT ME!
833    * @param pi
834    *                DOCUMENT ME!
835    * 
836    * @return DOCUMENT ME!
837    * 
838    * @throws PrinterException
839    *                 DOCUMENT ME!
840    */
841   public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
842           int pi) throws PrinterException
843   {
844
845     int annotationHeight = 0;
846     AnnotationLabels labels = null;
847     if (av.showAnnotation)
848     {
849       annotationHeight = annotationPanel.adjustPanelHeight();
850       labels = new AnnotationLabels(av);
851     }
852
853     int hgap = av.charHeight;
854     if (av.scaleAboveWrapped)
855     {
856       hgap += av.charHeight;
857     }
858
859     int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
860             + annotationHeight;
861
862     int idWidth = getVisibleIdWidth();
863
864     int maxwidth = av.alignment.getWidth();
865     if (av.hasHiddenColumns)
866     {
867       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
868     }
869
870     int resWidth = seqPanel.seqCanvas.getWrappedCanvasWidth(pwidth
871             - idWidth);
872
873     int totalHeight = cHeight * (maxwidth / resWidth + 1);
874
875     pg.setColor(Color.white);
876     pg.fillRect(0, 0, pwidth, pheight);
877     pg.setFont(av.getFont());
878
879     // //////////////
880     // Draw the ids
881     pg.setColor(Color.black);
882
883     pg.translate(0, -pi * pheight);
884
885     pg.setClip(0, pi * pheight, pwidth, pheight);
886
887     int ypos = hgap;
888
889     do
890     {
891       for (int i = 0; i < av.alignment.getHeight(); i++)
892       {
893         pg.setFont(idPanel.idCanvas.idfont);
894         SequenceI s = av.alignment.getSequenceAt(i);
895         String string = s.getDisplayId(av.getShowJVSuffix());
896         int xPos = 0;
897         if (av.rightAlignIds)
898         {
899           FontMetrics fm = pg.getFontMetrics();
900           xPos = idWidth - fm.stringWidth(string) - 4;
901         }
902         pg.drawString(string, xPos,
903                 ((i * av.charHeight) + ypos + av.charHeight)
904                         - (av.charHeight / 5));
905       }
906       if (labels != null)
907       {
908         pg.translate(-3, ypos
909                 + (av.getAlignment().getHeight() * av.charHeight));
910
911         pg.setFont(av.getFont());
912         labels.drawComponent(pg, idWidth);
913         pg.translate(+3, -ypos
914                 - (av.getAlignment().getHeight() * av.charHeight));
915       }
916
917       ypos += cHeight;
918     } while (ypos < totalHeight);
919
920     pg.translate(idWidth, 0);
921
922     seqPanel.seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
923             0);
924
925     if ((pi * pheight) < totalHeight)
926     {
927       return Printable.PAGE_EXISTS;
928
929     }
930     else
931     {
932       return Printable.NO_SUCH_PAGE;
933     }
934   }
935
936   int getVisibleIdWidth()
937   {
938     return idPanel.getWidth() > 0 ? idPanel.getWidth()
939             : calculateIdWidth().width + 4;
940   }
941
942   void makeAlignmentImage(int type, File file)
943   {
944     int maxwidth = av.alignment.getWidth();
945     if (av.hasHiddenColumns)
946     {
947       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
948     }
949
950     int height = ((av.alignment.getHeight() + 1) * av.charHeight)
951             + scalePanel.getHeight();
952     int width = getVisibleIdWidth() + (maxwidth * av.charWidth);
953
954     if (av.getWrapAlignment())
955     {
956       height = getWrappedHeight();
957       if (System.getProperty("java.awt.headless") != null
958               && System.getProperty("java.awt.headless").equals("true"))
959       {
960         width = alignFrame.getWidth() - vscroll.getPreferredSize().width
961                 - alignFrame.getInsets().left
962                 - alignFrame.getInsets().right;
963       }
964       else
965       {
966         width = seqPanel.getWidth() + getVisibleIdWidth();
967       }
968
969     }
970     else if (av.getShowAnnotation())
971     {
972       height += annotationPanel.adjustPanelHeight() + 3;
973     }
974
975     try
976     {
977
978       jalview.util.ImageMaker im;
979       if (type == jalview.util.ImageMaker.PNG)
980       {
981         im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
982                 "Create PNG image from alignment", width, height, file,
983                 null);
984       }
985       else
986       {
987         im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
988                 "Create EPS file from alignment", width, height, file,
989                 alignFrame.getTitle());
990       }
991
992       if (av.getWrapAlignment())
993       {
994         if (im.getGraphics() != null)
995         {
996           printWrappedAlignment(im.getGraphics(), width, height, 0);
997           im.writeImage();
998         }
999       }
1000       else
1001       {
1002         if (im.getGraphics() != null)
1003         {
1004           printUnwrapped(im.getGraphics(), width, height, 0);
1005           im.writeImage();
1006         }
1007       }
1008     } catch (OutOfMemoryError err)
1009     {
1010       // Be noisy here.
1011       System.out.println("########################\n" + "OUT OF MEMORY "
1012               + file + "\n" + "########################");
1013       new OOMWarning("Creating Image for " + file, err);
1014       // System.out.println("Create IMAGE: " + err);
1015     } catch (Exception ex)
1016     {
1017       ex.printStackTrace();
1018     }
1019   }
1020
1021   /**
1022    * DOCUMENT ME!
1023    */
1024   public void makeEPS(File epsFile)
1025   {
1026     makeAlignmentImage(jalview.util.ImageMaker.EPS, epsFile);
1027   }
1028
1029   /**
1030    * DOCUMENT ME!
1031    */
1032   public void makePNG(File pngFile)
1033   {
1034     makeAlignmentImage(jalview.util.ImageMaker.PNG, pngFile);
1035   }
1036
1037   public void makePNGImageMap(File imgMapFile, String imageName)
1038   {
1039     // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1040     // ////////////////////////////////////////////
1041     int idWidth = getVisibleIdWidth();
1042     FontMetrics fm = getFontMetrics(av.getFont());
1043     int scaleHeight = av.charHeight + fm.getDescent();
1044
1045     // Gen image map
1046     // ////////////////////////////////
1047     if (imgMapFile != null)
1048     {
1049       try
1050       {
1051         int s, sSize = av.alignment.getHeight(), res, alwidth = av.alignment
1052                 .getWidth(), g, gSize, f, fSize, sy;
1053         StringBuffer text = new StringBuffer();
1054         PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1055         out.println(jalview.io.HTMLOutput.getImageMapHTML());
1056         out.println("<img src=\"" + imageName
1057                 + "\" border=\"0\" usemap=\"#Map\" >"
1058                 + "<map name=\"Map\">");
1059
1060         for (s = 0; s < sSize; s++)
1061         {
1062           sy = s * av.charHeight + scaleHeight;
1063
1064           SequenceI seq = av.alignment.getSequenceAt(s);
1065           SequenceFeature[] features = seq.getDatasetSequence()
1066                   .getSequenceFeatures();
1067           SequenceGroup[] groups = av.alignment.findAllGroups(seq);
1068           for (res = 0; res < alwidth; res++)
1069           {
1070             text = new StringBuffer();
1071             Object obj = null;
1072             if (av.alignment.isNucleotide())
1073             {
1074               obj = ResidueProperties.nucleotideName.get(seq.getCharAt(res)
1075                       + "");
1076             }
1077             else
1078             {
1079               obj = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1080                       + "");
1081             }
1082
1083             if (obj == null)
1084             {
1085               continue;
1086             }
1087
1088             String triplet = obj.toString();
1089             int alIndex = seq.findPosition(res);
1090             gSize = groups.length;
1091             for (g = 0; g < gSize; g++)
1092             {
1093               if (text.length() < 1)
1094               {
1095                 text.append("<area shape=\"rect\" coords=\""
1096                         + (idWidth + res * av.charWidth) + "," + sy + ","
1097                         + (idWidth + (res + 1) * av.charWidth) + ","
1098                         + (av.charHeight + sy) + "\""
1099                         + " onMouseOver=\"toolTip('" + alIndex + " "
1100                         + triplet);
1101               }
1102
1103               if (groups[g].getStartRes() < res
1104                       && groups[g].getEndRes() > res)
1105               {
1106                 text.append("<br><em>" + groups[g].getName() + "</em>");
1107               }
1108             }
1109
1110             if (features != null)
1111             {
1112               if (text.length() < 1)
1113               {
1114                 text.append("<area shape=\"rect\" coords=\""
1115                         + (idWidth + res * av.charWidth) + "," + sy + ","
1116                         + (idWidth + (res + 1) * av.charWidth) + ","
1117                         + (av.charHeight + sy) + "\""
1118                         + " onMouseOver=\"toolTip('" + alIndex + " "
1119                         + triplet);
1120               }
1121               fSize = features.length;
1122               for (f = 0; f < fSize; f++)
1123               {
1124
1125                 if ((features[f].getBegin() <= seq.findPosition(res))
1126                         && (features[f].getEnd() >= seq.findPosition(res)))
1127                 {
1128                   if (features[f].getType().equals("disulfide bond"))
1129                   {
1130                     if (features[f].getBegin() == seq.findPosition(res)
1131                             || features[f].getEnd() == seq
1132                                     .findPosition(res))
1133                     {
1134                       text.append("<br>disulfide bond "
1135                               + features[f].getBegin() + ":"
1136                               + features[f].getEnd());
1137                     }
1138                   }
1139                   else
1140                   {
1141                     text.append("<br>");
1142                     text.append(features[f].getType());
1143                     if (features[f].getDescription() != null
1144                             && !features[f].getType().equals(
1145                                     features[f].getDescription()))
1146                     {
1147                       text.append(" " + features[f].getDescription());
1148                     }
1149
1150                     if (features[f].getValue("status") != null)
1151                     {
1152                       text.append(" (" + features[f].getValue("status")
1153                               + ")");
1154                     }
1155                   }
1156                 }
1157
1158               }
1159             }
1160             if (text.length() > 1)
1161             {
1162               text.append("')\"; onMouseOut=\"toolTip()\";  href=\"#\">");
1163               out.println(text.toString());
1164             }
1165           }
1166         }
1167         out.println("</map></body></html>");
1168         out.close();
1169
1170       } catch (Exception ex)
1171       {
1172         ex.printStackTrace();
1173       }
1174     } // /////////END OF IMAGE MAP
1175
1176   }
1177
1178   int getWrappedHeight()
1179   {
1180     int seqPanelWidth = seqPanel.seqCanvas.getWidth();
1181
1182     if (System.getProperty("java.awt.headless") != null
1183             && System.getProperty("java.awt.headless").equals("true"))
1184     {
1185       seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1186               - vscroll.getPreferredSize().width
1187               - alignFrame.getInsets().left - alignFrame.getInsets().right;
1188     }
1189
1190     int chunkWidth = seqPanel.seqCanvas
1191             .getWrappedCanvasWidth(seqPanelWidth);
1192
1193     int hgap = av.charHeight;
1194     if (av.scaleAboveWrapped)
1195     {
1196       hgap += av.charHeight;
1197     }
1198
1199     int annotationHeight = 0;
1200     if (av.showAnnotation)
1201     {
1202       annotationHeight = annotationPanel.adjustPanelHeight();
1203     }
1204
1205     int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
1206             + annotationHeight;
1207
1208     int maxwidth = av.alignment.getWidth();
1209     if (av.hasHiddenColumns)
1210     {
1211       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1212     }
1213
1214     int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1215
1216     return height;
1217   }
1218 }