2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
23 import java.awt.event.*;
24 import java.awt.image.*;
25 import java.beans.PropertyChangeListener;
26 import java.beans.PropertyChangeSupport;
30 import jalview.datamodel.*;
31 import jalview.schemes.GraduatedColor;
39 public class FeatureRenderer implements jalview.api.FeatureRenderer
48 * global transparency for feature
50 float transparency = 1.0f;
56 Hashtable featureColours = new Hashtable();
58 // A higher level for grouping features of a
60 Hashtable featureGroups = new Hashtable();
62 // This is actually an Integer held in the hashtable,
63 // Retrieved using the key feature type
68 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
73 * Creates a new FeatureRenderer object.
78 public FeatureRenderer(AlignmentPanel ap)
84 public class FeatureRendererSettings implements Cloneable
88 Hashtable featureGroups;
90 Hashtable featureColours;
94 Hashtable featureOrder;
96 public FeatureRendererSettings(String[] renderOrder,
97 Hashtable featureGroups, Hashtable featureColours,
98 float transparency, Hashtable featureOrder)
101 this.renderOrder = renderOrder;
102 this.featureGroups = featureGroups;
103 this.featureColours = featureColours;
104 this.transparency = transparency;
105 this.featureOrder = featureOrder;
109 * create an independent instance of the feature renderer settings
113 public FeatureRendererSettings(FeatureRenderer fr)
116 featureGroups = new Hashtable();
117 featureColours = new Hashtable();
118 featureOrder = new Hashtable();
119 if (fr.renderOrder != null)
121 this.renderOrder = new String[fr.renderOrder.length];
122 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
125 if (fr.featureGroups != null)
127 this.featureGroups = new Hashtable(fr.featureGroups);
129 if (fr.featureColours != null)
131 this.featureColours = new Hashtable(fr.featureColours);
133 Enumeration en = fr.featureColours.keys();
134 while (en.hasMoreElements())
136 Object next = en.nextElement();
137 Object val = featureColours.get(next);
138 if (val instanceof GraduatedColor)
141 .put(next, new GraduatedColor((GraduatedColor) val));
144 this.transparency = fr.transparency;
145 if (fr.featureOrder != null)
147 this.featureOrder = new Hashtable(fr.featureOrder);
152 public FeatureRendererSettings getSettings()
154 return new FeatureRendererSettings(this);
157 public void transferSettings(FeatureRendererSettings fr)
159 this.renderOrder = fr.renderOrder;
160 this.featureGroups = fr.featureGroups;
161 this.featureColours = fr.featureColours;
162 this.transparency = fr.transparency;
163 this.featureOrder = fr.featureOrder;
166 public void transferSettings(FeatureRenderer fr)
168 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
169 this.renderOrder = frs.renderOrder;
170 this.featureGroups = frs.featureGroups;
171 this.featureColours = frs.featureColours;
172 this.transparency = frs.transparency;
173 this.featureOrder = frs.featureOrder;
174 if (av != null && av!=fr.av)
176 // copy over the displayed feature settings
179 if (fr.av.featuresDisplayed != null)
181 // update display settings
182 if (av.featuresDisplayed == null)
184 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
188 av.featuresDisplayed.clear();
189 Enumeration en = fr.av.featuresDisplayed.keys();
190 while (en.hasMoreElements())
192 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
201 BufferedImage offscreenImage;
203 boolean offscreenRender = false;
205 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
207 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
211 * This is used by the Molecule Viewer and Overview to get the accurate
212 * colourof the rendered sequence
214 public synchronized int findFeatureColour(int initialCol, SequenceI seq, int column)
216 if (!av.showSequenceFeatures)
224 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
225 if (sequenceFeatures != null)
227 sfSize = sequenceFeatures.length;
231 if (sequenceFeatures != lastSeq.getDatasetSequence()
232 .getSequenceFeatures())
234 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
235 if (sequenceFeatures != null)
237 sfSize = sequenceFeatures.length;
241 if (sequenceFeatures == null || sfSize == 0)
246 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
248 return Color.white.getRGB();
251 // Only bother making an offscreen image if transparency is applied
252 if (transparency != 1.0f && offscreenImage == null)
254 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
257 currentColour = null;
258 // TODO: non-threadsafe - each rendering thread needs its own instance of the feature renderer - or this should be synchronized.
259 offscreenRender = true;
261 if (offscreenImage != null)
263 offscreenImage.setRGB(0, 0, initialCol);
264 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
266 return offscreenImage.getRGB(0, 0);
270 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
272 if (currentColour == null)
278 return ((Integer) currentColour).intValue();
307 // SequenceFeature sf;
310 SequenceFeature[] sequenceFeatures;
312 int sfSize, sfindex, spos, epos;
315 * show scores as heights
317 protected boolean varyHeight = false;
319 synchronized public void drawSequence(Graphics g, SequenceI seq,
320 int start, int end, int y1)
323 if (seq.getDatasetSequence().getSequenceFeatures() == null
324 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
331 fm = g.getFontMetrics();
334 if (av.featuresDisplayed == null || renderOrder == null
338 if (av.featuresDisplayed.size() < 1)
343 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
348 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
351 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
354 if (transparency != 1 && g != null)
356 Graphics2D g2 = (Graphics2D) g;
357 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
361 if (!offscreenRender)
363 spos = lastSeq.findPosition(start);
364 epos = lastSeq.findPosition(end);
367 sfSize = sequenceFeatures.length;
369 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
371 type = renderOrder[renderIndex];
373 if (type == null || !av.featuresDisplayed.containsKey(type))
378 // loop through all features in sequence to find
379 // current feature to render
380 for (sfindex = 0; sfindex < sfSize; sfindex++)
382 if (!sequenceFeatures[sfindex].type.equals(type))
387 if (featureGroups != null
388 && sequenceFeatures[sfindex].featureGroup != null
389 && sequenceFeatures[sfindex].featureGroup.length() != 0
391 .containsKey(sequenceFeatures[sfindex].featureGroup)
392 && !((Boolean) featureGroups
393 .get(sequenceFeatures[sfindex].featureGroup))
400 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
406 if (offscreenRender && offscreenImage == null)
408 if (sequenceFeatures[sfindex].begin <= start
409 && sequenceFeatures[sfindex].end >= start)
411 // this is passed out to the overview and other sequence renderers
412 // (e.g. molecule viewer) to get displayed colour for rendered
414 currentColour = new Integer(
415 getColour(sequenceFeatures[sfindex]).getRGB());
416 // used to be retreived from av.featuresDisplayed
417 // currentColour = av.featuresDisplayed
418 // .get(sequenceFeatures[sfindex].type);
422 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
425 renderFeature(g, seq,
426 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
427 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
428 getColour(sequenceFeatures[sfindex])
429 // new Color(((Integer) av.featuresDisplayed
430 // .get(sequenceFeatures[sfindex].type)).intValue())
432 renderFeature(g, seq,
433 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
434 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
435 getColour(sequenceFeatures[sfindex])
436 // new Color(((Integer) av.featuresDisplayed
437 // .get(sequenceFeatures[sfindex].type)).intValue())
441 else if (showFeature(sequenceFeatures[sfindex]))
443 if (av.showSeqFeaturesHeight
444 && sequenceFeatures[sfindex].score != Float.NaN)
446 renderScoreFeature(g, seq,
447 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
448 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
449 getColour(sequenceFeatures[sfindex]), start, end, y1,
450 normaliseScore(sequenceFeatures[sfindex]));
454 renderFeature(g, seq,
455 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
456 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
457 getColour(sequenceFeatures[sfindex]), start, end, y1);
465 if (transparency != 1.0f && g != null)
467 Graphics2D g2 = (Graphics2D) g;
468 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
473 Hashtable minmax = new Hashtable();
476 * normalise a score against the max/min bounds for the feature type.
478 * @param sequenceFeature
479 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
482 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
484 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
485 final byte[] r = new byte[]
489 if (r[0] != 0 || mm[0] < 0.0)
492 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
496 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
506 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
507 Color featureColour, int start, int end, int y1)
510 if (((fstart <= end) && (fend >= start)))
513 { // fix for if the feature we have starts before the sequence start,
514 fstart = start; // but the feature end is still valid!!
521 int pady = (y1 + av.charHeight) - av.charHeight / 5;
522 for (i = fstart; i <= fend; i++)
524 s = seq.getCharAt(i);
526 if (jalview.util.Comparison.isGap(s))
531 g.setColor(featureColour);
533 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
536 if (offscreenRender || !av.validCharWidth)
541 g.setColor(Color.white);
542 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
543 g.drawString(String.valueOf(s), charOffset
544 + (av.charWidth * (i - start)), pady);
550 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
551 Color featureColour, int start, int end, int y1, byte[] bs)
554 if (((fstart <= end) && (fend >= start)))
557 { // fix for if the feature we have starts before the sequence start,
558 fstart = start; // but the feature end is still valid!!
565 int pady = (y1 + av.charHeight) - av.charHeight / 5;
566 int ystrt = 0, yend = av.charHeight;
569 // signed - zero is always middle of residue line.
572 yend = av.charHeight * (128 - bs[1]) / 512;
573 ystrt = av.charHeight - yend / 2;
577 ystrt = av.charHeight / 2;
578 yend = av.charHeight * (bs[1] - 128) / 512;
583 yend = av.charHeight * bs[1] / 255;
584 ystrt = av.charHeight - yend;
587 for (i = fstart; i <= fend; i++)
589 s = seq.getCharAt(i);
591 if (jalview.util.Comparison.isGap(s))
596 g.setColor(featureColour);
597 int x = (i - start) * av.charWidth;
598 g.drawRect(x, y1, av.charWidth, av.charHeight);
599 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
601 if (offscreenRender || !av.validCharWidth)
606 g.setColor(Color.black);
607 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
608 g.drawString(String.valueOf(s), charOffset
609 + (av.charWidth * (i - start)), pady);
615 boolean newFeatureAdded = false;
618 * Called when alignment in associated view has new/modified features to
619 * discover and display.
622 public void featuresAdded()
628 boolean findingFeatures = false;
631 * search the alignment for all new features, give them a colour and display
632 * them. Then fires a PropertyChangeEvent on the changeSupport object.
635 void findAllFeatures()
637 synchronized (firing)
639 if (firing.equals(Boolean.FALSE))
641 firing = Boolean.TRUE;
642 findAllFeatures(true); // add all new features as visible
643 changeSupport.firePropertyChange("changeSupport", null, null);
644 firing = Boolean.FALSE;
650 * Searches alignment for all features and updates colours
652 * @param newMadeVisible
653 * if true newly added feature types will be rendered immediatly
655 synchronized void findAllFeatures(boolean newMadeVisible)
657 newFeatureAdded = false;
661 newFeatureAdded = true;
665 findingFeatures = true;
667 if (av.featuresDisplayed == null)
669 av.featuresDisplayed = new Hashtable();
672 allfeatures = new Vector();
673 Vector oldfeatures = new Vector();
674 if (renderOrder != null)
676 for (int i = 0; i < renderOrder.length; i++)
678 if (renderOrder[i] != null)
680 oldfeatures.addElement(renderOrder[i]);
686 minmax = new Hashtable();
688 AlignmentI alignment=av.getAlignment();
689 for (int i = 0; i < alignment.getHeight(); i++)
691 SequenceFeature[] features = alignment.getSequenceAt(i)
692 .getDatasetSequence().getSequenceFeatures();
694 if (features == null)
700 while (index < features.length)
702 if (!av.featuresDisplayed.containsKey(features[index].getType()))
705 if (featureGroups.containsKey(features[index].getType()))
707 boolean visible = ((Boolean) featureGroups
708 .get(features[index].featureGroup)).booleanValue();
717 if (!(features[index].begin == 0 && features[index].end == 0))
719 // If beginning and end are 0, the feature is for the whole sequence
720 // and we don't want to render the feature in the normal way
723 && !oldfeatures.contains(features[index].getType()))
725 // this is a new feature type on the alignment. Mark it for
727 av.featuresDisplayed.put(features[index].getType(),
728 new Integer(getColour(features[index].getType())
730 setOrder(features[index].getType(), 0);
734 if (!allfeatures.contains(features[index].getType()))
736 allfeatures.addElement(features[index].getType());
738 if (features[index].score != Float.NaN)
740 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
741 float[][] mm = (float[][]) minmax.get(features[index].getType());
746 minmax.put(features[index].getType(), mm);
748 if (mm[nonpos] == null)
750 mm[nonpos] = new float[]
751 { features[index].score, features[index].score };
756 if (mm[nonpos][0] > features[index].score)
758 mm[nonpos][0] = features[index].score;
760 if (mm[nonpos][1] < features[index].score)
762 mm[nonpos][1] = features[index].score;
769 updateRenderOrder(allfeatures);
770 findingFeatures = false;
773 protected Boolean firing = Boolean.FALSE;
776 * replaces the current renderOrder with the unordered features in
777 * allfeatures. The ordering of any types in both renderOrder and allfeatures
778 * is preserved, and all new feature types are rendered on top of the existing
779 * types, in the order given by getOrder or the order given in allFeatures.
780 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
781 * eliminate the float storage for computing/recalling the persistent ordering
782 * New Cability: updates min/max for colourscheme range if its dynamic
786 private void updateRenderOrder(Vector allFeatures)
788 Vector allfeatures = new Vector(allFeatures);
789 String[] oldRender = renderOrder;
790 renderOrder = new String[allfeatures.size()];
791 Object mmrange, fc = null;
792 boolean initOrders = (featureOrder == null);
794 if (oldRender != null && oldRender.length > 0)
796 for (int j = 0; j < oldRender.length; j++)
798 if (oldRender[j] != null)
802 setOrder(oldRender[j], (1 - (1 + (float) j)
803 / (float) oldRender.length));
805 if (allfeatures.contains(oldRender[j]))
807 renderOrder[opos++] = oldRender[j]; // existing features always
808 // appear below new features
809 allfeatures.removeElement(oldRender[j]);
812 mmrange = minmax.get(oldRender[j]);
815 fc = featureColours.get(oldRender[j]);
816 if (fc != null && fc instanceof GraduatedColor
817 && ((GraduatedColor) fc).isAutoScale())
819 ((GraduatedColor) fc).updateBounds(
820 ((float[][]) mmrange)[0][0],
821 ((float[][]) mmrange)[0][1]);
829 if (allfeatures.size() == 0)
831 // no new features - leave order unchanged.
834 int i = allfeatures.size() - 1;
836 boolean sort = false;
837 String[] newf = new String[allfeatures.size()];
838 float[] sortOrder = new float[allfeatures.size()];
839 Enumeration en = allfeatures.elements();
840 // sort remaining elements
841 while (en.hasMoreElements())
843 newf[i] = en.nextElement().toString();
846 // update from new features minmax if necessary
847 mmrange = minmax.get(newf[i]);
850 fc = featureColours.get(newf[i]);
851 if (fc != null && fc instanceof GraduatedColor
852 && ((GraduatedColor) fc).isAutoScale())
854 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
855 ((float[][]) mmrange)[0][1]);
859 if (initOrders || !featureOrder.containsKey(newf[i]))
861 int denom = initOrders ? allfeatures.size() : featureOrder.size();
862 // new unordered feature - compute persistent ordering at head of
863 // existing features.
864 setOrder(newf[i], i / (float) denom);
866 // set order from newly found feature from persisted ordering.
867 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
870 // only sort if we need to
871 sort = sort || sortOrder[i] > sortOrder[i + 1];
875 if (iSize > 1 && sort)
877 jalview.util.QuickSort.sort(sortOrder, newf);
880 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
884 * get a feature style object for the given type string. Creates a
885 * java.awt.Color for a featureType with no existing colourscheme. TODO:
886 * replace return type with object implementing standard abstract colour/style
890 * @return java.awt.Color or GraduatedColor
892 public Object getFeatureStyle(String featureType)
894 Object fc = featureColours.get(featureType);
897 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
898 Color col = ucs.createColourFromName(featureType);
899 featureColours.put(featureType, fc = col);
905 * return a nominal colour for this feature
908 * @return standard color, or maximum colour for graduated colourscheme
910 public Color getColour(String featureType)
912 Object fc = getFeatureStyle(featureType);
914 if (fc instanceof Color)
920 if (fc instanceof GraduatedColor)
922 return ((GraduatedColor) fc).getMaxColor();
925 throw new Error("Implementation Error: Unrecognised render object "
926 + fc.getClass() + " for features of type " + featureType);
930 * calculate the render colour for a specific feature using current feature
934 * @return render colour for the given feature
936 public Color getColour(SequenceFeature feature)
938 Object fc = getFeatureStyle(feature.getType());
939 if (fc instanceof Color)
945 if (fc instanceof GraduatedColor)
947 return ((GraduatedColor) fc).findColor(feature);
950 throw new Error("Implementation Error: Unrecognised render object "
951 + fc.getClass() + " for features of type " + feature.getType());
954 private boolean showFeature(SequenceFeature sequenceFeature)
956 Object fc = getFeatureStyle(sequenceFeature.type);
957 if (fc instanceof GraduatedColor)
959 return ((GraduatedColor) fc).isColored(sequenceFeature);
968 // // Feature Editing Dialog
969 // // Will be refactored in next release.
971 static String lastFeatureAdded;
973 static String lastFeatureGroupAdded;
975 static String lastDescriptionAdded;
979 int featureIndex = 0;
981 boolean amendFeatures(final SequenceI[] sequences,
982 final SequenceFeature[] features, boolean newFeatures,
983 final AlignmentPanel ap)
988 final JPanel bigPanel = new JPanel(new BorderLayout());
989 final JComboBox overlaps;
990 final JTextField name = new JTextField(25);
991 final JTextField source = new JTextField(25);
992 final JTextArea description = new JTextArea(3, 25);
993 final JSpinner start = new JSpinner();
994 final JSpinner end = new JSpinner();
995 start.setPreferredSize(new Dimension(80, 20));
996 end.setPreferredSize(new Dimension(80, 20));
997 final FeatureRenderer me = this;
998 final JLabel colour = new JLabel();
999 colour.setOpaque(true);
1000 // colour.setBorder(BorderFactory.createEtchedBorder());
1001 colour.setMaximumSize(new Dimension(30, 16));
1002 colour.addMouseListener(new MouseAdapter()
1004 FeatureColourChooser fcc = null;
1006 public void mousePressed(MouseEvent evt)
1008 if (fcol instanceof Color)
1010 Color col = JColorChooser.showDialog(Desktop.desktop,
1011 "Select Feature Colour", ((Color) fcol));
1015 updateColourButton(bigPanel, colour, col);
1023 final String type = features[featureIndex].getType();
1024 fcc = new FeatureColourChooser(me, type);
1025 fcc.setRequestFocusEnabled(true);
1028 fcc.addActionListener(new ActionListener()
1031 public void actionPerformed(ActionEvent e)
1033 fcol = fcc.getLastColour();
1035 setColour(type, fcol);
1036 updateColourButton(bigPanel, colour, fcol);
1044 JPanel tmp = new JPanel();
1045 JPanel panel = new JPanel(new GridLayout(3, 1));
1047 // /////////////////////////////////////
1048 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1049 if (!newFeatures && features.length > 1)
1051 panel = new JPanel(new GridLayout(4, 1));
1053 tmp.add(new JLabel("Select Feature: "));
1054 overlaps = new JComboBox();
1055 for (int i = 0; i < features.length; i++)
1057 overlaps.addItem(features[i].getType() + "/"
1058 + features[i].getBegin() + "-" + features[i].getEnd()
1059 + " (" + features[i].getFeatureGroup() + ")");
1064 overlaps.addItemListener(new ItemListener()
1066 public void itemStateChanged(ItemEvent e)
1068 int index = overlaps.getSelectedIndex();
1071 featureIndex = index;
1072 name.setText(features[index].getType());
1073 description.setText(features[index].getDescription());
1074 source.setText(features[index].getFeatureGroup());
1075 start.setValue(new Integer(features[index].getBegin()));
1076 end.setValue(new Integer(features[index].getEnd()));
1078 SearchResults highlight = new SearchResults();
1079 highlight.addResult(sequences[0], features[index].getBegin(),
1080 features[index].getEnd());
1082 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1085 Object col = getFeatureStyle(name.getText());
1088 col = new jalview.schemes.UserColourScheme()
1089 .createColourFromName(name.getText());
1091 oldcol = fcol = col;
1092 updateColourButton(bigPanel, colour, col);
1099 // ////////////////////////////////////
1103 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1108 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1113 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1115 colour.setPreferredSize(new Dimension(150, 15));
1116 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1117 colour.setForeground(Color.black);
1118 colour.setHorizontalAlignment(SwingConstants.CENTER);
1119 colour.setVerticalAlignment(SwingConstants.CENTER);
1120 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1121 colour.setVerticalTextPosition(SwingConstants.CENTER);
1122 bigPanel.add(panel, BorderLayout.NORTH);
1124 panel = new JPanel();
1125 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1126 description.setFont(JvSwingUtils.getTextAreaFont());
1127 description.setLineWrap(true);
1128 panel.add(new JScrollPane(description));
1132 bigPanel.add(panel, BorderLayout.SOUTH);
1134 panel = new JPanel();
1135 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1137 panel.add(new JLabel(" End:", JLabel.RIGHT));
1139 bigPanel.add(panel, BorderLayout.CENTER);
1143 bigPanel.add(panel, BorderLayout.CENTER);
1146 if (lastFeatureAdded == null)
1148 if (features[0].type != null)
1150 lastFeatureAdded = features[0].type;
1154 lastFeatureAdded = "feature_1";
1158 if (lastFeatureGroupAdded == null)
1160 if (features[0].featureGroup != null)
1162 lastFeatureGroupAdded = features[0].featureGroup;
1166 lastFeatureGroupAdded = "Jalview";
1172 name.setText(lastFeatureAdded);
1173 source.setText(lastFeatureGroupAdded);
1177 name.setText(features[0].getType());
1178 source.setText(features[0].getFeatureGroup());
1181 start.setValue(new Integer(features[0].getBegin()));
1182 end.setValue(new Integer(features[0].getEnd()));
1183 description.setText(features[0].getDescription());
1184 updateColourButton(bigPanel, colour,
1185 (oldcol = fcol = getFeatureStyle(name.getText())));
1189 options = new Object[]
1190 { "Amend", "Delete", "Cancel" };
1194 options = new Object[]
1198 String title = newFeatures ? "Create New Sequence Feature(s)"
1199 : "Amend/Delete Features for " + sequences[0].getName();
1201 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1202 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1203 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1205 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1207 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1209 // This ensures that the last sequence
1210 // is refreshed and new features are rendered
1212 lastFeatureAdded = name.getText().trim();
1213 lastFeatureGroupAdded = source.getText().trim();
1214 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1215 // TODO: determine if the null feature group is valid
1216 if (lastFeatureGroupAdded.length() < 1)
1217 lastFeatureGroupAdded = null;
1222 SequenceFeature sf = features[featureIndex];
1224 if (reply == JOptionPane.NO_OPTION)
1226 sequences[0].getDatasetSequence().deleteFeature(sf);
1228 else if (reply == JOptionPane.YES_OPTION)
1230 sf.type = lastFeatureAdded;
1231 sf.featureGroup = lastFeatureGroupAdded;
1232 sf.description = lastDescriptionAdded;
1234 setColour(sf.type, fcol);
1235 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1239 sf.begin = ((Integer) start.getValue()).intValue();
1240 sf.end = ((Integer) end.getValue()).intValue();
1241 } catch (NumberFormatException ex)
1245 ffile.parseDescriptionHTML(sf, false);
1249 // NEW FEATURES ADDED
1251 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1253 for (int i = 0; i < sequences.length; i++)
1255 features[i].type = lastFeatureAdded;
1256 if (lastFeatureGroupAdded != null)
1257 features[i].featureGroup = lastFeatureGroupAdded;
1258 features[i].description = lastDescriptionAdded;
1259 sequences[i].addSequenceFeature(features[i]);
1260 ffile.parseDescriptionHTML(features[i], false);
1263 if (av.featuresDisplayed == null)
1265 av.featuresDisplayed = new Hashtable();
1268 if (lastFeatureGroupAdded != null)
1270 if (featureGroups == null)
1271 featureGroups = new Hashtable();
1272 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1274 setColour(lastFeatureAdded, fcol);
1275 av.featuresDisplayed.put(lastFeatureAdded,
1276 getColour(lastFeatureAdded));
1278 findAllFeatures(false);
1280 ap.paintAlignment(true);
1290 ap.paintAlignment(true);
1296 * update the amend feature button dependent on the given style
1302 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1306 colour.setIcon(null);
1307 colour.setToolTipText(null);
1310 if (col2 instanceof Color)
1312 colour.setBackground((Color) col2);
1316 colour.setBackground(bigPanel.getBackground());
1317 colour.setForeground(Color.black);
1318 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1319 // colour.setForeground(colour.getBackground());
1323 public void setColour(String featureType, Object col)
1326 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1327 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1328 // Object c = featureColours.get(featureType);
1329 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1330 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1332 featureColours.put(featureType, col);
1336 public void setTransparency(float value)
1338 transparency = value;
1341 public float getTransparency()
1343 return transparency;
1347 * Replace current ordering with new ordering
1350 * { String(Type), Colour(Type), Boolean(Displayed) }
1352 public void setFeaturePriority(Object[][] data)
1354 setFeaturePriority(data, true);
1360 * { String(Type), Colour(Type), Boolean(Displayed) }
1362 * when true current featureDisplay list will be cleared
1364 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1368 if (av.featuresDisplayed != null)
1370 av.featuresDisplayed.clear();
1374 av.featuresDisplayed = new Hashtable();
1382 // The feature table will display high priority
1383 // features at the top, but theses are the ones
1384 // we need to render last, so invert the data
1385 renderOrder = new String[data.length];
1387 if (data.length > 0)
1389 for (int i = 0; i < data.length; i++)
1391 String type = data[i][0].toString();
1392 setColour(type, data[i][1]); // todo : typesafety - feature color
1394 if (((Boolean) data[i][2]).booleanValue())
1396 av.featuresDisplayed.put(type, new Integer(getColour(type)
1400 renderOrder[data.length - i - 1] = type;
1406 Hashtable featureOrder = null;
1409 * analogous to colour - store a normalized ordering for all feature types in
1410 * this rendering context.
1413 * Feature type string
1415 * normalized priority - 0 means always appears on top, 1 means
1418 public float setOrder(String type, float position)
1420 if (featureOrder == null)
1422 featureOrder = new Hashtable();
1424 featureOrder.put(type, new Float(position));
1429 * get the global priority (0 (top) to 1 (bottom))
1432 * @return [0,1] or -1 for a type without a priority
1434 public float getOrder(String type)
1436 if (featureOrder != null)
1438 if (featureOrder.containsKey(type))
1440 return ((Float) featureOrder.get(type)).floatValue();
1448 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1450 public void addPropertyChangeListener(PropertyChangeListener listener)
1452 changeSupport.addPropertyChangeListener(listener);
1457 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1459 public void removePropertyChangeListener(PropertyChangeListener listener)
1461 changeSupport.removePropertyChangeListener(listener);