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.FeatureSettingsControllerI;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceFeature;
26 import jalview.schemes.AnnotationColourGradient;
27 import jalview.schemes.GraduatedColor;
28 import jalview.util.MessageManager;
30 import java.awt.BorderLayout;
31 import java.awt.Button;
32 import java.awt.Checkbox;
33 import java.awt.Color;
34 import java.awt.Component;
35 import java.awt.Dimension;
37 import java.awt.FontMetrics;
38 import java.awt.Frame;
39 import java.awt.Graphics;
40 import java.awt.GridLayout;
41 import java.awt.Image;
42 import java.awt.Label;
43 import java.awt.MenuItem;
44 import java.awt.Panel;
45 import java.awt.PopupMenu;
46 import java.awt.ScrollPane;
47 import java.awt.Scrollbar;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.AdjustmentEvent;
51 import java.awt.event.AdjustmentListener;
52 import java.awt.event.InputEvent;
53 import java.awt.event.ItemEvent;
54 import java.awt.event.ItemListener;
55 import java.awt.event.MouseEvent;
56 import java.awt.event.MouseListener;
57 import java.awt.event.MouseMotionListener;
58 import java.awt.event.WindowAdapter;
59 import java.awt.event.WindowEvent;
60 import java.util.Arrays;
61 import java.util.Enumeration;
62 import java.util.Hashtable;
63 import java.util.List;
64 import java.util.Vector;
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 if (fr.isTransparencyAvailable())
100 transparency.addAdjustmentListener(this);
104 transparency.setEnabled(false);
107 java.net.URL url = getClass().getResource("/images/link.gif");
110 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
113 if (av.isShowSequenceFeatures() || !fr.hasRenderOrder())
115 fr.findAllFeatures(true); // was default - now true to make all visible
118 discoverAllFeatureData();
120 this.setLayout(new BorderLayout());
121 scrollPane = new ScrollPane();
122 scrollPane.add(featurePanel);
123 if (fr.getAllFeatureColours() != null
124 && fr.getAllFeatureColours().size() > 0)
126 add(scrollPane, BorderLayout.CENTER);
129 Button invert = new Button("Invert Selection");
130 invert.addActionListener(this);
132 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
133 lowerPanel.add(invert);
135 Panel tPanel = new Panel(new BorderLayout());
137 if (fr.isTransparencyAvailable())
139 tPanel.add(transparency, BorderLayout.CENTER);
140 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
145 new Label("Transparency not available in this web browser"),
146 BorderLayout.CENTER);
149 lowerPanel.add(tPanel, BorderLayout.SOUTH);
151 add(lowerPanel, BorderLayout.SOUTH);
153 if (groupPanel != null)
155 groupPanel.setLayout(new GridLayout(
156 (fr.getFeatureGroupsSize()) / 4 + 1, 4)); // JBPNote - this was
157 // scaled on number of
158 // visible groups. seems
160 groupPanel.validate();
162 add(groupPanel, BorderLayout.NORTH);
166 final FeatureSettings me = this;
167 frame.addWindowListener(new WindowAdapter()
170 public void windowClosing(WindowEvent e)
172 if (me.av.featureSettings == me)
174 me.av.featureSettings = null;
180 int height = featurePanel.getComponentCount() * 50 + 60;
182 height = Math.max(200, height);
183 height = Math.min(400, height);
185 jalview.bin.JalviewLite.addFrame(frame,
186 MessageManager.getString("label.sequence_feature_settings"),
191 public void paint(Graphics g)
193 g.setColor(Color.black);
194 g.drawString(MessageManager
195 .getString("label.no_features_added_to_this_alignment"), 10, 20);
196 g.drawString(MessageManager
197 .getString("label.features_can_be_added_from_searches_1"), 10,
199 g.drawString(MessageManager
200 .getString("label.features_can_be_added_from_searches_2"), 10,
204 protected void popupSort(final MyCheckbox check, final Hashtable minmax,
207 final String type = check.type;
208 final Object typeCol = fr.getFeatureStyle(type);
209 java.awt.PopupMenu men = new PopupMenu(MessageManager.formatMessage(
210 "label.settings_for_type", new String[] { type }));
211 java.awt.MenuItem scr = new MenuItem(
212 MessageManager.getString("label.sort_by_score"));
214 final FeatureSettings me = this;
215 scr.addActionListener(new ActionListener()
219 public void actionPerformed(ActionEvent e)
221 me.ap.alignFrame.avc.sortAlignmentByFeatureScore(Arrays
222 .asList(new String[] { type }));
226 MenuItem dens = new MenuItem(
227 MessageManager.getString("label.sort_by_density"));
228 dens.addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
234 me.ap.alignFrame.avc.sortAlignmentByFeatureDensity(Arrays
235 .asList(new String[] { type }));
242 final Object typeMinMax = minmax.get(type);
244 * final java.awt.CheckboxMenuItem chb = new
245 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
246 * moment chb.setState(minmax.get(type) != null);
247 * chb.addActionListener(new ActionListener() {
249 * public void actionPerformed(ActionEvent e) {
250 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
251 * null); } else { minmax.put(type, typeMinMax); } }
255 if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null)
257 // graduated colourschemes for those where minmax exists for the
258 // positional features
259 MenuItem mxcol = new MenuItem(
260 (typeCol instanceof Color) ? "Graduated Colour"
263 mxcol.addActionListener(new ActionListener()
267 public void actionPerformed(ActionEvent e)
269 if (typeCol instanceof Color)
271 new FeatureColourChooser(me, type);
272 // write back the current colour object to update the table
273 check.updateColor(fr.getFeatureStyle(type));
277 new UserDefinedColours(me, check.type,
278 ((GraduatedColor) typeCol));
285 this.featurePanel.add(men);
286 men.show(this.featurePanel, x, y);
290 public void discoverAllFeatureData()
292 if (fr.getAllFeatureColours() != null
293 && fr.getAllFeatureColours().size() > 0)
302 * rebuilds the group panel
304 public void rebuildGroups()
306 boolean rdrw = false;
307 if (groupPanel == null)
309 groupPanel = new Panel();
314 groupPanel.removeAll();
316 // TODO: JAL-964 - smoothly incorporate new group entries if panel already
317 // displayed and new groups present
318 for (String group : fr.getFeatureGroups())
320 boolean vis = fr.checkGroupVisibility(group, false);
321 Checkbox check = new MyCheckbox(group, vis, false);
322 check.addMouseListener(this);
323 check.setFont(new Font("Serif", Font.BOLD, 12));
324 check.addItemListener(groupItemListener);
325 // note - visibility seems to correlate with displayed. ???wtf ??
326 check.setVisible(vis);
327 groupPanel.add(check);
331 groupPanel.validate();
335 // This routine adds and removes checkboxes depending on
336 // Group selection states
337 void resetTable(boolean groupsChanged)
339 SequenceFeature[] tmpfeatures;
340 String group = null, type;
341 Vector visibleChecks = new Vector();
342 AlignmentI alignment = av.getAlignment();
343 for (int i = 0; i < alignment.getHeight(); i++)
345 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
350 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
352 while (index < tmpfeatures.length)
354 group = tmpfeatures[index].featureGroup;
356 if (group == null || fr.checkGroupVisibility(group, true))
358 type = tmpfeatures[index].getType();
359 if (!visibleChecks.contains(type))
361 visibleChecks.addElement(type);
369 int cSize = featurePanel.getComponentCount();
371 // This will remove any checkboxes which shouldn't be
373 for (int i = 0; i < cSize; i++)
375 comps = featurePanel.getComponents();
376 check = (MyCheckbox) comps[i];
377 if (!visibleChecks.contains(check.type))
379 featurePanel.remove(i);
385 if (fr.getRenderOrder() != null)
387 // First add the checks in the previous render order,
388 // in case the window has been closed and reopened
389 List<String> rol = fr.getRenderOrder();
390 for (int ro = rol.size() - 1; ro > -1; ro--)
392 String item = rol.get(ro);
394 if (!visibleChecks.contains(item))
399 visibleChecks.removeElement(item);
401 addCheck(false, item);
405 // now add checkboxes which should be visible,
406 // if they have not already been added
407 Enumeration en = visibleChecks.elements();
409 while (en.hasMoreElements())
411 addCheck(groupsChanged, en.nextElement().toString());
414 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
416 featurePanel.validate();
418 if (scrollPane != null)
420 scrollPane.validate();
423 itemStateChanged(null);
427 * update the checklist of feature types with the given type
429 * @param groupsChanged
430 * true means if the type is not in the display list then it will be
431 * added and displayed
433 * feature type to be checked for in the list.
435 void addCheck(boolean groupsChanged, String type)
438 Component[] comps = featurePanel.getComponents();
441 for (int i = 0; i < featurePanel.getComponentCount(); i++)
443 check = (MyCheckbox) comps[i];
444 if (check.type.equals(type))
453 boolean selected = false;
454 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
459 check = new MyCheckbox(type, selected, false,
460 fr.getFeatureStyle(type));
462 check.addMouseListener(this);
463 check.addMouseMotionListener(this);
464 check.addItemListener(this);
467 // add at beginning of stack.
468 featurePanel.add(check, 0);
472 // add at end of stack.
473 featurePanel.add(check);
479 public void actionPerformed(ActionEvent evt)
481 for (int i = 0; i < featurePanel.getComponentCount(); i++)
483 Checkbox check = (Checkbox) featurePanel.getComponent(i);
484 check.setState(!check.getState());
489 private ItemListener groupItemListener = new ItemListener()
492 public void itemStateChanged(ItemEvent evt)
494 Checkbox source = (Checkbox) evt.getSource();
495 fr.setGroupVisibility(source.getLabel(), source.getState());
496 ap.seqPanel.seqCanvas.repaint();
497 if (ap.overviewPanel != null)
499 ap.overviewPanel.updateOverviewImage();
507 public void itemStateChanged(ItemEvent evt)
512 void selectionChanged()
514 Component[] comps = featurePanel.getComponents();
515 int cSize = comps.length;
517 Object[][] tmp = new Object[cSize][3];
519 for (int i = 0; i < cSize; i++)
521 MyCheckbox check = (MyCheckbox) comps[i];
522 tmp[tmpSize][0] = check.type;
523 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
524 tmp[tmpSize][2] = new Boolean(check.getState());
528 Object[][] data = new Object[tmpSize][3];
529 System.arraycopy(tmp, 0, data, 0, tmpSize);
531 fr.setFeaturePriority(data);
533 ap.paintAlignment(true);
536 MyCheckbox selectedCheck;
538 boolean dragging = false;
541 public void mouseDragged(MouseEvent evt)
543 if (((Component) evt.getSource()).getParent() != featurePanel)
551 public void mouseReleased(MouseEvent evt)
553 if (((Component) evt.getSource()).getParent() != featurePanel)
558 Component comp = null;
559 Checkbox target = null;
561 int height = evt.getY() + evt.getComponent().getLocation().y;
563 if (height > featurePanel.getSize().height)
567 .getComponent(featurePanel.getComponentCount() - 1);
571 comp = featurePanel.getComponent(0);
575 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
576 + evt.getComponent().getLocation().y);
579 if (comp != null && comp instanceof Checkbox)
581 target = (Checkbox) comp;
584 if (selectedCheck != null && target != null && selectedCheck != target)
586 int targetIndex = -1;
587 for (int i = 0; i < featurePanel.getComponentCount(); i++)
589 if (target == featurePanel.getComponent(i))
596 featurePanel.remove(selectedCheck);
597 featurePanel.add(selectedCheck, targetIndex);
598 featurePanel.validate();
599 itemStateChanged(null);
603 public void setUserColour(String feature, Object originalColour)
605 if (originalColour instanceof Color
606 || originalColour instanceof GraduatedColor)
608 fr.setColour(feature, originalColour);
614 .getString("error.implementation_error_unsupported_feature_colour_object"));
619 public void refreshTable()
621 featurePanel.removeAll();
623 ap.paintAlignment(true);
627 public void mouseEntered(MouseEvent evt)
632 public void mouseExited(MouseEvent evt)
637 public void mouseClicked(MouseEvent evt)
639 MyCheckbox check = (MyCheckbox) evt.getSource();
640 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
642 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
645 if (check.getParent() != featurePanel)
650 if (evt.getClickCount() > 1)
652 Object fcol = fr.getFeatureStyle(check.type);
653 if (fcol instanceof Color)
655 new UserDefinedColours(this, check.type, (Color) fcol);
659 new FeatureColourChooser(this, check.type);
660 // write back the current colour object to update the table
661 check.updateColor(fr.getFeatureStyle(check.type));
667 public void mouseMoved(MouseEvent evt)
672 public void adjustmentValueChanged(AdjustmentEvent evt)
674 fr.setTransparency((100 - transparency.getValue()) / 100f);
675 ap.seqPanel.seqCanvas.repaint();
679 class MyCheckbox extends Checkbox
683 public int stringWidth;
691 public void updateColor(Object newcol)
693 if (newcol instanceof Color)
695 col = (Color) newcol;
698 else if (newcol instanceof GraduatedColor)
700 gcol = (GraduatedColor) newcol;
707 .getString("error.invalid_colour_for_mycheckbox"));
715 String vlabel = type;
716 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
719 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
722 if (gcol.isColourByLabel())
724 setBackground(Color.white);
725 vlabel += " (by Label)";
729 setBackground(gcol.getMinColor());
731 this.setLabel(vlabel);
736 public MyCheckbox(String label, boolean checked, boolean haslink)
738 super(label, checked);
740 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
741 stringWidth = fm.stringWidth(label);
742 this.hasLink = haslink;
745 public MyCheckbox(String type, boolean selected, boolean b,
748 this(type, selected, b);
749 updateColor(featureStyle);
753 public void paint(Graphics g)
755 Dimension d = getSize();
758 if (gcol.isColourByLabel())
760 g.setColor(Color.white);
761 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
763 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
766 * // g.setFont(g.getFont().deriveFont( //
767 * AffineTransform.getScaleInstance( //
768 * width/g.getFontMetrics().stringWidth("Label"), //
769 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
776 Color maxCol = gcol.getMaxColor();
778 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
785 g.drawImage(linkImage, stringWidth + 25,
786 (getSize().height - linkImage.getHeight(this)) / 2, this);
792 public void mousePressed(MouseEvent e)