2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
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.
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.
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
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
28 public class FeatureSettings
29 extends Panel implements ItemListener,
30 MouseListener, MouseMotionListener, ActionListener, AdjustmentListener
37 Panel featurePanel = new Panel();
38 ScrollPane scrollPane;
39 boolean alignmentHasFeatures = false;
41 Scrollbar transparency;
43 public FeatureSettings(final AlignmentPanel ap)
47 ap.av.featureSettings = this;
48 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
50 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
51 100 - (int) (fr.transparency * 100), 1, 1, 100);
53 if (fr.transparencySetter != null)
55 transparency.addAdjustmentListener(this);
59 transparency.setEnabled(false);
62 java.net.URL url = getClass().getResource("/images/link.gif");
65 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
68 if (av.featuresDisplayed == null)
75 this.setLayout(new BorderLayout());
76 scrollPane = new ScrollPane();
77 scrollPane.add(featurePanel);
78 if (alignmentHasFeatures)
80 add(scrollPane, BorderLayout.CENTER);
83 Button invert = new Button("Invert Selection");
84 invert.addActionListener(this);
86 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
87 lowerPanel.add(invert);
89 Panel tPanel = new Panel(new BorderLayout());
91 if (fr.transparencySetter != null)
93 tPanel.add(transparency, BorderLayout.CENTER);
94 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
98 tPanel.add(new Label("Transparency not available in this web browser"),
102 lowerPanel.add(tPanel, BorderLayout.SOUTH);
104 add(lowerPanel, BorderLayout.SOUTH);
106 if (groupPanel != null)
108 groupPanel.setLayout(
109 new GridLayout(fr.featureGroups.size() / 4 + 1, 4));
110 groupPanel.validate();
112 add(groupPanel, BorderLayout.NORTH);
116 final FeatureSettings me = this;
117 frame.addWindowListener(new WindowAdapter()
119 public void windowClosing(WindowEvent e)
121 if (me.av.featureSettings==me)
123 me.av.featureSettings = null;
130 int height = featurePanel.getComponentCount() * 50 + 60;
132 height = Math.max(200, height);
133 height = Math.min(400, height);
135 jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", width,
139 public void paint(Graphics g)
141 g.setColor(Color.black);
142 g.drawString("No Features added to this alignment!!", 10, 20);
143 g.drawString("(Features can be added from searches or", 10, 40);
144 g.drawString("from Jalview / GFF features files)", 10, 60);
147 public void setTableData()
149 alignmentHasFeatures = fr.buildGroupHash();
150 if (alignmentHasFeatures)
158 * rebuilds the group panel
160 public void rebuildGroups()
162 boolean rdrw = false;
163 if (groupPanel == null)
165 groupPanel = new Panel();
168 groupPanel.removeAll();
171 Enumeration gps = fr.featureGroups.keys();
172 while (gps.hasMoreElements())
174 String group = (String) gps.nextElement();
175 Boolean vis = (Boolean) fr.featureGroups.get(group);
176 Checkbox check = new MyCheckbox(
179 (fr.featureLinks != null && fr.featureLinks.containsKey(group))
181 check.addMouseListener(this);
182 check.setFont(new Font("Serif", Font.BOLD, 12));
183 check.addItemListener(this);
184 groupPanel.add(check);
188 groupPanel.validate();
192 //This routine adds and removes checkboxes depending on
193 //Group selection states
194 void resetTable(boolean groupsChanged)
196 SequenceFeature[] tmpfeatures;
197 String group = null, type;
198 Vector visibleChecks = new Vector();
200 for (int i = 0; i < av.alignment.getHeight(); i++)
202 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
207 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
209 while (index < tmpfeatures.length)
211 group = tmpfeatures[index].featureGroup;
213 if (group == null || fr.featureGroups.get(group) == null ||
214 ( (Boolean) fr.featureGroups.get(group)).booleanValue())
216 type = tmpfeatures[index].getType();
217 if (!visibleChecks.contains(type))
219 visibleChecks.addElement(type);
227 int cSize = featurePanel.getComponentCount();
229 //This will remove any checkboxes which shouldn't be
231 for (int i = 0; i < cSize; i++)
233 comps = featurePanel.getComponents();
234 check = (Checkbox) comps[i];
235 if (!visibleChecks.contains(check.getLabel()))
237 featurePanel.remove(i);
243 if (fr.renderOrder != null)
245 //First add the checks in the previous render order,
246 //in case the window has been closed and reopened
247 for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
249 String item = fr.renderOrder[ro];
251 if (!visibleChecks.contains(item))
256 visibleChecks.removeElement(item);
258 addCheck(false, item);
262 // now add checkboxes which should be visible,
263 // if they have not already been added
264 Enumeration en = visibleChecks.elements();
266 while (en.hasMoreElements())
268 addCheck(groupsChanged, en.nextElement().toString());
271 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(), 1,
273 featurePanel.validate();
275 if (scrollPane != null)
277 scrollPane.validate();
280 itemStateChanged(null);
283 * update the checklist of feature types with the given type
284 * @param groupsChanged true means if the type is not in the display list then it will be added and displayed
285 * @param type feature type to be checked for in the list.
287 void addCheck(boolean groupsChanged, String type)
290 Component[] comps = featurePanel.getComponents();
293 for (int i = 0; i < featurePanel.getComponentCount(); i++)
295 check = (Checkbox) comps[i];
296 if (check.getLabel().equals(type))
305 boolean selected = false;
306 if (groupsChanged || av.featuresDisplayed.containsKey(type))
311 check = new MyCheckbox(type,
313 (fr.featureLinks != null &&
314 fr.featureLinks.containsKey(type))
317 check.addMouseListener(this);
318 check.addMouseMotionListener(this);
319 check.setBackground(fr.getColour(type));
320 check.addItemListener(this);
321 featurePanel.add(check);
325 public void actionPerformed(ActionEvent evt)
327 for (int i = 0; i < featurePanel.getComponentCount(); i++)
329 Checkbox check = (Checkbox) featurePanel.getComponent(i);
330 check.setState(!check.getState());
335 public void itemStateChanged(ItemEvent evt)
339 //Is the source a top level featureGroup?
340 Checkbox source = (Checkbox) evt.getSource();
341 if (fr.featureGroups.containsKey(source.getLabel()))
343 fr.featureGroups.put(source.getLabel(), new Boolean(source.getState()));
344 ap.seqPanel.seqCanvas.repaint();
345 if (ap.overviewPanel != null)
347 ap.overviewPanel.updateOverviewImage();
357 void selectionChanged()
359 Component[] comps = featurePanel.getComponents();
360 int cSize = comps.length;
362 Object[][] tmp = new Object[cSize][3];
364 for (int i = 0; i < cSize; i++)
366 Checkbox check = (Checkbox) comps[i];
367 tmp[tmpSize][0] = check.getLabel();
368 tmp[tmpSize][1] = fr.getColour(check.getLabel());
369 tmp[tmpSize][2] = new Boolean(check.getState());
373 Object[][] data = new Object[tmpSize][3];
374 System.arraycopy(tmp, 0, data, 0, tmpSize);
376 fr.setFeaturePriority(data);
378 ap.paintAlignment(true);
381 MyCheckbox selectedCheck;
382 boolean dragging = false;
384 public void mousePressed(MouseEvent evt)
387 selectedCheck = (MyCheckbox) evt.getSource();
389 if (fr.featureLinks != null
390 && fr.featureLinks.containsKey(selectedCheck.getLabel())
393 if (evt.getX() > selectedCheck.stringWidth + 20)
401 public void mouseDragged(MouseEvent evt)
403 if ( ( (Component) evt.getSource()).getParent() != featurePanel)
410 public void mouseReleased(MouseEvent evt)
412 if ( ( (Component) evt.getSource()).getParent() != featurePanel)
417 Component comp = null;
418 Checkbox target = null;
420 int height = evt.getY() + evt.getComponent().getLocation().y;
422 if (height > featurePanel.getSize().height)
425 comp = featurePanel.getComponent(featurePanel.getComponentCount() - 1);
429 comp = featurePanel.getComponent(0);
433 comp = featurePanel.getComponentAt(evt.getX(),
435 evt.getComponent().getLocation().y);
438 if (comp != null && comp instanceof Checkbox)
440 target = (Checkbox) comp;
443 if (selectedCheck != null
445 && selectedCheck != target)
447 int targetIndex = -1;
448 for (int i = 0; i < featurePanel.getComponentCount(); i++)
450 if (target == featurePanel.getComponent(i))
457 featurePanel.remove(selectedCheck);
458 featurePanel.add(selectedCheck, targetIndex);
459 featurePanel.validate();
460 itemStateChanged(null);
464 public void setUserColour(String feature, Color col)
466 fr.setColour(feature, col);
467 featurePanel.removeAll();
469 ap.paintAlignment(true);
472 public void mouseEntered(MouseEvent evt)
475 public void mouseExited(MouseEvent evt)
478 public void mouseClicked(MouseEvent evt)
480 MyCheckbox check = (MyCheckbox) evt.getSource();
482 if (fr.featureLinks != null
483 && fr.featureLinks.containsKey(check.getLabel()))
485 if (evt.getX() > check.stringWidth + 20)
488 String link = fr.featureLinks.get(check.getLabel()).toString();
489 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
490 link.substring(0, link.indexOf("|")));
494 if (check.getParent() != featurePanel)
499 if (evt.getClickCount() > 1)
501 new UserDefinedColours(this, check.getLabel(),
502 fr.getColour(check.getLabel()));
506 public void mouseMoved(MouseEvent evt)
509 public void adjustmentValueChanged(AdjustmentEvent evt)
511 fr.transparency = ( (float) (100 - transparency.getValue()) / 100f);
512 ap.seqPanel.seqCanvas.repaint();
519 public int stringWidth;
521 public MyCheckbox(String label, boolean checked, boolean haslink)
523 super(label, checked);
525 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
526 stringWidth = fm.stringWidth(label);
527 this.hasLink = haslink;
530 public void paint(Graphics g)
534 g.drawImage(linkImage, stringWidth + 25, (
535 getSize().height - linkImage.getHeight(this)) / 2,