2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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;
26 import java.awt.event.*;
28 import jalview.analysis.AlignmentSorter;
29 import jalview.commands.OrderCommand;
30 import jalview.datamodel.*;
31 import jalview.schemes.AnnotationColourGradient;
32 import jalview.schemes.GraduatedColor;
33 import jalview.util.MessageManager;
35 public class FeatureSettings extends Panel implements ItemListener,
36 MouseListener, MouseMotionListener, ActionListener,
49 Panel featurePanel = new Panel();
51 ScrollPane scrollPane;
53 boolean alignmentHasFeatures = false;
57 Scrollbar transparency;
59 public FeatureSettings(final AlignmentPanel ap)
63 ap.av.featureSettings = this;
64 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
66 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
67 100 - (int) (fr.transparency * 100), 1, 1, 100);
69 if (fr.transparencySetter != null)
71 transparency.addAdjustmentListener(this);
75 transparency.setEnabled(false);
78 java.net.URL url = getClass().getResource("/images/link.gif");
81 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
84 if (av.featuresDisplayed == null)
91 this.setLayout(new BorderLayout());
92 scrollPane = new ScrollPane();
93 scrollPane.add(featurePanel);
94 if (alignmentHasFeatures)
96 add(scrollPane, BorderLayout.CENTER);
99 Button invert = new Button("Invert Selection");
100 invert.addActionListener(this);
102 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
103 lowerPanel.add(invert);
105 Panel tPanel = new Panel(new BorderLayout());
107 if (fr.transparencySetter != null)
109 tPanel.add(transparency, BorderLayout.CENTER);
110 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
115 new Label("Transparency not available in this web browser"),
116 BorderLayout.CENTER);
119 lowerPanel.add(tPanel, BorderLayout.SOUTH);
121 add(lowerPanel, BorderLayout.SOUTH);
123 if (groupPanel != null)
126 .setLayout(new GridLayout(
127 (fr.featureGroups.size() - fr.hiddenGroups.size()) / 4 + 1,
129 groupPanel.validate();
131 add(groupPanel, BorderLayout.NORTH);
135 final FeatureSettings me = this;
136 frame.addWindowListener(new WindowAdapter()
138 public void windowClosing(WindowEvent e)
140 if (me.av.featureSettings == me)
142 me.av.featureSettings = null;
148 int height = featurePanel.getComponentCount() * 50 + 60;
150 height = Math.max(200, height);
151 height = Math.min(400, height);
153 jalview.bin.JalviewLite.addFrame(frame,
154 MessageManager.getString("label.feature_settings"), width,
158 public void paint(Graphics g)
160 g.setColor(Color.black);
161 g.drawString(MessageManager
162 .getString("label.no_features_added_to_this_alignment"), 10, 20);
163 g.drawString(MessageManager
164 .getString("label.features_can_be_added_from_searches_1"), 10,
166 g.drawString(MessageManager
167 .getString("label.features_can_be_added_from_searches_2"), 10,
171 protected void popupSort(final MyCheckbox check, final Hashtable minmax,
174 final String type = check.type;
175 final Object typeCol = fr.getFeatureStyle(type);
176 java.awt.PopupMenu men = new PopupMenu(MessageManager.formatMessage(
177 "label.settings_for_type", new String[]
179 java.awt.MenuItem scr = new MenuItem(
180 MessageManager.getString("label.sort_by_score"));
182 final FeatureSettings me = this;
183 scr.addActionListener(new ActionListener()
186 public void actionPerformed(ActionEvent e)
188 me.sortByScore(new String[]
193 MenuItem dens = new MenuItem(
194 MessageManager.getString("label.sort_by_density"));
195 dens.addActionListener(new ActionListener()
198 public void actionPerformed(ActionEvent e)
200 me.sortByDens(new String[]
208 final Object typeMinMax = minmax.get(type);
210 * final java.awt.CheckboxMenuItem chb = new
211 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
212 * moment chb.setState(minmax.get(type) != null);
213 * chb.addActionListener(new ActionListener() {
215 * public void actionPerformed(ActionEvent e) {
216 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
217 * null); } else { minmax.put(type, typeMinMax); } }
221 if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null)
223 // graduated colourschemes for those where minmax exists for the
224 // positional features
225 MenuItem mxcol = new MenuItem(
226 (typeCol instanceof Color) ? "Graduated Colour"
229 mxcol.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
234 if (typeCol instanceof Color)
236 new FeatureColourChooser(me, type);
237 // write back the current colour object to update the table
238 check.updateColor(fr.getFeatureStyle(type));
242 new UserDefinedColours(me, check.type,
243 ((GraduatedColor) typeCol));
250 this.featurePanel.add(men);
251 men.show(this.featurePanel, x, y);
254 public void setTableData()
256 alignmentHasFeatures = fr.buildGroupHash();
257 if (alignmentHasFeatures)
266 * rebuilds the group panel
268 public void rebuildGroups()
270 boolean rdrw = false;
271 if (groupPanel == null)
273 groupPanel = new Panel();
278 groupPanel.removeAll();
280 // TODO: JAL-964 - smoothly incorporate new group entries if panel already
281 // displayed and new groups present
282 Enumeration gps = fr.featureGroups.keys();
283 while (gps.hasMoreElements())
285 String group = (String) gps.nextElement();
286 Boolean vis = (Boolean) fr.featureGroups.get(group);
287 Checkbox check = new MyCheckbox(group, vis.booleanValue(),
288 (fr.featureLinks != null && fr.featureLinks
289 .containsKey(group)));
290 check.addMouseListener(this);
291 check.setFont(new Font("Serif", Font.BOLD, 12));
292 check.addItemListener(this);
293 check.setVisible(fr.hiddenGroups.contains(group));
294 groupPanel.add(check);
298 groupPanel.validate();
302 // This routine adds and removes checkboxes depending on
303 // Group selection states
304 void resetTable(boolean groupsChanged)
306 SequenceFeature[] tmpfeatures;
307 String group = null, type;
308 Vector visibleChecks = new Vector();
309 AlignmentI alignment = av.getAlignment();
310 for (int i = 0; i < alignment.getHeight(); i++)
312 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
317 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
319 while (index < tmpfeatures.length)
321 group = tmpfeatures[index].featureGroup;
323 if (group == null || fr.featureGroups.get(group) == null
324 || ((Boolean) fr.featureGroups.get(group)).booleanValue())
326 type = tmpfeatures[index].getType();
327 if (!visibleChecks.contains(type))
329 visibleChecks.addElement(type);
337 int cSize = featurePanel.getComponentCount();
339 // This will remove any checkboxes which shouldn't be
341 for (int i = 0; i < cSize; i++)
343 comps = featurePanel.getComponents();
344 check = (MyCheckbox) comps[i];
345 if (!visibleChecks.contains(check.type))
347 featurePanel.remove(i);
353 if (fr.renderOrder != null)
355 // First add the checks in the previous render order,
356 // in case the window has been closed and reopened
357 for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
359 String item = fr.renderOrder[ro];
361 if (!visibleChecks.contains(item))
366 visibleChecks.removeElement(item);
368 addCheck(false, item);
372 // now add checkboxes which should be visible,
373 // if they have not already been added
374 Enumeration en = visibleChecks.elements();
376 while (en.hasMoreElements())
378 addCheck(groupsChanged, en.nextElement().toString());
381 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
383 featurePanel.validate();
385 if (scrollPane != null)
387 scrollPane.validate();
390 itemStateChanged(null);
394 * update the checklist of feature types with the given type
396 * @param groupsChanged
397 * true means if the type is not in the display list then it will be
398 * added and displayed
400 * feature type to be checked for in the list.
402 void addCheck(boolean groupsChanged, String type)
405 Component[] comps = featurePanel.getComponents();
408 for (int i = 0; i < featurePanel.getComponentCount(); i++)
410 check = (MyCheckbox) comps[i];
411 if (check.type.equals(type))
420 boolean selected = false;
421 if (groupsChanged || av.featuresDisplayed.containsKey(type))
426 check = new MyCheckbox(
429 (fr.featureLinks != null && fr.featureLinks.containsKey(type)),
430 fr.getFeatureStyle(type));
432 check.addMouseListener(this);
433 check.addMouseMotionListener(this);
434 check.addItemListener(this);
437 // add at beginning of stack.
438 featurePanel.add(check, 0);
442 // add at end of stack.
443 featurePanel.add(check);
448 public void actionPerformed(ActionEvent evt)
450 for (int i = 0; i < featurePanel.getComponentCount(); i++)
452 Checkbox check = (Checkbox) featurePanel.getComponent(i);
453 check.setState(!check.getState());
458 public void itemStateChanged(ItemEvent evt)
462 // Is the source a top level featureGroup?
463 Checkbox source = (Checkbox) evt.getSource();
464 if (fr.featureGroups.containsKey(source.getLabel()))
466 fr.featureGroups.put(source.getLabel(),
467 new Boolean(source.getState()));
468 ap.seqPanel.seqCanvas.repaint();
469 if (ap.overviewPanel != null)
471 ap.overviewPanel.updateOverviewImage();
481 void selectionChanged()
483 Component[] comps = featurePanel.getComponents();
484 int cSize = comps.length;
486 Object[][] tmp = new Object[cSize][3];
488 for (int i = 0; i < cSize; i++)
490 MyCheckbox check = (MyCheckbox) comps[i];
491 tmp[tmpSize][0] = check.type;
492 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
493 tmp[tmpSize][2] = new Boolean(check.getState());
497 Object[][] data = new Object[tmpSize][3];
498 System.arraycopy(tmp, 0, data, 0, tmpSize);
500 fr.setFeaturePriority(data);
502 ap.paintAlignment(true);
505 MyCheckbox selectedCheck;
507 boolean dragging = false;
509 public void mousePressed(MouseEvent evt)
512 selectedCheck = (MyCheckbox) evt.getSource();
514 if (fr.featureLinks != null
515 && fr.featureLinks.containsKey(selectedCheck.type))
517 if (evt.getX() > selectedCheck.stringWidth + 20)
525 public void mouseDragged(MouseEvent evt)
527 if (((Component) evt.getSource()).getParent() != featurePanel)
534 public void mouseReleased(MouseEvent evt)
536 if (((Component) evt.getSource()).getParent() != featurePanel)
541 Component comp = null;
542 Checkbox target = null;
544 int height = evt.getY() + evt.getComponent().getLocation().y;
546 if (height > featurePanel.getSize().height)
550 .getComponent(featurePanel.getComponentCount() - 1);
554 comp = featurePanel.getComponent(0);
558 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
559 + evt.getComponent().getLocation().y);
562 if (comp != null && comp instanceof Checkbox)
564 target = (Checkbox) comp;
567 if (selectedCheck != null && target != null && selectedCheck != target)
569 int targetIndex = -1;
570 for (int i = 0; i < featurePanel.getComponentCount(); i++)
572 if (target == featurePanel.getComponent(i))
579 featurePanel.remove(selectedCheck);
580 featurePanel.add(selectedCheck, targetIndex);
581 featurePanel.validate();
582 itemStateChanged(null);
586 public void setUserColour(String feature, Object originalColour)
588 if (originalColour instanceof Color
589 || originalColour instanceof GraduatedColor)
591 fr.setColour(feature, originalColour);
595 throw new Error(MessageManager.getString("error.implementation_error_unsupported_feature_colour_object"));
600 public void refreshTable()
602 featurePanel.removeAll();
604 ap.paintAlignment(true);
607 public void mouseEntered(MouseEvent evt)
611 public void mouseExited(MouseEvent evt)
615 public void mouseClicked(MouseEvent evt)
617 MyCheckbox check = (MyCheckbox) evt.getSource();
618 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
620 this.popupSort(check, fr.minmax, evt.getX(), evt.getY());
622 if (fr.featureLinks != null && fr.featureLinks.containsKey(check.type))
624 if (evt.getX() > check.stringWidth + 20)
627 String link = fr.featureLinks.get(check.type).toString();
628 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
629 link.substring(0, link.indexOf("|")));
633 if (check.getParent() != featurePanel)
638 if (evt.getClickCount() > 1)
640 Object fcol = fr.getFeatureStyle(check.type);
641 if (fcol instanceof Color)
643 new UserDefinedColours(this, check.type, (Color) fcol);
647 new FeatureColourChooser(this, check.type);
648 // write back the current colour object to update the table
649 check.updateColor(fr.getFeatureStyle(check.type));
654 public void mouseMoved(MouseEvent evt)
658 public void adjustmentValueChanged(AdjustmentEvent evt)
660 fr.transparency = ((float) (100 - transparency.getValue()) / 100f);
661 ap.seqPanel.seqCanvas.repaint();
665 class MyCheckbox extends Checkbox
669 public int stringWidth;
677 public void updateColor(Object newcol)
679 if (newcol instanceof Color)
681 col = (Color) newcol;
684 else if (newcol instanceof GraduatedColor)
686 gcol = (GraduatedColor) newcol;
691 throw new Error(MessageManager.getString("error.invalid_colour_for_mycheckbox"));
699 String vlabel = type;
700 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
703 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
706 if (gcol.isColourByLabel())
708 setBackground(Color.white);
709 vlabel += " (by Label)";
713 setBackground(gcol.getMinColor());
715 this.setLabel(vlabel);
720 public MyCheckbox(String label, boolean checked, boolean haslink)
722 super(label, checked);
724 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
725 stringWidth = fm.stringWidth(label);
726 this.hasLink = haslink;
729 public MyCheckbox(String type, boolean selected, boolean b,
732 this(type, selected, b);
733 updateColor(featureStyle);
736 public void paint(Graphics g)
738 Dimension d = getSize();
741 if (gcol.isColourByLabel())
743 g.setColor(Color.white);
744 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
746 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
749 * // g.setFont(g.getFont().deriveFont( //
750 * AffineTransform.getScaleInstance( //
751 * width/g.getFontMetrics().stringWidth("Label"), //
752 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
759 Color maxCol = gcol.getMaxColor();
761 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
768 g.drawImage(linkImage, stringWidth + 25,
769 (getSize().height - linkImage.getHeight(this)) / 2, this);
774 protected void sortByDens(String[] typ)
776 sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
779 private String[] getDisplayedFeatureTypes()
784 synchronized (fr.renderOrder)
786 typ = new String[fr.renderOrder.length];
787 System.arraycopy(fr.renderOrder, 0, typ, 0, typ.length);
788 for (int i = 0; i < typ.length; i++)
790 if (av.featuresDisplayed.get(typ[i]) == null)
800 protected void sortBy(String[] typ, String methodText, final String method)
804 typ = getDisplayedFeatureTypes();
807 gps = fr.getGroups(true);
810 for (int i = 0; i < typ.length; i++)
812 System.err.println("Sorting on Types:" + typ[i]);
818 for (int i = 0; i < gps.length; i++)
820 System.err.println("Sorting on groups:" + gps[i]);
823 AlignmentPanel alignPanel = ap;
824 AlignmentI al = alignPanel.av.getAlignment();
827 SequenceGroup sg = alignPanel.av.getSelectionGroup();
830 start = sg.getStartRes();
831 stop = sg.getEndRes();
836 stop = al.getWidth();
838 SequenceI[] oldOrder = al.getSequencesArray();
839 AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
840 this.ap.alignFrame.addHistoryItem(new OrderCommand(methodText,
841 oldOrder, alignPanel.av.getAlignment()));
842 alignPanel.paintAlignment(true);
846 protected void sortByScore(String[] typ)
848 sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);