2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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.
22 import java.util.concurrent.ConcurrentHashMap;
25 import java.awt.event.*;
26 import java.awt.image.*;
27 import java.beans.PropertyChangeListener;
28 import java.beans.PropertyChangeSupport;
32 import jalview.datamodel.*;
33 import jalview.schemes.GraduatedColor;
34 import jalview.util.MessageManager;
42 public class FeatureRenderer extends jalview.viewmodel.FeatureRenderer implements jalview.api.FeatureRenderer
51 * global transparency for feature
53 float transparency = 1.0f;
59 Map featureColours = new ConcurrentHashMap();
61 // A higher level for grouping features of a
63 Map featureGroups = new ConcurrentHashMap();
65 // This is actually an Integer held in the hashtable,
66 // Retrieved using the key feature type
71 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
76 * Creates a new FeatureRenderer object.
81 public FeatureRenderer(AlignmentPanel ap)
85 if (ap != null && ap.seqPanel != null && ap.seqPanel.seqCanvas != null
86 && ap.seqPanel.seqCanvas.fr != null)
88 transferSettings(ap.seqPanel.seqCanvas.fr);
92 public class FeatureRendererSettings implements Cloneable
104 public FeatureRendererSettings(String[] renderOrder,
105 Hashtable featureGroups, Hashtable featureColours,
106 float transparency, Hashtable featureOrder)
109 this.renderOrder = renderOrder;
110 this.featureGroups = featureGroups;
111 this.featureColours = featureColours;
112 this.transparency = transparency;
113 this.featureOrder = featureOrder;
117 * create an independent instance of the feature renderer settings
121 public FeatureRendererSettings(FeatureRenderer fr)
124 featureGroups = new ConcurrentHashMap();
125 featureColours = new ConcurrentHashMap();
126 featureOrder = new ConcurrentHashMap();
127 if (fr.renderOrder != null)
129 this.renderOrder = new String[fr.renderOrder.length];
130 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
131 fr.renderOrder.length);
133 if (fr.featureGroups != null)
135 this.featureGroups = new ConcurrentHashMap(fr.featureGroups);
137 if (fr.featureColours != null)
139 this.featureColours = new ConcurrentHashMap(fr.featureColours);
141 Iterator en = fr.featureColours.keySet().iterator();
144 Object next = en.next();
145 Object val = featureColours.get(next);
146 if (val instanceof GraduatedColor)
149 .put(next, new GraduatedColor((GraduatedColor) val));
152 this.transparency = fr.transparency;
153 if (fr.featureOrder != null)
155 this.featureOrder = new ConcurrentHashMap(fr.featureOrder);
160 public FeatureRendererSettings getSettings()
162 return new FeatureRendererSettings(this);
165 public void transferSettings(FeatureRendererSettings fr)
167 this.renderOrder = fr.renderOrder;
168 this.featureGroups = fr.featureGroups;
169 this.featureColours = fr.featureColours;
170 this.transparency = fr.transparency;
171 this.featureOrder = fr.featureOrder;
174 * update from another feature renderer
175 * @param fr settings to copy
177 public void transferSettings(jalview.api.FeatureRenderer _fr)
179 FeatureRenderer fr = (FeatureRenderer) _fr;
180 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
181 this.renderOrder = frs.renderOrder;
182 this.featureGroups = frs.featureGroups;
183 this.featureColours = frs.featureColours;
184 this.transparency = frs.transparency;
185 this.featureOrder = frs.featureOrder;
186 if (av != null && av != fr.av)
188 // copy over the displayed feature settings
191 if (fr.av.featuresDisplayed != null)
193 // update display settings
194 if (av.featuresDisplayed == null)
196 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
200 av.featuresDisplayed.clear();
201 Enumeration en = fr.av.featuresDisplayed.keys();
202 while (en.hasMoreElements())
204 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
213 BufferedImage offscreenImage;
215 boolean offscreenRender = false;
217 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
219 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
223 * This is used by the Molecule Viewer and Overview to get the accurate
224 * colourof the rendered sequence
226 public synchronized int findFeatureColour(int initialCol, SequenceI seq,
229 if (!av.showSequenceFeatures)
237 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
238 if (sequenceFeatures != null)
240 sfSize = sequenceFeatures.length;
244 if (sequenceFeatures != lastSeq.getDatasetSequence()
245 .getSequenceFeatures())
247 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
248 if (sequenceFeatures != null)
250 sfSize = sequenceFeatures.length;
254 if (sequenceFeatures == null || sfSize == 0)
259 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
261 return Color.white.getRGB();
264 // Only bother making an offscreen image if transparency is applied
265 if (transparency != 1.0f && offscreenImage == null)
267 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
270 currentColour = null;
271 // TODO: non-threadsafe - each rendering thread needs its own instance of
272 // the feature renderer - or this should be synchronized.
273 offscreenRender = true;
275 if (offscreenImage != null)
277 offscreenImage.setRGB(0, 0, initialCol);
278 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
280 return offscreenImage.getRGB(0, 0);
284 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
286 if (currentColour == null)
292 return ((Integer) currentColour).intValue();
321 // SequenceFeature sf;
324 SequenceFeature[] sequenceFeatures;
326 int sfSize, sfindex, spos, epos;
329 * show scores as heights
331 protected boolean varyHeight = false;
333 synchronized public void drawSequence(Graphics g, SequenceI seq,
334 int start, int end, int y1)
337 if (seq.getDatasetSequence().getSequenceFeatures() == null
338 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
345 fm = g.getFontMetrics();
348 if (av.featuresDisplayed == null || renderOrder == null
352 if (av.featuresDisplayed.size() < 1)
357 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
362 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
365 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
368 if (transparency != 1 && g != null)
370 Graphics2D g2 = (Graphics2D) g;
371 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
375 if (!offscreenRender)
377 spos = lastSeq.findPosition(start);
378 epos = lastSeq.findPosition(end);
381 sfSize = sequenceFeatures.length;
383 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
385 type = renderOrder[renderIndex];
387 if (type == null || !av.featuresDisplayed.containsKey(type))
392 // loop through all features in sequence to find
393 // current feature to render
394 for (sfindex = 0; sfindex < sfSize; sfindex++)
396 if (!sequenceFeatures[sfindex].type.equals(type))
401 if (featureGroups != null
402 && sequenceFeatures[sfindex].featureGroup != null
403 && sequenceFeatures[sfindex].featureGroup.length() != 0
405 .containsKey(sequenceFeatures[sfindex].featureGroup)
406 && !((Boolean) featureGroups
407 .get(sequenceFeatures[sfindex].featureGroup))
414 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
420 if (offscreenRender && offscreenImage == null)
422 if (sequenceFeatures[sfindex].begin <= start
423 && sequenceFeatures[sfindex].end >= start)
425 // this is passed out to the overview and other sequence renderers
426 // (e.g. molecule viewer) to get displayed colour for rendered
428 currentColour = new Integer(
429 getColour(sequenceFeatures[sfindex]).getRGB());
430 // used to be retreived from av.featuresDisplayed
431 // currentColour = av.featuresDisplayed
432 // .get(sequenceFeatures[sfindex].type);
436 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
439 renderFeature(g, seq,
440 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
441 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
442 getColour(sequenceFeatures[sfindex])
443 // new Color(((Integer) av.featuresDisplayed
444 // .get(sequenceFeatures[sfindex].type)).intValue())
446 renderFeature(g, seq,
447 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
448 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
449 getColour(sequenceFeatures[sfindex])
450 // new Color(((Integer) av.featuresDisplayed
451 // .get(sequenceFeatures[sfindex].type)).intValue())
455 else if (showFeature(sequenceFeatures[sfindex]))
457 if (av.showSeqFeaturesHeight
458 && sequenceFeatures[sfindex].score != Float.NaN)
460 renderScoreFeature(g, seq,
461 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
462 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
463 getColour(sequenceFeatures[sfindex]), start, end, y1,
464 normaliseScore(sequenceFeatures[sfindex]));
468 renderFeature(g, seq,
469 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
470 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
471 getColour(sequenceFeatures[sfindex]), start, end, y1);
479 if (transparency != 1.0f && g != null)
481 Graphics2D g2 = (Graphics2D) g;
482 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
487 Hashtable minmax = new Hashtable();
490 * normalise a score against the max/min bounds for the feature type.
492 * @param sequenceFeature
493 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
496 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
498 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
499 final byte[] r = new byte[]
503 if (r[0] != 0 || mm[0] < 0.0)
506 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
510 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
520 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
521 Color featureColour, int start, int end, int y1)
524 if (((fstart <= end) && (fend >= start)))
527 { // fix for if the feature we have starts before the sequence start,
528 fstart = start; // but the feature end is still valid!!
535 int pady = (y1 + av.charHeight) - av.charHeight / 5;
536 for (i = fstart; i <= fend; i++)
538 s = seq.getCharAt(i);
540 if (jalview.util.Comparison.isGap(s))
545 g.setColor(featureColour);
547 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
550 if (offscreenRender || !av.validCharWidth)
555 g.setColor(Color.white);
556 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
557 g.drawString(String.valueOf(s), charOffset
558 + (av.charWidth * (i - start)), pady);
564 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
565 Color featureColour, int start, int end, int y1, byte[] bs)
568 if (((fstart <= end) && (fend >= start)))
571 { // fix for if the feature we have starts before the sequence start,
572 fstart = start; // but the feature end is still valid!!
579 int pady = (y1 + av.charHeight) - av.charHeight / 5;
580 int ystrt = 0, yend = av.charHeight;
583 // signed - zero is always middle of residue line.
586 yend = av.charHeight * (128 - bs[1]) / 512;
587 ystrt = av.charHeight - yend / 2;
591 ystrt = av.charHeight / 2;
592 yend = av.charHeight * (bs[1] - 128) / 512;
597 yend = av.charHeight * bs[1] / 255;
598 ystrt = av.charHeight - yend;
601 for (i = fstart; i <= fend; i++)
603 s = seq.getCharAt(i);
605 if (jalview.util.Comparison.isGap(s))
610 g.setColor(featureColour);
611 int x = (i - start) * av.charWidth;
612 g.drawRect(x, y1, av.charWidth, av.charHeight);
613 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
615 if (offscreenRender || !av.validCharWidth)
620 g.setColor(Color.black);
621 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
622 g.drawString(String.valueOf(s), charOffset
623 + (av.charWidth * (i - start)), pady);
629 boolean newFeatureAdded = false;
632 * Called when alignment in associated view has new/modified features to
633 * discover and display.
636 public void featuresAdded()
642 boolean findingFeatures = false;
645 * search the alignment for all new features, give them a colour and display
646 * them. Then fires a PropertyChangeEvent on the changeSupport object.
649 void findAllFeatures()
651 synchronized (firing)
653 if (firing.equals(Boolean.FALSE))
655 firing = Boolean.TRUE;
656 findAllFeatures(true); // add all new features as visible
657 changeSupport.firePropertyChange("changeSupport", null, null);
658 firing = Boolean.FALSE;
664 * Searches alignment for all features and updates colours
666 * @param newMadeVisible
667 * if true newly added feature types will be rendered immediatly
669 synchronized void findAllFeatures(boolean newMadeVisible)
671 newFeatureAdded = false;
675 newFeatureAdded = true;
679 findingFeatures = true;
681 if (av.featuresDisplayed == null)
683 av.featuresDisplayed = new Hashtable();
686 allfeatures = new Vector();
687 Vector oldfeatures = new Vector();
688 if (renderOrder != null)
690 for (int i = 0; i < renderOrder.length; i++)
692 if (renderOrder[i] != null)
694 oldfeatures.addElement(renderOrder[i]);
700 minmax = new Hashtable();
702 AlignmentI alignment = av.getAlignment();
703 for (int i = 0; i < alignment.getHeight(); i++)
705 SequenceFeature[] features = alignment.getSequenceAt(i)
706 .getDatasetSequence().getSequenceFeatures();
708 if (features == null)
714 while (index < features.length)
716 if (!av.featuresDisplayed.containsKey(features[index].getType()))
719 if (featureGroups.containsKey(features[index].getType()))
721 boolean visible = ((Boolean) featureGroups
722 .get(features[index].featureGroup)).booleanValue();
731 if (!(features[index].begin == 0 && features[index].end == 0))
733 // If beginning and end are 0, the feature is for the whole sequence
734 // and we don't want to render the feature in the normal way
737 && !oldfeatures.contains(features[index].getType()))
739 // this is a new feature type on the alignment. Mark it for
741 av.featuresDisplayed.put(features[index].getType(),
742 new Integer(getColour(features[index].getType())
744 setOrder(features[index].getType(), 0);
748 if (!allfeatures.contains(features[index].getType()))
750 allfeatures.addElement(features[index].getType());
752 if (features[index].score != Float.NaN)
754 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
755 float[][] mm = (float[][]) minmax.get(features[index].getType());
760 minmax.put(features[index].getType(), mm);
762 if (mm[nonpos] == null)
764 mm[nonpos] = new float[]
765 { features[index].score, features[index].score };
770 if (mm[nonpos][0] > features[index].score)
772 mm[nonpos][0] = features[index].score;
774 if (mm[nonpos][1] < features[index].score)
776 mm[nonpos][1] = features[index].score;
783 updateRenderOrder(allfeatures);
784 findingFeatures = false;
787 protected Boolean firing = Boolean.FALSE;
790 * replaces the current renderOrder with the unordered features in
791 * allfeatures. The ordering of any types in both renderOrder and allfeatures
792 * is preserved, and all new feature types are rendered on top of the existing
793 * types, in the order given by getOrder or the order given in allFeatures.
794 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
795 * eliminate the float storage for computing/recalling the persistent ordering
796 * New Cability: updates min/max for colourscheme range if its dynamic
800 private void updateRenderOrder(Vector allFeatures)
802 Vector allfeatures = new Vector(allFeatures);
803 String[] oldRender = renderOrder;
804 renderOrder = new String[allfeatures.size()];
805 Object mmrange, fc = null;
806 boolean initOrders = (featureOrder == null);
808 if (oldRender != null && oldRender.length > 0)
810 for (int j = 0; j < oldRender.length; j++)
812 if (oldRender[j] != null)
816 setOrder(oldRender[j], (1 - (1 + (float) j)
817 / (float) oldRender.length));
819 if (allfeatures.contains(oldRender[j]))
821 renderOrder[opos++] = oldRender[j]; // existing features always
822 // appear below new features
823 allfeatures.removeElement(oldRender[j]);
826 mmrange = minmax.get(oldRender[j]);
829 fc = featureColours.get(oldRender[j]);
830 if (fc != null && fc instanceof GraduatedColor
831 && ((GraduatedColor) fc).isAutoScale())
833 ((GraduatedColor) fc).updateBounds(
834 ((float[][]) mmrange)[0][0],
835 ((float[][]) mmrange)[0][1]);
843 if (allfeatures.size() == 0)
845 // no new features - leave order unchanged.
848 int i = allfeatures.size() - 1;
850 boolean sort = false;
851 String[] newf = new String[allfeatures.size()];
852 float[] sortOrder = new float[allfeatures.size()];
853 Enumeration en = allfeatures.elements();
854 // sort remaining elements
855 while (en.hasMoreElements())
857 newf[i] = en.nextElement().toString();
860 // update from new features minmax if necessary
861 mmrange = minmax.get(newf[i]);
864 fc = featureColours.get(newf[i]);
865 if (fc != null && fc instanceof GraduatedColor
866 && ((GraduatedColor) fc).isAutoScale())
868 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
869 ((float[][]) mmrange)[0][1]);
873 if (initOrders || !featureOrder.containsKey(newf[i]))
875 int denom = initOrders ? allfeatures.size() : featureOrder.size();
876 // new unordered feature - compute persistent ordering at head of
877 // existing features.
878 setOrder(newf[i], i / (float) denom);
880 // set order from newly found feature from persisted ordering.
881 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
884 // only sort if we need to
885 sort = sort || sortOrder[i] > sortOrder[i + 1];
889 if (iSize > 1 && sort)
891 jalview.util.QuickSort.sort(sortOrder, newf);
894 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
898 * get a feature style object for the given type string. Creates a
899 * java.awt.Color for a featureType with no existing colourscheme. TODO:
900 * replace return type with object implementing standard abstract colour/style
904 * @return java.awt.Color or GraduatedColor
906 public Object getFeatureStyle(String featureType)
908 Object fc = featureColours.get(featureType);
911 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
912 Color col = ucs.createColourFromName(featureType);
913 featureColours.put(featureType, fc = col);
919 * return a nominal colour for this feature
922 * @return standard color, or maximum colour for graduated colourscheme
924 public Color getColour(String featureType)
926 Object fc = getFeatureStyle(featureType);
928 if (fc instanceof Color)
934 if (fc instanceof GraduatedColor)
936 return ((GraduatedColor) fc).getMaxColor();
939 throw new Error("Implementation Error: Unrecognised render object "
940 + fc.getClass() + " for features of type " + featureType);
944 * calculate the render colour for a specific feature using current feature
948 * @return render colour for the given feature
950 public Color getColour(SequenceFeature feature)
952 Object fc = getFeatureStyle(feature.getType());
953 if (fc instanceof Color)
959 if (fc instanceof GraduatedColor)
961 return ((GraduatedColor) fc).findColor(feature);
964 throw new Error("Implementation Error: Unrecognised render object "
965 + fc.getClass() + " for features of type " + feature.getType());
968 private boolean showFeature(SequenceFeature sequenceFeature)
970 Object fc = getFeatureStyle(sequenceFeature.type);
971 if (fc instanceof GraduatedColor)
973 return ((GraduatedColor) fc).isColored(sequenceFeature);
982 // // Feature Editing Dialog
983 // // Will be refactored in next release.
985 static String lastFeatureAdded;
987 static String lastFeatureGroupAdded;
989 static String lastDescriptionAdded;
993 int featureIndex = 0;
995 boolean amendFeatures(final SequenceI[] sequences,
996 final SequenceFeature[] features, boolean newFeatures,
997 final AlignmentPanel ap)
1002 final JPanel bigPanel = new JPanel(new BorderLayout());
1003 final JComboBox overlaps;
1004 final JTextField name = new JTextField(25);
1005 final JTextField source = new JTextField(25);
1006 final JTextArea description = new JTextArea(3, 25);
1007 final JSpinner start = new JSpinner();
1008 final JSpinner end = new JSpinner();
1009 start.setPreferredSize(new Dimension(80, 20));
1010 end.setPreferredSize(new Dimension(80, 20));
1011 final FeatureRenderer me = this;
1012 final JLabel colour = new JLabel();
1013 colour.setOpaque(true);
1014 // colour.setBorder(BorderFactory.createEtchedBorder());
1015 colour.setMaximumSize(new Dimension(30, 16));
1016 colour.addMouseListener(new MouseAdapter()
1018 FeatureColourChooser fcc = null;
1020 public void mousePressed(MouseEvent evt)
1022 if (fcol instanceof Color)
1024 Color col = JColorChooser.showDialog(Desktop.desktop,
1025 "Select Feature Colour", ((Color) fcol));
1029 updateColourButton(bigPanel, colour, col);
1037 final String type = features[featureIndex].getType();
1038 fcc = new FeatureColourChooser(me, type);
1039 fcc.setRequestFocusEnabled(true);
1042 fcc.addActionListener(new ActionListener()
1045 public void actionPerformed(ActionEvent e)
1047 fcol = fcc.getLastColour();
1049 setColour(type, fcol);
1050 updateColourButton(bigPanel, colour, fcol);
1058 JPanel tmp = new JPanel();
1059 JPanel panel = new JPanel(new GridLayout(3, 1));
1061 // /////////////////////////////////////
1062 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1063 if (!newFeatures && features.length > 1)
1065 panel = new JPanel(new GridLayout(4, 1));
1067 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
1068 overlaps = new JComboBox();
1069 for (int i = 0; i < features.length; i++)
1071 overlaps.addItem(features[i].getType() + "/"
1072 + features[i].getBegin() + "-" + features[i].getEnd()
1073 + " (" + features[i].getFeatureGroup() + ")");
1078 overlaps.addItemListener(new ItemListener()
1080 public void itemStateChanged(ItemEvent e)
1082 int index = overlaps.getSelectedIndex();
1085 featureIndex = index;
1086 name.setText(features[index].getType());
1087 description.setText(features[index].getDescription());
1088 source.setText(features[index].getFeatureGroup());
1089 start.setValue(new Integer(features[index].getBegin()));
1090 end.setValue(new Integer(features[index].getEnd()));
1092 SearchResults highlight = new SearchResults();
1093 highlight.addResult(sequences[0], features[index].getBegin(),
1094 features[index].getEnd());
1096 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1099 Object col = getFeatureStyle(name.getText());
1102 col = new jalview.schemes.UserColourScheme()
1103 .createColourFromName(name.getText());
1105 oldcol = fcol = col;
1106 updateColourButton(bigPanel, colour, col);
1113 // ////////////////////////////////////
1117 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
1122 tmp.add(new JLabel(MessageManager.getString("label.group")+":", JLabel.RIGHT));
1127 tmp.add(new JLabel(MessageManager.getString("label.colour"), JLabel.RIGHT));
1129 colour.setPreferredSize(new Dimension(150, 15));
1130 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1131 colour.setForeground(Color.black);
1132 colour.setHorizontalAlignment(SwingConstants.CENTER);
1133 colour.setVerticalAlignment(SwingConstants.CENTER);
1134 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1135 colour.setVerticalTextPosition(SwingConstants.CENTER);
1136 bigPanel.add(panel, BorderLayout.NORTH);
1138 panel = new JPanel();
1139 panel.add(new JLabel(MessageManager.getString("label.description"), JLabel.RIGHT));
1140 description.setFont(JvSwingUtils.getTextAreaFont());
1141 description.setLineWrap(true);
1142 panel.add(new JScrollPane(description));
1146 bigPanel.add(panel, BorderLayout.SOUTH);
1148 panel = new JPanel();
1149 panel.add(new JLabel(MessageManager.getString("label.start"), JLabel.RIGHT));
1151 panel.add(new JLabel(MessageManager.getString("label.end"), JLabel.RIGHT));
1153 bigPanel.add(panel, BorderLayout.CENTER);
1157 bigPanel.add(panel, BorderLayout.CENTER);
1160 if (lastFeatureAdded == null)
1162 if (features[0].type != null)
1164 lastFeatureAdded = features[0].type;
1168 lastFeatureAdded = "feature_1";
1172 if (lastFeatureGroupAdded == null)
1174 if (features[0].featureGroup != null)
1176 lastFeatureGroupAdded = features[0].featureGroup;
1180 lastFeatureGroupAdded = "Jalview";
1186 name.setText(lastFeatureAdded);
1187 source.setText(lastFeatureGroupAdded);
1191 name.setText(features[0].getType());
1192 source.setText(features[0].getFeatureGroup());
1195 start.setValue(new Integer(features[0].getBegin()));
1196 end.setValue(new Integer(features[0].getEnd()));
1197 description.setText(features[0].getDescription());
1198 updateColourButton(bigPanel, colour,
1199 (oldcol = fcol = getFeatureStyle(name.getText())));
1203 options = new Object[]
1204 { "Amend", "Delete", "Cancel" };
1208 options = new Object[]
1212 String title = newFeatures ? "Create New Sequence Feature(s)"
1213 : "Amend/Delete Features for " + sequences[0].getName();
1215 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1216 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1217 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1219 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1221 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1223 // This ensures that the last sequence
1224 // is refreshed and new features are rendered
1226 lastFeatureAdded = name.getText().trim();
1227 lastFeatureGroupAdded = source.getText().trim();
1228 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1229 // TODO: determine if the null feature group is valid
1230 if (lastFeatureGroupAdded.length() < 1)
1231 lastFeatureGroupAdded = null;
1236 SequenceFeature sf = features[featureIndex];
1238 if (reply == JOptionPane.NO_OPTION)
1240 sequences[0].getDatasetSequence().deleteFeature(sf);
1242 else if (reply == JOptionPane.YES_OPTION)
1244 sf.type = lastFeatureAdded;
1245 sf.featureGroup = lastFeatureGroupAdded;
1246 sf.description = lastDescriptionAdded;
1248 setColour(sf.type, fcol);
1249 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1253 sf.begin = ((Integer) start.getValue()).intValue();
1254 sf.end = ((Integer) end.getValue()).intValue();
1255 } catch (NumberFormatException ex)
1259 ffile.parseDescriptionHTML(sf, false);
1263 // NEW FEATURES ADDED
1265 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1267 for (int i = 0; i < sequences.length; i++)
1269 features[i].type = lastFeatureAdded;
1270 if (lastFeatureGroupAdded != null)
1271 features[i].featureGroup = lastFeatureGroupAdded;
1272 features[i].description = lastDescriptionAdded;
1273 sequences[i].addSequenceFeature(features[i]);
1274 ffile.parseDescriptionHTML(features[i], false);
1277 if (av.featuresDisplayed == null)
1279 av.featuresDisplayed = new Hashtable();
1282 if (lastFeatureGroupAdded != null)
1284 if (featureGroups == null)
1285 featureGroups = new Hashtable();
1286 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1288 setColour(lastFeatureAdded, fcol);
1289 av.featuresDisplayed.put(lastFeatureAdded,
1290 getColour(lastFeatureAdded));
1292 findAllFeatures(false);
1294 ap.paintAlignment(true);
1304 ap.paintAlignment(true);
1310 * update the amend feature button dependent on the given style
1316 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1320 colour.setIcon(null);
1321 colour.setToolTipText(null);
1324 if (col2 instanceof Color)
1326 colour.setBackground((Color) col2);
1330 colour.setBackground(bigPanel.getBackground());
1331 colour.setForeground(Color.black);
1332 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1333 // colour.setForeground(colour.getBackground());
1337 public void setColour(String featureType, Object col)
1340 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1341 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1342 // Object c = featureColours.get(featureType);
1343 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1344 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1346 featureColours.put(featureType, col);
1350 public void setTransparency(float value)
1352 transparency = value;
1355 public float getTransparency()
1357 return transparency;
1361 * Replace current ordering with new ordering
1364 * { String(Type), Colour(Type), Boolean(Displayed) }
1366 public void setFeaturePriority(Object[][] data)
1368 setFeaturePriority(data, true);
1374 * { String(Type), Colour(Type), Boolean(Displayed) }
1376 * when true current featureDisplay list will be cleared
1378 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1382 if (av.featuresDisplayed != null)
1384 av.featuresDisplayed.clear();
1388 av.featuresDisplayed = new Hashtable();
1396 // The feature table will display high priority
1397 // features at the top, but theses are the ones
1398 // we need to render last, so invert the data
1399 renderOrder = new String[data.length];
1401 if (data.length > 0)
1403 for (int i = 0; i < data.length; i++)
1405 String type = data[i][0].toString();
1406 setColour(type, data[i][1]); // todo : typesafety - feature color
1408 if (((Boolean) data[i][2]).booleanValue())
1410 av.featuresDisplayed.put(type, new Integer(getColour(type)
1414 renderOrder[data.length - i - 1] = type;
1420 Map featureOrder = null;
1423 * analogous to colour - store a normalized ordering for all feature types in
1424 * this rendering context.
1427 * Feature type string
1429 * normalized priority - 0 means always appears on top, 1 means
1432 public float setOrder(String type, float position)
1434 if (featureOrder == null)
1436 featureOrder = new Hashtable();
1438 featureOrder.put(type, new Float(position));
1443 * get the global priority (0 (top) to 1 (bottom))
1446 * @return [0,1] or -1 for a type without a priority
1448 public float getOrder(String type)
1450 if (featureOrder != null)
1452 if (featureOrder.containsKey(type))
1454 return ((Float) featureOrder.get(type)).floatValue();
1462 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1464 public void addPropertyChangeListener(PropertyChangeListener listener)
1466 changeSupport.addPropertyChangeListener(listener);
1471 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1473 public void removePropertyChangeListener(PropertyChangeListener listener)
1475 changeSupport.removePropertyChangeListener(listener);