2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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
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)
115 this.renderOrder = new String[fr.renderOrder.length];
116 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
118 this.featureGroups = new Hashtable(fr.featureGroups);
119 this.featureColours = new Hashtable(fr.featureColours);
120 Enumeration en = fr.featureColours.keys();
121 while (en.hasMoreElements())
123 Object next = en.nextElement();
124 Object val = featureColours.get(next);
125 if (val instanceof GraduatedColor)
128 .put(next, new GraduatedColor((GraduatedColor) val));
131 this.transparency = fr.transparency;
132 this.featureOrder = new Hashtable(fr.featureOrder);
136 public FeatureRendererSettings getSettings()
138 return new FeatureRendererSettings(this);
141 public void transferSettings(FeatureRendererSettings fr)
143 this.renderOrder = fr.renderOrder;
144 this.featureGroups = fr.featureGroups;
145 this.featureColours = fr.featureColours;
146 this.transparency = fr.transparency;
147 this.featureOrder = fr.featureOrder;
150 public void transferSettings(FeatureRenderer fr)
152 this.renderOrder = fr.renderOrder;
153 this.featureGroups = fr.featureGroups;
154 this.featureColours = fr.featureColours;
155 this.transparency = fr.transparency;
156 this.featureOrder = fr.featureOrder;
159 BufferedImage offscreenImage;
161 boolean offscreenRender = false;
163 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
165 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
169 * This is used by the Molecule Viewer and Overview to get the accurate
170 * colourof the rendered sequence
172 public int findFeatureColour(int initialCol, SequenceI seq, int column)
174 if (!av.showSequenceFeatures)
182 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
183 if (sequenceFeatures != null)
185 sfSize = sequenceFeatures.length;
189 if (sequenceFeatures != lastSeq.getDatasetSequence()
190 .getSequenceFeatures())
192 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
193 if (sequenceFeatures != null)
195 sfSize = sequenceFeatures.length;
199 if (sequenceFeatures == null || sfSize == 0)
204 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
206 return Color.white.getRGB();
209 // Only bother making an offscreen image if transparency is applied
210 if (transparency != 1.0f && offscreenImage == null)
212 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
215 currentColour = null;
217 offscreenRender = true;
219 if (offscreenImage != null)
221 offscreenImage.setRGB(0, 0, initialCol);
222 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
224 return offscreenImage.getRGB(0, 0);
228 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
230 if (currentColour == null)
236 return ((Integer) currentColour).intValue();
265 // SequenceFeature sf;
268 SequenceFeature[] sequenceFeatures;
270 int sfSize, sfindex, spos, epos;
273 * show scores as heights
275 protected boolean varyHeight = false;
277 synchronized public void drawSequence(Graphics g, SequenceI seq,
278 int start, int end, int y1)
281 if (seq.getDatasetSequence().getSequenceFeatures() == null
282 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
289 fm = g.getFontMetrics();
292 if (av.featuresDisplayed == null || renderOrder == null
296 if (av.featuresDisplayed.size() < 1)
301 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
306 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
309 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
312 if (transparency != 1 && g != null)
314 Graphics2D g2 = (Graphics2D) g;
315 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
319 if (!offscreenRender)
321 spos = lastSeq.findPosition(start);
322 epos = lastSeq.findPosition(end);
325 sfSize = sequenceFeatures.length;
327 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
329 type = renderOrder[renderIndex];
331 if (type == null || !av.featuresDisplayed.containsKey(type))
336 // loop through all features in sequence to find
337 // current feature to render
338 for (sfindex = 0; sfindex < sfSize; sfindex++)
340 if (!sequenceFeatures[sfindex].type.equals(type))
345 if (featureGroups != null
346 && sequenceFeatures[sfindex].featureGroup != null
347 && sequenceFeatures[sfindex].featureGroup.length() != 0
349 .containsKey(sequenceFeatures[sfindex].featureGroup)
350 && !((Boolean) featureGroups
351 .get(sequenceFeatures[sfindex].featureGroup))
358 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
364 if (offscreenRender && offscreenImage == null)
366 if (sequenceFeatures[sfindex].begin <= start
367 && sequenceFeatures[sfindex].end >= start)
369 // this is passed out to the overview and other sequence renderers
370 // (e.g. molecule viewer) to get displayed colour for rendered
372 currentColour = new Integer(
373 getColour(sequenceFeatures[sfindex]).getRGB());
374 // used to be retreived from av.featuresDisplayed
375 // currentColour = av.featuresDisplayed
376 // .get(sequenceFeatures[sfindex].type);
380 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
383 renderFeature(g, seq, seq
384 .findIndex(sequenceFeatures[sfindex].begin) - 1, seq
385 .findIndex(sequenceFeatures[sfindex].begin) - 1,
386 getColour(sequenceFeatures[sfindex])
387 // new Color(((Integer) av.featuresDisplayed
388 // .get(sequenceFeatures[sfindex].type)).intValue())
390 renderFeature(g, seq, seq
391 .findIndex(sequenceFeatures[sfindex].end) - 1, seq
392 .findIndex(sequenceFeatures[sfindex].end) - 1,
393 getColour(sequenceFeatures[sfindex])
394 // new Color(((Integer) av.featuresDisplayed
395 // .get(sequenceFeatures[sfindex].type)).intValue())
399 else if (showFeature(sequenceFeatures[sfindex]))
401 if (av.showSeqFeaturesHeight
402 && sequenceFeatures[sfindex].score != Float.NaN)
404 renderScoreFeature(g, seq, seq
405 .findIndex(sequenceFeatures[sfindex].begin) - 1, seq
406 .findIndex(sequenceFeatures[sfindex].end) - 1,
407 getColour(sequenceFeatures[sfindex]), start, end, y1,
408 normaliseScore(sequenceFeatures[sfindex]));
412 renderFeature(g, seq, seq
413 .findIndex(sequenceFeatures[sfindex].begin) - 1, seq
414 .findIndex(sequenceFeatures[sfindex].end) - 1,
415 getColour(sequenceFeatures[sfindex]), start, end, y1);
423 if (transparency != 1.0f && g != null)
425 Graphics2D g2 = (Graphics2D) g;
426 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
431 Hashtable minmax = new Hashtable();
434 * normalise a score against the max/min bounds for the feature type.
436 * @param sequenceFeature
437 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
440 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
442 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
443 final byte[] r = new byte[]
447 if (r[0] != 0 || mm[0] < 0.0)
450 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
454 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
464 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
465 Color featureColour, int start, int end, int y1)
468 if (((fstart <= end) && (fend >= start)))
471 { // fix for if the feature we have starts before the sequence start,
472 fstart = start; // but the feature end is still valid!!
479 int pady = (y1 + av.charHeight) - av.charHeight / 5;
480 for (i = fstart; i <= fend; i++)
482 s = seq.getCharAt(i);
484 if (jalview.util.Comparison.isGap(s))
489 g.setColor(featureColour);
491 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
494 if (offscreenRender || !av.validCharWidth)
499 g.setColor(Color.white);
500 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
501 g.drawString(String.valueOf(s), charOffset
502 + (av.charWidth * (i - start)), pady);
508 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
509 Color featureColour, int start, int end, int y1, byte[] bs)
512 if (((fstart <= end) && (fend >= start)))
515 { // fix for if the feature we have starts before the sequence start,
516 fstart = start; // but the feature end is still valid!!
523 int pady = (y1 + av.charHeight) - av.charHeight / 5;
524 int ystrt = 0, yend = av.charHeight;
527 // signed - zero is always middle of residue line.
530 yend = av.charHeight * (128 - bs[1]) / 512;
531 ystrt = av.charHeight - yend / 2;
535 ystrt = av.charHeight / 2;
536 yend = av.charHeight * (bs[1] - 128) / 512;
541 yend = av.charHeight * bs[1] / 255;
542 ystrt = av.charHeight - yend;
545 for (i = fstart; i <= fend; i++)
547 s = seq.getCharAt(i);
549 if (jalview.util.Comparison.isGap(s))
554 g.setColor(featureColour);
555 int x = (i - start) * av.charWidth;
556 g.drawRect(x, y1, av.charWidth, av.charHeight);
557 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
559 if (offscreenRender || !av.validCharWidth)
564 g.setColor(Color.black);
565 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
566 g.drawString(String.valueOf(s), charOffset
567 + (av.charWidth * (i - start)), pady);
573 boolean newFeatureAdded = false;
576 * Called when alignment in associated view has new/modified features to
577 * discover and display.
580 public void featuresAdded()
586 boolean findingFeatures = false;
589 * search the alignment for all new features, give them a colour and display
590 * them. Then fires a PropertyChangeEvent on the changeSupport object.
593 void findAllFeatures()
595 synchronized (firing)
597 if (firing.equals(Boolean.FALSE))
599 firing = Boolean.TRUE;
600 findAllFeatures(true); // add all new features as visible
601 changeSupport.firePropertyChange("changeSupport", null, null);
602 firing = Boolean.FALSE;
608 * Searches alignment for all features and updates colours
610 * @param newMadeVisible
611 * if true newly added feature types will be rendered immediatly
613 synchronized void findAllFeatures(boolean newMadeVisible)
615 newFeatureAdded = false;
619 newFeatureAdded = true;
623 findingFeatures = true;
625 if (av.featuresDisplayed == null)
627 av.featuresDisplayed = new Hashtable();
630 allfeatures = new Vector();
631 Vector oldfeatures = new Vector();
632 if (renderOrder != null)
634 for (int i = 0; i < renderOrder.length; i++)
636 if (renderOrder[i] != null)
638 oldfeatures.addElement(renderOrder[i]);
644 minmax = new Hashtable();
646 for (int i = 0; i < av.alignment.getHeight(); i++)
648 SequenceFeature[] features = av.alignment.getSequenceAt(i)
649 .getDatasetSequence().getSequenceFeatures();
651 if (features == null)
657 while (index < features.length)
659 if (!av.featuresDisplayed.containsKey(features[index].getType()))
662 if (featureGroups.containsKey(features[index].getType()))
664 boolean visible = ((Boolean) featureGroups
665 .get(features[index].featureGroup)).booleanValue();
674 if (!(features[index].begin == 0 && features[index].end == 0))
676 // If beginning and end are 0, the feature is for the whole sequence
677 // and we don't want to render the feature in the normal way
680 && !oldfeatures.contains(features[index].getType()))
682 // this is a new feature type on the alignment. Mark it for
684 av.featuresDisplayed.put(features[index].getType(),
685 new Integer(getColour(features[index].getType())
687 setOrder(features[index].getType(), 0);
691 if (!allfeatures.contains(features[index].getType()))
693 allfeatures.addElement(features[index].getType());
695 if (features[index].score != Float.NaN)
697 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
698 float[][] mm = (float[][]) minmax.get(features[index].getType());
703 minmax.put(features[index].getType(), mm);
705 if (mm[nonpos] == null)
707 mm[nonpos] = new float[]
708 { features[index].score, features[index].score };
713 if (mm[nonpos][0] > features[index].score)
715 mm[nonpos][0] = features[index].score;
717 if (mm[nonpos][1] < features[index].score)
719 mm[nonpos][1] = features[index].score;
726 updateRenderOrder(allfeatures);
727 findingFeatures = false;
730 protected Boolean firing = Boolean.FALSE;
733 * replaces the current renderOrder with the unordered features in
734 * allfeatures. The ordering of any types in both renderOrder and allfeatures
735 * is preserved, and all new feature types are rendered on top of the existing
736 * types, in the order given by getOrder or the order given in allFeatures.
737 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
738 * eliminate the float storage for computing/recalling the persistent ordering
739 * New Cability: updates min/max for colourscheme range if its dynamic
743 private void updateRenderOrder(Vector allFeatures)
745 Vector allfeatures = new Vector(allFeatures);
746 String[] oldRender = renderOrder;
747 renderOrder = new String[allfeatures.size()];
748 Object mmrange, fc = null;
749 boolean initOrders = (featureOrder == null);
751 if (oldRender != null && oldRender.length > 0)
753 for (int j = 0; j < oldRender.length; j++)
755 if (oldRender[j] != null)
759 setOrder(oldRender[j], (1 - (1 + (float) j)
760 / (float) oldRender.length));
762 if (allfeatures.contains(oldRender[j]))
764 renderOrder[opos++] = oldRender[j]; // existing features always
765 // appear below new features
766 allfeatures.removeElement(oldRender[j]);
769 mmrange = minmax.get(oldRender[j]);
772 fc = featureColours.get(oldRender[j]);
773 if (fc != null && fc instanceof GraduatedColor
774 && ((GraduatedColor) fc).isAutoScale())
776 ((GraduatedColor) fc).updateBounds(
777 ((float[][]) mmrange)[0][0],
778 ((float[][]) mmrange)[0][1]);
786 if (allfeatures.size() == 0)
788 // no new features - leave order unchanged.
791 int i = allfeatures.size() - 1;
793 boolean sort = false;
794 String[] newf = new String[allfeatures.size()];
795 float[] sortOrder = new float[allfeatures.size()];
796 Enumeration en = allfeatures.elements();
797 // sort remaining elements
798 while (en.hasMoreElements())
800 newf[i] = en.nextElement().toString();
803 // update from new features minmax if necessary
804 mmrange = minmax.get(newf[i]);
807 fc = featureColours.get(newf[i]);
808 if (fc != null && fc instanceof GraduatedColor
809 && ((GraduatedColor) fc).isAutoScale())
811 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
812 ((float[][]) mmrange)[0][1]);
816 if (initOrders || !featureOrder.containsKey(newf[i]))
818 int denom = initOrders ? allfeatures.size() : featureOrder.size();
819 // new unordered feature - compute persistent ordering at head of
820 // existing features.
821 setOrder(newf[i], i / (float) denom);
823 // set order from newly found feature from persisted ordering.
824 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
827 // only sort if we need to
828 sort = sort || sortOrder[i] > sortOrder[i + 1];
832 if (iSize > 1 && sort)
834 jalview.util.QuickSort.sort(sortOrder, newf);
837 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
841 * get a feature style object for the given type string. Creates a
842 * java.awt.Color for a featureType with no existing colourscheme. TODO:
843 * replace return type with object implementing standard abstract colour/style
847 * @return java.awt.Color or GraduatedColor
849 public Object getFeatureStyle(String featureType)
851 Object fc = featureColours.get(featureType);
854 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
855 Color col = ucs.createColourFromName(featureType);
856 featureColours.put(featureType, fc = col);
862 * return a nominal colour for this feature
865 * @return standard color, or maximum colour for graduated colourscheme
867 public Color getColour(String featureType)
869 Object fc = getFeatureStyle(featureType);
871 if (fc instanceof Color)
877 if (fc instanceof GraduatedColor)
879 return ((GraduatedColor) fc).getMaxColor();
882 throw new Error("Implementation Error: Unrecognised render object "
883 + fc.getClass() + " for features of type " + featureType);
887 * calculate the render colour for a specific feature using current feature
891 * @return render colour for the given feature
893 public Color getColour(SequenceFeature feature)
895 Object fc = getFeatureStyle(feature.getType());
896 if (fc instanceof Color)
902 if (fc instanceof GraduatedColor)
904 return ((GraduatedColor) fc).findColor(feature);
907 throw new Error("Implementation Error: Unrecognised render object "
908 + fc.getClass() + " for features of type " + feature.getType());
911 private boolean showFeature(SequenceFeature sequenceFeature)
913 Object fc = getFeatureStyle(sequenceFeature.type);
914 if (fc instanceof GraduatedColor)
916 return ((GraduatedColor) fc).isColored(sequenceFeature);
925 // // Feature Editing Dialog
926 // // Will be refactored in next release.
928 static String lastFeatureAdded;
930 static String lastFeatureGroupAdded;
932 static String lastDescriptionAdded;
936 int featureIndex = 0;
938 boolean amendFeatures(final SequenceI[] sequences,
939 final SequenceFeature[] features, boolean newFeatures,
940 final AlignmentPanel ap)
945 final JPanel bigPanel = new JPanel(new BorderLayout());
946 final JComboBox overlaps;
947 final JTextField name = new JTextField(25);
948 final JTextField source = new JTextField(25);
949 final JTextArea description = new JTextArea(3, 25);
950 final JSpinner start = new JSpinner();
951 final JSpinner end = new JSpinner();
952 start.setPreferredSize(new Dimension(80, 20));
953 end.setPreferredSize(new Dimension(80, 20));
954 final FeatureRenderer me = this;
955 final JLabel colour = new JLabel();
956 colour.setOpaque(true);
957 // colour.setBorder(BorderFactory.createEtchedBorder());
958 colour.setMaximumSize(new Dimension(30, 16));
959 colour.addMouseListener(new MouseAdapter()
961 FeatureColourChooser fcc = null;
963 public void mousePressed(MouseEvent evt)
965 if (fcol instanceof Color)
967 Color col = JColorChooser.showDialog(Desktop.desktop,
968 "Select Feature Colour", ((Color) fcol));
972 updateColourButton(bigPanel, colour, col);
980 final String type = features[featureIndex].getType();
981 fcc = new FeatureColourChooser(me, type);
982 fcc.setRequestFocusEnabled(true);
985 fcc.addActionListener(new ActionListener()
988 public void actionPerformed(ActionEvent e)
990 fcol = fcc.getLastColour();
992 setColour(type, fcol);
993 updateColourButton(bigPanel, colour, fcol);
1001 JPanel tmp = new JPanel();
1002 JPanel panel = new JPanel(new GridLayout(3, 1));
1004 // /////////////////////////////////////
1005 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1006 if (!newFeatures && features.length > 1)
1008 panel = new JPanel(new GridLayout(4, 1));
1010 tmp.add(new JLabel("Select Feature: "));
1011 overlaps = new JComboBox();
1012 for (int i = 0; i < features.length; i++)
1014 overlaps.addItem(features[i].getType() + "/"
1015 + features[i].getBegin() + "-" + features[i].getEnd()
1016 + " (" + features[i].getFeatureGroup() + ")");
1021 overlaps.addItemListener(new ItemListener()
1023 public void itemStateChanged(ItemEvent e)
1025 int index = overlaps.getSelectedIndex();
1028 featureIndex = index;
1029 name.setText(features[index].getType());
1030 description.setText(features[index].getDescription());
1031 source.setText(features[index].getFeatureGroup());
1032 start.setValue(new Integer(features[index].getBegin()));
1033 end.setValue(new Integer(features[index].getEnd()));
1035 SearchResults highlight = new SearchResults();
1036 highlight.addResult(sequences[0], features[index].getBegin(),
1037 features[index].getEnd());
1039 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1042 Object col = getFeatureStyle(name.getText());
1045 col = new jalview.schemes.UserColourScheme()
1046 .createColourFromName(name.getText());
1048 oldcol = fcol = col;
1049 updateColourButton(bigPanel, colour, col);
1056 // ////////////////////////////////////
1060 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1065 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1070 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1072 colour.setPreferredSize(new Dimension(150, 15));
1073 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1074 colour.setForeground(Color.black);
1075 colour.setHorizontalAlignment(SwingConstants.CENTER);
1076 colour.setVerticalAlignment(SwingConstants.CENTER);
1077 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1078 colour.setVerticalTextPosition(SwingConstants.CENTER);
1079 bigPanel.add(panel, BorderLayout.NORTH);
1081 panel = new JPanel();
1082 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1083 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
1084 description.setLineWrap(true);
1085 panel.add(new JScrollPane(description));
1089 bigPanel.add(panel, BorderLayout.SOUTH);
1091 panel = new JPanel();
1092 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1094 panel.add(new JLabel(" End:", JLabel.RIGHT));
1096 bigPanel.add(panel, BorderLayout.CENTER);
1100 bigPanel.add(panel, BorderLayout.CENTER);
1103 if (lastFeatureAdded == null)
1105 if (features[0].type != null)
1107 lastFeatureAdded = features[0].type;
1111 lastFeatureAdded = "feature_1";
1115 if (lastFeatureGroupAdded == null)
1117 if (features[0].featureGroup != null)
1119 lastFeatureGroupAdded = features[0].featureGroup;
1123 lastFeatureGroupAdded = "Jalview";
1129 name.setText(lastFeatureAdded);
1130 source.setText(lastFeatureGroupAdded);
1134 name.setText(features[0].getType());
1135 source.setText(features[0].getFeatureGroup());
1138 start.setValue(new Integer(features[0].getBegin()));
1139 end.setValue(new Integer(features[0].getEnd()));
1140 description.setText(features[0].getDescription());
1141 updateColourButton(bigPanel, colour,
1142 (oldcol = fcol = getFeatureStyle(name.getText())));
1146 options = new Object[]
1147 { "Amend", "Delete", "Cancel" };
1151 options = new Object[]
1155 String title = newFeatures ? "Create New Sequence Feature(s)"
1156 : "Amend/Delete Features for " + sequences[0].getName();
1158 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1159 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1160 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1162 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1164 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1166 // This ensures that the last sequence
1167 // is refreshed and new features are rendered
1169 lastFeatureAdded = name.getText().trim();
1170 lastFeatureGroupAdded = source.getText().trim();
1171 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1172 // TODO: determine if the null feature group is valid
1173 if (lastFeatureGroupAdded.length() < 1)
1174 lastFeatureGroupAdded = null;
1179 SequenceFeature sf = features[featureIndex];
1181 if (reply == JOptionPane.NO_OPTION)
1183 sequences[0].getDatasetSequence().deleteFeature(sf);
1185 else if (reply == JOptionPane.YES_OPTION)
1187 sf.type = lastFeatureAdded;
1188 sf.featureGroup = lastFeatureGroupAdded;
1189 sf.description = lastDescriptionAdded;
1191 setColour(sf.type, fcol);
1192 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1196 sf.begin = ((Integer) start.getValue()).intValue();
1197 sf.end = ((Integer) end.getValue()).intValue();
1198 } catch (NumberFormatException ex)
1202 ffile.parseDescriptionHTML(sf, false);
1206 // NEW FEATURES ADDED
1208 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1210 for (int i = 0; i < sequences.length; i++)
1212 features[i].type = lastFeatureAdded;
1213 if (lastFeatureGroupAdded != null)
1214 features[i].featureGroup = lastFeatureGroupAdded;
1215 features[i].description = lastDescriptionAdded;
1216 sequences[i].addSequenceFeature(features[i]);
1217 ffile.parseDescriptionHTML(features[i], false);
1220 if (av.featuresDisplayed == null)
1222 av.featuresDisplayed = new Hashtable();
1225 if (lastFeatureGroupAdded != null)
1227 if (featureGroups == null)
1228 featureGroups = new Hashtable();
1229 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1231 setColour(lastFeatureAdded, fcol);
1232 av.featuresDisplayed.put(lastFeatureAdded,
1233 getColour(lastFeatureAdded));
1235 findAllFeatures(false);
1237 ap.paintAlignment(true);
1247 ap.paintAlignment(true);
1253 * update the amend feature button dependent on the given style
1259 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1263 colour.setIcon(null);
1264 colour.setToolTipText(null);
1267 if (col2 instanceof Color)
1269 colour.setBackground((Color) col2);
1273 colour.setBackground(bigPanel.getBackground());
1274 colour.setForeground(Color.black);
1275 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1276 // colour.setForeground(colour.getBackground());
1280 public void setColour(String featureType, Object col)
1283 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1284 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1285 // Object c = featureColours.get(featureType);
1286 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1287 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1289 featureColours.put(featureType, col);
1293 public void setTransparency(float value)
1295 transparency = value;
1298 public float getTransparency()
1300 return transparency;
1304 * Replace current ordering with new ordering
1307 * { String(Type), Colour(Type), Boolean(Displayed) }
1309 public void setFeaturePriority(Object[][] data)
1311 setFeaturePriority(data, true);
1317 * { String(Type), Colour(Type), Boolean(Displayed) }
1319 * when true current featureDisplay list will be cleared
1321 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1325 if (av.featuresDisplayed != null)
1327 av.featuresDisplayed.clear();
1331 av.featuresDisplayed = new Hashtable();
1339 // The feature table will display high priority
1340 // features at the top, but theses are the ones
1341 // we need to render last, so invert the data
1342 renderOrder = new String[data.length];
1344 if (data.length > 0)
1346 for (int i = 0; i < data.length; i++)
1348 String type = data[i][0].toString();
1349 setColour(type, data[i][1]); // todo : typesafety - feature color
1351 if (((Boolean) data[i][2]).booleanValue())
1353 av.featuresDisplayed.put(type, new Integer(getColour(type)
1357 renderOrder[data.length - i - 1] = type;
1363 Hashtable featureOrder = null;
1366 * analogous to colour - store a normalized ordering for all feature types in
1367 * this rendering context.
1370 * Feature type string
1372 * normalized priority - 0 means always appears on top, 1 means
1375 public float setOrder(String type, float position)
1377 if (featureOrder == null)
1379 featureOrder = new Hashtable();
1381 featureOrder.put(type, new Float(position));
1386 * get the global priority (0 (top) to 1 (bottom))
1389 * @return [0,1] or -1 for a type without a priority
1391 public float getOrder(String type)
1393 if (featureOrder != null)
1395 if (featureOrder.containsKey(type))
1397 return ((Float) featureOrder.get(type)).floatValue();
1405 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1407 public void addPropertyChangeListener(PropertyChangeListener listener)
1409 changeSupport.addPropertyChangeListener(listener);
1414 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1416 public void removePropertyChangeListener(PropertyChangeListener listener)
1418 changeSupport.removePropertyChangeListener(listener);