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 implements ItemListener,
67 MouseListener, MouseMotionListener, ActionListener,
68 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(new GridLayout(
139 (fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote - this was
140 // scaled on number of
141 // visible groups. seems
143 groupPanel.validate();
145 add(groupPanel, BorderLayout.NORTH);
149 final FeatureSettings me = this;
150 frame.addWindowListener(new WindowAdapter()
153 public void windowClosing(WindowEvent e)
155 if (me.av.featureSettings == me)
157 me.av.featureSettings = null;
163 int height = featurePanel.getComponentCount() * 50 + 60;
165 height = Math.max(200, height);
166 height = Math.min(400, height);
168 jalview.bin.JalviewLite.addFrame(frame,
169 MessageManager.getString("label.sequence_feature_settings"),
174 public void paint(Graphics g)
176 g.setColor(Color.black);
177 g.drawString(MessageManager
178 .getString("label.no_features_added_to_this_alignment"), 10, 20);
179 g.drawString(MessageManager
180 .getString("label.features_can_be_added_from_searches_1"), 10,
182 g.drawString(MessageManager
183 .getString("label.features_can_be_added_from_searches_2"), 10,
187 protected void popupSort(final MyCheckbox check,
188 final Map<String, float[][]> minmax, int x, int y)
190 final String type = check.type;
191 final FeatureColourI typeCol = fr.getFeatureStyle(type);
192 PopupMenu men = new PopupMenu(MessageManager.formatMessage(
193 "label.settings_for_type", new String[] { type }));
194 java.awt.MenuItem scr = new MenuItem(
195 MessageManager.getString("label.sort_by_score"));
197 final FeatureSettings me = this;
198 scr.addActionListener(new ActionListener()
202 public void actionPerformed(ActionEvent e)
204 me.ap.alignFrame.avc.sortAlignmentByFeatureScore(Arrays
205 .asList(new String[] { type }));
209 MenuItem dens = new MenuItem(
210 MessageManager.getString("label.sort_by_density"));
211 dens.addActionListener(new ActionListener()
215 public void actionPerformed(ActionEvent e)
217 me.ap.alignFrame.avc.sortAlignmentByFeatureDensity(Arrays
218 .asList(new String[] { type }));
226 final float[][] typeMinMax = minmax.get(type);
228 * final java.awt.CheckboxMenuItem chb = new
229 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
230 * moment chb.setState(minmax.get(type) != null);
231 * chb.addActionListener(new ActionListener() {
233 * public void actionPerformed(ActionEvent e) {
234 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
235 * null); } else { minmax.put(type, typeMinMax); } }
239 if (typeMinMax != null && typeMinMax[0] != null)
241 // graduated colourschemes for those where minmax exists for the
242 // positional features
243 MenuItem mxcol = new MenuItem(
244 (typeCol.isSimpleColour()) ? "Graduated Colour"
247 mxcol.addActionListener(new ActionListener()
251 public void actionPerformed(ActionEvent e)
253 if (typeCol.isSimpleColour())
255 new FeatureColourChooser(me, type);
256 // write back the current colour object to update the table
257 check.updateColor(fr.getFeatureStyle(type));
261 new UserDefinedColours(me, check.type, typeCol);
269 MenuItem selectContaining = new MenuItem(
270 MessageManager.getString("label.select_columns_containing"));
271 selectContaining.addActionListener(new ActionListener()
274 public void actionPerformed(ActionEvent e)
276 me.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
280 men.add(selectContaining);
282 MenuItem selectNotContaining = new MenuItem(
283 MessageManager.getString("label.select_columns_not_containing"));
284 selectNotContaining.addActionListener(new ActionListener()
287 public void actionPerformed(ActionEvent e)
289 me.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
293 men.add(selectNotContaining);
295 MenuItem hideContaining = new MenuItem(
296 MessageManager.getString("label.hide_columns_containing"));
297 hideContaining.addActionListener(new ActionListener()
300 public void actionPerformed(ActionEvent e)
302 hideFeatureColumns(type, true);
305 men.add(hideContaining);
307 MenuItem hideNotContaining = new MenuItem(
308 MessageManager.getString("label.hide_columns_not_containing"));
309 hideNotContaining.addActionListener(new ActionListener()
312 public void actionPerformed(ActionEvent e)
314 hideFeatureColumns(type, false);
317 men.add(hideNotContaining);
319 this.featurePanel.add(men);
320 men.show(this.featurePanel, x, y);
324 public void discoverAllFeatureData()
326 if (fr.getAllFeatureColours() != null
327 && fr.getAllFeatureColours().size() > 0)
336 * Answers the visibility of the given group, and adds a checkbox for it if
337 * there is not one already
339 public boolean checkGroupState(String group)
341 boolean visible = fr.checkGroupVisibility(group, true);
344 * is there already a checkbox for this group?
346 for (int g = 0; g < groupPanel.getComponentCount(); g++)
348 if (((Checkbox) groupPanel.getComponent(g)).getLabel().equals(group))
350 ((Checkbox) groupPanel.getComponent(g)).setState(visible);
358 Checkbox check = new MyCheckbox(group, visible, false);
359 check.addMouseListener(this);
360 check.setFont(new Font("Serif", Font.BOLD, 12));
361 check.addItemListener(groupItemListener);
362 groupPanel.add(check);
364 groupPanel.validate();
368 // This routine adds and removes checkboxes depending on
369 // Group selection states
370 void resetTable(boolean groupsChanged)
372 List<String> displayableTypes = new ArrayList<String>();
373 Set<String> foundGroups = new HashSet<String>();
375 AlignmentI alignment = av.getAlignment();
377 for (int i = 0; i < alignment.getHeight(); i++)
379 SequenceI seq = alignment.getSequenceAt(i);
382 * get the sequence's groups for positional features
383 * and keep track of which groups are visible
385 Set<String> groups = seq.getFeatures().getFeatureGroups(true);
386 Set<String> visibleGroups = new HashSet<String>();
387 for (String group : groups)
389 if (group == null || fr.checkGroupVisibility(group, true))
391 visibleGroups.add(group);
396 * get distinct feature types for visible groups
397 * record distinct visible types
399 Set<String> types = seq.getFeatures().getFeatureTypesForGroups(true,
400 visibleGroups.toArray(new String[visibleGroups.size()]));
401 displayableTypes.addAll(types);
405 * remove any checkboxes for groups not present
407 pruneGroups(foundGroups);
410 int cSize = featurePanel.getComponentCount();
412 // This will remove any checkboxes which shouldn't be
414 for (int i = 0; i < cSize; i++)
416 comps = featurePanel.getComponents();
417 check = (MyCheckbox) comps[i];
418 if (!displayableTypes.contains(check.type))
420 featurePanel.remove(i);
426 if (fr.getRenderOrder() != null)
428 // First add the checks in the previous render order,
429 // in case the window has been closed and reopened
430 List<String> rol = fr.getRenderOrder();
431 for (int ro = rol.size() - 1; ro > -1; ro--)
433 String item = rol.get(ro);
435 if (!displayableTypes.contains(item))
440 displayableTypes.remove(item);
442 addCheck(false, item);
447 * now add checkboxes which should be visible,
448 * if they have not already been added
450 for (String type : displayableTypes)
452 addCheck(groupsChanged, type);
455 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
457 featurePanel.validate();
459 if (scrollPane != null)
461 scrollPane.validate();
464 itemStateChanged(null);
468 * Remove from the groups panel any checkboxes for groups that are not in the
469 * foundGroups set. This enables removing a group from the display when the
470 * last feature in that group is deleted.
474 protected void pruneGroups(Set<String> foundGroups)
476 for (int g = 0; g < groupPanel.getComponentCount(); g++)
478 Checkbox checkbox = (Checkbox) groupPanel.getComponent(g);
479 if (!foundGroups.contains(checkbox.getLabel()))
481 groupPanel.remove(checkbox);
487 * update the checklist of feature types with the given type
489 * @param groupsChanged
490 * true means if the type is not in the display list then it will be
491 * added and displayed
493 * feature type to be checked for in the list.
495 void addCheck(boolean groupsChanged, String type)
498 Component[] comps = featurePanel.getComponents();
501 for (int i = 0; i < featurePanel.getComponentCount(); i++)
503 check = (MyCheckbox) comps[i];
504 if (check.type.equals(type))
513 boolean selected = false;
514 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
519 check = new MyCheckbox(type, selected, false,
520 fr.getFeatureStyle(type));
522 check.addMouseListener(this);
523 check.addMouseMotionListener(this);
524 check.addItemListener(this);
527 // add at beginning of stack.
528 featurePanel.add(check, 0);
532 // add at end of stack.
533 featurePanel.add(check);
539 public void actionPerformed(ActionEvent evt)
541 for (int i = 0; i < featurePanel.getComponentCount(); i++)
543 Checkbox check = (Checkbox) featurePanel.getComponent(i);
544 check.setState(!check.getState());
549 private ItemListener groupItemListener = new ItemListener()
552 public void itemStateChanged(ItemEvent evt)
554 Checkbox source = (Checkbox) evt.getSource();
555 fr.setGroupVisibility(source.getLabel(), source.getState());
556 ap.seqPanel.seqCanvas.repaint();
557 if (ap.overviewPanel != null)
559 ap.overviewPanel.updateOverviewImage();
567 public void itemStateChanged(ItemEvent evt)
572 void selectionChanged()
574 Component[] comps = featurePanel.getComponents();
575 int cSize = comps.length;
577 Object[][] tmp = new Object[cSize][3];
579 for (int i = 0; i < cSize; i++)
581 MyCheckbox check = (MyCheckbox) comps[i];
582 tmp[tmpSize][0] = check.type;
583 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
584 tmp[tmpSize][2] = new Boolean(check.getState());
588 Object[][] data = new Object[tmpSize][3];
589 System.arraycopy(tmp, 0, data, 0, tmpSize);
591 fr.setFeaturePriority(data);
593 ap.paintAlignment(true);
596 MyCheckbox selectedCheck;
598 boolean dragging = false;
601 public void mouseDragged(MouseEvent evt)
603 if (((Component) evt.getSource()).getParent() != featurePanel)
611 public void mouseReleased(MouseEvent evt)
613 if (((Component) evt.getSource()).getParent() != featurePanel)
618 Component comp = null;
619 Checkbox target = null;
621 int height = evt.getY() + evt.getComponent().getLocation().y;
623 if (height > featurePanel.getSize().height)
627 .getComponent(featurePanel.getComponentCount() - 1);
631 comp = featurePanel.getComponent(0);
635 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
636 + evt.getComponent().getLocation().y);
639 if (comp != null && comp instanceof Checkbox)
641 target = (Checkbox) comp;
644 if (selectedCheck != null && target != null && selectedCheck != target)
646 int targetIndex = -1;
647 for (int i = 0; i < featurePanel.getComponentCount(); i++)
649 if (target == featurePanel.getComponent(i))
656 featurePanel.remove(selectedCheck);
657 featurePanel.add(selectedCheck, targetIndex);
658 featurePanel.validate();
659 itemStateChanged(null);
663 public void setUserColour(String feature, FeatureColourI originalColour)
665 fr.setColour(feature, originalColour);
669 public void refreshTable()
671 featurePanel.removeAll();
673 ap.paintAlignment(true);
677 public void mouseEntered(MouseEvent evt)
682 public void mouseExited(MouseEvent evt)
687 public void mouseClicked(MouseEvent evt)
689 MyCheckbox check = (MyCheckbox) evt.getSource();
690 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
692 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
695 if (check.getParent() != featurePanel)
700 if (evt.getClickCount() > 1)
702 FeatureColourI fcol = fr.getFeatureStyle(check.type);
703 if (fcol.isSimpleColour())
705 new UserDefinedColours(this, check.type, fcol.getColour());
709 new FeatureColourChooser(this, check.type);
710 // write back the current colour object to update the table
711 check.updateColor(fr.getFeatureStyle(check.type));
717 public void mouseMoved(MouseEvent evt)
722 public void adjustmentValueChanged(AdjustmentEvent evt)
724 fr.setTransparency((100 - transparency.getValue()) / 100f);
725 ap.paintAlignment(true);
728 class MyCheckbox extends Checkbox
732 public int stringWidth;
738 public void updateColor(FeatureColourI newcol)
741 if (col.isSimpleColour())
743 setBackground(col.getColour());
747 String vlabel = type;
748 if (col.isAboveThreshold())
752 else if (col.isBelowThreshold())
756 if (col.isColourByLabel())
758 setBackground(Color.white);
759 vlabel += " (by Label)";
763 setBackground(col.getMinColour());
765 this.setLabel(vlabel);
770 public MyCheckbox(String label, boolean checked, boolean haslink)
772 super(label, checked);
774 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
775 stringWidth = fm.stringWidth(label);
776 this.hasLink = haslink;
779 public MyCheckbox(String type, boolean selected, boolean b,
780 FeatureColourI featureStyle)
782 this(type, selected, b);
783 updateColor(featureStyle);
787 public void paint(Graphics g)
789 Dimension d = getSize();
792 if (col.isColourByLabel())
794 g.setColor(Color.white);
795 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
797 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
800 * // g.setFont(g.getFont().deriveFont( //
801 * AffineTransform.getScaleInstance( //
802 * width/g.getFontMetrics().stringWidth("Label"), //
803 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
808 else if (col.isGraduatedColour())
810 Color maxCol = col.getMaxColour();
812 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
819 g.drawImage(linkImage, stringWidth + 25,
820 (getSize().height - linkImage.getHeight(this)) / 2, this);
826 * Hide columns containing (or not containing) a given feature type
829 * @param columnsContaining
831 void hideFeatureColumns(final String type, boolean columnsContaining)
833 if (ap.alignFrame.avc.markColumnsContainingFeatures(columnsContaining,
836 if (ap.alignFrame.avc.markColumnsContainingFeatures(
837 !columnsContaining, false, false, type))
839 ap.alignFrame.viewport.hideSelectedColumns();
845 public void mousePressed(MouseEvent e)
847 // TODO Auto-generated method stub