2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
25 import java.awt.image.*;
26 import java.beans.PropertyChangeListener;
27 import java.beans.PropertyChangeSupport;
31 import jalview.datamodel.*;
32 import jalview.schemes.GraduatedColor;
40 public class FeatureRenderer
49 * global transparency for feature
51 float transparency = 1.0f;
57 Hashtable featureColours = new Hashtable();
59 // A higher level for grouping features of a
61 Hashtable featureGroups = new Hashtable();
63 // This is actually an Integer held in the hashtable,
64 // Retrieved using the key feature type
69 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
74 * Creates a new FeatureRenderer object.
79 public FeatureRenderer(AlignmentPanel ap)
85 public class FeatureRendererSettings implements Cloneable
89 Hashtable featureGroups;
91 Hashtable featureColours;
95 Hashtable featureOrder;
97 public FeatureRendererSettings(String[] renderOrder,
98 Hashtable featureGroups, Hashtable featureColours,
99 float transparency, Hashtable featureOrder)
102 this.renderOrder = renderOrder;
103 this.featureGroups = featureGroups;
104 this.featureColours = featureColours;
105 this.transparency = transparency;
106 this.featureOrder = featureOrder;
109 public FeatureRendererSettings(FeatureRenderer fr)
111 this.renderOrder = fr.renderOrder;
112 this.featureGroups = fr.featureGroups;
113 this.featureColours = fr.featureColours;
114 this.transparency = fr.transparency;
115 this.featureOrder = fr.featureOrder;
119 public FeatureRendererSettings getSettings()
121 return new FeatureRendererSettings(this);
124 public void transferSettings(FeatureRendererSettings fr)
126 this.renderOrder = fr.renderOrder;
127 this.featureGroups = fr.featureGroups;
128 this.featureColours = fr.featureColours;
129 this.transparency = fr.transparency;
130 this.featureOrder = fr.featureOrder;
133 public void transferSettings(FeatureRenderer fr)
135 this.renderOrder = fr.renderOrder;
136 this.featureGroups = fr.featureGroups;
137 this.featureColours = fr.featureColours;
138 this.transparency = fr.transparency;
139 this.featureOrder = fr.featureOrder;
142 BufferedImage offscreenImage;
144 boolean offscreenRender = false;
146 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
148 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
152 * This is used by the Molecule Viewer and Overview to get the accurate
153 * colourof the rendered sequence
155 public int findFeatureColour(int initialCol, SequenceI seq, int column)
157 if (!av.showSequenceFeatures)
165 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
166 if (sequenceFeatures != null)
168 sfSize = sequenceFeatures.length;
172 if (sequenceFeatures != lastSeq.getDatasetSequence()
173 .getSequenceFeatures())
175 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
176 if (sequenceFeatures != null)
178 sfSize = sequenceFeatures.length;
182 if (sequenceFeatures == null || sfSize == 0)
187 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
189 return Color.white.getRGB();
192 // Only bother making an offscreen image if transparency is applied
193 if (transparency != 1.0f && offscreenImage == null)
195 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
198 currentColour = null;
200 offscreenRender = true;
202 if (offscreenImage != null)
204 offscreenImage.setRGB(0, 0, initialCol);
205 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
207 return offscreenImage.getRGB(0, 0);
211 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
213 if (currentColour == null)
219 return ((Integer) currentColour).intValue();
248 // SequenceFeature sf;
251 SequenceFeature[] sequenceFeatures;
253 int sfSize, sfindex, spos, epos;
256 * show scores as heights
258 protected boolean varyHeight = false;
260 synchronized public void drawSequence(Graphics g, SequenceI seq,
261 int start, int end, int y1)
264 if (seq.getDatasetSequence().getSequenceFeatures() == null
265 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
272 fm = g.getFontMetrics();
275 if (av.featuresDisplayed == null || renderOrder == null
279 if (av.featuresDisplayed.size() < 1)
284 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
289 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
292 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
295 if (transparency != 1 && g != null)
297 Graphics2D g2 = (Graphics2D) g;
298 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
302 if (!offscreenRender)
304 spos = lastSeq.findPosition(start);
305 epos = lastSeq.findPosition(end);
308 sfSize = sequenceFeatures.length;
310 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
312 type = renderOrder[renderIndex];
314 if (type == null || !av.featuresDisplayed.containsKey(type))
319 // loop through all features in sequence to find
320 // current feature to render
321 for (sfindex = 0; sfindex < sfSize; sfindex++)
323 if (!sequenceFeatures[sfindex].type.equals(type))
328 if (featureGroups != null
329 && sequenceFeatures[sfindex].featureGroup != null
330 && sequenceFeatures[sfindex].featureGroup.length() != 0
332 .containsKey(sequenceFeatures[sfindex].featureGroup)
333 && !((Boolean) featureGroups
334 .get(sequenceFeatures[sfindex].featureGroup))
341 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
347 if (offscreenRender && offscreenImage == null)
349 if (sequenceFeatures[sfindex].begin <= start
350 && sequenceFeatures[sfindex].end >= start)
352 // this is passed out to the overview and other sequence renderers (e.g. molecule viewer) to get displayed colour for rendered sequence
353 currentColour = new Integer(getColour(sequenceFeatures[sfindex]).getRGB());
354 // used to be retreived from av.featuresDisplayed
355 // currentColour = av.featuresDisplayed
356 // .get(sequenceFeatures[sfindex].type);
361 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
367 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
368 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
369 getColour(sequenceFeatures[sfindex])
370 //new Color(((Integer) av.featuresDisplayed
371 // .get(sequenceFeatures[sfindex].type)).intValue())
377 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
378 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
379 getColour(sequenceFeatures[sfindex])
380 //new Color(((Integer) av.featuresDisplayed
381 // .get(sequenceFeatures[sfindex].type)).intValue())
387 if (showFeature(sequenceFeatures[sfindex]))
389 if (av.showSeqFeaturesHeight
390 && sequenceFeatures[sfindex].score != Float.NaN)
392 renderScoreFeature(g, seq, seq
393 .findIndex(sequenceFeatures[sfindex].begin) - 1,
394 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
395 getColour(sequenceFeatures[sfindex]), start, end,
396 y1, normaliseScore(sequenceFeatures[sfindex]));
400 renderFeature(g, seq, seq
401 .findIndex(sequenceFeatures[sfindex].begin) - 1,
402 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
403 getColour(sequenceFeatures[sfindex]), start, end,
412 if (transparency != 1.0f && g != null)
414 Graphics2D g2 = (Graphics2D) g;
415 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
420 Hashtable minmax = new Hashtable();
423 * normalise a score against the max/min bounds for the feature type.
425 * @param sequenceFeature
426 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
429 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
431 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
432 final byte[] r = new byte[]
436 if (r[0] != 0 || mm[0] < 0.0)
439 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
443 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
453 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
454 Color featureColour, int start, int end, int y1)
457 if (((fstart <= end) && (fend >= start)))
460 { // fix for if the feature we have starts before the sequence start,
461 fstart = start; // but the feature end is still valid!!
468 int pady = (y1 + av.charHeight) - av.charHeight / 5;
469 for (i = fstart; i <= fend; i++)
471 s = seq.getCharAt(i);
473 if (jalview.util.Comparison.isGap(s))
478 g.setColor(featureColour);
480 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
483 if (offscreenRender || !av.validCharWidth)
488 g.setColor(Color.white);
489 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
490 g.drawString(String.valueOf(s), charOffset
491 + (av.charWidth * (i - start)), pady);
497 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
498 Color featureColour, int start, int end, int y1, byte[] bs)
501 if (((fstart <= end) && (fend >= start)))
504 { // fix for if the feature we have starts before the sequence start,
505 fstart = start; // but the feature end is still valid!!
512 int pady = (y1 + av.charHeight) - av.charHeight / 5;
513 int ystrt = 0, yend = av.charHeight;
516 // signed - zero is always middle of residue line.
519 yend = av.charHeight * (128 - bs[1]) / 512;
520 ystrt = av.charHeight - yend / 2;
524 ystrt = av.charHeight / 2;
525 yend = av.charHeight * (bs[1] - 128) / 512;
530 yend = av.charHeight * bs[1] / 255;
531 ystrt = av.charHeight - yend;
534 for (i = fstart; i <= fend; i++)
536 s = seq.getCharAt(i);
538 if (jalview.util.Comparison.isGap(s))
543 g.setColor(featureColour);
544 int x = (i - start) * av.charWidth;
545 g.drawRect(x, y1, av.charWidth, av.charHeight);
546 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
548 if (offscreenRender || !av.validCharWidth)
553 g.setColor(Color.black);
554 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
555 g.drawString(String.valueOf(s), charOffset
556 + (av.charWidth * (i - start)), pady);
562 boolean newFeatureAdded = false;
565 * Called when alignment in associated view has new/modified features to
566 * discover and display.
569 public void featuresAdded()
575 boolean findingFeatures = false;
578 * search the alignment for all new features, give them a colour and display
579 * them. Then fires a PropertyChangeEvent on the changeSupport object.
582 void findAllFeatures()
584 synchronized (firing)
586 if (firing.equals(Boolean.FALSE))
588 firing = Boolean.TRUE;
589 findAllFeatures(true); // add all new features as visible
590 changeSupport.firePropertyChange("changeSupport", null, null);
591 firing = Boolean.FALSE;
597 * Searches alignment for all features and updates colours
599 * @param newMadeVisible
600 * if true newly added feature types will be rendered
603 synchronized void findAllFeatures(boolean newMadeVisible)
605 newFeatureAdded = false;
609 newFeatureAdded = true;
613 findingFeatures = true;
615 if (av.featuresDisplayed == null)
617 av.featuresDisplayed = new Hashtable();
620 allfeatures = new Vector();
621 Vector oldfeatures = new Vector();
622 if (renderOrder != null)
624 for (int i = 0; i < renderOrder.length; i++)
626 if (renderOrder[i] != null)
628 oldfeatures.addElement(renderOrder[i]);
634 minmax = new Hashtable();
636 for (int i = 0; i < av.alignment.getHeight(); i++)
638 SequenceFeature[] features = av.alignment.getSequenceAt(i)
639 .getDatasetSequence().getSequenceFeatures();
641 if (features == null)
647 while (index < features.length)
649 if (!av.featuresDisplayed.containsKey(features[index].getType()))
652 if (featureGroups.containsKey(features[index].getType()))
654 boolean visible = ((Boolean) featureGroups
655 .get(features[index].featureGroup)).booleanValue();
664 if (!(features[index].begin == 0 && features[index].end == 0))
666 // If beginning and end are 0, the feature is for the whole sequence
667 // and we don't want to render the feature in the normal way
670 && !oldfeatures.contains(features[index].getType()))
672 // this is a new feature type on the alignment. Mark it for
674 av.featuresDisplayed.put(features[index].getType(),
675 new Integer(getColour(features[index].getType())
677 setOrder(features[index].getType(), 0);
681 if (!allfeatures.contains(features[index].getType()))
683 allfeatures.addElement(features[index].getType());
685 if (features[index].score != Float.NaN)
687 int nonpos= features[index].getBegin()>=1 ? 0 : 1;
688 float[][] mm = (float[][]) minmax.get(features[index].getType());
691 mm = new float[][] {null, null };
692 minmax.put(features[index].getType(), mm);
694 if (mm[nonpos]==null)
696 mm[nonpos] = new float[] { features[index].score, features[index].score };
701 if (mm[nonpos][0] > features[index].score)
703 mm[nonpos][0] = features[index].score;
705 if (mm[nonpos][1] < features[index].score)
707 mm[nonpos][1] = features[index].score;
714 updateRenderOrder(allfeatures);
715 findingFeatures = false;
718 protected Boolean firing = Boolean.FALSE;
721 * replaces the current renderOrder with the unordered features in
722 * allfeatures. The ordering of any types in both renderOrder and allfeatures
723 * is preserved, and all new feature types are rendered on top of the existing
724 * types, in the order given by getOrder or the order given in allFeatures.
725 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
726 * eliminate the float storage for computing/recalling the persistent ordering
727 * New Cability: updates min/max for colourscheme range if its dynamic
730 private void updateRenderOrder(Vector allFeatures)
732 Vector allfeatures = new Vector(allFeatures);
733 String[] oldRender = renderOrder;
734 renderOrder = new String[allfeatures.size()];
735 Object mmrange,fc=null;
736 boolean initOrders = (featureOrder == null);
738 if (oldRender != null && oldRender.length > 0)
740 for (int j = 0; j < oldRender.length; j++)
742 if (oldRender[j] != null)
746 setOrder(oldRender[j], (1 - (1 + (float) j)
747 / (float) oldRender.length));
749 if (allfeatures.contains(oldRender[j]))
751 renderOrder[opos++] = oldRender[j]; // existing features always
752 // appear below new features
753 allfeatures.removeElement(oldRender[j]);
756 mmrange = minmax.get(oldRender[j]);
759 fc = featureColours.get(oldRender[j]);
760 if (fc!=null && fc instanceof GraduatedColor && ((GraduatedColor)fc).isAutoScale())
762 ((GraduatedColor) fc).updateBounds(((float[][])mmrange)[0][0],((float[][])mmrange)[0][1]);
770 if (allfeatures.size() == 0)
772 // no new features - leave order unchanged.
775 int i = allfeatures.size() - 1;
777 boolean sort = false;
778 String[] newf = new String[allfeatures.size()];
779 float[] sortOrder = new float[allfeatures.size()];
780 Enumeration en = allfeatures.elements();
781 // sort remaining elements
782 while (en.hasMoreElements())
784 newf[i] = en.nextElement().toString();
787 // update from new features minmax if necessary
788 mmrange = minmax.get(newf[i]);
791 fc = featureColours.get(newf[i]);
792 if (fc!=null && fc instanceof GraduatedColor && ((GraduatedColor)fc).isAutoScale())
794 ((GraduatedColor) fc).updateBounds(((float[][])mmrange)[0][0],((float[][])mmrange)[0][1]);
798 if (initOrders || !featureOrder.containsKey(newf[i]))
800 int denom = initOrders ? allfeatures.size() : featureOrder.size();
801 // new unordered feature - compute persistent ordering at head of
802 // existing features.
803 setOrder(newf[i], i / (float) denom);
805 // set order from newly found feature from persisted ordering.
806 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
809 // only sort if we need to
810 sort = sort || sortOrder[i] > sortOrder[i + 1];
814 if (iSize > 1 && sort)
816 jalview.util.QuickSort.sort(sortOrder, newf);
819 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
822 * get a feature style object for the given type string.
823 * Creates a java.awt.Color for a featureType with no existing
825 * TODO: replace return type with object implementing standard abstract colour/style interface
827 * @return java.awt.Color or GraduatedColor
829 public Object getFeatureStyle(String featureType) {
830 Object fc = featureColours.get(featureType);
833 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
834 Color col = ucs.createColourFromName(featureType);
835 featureColours.put(featureType, fc = col);
840 * return a nominal colour for this feature
842 * @return standard color, or maximum colour for graduated colourscheme
844 public Color getColour(String featureType)
846 Object fc = getFeatureStyle(featureType);
848 if (fc instanceof Color)
854 if (fc instanceof GraduatedColor)
856 return ((GraduatedColor) fc).getMaxColor();
859 throw new Error("Implementation Error: Unrecognised render object "+fc.getClass()+" for features of type "+featureType);
863 * calculate the render colour for a specific feature using current feature settings.
866 * @return render colour for the given feature
868 public Color getColour(SequenceFeature feature)
870 Object fc = getFeatureStyle(feature.getType());
871 if (fc instanceof Color)
877 if (fc instanceof GraduatedColor)
879 return ((GraduatedColor) fc).findColor(feature);
882 throw new Error("Implementation Error: Unrecognised render object "+fc.getClass()+" for features of type "+feature.getType());
884 private boolean showFeature(SequenceFeature sequenceFeature)
886 Object fc = getFeatureStyle(sequenceFeature.type);
887 if (fc instanceof GraduatedColor)
889 return ((GraduatedColor) fc).isColored(sequenceFeature);
890 } else { return true; }
894 //// Feature Editing Dialog
895 //// Will be refactored in next release.
897 static String lastFeatureAdded;
899 static String lastFeatureGroupAdded;
901 static String lastDescriptionAdded;
903 int featureIndex = 0;
905 boolean amendFeatures(final SequenceI[] sequences,
906 final SequenceFeature[] features, boolean newFeatures,
907 final AlignmentPanel ap)
912 final JPanel bigPanel = new JPanel(new BorderLayout());
913 final JComboBox overlaps;
914 final JTextField name = new JTextField(25);
915 final JTextField source = new JTextField(25);
916 final JTextArea description = new JTextArea(3, 25);
917 final JSpinner start = new JSpinner();
918 final JSpinner end = new JSpinner();
919 start.setPreferredSize(new Dimension(80, 20));
920 end.setPreferredSize(new Dimension(80, 20));
921 final FeatureRenderer me = this;
922 final JLabel colour = new JLabel();
923 colour.setOpaque(true);
924 //colour.setBorder(BorderFactory.createEtchedBorder());
925 colour.setMaximumSize(new Dimension(30, 16));
926 colour.addMouseListener(new MouseAdapter()
928 FeatureColourChooser fcc =null;
929 public void mousePressed(MouseEvent evt)
931 if (fcol instanceof Color)
933 Color col = JColorChooser.showDialog(Desktop.desktop,
934 "Select Feature Colour", ((Color)fcol));
938 updateColourButton(bigPanel,colour,col);
943 final String type = features[featureIndex].getType();
944 fcc = new FeatureColourChooser(me, type);
945 fcc.setRequestFocusEnabled(true);
948 fcc.addActionListener(new ActionListener() {
950 public void actionPerformed(ActionEvent e)
952 fcol = fcc.getLastColour();
954 setColour(type, fcol);
955 updateColourButton(bigPanel,colour,fcol);
964 JPanel tmp = new JPanel();
965 JPanel panel = new JPanel(new GridLayout(3, 1));
967 // /////////////////////////////////////
968 // /MULTIPLE FEATURES AT SELECTED RESIDUE
969 if (!newFeatures && features.length > 1)
971 panel = new JPanel(new GridLayout(4, 1));
973 tmp.add(new JLabel("Select Feature: "));
974 overlaps = new JComboBox();
975 for (int i = 0; i < features.length; i++)
977 overlaps.addItem(features[i].getType() + "/"
978 + features[i].getBegin() + "-" + features[i].getEnd()
979 + " (" + features[i].getFeatureGroup() + ")");
984 overlaps.addItemListener(new ItemListener()
986 public void itemStateChanged(ItemEvent e)
988 int index = overlaps.getSelectedIndex();
991 featureIndex = index;
992 name.setText(features[index].getType());
993 description.setText(features[index].getDescription());
994 source.setText(features[index].getFeatureGroup());
995 start.setValue(new Integer(features[index].getBegin()));
996 end.setValue(new Integer(features[index].getEnd()));
998 SearchResults highlight = new SearchResults();
999 highlight.addResult(sequences[0], features[index].getBegin(),
1000 features[index].getEnd());
1002 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1005 Object col = getFeatureStyle(name.getText());
1008 col = new jalview.schemes.UserColourScheme()
1009 .createColourFromName(name.getText());
1011 oldcol = fcol = col;
1012 updateColourButton(bigPanel, colour,col);
1019 // ////////////////////////////////////
1023 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1028 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1033 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1035 colour.setPreferredSize(new Dimension(150, 15));
1037 bigPanel.add(panel, BorderLayout.NORTH);
1039 panel = new JPanel();
1040 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1041 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
1042 description.setLineWrap(true);
1043 panel.add(new JScrollPane(description));
1047 bigPanel.add(panel, BorderLayout.SOUTH);
1049 panel = new JPanel();
1050 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1052 panel.add(new JLabel(" End:", JLabel.RIGHT));
1054 bigPanel.add(panel, BorderLayout.CENTER);
1058 bigPanel.add(panel, BorderLayout.CENTER);
1061 if (lastFeatureAdded == null)
1063 if (features[0].type != null)
1065 lastFeatureAdded = features[0].type;
1069 lastFeatureAdded = "feature_1";
1073 if (lastFeatureGroupAdded == null)
1075 if (features[0].featureGroup != null)
1077 lastFeatureGroupAdded = features[0].featureGroup;
1081 lastFeatureGroupAdded = "Jalview";
1087 name.setText(lastFeatureAdded);
1088 source.setText(lastFeatureGroupAdded);
1092 name.setText(features[0].getType());
1093 source.setText(features[0].getFeatureGroup());
1096 start.setValue(new Integer(features[0].getBegin()));
1097 end.setValue(new Integer(features[0].getEnd()));
1098 description.setText(features[0].getDescription());
1099 updateColourButton(bigPanel, colour, (oldcol = fcol = getFeatureStyle(name.getText())));
1103 options = new Object[]
1104 { "Amend", "Delete", "Cancel" };
1108 options = new Object[]
1112 String title = newFeatures ? "Create New Sequence Feature(s)"
1113 : "Amend/Delete Features for " + sequences[0].getName();
1115 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1116 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1117 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1119 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1121 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1123 // This ensures that the last sequence
1124 // is refreshed and new features are rendered
1126 lastFeatureAdded = name.getText().trim();
1127 lastFeatureGroupAdded = source.getText().trim();
1128 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1129 // TODO: determine if the null feature group is valid
1130 if (lastFeatureGroupAdded.length() < 1)
1131 lastFeatureGroupAdded = null;
1136 SequenceFeature sf = features[featureIndex];
1138 if (reply == JOptionPane.NO_OPTION)
1140 sequences[0].getDatasetSequence().deleteFeature(sf);
1142 else if (reply == JOptionPane.YES_OPTION)
1144 sf.type = lastFeatureAdded;
1145 sf.featureGroup = lastFeatureGroupAdded;
1146 sf.description = lastDescriptionAdded;
1148 setColour(sf.type, fcol);
1149 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1153 sf.begin = ((Integer) start.getValue()).intValue();
1154 sf.end = ((Integer) end.getValue()).intValue();
1155 } catch (NumberFormatException ex)
1159 ffile.parseDescriptionHTML(sf, false);
1163 // NEW FEATURES ADDED
1165 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1167 for (int i = 0; i < sequences.length; i++)
1169 features[i].type = lastFeatureAdded;
1170 if (lastFeatureGroupAdded != null)
1171 features[i].featureGroup = lastFeatureGroupAdded;
1172 features[i].description = lastDescriptionAdded;
1173 sequences[i].addSequenceFeature(features[i]);
1174 ffile.parseDescriptionHTML(features[i], false);
1177 if (av.featuresDisplayed == null)
1179 av.featuresDisplayed = new Hashtable();
1182 if (lastFeatureGroupAdded != null)
1184 if (featureGroups == null)
1185 featureGroups = new Hashtable();
1186 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1188 setColour(lastFeatureAdded, fcol);
1189 av.featuresDisplayed.put(lastFeatureAdded, getColour(lastFeatureAdded));
1191 findAllFeatures(false);
1193 ap.paintAlignment(true);
1203 ap.paintAlignment(true);
1209 * update the amend feature button dependent on the given style
1214 protected void updateColourButton(JPanel bigPanel, JLabel colour, Object col2)
1217 colour.setIcon(null);
1218 colour.setToolTipText(null);
1220 if (col2 instanceof Color) {
1221 colour.setBackground((Color)col2);
1223 colour.setBackground(bigPanel.getBackground());
1224 colour.setForeground(bigPanel.getForeground());
1225 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1226 //colour.setForeground(colour.getBackground());
1230 public void setColour(String featureType, Object col)
1233 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1234 // Object c = featureColours.get(featureType);
1235 // if (c == null || c instanceof Color || (c instanceof GraduatedColor && !((GraduatedColor)c).getMaxColor().equals(_col)))
1237 featureColours.put(featureType, col);
1241 public void setTransparency(float value)
1243 transparency = value;
1246 public float getTransparency()
1248 return transparency;
1252 * Replace current ordering with new ordering
1255 * String(Type), Colour(Type), Boolean(Displayed) }
1257 public void setFeaturePriority(Object[][] data)
1259 setFeaturePriority(data, true);
1265 * String(Type), Colour(Type), Boolean(Displayed) }
1267 * when true current featureDisplay list will be cleared
1269 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1273 if (av.featuresDisplayed != null)
1275 av.featuresDisplayed.clear();
1279 av.featuresDisplayed = new Hashtable();
1287 // The feature table will display high priority
1288 // features at the top, but theses are the ones
1289 // we need to render last, so invert the data
1290 renderOrder = new String[data.length];
1292 if (data.length > 0)
1294 for (int i = 0; i < data.length; i++)
1296 String type = data[i][0].toString();
1297 setColour(type, data[i][1]); // todo : typesafety - feature color interface object
1298 if (((Boolean) data[i][2]).booleanValue())
1300 av.featuresDisplayed.put(type, new Integer(getColour(type)
1304 renderOrder[data.length - i - 1] = type;
1310 Hashtable featureOrder = null;
1313 * analogous to colour - store a normalized ordering for all feature types in
1314 * this rendering context.
1317 * Feature type string
1319 * normalized priority - 0 means always appears on top, 1 means
1322 public float setOrder(String type, float position)
1324 if (featureOrder == null)
1326 featureOrder = new Hashtable();
1328 featureOrder.put(type, new Float(position));
1333 * get the global priority (0 (top) to 1 (bottom))
1336 * @return [0,1] or -1 for a type without a priority
1338 public float getOrder(String type)
1340 if (featureOrder != null)
1342 if (featureOrder.containsKey(type))
1344 return ((Float) featureOrder.get(type)).floatValue();
1352 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1354 public void addPropertyChangeListener(PropertyChangeListener listener)
1356 changeSupport.addPropertyChangeListener(listener);
1361 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1363 public void removePropertyChangeListener(PropertyChangeListener listener)
1365 changeSupport.removePropertyChangeListener(listener);