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 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(
125 MessageManager.getString("label.invert_selection"));
126 invert.addActionListener(new ActionListener()
130 public void actionPerformed(ActionEvent e)
136 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
137 lowerPanel.add(invert);
139 Panel tPanel = new Panel(new BorderLayout());
141 tPanel.add(transparency, BorderLayout.CENTER);
142 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
144 lowerPanel.add(tPanel, BorderLayout.SOUTH);
146 add(lowerPanel, BorderLayout.SOUTH);
148 groupPanel.setLayout(
149 new GridLayout((fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote
160 groupPanel.validate();
162 add(groupPanel, BorderLayout.NORTH);
166 final FeatureSettings me = this;
167 frame.addWindowListener(new WindowAdapter()
170 public void windowClosing(WindowEvent e)
172 if (me.av.featureSettings == me)
174 me.av.featureSettings = null;
180 int height = featurePanel.getComponentCount() * 50 + 60;
182 height = Math.max(200, height);
183 height = Math.min(400, height);
185 jalview.bin.JalviewLite.addFrame(frame,
186 MessageManager.getString("label.sequence_feature_settings"),
191 public void paint(Graphics g)
193 g.setColor(Color.black);
194 g.drawString(MessageManager.getString(
195 "label.no_features_added_to_this_alignment"), 10, 20);
196 g.drawString(MessageManager.getString(
197 "label.features_can_be_added_from_searches_1"), 10, 40);
198 g.drawString(MessageManager.getString(
199 "label.features_can_be_added_from_searches_2"), 10, 60);
202 protected void popupSort(final MyCheckbox check,
203 final Map<String, float[][]> minmax, int x, int y)
205 final String type = check.type;
206 final FeatureColourI typeCol = fr.getFeatureStyle(type);
207 PopupMenu men = new PopupMenu(MessageManager
208 .formatMessage("label.settings_for_type", new String[]
210 java.awt.MenuItem scr = new MenuItem(
211 MessageManager.getString("label.sort_by_score"));
213 final FeatureSettings me = this;
214 scr.addActionListener(new ActionListener()
218 public void actionPerformed(ActionEvent e)
221 .sortAlignmentByFeatureScore(Arrays.asList(new String[]
226 MenuItem dens = new MenuItem(
227 MessageManager.getString("label.sort_by_density"));
228 dens.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
235 .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
244 final float[][] typeMinMax = minmax.get(type);
246 * final java.awt.CheckboxMenuItem chb = new
247 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
248 * moment chb.setState(minmax.get(type) != null);
249 * chb.addActionListener(new ActionListener() {
251 * public void actionPerformed(ActionEvent e) {
252 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
253 * null); } else { minmax.put(type, typeMinMax); } }
257 if (typeMinMax != null && typeMinMax[0] != null)
259 // graduated colourschemes for those where minmax exists for the
260 // positional features
261 MenuItem mxcol = new MenuItem(
262 (typeCol.isSimpleColour()) ? "Graduated Colour"
265 mxcol.addActionListener(new ActionListener()
269 public void actionPerformed(ActionEvent e)
271 if (typeCol.isSimpleColour())
273 new FeatureColourChooser(me, type);
274 // write back the current colour object to update the table
275 check.updateColor(fr.getFeatureStyle(type));
279 new UserDefinedColours(me, check.type, typeCol);
287 MenuItem selectContaining = new MenuItem(
288 MessageManager.getString("label.select_columns_containing"));
289 selectContaining.addActionListener(new ActionListener()
292 public void actionPerformed(ActionEvent e)
294 me.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
298 men.add(selectContaining);
300 MenuItem selectNotContaining = new MenuItem(MessageManager
301 .getString("label.select_columns_not_containing"));
302 selectNotContaining.addActionListener(new ActionListener()
305 public void actionPerformed(ActionEvent e)
307 me.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
311 men.add(selectNotContaining);
313 MenuItem hideContaining = new MenuItem(
314 MessageManager.getString("label.hide_columns_containing"));
315 hideContaining.addActionListener(new ActionListener()
318 public void actionPerformed(ActionEvent e)
320 hideFeatureColumns(type, true);
323 men.add(hideContaining);
325 MenuItem hideNotContaining = new MenuItem(
326 MessageManager.getString("label.hide_columns_not_containing"));
327 hideNotContaining.addActionListener(new ActionListener()
330 public void actionPerformed(ActionEvent e)
332 hideFeatureColumns(type, false);
335 men.add(hideNotContaining);
337 this.featurePanel.add(men);
338 men.show(this.featurePanel, x, y);
342 public void discoverAllFeatureData()
344 if (fr.getAllFeatureColours() != null
345 && fr.getAllFeatureColours().size() > 0)
354 * Answers the visibility of the given group, and adds a checkbox for it if
355 * there is not one already
357 public boolean checkGroupState(String group)
359 boolean visible = fr.checkGroupVisibility(group, true);
362 * is there already a checkbox for this group?
364 for (int g = 0; g < groupPanel.getComponentCount(); g++)
366 if (((Checkbox) groupPanel.getComponent(g)).getLabel().equals(group))
368 ((Checkbox) groupPanel.getComponent(g)).setState(visible);
376 Checkbox check = new MyCheckbox(group, visible, false);
377 check.addMouseListener(this);
378 check.setFont(new Font("Serif", Font.BOLD, 12));
379 check.addItemListener(groupItemListener);
380 groupPanel.add(check);
382 groupPanel.validate();
386 // This routine adds and removes checkboxes depending on
387 // Group selection states
388 void resetTable(boolean groupsChanged)
390 List<String> displayableTypes = new ArrayList<>();
391 Set<String> foundGroups = new HashSet<>();
393 AlignmentI alignment = av.getAlignment();
395 for (int i = 0; i < alignment.getHeight(); i++)
397 SequenceI seq = alignment.getSequenceAt(i);
400 * get the sequence's groups for positional features
401 * and keep track of which groups are visible
403 Set<String> groups = seq.getFeatures().getFeatureGroups(true);
404 Set<String> visibleGroups = new HashSet<>();
405 for (String group : groups)
407 // if (group == null || fr.checkGroupVisibility(group, true))
408 if (group == null || checkGroupState(group))
410 visibleGroups.add(group);
413 foundGroups.addAll(groups);
416 * get distinct feature types for visible groups
417 * record distinct visible types
419 Set<String> types = seq.getFeatures().getFeatureTypesForGroups(true,
420 visibleGroups.toArray(new String[visibleGroups.size()]));
421 displayableTypes.addAll(types);
425 * remove any checkboxes for groups not present
427 pruneGroups(foundGroups);
430 int cSize = featurePanel.getComponentCount();
432 // This will remove any checkboxes which shouldn't be
434 for (int i = 0; i < cSize; i++)
436 comps = featurePanel.getComponents();
437 check = (MyCheckbox) comps[i];
438 if (!displayableTypes.contains(check.type))
440 featurePanel.remove(i);
446 if (fr.getRenderOrder() != null)
448 // First add the checks in the previous render order,
449 // in case the window has been closed and reopened
450 List<String> rol = fr.getRenderOrder();
451 for (int ro = rol.size() - 1; ro > -1; ro--)
453 String item = rol.get(ro);
455 if (!displayableTypes.contains(item))
460 displayableTypes.remove(item);
462 addCheck(false, item);
467 * now add checkboxes which should be visible,
468 * if they have not already been added
470 for (String type : displayableTypes)
472 addCheck(groupsChanged, type);
475 featurePanel.setLayout(
476 new GridLayout(featurePanel.getComponentCount(), 1, 10, 5));
477 featurePanel.validate();
479 if (scrollPane != null)
481 scrollPane.validate();
484 itemStateChanged(null);
488 * Remove from the groups panel any checkboxes for groups that are not in the
489 * foundGroups set. This enables removing a group from the display when the
490 * last feature in that group is deleted.
494 protected void pruneGroups(Set<String> foundGroups)
496 for (int g = 0; g < groupPanel.getComponentCount(); g++)
498 Checkbox checkbox = (Checkbox) groupPanel.getComponent(g);
499 if (!foundGroups.contains(checkbox.getLabel()))
501 groupPanel.remove(checkbox);
507 * update the checklist of feature types with the given type
509 * @param groupsChanged
510 * true means if the type is not in the display list then it will be
511 * added and displayed
513 * feature type to be checked for in the list.
515 void addCheck(boolean groupsChanged, String type)
518 Component[] comps = featurePanel.getComponents();
521 for (int i = 0; i < featurePanel.getComponentCount(); i++)
523 check = (MyCheckbox) comps[i];
524 if (check.type.equals(type))
533 boolean selected = false;
534 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
539 check = new MyCheckbox(type, selected, false,
540 fr.getFeatureStyle(type));
542 check.addMouseListener(this);
543 check.addMouseMotionListener(this);
544 check.addItemListener(this);
547 // add at beginning of stack.
548 featurePanel.add(check, 0);
552 // add at end of stack.
553 featurePanel.add(check);
558 protected void invertSelection()
560 for (int i = 0; i < featurePanel.getComponentCount(); i++)
562 Checkbox check = (Checkbox) featurePanel.getComponent(i);
563 check.setState(!check.getState());
565 selectionChanged(true);
568 private ItemListener groupItemListener = new ItemListener()
571 public void itemStateChanged(ItemEvent evt)
573 Checkbox source = (Checkbox) evt.getSource();
574 fr.setGroupVisibility(source.getLabel(), source.getState());
575 ap.seqPanel.seqCanvas.repaint();
576 if (ap.overviewPanel != null)
578 ap.overviewPanel.updateOverviewImage();
586 public void itemStateChanged(ItemEvent evt)
588 selectionChanged(true);
591 void selectionChanged(boolean updateOverview)
593 Component[] comps = featurePanel.getComponents();
594 int cSize = comps.length;
595 FeatureSettingsBean[] rowData = new FeatureSettingsBean[cSize];
597 for (Component comp : comps)
599 MyCheckbox check = (MyCheckbox) comp;
600 // feature filter set to null as not (yet) offered in applet
601 FeatureColourI colour = fr.getFeatureStyle(check.type);
602 rowData[i] = new FeatureSettingsBean(check.type, colour, null,
607 fr.setFeaturePriority(rowData);
609 ap.paintAlignment(updateOverview, updateOverview);
612 MyCheckbox selectedCheck;
614 boolean dragging = false;
617 public void mouseDragged(MouseEvent evt)
619 if (((Component) evt.getSource()).getParent() != featurePanel)
627 public void mouseReleased(MouseEvent evt)
629 if (((Component) evt.getSource()).getParent() != featurePanel)
634 Component comp = null;
635 Checkbox target = null;
637 int height = evt.getY() + evt.getComponent().getLocation().y;
639 if (height > featurePanel.getSize().height)
643 .getComponent(featurePanel.getComponentCount() - 1);
647 comp = featurePanel.getComponent(0);
651 comp = featurePanel.getComponentAt(evt.getX(),
652 evt.getY() + evt.getComponent().getLocation().y);
655 if (comp != null && comp instanceof Checkbox)
657 target = (Checkbox) comp;
660 if (selectedCheck != null && target != null && selectedCheck != target)
662 int targetIndex = -1;
663 for (int i = 0; i < featurePanel.getComponentCount(); i++)
665 if (target == featurePanel.getComponent(i))
672 featurePanel.remove(selectedCheck);
673 featurePanel.add(selectedCheck, targetIndex);
674 featurePanel.validate();
675 itemStateChanged(null);
679 public void setUserColour(String feature, FeatureColourI originalColour)
681 fr.setColour(feature, originalColour);
685 public void refreshTable()
687 featurePanel.removeAll();
689 ap.paintAlignment(true, true);
693 public void mouseEntered(MouseEvent evt)
698 public void mouseExited(MouseEvent evt)
703 public void mouseClicked(MouseEvent evt)
705 MyCheckbox check = (MyCheckbox) evt.getSource();
706 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
708 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
711 if (check.getParent() != featurePanel)
716 if (evt.getClickCount() > 1)
718 FeatureColourI fcol = fr.getFeatureStyle(check.type);
719 if (fcol.isSimpleColour())
721 new UserDefinedColours(this, check.type, fcol.getColour());
725 new FeatureColourChooser(this, check.type);
726 // write back the current colour object to update the table
727 check.updateColor(fr.getFeatureStyle(check.type));
733 public void mouseMoved(MouseEvent evt)
738 public void adjustmentValueChanged(AdjustmentEvent evt)
740 fr.setTransparency((100 - transparency.getValue()) / 100f);
741 ap.paintAlignment(true, true);
744 class MyCheckbox extends Checkbox
748 public int stringWidth;
754 public void updateColor(FeatureColourI newcol)
757 if (col.isSimpleColour())
759 setBackground(col.getColour());
763 String vlabel = type;
764 if (col.isAboveThreshold())
768 else if (col.isBelowThreshold())
772 if (col.isColourByLabel())
774 setBackground(Color.white);
775 vlabel += " (by Label)";
779 setBackground(col.getMinColour());
781 this.setLabel(vlabel);
786 public MyCheckbox(String label, boolean checked, boolean haslink)
788 super(label, checked);
790 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
791 stringWidth = fm.stringWidth(label);
792 this.hasLink = haslink;
795 public MyCheckbox(String type, boolean selected, boolean b,
796 FeatureColourI featureStyle)
798 this(type, selected, b);
799 updateColor(featureStyle);
803 public void paint(Graphics g)
805 Dimension d = getSize();
808 if (col.isColourByLabel())
810 g.setColor(Color.white);
811 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
813 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
816 * // g.setFont(g.getFont().deriveFont( //
817 * AffineTransform.getScaleInstance( //
818 * width/g.getFontMetrics().stringWidth("Label"), //
819 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
824 else if (col.isGraduatedColour())
826 Color maxCol = col.getMaxColour();
828 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
835 g.drawImage(linkImage, stringWidth + 25,
836 (getSize().height - linkImage.getHeight(this)) / 2, this);
842 * Hide columns containing (or not containing) a given feature type
845 * @param columnsContaining
847 void hideFeatureColumns(final String type, boolean columnsContaining)
849 if (ap.alignFrame.avc.markColumnsContainingFeatures(columnsContaining,
852 if (ap.alignFrame.avc.markColumnsContainingFeatures(
853 !columnsContaining, false, false, type))
855 ap.alignFrame.viewport.hideSelectedColumns();
861 public void mousePressed(MouseEvent e)
863 // TODO Auto-generated method stub