2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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);
323 // add at beginning of stack.
324 featurePanel.add(check, 0);
328 // add at end of stack.
329 featurePanel.add(check);
334 public void actionPerformed(ActionEvent evt)
336 for (int i = 0; i < featurePanel.getComponentCount(); i++)
338 Checkbox check = (Checkbox) featurePanel.getComponent(i);
339 check.setState(!check.getState());
344 public void itemStateChanged(ItemEvent evt)
348 //Is the source a top level featureGroup?
349 Checkbox source = (Checkbox) evt.getSource();
350 if (fr.featureGroups.containsKey(source.getLabel()))
352 fr.featureGroups.put(source.getLabel(), new Boolean(source.getState()));
353 ap.seqPanel.seqCanvas.repaint();
354 if (ap.overviewPanel != null)
356 ap.overviewPanel.updateOverviewImage();
366 void selectionChanged()
368 Component[] comps = featurePanel.getComponents();
369 int cSize = comps.length;
371 Object[][] tmp = new Object[cSize][3];
373 for (int i = 0; i < cSize; i++)
375 Checkbox check = (Checkbox) comps[i];
376 tmp[tmpSize][0] = check.getLabel();
377 tmp[tmpSize][1] = fr.getColour(check.getLabel());
378 tmp[tmpSize][2] = new Boolean(check.getState());
382 Object[][] data = new Object[tmpSize][3];
383 System.arraycopy(tmp, 0, data, 0, tmpSize);
385 fr.setFeaturePriority(data);
387 ap.paintAlignment(true);
390 MyCheckbox selectedCheck;
391 boolean dragging = false;
393 public void mousePressed(MouseEvent evt)
396 selectedCheck = (MyCheckbox) evt.getSource();
398 if (fr.featureLinks != null
399 && fr.featureLinks.containsKey(selectedCheck.getLabel())
402 if (evt.getX() > selectedCheck.stringWidth + 20)
410 public void mouseDragged(MouseEvent evt)
412 if ( ( (Component) evt.getSource()).getParent() != featurePanel)
419 public void mouseReleased(MouseEvent evt)
421 if ( ( (Component) evt.getSource()).getParent() != featurePanel)
426 Component comp = null;
427 Checkbox target = null;
429 int height = evt.getY() + evt.getComponent().getLocation().y;
431 if (height > featurePanel.getSize().height)
434 comp = featurePanel.getComponent(featurePanel.getComponentCount() - 1);
438 comp = featurePanel.getComponent(0);
442 comp = featurePanel.getComponentAt(evt.getX(),
444 evt.getComponent().getLocation().y);
447 if (comp != null && comp instanceof Checkbox)
449 target = (Checkbox) comp;
452 if (selectedCheck != null
454 && selectedCheck != target)
456 int targetIndex = -1;
457 for (int i = 0; i < featurePanel.getComponentCount(); i++)
459 if (target == featurePanel.getComponent(i))
466 featurePanel.remove(selectedCheck);
467 featurePanel.add(selectedCheck, targetIndex);
468 featurePanel.validate();
469 itemStateChanged(null);
473 public void setUserColour(String feature, Color col)
475 fr.setColour(feature, col);
476 featurePanel.removeAll();
478 ap.paintAlignment(true);
481 public void mouseEntered(MouseEvent evt)
484 public void mouseExited(MouseEvent evt)
487 public void mouseClicked(MouseEvent evt)
489 MyCheckbox check = (MyCheckbox) evt.getSource();
491 if (fr.featureLinks != null
492 && fr.featureLinks.containsKey(check.getLabel()))
494 if (evt.getX() > check.stringWidth + 20)
497 String link = fr.featureLinks.get(check.getLabel()).toString();
498 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
499 link.substring(0, link.indexOf("|")));
503 if (check.getParent() != featurePanel)
508 if (evt.getClickCount() > 1)
510 new UserDefinedColours(this, check.getLabel(),
511 fr.getColour(check.getLabel()));
515 public void mouseMoved(MouseEvent evt)
518 public void adjustmentValueChanged(AdjustmentEvent evt)
520 fr.transparency = ( (float) (100 - transparency.getValue()) / 100f);
521 ap.seqPanel.seqCanvas.repaint();
528 public int stringWidth;
530 public MyCheckbox(String label, boolean checked, boolean haslink)
532 super(label, checked);
534 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
535 stringWidth = fm.stringWidth(label);
536 this.hasLink = haslink;
539 public void paint(Graphics g)
543 g.drawImage(linkImage, stringWidth + 25, (
544 getSize().height - linkImage.getHeight(this)) / 2,