2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
21 import java.util.concurrent.ConcurrentHashMap;
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 implements jalview.api.FeatureRenderer
49 * global transparency for feature
51 float transparency = 1.0f;
57 Map featureColours = new ConcurrentHashMap();
59 // A higher level for grouping features of a
61 Map featureGroups = new ConcurrentHashMap();
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
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;
110 * create an independent instance of the feature renderer settings
114 public FeatureRendererSettings(FeatureRenderer fr)
117 featureGroups = new ConcurrentHashMap();
118 featureColours = new ConcurrentHashMap();
119 featureOrder = new ConcurrentHashMap();
120 if (fr.renderOrder != null)
122 this.renderOrder = new String[fr.renderOrder.length];
123 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
124 fr.renderOrder.length);
126 if (fr.featureGroups != null)
128 this.featureGroups = new ConcurrentHashMap(fr.featureGroups);
130 if (fr.featureColours != null)
132 this.featureColours = new ConcurrentHashMap(fr.featureColours);
134 Iterator en = fr.featureColours.keySet().iterator();
137 Object next = en.next();
138 Object val = featureColours.get(next);
139 if (val instanceof GraduatedColor)
142 .put(next, new GraduatedColor((GraduatedColor) val));
145 this.transparency = fr.transparency;
146 if (fr.featureOrder != null)
148 this.featureOrder = new ConcurrentHashMap(fr.featureOrder);
153 public FeatureRendererSettings getSettings()
155 return new FeatureRendererSettings(this);
158 public void transferSettings(FeatureRendererSettings fr)
160 this.renderOrder = fr.renderOrder;
161 this.featureGroups = fr.featureGroups;
162 this.featureColours = fr.featureColours;
163 this.transparency = fr.transparency;
164 this.featureOrder = fr.featureOrder;
167 public void transferSettings(FeatureRenderer fr)
169 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
170 this.renderOrder = frs.renderOrder;
171 this.featureGroups = frs.featureGroups;
172 this.featureColours = frs.featureColours;
173 this.transparency = frs.transparency;
174 this.featureOrder = frs.featureOrder;
175 if (av != null && av != fr.av)
177 // copy over the displayed feature settings
180 if (fr.av.featuresDisplayed != null)
182 // update display settings
183 if (av.featuresDisplayed == null)
185 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
189 av.featuresDisplayed.clear();
190 Enumeration en = fr.av.featuresDisplayed.keys();
191 while (en.hasMoreElements())
193 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
202 BufferedImage offscreenImage;
204 boolean offscreenRender = false;
206 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
208 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
212 * This is used by the Molecule Viewer and Overview to get the accurate
213 * colourof the rendered sequence
215 public synchronized int findFeatureColour(int initialCol, SequenceI seq,
218 if (!av.showSequenceFeatures)
226 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
227 if (sequenceFeatures != null)
229 sfSize = sequenceFeatures.length;
233 if (sequenceFeatures != lastSeq.getDatasetSequence()
234 .getSequenceFeatures())
236 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
237 if (sequenceFeatures != null)
239 sfSize = sequenceFeatures.length;
243 if (sequenceFeatures == null || sfSize == 0)
248 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
250 return Color.white.getRGB();
253 // Only bother making an offscreen image if transparency is applied
254 if (transparency != 1.0f && offscreenImage == null)
256 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
259 currentColour = null;
260 // TODO: non-threadsafe - each rendering thread needs its own instance of
261 // the feature renderer - or this should be synchronized.
262 offscreenRender = true;
264 if (offscreenImage != null)
266 offscreenImage.setRGB(0, 0, initialCol);
267 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
269 return offscreenImage.getRGB(0, 0);
273 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
275 if (currentColour == null)
281 return ((Integer) currentColour).intValue();
310 // SequenceFeature sf;
313 SequenceFeature[] sequenceFeatures;
315 int sfSize, sfindex, spos, epos;
318 * show scores as heights
320 protected boolean varyHeight = false;
322 synchronized public void drawSequence(Graphics g, SequenceI seq,
323 int start, int end, int y1)
326 if (seq.getDatasetSequence().getSequenceFeatures() == null
327 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
334 fm = g.getFontMetrics();
337 if (av.featuresDisplayed == null || renderOrder == null
341 if (av.featuresDisplayed.size() < 1)
346 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
351 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
354 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
357 if (transparency != 1 && g != null)
359 Graphics2D g2 = (Graphics2D) g;
360 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
364 if (!offscreenRender)
366 spos = lastSeq.findPosition(start);
367 epos = lastSeq.findPosition(end);
370 sfSize = sequenceFeatures.length;
372 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
374 type = renderOrder[renderIndex];
376 if (type == null || !av.featuresDisplayed.containsKey(type))
381 // loop through all features in sequence to find
382 // current feature to render
383 for (sfindex = 0; sfindex < sfSize; sfindex++)
385 if (!sequenceFeatures[sfindex].type.equals(type))
390 if (featureGroups != null
391 && sequenceFeatures[sfindex].featureGroup != null
392 && sequenceFeatures[sfindex].featureGroup.length() != 0
394 .containsKey(sequenceFeatures[sfindex].featureGroup)
395 && !((Boolean) featureGroups
396 .get(sequenceFeatures[sfindex].featureGroup))
403 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
409 if (offscreenRender && offscreenImage == null)
411 if (sequenceFeatures[sfindex].begin <= start
412 && sequenceFeatures[sfindex].end >= start)
414 // this is passed out to the overview and other sequence renderers
415 // (e.g. molecule viewer) to get displayed colour for rendered
417 currentColour = new Integer(
418 getColour(sequenceFeatures[sfindex]).getRGB());
419 // used to be retreived from av.featuresDisplayed
420 // currentColour = av.featuresDisplayed
421 // .get(sequenceFeatures[sfindex].type);
425 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
428 renderFeature(g, seq,
429 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
430 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
431 getColour(sequenceFeatures[sfindex])
432 // new Color(((Integer) av.featuresDisplayed
433 // .get(sequenceFeatures[sfindex].type)).intValue())
435 renderFeature(g, seq,
436 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
437 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
438 getColour(sequenceFeatures[sfindex])
439 // new Color(((Integer) av.featuresDisplayed
440 // .get(sequenceFeatures[sfindex].type)).intValue())
444 else if (showFeature(sequenceFeatures[sfindex]))
446 if (av.showSeqFeaturesHeight
447 && sequenceFeatures[sfindex].score != Float.NaN)
449 renderScoreFeature(g, seq,
450 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
451 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
452 getColour(sequenceFeatures[sfindex]), start, end, y1,
453 normaliseScore(sequenceFeatures[sfindex]));
457 renderFeature(g, seq,
458 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
459 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
460 getColour(sequenceFeatures[sfindex]), start, end, y1);
468 if (transparency != 1.0f && g != null)
470 Graphics2D g2 = (Graphics2D) g;
471 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
476 Hashtable minmax = new Hashtable();
479 * normalise a score against the max/min bounds for the feature type.
481 * @param sequenceFeature
482 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
485 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
487 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
488 final byte[] r = new byte[]
492 if (r[0] != 0 || mm[0] < 0.0)
495 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
499 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
509 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
510 Color featureColour, int start, int end, int y1)
513 if (((fstart <= end) && (fend >= start)))
516 { // fix for if the feature we have starts before the sequence start,
517 fstart = start; // but the feature end is still valid!!
524 int pady = (y1 + av.charHeight) - av.charHeight / 5;
525 for (i = fstart; i <= fend; i++)
527 s = seq.getCharAt(i);
529 if (jalview.util.Comparison.isGap(s))
534 g.setColor(featureColour);
536 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
539 if (offscreenRender || !av.validCharWidth)
544 g.setColor(Color.white);
545 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
546 g.drawString(String.valueOf(s), charOffset
547 + (av.charWidth * (i - start)), pady);
553 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
554 Color featureColour, int start, int end, int y1, byte[] bs)
557 if (((fstart <= end) && (fend >= start)))
560 { // fix for if the feature we have starts before the sequence start,
561 fstart = start; // but the feature end is still valid!!
568 int pady = (y1 + av.charHeight) - av.charHeight / 5;
569 int ystrt = 0, yend = av.charHeight;
572 // signed - zero is always middle of residue line.
575 yend = av.charHeight * (128 - bs[1]) / 512;
576 ystrt = av.charHeight - yend / 2;
580 ystrt = av.charHeight / 2;
581 yend = av.charHeight * (bs[1] - 128) / 512;
586 yend = av.charHeight * bs[1] / 255;
587 ystrt = av.charHeight - yend;
590 for (i = fstart; i <= fend; i++)
592 s = seq.getCharAt(i);
594 if (jalview.util.Comparison.isGap(s))
599 g.setColor(featureColour);
600 int x = (i - start) * av.charWidth;
601 g.drawRect(x, y1, av.charWidth, av.charHeight);
602 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
604 if (offscreenRender || !av.validCharWidth)
609 g.setColor(Color.black);
610 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
611 g.drawString(String.valueOf(s), charOffset
612 + (av.charWidth * (i - start)), pady);
618 boolean newFeatureAdded = false;
621 * Called when alignment in associated view has new/modified features to
622 * discover and display.
625 public void featuresAdded()
631 boolean findingFeatures = false;
634 * search the alignment for all new features, give them a colour and display
635 * them. Then fires a PropertyChangeEvent on the changeSupport object.
638 void findAllFeatures()
640 synchronized (firing)
642 if (firing.equals(Boolean.FALSE))
644 firing = Boolean.TRUE;
645 findAllFeatures(true); // add all new features as visible
646 changeSupport.firePropertyChange("changeSupport", null, null);
647 firing = Boolean.FALSE;
653 * Searches alignment for all features and updates colours
655 * @param newMadeVisible
656 * if true newly added feature types will be rendered immediatly
658 synchronized void findAllFeatures(boolean newMadeVisible)
660 newFeatureAdded = false;
664 newFeatureAdded = true;
668 findingFeatures = true;
670 if (av.featuresDisplayed == null)
672 av.featuresDisplayed = new Hashtable();
675 allfeatures = new Vector();
676 Vector oldfeatures = new Vector();
677 if (renderOrder != null)
679 for (int i = 0; i < renderOrder.length; i++)
681 if (renderOrder[i] != null)
683 oldfeatures.addElement(renderOrder[i]);
689 minmax = new Hashtable();
691 AlignmentI alignment = av.getAlignment();
692 for (int i = 0; i < alignment.getHeight(); i++)
694 SequenceFeature[] features = alignment.getSequenceAt(i)
695 .getDatasetSequence().getSequenceFeatures();
697 if (features == null)
703 while (index < features.length)
705 if (!av.featuresDisplayed.containsKey(features[index].getType()))
708 if (featureGroups.containsKey(features[index].getType()))
710 boolean visible = ((Boolean) featureGroups
711 .get(features[index].featureGroup)).booleanValue();
720 if (!(features[index].begin == 0 && features[index].end == 0))
722 // If beginning and end are 0, the feature is for the whole sequence
723 // and we don't want to render the feature in the normal way
726 && !oldfeatures.contains(features[index].getType()))
728 // this is a new feature type on the alignment. Mark it for
730 av.featuresDisplayed.put(features[index].getType(),
731 new Integer(getColour(features[index].getType())
733 setOrder(features[index].getType(), 0);
737 if (!allfeatures.contains(features[index].getType()))
739 allfeatures.addElement(features[index].getType());
741 if (features[index].score != Float.NaN)
743 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
744 float[][] mm = (float[][]) minmax.get(features[index].getType());
749 minmax.put(features[index].getType(), mm);
751 if (mm[nonpos] == null)
753 mm[nonpos] = new float[]
754 { features[index].score, features[index].score };
759 if (mm[nonpos][0] > features[index].score)
761 mm[nonpos][0] = features[index].score;
763 if (mm[nonpos][1] < features[index].score)
765 mm[nonpos][1] = features[index].score;
772 updateRenderOrder(allfeatures);
773 findingFeatures = false;
776 protected Boolean firing = Boolean.FALSE;
779 * replaces the current renderOrder with the unordered features in
780 * allfeatures. The ordering of any types in both renderOrder and allfeatures
781 * is preserved, and all new feature types are rendered on top of the existing
782 * types, in the order given by getOrder or the order given in allFeatures.
783 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
784 * eliminate the float storage for computing/recalling the persistent ordering
785 * New Cability: updates min/max for colourscheme range if its dynamic
789 private void updateRenderOrder(Vector allFeatures)
791 Vector allfeatures = new Vector(allFeatures);
792 String[] oldRender = renderOrder;
793 renderOrder = new String[allfeatures.size()];
794 Object mmrange, fc = null;
795 boolean initOrders = (featureOrder == null);
797 if (oldRender != null && oldRender.length > 0)
799 for (int j = 0; j < oldRender.length; j++)
801 if (oldRender[j] != null)
805 setOrder(oldRender[j], (1 - (1 + (float) j)
806 / (float) oldRender.length));
808 if (allfeatures.contains(oldRender[j]))
810 renderOrder[opos++] = oldRender[j]; // existing features always
811 // appear below new features
812 allfeatures.removeElement(oldRender[j]);
815 mmrange = minmax.get(oldRender[j]);
818 fc = featureColours.get(oldRender[j]);
819 if (fc != null && fc instanceof GraduatedColor
820 && ((GraduatedColor) fc).isAutoScale())
822 ((GraduatedColor) fc).updateBounds(
823 ((float[][]) mmrange)[0][0],
824 ((float[][]) mmrange)[0][1]);
832 if (allfeatures.size() == 0)
834 // no new features - leave order unchanged.
837 int i = allfeatures.size() - 1;
839 boolean sort = false;
840 String[] newf = new String[allfeatures.size()];
841 float[] sortOrder = new float[allfeatures.size()];
842 Enumeration en = allfeatures.elements();
843 // sort remaining elements
844 while (en.hasMoreElements())
846 newf[i] = en.nextElement().toString();
849 // update from new features minmax if necessary
850 mmrange = minmax.get(newf[i]);
853 fc = featureColours.get(newf[i]);
854 if (fc != null && fc instanceof GraduatedColor
855 && ((GraduatedColor) fc).isAutoScale())
857 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
858 ((float[][]) mmrange)[0][1]);
862 if (initOrders || !featureOrder.containsKey(newf[i]))
864 int denom = initOrders ? allfeatures.size() : featureOrder.size();
865 // new unordered feature - compute persistent ordering at head of
866 // existing features.
867 setOrder(newf[i], i / (float) denom);
869 // set order from newly found feature from persisted ordering.
870 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
873 // only sort if we need to
874 sort = sort || sortOrder[i] > sortOrder[i + 1];
878 if (iSize > 1 && sort)
880 jalview.util.QuickSort.sort(sortOrder, newf);
883 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
887 * get a feature style object for the given type string. Creates a
888 * java.awt.Color for a featureType with no existing colourscheme. TODO:
889 * replace return type with object implementing standard abstract colour/style
893 * @return java.awt.Color or GraduatedColor
895 public Object getFeatureStyle(String featureType)
897 Object fc = featureColours.get(featureType);
900 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
901 Color col = ucs.createColourFromName(featureType);
902 featureColours.put(featureType, fc = col);
908 * return a nominal colour for this feature
911 * @return standard color, or maximum colour for graduated colourscheme
913 public Color getColour(String featureType)
915 Object fc = getFeatureStyle(featureType);
917 if (fc instanceof Color)
923 if (fc instanceof GraduatedColor)
925 return ((GraduatedColor) fc).getMaxColor();
928 throw new Error("Implementation Error: Unrecognised render object "
929 + fc.getClass() + " for features of type " + featureType);
933 * calculate the render colour for a specific feature using current feature
937 * @return render colour for the given feature
939 public Color getColour(SequenceFeature feature)
941 Object fc = getFeatureStyle(feature.getType());
942 if (fc instanceof Color)
948 if (fc instanceof GraduatedColor)
950 return ((GraduatedColor) fc).findColor(feature);
953 throw new Error("Implementation Error: Unrecognised render object "
954 + fc.getClass() + " for features of type " + feature.getType());
957 private boolean showFeature(SequenceFeature sequenceFeature)
959 Object fc = getFeatureStyle(sequenceFeature.type);
960 if (fc instanceof GraduatedColor)
962 return ((GraduatedColor) fc).isColored(sequenceFeature);
971 // // Feature Editing Dialog
972 // // Will be refactored in next release.
974 static String lastFeatureAdded;
976 static String lastFeatureGroupAdded;
978 static String lastDescriptionAdded;
982 int featureIndex = 0;
984 boolean amendFeatures(final SequenceI[] sequences,
985 final SequenceFeature[] features, boolean newFeatures,
986 final AlignmentPanel ap)
991 final JPanel bigPanel = new JPanel(new BorderLayout());
992 final JComboBox overlaps;
993 final JTextField name = new JTextField(25);
994 final JTextField source = new JTextField(25);
995 final JTextArea description = new JTextArea(3, 25);
996 final JSpinner start = new JSpinner();
997 final JSpinner end = new JSpinner();
998 start.setPreferredSize(new Dimension(80, 20));
999 end.setPreferredSize(new Dimension(80, 20));
1000 final FeatureRenderer me = this;
1001 final JLabel colour = new JLabel();
1002 colour.setOpaque(true);
1003 // colour.setBorder(BorderFactory.createEtchedBorder());
1004 colour.setMaximumSize(new Dimension(30, 16));
1005 colour.addMouseListener(new MouseAdapter()
1007 FeatureColourChooser fcc = null;
1009 public void mousePressed(MouseEvent evt)
1011 if (fcol instanceof Color)
1013 Color col = JColorChooser.showDialog(Desktop.desktop,
1014 "Select Feature Colour", ((Color) fcol));
1018 updateColourButton(bigPanel, colour, col);
1026 final String type = features[featureIndex].getType();
1027 fcc = new FeatureColourChooser(me, type);
1028 fcc.setRequestFocusEnabled(true);
1031 fcc.addActionListener(new ActionListener()
1034 public void actionPerformed(ActionEvent e)
1036 fcol = fcc.getLastColour();
1038 setColour(type, fcol);
1039 updateColourButton(bigPanel, colour, fcol);
1047 JPanel tmp = new JPanel();
1048 JPanel panel = new JPanel(new GridLayout(3, 1));
1050 // /////////////////////////////////////
1051 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1052 if (!newFeatures && features.length > 1)
1054 panel = new JPanel(new GridLayout(4, 1));
1056 tmp.add(new JLabel("Select Feature: "));
1057 overlaps = new JComboBox();
1058 for (int i = 0; i < features.length; i++)
1060 overlaps.addItem(features[i].getType() + "/"
1061 + features[i].getBegin() + "-" + features[i].getEnd()
1062 + " (" + features[i].getFeatureGroup() + ")");
1067 overlaps.addItemListener(new ItemListener()
1069 public void itemStateChanged(ItemEvent e)
1071 int index = overlaps.getSelectedIndex();
1074 featureIndex = index;
1075 name.setText(features[index].getType());
1076 description.setText(features[index].getDescription());
1077 source.setText(features[index].getFeatureGroup());
1078 start.setValue(new Integer(features[index].getBegin()));
1079 end.setValue(new Integer(features[index].getEnd()));
1081 SearchResults highlight = new SearchResults();
1082 highlight.addResult(sequences[0], features[index].getBegin(),
1083 features[index].getEnd());
1085 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1088 Object col = getFeatureStyle(name.getText());
1091 col = new jalview.schemes.UserColourScheme()
1092 .createColourFromName(name.getText());
1094 oldcol = fcol = col;
1095 updateColourButton(bigPanel, colour, col);
1102 // ////////////////////////////////////
1106 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1111 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1116 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1118 colour.setPreferredSize(new Dimension(150, 15));
1119 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1120 colour.setForeground(Color.black);
1121 colour.setHorizontalAlignment(SwingConstants.CENTER);
1122 colour.setVerticalAlignment(SwingConstants.CENTER);
1123 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1124 colour.setVerticalTextPosition(SwingConstants.CENTER);
1125 bigPanel.add(panel, BorderLayout.NORTH);
1127 panel = new JPanel();
1128 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1129 description.setFont(JvSwingUtils.getTextAreaFont());
1130 description.setLineWrap(true);
1131 panel.add(new JScrollPane(description));
1135 bigPanel.add(panel, BorderLayout.SOUTH);
1137 panel = new JPanel();
1138 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1140 panel.add(new JLabel(" End:", JLabel.RIGHT));
1142 bigPanel.add(panel, BorderLayout.CENTER);
1146 bigPanel.add(panel, BorderLayout.CENTER);
1149 if (lastFeatureAdded == null)
1151 if (features[0].type != null)
1153 lastFeatureAdded = features[0].type;
1157 lastFeatureAdded = "feature_1";
1161 if (lastFeatureGroupAdded == null)
1163 if (features[0].featureGroup != null)
1165 lastFeatureGroupAdded = features[0].featureGroup;
1169 lastFeatureGroupAdded = "Jalview";
1175 name.setText(lastFeatureAdded);
1176 source.setText(lastFeatureGroupAdded);
1180 name.setText(features[0].getType());
1181 source.setText(features[0].getFeatureGroup());
1184 start.setValue(new Integer(features[0].getBegin()));
1185 end.setValue(new Integer(features[0].getEnd()));
1186 description.setText(features[0].getDescription());
1187 updateColourButton(bigPanel, colour,
1188 (oldcol = fcol = getFeatureStyle(name.getText())));
1192 options = new Object[]
1193 { "Amend", "Delete", "Cancel" };
1197 options = new Object[]
1201 String title = newFeatures ? "Create New Sequence Feature(s)"
1202 : "Amend/Delete Features for " + sequences[0].getName();
1204 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1205 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1206 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1208 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1210 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1212 // This ensures that the last sequence
1213 // is refreshed and new features are rendered
1215 lastFeatureAdded = name.getText().trim();
1216 lastFeatureGroupAdded = source.getText().trim();
1217 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1218 // TODO: determine if the null feature group is valid
1219 if (lastFeatureGroupAdded.length() < 1)
1220 lastFeatureGroupAdded = null;
1225 SequenceFeature sf = features[featureIndex];
1227 if (reply == JOptionPane.NO_OPTION)
1229 sequences[0].getDatasetSequence().deleteFeature(sf);
1231 else if (reply == JOptionPane.YES_OPTION)
1233 sf.type = lastFeatureAdded;
1234 sf.featureGroup = lastFeatureGroupAdded;
1235 sf.description = lastDescriptionAdded;
1237 setColour(sf.type, fcol);
1238 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1242 sf.begin = ((Integer) start.getValue()).intValue();
1243 sf.end = ((Integer) end.getValue()).intValue();
1244 } catch (NumberFormatException ex)
1248 ffile.parseDescriptionHTML(sf, false);
1252 // NEW FEATURES ADDED
1254 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1256 for (int i = 0; i < sequences.length; i++)
1258 features[i].type = lastFeatureAdded;
1259 if (lastFeatureGroupAdded != null)
1260 features[i].featureGroup = lastFeatureGroupAdded;
1261 features[i].description = lastDescriptionAdded;
1262 sequences[i].addSequenceFeature(features[i]);
1263 ffile.parseDescriptionHTML(features[i], false);
1266 if (av.featuresDisplayed == null)
1268 av.featuresDisplayed = new Hashtable();
1271 if (lastFeatureGroupAdded != null)
1273 if (featureGroups == null)
1274 featureGroups = new Hashtable();
1275 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1277 setColour(lastFeatureAdded, fcol);
1278 av.featuresDisplayed.put(lastFeatureAdded,
1279 getColour(lastFeatureAdded));
1281 findAllFeatures(false);
1283 ap.paintAlignment(true);
1293 ap.paintAlignment(true);
1299 * update the amend feature button dependent on the given style
1305 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1309 colour.setIcon(null);
1310 colour.setToolTipText(null);
1313 if (col2 instanceof Color)
1315 colour.setBackground((Color) col2);
1319 colour.setBackground(bigPanel.getBackground());
1320 colour.setForeground(Color.black);
1321 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1322 // colour.setForeground(colour.getBackground());
1326 public void setColour(String featureType, Object col)
1329 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1330 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1331 // Object c = featureColours.get(featureType);
1332 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1333 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1335 featureColours.put(featureType, col);
1339 public void setTransparency(float value)
1341 transparency = value;
1344 public float getTransparency()
1346 return transparency;
1350 * Replace current ordering with new ordering
1353 * { String(Type), Colour(Type), Boolean(Displayed) }
1355 public void setFeaturePriority(Object[][] data)
1357 setFeaturePriority(data, true);
1363 * { String(Type), Colour(Type), Boolean(Displayed) }
1365 * when true current featureDisplay list will be cleared
1367 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1371 if (av.featuresDisplayed != null)
1373 av.featuresDisplayed.clear();
1377 av.featuresDisplayed = new Hashtable();
1385 // The feature table will display high priority
1386 // features at the top, but theses are the ones
1387 // we need to render last, so invert the data
1388 renderOrder = new String[data.length];
1390 if (data.length > 0)
1392 for (int i = 0; i < data.length; i++)
1394 String type = data[i][0].toString();
1395 setColour(type, data[i][1]); // todo : typesafety - feature color
1397 if (((Boolean) data[i][2]).booleanValue())
1399 av.featuresDisplayed.put(type, new Integer(getColour(type)
1403 renderOrder[data.length - i - 1] = type;
1409 Map featureOrder = null;
1412 * analogous to colour - store a normalized ordering for all feature types in
1413 * this rendering context.
1416 * Feature type string
1418 * normalized priority - 0 means always appears on top, 1 means
1421 public float setOrder(String type, float position)
1423 if (featureOrder == null)
1425 featureOrder = new Hashtable();
1427 featureOrder.put(type, new Float(position));
1432 * get the global priority (0 (top) to 1 (bottom))
1435 * @return [0,1] or -1 for a type without a priority
1437 public float getOrder(String type)
1439 if (featureOrder != null)
1441 if (featureOrder.containsKey(type))
1443 return ((Float) featureOrder.get(type)).floatValue();
1451 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1453 public void addPropertyChangeListener(PropertyChangeListener listener)
1455 changeSupport.addPropertyChangeListener(listener);
1460 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1462 public void removePropertyChangeListener(PropertyChangeListener listener)
1464 changeSupport.removePropertyChangeListener(listener);