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.
24 import java.util.concurrent.ConcurrentHashMap;
27 import java.awt.event.*;
28 import java.awt.image.*;
29 import java.beans.PropertyChangeListener;
30 import java.beans.PropertyChangeSupport;
34 import jalview.datamodel.*;
35 import jalview.schemes.GraduatedColor;
36 import jalview.util.MessageManager;
44 public class FeatureRenderer implements jalview.api.FeatureRenderer
53 * global transparency for feature
55 float transparency = 1.0f;
61 Map featureColours = new ConcurrentHashMap();
63 // A higher level for grouping features of a
65 Map featureGroups = new ConcurrentHashMap();
67 // This is actually an Integer held in the hashtable,
68 // Retrieved using the key feature type
73 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
78 * Creates a new FeatureRenderer object.
83 public FeatureRenderer(AlignmentPanel ap)
87 if (ap != null && ap.seqPanel != null && ap.seqPanel.seqCanvas != null
88 && ap.seqPanel.seqCanvas.fr != null)
90 transferSettings(ap.seqPanel.seqCanvas.fr);
94 public class FeatureRendererSettings implements Cloneable
106 public FeatureRendererSettings(String[] renderOrder,
107 Hashtable featureGroups, Hashtable featureColours,
108 float transparency, Hashtable featureOrder)
111 this.renderOrder = renderOrder;
112 this.featureGroups = featureGroups;
113 this.featureColours = featureColours;
114 this.transparency = transparency;
115 this.featureOrder = featureOrder;
119 * create an independent instance of the feature renderer settings
123 public FeatureRendererSettings(FeatureRenderer fr)
126 featureGroups = new ConcurrentHashMap();
127 featureColours = new ConcurrentHashMap();
128 featureOrder = new ConcurrentHashMap();
129 if (fr.renderOrder != null)
131 this.renderOrder = new String[fr.renderOrder.length];
132 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
133 fr.renderOrder.length);
135 if (fr.featureGroups != null)
137 this.featureGroups = new ConcurrentHashMap(fr.featureGroups);
139 if (fr.featureColours != null)
141 this.featureColours = new ConcurrentHashMap(fr.featureColours);
143 Iterator en = fr.featureColours.keySet().iterator();
146 Object next = en.next();
147 Object val = featureColours.get(next);
148 if (val instanceof GraduatedColor)
151 .put(next, new GraduatedColor((GraduatedColor) val));
154 this.transparency = fr.transparency;
155 if (fr.featureOrder != null)
157 this.featureOrder = new ConcurrentHashMap(fr.featureOrder);
162 public FeatureRendererSettings getSettings()
164 return new FeatureRendererSettings(this);
167 public void transferSettings(FeatureRendererSettings fr)
169 this.renderOrder = fr.renderOrder;
170 this.featureGroups = fr.featureGroups;
171 this.featureColours = fr.featureColours;
172 this.transparency = fr.transparency;
173 this.featureOrder = fr.featureOrder;
177 * update from another feature renderer
182 public void transferSettings(FeatureRenderer fr)
184 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
185 this.renderOrder = frs.renderOrder;
186 this.featureGroups = frs.featureGroups;
187 this.featureColours = frs.featureColours;
188 this.transparency = frs.transparency;
189 this.featureOrder = frs.featureOrder;
190 if (av != null && av != fr.av)
192 // copy over the displayed feature settings
195 if (fr.av.featuresDisplayed != null)
197 // update display settings
198 if (av.featuresDisplayed == null)
200 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
204 av.featuresDisplayed.clear();
205 Enumeration en = fr.av.featuresDisplayed.keys();
206 while (en.hasMoreElements())
208 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
217 BufferedImage offscreenImage;
219 boolean offscreenRender = false;
221 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
223 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
227 * This is used by the Molecule Viewer and Overview to get the accurate
228 * colourof the rendered sequence
230 public synchronized int findFeatureColour(int initialCol, SequenceI seq,
233 if (!av.showSequenceFeatures)
241 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
242 if (sequenceFeatures != null)
244 sfSize = sequenceFeatures.length;
248 if (sequenceFeatures != lastSeq.getDatasetSequence()
249 .getSequenceFeatures())
251 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
252 if (sequenceFeatures != null)
254 sfSize = sequenceFeatures.length;
258 if (sequenceFeatures == null || sfSize == 0)
263 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
265 return Color.white.getRGB();
268 // Only bother making an offscreen image if transparency is applied
269 if (transparency != 1.0f && offscreenImage == null)
271 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
274 currentColour = null;
275 // TODO: non-threadsafe - each rendering thread needs its own instance of
276 // the feature renderer - or this should be synchronized.
277 offscreenRender = true;
279 if (offscreenImage != null)
281 offscreenImage.setRGB(0, 0, initialCol);
282 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
284 return offscreenImage.getRGB(0, 0);
288 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
290 if (currentColour == null)
296 return ((Integer) currentColour).intValue();
325 // SequenceFeature sf;
328 SequenceFeature[] sequenceFeatures;
330 int sfSize, sfindex, spos, epos;
333 * show scores as heights
335 protected boolean varyHeight = false;
337 synchronized public void drawSequence(Graphics g, SequenceI seq,
338 int start, int end, int y1)
341 if (seq.getDatasetSequence().getSequenceFeatures() == null
342 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
349 fm = g.getFontMetrics();
352 if (av.featuresDisplayed == null || renderOrder == null
356 if (av.featuresDisplayed.size() < 1)
361 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
366 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
369 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
372 if (transparency != 1 && g != null)
374 Graphics2D g2 = (Graphics2D) g;
375 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
379 if (!offscreenRender)
381 spos = lastSeq.findPosition(start);
382 epos = lastSeq.findPosition(end);
385 sfSize = sequenceFeatures.length;
387 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
389 type = renderOrder[renderIndex];
391 if (type == null || !av.featuresDisplayed.containsKey(type))
396 // loop through all features in sequence to find
397 // current feature to render
398 for (sfindex = 0; sfindex < sfSize; sfindex++)
400 if (!sequenceFeatures[sfindex].type.equals(type))
405 if (featureGroups != null
406 && sequenceFeatures[sfindex].featureGroup != null
407 && sequenceFeatures[sfindex].featureGroup.length() != 0
409 .containsKey(sequenceFeatures[sfindex].featureGroup)
410 && !((Boolean) featureGroups
411 .get(sequenceFeatures[sfindex].featureGroup))
418 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
424 if (offscreenRender && offscreenImage == null)
426 if (sequenceFeatures[sfindex].begin <= start
427 && sequenceFeatures[sfindex].end >= start)
429 // this is passed out to the overview and other sequence renderers
430 // (e.g. molecule viewer) to get displayed colour for rendered
432 currentColour = new Integer(
433 getColour(sequenceFeatures[sfindex]).getRGB());
434 // used to be retreived from av.featuresDisplayed
435 // currentColour = av.featuresDisplayed
436 // .get(sequenceFeatures[sfindex].type);
440 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
443 renderFeature(g, seq,
444 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
445 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
446 getColour(sequenceFeatures[sfindex])
447 // new Color(((Integer) av.featuresDisplayed
448 // .get(sequenceFeatures[sfindex].type)).intValue())
450 renderFeature(g, seq,
451 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
452 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
453 getColour(sequenceFeatures[sfindex])
454 // new Color(((Integer) av.featuresDisplayed
455 // .get(sequenceFeatures[sfindex].type)).intValue())
459 else if (showFeature(sequenceFeatures[sfindex]))
461 if (av.showSeqFeaturesHeight
462 && sequenceFeatures[sfindex].score != Float.NaN)
464 renderScoreFeature(g, seq,
465 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
466 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
467 getColour(sequenceFeatures[sfindex]), start, end, y1,
468 normaliseScore(sequenceFeatures[sfindex]));
472 renderFeature(g, seq,
473 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
474 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
475 getColour(sequenceFeatures[sfindex]), start, end, y1);
483 if (transparency != 1.0f && g != null)
485 Graphics2D g2 = (Graphics2D) g;
486 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
491 Hashtable minmax = new Hashtable();
494 * normalise a score against the max/min bounds for the feature type.
496 * @param sequenceFeature
497 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
500 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
502 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
503 final byte[] r = new byte[]
507 if (r[0] != 0 || mm[0] < 0.0)
510 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
514 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
524 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
525 Color featureColour, int start, int end, int y1)
528 if (((fstart <= end) && (fend >= start)))
531 { // fix for if the feature we have starts before the sequence start,
532 fstart = start; // but the feature end is still valid!!
539 int pady = (y1 + av.charHeight) - av.charHeight / 5;
540 for (i = fstart; i <= fend; i++)
542 s = seq.getCharAt(i);
544 if (jalview.util.Comparison.isGap(s))
549 g.setColor(featureColour);
551 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
554 if (offscreenRender || !av.validCharWidth)
559 g.setColor(Color.white);
560 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
561 g.drawString(String.valueOf(s), charOffset
562 + (av.charWidth * (i - start)), pady);
568 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
569 Color featureColour, int start, int end, int y1, byte[] bs)
572 if (((fstart <= end) && (fend >= start)))
575 { // fix for if the feature we have starts before the sequence start,
576 fstart = start; // but the feature end is still valid!!
583 int pady = (y1 + av.charHeight) - av.charHeight / 5;
584 int ystrt = 0, yend = av.charHeight;
587 // signed - zero is always middle of residue line.
590 yend = av.charHeight * (128 - bs[1]) / 512;
591 ystrt = av.charHeight - yend / 2;
595 ystrt = av.charHeight / 2;
596 yend = av.charHeight * (bs[1] - 128) / 512;
601 yend = av.charHeight * bs[1] / 255;
602 ystrt = av.charHeight - yend;
605 for (i = fstart; i <= fend; i++)
607 s = seq.getCharAt(i);
609 if (jalview.util.Comparison.isGap(s))
614 g.setColor(featureColour);
615 int x = (i - start) * av.charWidth;
616 g.drawRect(x, y1, av.charWidth, av.charHeight);
617 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
619 if (offscreenRender || !av.validCharWidth)
624 g.setColor(Color.black);
625 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
626 g.drawString(String.valueOf(s), charOffset
627 + (av.charWidth * (i - start)), pady);
633 boolean newFeatureAdded = false;
636 * Called when alignment in associated view has new/modified features to
637 * discover and display.
640 public void featuresAdded()
646 boolean findingFeatures = false;
649 * search the alignment for all new features, give them a colour and display
650 * them. Then fires a PropertyChangeEvent on the changeSupport object.
653 void findAllFeatures()
655 synchronized (firing)
657 if (firing.equals(Boolean.FALSE))
659 firing = Boolean.TRUE;
660 findAllFeatures(true); // add all new features as visible
661 changeSupport.firePropertyChange("changeSupport", null, null);
662 firing = Boolean.FALSE;
668 * Searches alignment for all features and updates colours
670 * @param newMadeVisible
671 * if true newly added feature types will be rendered immediatly
673 synchronized void findAllFeatures(boolean newMadeVisible)
675 newFeatureAdded = false;
679 newFeatureAdded = true;
683 findingFeatures = true;
685 if (av.featuresDisplayed == null)
687 av.featuresDisplayed = new Hashtable();
690 allfeatures = new Vector();
691 Vector oldfeatures = new Vector();
692 if (renderOrder != null)
694 for (int i = 0; i < renderOrder.length; i++)
696 if (renderOrder[i] != null)
698 oldfeatures.addElement(renderOrder[i]);
704 minmax = new Hashtable();
706 AlignmentI alignment = av.getAlignment();
707 for (int i = 0; i < alignment.getHeight(); i++)
709 SequenceFeature[] features = alignment.getSequenceAt(i)
710 .getDatasetSequence().getSequenceFeatures();
712 if (features == null)
718 while (index < features.length)
720 if (!av.featuresDisplayed.containsKey(features[index].getType()))
723 if (featureGroups.containsKey(features[index].getType()))
725 boolean visible = ((Boolean) featureGroups
726 .get(features[index].featureGroup)).booleanValue();
735 if (!(features[index].begin == 0 && features[index].end == 0))
737 // If beginning and end are 0, the feature is for the whole sequence
738 // and we don't want to render the feature in the normal way
741 && !oldfeatures.contains(features[index].getType()))
743 // this is a new feature type on the alignment. Mark it for
745 av.featuresDisplayed.put(features[index].getType(),
746 new Integer(getColour(features[index].getType())
748 setOrder(features[index].getType(), 0);
752 if (!allfeatures.contains(features[index].getType()))
754 allfeatures.addElement(features[index].getType());
756 if (features[index].score != Float.NaN)
758 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
759 float[][] mm = (float[][]) minmax.get(features[index].getType());
764 minmax.put(features[index].getType(), mm);
766 if (mm[nonpos] == null)
768 mm[nonpos] = new float[]
769 { features[index].score, features[index].score };
774 if (mm[nonpos][0] > features[index].score)
776 mm[nonpos][0] = features[index].score;
778 if (mm[nonpos][1] < features[index].score)
780 mm[nonpos][1] = features[index].score;
787 updateRenderOrder(allfeatures);
788 findingFeatures = false;
791 protected Boolean firing = Boolean.FALSE;
794 * replaces the current renderOrder with the unordered features in
795 * allfeatures. The ordering of any types in both renderOrder and allfeatures
796 * is preserved, and all new feature types are rendered on top of the existing
797 * types, in the order given by getOrder or the order given in allFeatures.
798 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
799 * eliminate the float storage for computing/recalling the persistent ordering
800 * New Cability: updates min/max for colourscheme range if its dynamic
804 private void updateRenderOrder(Vector allFeatures)
806 Vector allfeatures = new Vector(allFeatures);
807 String[] oldRender = renderOrder;
808 renderOrder = new String[allfeatures.size()];
809 Object mmrange, fc = null;
810 boolean initOrders = (featureOrder == null);
812 if (oldRender != null && oldRender.length > 0)
814 for (int j = 0; j < oldRender.length; j++)
816 if (oldRender[j] != null)
820 setOrder(oldRender[j], (1 - (1 + (float) j)
821 / (float) oldRender.length));
823 if (allfeatures.contains(oldRender[j]))
825 renderOrder[opos++] = oldRender[j]; // existing features always
826 // appear below new features
827 allfeatures.removeElement(oldRender[j]);
830 mmrange = minmax.get(oldRender[j]);
833 fc = featureColours.get(oldRender[j]);
834 if (fc != null && fc instanceof GraduatedColor
835 && ((GraduatedColor) fc).isAutoScale())
837 ((GraduatedColor) fc).updateBounds(
838 ((float[][]) mmrange)[0][0],
839 ((float[][]) mmrange)[0][1]);
847 if (allfeatures.size() == 0)
849 // no new features - leave order unchanged.
852 int i = allfeatures.size() - 1;
854 boolean sort = false;
855 String[] newf = new String[allfeatures.size()];
856 float[] sortOrder = new float[allfeatures.size()];
857 Enumeration en = allfeatures.elements();
858 // sort remaining elements
859 while (en.hasMoreElements())
861 newf[i] = en.nextElement().toString();
864 // update from new features minmax if necessary
865 mmrange = minmax.get(newf[i]);
868 fc = featureColours.get(newf[i]);
869 if (fc != null && fc instanceof GraduatedColor
870 && ((GraduatedColor) fc).isAutoScale())
872 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
873 ((float[][]) mmrange)[0][1]);
877 if (initOrders || !featureOrder.containsKey(newf[i]))
879 int denom = initOrders ? allfeatures.size() : featureOrder.size();
880 // new unordered feature - compute persistent ordering at head of
881 // existing features.
882 setOrder(newf[i], i / (float) denom);
884 // set order from newly found feature from persisted ordering.
885 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
888 // only sort if we need to
889 sort = sort || sortOrder[i] > sortOrder[i + 1];
893 if (iSize > 1 && sort)
895 jalview.util.QuickSort.sort(sortOrder, newf);
898 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
902 * get a feature style object for the given type string. Creates a
903 * java.awt.Color for a featureType with no existing colourscheme. TODO:
904 * replace return type with object implementing standard abstract colour/style
908 * @return java.awt.Color or GraduatedColor
910 public Object getFeatureStyle(String featureType)
912 Object fc = featureColours.get(featureType);
915 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
916 Color col = ucs.createColourFromName(featureType);
917 featureColours.put(featureType, fc = col);
923 * return a nominal colour for this feature
926 * @return standard color, or maximum colour for graduated colourscheme
928 public Color getColour(String featureType)
930 Object fc = getFeatureStyle(featureType);
932 if (fc instanceof Color)
938 if (fc instanceof GraduatedColor)
940 return ((GraduatedColor) fc).getMaxColor();
943 throw new Error(MessageManager.formatMessage("error.implementation_error_unrecognised_render_object_for_features_type", new String[]{fc.getClass().toString(),featureType}));
947 * calculate the render colour for a specific feature using current feature
951 * @return render colour for the given feature
953 public Color getColour(SequenceFeature feature)
955 Object fc = getFeatureStyle(feature.getType());
956 if (fc instanceof Color)
962 if (fc instanceof GraduatedColor)
964 return ((GraduatedColor) fc).findColor(feature);
967 throw new Error(MessageManager.formatMessage("error.implementation_error_unrecognised_render_object_for_features_type", new String[]{fc.getClass().toString(),feature.getType()}));
970 private boolean showFeature(SequenceFeature sequenceFeature)
972 Object fc = getFeatureStyle(sequenceFeature.type);
973 if (fc instanceof GraduatedColor)
975 return ((GraduatedColor) fc).isColored(sequenceFeature);
984 // // Feature Editing Dialog
985 // // Will be refactored in next release.
987 static String lastFeatureAdded;
989 static String lastFeatureGroupAdded;
991 static String lastDescriptionAdded;
995 int featureIndex = 0;
997 boolean amendFeatures(final SequenceI[] sequences,
998 final SequenceFeature[] features, boolean newFeatures,
999 final AlignmentPanel ap)
1004 final JPanel bigPanel = new JPanel(new BorderLayout());
1005 final JComboBox overlaps;
1006 final JTextField name = new JTextField(25);
1007 final JTextField source = new JTextField(25);
1008 final JTextArea description = new JTextArea(3, 25);
1009 final JSpinner start = new JSpinner();
1010 final JSpinner end = new JSpinner();
1011 start.setPreferredSize(new Dimension(80, 20));
1012 end.setPreferredSize(new Dimension(80, 20));
1013 final FeatureRenderer me = this;
1014 final JLabel colour = new JLabel();
1015 colour.setOpaque(true);
1016 // colour.setBorder(BorderFactory.createEtchedBorder());
1017 colour.setMaximumSize(new Dimension(30, 16));
1018 colour.addMouseListener(new MouseAdapter()
1020 FeatureColourChooser fcc = null;
1022 public void mousePressed(MouseEvent evt)
1024 if (fcol instanceof Color)
1026 Color col = JColorChooser.showDialog(Desktop.desktop,
1027 MessageManager.getString("label.select_feature_colour"), ((Color) fcol));
1031 updateColourButton(bigPanel, colour, col);
1039 final String type = features[featureIndex].getType();
1040 fcc = new FeatureColourChooser(me, type);
1041 fcc.setRequestFocusEnabled(true);
1044 fcc.addActionListener(new ActionListener()
1047 public void actionPerformed(ActionEvent e)
1049 fcol = fcc.getLastColour();
1051 setColour(type, fcol);
1052 updateColourButton(bigPanel, colour, fcol);
1060 JPanel tmp = new JPanel();
1061 JPanel panel = new JPanel(new GridLayout(3, 1));
1063 // /////////////////////////////////////
1064 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1065 if (!newFeatures && features.length > 1)
1067 panel = new JPanel(new GridLayout(4, 1));
1069 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
1070 overlaps = new JComboBox();
1071 for (int i = 0; i < features.length; i++)
1073 overlaps.addItem(features[i].getType() + "/"
1074 + features[i].getBegin() + "-" + features[i].getEnd()
1075 + " (" + features[i].getFeatureGroup() + ")");
1080 overlaps.addItemListener(new ItemListener()
1082 public void itemStateChanged(ItemEvent e)
1084 int index = overlaps.getSelectedIndex();
1087 featureIndex = index;
1088 name.setText(features[index].getType());
1089 description.setText(features[index].getDescription());
1090 source.setText(features[index].getFeatureGroup());
1091 start.setValue(new Integer(features[index].getBegin()));
1092 end.setValue(new Integer(features[index].getEnd()));
1094 SearchResults highlight = new SearchResults();
1095 highlight.addResult(sequences[0], features[index].getBegin(),
1096 features[index].getEnd());
1098 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1101 Object col = getFeatureStyle(name.getText());
1104 col = new jalview.schemes.UserColourScheme()
1105 .createColourFromName(name.getText());
1107 oldcol = fcol = col;
1108 updateColourButton(bigPanel, colour, col);
1115 // ////////////////////////////////////
1119 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
1124 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
1130 tmp.add(new JLabel(MessageManager.getString("label.colour"),
1133 colour.setPreferredSize(new Dimension(150, 15));
1134 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1135 colour.setForeground(Color.black);
1136 colour.setHorizontalAlignment(SwingConstants.CENTER);
1137 colour.setVerticalAlignment(SwingConstants.CENTER);
1138 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1139 colour.setVerticalTextPosition(SwingConstants.CENTER);
1140 bigPanel.add(panel, BorderLayout.NORTH);
1142 panel = new JPanel();
1143 panel.add(new JLabel(MessageManager.getString("label.description"),
1145 description.setFont(JvSwingUtils.getTextAreaFont());
1146 description.setLineWrap(true);
1147 panel.add(new JScrollPane(description));
1151 bigPanel.add(panel, BorderLayout.SOUTH);
1153 panel = new JPanel();
1154 panel.add(new JLabel(MessageManager.getString("label.start"),
1157 panel.add(new JLabel(MessageManager.getString("label.end"),
1160 bigPanel.add(panel, BorderLayout.CENTER);
1164 bigPanel.add(panel, BorderLayout.CENTER);
1167 if (lastFeatureAdded == null)
1169 if (features[0].type != null)
1171 lastFeatureAdded = features[0].type;
1175 lastFeatureAdded = "feature_1";
1179 if (lastFeatureGroupAdded == null)
1181 if (features[0].featureGroup != null)
1183 lastFeatureGroupAdded = features[0].featureGroup;
1187 lastFeatureGroupAdded = "Jalview";
1193 name.setText(lastFeatureAdded);
1194 source.setText(lastFeatureGroupAdded);
1198 name.setText(features[0].getType());
1199 source.setText(features[0].getFeatureGroup());
1202 start.setValue(new Integer(features[0].getBegin()));
1203 end.setValue(new Integer(features[0].getEnd()));
1204 description.setText(features[0].getDescription());
1205 updateColourButton(bigPanel, colour,
1206 (oldcol = fcol = getFeatureStyle(name.getText())));
1210 options = new Object[]
1211 { "Amend", "Delete", "Cancel" };
1215 options = new Object[]
1219 String title = newFeatures ? MessageManager.getString("label.create_new_sequence_features")
1220 : MessageManager.formatMessage("label.amend_delete_features", new String[]{sequences[0].getName()});
1222 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1223 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1224 JOptionPane.QUESTION_MESSAGE, null, options, MessageManager.getString("action.ok"));
1226 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1228 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1230 // This ensures that the last sequence
1231 // is refreshed and new features are rendered
1233 lastFeatureAdded = name.getText().trim();
1234 lastFeatureGroupAdded = source.getText().trim();
1235 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1236 // TODO: determine if the null feature group is valid
1237 if (lastFeatureGroupAdded.length() < 1)
1238 lastFeatureGroupAdded = null;
1243 SequenceFeature sf = features[featureIndex];
1245 if (reply == JOptionPane.NO_OPTION)
1247 sequences[0].getDatasetSequence().deleteFeature(sf);
1249 else if (reply == JOptionPane.YES_OPTION)
1251 sf.type = lastFeatureAdded;
1252 sf.featureGroup = lastFeatureGroupAdded;
1253 sf.description = lastDescriptionAdded;
1255 setColour(sf.type, fcol);
1256 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1260 sf.begin = ((Integer) start.getValue()).intValue();
1261 sf.end = ((Integer) end.getValue()).intValue();
1262 } catch (NumberFormatException ex)
1266 ffile.parseDescriptionHTML(sf, false);
1270 // NEW FEATURES ADDED
1272 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1274 for (int i = 0; i < sequences.length; i++)
1276 features[i].type = lastFeatureAdded;
1277 if (lastFeatureGroupAdded != null)
1278 features[i].featureGroup = lastFeatureGroupAdded;
1279 features[i].description = lastDescriptionAdded;
1280 sequences[i].addSequenceFeature(features[i]);
1281 ffile.parseDescriptionHTML(features[i], false);
1284 if (av.featuresDisplayed == null)
1286 av.featuresDisplayed = new Hashtable();
1289 if (lastFeatureGroupAdded != null)
1291 if (featureGroups == null)
1292 featureGroups = new Hashtable();
1293 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1295 setColour(lastFeatureAdded, fcol);
1296 av.featuresDisplayed.put(lastFeatureAdded,
1297 getColour(lastFeatureAdded));
1299 findAllFeatures(false);
1301 ap.paintAlignment(true);
1311 ap.paintAlignment(true);
1317 * update the amend feature button dependent on the given style
1323 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1327 colour.setIcon(null);
1328 colour.setToolTipText(null);
1331 if (col2 instanceof Color)
1333 colour.setBackground((Color) col2);
1337 colour.setBackground(bigPanel.getBackground());
1338 colour.setForeground(Color.black);
1339 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1340 // colour.setForeground(colour.getBackground());
1344 public void setColour(String featureType, Object col)
1347 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1348 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1349 // Object c = featureColours.get(featureType);
1350 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1351 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1353 featureColours.put(featureType, col);
1357 public void setTransparency(float value)
1359 transparency = value;
1362 public float getTransparency()
1364 return transparency;
1368 * Replace current ordering with new ordering
1371 * { String(Type), Colour(Type), Boolean(Displayed) }
1373 public void setFeaturePriority(Object[][] data)
1375 setFeaturePriority(data, true);
1381 * { String(Type), Colour(Type), Boolean(Displayed) }
1383 * when true current featureDisplay list will be cleared
1385 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1389 if (av.featuresDisplayed != null)
1391 av.featuresDisplayed.clear();
1395 av.featuresDisplayed = new Hashtable();
1403 // The feature table will display high priority
1404 // features at the top, but theses are the ones
1405 // we need to render last, so invert the data
1406 renderOrder = new String[data.length];
1408 if (data.length > 0)
1410 for (int i = 0; i < data.length; i++)
1412 String type = data[i][0].toString();
1413 setColour(type, data[i][1]); // todo : typesafety - feature color
1415 if (((Boolean) data[i][2]).booleanValue())
1417 av.featuresDisplayed.put(type, new Integer(getColour(type)
1421 renderOrder[data.length - i - 1] = type;
1427 Map featureOrder = null;
1430 * analogous to colour - store a normalized ordering for all feature types in
1431 * this rendering context.
1434 * Feature type string
1436 * normalized priority - 0 means always appears on top, 1 means
1439 public float setOrder(String type, float position)
1441 if (featureOrder == null)
1443 featureOrder = new Hashtable();
1445 featureOrder.put(type, new Float(position));
1450 * get the global priority (0 (top) to 1 (bottom))
1453 * @return [0,1] or -1 for a type without a priority
1455 public float getOrder(String type)
1457 if (featureOrder != null)
1459 if (featureOrder.containsKey(type))
1461 return ((Float) featureOrder.get(type)).floatValue();
1469 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1471 public void addPropertyChangeListener(PropertyChangeListener listener)
1473 changeSupport.addPropertyChangeListener(listener);
1478 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1480 public void removePropertyChangeListener(PropertyChangeListener listener)
1482 changeSupport.removePropertyChangeListener(listener);