2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.analysis.AlignmentSorter;
27 import jalview.commands.OrderCommand;
28 import jalview.datamodel.*;
29 import jalview.schemes.AnnotationColourGradient;
30 import jalview.schemes.GraduatedColor;
31 import jalview.util.MessageManager;
33 public class FeatureSettings extends Panel implements ItemListener,
34 MouseListener, MouseMotionListener, ActionListener,
47 Panel featurePanel = new Panel();
49 ScrollPane scrollPane;
51 boolean alignmentHasFeatures = false;
55 Scrollbar transparency;
57 public FeatureSettings(final AlignmentPanel ap)
61 ap.av.featureSettings = this;
62 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
64 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
65 100 - (int) (fr.transparency * 100), 1, 1, 100);
67 if (fr.transparencySetter != null)
69 transparency.addAdjustmentListener(this);
73 transparency.setEnabled(false);
76 java.net.URL url = getClass().getResource("/images/link.gif");
79 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
82 if (av.featuresDisplayed == null)
89 this.setLayout(new BorderLayout());
90 scrollPane = new ScrollPane();
91 scrollPane.add(featurePanel);
92 if (alignmentHasFeatures)
94 add(scrollPane, BorderLayout.CENTER);
97 Button invert = new Button("Invert Selection");
98 invert.addActionListener(this);
100 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
101 lowerPanel.add(invert);
103 Panel tPanel = new Panel(new BorderLayout());
105 if (fr.transparencySetter != null)
107 tPanel.add(transparency, BorderLayout.CENTER);
108 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
113 new Label("Transparency not available in this web browser"),
114 BorderLayout.CENTER);
117 lowerPanel.add(tPanel, BorderLayout.SOUTH);
119 add(lowerPanel, BorderLayout.SOUTH);
121 if (groupPanel != null)
124 .setLayout(new GridLayout(
125 (fr.featureGroups.size() - fr.hiddenGroups.size()) / 4 + 1,
127 groupPanel.validate();
129 add(groupPanel, BorderLayout.NORTH);
133 final FeatureSettings me = this;
134 frame.addWindowListener(new WindowAdapter()
136 public void windowClosing(WindowEvent e)
138 if (me.av.featureSettings == me)
140 me.av.featureSettings = null;
146 int height = featurePanel.getComponentCount() * 50 + 60;
148 height = Math.max(200, height);
149 height = Math.min(400, height);
151 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("label.feature_settings"), width,
155 public void paint(Graphics g)
157 g.setColor(Color.black);
158 g.drawString(MessageManager.getString("label.no_features_added_to_this_alignment"), 10, 20);
159 g.drawString(MessageManager.getString("label.features_can_be_added_from_searches_1"), 10, 40);
160 g.drawString(MessageManager.getString("label.features_can_be_added_from_searches_2"), 10, 60);
163 protected void popupSort(final MyCheckbox check, final Hashtable minmax,
166 final String type = check.type;
167 final Object typeCol = fr.getFeatureStyle(type);
168 java.awt.PopupMenu men = new PopupMenu(MessageManager.formatMessage("label.settings_for_type", new String[]{type}));
169 java.awt.MenuItem scr = new MenuItem(MessageManager.getString("label.sort_by_score"));
171 final FeatureSettings me = this;
172 scr.addActionListener(new ActionListener()
175 public void actionPerformed(ActionEvent e)
177 me.sortByScore(new String[]
182 MenuItem dens = new MenuItem(MessageManager.getString("label.sort_by_density"));
183 dens.addActionListener(new ActionListener()
186 public void actionPerformed(ActionEvent e)
188 me.sortByDens(new String[]
196 final Object typeMinMax = minmax.get(type);
198 * final java.awt.CheckboxMenuItem chb = new
199 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
200 * moment chb.setState(minmax.get(type) != null);
201 * chb.addActionListener(new ActionListener() {
203 * public void actionPerformed(ActionEvent e) {
204 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
205 * null); } else { minmax.put(type, typeMinMax); } }
209 if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null)
211 // graduated colourschemes for those where minmax exists for the
212 // positional features
213 MenuItem mxcol = new MenuItem(
214 (typeCol instanceof Color) ? "Graduated Colour"
217 mxcol.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 if (typeCol instanceof Color)
224 new FeatureColourChooser(me, type);
225 // write back the current colour object to update the table
226 check.updateColor(fr.getFeatureStyle(type));
230 new UserDefinedColours(me, check.type,
231 ((GraduatedColor) typeCol));
238 this.featurePanel.add(men);
239 men.show(this.featurePanel, x, y);
242 public void setTableData()
244 alignmentHasFeatures = fr.buildGroupHash();
245 if (alignmentHasFeatures)
254 * rebuilds the group panel
256 public void rebuildGroups()
258 boolean rdrw = false;
259 if (groupPanel == null)
261 groupPanel = new Panel();
266 groupPanel.removeAll();
268 // TODO: JAL-964 - smoothly incorporate new group entries if panel already
269 // displayed and new groups present
270 Enumeration gps = fr.featureGroups.keys();
271 while (gps.hasMoreElements())
273 String group = (String) gps.nextElement();
274 Boolean vis = (Boolean) fr.featureGroups.get(group);
275 Checkbox check = new MyCheckbox(group, vis.booleanValue(),
276 (fr.featureLinks != null && fr.featureLinks
277 .containsKey(group)));
278 check.addMouseListener(this);
279 check.setFont(new Font("Serif", Font.BOLD, 12));
280 check.addItemListener(this);
281 check.setVisible(fr.hiddenGroups.contains(group));
282 groupPanel.add(check);
286 groupPanel.validate();
290 // This routine adds and removes checkboxes depending on
291 // Group selection states
292 void resetTable(boolean groupsChanged)
294 SequenceFeature[] tmpfeatures;
295 String group = null, type;
296 Vector visibleChecks = new Vector();
297 AlignmentI alignment = av.getAlignment();
298 for (int i = 0; i < alignment.getHeight(); i++)
300 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
305 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
307 while (index < tmpfeatures.length)
309 group = tmpfeatures[index].featureGroup;
311 if (group == null || fr.featureGroups.get(group) == null
312 || ((Boolean) fr.featureGroups.get(group)).booleanValue())
314 type = tmpfeatures[index].getType();
315 if (!visibleChecks.contains(type))
317 visibleChecks.addElement(type);
325 int cSize = featurePanel.getComponentCount();
327 // This will remove any checkboxes which shouldn't be
329 for (int i = 0; i < cSize; i++)
331 comps = featurePanel.getComponents();
332 check = (MyCheckbox) comps[i];
333 if (!visibleChecks.contains(check.type))
335 featurePanel.remove(i);
341 if (fr.renderOrder != null)
343 // First add the checks in the previous render order,
344 // in case the window has been closed and reopened
345 for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
347 String item = fr.renderOrder[ro];
349 if (!visibleChecks.contains(item))
354 visibleChecks.removeElement(item);
356 addCheck(false, item);
360 // now add checkboxes which should be visible,
361 // if they have not already been added
362 Enumeration en = visibleChecks.elements();
364 while (en.hasMoreElements())
366 addCheck(groupsChanged, en.nextElement().toString());
369 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
371 featurePanel.validate();
373 if (scrollPane != null)
375 scrollPane.validate();
378 itemStateChanged(null);
382 * update the checklist of feature types with the given type
384 * @param groupsChanged
385 * true means if the type is not in the display list then it will be
386 * added and displayed
388 * feature type to be checked for in the list.
390 void addCheck(boolean groupsChanged, String type)
393 Component[] comps = featurePanel.getComponents();
396 for (int i = 0; i < featurePanel.getComponentCount(); i++)
398 check = (MyCheckbox) comps[i];
399 if (check.type.equals(type))
408 boolean selected = false;
409 if (groupsChanged || av.featuresDisplayed.containsKey(type))
414 check = new MyCheckbox(
417 (fr.featureLinks != null && fr.featureLinks.containsKey(type)),
418 fr.getFeatureStyle(type));
420 check.addMouseListener(this);
421 check.addMouseMotionListener(this);
422 check.addItemListener(this);
425 // add at beginning of stack.
426 featurePanel.add(check, 0);
430 // add at end of stack.
431 featurePanel.add(check);
436 public void actionPerformed(ActionEvent evt)
438 for (int i = 0; i < featurePanel.getComponentCount(); i++)
440 Checkbox check = (Checkbox) featurePanel.getComponent(i);
441 check.setState(!check.getState());
446 public void itemStateChanged(ItemEvent evt)
450 // Is the source a top level featureGroup?
451 Checkbox source = (Checkbox) evt.getSource();
452 if (fr.featureGroups.containsKey(source.getLabel()))
454 fr.featureGroups.put(source.getLabel(),
455 new Boolean(source.getState()));
456 ap.seqPanel.seqCanvas.repaint();
457 if (ap.overviewPanel != null)
459 ap.overviewPanel.updateOverviewImage();
469 void selectionChanged()
471 Component[] comps = featurePanel.getComponents();
472 int cSize = comps.length;
474 Object[][] tmp = new Object[cSize][3];
476 for (int i = 0; i < cSize; i++)
478 MyCheckbox check = (MyCheckbox) comps[i];
479 tmp[tmpSize][0] = check.type;
480 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
481 tmp[tmpSize][2] = new Boolean(check.getState());
485 Object[][] data = new Object[tmpSize][3];
486 System.arraycopy(tmp, 0, data, 0, tmpSize);
488 fr.setFeaturePriority(data);
490 ap.paintAlignment(true);
493 MyCheckbox selectedCheck;
495 boolean dragging = false;
497 public void mousePressed(MouseEvent evt)
500 selectedCheck = (MyCheckbox) evt.getSource();
502 if (fr.featureLinks != null
503 && fr.featureLinks.containsKey(selectedCheck.type))
505 if (evt.getX() > selectedCheck.stringWidth + 20)
513 public void mouseDragged(MouseEvent evt)
515 if (((Component) evt.getSource()).getParent() != featurePanel)
522 public void mouseReleased(MouseEvent evt)
524 if (((Component) evt.getSource()).getParent() != featurePanel)
529 Component comp = null;
530 Checkbox target = null;
532 int height = evt.getY() + evt.getComponent().getLocation().y;
534 if (height > featurePanel.getSize().height)
538 .getComponent(featurePanel.getComponentCount() - 1);
542 comp = featurePanel.getComponent(0);
546 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
547 + evt.getComponent().getLocation().y);
550 if (comp != null && comp instanceof Checkbox)
552 target = (Checkbox) comp;
555 if (selectedCheck != null && target != null && selectedCheck != target)
557 int targetIndex = -1;
558 for (int i = 0; i < featurePanel.getComponentCount(); i++)
560 if (target == featurePanel.getComponent(i))
567 featurePanel.remove(selectedCheck);
568 featurePanel.add(selectedCheck, targetIndex);
569 featurePanel.validate();
570 itemStateChanged(null);
574 public void setUserColour(String feature, Object originalColour)
576 if (originalColour instanceof Color
577 || originalColour instanceof GraduatedColor)
579 fr.setColour(feature, originalColour);
584 "Implementation error: Unsupported feature colour object.");
589 public void refreshTable()
591 featurePanel.removeAll();
593 ap.paintAlignment(true);
596 public void mouseEntered(MouseEvent evt)
600 public void mouseExited(MouseEvent evt)
604 public void mouseClicked(MouseEvent evt)
606 MyCheckbox check = (MyCheckbox) evt.getSource();
607 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
609 this.popupSort(check, fr.minmax, evt.getX(), evt.getY());
611 if (fr.featureLinks != null && fr.featureLinks.containsKey(check.type))
613 if (evt.getX() > check.stringWidth + 20)
616 String link = fr.featureLinks.get(check.type).toString();
617 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
618 link.substring(0, link.indexOf("|")));
622 if (check.getParent() != featurePanel)
627 if (evt.getClickCount() > 1)
629 Object fcol = fr.getFeatureStyle(check.type);
630 if (fcol instanceof Color)
632 new UserDefinedColours(this, check.type, (Color) fcol);
636 new FeatureColourChooser(this, check.type);
637 // write back the current colour object to update the table
638 check.updateColor(fr.getFeatureStyle(check.type));
643 public void mouseMoved(MouseEvent evt)
647 public void adjustmentValueChanged(AdjustmentEvent evt)
649 fr.transparency = ((float) (100 - transparency.getValue()) / 100f);
650 ap.seqPanel.seqCanvas.repaint();
654 class MyCheckbox extends Checkbox
658 public int stringWidth;
666 public void updateColor(Object newcol)
668 if (newcol instanceof Color)
670 col = (Color) newcol;
673 else if (newcol instanceof GraduatedColor)
675 gcol = (GraduatedColor) newcol;
680 throw new Error("Invalid color for MyCheckBox");
688 String vlabel = type;
689 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
692 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
695 if (gcol.isColourByLabel())
697 setBackground(Color.white);
698 vlabel += " (by Label)";
702 setBackground(gcol.getMinColor());
704 this.setLabel(vlabel);
709 public MyCheckbox(String label, boolean checked, boolean haslink)
711 super(label, checked);
713 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
714 stringWidth = fm.stringWidth(label);
715 this.hasLink = haslink;
718 public MyCheckbox(String type, boolean selected, boolean b,
721 this(type, selected, b);
722 updateColor(featureStyle);
725 public void paint(Graphics g)
727 Dimension d = getSize();
730 if (gcol.isColourByLabel())
732 g.setColor(Color.white);
733 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
735 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
738 * // g.setFont(g.getFont().deriveFont( //
739 * AffineTransform.getScaleInstance( //
740 * width/g.getFontMetrics().stringWidth("Label"), //
741 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
748 Color maxCol = gcol.getMaxColor();
750 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
757 g.drawImage(linkImage, stringWidth + 25,
758 (getSize().height - linkImage.getHeight(this)) / 2, this);
763 protected void sortByDens(String[] typ)
765 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
768 private String[] getDisplayedFeatureTypes()
773 synchronized (fr.renderOrder)
775 typ = new String[fr.renderOrder.length];
776 System.arraycopy(fr.renderOrder, 0, typ, 0, typ.length);
777 for (int i = 0; i < typ.length; i++)
779 if (av.featuresDisplayed.get(typ[i]) == null)
789 protected void sortBy(String[] typ, String methodText, final String method)
793 typ = getDisplayedFeatureTypes();
796 gps = fr.getGroups(true);
799 for (int i = 0; i < typ.length; i++)
801 System.err.println("Sorting on Types:" + typ[i]);
807 for (int i = 0; i < gps.length; i++)
809 System.err.println("Sorting on groups:" + gps[i]);
812 AlignmentPanel alignPanel = ap;
813 AlignmentI al = alignPanel.av.getAlignment();
816 SequenceGroup sg = alignPanel.av.getSelectionGroup();
819 start = sg.getStartRes();
820 stop = sg.getEndRes();
825 stop = al.getWidth();
827 SequenceI[] oldOrder = al.getSequencesArray();
828 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
829 this.ap.alignFrame.addHistoryItem(new OrderCommand(methodText,
830 oldOrder, alignPanel.av.getAlignment()));
831 alignPanel.paintAlignment(true);
835 protected void sortByScore(String[] typ)
837 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);