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;
29 import java.awt.BorderLayout;
30 import java.awt.Button;
31 import java.awt.Checkbox;
32 import java.awt.Color;
33 import java.awt.Component;
34 import java.awt.Dimension;
36 import java.awt.FontMetrics;
37 import java.awt.Frame;
38 import java.awt.Graphics;
39 import java.awt.GridLayout;
40 import java.awt.Image;
41 import java.awt.Label;
42 import java.awt.MenuItem;
43 import java.awt.Panel;
44 import java.awt.PopupMenu;
45 import java.awt.ScrollPane;
46 import java.awt.Scrollbar;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 import java.awt.event.AdjustmentEvent;
50 import java.awt.event.AdjustmentListener;
51 import java.awt.event.InputEvent;
52 import java.awt.event.ItemEvent;
53 import java.awt.event.ItemListener;
54 import java.awt.event.MouseEvent;
55 import java.awt.event.MouseListener;
56 import java.awt.event.MouseMotionListener;
57 import java.awt.event.WindowAdapter;
58 import java.awt.event.WindowEvent;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.HashSet;
62 import java.util.List;
66 public class FeatureSettings extends Panel
67 implements ItemListener, MouseListener, MouseMotionListener,
68 ActionListener, AdjustmentListener, FeatureSettingsControllerI
80 Panel featurePanel = new Panel();
82 ScrollPane scrollPane;
86 Scrollbar transparency;
88 public FeatureSettings(final AlignmentPanel ap)
92 ap.av.featureSettings = this;
93 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
95 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
96 100 - (int) (fr.getTransparency() * 100), 1, 1, 100);
98 transparency.addAdjustmentListener(this);
100 java.net.URL url = getClass().getResource("/images/link.gif");
103 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
106 if (av.isShowSequenceFeatures() || !fr.hasRenderOrder())
108 fr.findAllFeatures(true); // was default - now true to make all visible
110 groupPanel = new Panel();
112 discoverAllFeatureData();
114 this.setLayout(new BorderLayout());
115 scrollPane = new ScrollPane();
116 scrollPane.add(featurePanel);
117 if (fr.getAllFeatureColours() != null
118 && fr.getAllFeatureColours().size() > 0)
120 add(scrollPane, BorderLayout.CENTER);
123 Button invert = new Button("Invert Selection");
124 invert.addActionListener(this);
126 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
127 lowerPanel.add(invert);
129 Panel tPanel = new Panel(new BorderLayout());
131 tPanel.add(transparency, BorderLayout.CENTER);
132 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
134 lowerPanel.add(tPanel, BorderLayout.SOUTH);
136 add(lowerPanel, BorderLayout.SOUTH);
138 groupPanel.setLayout(
139 new GridLayout((fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote
150 groupPanel.validate();
152 add(groupPanel, BorderLayout.NORTH);
156 final FeatureSettings me = this;
157 frame.addWindowListener(new WindowAdapter()
160 public void windowClosing(WindowEvent e)
162 if (me.av.featureSettings == me)
164 me.av.featureSettings = null;
170 int height = featurePanel.getComponentCount() * 50 + 60;
172 height = Math.max(200, height);
173 height = Math.min(400, height);
175 jalview.bin.JalviewLite.addFrame(frame,
176 MessageManager.getString("label.sequence_feature_settings"),
181 public void paint(Graphics g)
183 g.setColor(Color.black);
184 g.drawString(MessageManager.getString(
185 "label.no_features_added_to_this_alignment"), 10, 20);
186 g.drawString(MessageManager.getString(
187 "label.features_can_be_added_from_searches_1"), 10, 40);
188 g.drawString(MessageManager.getString(
189 "label.features_can_be_added_from_searches_2"), 10, 60);
192 protected void popupSort(final MyCheckbox check,
193 final Map<String, float[][]> minmax, int x, int y)
195 final String type = check.type;
196 final FeatureColourI typeCol = fr.getFeatureStyle(type);
197 PopupMenu men = new PopupMenu(MessageManager
198 .formatMessage("label.settings_for_type", new String[]
200 java.awt.MenuItem scr = new MenuItem(
201 MessageManager.getString("label.sort_by_score"));
203 final FeatureSettings me = this;
204 scr.addActionListener(new ActionListener()
208 public void actionPerformed(ActionEvent e)
211 .sortAlignmentByFeatureScore(Arrays.asList(new String[]
216 MenuItem dens = new MenuItem(
217 MessageManager.getString("label.sort_by_density"));
218 dens.addActionListener(new ActionListener()
222 public void actionPerformed(ActionEvent e)
225 .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
234 final float[][] typeMinMax = minmax.get(type);
236 * final java.awt.CheckboxMenuItem chb = new
237 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
238 * moment chb.setState(minmax.get(type) != null);
239 * chb.addActionListener(new ActionListener() {
241 * public void actionPerformed(ActionEvent e) {
242 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
243 * null); } else { minmax.put(type, typeMinMax); } }
247 if (typeMinMax != null && typeMinMax[0] != null)
249 // graduated colourschemes for those where minmax exists for the
250 // positional features
251 MenuItem mxcol = new MenuItem(
252 (typeCol.isSimpleColour()) ? "Graduated Colour"
255 mxcol.addActionListener(new ActionListener()
259 public void actionPerformed(ActionEvent e)
261 if (typeCol.isSimpleColour())
263 new FeatureColourChooser(me, type);
264 // write back the current colour object to update the table
265 check.updateColor(fr.getFeatureStyle(type));
269 new UserDefinedColours(me, check.type, typeCol);
277 MenuItem selectContaining = new MenuItem(
278 MessageManager.getString("label.select_columns_containing"));
279 selectContaining.addActionListener(new ActionListener()
282 public void actionPerformed(ActionEvent e)
284 me.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
288 men.add(selectContaining);
290 MenuItem selectNotContaining = new MenuItem(MessageManager
291 .getString("label.select_columns_not_containing"));
292 selectNotContaining.addActionListener(new ActionListener()
295 public void actionPerformed(ActionEvent e)
297 me.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
301 men.add(selectNotContaining);
303 MenuItem hideContaining = new MenuItem(
304 MessageManager.getString("label.hide_columns_containing"));
305 hideContaining.addActionListener(new ActionListener()
308 public void actionPerformed(ActionEvent e)
310 hideFeatureColumns(type, true);
313 men.add(hideContaining);
315 MenuItem hideNotContaining = new MenuItem(
316 MessageManager.getString("label.hide_columns_not_containing"));
317 hideNotContaining.addActionListener(new ActionListener()
320 public void actionPerformed(ActionEvent e)
322 hideFeatureColumns(type, false);
325 men.add(hideNotContaining);
327 this.featurePanel.add(men);
328 men.show(this.featurePanel, x, y);
332 public void discoverAllFeatureData()
334 if (fr.getAllFeatureColours() != null
335 && fr.getAllFeatureColours().size() > 0)
344 * Answers the visibility of the given group, and adds a checkbox for it if
345 * there is not one already
347 public boolean checkGroupState(String group)
349 boolean visible = fr.checkGroupVisibility(group, true);
352 * is there already a checkbox for this group?
354 for (int g = 0; g < groupPanel.getComponentCount(); g++)
356 if (((Checkbox) groupPanel.getComponent(g)).getLabel().equals(group))
358 ((Checkbox) groupPanel.getComponent(g)).setState(visible);
366 Checkbox check = new MyCheckbox(group, visible, false);
367 check.addMouseListener(this);
368 check.setFont(new Font("Serif", Font.BOLD, 12));
369 check.addItemListener(groupItemListener);
370 groupPanel.add(check);
372 groupPanel.validate();
376 // This routine adds and removes checkboxes depending on
377 // Group selection states
378 void resetTable(boolean groupsChanged)
380 List<String> displayableTypes = new ArrayList<>();
381 Set<String> foundGroups = new HashSet<>();
383 AlignmentI alignment = av.getAlignment();
385 for (int i = 0; i < alignment.getHeight(); i++)
387 SequenceI seq = alignment.getSequenceAt(i);
390 * get the sequence's groups for positional features
391 * and keep track of which groups are visible
393 Set<String> groups = seq.getFeatures().getFeatureGroups(true);
394 Set<String> visibleGroups = new HashSet<>();
395 for (String group : groups)
397 // if (group == null || fr.checkGroupVisibility(group, true))
398 if (group == null || checkGroupState(group))
400 visibleGroups.add(group);
403 foundGroups.addAll(groups);
406 * get distinct feature types for visible groups
407 * record distinct visible types
409 Set<String> types = seq.getFeatures().getFeatureTypesForGroups(true,
410 visibleGroups.toArray(new String[visibleGroups.size()]));
411 displayableTypes.addAll(types);
415 * remove any checkboxes for groups not present
417 pruneGroups(foundGroups);
420 int cSize = featurePanel.getComponentCount();
422 // This will remove any checkboxes which shouldn't be
424 for (int i = 0; i < cSize; i++)
426 comps = featurePanel.getComponents();
427 check = (MyCheckbox) comps[i];
428 if (!displayableTypes.contains(check.type))
430 featurePanel.remove(i);
436 if (fr.getRenderOrder() != null)
438 // First add the checks in the previous render order,
439 // in case the window has been closed and reopened
440 List<String> rol = fr.getRenderOrder();
441 for (int ro = rol.size() - 1; ro > -1; ro--)
443 String item = rol.get(ro);
445 if (!displayableTypes.contains(item))
450 displayableTypes.remove(item);
452 addCheck(false, item);
457 * now add checkboxes which should be visible,
458 * if they have not already been added
460 for (String type : displayableTypes)
462 addCheck(groupsChanged, type);
465 featurePanel.setLayout(
466 new GridLayout(featurePanel.getComponentCount(), 1, 10, 5));
467 featurePanel.validate();
469 if (scrollPane != null)
471 scrollPane.validate();
474 itemStateChanged(null);
478 * Remove from the groups panel any checkboxes for groups that are not in the
479 * foundGroups set. This enables removing a group from the display when the
480 * last feature in that group is deleted.
484 protected void pruneGroups(Set<String> foundGroups)
486 for (int g = 0; g < groupPanel.getComponentCount(); g++)
488 Checkbox checkbox = (Checkbox) groupPanel.getComponent(g);
489 if (!foundGroups.contains(checkbox.getLabel()))
491 groupPanel.remove(checkbox);
497 * update the checklist of feature types with the given type
499 * @param groupsChanged
500 * true means if the type is not in the display list then it will be
501 * added and displayed
503 * feature type to be checked for in the list.
505 void addCheck(boolean groupsChanged, String type)
508 Component[] comps = featurePanel.getComponents();
511 for (int i = 0; i < featurePanel.getComponentCount(); i++)
513 check = (MyCheckbox) comps[i];
514 if (check.type.equals(type))
523 boolean selected = false;
524 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
529 check = new MyCheckbox(type, selected, false,
530 fr.getFeatureStyle(type));
532 check.addMouseListener(this);
533 check.addMouseMotionListener(this);
534 check.addItemListener(this);
537 // add at beginning of stack.
538 featurePanel.add(check, 0);
542 // add at end of stack.
543 featurePanel.add(check);
549 public void actionPerformed(ActionEvent evt)
551 for (int i = 0; i < featurePanel.getComponentCount(); i++)
553 Checkbox check = (Checkbox) featurePanel.getComponent(i);
554 check.setState(!check.getState());
556 selectionChanged(true);
559 private ItemListener groupItemListener = new ItemListener()
562 public void itemStateChanged(ItemEvent evt)
564 Checkbox source = (Checkbox) evt.getSource();
565 fr.setGroupVisibility(source.getLabel(), source.getState());
566 ap.seqPanel.seqCanvas.repaint();
567 if (ap.overviewPanel != null)
569 ap.overviewPanel.updateOverviewImage();
577 public void itemStateChanged(ItemEvent evt)
579 selectionChanged(true);
582 void selectionChanged(boolean updateOverview)
584 Component[] comps = featurePanel.getComponents();
585 int cSize = comps.length;
587 Object[][] tmp = new Object[cSize][3];
589 for (int i = 0; i < cSize; i++)
591 MyCheckbox check = (MyCheckbox) comps[i];
592 tmp[tmpSize][0] = check.type;
593 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
594 tmp[tmpSize][2] = new Boolean(check.getState());
598 Object[][] data = new Object[tmpSize][3];
599 System.arraycopy(tmp, 0, data, 0, tmpSize);
601 fr.setFeaturePriority(data);
603 ap.paintAlignment(updateOverview, updateOverview);
606 MyCheckbox selectedCheck;
608 boolean dragging = false;
611 public void mouseDragged(MouseEvent evt)
613 if (((Component) evt.getSource()).getParent() != featurePanel)
621 public void mouseReleased(MouseEvent evt)
623 if (((Component) evt.getSource()).getParent() != featurePanel)
628 Component comp = null;
629 Checkbox target = null;
631 int height = evt.getY() + evt.getComponent().getLocation().y;
633 if (height > featurePanel.getSize().height)
637 .getComponent(featurePanel.getComponentCount() - 1);
641 comp = featurePanel.getComponent(0);
645 comp = featurePanel.getComponentAt(evt.getX(),
646 evt.getY() + evt.getComponent().getLocation().y);
649 if (comp != null && comp instanceof Checkbox)
651 target = (Checkbox) comp;
654 if (selectedCheck != null && target != null && selectedCheck != target)
656 int targetIndex = -1;
657 for (int i = 0; i < featurePanel.getComponentCount(); i++)
659 if (target == featurePanel.getComponent(i))
666 featurePanel.remove(selectedCheck);
667 featurePanel.add(selectedCheck, targetIndex);
668 featurePanel.validate();
669 itemStateChanged(null);
673 public void setUserColour(String feature, FeatureColourI originalColour)
675 fr.setColour(feature, originalColour);
679 public void refreshTable()
681 featurePanel.removeAll();
683 ap.paintAlignment(true, true);
687 public void mouseEntered(MouseEvent evt)
692 public void mouseExited(MouseEvent evt)
697 public void mouseClicked(MouseEvent evt)
699 MyCheckbox check = (MyCheckbox) evt.getSource();
700 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
702 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
705 if (check.getParent() != featurePanel)
710 if (evt.getClickCount() > 1)
712 FeatureColourI fcol = fr.getFeatureStyle(check.type);
713 if (fcol.isSimpleColour())
715 new UserDefinedColours(this, check.type, fcol.getColour());
719 new FeatureColourChooser(this, check.type);
720 // write back the current colour object to update the table
721 check.updateColor(fr.getFeatureStyle(check.type));
727 public void mouseMoved(MouseEvent evt)
732 public void adjustmentValueChanged(AdjustmentEvent evt)
734 fr.setTransparency((100 - transparency.getValue()) / 100f);
735 ap.paintAlignment(true, true);
738 class MyCheckbox extends Checkbox
742 public int stringWidth;
748 public void updateColor(FeatureColourI newcol)
751 if (col.isSimpleColour())
753 setBackground(col.getColour());
757 String vlabel = type;
758 if (col.isAboveThreshold())
762 else if (col.isBelowThreshold())
766 if (col.isColourByLabel())
768 setBackground(Color.white);
769 vlabel += " (by Label)";
773 setBackground(col.getMinColour());
775 this.setLabel(vlabel);
780 public MyCheckbox(String label, boolean checked, boolean haslink)
782 super(label, checked);
784 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
785 stringWidth = fm.stringWidth(label);
786 this.hasLink = haslink;
789 public MyCheckbox(String type, boolean selected, boolean b,
790 FeatureColourI featureStyle)
792 this(type, selected, b);
793 updateColor(featureStyle);
797 public void paint(Graphics g)
799 Dimension d = getSize();
802 if (col.isColourByLabel())
804 g.setColor(Color.white);
805 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
807 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
810 * // g.setFont(g.getFont().deriveFont( //
811 * AffineTransform.getScaleInstance( //
812 * width/g.getFontMetrics().stringWidth("Label"), //
813 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
818 else if (col.isGraduatedColour())
820 Color maxCol = col.getMaxColour();
822 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
829 g.drawImage(linkImage, stringWidth + 25,
830 (getSize().height - linkImage.getHeight(this)) / 2, this);
836 * Hide columns containing (or not containing) a given feature type
839 * @param columnsContaining
841 void hideFeatureColumns(final String type, boolean columnsContaining)
843 if (ap.alignFrame.avc.markColumnsContainingFeatures(columnsContaining,
846 if (ap.alignFrame.avc.markColumnsContainingFeatures(
847 !columnsContaining, false, false, type))
849 ap.alignFrame.viewport.hideSelectedColumns();
855 public void mousePressed(MouseEvent e)
857 // TODO Auto-generated method stub