GPL license added
[jalview.git] / src / jalview / appletgui / AnnotationLabels.java
1 \r
2 /*\r
3 * Jalview - A Sequence Alignment Editor and Viewer\r
4 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
5 *\r
6 * This program is free software; you can redistribute it and/or\r
7 * modify it under the terms of the GNU General Public License\r
8 * as published by the Free Software Foundation; either version 2\r
9 * of the License, or (at your option) any later version.\r
10 *\r
11 * This program is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with this program; if not, write to the Free Software\r
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
19 */\r
20 \r
21 package jalview.appletgui;\r
22 \r
23 import java.awt.event.*;\r
24 import java.awt.*;\r
25 import jalview.datamodel.*;\r
26 \r
27 public class AnnotationLabels extends Panel implements ActionListener\r
28 {\r
29   boolean active = false;\r
30   AlignmentPanel ap ;\r
31   boolean resizing = false;\r
32   int oldY, mouseX;\r
33   static String ADDNEW = "Add new row";\r
34   static String HIDE = "Hide this row";\r
35   static String DELETE = "Delete this row";\r
36   static String SHOWALL="Show all hidden rows";\r
37   static String OUTPUT_TEXT="Show Values In Textbox";\r
38   int selectedRow = 0;\r
39   int scrollOffset = 0;\r
40 \r
41   public AnnotationLabels(AlignmentPanel ap)\r
42   {\r
43     this.ap = ap;\r
44     setLayout(null);\r
45     addMouseListener(new MouseAdapter()\r
46     {public void mousePressed(MouseEvent evt)\r
47       {\r
48         doMousePressed(evt);\r
49       }\r
50     });\r
51   }\r
52 \r
53   public void setScrollOffset(int y)\r
54   {\r
55     scrollOffset = y;\r
56     repaint();\r
57   }\r
58 \r
59   public void actionPerformed(ActionEvent evt)\r
60   {\r
61     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
62 \r
63    if(evt.getActionCommand().equals(HIDE))\r
64    {\r
65      aa[selectedRow].visible = false;\r
66    }\r
67    else if(evt.getActionCommand().equals(SHOWALL))\r
68    {\r
69        for(int i=0; i<aa.length; i++)\r
70          aa[i].visible = true;\r
71    }\r
72    else if (evt.getActionCommand().equals(OUTPUT_TEXT))\r
73    {\r
74      CutAndPasteTransfer cap = new CutAndPasteTransfer(false);\r
75      Frame frame = new Frame();\r
76      frame.add(cap);\r
77      jalview.bin.JalviewLite.addFrame(frame,\r
78                                       ap.alignFrame.getTitle() + " - " +\r
79                                       aa[selectedRow].label, 500, 100);\r
80      cap.setText(aa[selectedRow].toString());\r
81    }\r
82 \r
83    ap.annotationPanel.adjustPanelHeight();\r
84    setSize(getSize().width,ap.annotationPanel.getSize().height);\r
85    ap.validate();\r
86    ap.repaint();\r
87   }\r
88 \r
89   public void doMousePressed(MouseEvent evt)\r
90   {\r
91     int y = evt.getY() - scrollOffset;\r
92     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
93     int height = 0;\r
94     for (int i = 0; i < aa.length; i++)\r
95     {\r
96       if (!aa[i].visible)\r
97         continue;\r
98 \r
99       height += aa[i].height;\r
100       if (y < height)\r
101       {\r
102         selectedRow = i;\r
103         break;\r
104       }\r
105     }\r
106 \r
107     PopupMenu pop = new PopupMenu("Annotations");\r
108     MenuItem item = new MenuItem(HIDE);\r
109     item.addActionListener(this);\r
110     pop.add(item);\r
111     item = new MenuItem(SHOWALL);\r
112     item.addActionListener(this);\r
113     pop.add(item);\r
114     this.add(pop);\r
115     item = new MenuItem(OUTPUT_TEXT);\r
116     item.addActionListener(this);\r
117     pop.add(item);\r
118     pop.show(this, evt.getX(), evt.getY());\r
119 \r
120   }\r
121 \r
122   public void paint(Graphics g)\r
123   {\r
124     drawComponent(g);\r
125   }\r
126 \r
127   public void drawComponent(Graphics g)\r
128   {\r
129     FontMetrics fm = g.getFontMetrics(g.getFont());\r
130     g.setColor(Color.white);\r
131     g.fillRect(0,0, getSize().width, getSize().height);\r
132 \r
133     g.translate(0, scrollOffset);\r
134     g.setColor(Color.black);\r
135 \r
136    AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
137    int y = g.getFont().getSize();\r
138    int x = 0;\r
139 \r
140    if(aa!=null)\r
141    for(int i=0; i<aa.length; i++)\r
142    {\r
143      if(!aa[i].visible)\r
144        continue;\r
145 \r
146      x = getSize().width - fm.stringWidth(aa[i].label)-3;\r
147 \r
148      if(aa[i].isGraph)\r
149        y+=(aa[i].height/3);\r
150 \r
151      g.drawString(aa[i].label, x, y);\r
152 \r
153      if(aa[i].isGraph)\r
154        y+=(2*aa[i].height/3);\r
155     else\r
156        y+=aa[i].height;\r
157    }\r
158   }\r
159 \r
160 }\r