2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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;
23 import jalview.api.FeatureColourI;
24 import jalview.api.FeatureSettingsControllerI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.MessageManager;
28 import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
30 import java.awt.BorderLayout;
31 import java.awt.Button;
32 import java.awt.Checkbox;
33 import java.awt.Color;
34 import java.awt.Component;
35 import java.awt.Dimension;
37 import java.awt.FontMetrics;
38 import java.awt.Frame;
39 import java.awt.Graphics;
40 import java.awt.GridLayout;
41 import java.awt.Image;
42 import java.awt.Label;
43 import java.awt.MenuItem;
44 import java.awt.Panel;
45 import java.awt.PopupMenu;
46 import java.awt.ScrollPane;
47 import java.awt.Scrollbar;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.AdjustmentEvent;
51 import java.awt.event.AdjustmentListener;
52 import java.awt.event.InputEvent;
53 import java.awt.event.ItemEvent;
54 import java.awt.event.ItemListener;
55 import java.awt.event.MouseEvent;
56 import java.awt.event.MouseListener;
57 import java.awt.event.MouseMotionListener;
58 import java.awt.event.WindowAdapter;
59 import java.awt.event.WindowEvent;
60 import java.util.ArrayList;
61 import java.util.Arrays;
62 import java.util.HashSet;
63 import java.util.List;
67 public class FeatureSettings extends Panel
68 implements ItemListener, MouseListener, MouseMotionListener,
69 ActionListener, AdjustmentListener, FeatureSettingsControllerI
81 Panel featurePanel = new Panel();
83 ScrollPane scrollPane;
87 Scrollbar transparency;
89 public FeatureSettings(final AlignmentPanel ap)
93 ap.av.featureSettings = this;
94 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
96 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
97 100 - (int) (fr.getTransparency() * 100), 1, 1, 100);
99 transparency.addAdjustmentListener(this);
101 java.net.URL url = getClass().getResource("/images/link.gif");
104 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
107 if (av.isShowSequenceFeatures() || !fr.hasRenderOrder())
109 fr.findAllFeatures(true); // was default - now true to make all visible
111 groupPanel = new Panel();
113 discoverAllFeatureData();
115 this.setLayout(new BorderLayout());
116 scrollPane = new ScrollPane();
117 scrollPane.add(featurePanel);
118 if (fr.getAllFeatureColours() != null
119 && fr.getAllFeatureColours().size() > 0)
121 add(scrollPane, BorderLayout.CENTER);
124 Button invert = new Button("Invert Selection");
125 invert.addActionListener(this);
127 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
128 lowerPanel.add(invert);
130 Panel tPanel = new Panel(new BorderLayout());
132 tPanel.add(transparency, BorderLayout.CENTER);
133 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
135 lowerPanel.add(tPanel, BorderLayout.SOUTH);
137 add(lowerPanel, BorderLayout.SOUTH);
139 groupPanel.setLayout(
140 new GridLayout((fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote
151 groupPanel.validate();
153 add(groupPanel, BorderLayout.NORTH);
157 final FeatureSettings me = this;
158 frame.addWindowListener(new WindowAdapter()
161 public void windowClosing(WindowEvent e)
163 if (me.av.featureSettings == me)
165 me.av.featureSettings = null;
171 int height = featurePanel.getComponentCount() * 50 + 60;
173 height = Math.max(200, height);
174 height = Math.min(400, height);
176 jalview.bin.JalviewLite.addFrame(frame,
177 MessageManager.getString("label.sequence_feature_settings"),
182 public void paint(Graphics g)
184 g.setColor(Color.black);
185 g.drawString(MessageManager.getString(
186 "label.no_features_added_to_this_alignment"), 10, 20);
187 g.drawString(MessageManager.getString(
188 "label.features_can_be_added_from_searches_1"), 10, 40);
189 g.drawString(MessageManager.getString(
190 "label.features_can_be_added_from_searches_2"), 10, 60);
193 protected void popupSort(final MyCheckbox check,
194 final Map<String, float[][]> minmax, int x, int y)
196 final String type = check.type;
197 final FeatureColourI typeCol = fr.getFeatureStyle(type);
198 PopupMenu men = new PopupMenu(MessageManager
199 .formatMessage("label.settings_for_type", new String[]
201 java.awt.MenuItem scr = new MenuItem(
202 MessageManager.getString("label.sort_by_score"));
204 final FeatureSettings me = this;
205 scr.addActionListener(new ActionListener()
209 public void actionPerformed(ActionEvent e)
212 .sortAlignmentByFeatureScore(Arrays.asList(new String[]
217 MenuItem dens = new MenuItem(
218 MessageManager.getString("label.sort_by_density"));
219 dens.addActionListener(new ActionListener()
223 public void actionPerformed(ActionEvent e)
226 .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
235 final float[][] typeMinMax = minmax.get(type);
237 * final java.awt.CheckboxMenuItem chb = new
238 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
239 * moment chb.setState(minmax.get(type) != null);
240 * chb.addActionListener(new ActionListener() {
242 * public void actionPerformed(ActionEvent e) {
243 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
244 * null); } else { minmax.put(type, typeMinMax); } }
248 if (typeMinMax != null && typeMinMax[0] != null)
250 // graduated colourschemes for those where minmax exists for the
251 // positional features
252 MenuItem mxcol = new MenuItem(
253 (typeCol.isSimpleColour()) ? "Graduated Colour"
256 mxcol.addActionListener(new ActionListener()
260 public void actionPerformed(ActionEvent e)
262 if (typeCol.isSimpleColour())
264 new FeatureColourChooser(me, type);
265 // write back the current colour object to update the table
266 check.updateColor(fr.getFeatureStyle(type));
270 new UserDefinedColours(me, check.type, typeCol);
278 MenuItem selectContaining = new MenuItem(
279 MessageManager.getString("label.select_columns_containing"));
280 selectContaining.addActionListener(new ActionListener()
283 public void actionPerformed(ActionEvent e)
285 me.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
289 men.add(selectContaining);
291 MenuItem selectNotContaining = new MenuItem(MessageManager
292 .getString("label.select_columns_not_containing"));
293 selectNotContaining.addActionListener(new ActionListener()
296 public void actionPerformed(ActionEvent e)
298 me.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
302 men.add(selectNotContaining);
304 MenuItem hideContaining = new MenuItem(
305 MessageManager.getString("label.hide_columns_containing"));
306 hideContaining.addActionListener(new ActionListener()
309 public void actionPerformed(ActionEvent e)
311 hideFeatureColumns(type, true);
314 men.add(hideContaining);
316 MenuItem hideNotContaining = new MenuItem(
317 MessageManager.getString("label.hide_columns_not_containing"));
318 hideNotContaining.addActionListener(new ActionListener()
321 public void actionPerformed(ActionEvent e)
323 hideFeatureColumns(type, false);
326 men.add(hideNotContaining);
328 this.featurePanel.add(men);
329 men.show(this.featurePanel, x, y);
333 public void discoverAllFeatureData()
335 if (fr.getAllFeatureColours() != null
336 && fr.getAllFeatureColours().size() > 0)
345 * Answers the visibility of the given group, and adds a checkbox for it if
346 * there is not one already
348 public boolean checkGroupState(String group)
350 boolean visible = fr.checkGroupVisibility(group, true);
353 * is there already a checkbox for this group?
355 for (int g = 0; g < groupPanel.getComponentCount(); g++)
357 if (((Checkbox) groupPanel.getComponent(g)).getLabel().equals(group))
359 ((Checkbox) groupPanel.getComponent(g)).setState(visible);
367 Checkbox check = new MyCheckbox(group, visible, false);
368 check.addMouseListener(this);
369 check.setFont(new Font("Serif", Font.BOLD, 12));
370 check.addItemListener(groupItemListener);
371 groupPanel.add(check);
373 groupPanel.validate();
377 // This routine adds and removes checkboxes depending on
378 // Group selection states
379 void resetTable(boolean groupsChanged)
381 List<String> displayableTypes = new ArrayList<>();
382 Set<String> foundGroups = new HashSet<>();
384 AlignmentI alignment = av.getAlignment();
386 for (int i = 0; i < alignment.getHeight(); i++)
388 SequenceI seq = alignment.getSequenceAt(i);
391 * get the sequence's groups for positional features
392 * and keep track of which groups are visible
394 Set<String> groups = seq.getFeatures().getFeatureGroups(true);
395 Set<String> visibleGroups = new HashSet<>();
396 for (String group : groups)
398 // if (group == null || fr.checkGroupVisibility(group, true))
399 if (group == null || checkGroupState(group))
401 visibleGroups.add(group);
404 foundGroups.addAll(groups);
407 * get distinct feature types for visible groups
408 * record distinct visible types
410 Set<String> types = seq.getFeatures().getFeatureTypesForGroups(true,
411 visibleGroups.toArray(new String[visibleGroups.size()]));
412 displayableTypes.addAll(types);
416 * remove any checkboxes for groups not present
418 pruneGroups(foundGroups);
421 int cSize = featurePanel.getComponentCount();
423 // This will remove any checkboxes which shouldn't be
425 for (int i = 0; i < cSize; i++)
427 comps = featurePanel.getComponents();
428 check = (MyCheckbox) comps[i];
429 if (!displayableTypes.contains(check.type))
431 featurePanel.remove(i);
437 if (fr.getRenderOrder() != null)
439 // First add the checks in the previous render order,
440 // in case the window has been closed and reopened
441 List<String> rol = fr.getRenderOrder();
442 for (int ro = rol.size() - 1; ro > -1; ro--)
444 String item = rol.get(ro);
446 if (!displayableTypes.contains(item))
451 displayableTypes.remove(item);
453 addCheck(false, item);
458 * now add checkboxes which should be visible,
459 * if they have not already been added
461 for (String type : displayableTypes)
463 addCheck(groupsChanged, type);
466 featurePanel.setLayout(
467 new GridLayout(featurePanel.getComponentCount(), 1, 10, 5));
468 featurePanel.validate();
470 if (scrollPane != null)
472 scrollPane.validate();
475 itemStateChanged(null);
479 * Remove from the groups panel any checkboxes for groups that are not in the
480 * foundGroups set. This enables removing a group from the display when the
481 * last feature in that group is deleted.
485 protected void pruneGroups(Set<String> foundGroups)
487 for (int g = 0; g < groupPanel.getComponentCount(); g++)
489 Checkbox checkbox = (Checkbox) groupPanel.getComponent(g);
490 if (!foundGroups.contains(checkbox.getLabel()))
492 groupPanel.remove(checkbox);
498 * update the checklist of feature types with the given type
500 * @param groupsChanged
501 * true means if the type is not in the display list then it will be
502 * added and displayed
504 * feature type to be checked for in the list.
506 void addCheck(boolean groupsChanged, String type)
509 Component[] comps = featurePanel.getComponents();
512 for (int i = 0; i < featurePanel.getComponentCount(); i++)
514 check = (MyCheckbox) comps[i];
515 if (check.type.equals(type))
524 boolean selected = false;
525 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
530 check = new MyCheckbox(type, selected, false,
531 fr.getFeatureStyle(type));
533 check.addMouseListener(this);
534 check.addMouseMotionListener(this);
535 check.addItemListener(this);
538 // add at beginning of stack.
539 featurePanel.add(check, 0);
543 // add at end of stack.
544 featurePanel.add(check);
550 public void actionPerformed(ActionEvent evt)
552 for (int i = 0; i < featurePanel.getComponentCount(); i++)
554 Checkbox check = (Checkbox) featurePanel.getComponent(i);
555 check.setState(!check.getState());
557 selectionChanged(true);
560 private ItemListener groupItemListener = new ItemListener()
563 public void itemStateChanged(ItemEvent evt)
565 Checkbox source = (Checkbox) evt.getSource();
566 fr.setGroupVisibility(source.getLabel(), source.getState());
567 ap.seqPanel.seqCanvas.repaint();
568 if (ap.overviewPanel != null)
570 ap.overviewPanel.updateOverviewImage();
578 public void itemStateChanged(ItemEvent evt)
580 selectionChanged(true);
583 void selectionChanged(boolean updateOverview)
585 Component[] comps = featurePanel.getComponents();
586 int cSize = comps.length;
587 FeatureSettingsBean[] rowData = new FeatureSettingsBean[cSize];
589 for (Component comp : comps)
591 MyCheckbox check = (MyCheckbox) comp;
592 // feature filter set to null as not (yet) offered in applet
593 FeatureColourI colour = fr.getFeatureStyle(check.type);
594 rowData[i] = new FeatureSettingsBean(check.type, colour, null,
599 fr.setFeaturePriority(rowData);
601 ap.paintAlignment(updateOverview, updateOverview);
604 MyCheckbox selectedCheck;
606 boolean dragging = false;
609 public void mouseDragged(MouseEvent evt)
611 if (((Component) evt.getSource()).getParent() != featurePanel)
619 public void mouseReleased(MouseEvent evt)
621 if (((Component) evt.getSource()).getParent() != featurePanel)
626 Component comp = null;
627 Checkbox target = null;
629 int height = evt.getY() + evt.getComponent().getLocation().y;
631 if (height > featurePanel.getSize().height)
635 .getComponent(featurePanel.getComponentCount() - 1);
639 comp = featurePanel.getComponent(0);
643 comp = featurePanel.getComponentAt(evt.getX(),
644 evt.getY() + evt.getComponent().getLocation().y);
647 if (comp != null && comp instanceof Checkbox)
649 target = (Checkbox) comp;
652 if (selectedCheck != null && target != null && selectedCheck != target)
654 int targetIndex = -1;
655 for (int i = 0; i < featurePanel.getComponentCount(); i++)
657 if (target == featurePanel.getComponent(i))
664 featurePanel.remove(selectedCheck);
665 featurePanel.add(selectedCheck, targetIndex);
666 featurePanel.validate();
667 itemStateChanged(null);
671 public void setUserColour(String feature, FeatureColourI originalColour)
673 fr.setColour(feature, originalColour);
677 public void refreshTable()
679 featurePanel.removeAll();
681 ap.paintAlignment(true, true);
685 public void mouseEntered(MouseEvent evt)
690 public void mouseExited(MouseEvent evt)
695 public void mouseClicked(MouseEvent evt)
697 MyCheckbox check = (MyCheckbox) evt.getSource();
698 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
700 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
703 if (check.getParent() != featurePanel)
708 if (evt.getClickCount() > 1)
710 FeatureColourI fcol = fr.getFeatureStyle(check.type);
711 if (fcol.isSimpleColour())
713 new UserDefinedColours(this, check.type, fcol.getColour());
717 new FeatureColourChooser(this, check.type);
718 // write back the current colour object to update the table
719 check.updateColor(fr.getFeatureStyle(check.type));
725 public void mouseMoved(MouseEvent evt)
730 public void adjustmentValueChanged(AdjustmentEvent evt)
732 fr.setTransparency((100 - transparency.getValue()) / 100f);
733 ap.paintAlignment(true, true);
736 class MyCheckbox extends Checkbox
740 public int stringWidth;
746 public void updateColor(FeatureColourI newcol)
749 if (col.isSimpleColour())
751 setBackground(col.getColour());
755 String vlabel = type;
756 if (col.isAboveThreshold())
760 else if (col.isBelowThreshold())
764 if (col.isColourByLabel())
766 setBackground(Color.white);
767 vlabel += " (by Label)";
771 setBackground(col.getMinColour());
773 this.setLabel(vlabel);
778 public MyCheckbox(String label, boolean checked, boolean haslink)
780 super(label, checked);
782 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
783 stringWidth = fm.stringWidth(label);
784 this.hasLink = haslink;
787 public MyCheckbox(String type, boolean selected, boolean b,
788 FeatureColourI featureStyle)
790 this(type, selected, b);
791 updateColor(featureStyle);
795 public void paint(Graphics g)
797 Dimension d = getSize();
800 if (col.isColourByLabel())
802 g.setColor(Color.white);
803 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
805 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
808 * // g.setFont(g.getFont().deriveFont( //
809 * AffineTransform.getScaleInstance( //
810 * width/g.getFontMetrics().stringWidth("Label"), //
811 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
816 else if (col.isGraduatedColour())
818 Color maxCol = col.getMaxColour();
820 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
827 g.drawImage(linkImage, stringWidth + 25,
828 (getSize().height - linkImage.getHeight(this)) / 2, this);
834 * Hide columns containing (or not containing) a given feature type
837 * @param columnsContaining
839 void hideFeatureColumns(final String type, boolean columnsContaining)
841 if (ap.alignFrame.avc.markColumnsContainingFeatures(columnsContaining,
844 if (ap.alignFrame.avc.markColumnsContainingFeatures(
845 !columnsContaining, false, false, type))
847 ap.alignFrame.viewport.hideSelectedColumns();
853 public void mousePressed(MouseEvent e)
855 // TODO Auto-generated method stub