2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
26 import java.awt.event.*;
28 import jalview.analysis.AlignmentSorter;
29 import jalview.commands.OrderCommand;
30 import jalview.datamodel.*;
31 import jalview.schemes.AnnotationColourGradient;
32 import jalview.schemes.GraduatedColor;
33 import jalview.util.MessageManager;
35 public class FeatureSettings extends Panel implements ItemListener,
36 MouseListener, MouseMotionListener, ActionListener,
49 Panel featurePanel = new Panel();
51 ScrollPane scrollPane;
53 boolean alignmentHasFeatures = false;
57 Scrollbar transparency;
59 public FeatureSettings(final AlignmentPanel ap)
63 ap.av.featureSettings = this;
64 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
66 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
67 100 - (int) (fr.transparency * 100), 1, 1, 100);
69 if (fr.transparencySetter != null)
71 transparency.addAdjustmentListener(this);
75 transparency.setEnabled(false);
78 java.net.URL url = getClass().getResource("/images/link.gif");
81 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
84 if (av.featuresDisplayed == null)
91 this.setLayout(new BorderLayout());
92 scrollPane = new ScrollPane();
93 scrollPane.add(featurePanel);
94 if (alignmentHasFeatures)
96 add(scrollPane, BorderLayout.CENTER);
99 Button invert = new Button("Invert Selection");
100 invert.addActionListener(this);
102 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
103 lowerPanel.add(invert);
105 Panel tPanel = new Panel(new BorderLayout());
107 if (fr.transparencySetter != null)
109 tPanel.add(transparency, BorderLayout.CENTER);
110 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
115 new Label("Transparency not available in this web browser"),
116 BorderLayout.CENTER);
119 lowerPanel.add(tPanel, BorderLayout.SOUTH);
121 add(lowerPanel, BorderLayout.SOUTH);
123 if (groupPanel != null)
126 .setLayout(new GridLayout(
127 (fr.featureGroups.size() - fr.hiddenGroups.size()) / 4 + 1,
129 groupPanel.validate();
131 add(groupPanel, BorderLayout.NORTH);
135 final FeatureSettings me = this;
136 frame.addWindowListener(new WindowAdapter()
138 public void windowClosing(WindowEvent e)
140 if (me.av.featureSettings == me)
142 me.av.featureSettings = null;
148 int height = featurePanel.getComponentCount() * 50 + 60;
150 height = Math.max(200, height);
151 height = Math.min(400, height);
153 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("label.feature_settings"), width,
157 public void paint(Graphics g)
159 g.setColor(Color.black);
160 g.drawString(MessageManager.getString("label.no_features_added_to_this_alignment"), 10, 20);
161 g.drawString(MessageManager.getString("label.features_can_be_added_from_searches_1"), 10, 40);
162 g.drawString(MessageManager.getString("label.features_can_be_added_from_searches_2"), 10, 60);
165 protected void popupSort(final MyCheckbox check, final Hashtable minmax,
168 final String type = check.type;
169 final Object typeCol = fr.getFeatureStyle(type);
170 java.awt.PopupMenu men = new PopupMenu(MessageManager.formatMessage("label.settings_for_type", new String[]{type}));
171 java.awt.MenuItem scr = new MenuItem(MessageManager.getString("label.sort_by_score"));
173 final FeatureSettings me = this;
174 scr.addActionListener(new ActionListener()
177 public void actionPerformed(ActionEvent e)
179 me.sortByScore(new String[]
184 MenuItem dens = new MenuItem(MessageManager.getString("label.sort_by_density"));
185 dens.addActionListener(new ActionListener()
188 public void actionPerformed(ActionEvent e)
190 me.sortByDens(new String[]
198 final Object typeMinMax = minmax.get(type);
200 * final java.awt.CheckboxMenuItem chb = new
201 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
202 * moment chb.setState(minmax.get(type) != null);
203 * chb.addActionListener(new ActionListener() {
205 * public void actionPerformed(ActionEvent e) {
206 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
207 * null); } else { minmax.put(type, typeMinMax); } }
211 if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null)
213 // graduated colourschemes for those where minmax exists for the
214 // positional features
215 MenuItem mxcol = new MenuItem(
216 (typeCol instanceof Color) ? "Graduated Colour"
219 mxcol.addActionListener(new ActionListener()
222 public void actionPerformed(ActionEvent e)
224 if (typeCol instanceof Color)
226 new FeatureColourChooser(me, type);
227 // write back the current colour object to update the table
228 check.updateColor(fr.getFeatureStyle(type));
232 new UserDefinedColours(me, check.type,
233 ((GraduatedColor) typeCol));
240 this.featurePanel.add(men);
241 men.show(this.featurePanel, x, y);
244 public void setTableData()
246 alignmentHasFeatures = fr.buildGroupHash();
247 if (alignmentHasFeatures)
256 * rebuilds the group panel
258 public void rebuildGroups()
260 boolean rdrw = false;
261 if (groupPanel == null)
263 groupPanel = new Panel();
268 groupPanel.removeAll();
270 // TODO: JAL-964 - smoothly incorporate new group entries if panel already
271 // displayed and new groups present
272 Enumeration gps = fr.featureGroups.keys();
273 while (gps.hasMoreElements())
275 String group = (String) gps.nextElement();
276 Boolean vis = (Boolean) fr.featureGroups.get(group);
277 Checkbox check = new MyCheckbox(group, vis.booleanValue(),
278 (fr.featureLinks != null && fr.featureLinks
279 .containsKey(group)));
280 check.addMouseListener(this);
281 check.setFont(new Font("Serif", Font.BOLD, 12));
282 check.addItemListener(this);
283 check.setVisible(fr.hiddenGroups.contains(group));
284 groupPanel.add(check);
288 groupPanel.validate();
292 // This routine adds and removes checkboxes depending on
293 // Group selection states
294 void resetTable(boolean groupsChanged)
296 SequenceFeature[] tmpfeatures;
297 String group = null, type;
298 Vector visibleChecks = new Vector();
299 AlignmentI alignment = av.getAlignment();
300 for (int i = 0; i < alignment.getHeight(); i++)
302 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
307 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
309 while (index < tmpfeatures.length)
311 group = tmpfeatures[index].featureGroup;
313 if (group == null || fr.featureGroups.get(group) == null
314 || ((Boolean) fr.featureGroups.get(group)).booleanValue())
316 type = tmpfeatures[index].getType();
317 if (!visibleChecks.contains(type))
319 visibleChecks.addElement(type);
327 int cSize = featurePanel.getComponentCount();
329 // This will remove any checkboxes which shouldn't be
331 for (int i = 0; i < cSize; i++)
333 comps = featurePanel.getComponents();
334 check = (MyCheckbox) comps[i];
335 if (!visibleChecks.contains(check.type))
337 featurePanel.remove(i);
343 if (fr.renderOrder != null)
345 // First add the checks in the previous render order,
346 // in case the window has been closed and reopened
347 for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
349 String item = fr.renderOrder[ro];
351 if (!visibleChecks.contains(item))
356 visibleChecks.removeElement(item);
358 addCheck(false, item);
362 // now add checkboxes which should be visible,
363 // if they have not already been added
364 Enumeration en = visibleChecks.elements();
366 while (en.hasMoreElements())
368 addCheck(groupsChanged, en.nextElement().toString());
371 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
373 featurePanel.validate();
375 if (scrollPane != null)
377 scrollPane.validate();
380 itemStateChanged(null);
384 * update the checklist of feature types with the given type
386 * @param groupsChanged
387 * true means if the type is not in the display list then it will be
388 * added and displayed
390 * feature type to be checked for in the list.
392 void addCheck(boolean groupsChanged, String type)
395 Component[] comps = featurePanel.getComponents();
398 for (int i = 0; i < featurePanel.getComponentCount(); i++)
400 check = (MyCheckbox) comps[i];
401 if (check.type.equals(type))
410 boolean selected = false;
411 if (groupsChanged || av.featuresDisplayed.containsKey(type))
416 check = new MyCheckbox(
419 (fr.featureLinks != null && fr.featureLinks.containsKey(type)),
420 fr.getFeatureStyle(type));
422 check.addMouseListener(this);
423 check.addMouseMotionListener(this);
424 check.addItemListener(this);
427 // add at beginning of stack.
428 featurePanel.add(check, 0);
432 // add at end of stack.
433 featurePanel.add(check);
438 public void actionPerformed(ActionEvent evt)
440 for (int i = 0; i < featurePanel.getComponentCount(); i++)
442 Checkbox check = (Checkbox) featurePanel.getComponent(i);
443 check.setState(!check.getState());
448 public void itemStateChanged(ItemEvent evt)
452 // Is the source a top level featureGroup?
453 Checkbox source = (Checkbox) evt.getSource();
454 if (fr.featureGroups.containsKey(source.getLabel()))
456 fr.featureGroups.put(source.getLabel(),
457 new Boolean(source.getState()));
458 ap.seqPanel.seqCanvas.repaint();
459 if (ap.overviewPanel != null)
461 ap.overviewPanel.updateOverviewImage();
471 void selectionChanged()
473 Component[] comps = featurePanel.getComponents();
474 int cSize = comps.length;
476 Object[][] tmp = new Object[cSize][3];
478 for (int i = 0; i < cSize; i++)
480 MyCheckbox check = (MyCheckbox) comps[i];
481 tmp[tmpSize][0] = check.type;
482 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
483 tmp[tmpSize][2] = new Boolean(check.getState());
487 Object[][] data = new Object[tmpSize][3];
488 System.arraycopy(tmp, 0, data, 0, tmpSize);
490 fr.setFeaturePriority(data);
492 ap.paintAlignment(true);
495 MyCheckbox selectedCheck;
497 boolean dragging = false;
499 public void mousePressed(MouseEvent evt)
502 selectedCheck = (MyCheckbox) evt.getSource();
504 if (fr.featureLinks != null
505 && fr.featureLinks.containsKey(selectedCheck.type))
507 if (evt.getX() > selectedCheck.stringWidth + 20)
515 public void mouseDragged(MouseEvent evt)
517 if (((Component) evt.getSource()).getParent() != featurePanel)
524 public void mouseReleased(MouseEvent evt)
526 if (((Component) evt.getSource()).getParent() != featurePanel)
531 Component comp = null;
532 Checkbox target = null;
534 int height = evt.getY() + evt.getComponent().getLocation().y;
536 if (height > featurePanel.getSize().height)
540 .getComponent(featurePanel.getComponentCount() - 1);
544 comp = featurePanel.getComponent(0);
548 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
549 + evt.getComponent().getLocation().y);
552 if (comp != null && comp instanceof Checkbox)
554 target = (Checkbox) comp;
557 if (selectedCheck != null && target != null && selectedCheck != target)
559 int targetIndex = -1;
560 for (int i = 0; i < featurePanel.getComponentCount(); i++)
562 if (target == featurePanel.getComponent(i))
569 featurePanel.remove(selectedCheck);
570 featurePanel.add(selectedCheck, targetIndex);
571 featurePanel.validate();
572 itemStateChanged(null);
576 public void setUserColour(String feature, Object originalColour)
578 if (originalColour instanceof Color
579 || originalColour instanceof GraduatedColor)
581 fr.setColour(feature, originalColour);
586 "Implementation error: Unsupported feature colour object.");
591 public void refreshTable()
593 featurePanel.removeAll();
595 ap.paintAlignment(true);
598 public void mouseEntered(MouseEvent evt)
602 public void mouseExited(MouseEvent evt)
606 public void mouseClicked(MouseEvent evt)
608 MyCheckbox check = (MyCheckbox) evt.getSource();
609 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
611 this.popupSort(check, fr.minmax, evt.getX(), evt.getY());
613 if (fr.featureLinks != null && fr.featureLinks.containsKey(check.type))
615 if (evt.getX() > check.stringWidth + 20)
618 String link = fr.featureLinks.get(check.type).toString();
619 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
620 link.substring(0, link.indexOf("|")));
624 if (check.getParent() != featurePanel)
629 if (evt.getClickCount() > 1)
631 Object fcol = fr.getFeatureStyle(check.type);
632 if (fcol instanceof Color)
634 new UserDefinedColours(this, check.type, (Color) fcol);
638 new FeatureColourChooser(this, check.type);
639 // write back the current colour object to update the table
640 check.updateColor(fr.getFeatureStyle(check.type));
645 public void mouseMoved(MouseEvent evt)
649 public void adjustmentValueChanged(AdjustmentEvent evt)
651 fr.transparency = ((float) (100 - transparency.getValue()) / 100f);
652 ap.seqPanel.seqCanvas.repaint();
656 class MyCheckbox extends Checkbox
660 public int stringWidth;
668 public void updateColor(Object newcol)
670 if (newcol instanceof Color)
672 col = (Color) newcol;
675 else if (newcol instanceof GraduatedColor)
677 gcol = (GraduatedColor) newcol;
682 throw new Error("Invalid color for MyCheckBox");
690 String vlabel = type;
691 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
694 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
697 if (gcol.isColourByLabel())
699 setBackground(Color.white);
700 vlabel += " (by Label)";
704 setBackground(gcol.getMinColor());
706 this.setLabel(vlabel);
711 public MyCheckbox(String label, boolean checked, boolean haslink)
713 super(label, checked);
715 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
716 stringWidth = fm.stringWidth(label);
717 this.hasLink = haslink;
720 public MyCheckbox(String type, boolean selected, boolean b,
723 this(type, selected, b);
724 updateColor(featureStyle);
727 public void paint(Graphics g)
729 Dimension d = getSize();
732 if (gcol.isColourByLabel())
734 g.setColor(Color.white);
735 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
737 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
740 * // g.setFont(g.getFont().deriveFont( //
741 * AffineTransform.getScaleInstance( //
742 * width/g.getFontMetrics().stringWidth("Label"), //
743 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
750 Color maxCol = gcol.getMaxColor();
752 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
759 g.drawImage(linkImage, stringWidth + 25,
760 (getSize().height - linkImage.getHeight(this)) / 2, this);
765 protected void sortByDens(String[] typ)
767 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
770 private String[] getDisplayedFeatureTypes()
775 synchronized (fr.renderOrder)
777 typ = new String[fr.renderOrder.length];
778 System.arraycopy(fr.renderOrder, 0, typ, 0, typ.length);
779 for (int i = 0; i < typ.length; i++)
781 if (av.featuresDisplayed.get(typ[i]) == null)
791 protected void sortBy(String[] typ, String methodText, final String method)
795 typ = getDisplayedFeatureTypes();
798 gps = fr.getGroups(true);
801 for (int i = 0; i < typ.length; i++)
803 System.err.println("Sorting on Types:" + typ[i]);
809 for (int i = 0; i < gps.length; i++)
811 System.err.println("Sorting on groups:" + gps[i]);
814 AlignmentPanel alignPanel = ap;
815 AlignmentI al = alignPanel.av.getAlignment();
818 SequenceGroup sg = alignPanel.av.getSelectionGroup();
821 start = sg.getStartRes();
822 stop = sg.getEndRes();
827 stop = al.getWidth();
829 SequenceI[] oldOrder = al.getSequencesArray();
830 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
831 this.ap.alignFrame.addHistoryItem(new OrderCommand(methodText,
832 oldOrder, alignPanel.av.getAlignment()));
833 alignPanel.paintAlignment(true);
837 protected void sortByScore(String[] typ)
839 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);