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;
24 import java.util.List;
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;
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.getTransparency() * 100), 1, 1, 100);
67 if (fr.isTransparencyAvailable())
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.isShowSequenceFeatures() || !fr.hasRenderOrder())
84 fr.findAllFeatures(true); // was default - now true to make all visible
89 this.setLayout(new BorderLayout());
90 scrollPane = new ScrollPane();
91 scrollPane.add(featurePanel);
92 if (fr.getAllFeatureColours()!=null && fr.getAllFeatureColours().size()>0)
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.isTransparencyAvailable())
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.getFeatureGroupsSize()) / 4 + 1,
126 4)); // JBPNote - this was scaled on number of visible groups. seems broken
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,
152 MessageManager.getString("label.feature_settings"), width,
156 public void paint(Graphics g)
158 g.setColor(Color.black);
159 g.drawString(MessageManager
160 .getString("label.no_features_added_to_this_alignment"), 10, 20);
161 g.drawString(MessageManager
162 .getString("label.features_can_be_added_from_searches_1"), 10,
164 g.drawString(MessageManager
165 .getString("label.features_can_be_added_from_searches_2"), 10,
169 protected void popupSort(final MyCheckbox check, final Hashtable minmax,
172 final String type = check.type;
173 final Object typeCol = fr.getFeatureStyle(type);
174 java.awt.PopupMenu men = new PopupMenu(MessageManager.formatMessage(
175 "label.settings_for_type", new String[]
177 java.awt.MenuItem scr = new MenuItem(
178 MessageManager.getString("label.sort_by_score"));
180 final FeatureSettings me = this;
181 scr.addActionListener(new ActionListener()
184 public void actionPerformed(ActionEvent e)
186 me.ap.alignFrame.avc.sortAlignmentByFeatureScore(new String[]
191 MenuItem dens = new MenuItem(
192 MessageManager.getString("label.sort_by_density"));
193 dens.addActionListener(new ActionListener()
196 public void actionPerformed(ActionEvent e)
198 me.ap.alignFrame.avc.sortAlignmentByFeatureDensity(new String[]
206 final Object typeMinMax = minmax.get(type);
208 * final java.awt.CheckboxMenuItem chb = new
209 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
210 * moment chb.setState(minmax.get(type) != null);
211 * chb.addActionListener(new ActionListener() {
213 * public void actionPerformed(ActionEvent e) {
214 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
215 * null); } else { minmax.put(type, typeMinMax); } }
219 if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null)
221 // graduated colourschemes for those where minmax exists for the
222 // positional features
223 MenuItem mxcol = new MenuItem(
224 (typeCol instanceof Color) ? "Graduated Colour"
227 mxcol.addActionListener(new ActionListener()
230 public void actionPerformed(ActionEvent e)
232 if (typeCol instanceof Color)
234 new FeatureColourChooser(me, type);
235 // write back the current colour object to update the table
236 check.updateColor(fr.getFeatureStyle(type));
240 new UserDefinedColours(me, check.type,
241 ((GraduatedColor) typeCol));
248 this.featurePanel.add(men);
249 men.show(this.featurePanel, x, y);
252 public void setTableData()
254 if (fr.getAllFeatureColours()!=null && fr.getAllFeatureColours().size()>0)
263 * rebuilds the group panel
265 public void rebuildGroups()
267 boolean rdrw = false;
268 if (groupPanel == null)
270 groupPanel = new Panel();
275 groupPanel.removeAll();
277 // TODO: JAL-964 - smoothly incorporate new group entries if panel already
278 // displayed and new groups present
279 for (String group:(List<String>)fr.getFeatureGroups())
281 boolean vis = fr.checkGroupVisibility(group, false);
282 Checkbox check = new MyCheckbox(group, vis,
283 (fr.featureLinks != null && fr.featureLinks
284 .containsKey(group)));
285 check.addMouseListener(this);
286 check.setFont(new Font("Serif", Font.BOLD, 12));
287 check.addItemListener(groupItemListener);
288 // note - visibility seems to correlate with displayed. ???wtf ??
289 check.setVisible(vis);
290 groupPanel.add(check);
294 groupPanel.validate();
297 // This routine adds and removes checkboxes depending on
298 // Group selection states
299 void resetTable(boolean groupsChanged)
301 SequenceFeature[] tmpfeatures;
302 String group = null, type;
303 Vector visibleChecks = new Vector();
304 AlignmentI alignment = av.getAlignment();
305 for (int i = 0; i < alignment.getHeight(); i++)
307 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
312 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
314 while (index < tmpfeatures.length)
316 group = tmpfeatures[index].featureGroup;
318 if (group == null || fr.checkGroupVisibility(group, true))
320 type = tmpfeatures[index].getType();
321 if (!visibleChecks.contains(type))
323 visibleChecks.addElement(type);
331 int cSize = featurePanel.getComponentCount();
333 // This will remove any checkboxes which shouldn't be
335 for (int i = 0; i < cSize; i++)
337 comps = featurePanel.getComponents();
338 check = (MyCheckbox) comps[i];
339 if (!visibleChecks.contains(check.type))
341 featurePanel.remove(i);
347 if (fr.getRenderOrder() != null)
349 // First add the checks in the previous render order,
350 // in case the window has been closed and reopened
351 List<String> rol = fr.getRenderOrder();
352 for (int ro = rol.size() - 1; ro > -1; ro--)
354 String item = rol.get(ro);
356 if (!visibleChecks.contains(item))
361 visibleChecks.removeElement(item);
363 addCheck(false, item);
367 // now add checkboxes which should be visible,
368 // if they have not already been added
369 Enumeration en = visibleChecks.elements();
371 while (en.hasMoreElements())
373 addCheck(groupsChanged, en.nextElement().toString());
376 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
378 featurePanel.validate();
380 if (scrollPane != null)
382 scrollPane.validate();
385 itemStateChanged(null);
389 * update the checklist of feature types with the given type
391 * @param groupsChanged
392 * true means if the type is not in the display list then it will be
393 * added and displayed
395 * feature type to be checked for in the list.
397 void addCheck(boolean groupsChanged, String type)
400 Component[] comps = featurePanel.getComponents();
403 for (int i = 0; i < featurePanel.getComponentCount(); i++)
405 check = (MyCheckbox) comps[i];
406 if (check.type.equals(type))
415 boolean selected = false;
416 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
421 check = new MyCheckbox(
424 (fr.featureLinks != null && fr.featureLinks.containsKey(type)),
425 fr.getFeatureStyle(type));
427 check.addMouseListener(this);
428 check.addMouseMotionListener(this);
429 check.addItemListener(this);
432 // add at beginning of stack.
433 featurePanel.add(check, 0);
437 // add at end of stack.
438 featurePanel.add(check);
443 public void actionPerformed(ActionEvent evt)
445 for (int i = 0; i < featurePanel.getComponentCount(); i++)
447 Checkbox check = (Checkbox) featurePanel.getComponent(i);
448 check.setState(!check.getState());
453 private ItemListener groupItemListener = new ItemListener() {
454 public void itemStateChanged(ItemEvent evt) {
455 Checkbox source = (Checkbox) evt.getSource();
456 fr.setGroupVisibility(source.getLabel(),
458 ap.seqPanel.seqCanvas.repaint();
459 if (ap.overviewPanel != null)
461 ap.overviewPanel.updateOverviewImage();
467 public void itemStateChanged(ItemEvent evt)
472 void selectionChanged()
474 Component[] comps = featurePanel.getComponents();
475 int cSize = comps.length;
477 Object[][] tmp = new Object[cSize][3];
479 for (int i = 0; i < cSize; i++)
481 MyCheckbox check = (MyCheckbox) comps[i];
482 tmp[tmpSize][0] = check.type;
483 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
484 tmp[tmpSize][2] = new Boolean(check.getState());
488 Object[][] data = new Object[tmpSize][3];
489 System.arraycopy(tmp, 0, data, 0, tmpSize);
491 fr.setFeaturePriority(data);
493 ap.paintAlignment(true);
496 MyCheckbox selectedCheck;
498 boolean dragging = false;
500 public void mousePressed(MouseEvent evt)
503 selectedCheck = (MyCheckbox) evt.getSource();
505 if (fr.featureLinks != null
506 && fr.featureLinks.containsKey(selectedCheck.type))
508 if (evt.getX() > selectedCheck.stringWidth + 20)
516 public void mouseDragged(MouseEvent evt)
518 if (((Component) evt.getSource()).getParent() != featurePanel)
525 public void mouseReleased(MouseEvent evt)
527 if (((Component) evt.getSource()).getParent() != featurePanel)
532 Component comp = null;
533 Checkbox target = null;
535 int height = evt.getY() + evt.getComponent().getLocation().y;
537 if (height > featurePanel.getSize().height)
541 .getComponent(featurePanel.getComponentCount() - 1);
545 comp = featurePanel.getComponent(0);
549 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
550 + evt.getComponent().getLocation().y);
553 if (comp != null && comp instanceof Checkbox)
555 target = (Checkbox) comp;
558 if (selectedCheck != null && target != null && selectedCheck != target)
560 int targetIndex = -1;
561 for (int i = 0; i < featurePanel.getComponentCount(); i++)
563 if (target == featurePanel.getComponent(i))
570 featurePanel.remove(selectedCheck);
571 featurePanel.add(selectedCheck, targetIndex);
572 featurePanel.validate();
573 itemStateChanged(null);
577 public void setUserColour(String feature, Object originalColour)
579 if (originalColour instanceof Color
580 || originalColour instanceof GraduatedColor)
582 fr.setColour(feature, originalColour);
586 throw new Error(MessageManager.getString("error.implementation_error_unsupported_feature_colour_object"));
591 public void refreshTable()
593 featurePanel.removeAll();
595 ap.paintAlignment(true);
598 public void mouseEntered(MouseEvent evt)
602 public void mouseExited(MouseEvent evt)
606 public void mouseClicked(MouseEvent evt)
608 MyCheckbox check = (MyCheckbox) evt.getSource();
609 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
611 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
613 if (fr.featureLinks != null && fr.featureLinks.containsKey(check.type))
615 if (evt.getX() > check.stringWidth + 20)
618 String link = fr.featureLinks.get(check.type).toString();
619 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
620 link.substring(0, link.indexOf("|")));
624 if (check.getParent() != featurePanel)
629 if (evt.getClickCount() > 1)
631 Object fcol = fr.getFeatureStyle(check.type);
632 if (fcol instanceof Color)
634 new UserDefinedColours(this, check.type, (Color) fcol);
638 new FeatureColourChooser(this, check.type);
639 // write back the current colour object to update the table
640 check.updateColor(fr.getFeatureStyle(check.type));
645 public void mouseMoved(MouseEvent evt)
649 public void adjustmentValueChanged(AdjustmentEvent evt)
651 fr.setTransparency((float) (100 - transparency.getValue()) / 100f);
652 ap.seqPanel.seqCanvas.repaint();
656 class MyCheckbox extends Checkbox
660 public int stringWidth;
668 public void updateColor(Object newcol)
670 if (newcol instanceof Color)
672 col = (Color) newcol;
675 else if (newcol instanceof GraduatedColor)
677 gcol = (GraduatedColor) newcol;
682 throw new Error(MessageManager.getString("error.invalid_colour_for_mycheckbox"));
690 String vlabel = type;
691 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
694 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
697 if (gcol.isColourByLabel())
699 setBackground(Color.white);
700 vlabel += " (by Label)";
704 setBackground(gcol.getMinColor());
706 this.setLabel(vlabel);
711 public MyCheckbox(String label, boolean checked, boolean haslink)
713 super(label, checked);
715 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
716 stringWidth = fm.stringWidth(label);
717 this.hasLink = haslink;
720 public MyCheckbox(String type, boolean selected, boolean b,
723 this(type, selected, b);
724 updateColor(featureStyle);
727 public void paint(Graphics g)
729 Dimension d = getSize();
732 if (gcol.isColourByLabel())
734 g.setColor(Color.white);
735 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
737 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
740 * // g.setFont(g.getFont().deriveFont( //
741 * AffineTransform.getScaleInstance( //
742 * width/g.getFontMetrics().stringWidth("Label"), //
743 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
750 Color maxCol = gcol.getMaxColor();
752 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
759 g.drawImage(linkImage, stringWidth + 25,
760 (getSize().height - linkImage.getHeight(this)) / 2, this);