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.SequenceFeature;
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.Arrays;
60 import java.util.Enumeration;
61 import java.util.HashSet;
62 import java.util.List;
65 import java.util.Vector;
67 public class FeatureSettings extends Panel implements ItemListener,
68 MouseListener, MouseMotionListener, ActionListener,
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("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(new GridLayout(
140 (fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote - this was
141 // scaled on number of
142 // visible groups. seems
144 groupPanel.validate();
146 add(groupPanel, BorderLayout.NORTH);
150 final FeatureSettings me = this;
151 frame.addWindowListener(new WindowAdapter()
154 public void windowClosing(WindowEvent e)
156 if (me.av.featureSettings == me)
158 me.av.featureSettings = null;
164 int height = featurePanel.getComponentCount() * 50 + 60;
166 height = Math.max(200, height);
167 height = Math.min(400, height);
169 jalview.bin.JalviewLite.addFrame(frame,
170 MessageManager.getString("label.sequence_feature_settings"),
175 public void paint(Graphics g)
177 g.setColor(Color.black);
178 g.drawString(MessageManager
179 .getString("label.no_features_added_to_this_alignment"), 10, 20);
180 g.drawString(MessageManager
181 .getString("label.features_can_be_added_from_searches_1"), 10,
183 g.drawString(MessageManager
184 .getString("label.features_can_be_added_from_searches_2"), 10,
188 protected void popupSort(final MyCheckbox check,
189 final Map<String, float[][]> minmax, int x, int y)
191 final String type = check.type;
192 final FeatureColourI typeCol = fr.getFeatureStyle(type);
193 PopupMenu men = new PopupMenu(MessageManager.formatMessage(
194 "label.settings_for_type", new String[] { type }));
195 java.awt.MenuItem scr = new MenuItem(
196 MessageManager.getString("label.sort_by_score"));
198 final FeatureSettings me = this;
199 scr.addActionListener(new ActionListener()
203 public void actionPerformed(ActionEvent e)
205 me.ap.alignFrame.avc.sortAlignmentByFeatureScore(Arrays
206 .asList(new String[] { type }));
210 MenuItem dens = new MenuItem(
211 MessageManager.getString("label.sort_by_density"));
212 dens.addActionListener(new ActionListener()
216 public void actionPerformed(ActionEvent e)
218 me.ap.alignFrame.avc.sortAlignmentByFeatureDensity(Arrays
219 .asList(new String[] { type }));
227 final float[][] typeMinMax = minmax.get(type);
229 * final java.awt.CheckboxMenuItem chb = new
230 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
231 * moment chb.setState(minmax.get(type) != null);
232 * chb.addActionListener(new ActionListener() {
234 * public void actionPerformed(ActionEvent e) {
235 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
236 * null); } else { minmax.put(type, typeMinMax); } }
240 if (typeMinMax != null && typeMinMax[0] != null)
242 // graduated colourschemes for those where minmax exists for the
243 // positional features
244 MenuItem mxcol = new MenuItem(
245 (typeCol.isSimpleColour()) ? "Graduated Colour"
248 mxcol.addActionListener(new ActionListener()
252 public void actionPerformed(ActionEvent e)
254 if (typeCol.isSimpleColour())
256 new FeatureColourChooser(me, type);
257 // write back the current colour object to update the table
258 check.updateColor(fr.getFeatureStyle(type));
262 new UserDefinedColours(me, check.type, typeCol);
270 MenuItem selectContaining = new MenuItem(
271 MessageManager.getString("label.select_columns_containing"));
272 selectContaining.addActionListener(new ActionListener()
275 public void actionPerformed(ActionEvent e)
277 me.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
281 men.add(selectContaining);
283 MenuItem selectNotContaining = new MenuItem(
284 MessageManager.getString("label.select_columns_not_containing"));
285 selectNotContaining.addActionListener(new ActionListener()
288 public void actionPerformed(ActionEvent e)
290 me.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
294 men.add(selectNotContaining);
296 MenuItem hideContaining = new MenuItem(
297 MessageManager.getString("label.hide_columns_containing"));
298 hideContaining.addActionListener(new ActionListener()
301 public void actionPerformed(ActionEvent e)
303 hideFeatureColumns(type, true);
306 men.add(hideContaining);
308 MenuItem hideNotContaining = new MenuItem(
309 MessageManager.getString("label.hide_columns_not_containing"));
310 hideNotContaining.addActionListener(new ActionListener()
313 public void actionPerformed(ActionEvent e)
315 hideFeatureColumns(type, false);
318 men.add(hideNotContaining);
320 this.featurePanel.add(men);
321 men.show(this.featurePanel, x, y);
325 public void discoverAllFeatureData()
327 if (fr.getAllFeatureColours() != null
328 && fr.getAllFeatureColours().size() > 0)
337 * Answers the visibility of the given group, and adds a checkbox for it if
338 * there is not one already
340 public boolean checkGroupState(String group)
342 boolean visible = fr.checkGroupVisibility(group, true);
345 * is there already a checkbox for this group?
347 for (int g = 0; g < groupPanel.getComponentCount(); g++)
349 if (((Checkbox) groupPanel.getComponent(g)).getLabel().equals(group))
351 ((Checkbox) groupPanel.getComponent(g)).setState(visible);
359 Checkbox check = new MyCheckbox(group, visible, false);
360 check.addMouseListener(this);
361 check.setFont(new Font("Serif", Font.BOLD, 12));
362 check.addItemListener(groupItemListener);
363 groupPanel.add(check);
365 groupPanel.validate();
369 // This routine adds and removes checkboxes depending on
370 // Group selection states
371 void resetTable(boolean groupsChanged)
373 SequenceFeature[] tmpfeatures;
374 String group = null, type;
375 Vector<String> visibleChecks = new Vector<String>();
376 Set<String> foundGroups = new HashSet<String>();
377 AlignmentI alignment = av.getAlignment();
379 for (int i = 0; i < alignment.getHeight(); i++)
381 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
386 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
388 while (index < tmpfeatures.length)
390 group = tmpfeatures[index].featureGroup;
391 foundGroups.add(group);
393 if (group == null || checkGroupState(group))
395 type = tmpfeatures[index].getType();
396 if (!visibleChecks.contains(type))
398 visibleChecks.addElement(type);
406 * remove any checkboxes for groups not present
408 pruneGroups(foundGroups);
411 int cSize = featurePanel.getComponentCount();
413 // This will remove any checkboxes which shouldn't be
415 for (int i = 0; i < cSize; i++)
417 comps = featurePanel.getComponents();
418 check = (MyCheckbox) comps[i];
419 if (!visibleChecks.contains(check.type))
421 featurePanel.remove(i);
427 if (fr.getRenderOrder() != null)
429 // First add the checks in the previous render order,
430 // in case the window has been closed and reopened
431 List<String> rol = fr.getRenderOrder();
432 for (int ro = rol.size() - 1; ro > -1; ro--)
434 String item = rol.get(ro);
436 if (!visibleChecks.contains(item))
441 visibleChecks.removeElement(item);
443 addCheck(false, item);
447 // now add checkboxes which should be visible,
448 // if they have not already been added
449 Enumeration<String> en = visibleChecks.elements();
451 while (en.hasMoreElements())
453 addCheck(groupsChanged, en.nextElement().toString());
456 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
458 featurePanel.validate();
460 if (scrollPane != null)
462 scrollPane.validate();
465 itemStateChanged(null);
469 * Remove from the groups panel any checkboxes for groups that are not in the
470 * foundGroups set. This enables removing a group from the display when the
471 * last feature in that group is deleted.
475 protected void pruneGroups(Set<String> foundGroups)
477 for (int g = 0; g < groupPanel.getComponentCount(); g++)
479 Checkbox checkbox = (Checkbox) groupPanel.getComponent(g);
480 if (!foundGroups.contains(checkbox.getLabel()))
482 groupPanel.remove(checkbox);
488 * update the checklist of feature types with the given type
490 * @param groupsChanged
491 * true means if the type is not in the display list then it will be
492 * added and displayed
494 * feature type to be checked for in the list.
496 void addCheck(boolean groupsChanged, String type)
499 Component[] comps = featurePanel.getComponents();
502 for (int i = 0; i < featurePanel.getComponentCount(); i++)
504 check = (MyCheckbox) comps[i];
505 if (check.type.equals(type))
514 boolean selected = false;
515 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
520 check = new MyCheckbox(type, selected, false,
521 fr.getFeatureStyle(type));
523 check.addMouseListener(this);
524 check.addMouseMotionListener(this);
525 check.addItemListener(this);
528 // add at beginning of stack.
529 featurePanel.add(check, 0);
533 // add at end of stack.
534 featurePanel.add(check);
540 public void actionPerformed(ActionEvent evt)
542 for (int i = 0; i < featurePanel.getComponentCount(); i++)
544 Checkbox check = (Checkbox) featurePanel.getComponent(i);
545 check.setState(!check.getState());
550 private ItemListener groupItemListener = new ItemListener()
553 public void itemStateChanged(ItemEvent evt)
555 Checkbox source = (Checkbox) evt.getSource();
556 fr.setGroupVisibility(source.getLabel(), source.getState());
557 ap.seqPanel.seqCanvas.repaint();
558 if (ap.overviewPanel != null)
560 ap.overviewPanel.updateOverviewImage();
568 public void itemStateChanged(ItemEvent evt)
573 void selectionChanged()
575 Component[] comps = featurePanel.getComponents();
576 int cSize = comps.length;
578 Object[][] tmp = new Object[cSize][3];
580 for (int i = 0; i < cSize; i++)
582 MyCheckbox check = (MyCheckbox) comps[i];
583 tmp[tmpSize][0] = check.type;
584 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
585 tmp[tmpSize][2] = new Boolean(check.getState());
589 Object[][] data = new Object[tmpSize][3];
590 System.arraycopy(tmp, 0, data, 0, tmpSize);
592 fr.setFeaturePriority(data);
594 ap.paintAlignment(true);
597 MyCheckbox selectedCheck;
599 boolean dragging = false;
602 public void mouseDragged(MouseEvent evt)
604 if (((Component) evt.getSource()).getParent() != featurePanel)
612 public void mouseReleased(MouseEvent evt)
614 if (((Component) evt.getSource()).getParent() != featurePanel)
619 Component comp = null;
620 Checkbox target = null;
622 int height = evt.getY() + evt.getComponent().getLocation().y;
624 if (height > featurePanel.getSize().height)
628 .getComponent(featurePanel.getComponentCount() - 1);
632 comp = featurePanel.getComponent(0);
636 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
637 + evt.getComponent().getLocation().y);
640 if (comp != null && comp instanceof Checkbox)
642 target = (Checkbox) comp;
645 if (selectedCheck != null && target != null && selectedCheck != target)
647 int targetIndex = -1;
648 for (int i = 0; i < featurePanel.getComponentCount(); i++)
650 if (target == featurePanel.getComponent(i))
657 featurePanel.remove(selectedCheck);
658 featurePanel.add(selectedCheck, targetIndex);
659 featurePanel.validate();
660 itemStateChanged(null);
664 public void setUserColour(String feature, FeatureColourI originalColour)
666 fr.setColour(feature, originalColour);
670 public void refreshTable()
672 featurePanel.removeAll();
674 ap.paintAlignment(true);
678 public void mouseEntered(MouseEvent evt)
683 public void mouseExited(MouseEvent evt)
688 public void mouseClicked(MouseEvent evt)
690 MyCheckbox check = (MyCheckbox) evt.getSource();
691 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
693 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
696 if (check.getParent() != featurePanel)
701 if (evt.getClickCount() > 1)
703 FeatureColourI fcol = fr.getFeatureStyle(check.type);
704 if (fcol.isSimpleColour())
706 new UserDefinedColours(this, check.type, fcol.getColour());
710 new FeatureColourChooser(this, check.type);
711 // write back the current colour object to update the table
712 check.updateColor(fr.getFeatureStyle(check.type));
718 public void mouseMoved(MouseEvent evt)
723 public void adjustmentValueChanged(AdjustmentEvent evt)
725 fr.setTransparency((100 - transparency.getValue()) / 100f);
726 ap.paintAlignment(true);
729 class MyCheckbox extends Checkbox
733 public int stringWidth;
739 public void updateColor(FeatureColourI newcol)
742 if (col.isSimpleColour())
744 setBackground(col.getColour());
748 String vlabel = type;
749 if (col.isAboveThreshold())
753 else if (col.isBelowThreshold())
757 if (col.isColourByLabel())
759 setBackground(Color.white);
760 vlabel += " (by Label)";
764 setBackground(col.getMinColour());
766 this.setLabel(vlabel);
771 public MyCheckbox(String label, boolean checked, boolean haslink)
773 super(label, checked);
775 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
776 stringWidth = fm.stringWidth(label);
777 this.hasLink = haslink;
780 public MyCheckbox(String type, boolean selected, boolean b,
781 FeatureColourI featureStyle)
783 this(type, selected, b);
784 updateColor(featureStyle);
788 public void paint(Graphics g)
790 Dimension d = getSize();
793 if (col.isColourByLabel())
795 g.setColor(Color.white);
796 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
798 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
801 * // g.setFont(g.getFont().deriveFont( //
802 * AffineTransform.getScaleInstance( //
803 * width/g.getFontMetrics().stringWidth("Label"), //
804 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
809 else if (col.isGraduatedColour())
811 Color maxCol = col.getMaxColour();
813 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
820 g.drawImage(linkImage, stringWidth + 25,
821 (getSize().height - linkImage.getHeight(this)) / 2, this);
827 * Hide columns containing (or not containing) a given feature type
830 * @param columnsContaining
832 void hideFeatureColumns(final String type, boolean columnsContaining)
834 if (ap.alignFrame.avc.markColumnsContainingFeatures(columnsContaining,
837 if (ap.alignFrame.avc.markColumnsContainingFeatures(
838 !columnsContaining, false, false, type))
840 ap.alignFrame.viewport.hideSelectedColumns();
846 public void mousePressed(MouseEvent e)
848 // TODO Auto-generated method stub