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.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,
205 final Map<String, float[][]> minmax,
208 final String type = check.type;
209 final Object typeCol = fr.getFeatureStyle(type);
210 java.awt.PopupMenu men = new PopupMenu(MessageManager.formatMessage(
211 "label.settings_for_type", new String[] { type }));
212 java.awt.MenuItem scr = new MenuItem(
213 MessageManager.getString("label.sort_by_score"));
215 final FeatureSettings me = this;
216 scr.addActionListener(new ActionListener()
220 public void actionPerformed(ActionEvent e)
222 me.ap.alignFrame.avc.sortAlignmentByFeatureScore(Arrays
223 .asList(new String[] { type }));
227 MenuItem dens = new MenuItem(
228 MessageManager.getString("label.sort_by_density"));
229 dens.addActionListener(new ActionListener()
233 public void actionPerformed(ActionEvent e)
235 me.ap.alignFrame.avc.sortAlignmentByFeatureDensity(Arrays
236 .asList(new String[] { type }));
243 final float[][] typeMinMax = minmax.get(type);
245 * final java.awt.CheckboxMenuItem chb = new
246 * java.awt.CheckboxMenuItem("Vary Height"); // this is broken at the
247 * moment chb.setState(minmax.get(type) != null);
248 * chb.addActionListener(new ActionListener() {
250 * public void actionPerformed(ActionEvent e) {
251 * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type,
252 * null); } else { minmax.put(type, typeMinMax); } }
256 if (typeMinMax != null && typeMinMax[0] != null)
258 // graduated colourschemes for those where minmax exists for the
259 // positional features
260 MenuItem mxcol = new MenuItem(
261 (typeCol instanceof Color) ? "Graduated Colour"
264 mxcol.addActionListener(new ActionListener()
268 public void actionPerformed(ActionEvent e)
270 if (typeCol instanceof Color)
272 new FeatureColourChooser(me, type);
273 // write back the current colour object to update the table
274 check.updateColor(fr.getFeatureStyle(type));
278 new UserDefinedColours(me, check.type,
279 ((GraduatedColor) typeCol));
286 this.featurePanel.add(men);
287 men.show(this.featurePanel, x, y);
291 public void discoverAllFeatureData()
293 if (fr.getAllFeatureColours() != null
294 && fr.getAllFeatureColours().size() > 0)
303 * rebuilds the group panel
305 public void rebuildGroups()
307 boolean rdrw = false;
308 if (groupPanel == null)
310 groupPanel = new Panel();
315 groupPanel.removeAll();
317 // TODO: JAL-964 - smoothly incorporate new group entries if panel already
318 // displayed and new groups present
319 for (String group : fr.getFeatureGroups())
321 boolean vis = fr.checkGroupVisibility(group, false);
322 Checkbox check = new MyCheckbox(group, vis, false);
323 check.addMouseListener(this);
324 check.setFont(new Font("Serif", Font.BOLD, 12));
325 check.addItemListener(groupItemListener);
326 // note - visibility seems to correlate with displayed. ???wtf ??
327 check.setVisible(vis);
328 groupPanel.add(check);
332 groupPanel.validate();
336 // This routine adds and removes checkboxes depending on
337 // Group selection states
338 void resetTable(boolean groupsChanged)
340 SequenceFeature[] tmpfeatures;
341 String group = null, type;
342 Vector<String> visibleChecks = new Vector<String>();
343 AlignmentI alignment = av.getAlignment();
344 for (int i = 0; i < alignment.getHeight(); i++)
346 if (alignment.getSequenceAt(i).getSequenceFeatures() == null)
351 tmpfeatures = alignment.getSequenceAt(i).getSequenceFeatures();
353 while (index < tmpfeatures.length)
355 group = tmpfeatures[index].featureGroup;
357 if (group == null || fr.checkGroupVisibility(group, true))
359 type = tmpfeatures[index].getType();
360 if (!visibleChecks.contains(type))
362 visibleChecks.addElement(type);
370 int cSize = featurePanel.getComponentCount();
372 // This will remove any checkboxes which shouldn't be
374 for (int i = 0; i < cSize; i++)
376 comps = featurePanel.getComponents();
377 check = (MyCheckbox) comps[i];
378 if (!visibleChecks.contains(check.type))
380 featurePanel.remove(i);
386 if (fr.getRenderOrder() != null)
388 // First add the checks in the previous render order,
389 // in case the window has been closed and reopened
390 List<String> rol = fr.getRenderOrder();
391 for (int ro = rol.size() - 1; ro > -1; ro--)
393 String item = rol.get(ro);
395 if (!visibleChecks.contains(item))
400 visibleChecks.removeElement(item);
402 addCheck(false, item);
406 // now add checkboxes which should be visible,
407 // if they have not already been added
408 Enumeration<String> en = visibleChecks.elements();
410 while (en.hasMoreElements())
412 addCheck(groupsChanged, en.nextElement().toString());
415 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
417 featurePanel.validate();
419 if (scrollPane != null)
421 scrollPane.validate();
424 itemStateChanged(null);
428 * update the checklist of feature types with the given type
430 * @param groupsChanged
431 * true means if the type is not in the display list then it will be
432 * added and displayed
434 * feature type to be checked for in the list.
436 void addCheck(boolean groupsChanged, String type)
439 Component[] comps = featurePanel.getComponents();
442 for (int i = 0; i < featurePanel.getComponentCount(); i++)
444 check = (MyCheckbox) comps[i];
445 if (check.type.equals(type))
454 boolean selected = false;
455 if (groupsChanged || av.getFeaturesDisplayed().isVisible(type))
460 check = new MyCheckbox(type, selected, false,
461 fr.getFeatureStyle(type));
463 check.addMouseListener(this);
464 check.addMouseMotionListener(this);
465 check.addItemListener(this);
468 // add at beginning of stack.
469 featurePanel.add(check, 0);
473 // add at end of stack.
474 featurePanel.add(check);
480 public void actionPerformed(ActionEvent evt)
482 for (int i = 0; i < featurePanel.getComponentCount(); i++)
484 Checkbox check = (Checkbox) featurePanel.getComponent(i);
485 check.setState(!check.getState());
490 private ItemListener groupItemListener = new ItemListener()
493 public void itemStateChanged(ItemEvent evt)
495 Checkbox source = (Checkbox) evt.getSource();
496 fr.setGroupVisibility(source.getLabel(), source.getState());
497 ap.seqPanel.seqCanvas.repaint();
498 if (ap.overviewPanel != null)
500 ap.overviewPanel.updateOverviewImage();
508 public void itemStateChanged(ItemEvent evt)
513 void selectionChanged()
515 Component[] comps = featurePanel.getComponents();
516 int cSize = comps.length;
518 Object[][] tmp = new Object[cSize][3];
520 for (int i = 0; i < cSize; i++)
522 MyCheckbox check = (MyCheckbox) comps[i];
523 tmp[tmpSize][0] = check.type;
524 tmp[tmpSize][1] = fr.getFeatureStyle(check.type);
525 tmp[tmpSize][2] = new Boolean(check.getState());
529 Object[][] data = new Object[tmpSize][3];
530 System.arraycopy(tmp, 0, data, 0, tmpSize);
532 fr.setFeaturePriority(data);
534 ap.paintAlignment(true);
537 MyCheckbox selectedCheck;
539 boolean dragging = false;
542 public void mouseDragged(MouseEvent evt)
544 if (((Component) evt.getSource()).getParent() != featurePanel)
552 public void mouseReleased(MouseEvent evt)
554 if (((Component) evt.getSource()).getParent() != featurePanel)
559 Component comp = null;
560 Checkbox target = null;
562 int height = evt.getY() + evt.getComponent().getLocation().y;
564 if (height > featurePanel.getSize().height)
568 .getComponent(featurePanel.getComponentCount() - 1);
572 comp = featurePanel.getComponent(0);
576 comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
577 + evt.getComponent().getLocation().y);
580 if (comp != null && comp instanceof Checkbox)
582 target = (Checkbox) comp;
585 if (selectedCheck != null && target != null && selectedCheck != target)
587 int targetIndex = -1;
588 for (int i = 0; i < featurePanel.getComponentCount(); i++)
590 if (target == featurePanel.getComponent(i))
597 featurePanel.remove(selectedCheck);
598 featurePanel.add(selectedCheck, targetIndex);
599 featurePanel.validate();
600 itemStateChanged(null);
604 public void setUserColour(String feature, Object originalColour)
606 if (originalColour instanceof Color
607 || originalColour instanceof GraduatedColor)
609 fr.setColour(feature, originalColour);
615 .getString("error.implementation_error_unsupported_feature_colour_object"));
620 public void refreshTable()
622 featurePanel.removeAll();
624 ap.paintAlignment(true);
628 public void mouseEntered(MouseEvent evt)
633 public void mouseExited(MouseEvent evt)
638 public void mouseClicked(MouseEvent evt)
640 MyCheckbox check = (MyCheckbox) evt.getSource();
641 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
643 this.popupSort(check, fr.getMinMax(), evt.getX(), evt.getY());
646 if (check.getParent() != featurePanel)
651 if (evt.getClickCount() > 1)
653 Object fcol = fr.getFeatureStyle(check.type);
654 if (fcol instanceof Color)
656 new UserDefinedColours(this, check.type, (Color) fcol);
660 new FeatureColourChooser(this, check.type);
661 // write back the current colour object to update the table
662 check.updateColor(fr.getFeatureStyle(check.type));
668 public void mouseMoved(MouseEvent evt)
673 public void adjustmentValueChanged(AdjustmentEvent evt)
675 fr.setTransparency((100 - transparency.getValue()) / 100f);
676 ap.seqPanel.seqCanvas.repaint();
680 class MyCheckbox extends Checkbox
684 public int stringWidth;
692 public void updateColor(Object newcol)
694 if (newcol instanceof Color)
696 col = (Color) newcol;
699 else if (newcol instanceof GraduatedColor)
701 gcol = (GraduatedColor) newcol;
708 .getString("error.invalid_colour_for_mycheckbox"));
716 String vlabel = type;
717 if (gcol.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
720 + ((gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) ? "(>)"
723 if (gcol.isColourByLabel())
725 setBackground(Color.white);
726 vlabel += " (by Label)";
730 setBackground(gcol.getMinColor());
732 this.setLabel(vlabel);
737 public MyCheckbox(String label, boolean checked, boolean haslink)
739 super(label, checked);
741 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
742 stringWidth = fm.stringWidth(label);
743 this.hasLink = haslink;
746 public MyCheckbox(String type, boolean selected, boolean b,
749 this(type, selected, b);
750 updateColor(featureStyle);
754 public void paint(Graphics g)
756 Dimension d = getSize();
759 if (gcol.isColourByLabel())
761 g.setColor(Color.white);
762 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
764 * g.setColor(Color.black); Font f=g.getFont().deriveFont(9);
767 * // g.setFont(g.getFont().deriveFont( //
768 * AffineTransform.getScaleInstance( //
769 * width/g.getFontMetrics().stringWidth("Label"), //
770 * height/g.getFontMetrics().getHeight()))); g.drawString("Label",
777 Color maxCol = gcol.getMaxColor();
779 g.fillRect(d.width / 2, 0, d.width / 2, d.height);
786 g.drawImage(linkImage, stringWidth + 25,
787 (getSize().height - linkImage.getHeight(this)) / 2, this);
793 public void mousePressed(MouseEvent e)