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;
33 import jalview.util.MessageManager;
41 public class FeatureRenderer implements jalview.api.FeatureRenderer
50 * global transparency for feature
52 float transparency = 1.0f;
58 Map featureColours = new ConcurrentHashMap();
60 // A higher level for grouping features of a
62 Map featureGroups = new ConcurrentHashMap();
64 // This is actually an Integer held in the hashtable,
65 // Retrieved using the key feature type
70 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
75 * Creates a new FeatureRenderer object.
80 public FeatureRenderer(AlignmentPanel ap)
84 if (ap != null && ap.seqPanel != null && ap.seqPanel.seqCanvas != null
85 && ap.seqPanel.seqCanvas.fr != null)
87 transferSettings(ap.seqPanel.seqCanvas.fr);
91 public class FeatureRendererSettings implements Cloneable
103 public FeatureRendererSettings(String[] renderOrder,
104 Hashtable featureGroups, Hashtable featureColours,
105 float transparency, Hashtable featureOrder)
108 this.renderOrder = renderOrder;
109 this.featureGroups = featureGroups;
110 this.featureColours = featureColours;
111 this.transparency = transparency;
112 this.featureOrder = featureOrder;
116 * create an independent instance of the feature renderer settings
120 public FeatureRendererSettings(FeatureRenderer fr)
123 featureGroups = new ConcurrentHashMap();
124 featureColours = new ConcurrentHashMap();
125 featureOrder = new ConcurrentHashMap();
126 if (fr.renderOrder != null)
128 this.renderOrder = new String[fr.renderOrder.length];
129 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
130 fr.renderOrder.length);
132 if (fr.featureGroups != null)
134 this.featureGroups = new ConcurrentHashMap(fr.featureGroups);
136 if (fr.featureColours != null)
138 this.featureColours = new ConcurrentHashMap(fr.featureColours);
140 Iterator en = fr.featureColours.keySet().iterator();
143 Object next = en.next();
144 Object val = featureColours.get(next);
145 if (val instanceof GraduatedColor)
148 .put(next, new GraduatedColor((GraduatedColor) val));
151 this.transparency = fr.transparency;
152 if (fr.featureOrder != null)
154 this.featureOrder = new ConcurrentHashMap(fr.featureOrder);
159 public FeatureRendererSettings getSettings()
161 return new FeatureRendererSettings(this);
164 public void transferSettings(FeatureRendererSettings fr)
166 this.renderOrder = fr.renderOrder;
167 this.featureGroups = fr.featureGroups;
168 this.featureColours = fr.featureColours;
169 this.transparency = fr.transparency;
170 this.featureOrder = fr.featureOrder;
173 * update from another feature renderer
174 * @param fr settings to copy
176 public void transferSettings(FeatureRenderer fr)
178 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
179 this.renderOrder = frs.renderOrder;
180 this.featureGroups = frs.featureGroups;
181 this.featureColours = frs.featureColours;
182 this.transparency = frs.transparency;
183 this.featureOrder = frs.featureOrder;
184 if (av != null && av != fr.av)
186 // copy over the displayed feature settings
189 if (fr.av.featuresDisplayed != null)
191 // update display settings
192 if (av.featuresDisplayed == null)
194 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
198 av.featuresDisplayed.clear();
199 Enumeration en = fr.av.featuresDisplayed.keys();
200 while (en.hasMoreElements())
202 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
211 BufferedImage offscreenImage;
213 boolean offscreenRender = false;
215 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
217 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
221 * This is used by the Molecule Viewer and Overview to get the accurate
222 * colourof the rendered sequence
224 public synchronized int findFeatureColour(int initialCol, SequenceI seq,
227 if (!av.showSequenceFeatures)
235 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
236 if (sequenceFeatures != null)
238 sfSize = sequenceFeatures.length;
242 if (sequenceFeatures != lastSeq.getDatasetSequence()
243 .getSequenceFeatures())
245 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
246 if (sequenceFeatures != null)
248 sfSize = sequenceFeatures.length;
252 if (sequenceFeatures == null || sfSize == 0)
257 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
259 return Color.white.getRGB();
262 // Only bother making an offscreen image if transparency is applied
263 if (transparency != 1.0f && offscreenImage == null)
265 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
268 currentColour = null;
269 // TODO: non-threadsafe - each rendering thread needs its own instance of
270 // the feature renderer - or this should be synchronized.
271 offscreenRender = true;
273 if (offscreenImage != null)
275 offscreenImage.setRGB(0, 0, initialCol);
276 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
278 return offscreenImage.getRGB(0, 0);
282 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
284 if (currentColour == null)
290 return ((Integer) currentColour).intValue();
319 // SequenceFeature sf;
322 SequenceFeature[] sequenceFeatures;
324 int sfSize, sfindex, spos, epos;
327 * show scores as heights
329 protected boolean varyHeight = false;
331 synchronized public void drawSequence(Graphics g, SequenceI seq,
332 int start, int end, int y1)
335 if (seq.getDatasetSequence().getSequenceFeatures() == null
336 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
343 fm = g.getFontMetrics();
346 if (av.featuresDisplayed == null || renderOrder == null
350 if (av.featuresDisplayed.size() < 1)
355 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
360 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
363 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
366 if (transparency != 1 && g != null)
368 Graphics2D g2 = (Graphics2D) g;
369 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
373 if (!offscreenRender)
375 spos = lastSeq.findPosition(start);
376 epos = lastSeq.findPosition(end);
379 sfSize = sequenceFeatures.length;
381 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
383 type = renderOrder[renderIndex];
385 if (type == null || !av.featuresDisplayed.containsKey(type))
390 // loop through all features in sequence to find
391 // current feature to render
392 for (sfindex = 0; sfindex < sfSize; sfindex++)
394 if (!sequenceFeatures[sfindex].type.equals(type))
399 if (featureGroups != null
400 && sequenceFeatures[sfindex].featureGroup != null
401 && sequenceFeatures[sfindex].featureGroup.length() != 0
403 .containsKey(sequenceFeatures[sfindex].featureGroup)
404 && !((Boolean) featureGroups
405 .get(sequenceFeatures[sfindex].featureGroup))
412 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
418 if (offscreenRender && offscreenImage == null)
420 if (sequenceFeatures[sfindex].begin <= start
421 && sequenceFeatures[sfindex].end >= start)
423 // this is passed out to the overview and other sequence renderers
424 // (e.g. molecule viewer) to get displayed colour for rendered
426 currentColour = new Integer(
427 getColour(sequenceFeatures[sfindex]).getRGB());
428 // used to be retreived from av.featuresDisplayed
429 // currentColour = av.featuresDisplayed
430 // .get(sequenceFeatures[sfindex].type);
434 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
437 renderFeature(g, seq,
438 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
439 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
440 getColour(sequenceFeatures[sfindex])
441 // new Color(((Integer) av.featuresDisplayed
442 // .get(sequenceFeatures[sfindex].type)).intValue())
444 renderFeature(g, seq,
445 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
446 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
447 getColour(sequenceFeatures[sfindex])
448 // new Color(((Integer) av.featuresDisplayed
449 // .get(sequenceFeatures[sfindex].type)).intValue())
453 else if (showFeature(sequenceFeatures[sfindex]))
455 if (av.showSeqFeaturesHeight
456 && sequenceFeatures[sfindex].score != Float.NaN)
458 renderScoreFeature(g, seq,
459 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
460 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
461 getColour(sequenceFeatures[sfindex]), start, end, y1,
462 normaliseScore(sequenceFeatures[sfindex]));
466 renderFeature(g, seq,
467 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
468 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
469 getColour(sequenceFeatures[sfindex]), start, end, y1);
477 if (transparency != 1.0f && g != null)
479 Graphics2D g2 = (Graphics2D) g;
480 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
485 Hashtable minmax = new Hashtable();
488 * normalise a score against the max/min bounds for the feature type.
490 * @param sequenceFeature
491 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
494 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
496 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
497 final byte[] r = new byte[]
501 if (r[0] != 0 || mm[0] < 0.0)
504 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
508 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
518 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
519 Color featureColour, int start, int end, int y1)
522 if (((fstart <= end) && (fend >= start)))
525 { // fix for if the feature we have starts before the sequence start,
526 fstart = start; // but the feature end is still valid!!
533 int pady = (y1 + av.charHeight) - av.charHeight / 5;
534 for (i = fstart; i <= fend; i++)
536 s = seq.getCharAt(i);
538 if (jalview.util.Comparison.isGap(s))
543 g.setColor(featureColour);
545 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
548 if (offscreenRender || !av.validCharWidth)
553 g.setColor(Color.white);
554 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
555 g.drawString(String.valueOf(s), charOffset
556 + (av.charWidth * (i - start)), pady);
562 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
563 Color featureColour, int start, int end, int y1, byte[] bs)
566 if (((fstart <= end) && (fend >= start)))
569 { // fix for if the feature we have starts before the sequence start,
570 fstart = start; // but the feature end is still valid!!
577 int pady = (y1 + av.charHeight) - av.charHeight / 5;
578 int ystrt = 0, yend = av.charHeight;
581 // signed - zero is always middle of residue line.
584 yend = av.charHeight * (128 - bs[1]) / 512;
585 ystrt = av.charHeight - yend / 2;
589 ystrt = av.charHeight / 2;
590 yend = av.charHeight * (bs[1] - 128) / 512;
595 yend = av.charHeight * bs[1] / 255;
596 ystrt = av.charHeight - yend;
599 for (i = fstart; i <= fend; i++)
601 s = seq.getCharAt(i);
603 if (jalview.util.Comparison.isGap(s))
608 g.setColor(featureColour);
609 int x = (i - start) * av.charWidth;
610 g.drawRect(x, y1, av.charWidth, av.charHeight);
611 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
613 if (offscreenRender || !av.validCharWidth)
618 g.setColor(Color.black);
619 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
620 g.drawString(String.valueOf(s), charOffset
621 + (av.charWidth * (i - start)), pady);
627 boolean newFeatureAdded = false;
630 * Called when alignment in associated view has new/modified features to
631 * discover and display.
634 public void featuresAdded()
640 boolean findingFeatures = false;
643 * search the alignment for all new features, give them a colour and display
644 * them. Then fires a PropertyChangeEvent on the changeSupport object.
647 void findAllFeatures()
649 synchronized (firing)
651 if (firing.equals(Boolean.FALSE))
653 firing = Boolean.TRUE;
654 findAllFeatures(true); // add all new features as visible
655 changeSupport.firePropertyChange("changeSupport", null, null);
656 firing = Boolean.FALSE;
662 * Searches alignment for all features and updates colours
664 * @param newMadeVisible
665 * if true newly added feature types will be rendered immediatly
667 synchronized void findAllFeatures(boolean newMadeVisible)
669 newFeatureAdded = false;
673 newFeatureAdded = true;
677 findingFeatures = true;
679 if (av.featuresDisplayed == null)
681 av.featuresDisplayed = new Hashtable();
684 allfeatures = new Vector();
685 Vector oldfeatures = new Vector();
686 if (renderOrder != null)
688 for (int i = 0; i < renderOrder.length; i++)
690 if (renderOrder[i] != null)
692 oldfeatures.addElement(renderOrder[i]);
698 minmax = new Hashtable();
700 AlignmentI alignment = av.getAlignment();
701 for (int i = 0; i < alignment.getHeight(); i++)
703 SequenceFeature[] features = alignment.getSequenceAt(i)
704 .getDatasetSequence().getSequenceFeatures();
706 if (features == null)
712 while (index < features.length)
714 if (!av.featuresDisplayed.containsKey(features[index].getType()))
717 if (featureGroups.containsKey(features[index].getType()))
719 boolean visible = ((Boolean) featureGroups
720 .get(features[index].featureGroup)).booleanValue();
729 if (!(features[index].begin == 0 && features[index].end == 0))
731 // If beginning and end are 0, the feature is for the whole sequence
732 // and we don't want to render the feature in the normal way
735 && !oldfeatures.contains(features[index].getType()))
737 // this is a new feature type on the alignment. Mark it for
739 av.featuresDisplayed.put(features[index].getType(),
740 new Integer(getColour(features[index].getType())
742 setOrder(features[index].getType(), 0);
746 if (!allfeatures.contains(features[index].getType()))
748 allfeatures.addElement(features[index].getType());
750 if (features[index].score != Float.NaN)
752 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
753 float[][] mm = (float[][]) minmax.get(features[index].getType());
758 minmax.put(features[index].getType(), mm);
760 if (mm[nonpos] == null)
762 mm[nonpos] = new float[]
763 { features[index].score, features[index].score };
768 if (mm[nonpos][0] > features[index].score)
770 mm[nonpos][0] = features[index].score;
772 if (mm[nonpos][1] < features[index].score)
774 mm[nonpos][1] = features[index].score;
781 updateRenderOrder(allfeatures);
782 findingFeatures = false;
785 protected Boolean firing = Boolean.FALSE;
788 * replaces the current renderOrder with the unordered features in
789 * allfeatures. The ordering of any types in both renderOrder and allfeatures
790 * is preserved, and all new feature types are rendered on top of the existing
791 * types, in the order given by getOrder or the order given in allFeatures.
792 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
793 * eliminate the float storage for computing/recalling the persistent ordering
794 * New Cability: updates min/max for colourscheme range if its dynamic
798 private void updateRenderOrder(Vector allFeatures)
800 Vector allfeatures = new Vector(allFeatures);
801 String[] oldRender = renderOrder;
802 renderOrder = new String[allfeatures.size()];
803 Object mmrange, fc = null;
804 boolean initOrders = (featureOrder == null);
806 if (oldRender != null && oldRender.length > 0)
808 for (int j = 0; j < oldRender.length; j++)
810 if (oldRender[j] != null)
814 setOrder(oldRender[j], (1 - (1 + (float) j)
815 / (float) oldRender.length));
817 if (allfeatures.contains(oldRender[j]))
819 renderOrder[opos++] = oldRender[j]; // existing features always
820 // appear below new features
821 allfeatures.removeElement(oldRender[j]);
824 mmrange = minmax.get(oldRender[j]);
827 fc = featureColours.get(oldRender[j]);
828 if (fc != null && fc instanceof GraduatedColor
829 && ((GraduatedColor) fc).isAutoScale())
831 ((GraduatedColor) fc).updateBounds(
832 ((float[][]) mmrange)[0][0],
833 ((float[][]) mmrange)[0][1]);
841 if (allfeatures.size() == 0)
843 // no new features - leave order unchanged.
846 int i = allfeatures.size() - 1;
848 boolean sort = false;
849 String[] newf = new String[allfeatures.size()];
850 float[] sortOrder = new float[allfeatures.size()];
851 Enumeration en = allfeatures.elements();
852 // sort remaining elements
853 while (en.hasMoreElements())
855 newf[i] = en.nextElement().toString();
858 // update from new features minmax if necessary
859 mmrange = minmax.get(newf[i]);
862 fc = featureColours.get(newf[i]);
863 if (fc != null && fc instanceof GraduatedColor
864 && ((GraduatedColor) fc).isAutoScale())
866 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
867 ((float[][]) mmrange)[0][1]);
871 if (initOrders || !featureOrder.containsKey(newf[i]))
873 int denom = initOrders ? allfeatures.size() : featureOrder.size();
874 // new unordered feature - compute persistent ordering at head of
875 // existing features.
876 setOrder(newf[i], i / (float) denom);
878 // set order from newly found feature from persisted ordering.
879 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
882 // only sort if we need to
883 sort = sort || sortOrder[i] > sortOrder[i + 1];
887 if (iSize > 1 && sort)
889 jalview.util.QuickSort.sort(sortOrder, newf);
892 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
896 * get a feature style object for the given type string. Creates a
897 * java.awt.Color for a featureType with no existing colourscheme. TODO:
898 * replace return type with object implementing standard abstract colour/style
902 * @return java.awt.Color or GraduatedColor
904 public Object getFeatureStyle(String featureType)
906 Object fc = featureColours.get(featureType);
909 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
910 Color col = ucs.createColourFromName(featureType);
911 featureColours.put(featureType, fc = col);
917 * return a nominal colour for this feature
920 * @return standard color, or maximum colour for graduated colourscheme
922 public Color getColour(String featureType)
924 Object fc = getFeatureStyle(featureType);
926 if (fc instanceof Color)
932 if (fc instanceof GraduatedColor)
934 return ((GraduatedColor) fc).getMaxColor();
937 throw new Error("Implementation Error: Unrecognised render object "
938 + fc.getClass() + " for features of type " + featureType);
942 * calculate the render colour for a specific feature using current feature
946 * @return render colour for the given feature
948 public Color getColour(SequenceFeature feature)
950 Object fc = getFeatureStyle(feature.getType());
951 if (fc instanceof Color)
957 if (fc instanceof GraduatedColor)
959 return ((GraduatedColor) fc).findColor(feature);
962 throw new Error("Implementation Error: Unrecognised render object "
963 + fc.getClass() + " for features of type " + feature.getType());
966 private boolean showFeature(SequenceFeature sequenceFeature)
968 Object fc = getFeatureStyle(sequenceFeature.type);
969 if (fc instanceof GraduatedColor)
971 return ((GraduatedColor) fc).isColored(sequenceFeature);
980 // // Feature Editing Dialog
981 // // Will be refactored in next release.
983 static String lastFeatureAdded;
985 static String lastFeatureGroupAdded;
987 static String lastDescriptionAdded;
991 int featureIndex = 0;
993 boolean amendFeatures(final SequenceI[] sequences,
994 final SequenceFeature[] features, boolean newFeatures,
995 final AlignmentPanel ap)
1000 final JPanel bigPanel = new JPanel(new BorderLayout());
1001 final JComboBox overlaps;
1002 final JTextField name = new JTextField(25);
1003 final JTextField source = new JTextField(25);
1004 final JTextArea description = new JTextArea(3, 25);
1005 final JSpinner start = new JSpinner();
1006 final JSpinner end = new JSpinner();
1007 start.setPreferredSize(new Dimension(80, 20));
1008 end.setPreferredSize(new Dimension(80, 20));
1009 final FeatureRenderer me = this;
1010 final JLabel colour = new JLabel();
1011 colour.setOpaque(true);
1012 // colour.setBorder(BorderFactory.createEtchedBorder());
1013 colour.setMaximumSize(new Dimension(30, 16));
1014 colour.addMouseListener(new MouseAdapter()
1016 FeatureColourChooser fcc = null;
1018 public void mousePressed(MouseEvent evt)
1020 if (fcol instanceof Color)
1022 Color col = JColorChooser.showDialog(Desktop.desktop,
1023 "Select Feature Colour", ((Color) fcol));
1027 updateColourButton(bigPanel, colour, col);
1035 final String type = features[featureIndex].getType();
1036 fcc = new FeatureColourChooser(me, type);
1037 fcc.setRequestFocusEnabled(true);
1040 fcc.addActionListener(new ActionListener()
1043 public void actionPerformed(ActionEvent e)
1045 fcol = fcc.getLastColour();
1047 setColour(type, fcol);
1048 updateColourButton(bigPanel, colour, fcol);
1056 JPanel tmp = new JPanel();
1057 JPanel panel = new JPanel(new GridLayout(3, 1));
1059 // /////////////////////////////////////
1060 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1061 if (!newFeatures && features.length > 1)
1063 panel = new JPanel(new GridLayout(4, 1));
1065 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
1066 overlaps = new JComboBox();
1067 for (int i = 0; i < features.length; i++)
1069 overlaps.addItem(features[i].getType() + "/"
1070 + features[i].getBegin() + "-" + features[i].getEnd()
1071 + " (" + features[i].getFeatureGroup() + ")");
1076 overlaps.addItemListener(new ItemListener()
1078 public void itemStateChanged(ItemEvent e)
1080 int index = overlaps.getSelectedIndex();
1083 featureIndex = index;
1084 name.setText(features[index].getType());
1085 description.setText(features[index].getDescription());
1086 source.setText(features[index].getFeatureGroup());
1087 start.setValue(new Integer(features[index].getBegin()));
1088 end.setValue(new Integer(features[index].getEnd()));
1090 SearchResults highlight = new SearchResults();
1091 highlight.addResult(sequences[0], features[index].getBegin(),
1092 features[index].getEnd());
1094 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1097 Object col = getFeatureStyle(name.getText());
1100 col = new jalview.schemes.UserColourScheme()
1101 .createColourFromName(name.getText());
1103 oldcol = fcol = col;
1104 updateColourButton(bigPanel, colour, col);
1111 // ////////////////////////////////////
1115 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
1120 tmp.add(new JLabel(MessageManager.getString("label.group"), JLabel.RIGHT));
1125 tmp.add(new JLabel(MessageManager.getString("label.colour"), JLabel.RIGHT));
1127 colour.setPreferredSize(new Dimension(150, 15));
1128 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1129 colour.setForeground(Color.black);
1130 colour.setHorizontalAlignment(SwingConstants.CENTER);
1131 colour.setVerticalAlignment(SwingConstants.CENTER);
1132 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1133 colour.setVerticalTextPosition(SwingConstants.CENTER);
1134 bigPanel.add(panel, BorderLayout.NORTH);
1136 panel = new JPanel();
1137 panel.add(new JLabel(MessageManager.getString("label.description"), JLabel.RIGHT));
1138 description.setFont(JvSwingUtils.getTextAreaFont());
1139 description.setLineWrap(true);
1140 panel.add(new JScrollPane(description));
1144 bigPanel.add(panel, BorderLayout.SOUTH);
1146 panel = new JPanel();
1147 panel.add(new JLabel(MessageManager.getString("label.start"), JLabel.RIGHT));
1149 panel.add(new JLabel(MessageManager.getString("label.end"), JLabel.RIGHT));
1151 bigPanel.add(panel, BorderLayout.CENTER);
1155 bigPanel.add(panel, BorderLayout.CENTER);
1158 if (lastFeatureAdded == null)
1160 if (features[0].type != null)
1162 lastFeatureAdded = features[0].type;
1166 lastFeatureAdded = "feature_1";
1170 if (lastFeatureGroupAdded == null)
1172 if (features[0].featureGroup != null)
1174 lastFeatureGroupAdded = features[0].featureGroup;
1178 lastFeatureGroupAdded = "Jalview";
1184 name.setText(lastFeatureAdded);
1185 source.setText(lastFeatureGroupAdded);
1189 name.setText(features[0].getType());
1190 source.setText(features[0].getFeatureGroup());
1193 start.setValue(new Integer(features[0].getBegin()));
1194 end.setValue(new Integer(features[0].getEnd()));
1195 description.setText(features[0].getDescription());
1196 updateColourButton(bigPanel, colour,
1197 (oldcol = fcol = getFeatureStyle(name.getText())));
1201 options = new Object[]
1202 { "Amend", "Delete", "Cancel" };
1206 options = new Object[]
1210 String title = newFeatures ? "Create New Sequence Feature(s)"
1211 : "Amend/Delete Features for " + sequences[0].getName();
1213 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1214 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1215 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1217 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1219 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1221 // This ensures that the last sequence
1222 // is refreshed and new features are rendered
1224 lastFeatureAdded = name.getText().trim();
1225 lastFeatureGroupAdded = source.getText().trim();
1226 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1227 // TODO: determine if the null feature group is valid
1228 if (lastFeatureGroupAdded.length() < 1)
1229 lastFeatureGroupAdded = null;
1234 SequenceFeature sf = features[featureIndex];
1236 if (reply == JOptionPane.NO_OPTION)
1238 sequences[0].getDatasetSequence().deleteFeature(sf);
1240 else if (reply == JOptionPane.YES_OPTION)
1242 sf.type = lastFeatureAdded;
1243 sf.featureGroup = lastFeatureGroupAdded;
1244 sf.description = lastDescriptionAdded;
1246 setColour(sf.type, fcol);
1247 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1251 sf.begin = ((Integer) start.getValue()).intValue();
1252 sf.end = ((Integer) end.getValue()).intValue();
1253 } catch (NumberFormatException ex)
1257 ffile.parseDescriptionHTML(sf, false);
1261 // NEW FEATURES ADDED
1263 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1265 for (int i = 0; i < sequences.length; i++)
1267 features[i].type = lastFeatureAdded;
1268 if (lastFeatureGroupAdded != null)
1269 features[i].featureGroup = lastFeatureGroupAdded;
1270 features[i].description = lastDescriptionAdded;
1271 sequences[i].addSequenceFeature(features[i]);
1272 ffile.parseDescriptionHTML(features[i], false);
1275 if (av.featuresDisplayed == null)
1277 av.featuresDisplayed = new Hashtable();
1280 if (lastFeatureGroupAdded != null)
1282 if (featureGroups == null)
1283 featureGroups = new Hashtable();
1284 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1286 setColour(lastFeatureAdded, fcol);
1287 av.featuresDisplayed.put(lastFeatureAdded,
1288 getColour(lastFeatureAdded));
1290 findAllFeatures(false);
1292 ap.paintAlignment(true);
1302 ap.paintAlignment(true);
1308 * update the amend feature button dependent on the given style
1314 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1318 colour.setIcon(null);
1319 colour.setToolTipText(null);
1322 if (col2 instanceof Color)
1324 colour.setBackground((Color) col2);
1328 colour.setBackground(bigPanel.getBackground());
1329 colour.setForeground(Color.black);
1330 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1331 // colour.setForeground(colour.getBackground());
1335 public void setColour(String featureType, Object col)
1338 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1339 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1340 // Object c = featureColours.get(featureType);
1341 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1342 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1344 featureColours.put(featureType, col);
1348 public void setTransparency(float value)
1350 transparency = value;
1353 public float getTransparency()
1355 return transparency;
1359 * Replace current ordering with new ordering
1362 * { String(Type), Colour(Type), Boolean(Displayed) }
1364 public void setFeaturePriority(Object[][] data)
1366 setFeaturePriority(data, true);
1372 * { String(Type), Colour(Type), Boolean(Displayed) }
1374 * when true current featureDisplay list will be cleared
1376 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1380 if (av.featuresDisplayed != null)
1382 av.featuresDisplayed.clear();
1386 av.featuresDisplayed = new Hashtable();
1394 // The feature table will display high priority
1395 // features at the top, but theses are the ones
1396 // we need to render last, so invert the data
1397 renderOrder = new String[data.length];
1399 if (data.length > 0)
1401 for (int i = 0; i < data.length; i++)
1403 String type = data[i][0].toString();
1404 setColour(type, data[i][1]); // todo : typesafety - feature color
1406 if (((Boolean) data[i][2]).booleanValue())
1408 av.featuresDisplayed.put(type, new Integer(getColour(type)
1412 renderOrder[data.length - i - 1] = type;
1418 Map featureOrder = null;
1421 * analogous to colour - store a normalized ordering for all feature types in
1422 * this rendering context.
1425 * Feature type string
1427 * normalized priority - 0 means always appears on top, 1 means
1430 public float setOrder(String type, float position)
1432 if (featureOrder == null)
1434 featureOrder = new Hashtable();
1436 featureOrder.put(type, new Float(position));
1441 * get the global priority (0 (top) to 1 (bottom))
1444 * @return [0,1] or -1 for a type without a priority
1446 public float getOrder(String type)
1448 if (featureOrder != null)
1450 if (featureOrder.containsKey(type))
1452 return ((Float) featureOrder.get(type)).floatValue();
1460 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1462 public void addPropertyChangeListener(PropertyChangeListener listener)
1464 changeSupport.addPropertyChangeListener(listener);
1469 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1471 public void removePropertyChangeListener(PropertyChangeListener listener)
1473 changeSupport.removePropertyChangeListener(listener);