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/>.
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,
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, int column)
217 if (!av.showSequenceFeatures)
225 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
226 if (sequenceFeatures != null)
228 sfSize = sequenceFeatures.length;
232 if (sequenceFeatures != lastSeq.getDatasetSequence()
233 .getSequenceFeatures())
235 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
236 if (sequenceFeatures != null)
238 sfSize = sequenceFeatures.length;
242 if (sequenceFeatures == null || sfSize == 0)
247 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
249 return Color.white.getRGB();
252 // Only bother making an offscreen image if transparency is applied
253 if (transparency != 1.0f && offscreenImage == null)
255 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
258 currentColour = null;
259 // TODO: non-threadsafe - each rendering thread needs its own instance of the feature renderer - or this should be synchronized.
260 offscreenRender = true;
262 if (offscreenImage != null)
264 offscreenImage.setRGB(0, 0, initialCol);
265 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
267 return offscreenImage.getRGB(0, 0);
271 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
273 if (currentColour == null)
279 return ((Integer) currentColour).intValue();
308 // SequenceFeature sf;
311 SequenceFeature[] sequenceFeatures;
313 int sfSize, sfindex, spos, epos;
316 * show scores as heights
318 protected boolean varyHeight = false;
320 synchronized public void drawSequence(Graphics g, SequenceI seq,
321 int start, int end, int y1)
324 if (seq.getDatasetSequence().getSequenceFeatures() == null
325 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
332 fm = g.getFontMetrics();
335 if (av.featuresDisplayed == null || renderOrder == null
339 if (av.featuresDisplayed.size() < 1)
344 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
349 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
352 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
355 if (transparency != 1 && g != null)
357 Graphics2D g2 = (Graphics2D) g;
358 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
362 if (!offscreenRender)
364 spos = lastSeq.findPosition(start);
365 epos = lastSeq.findPosition(end);
368 sfSize = sequenceFeatures.length;
370 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
372 type = renderOrder[renderIndex];
374 if (type == null || !av.featuresDisplayed.containsKey(type))
379 // loop through all features in sequence to find
380 // current feature to render
381 for (sfindex = 0; sfindex < sfSize; sfindex++)
383 if (!sequenceFeatures[sfindex].type.equals(type))
388 if (featureGroups != null
389 && sequenceFeatures[sfindex].featureGroup != null
390 && sequenceFeatures[sfindex].featureGroup.length() != 0
392 .containsKey(sequenceFeatures[sfindex].featureGroup)
393 && !((Boolean) featureGroups
394 .get(sequenceFeatures[sfindex].featureGroup))
401 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
407 if (offscreenRender && offscreenImage == null)
409 if (sequenceFeatures[sfindex].begin <= start
410 && sequenceFeatures[sfindex].end >= start)
412 // this is passed out to the overview and other sequence renderers
413 // (e.g. molecule viewer) to get displayed colour for rendered
415 currentColour = new Integer(
416 getColour(sequenceFeatures[sfindex]).getRGB());
417 // used to be retreived from av.featuresDisplayed
418 // currentColour = av.featuresDisplayed
419 // .get(sequenceFeatures[sfindex].type);
423 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
426 renderFeature(g, seq,
427 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
428 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
429 getColour(sequenceFeatures[sfindex])
430 // new Color(((Integer) av.featuresDisplayed
431 // .get(sequenceFeatures[sfindex].type)).intValue())
433 renderFeature(g, seq,
434 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
435 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
436 getColour(sequenceFeatures[sfindex])
437 // new Color(((Integer) av.featuresDisplayed
438 // .get(sequenceFeatures[sfindex].type)).intValue())
442 else if (showFeature(sequenceFeatures[sfindex]))
444 if (av.showSeqFeaturesHeight
445 && sequenceFeatures[sfindex].score != Float.NaN)
447 renderScoreFeature(g, seq,
448 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
449 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
450 getColour(sequenceFeatures[sfindex]), start, end, y1,
451 normaliseScore(sequenceFeatures[sfindex]));
455 renderFeature(g, seq,
456 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
457 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
458 getColour(sequenceFeatures[sfindex]), start, end, y1);
466 if (transparency != 1.0f && g != null)
468 Graphics2D g2 = (Graphics2D) g;
469 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
474 Hashtable minmax = new Hashtable();
477 * normalise a score against the max/min bounds for the feature type.
479 * @param sequenceFeature
480 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
483 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
485 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
486 final byte[] r = new byte[]
490 if (r[0] != 0 || mm[0] < 0.0)
493 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
497 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
507 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
508 Color featureColour, int start, int end, int y1)
511 if (((fstart <= end) && (fend >= start)))
514 { // fix for if the feature we have starts before the sequence start,
515 fstart = start; // but the feature end is still valid!!
522 int pady = (y1 + av.charHeight) - av.charHeight / 5;
523 for (i = fstart; i <= fend; i++)
525 s = seq.getCharAt(i);
527 if (jalview.util.Comparison.isGap(s))
532 g.setColor(featureColour);
534 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
537 if (offscreenRender || !av.validCharWidth)
542 g.setColor(Color.white);
543 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
544 g.drawString(String.valueOf(s), charOffset
545 + (av.charWidth * (i - start)), pady);
551 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
552 Color featureColour, int start, int end, int y1, byte[] bs)
555 if (((fstart <= end) && (fend >= start)))
558 { // fix for if the feature we have starts before the sequence start,
559 fstart = start; // but the feature end is still valid!!
566 int pady = (y1 + av.charHeight) - av.charHeight / 5;
567 int ystrt = 0, yend = av.charHeight;
570 // signed - zero is always middle of residue line.
573 yend = av.charHeight * (128 - bs[1]) / 512;
574 ystrt = av.charHeight - yend / 2;
578 ystrt = av.charHeight / 2;
579 yend = av.charHeight * (bs[1] - 128) / 512;
584 yend = av.charHeight * bs[1] / 255;
585 ystrt = av.charHeight - yend;
588 for (i = fstart; i <= fend; i++)
590 s = seq.getCharAt(i);
592 if (jalview.util.Comparison.isGap(s))
597 g.setColor(featureColour);
598 int x = (i - start) * av.charWidth;
599 g.drawRect(x, y1, av.charWidth, av.charHeight);
600 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
602 if (offscreenRender || !av.validCharWidth)
607 g.setColor(Color.black);
608 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
609 g.drawString(String.valueOf(s), charOffset
610 + (av.charWidth * (i - start)), pady);
616 boolean newFeatureAdded = false;
619 * Called when alignment in associated view has new/modified features to
620 * discover and display.
623 public void featuresAdded()
629 boolean findingFeatures = false;
632 * search the alignment for all new features, give them a colour and display
633 * them. Then fires a PropertyChangeEvent on the changeSupport object.
636 void findAllFeatures()
638 synchronized (firing)
640 if (firing.equals(Boolean.FALSE))
642 firing = Boolean.TRUE;
643 findAllFeatures(true); // add all new features as visible
644 changeSupport.firePropertyChange("changeSupport", null, null);
645 firing = Boolean.FALSE;
651 * Searches alignment for all features and updates colours
653 * @param newMadeVisible
654 * if true newly added feature types will be rendered immediatly
656 synchronized void findAllFeatures(boolean newMadeVisible)
658 newFeatureAdded = false;
662 newFeatureAdded = true;
666 findingFeatures = true;
668 if (av.featuresDisplayed == null)
670 av.featuresDisplayed = new Hashtable();
673 allfeatures = new Vector();
674 Vector oldfeatures = new Vector();
675 if (renderOrder != null)
677 for (int i = 0; i < renderOrder.length; i++)
679 if (renderOrder[i] != null)
681 oldfeatures.addElement(renderOrder[i]);
687 minmax = new Hashtable();
689 AlignmentI alignment=av.getAlignment();
690 for (int i = 0; i < alignment.getHeight(); i++)
692 SequenceFeature[] features = alignment.getSequenceAt(i)
693 .getDatasetSequence().getSequenceFeatures();
695 if (features == null)
701 while (index < features.length)
703 if (!av.featuresDisplayed.containsKey(features[index].getType()))
706 if (featureGroups.containsKey(features[index].getType()))
708 boolean visible = ((Boolean) featureGroups
709 .get(features[index].featureGroup)).booleanValue();
718 if (!(features[index].begin == 0 && features[index].end == 0))
720 // If beginning and end are 0, the feature is for the whole sequence
721 // and we don't want to render the feature in the normal way
724 && !oldfeatures.contains(features[index].getType()))
726 // this is a new feature type on the alignment. Mark it for
728 av.featuresDisplayed.put(features[index].getType(),
729 new Integer(getColour(features[index].getType())
731 setOrder(features[index].getType(), 0);
735 if (!allfeatures.contains(features[index].getType()))
737 allfeatures.addElement(features[index].getType());
739 if (features[index].score != Float.NaN)
741 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
742 float[][] mm = (float[][]) minmax.get(features[index].getType());
747 minmax.put(features[index].getType(), mm);
749 if (mm[nonpos] == null)
751 mm[nonpos] = new float[]
752 { features[index].score, features[index].score };
757 if (mm[nonpos][0] > features[index].score)
759 mm[nonpos][0] = features[index].score;
761 if (mm[nonpos][1] < features[index].score)
763 mm[nonpos][1] = features[index].score;
770 updateRenderOrder(allfeatures);
771 findingFeatures = false;
774 protected Boolean firing = Boolean.FALSE;
777 * replaces the current renderOrder with the unordered features in
778 * allfeatures. The ordering of any types in both renderOrder and allfeatures
779 * is preserved, and all new feature types are rendered on top of the existing
780 * types, in the order given by getOrder or the order given in allFeatures.
781 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
782 * eliminate the float storage for computing/recalling the persistent ordering
783 * New Cability: updates min/max for colourscheme range if its dynamic
787 private void updateRenderOrder(Vector allFeatures)
789 Vector allfeatures = new Vector(allFeatures);
790 String[] oldRender = renderOrder;
791 renderOrder = new String[allfeatures.size()];
792 Object mmrange, fc = null;
793 boolean initOrders = (featureOrder == null);
795 if (oldRender != null && oldRender.length > 0)
797 for (int j = 0; j < oldRender.length; j++)
799 if (oldRender[j] != null)
803 setOrder(oldRender[j], (1 - (1 + (float) j)
804 / (float) oldRender.length));
806 if (allfeatures.contains(oldRender[j]))
808 renderOrder[opos++] = oldRender[j]; // existing features always
809 // appear below new features
810 allfeatures.removeElement(oldRender[j]);
813 mmrange = minmax.get(oldRender[j]);
816 fc = featureColours.get(oldRender[j]);
817 if (fc != null && fc instanceof GraduatedColor
818 && ((GraduatedColor) fc).isAutoScale())
820 ((GraduatedColor) fc).updateBounds(
821 ((float[][]) mmrange)[0][0],
822 ((float[][]) mmrange)[0][1]);
830 if (allfeatures.size() == 0)
832 // no new features - leave order unchanged.
835 int i = allfeatures.size() - 1;
837 boolean sort = false;
838 String[] newf = new String[allfeatures.size()];
839 float[] sortOrder = new float[allfeatures.size()];
840 Enumeration en = allfeatures.elements();
841 // sort remaining elements
842 while (en.hasMoreElements())
844 newf[i] = en.nextElement().toString();
847 // update from new features minmax if necessary
848 mmrange = minmax.get(newf[i]);
851 fc = featureColours.get(newf[i]);
852 if (fc != null && fc instanceof GraduatedColor
853 && ((GraduatedColor) fc).isAutoScale())
855 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
856 ((float[][]) mmrange)[0][1]);
860 if (initOrders || !featureOrder.containsKey(newf[i]))
862 int denom = initOrders ? allfeatures.size() : featureOrder.size();
863 // new unordered feature - compute persistent ordering at head of
864 // existing features.
865 setOrder(newf[i], i / (float) denom);
867 // set order from newly found feature from persisted ordering.
868 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
871 // only sort if we need to
872 sort = sort || sortOrder[i] > sortOrder[i + 1];
876 if (iSize > 1 && sort)
878 jalview.util.QuickSort.sort(sortOrder, newf);
881 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
885 * get a feature style object for the given type string. Creates a
886 * java.awt.Color for a featureType with no existing colourscheme. TODO:
887 * replace return type with object implementing standard abstract colour/style
891 * @return java.awt.Color or GraduatedColor
893 public Object getFeatureStyle(String featureType)
895 Object fc = featureColours.get(featureType);
898 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
899 Color col = ucs.createColourFromName(featureType);
900 featureColours.put(featureType, fc = col);
906 * return a nominal colour for this feature
909 * @return standard color, or maximum colour for graduated colourscheme
911 public Color getColour(String featureType)
913 Object fc = getFeatureStyle(featureType);
915 if (fc instanceof Color)
921 if (fc instanceof GraduatedColor)
923 return ((GraduatedColor) fc).getMaxColor();
926 throw new Error("Implementation Error: Unrecognised render object "
927 + fc.getClass() + " for features of type " + featureType);
931 * calculate the render colour for a specific feature using current feature
935 * @return render colour for the given feature
937 public Color getColour(SequenceFeature feature)
939 Object fc = getFeatureStyle(feature.getType());
940 if (fc instanceof Color)
946 if (fc instanceof GraduatedColor)
948 return ((GraduatedColor) fc).findColor(feature);
951 throw new Error("Implementation Error: Unrecognised render object "
952 + fc.getClass() + " for features of type " + feature.getType());
955 private boolean showFeature(SequenceFeature sequenceFeature)
957 Object fc = getFeatureStyle(sequenceFeature.type);
958 if (fc instanceof GraduatedColor)
960 return ((GraduatedColor) fc).isColored(sequenceFeature);
969 // // Feature Editing Dialog
970 // // Will be refactored in next release.
972 static String lastFeatureAdded;
974 static String lastFeatureGroupAdded;
976 static String lastDescriptionAdded;
980 int featureIndex = 0;
982 boolean amendFeatures(final SequenceI[] sequences,
983 final SequenceFeature[] features, boolean newFeatures,
984 final AlignmentPanel ap)
989 final JPanel bigPanel = new JPanel(new BorderLayout());
990 final JComboBox overlaps;
991 final JTextField name = new JTextField(25);
992 final JTextField source = new JTextField(25);
993 final JTextArea description = new JTextArea(3, 25);
994 final JSpinner start = new JSpinner();
995 final JSpinner end = new JSpinner();
996 start.setPreferredSize(new Dimension(80, 20));
997 end.setPreferredSize(new Dimension(80, 20));
998 final FeatureRenderer me = this;
999 final JLabel colour = new JLabel();
1000 colour.setOpaque(true);
1001 // colour.setBorder(BorderFactory.createEtchedBorder());
1002 colour.setMaximumSize(new Dimension(30, 16));
1003 colour.addMouseListener(new MouseAdapter()
1005 FeatureColourChooser fcc = null;
1007 public void mousePressed(MouseEvent evt)
1009 if (fcol instanceof Color)
1011 Color col = JColorChooser.showDialog(Desktop.desktop,
1012 "Select Feature Colour", ((Color) fcol));
1016 updateColourButton(bigPanel, colour, col);
1024 final String type = features[featureIndex].getType();
1025 fcc = new FeatureColourChooser(me, type);
1026 fcc.setRequestFocusEnabled(true);
1029 fcc.addActionListener(new ActionListener()
1032 public void actionPerformed(ActionEvent e)
1034 fcol = fcc.getLastColour();
1036 setColour(type, fcol);
1037 updateColourButton(bigPanel, colour, fcol);
1045 JPanel tmp = new JPanel();
1046 JPanel panel = new JPanel(new GridLayout(3, 1));
1048 // /////////////////////////////////////
1049 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1050 if (!newFeatures && features.length > 1)
1052 panel = new JPanel(new GridLayout(4, 1));
1054 tmp.add(new JLabel("Select Feature: "));
1055 overlaps = new JComboBox();
1056 for (int i = 0; i < features.length; i++)
1058 overlaps.addItem(features[i].getType() + "/"
1059 + features[i].getBegin() + "-" + features[i].getEnd()
1060 + " (" + features[i].getFeatureGroup() + ")");
1065 overlaps.addItemListener(new ItemListener()
1067 public void itemStateChanged(ItemEvent e)
1069 int index = overlaps.getSelectedIndex();
1072 featureIndex = index;
1073 name.setText(features[index].getType());
1074 description.setText(features[index].getDescription());
1075 source.setText(features[index].getFeatureGroup());
1076 start.setValue(new Integer(features[index].getBegin()));
1077 end.setValue(new Integer(features[index].getEnd()));
1079 SearchResults highlight = new SearchResults();
1080 highlight.addResult(sequences[0], features[index].getBegin(),
1081 features[index].getEnd());
1083 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1086 Object col = getFeatureStyle(name.getText());
1089 col = new jalview.schemes.UserColourScheme()
1090 .createColourFromName(name.getText());
1092 oldcol = fcol = col;
1093 updateColourButton(bigPanel, colour, col);
1100 // ////////////////////////////////////
1104 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1109 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1114 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1116 colour.setPreferredSize(new Dimension(150, 15));
1117 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1118 colour.setForeground(Color.black);
1119 colour.setHorizontalAlignment(SwingConstants.CENTER);
1120 colour.setVerticalAlignment(SwingConstants.CENTER);
1121 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1122 colour.setVerticalTextPosition(SwingConstants.CENTER);
1123 bigPanel.add(panel, BorderLayout.NORTH);
1125 panel = new JPanel();
1126 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1127 description.setFont(JvSwingUtils.getTextAreaFont());
1128 description.setLineWrap(true);
1129 panel.add(new JScrollPane(description));
1133 bigPanel.add(panel, BorderLayout.SOUTH);
1135 panel = new JPanel();
1136 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1138 panel.add(new JLabel(" End:", JLabel.RIGHT));
1140 bigPanel.add(panel, BorderLayout.CENTER);
1144 bigPanel.add(panel, BorderLayout.CENTER);
1147 if (lastFeatureAdded == null)
1149 if (features[0].type != null)
1151 lastFeatureAdded = features[0].type;
1155 lastFeatureAdded = "feature_1";
1159 if (lastFeatureGroupAdded == null)
1161 if (features[0].featureGroup != null)
1163 lastFeatureGroupAdded = features[0].featureGroup;
1167 lastFeatureGroupAdded = "Jalview";
1173 name.setText(lastFeatureAdded);
1174 source.setText(lastFeatureGroupAdded);
1178 name.setText(features[0].getType());
1179 source.setText(features[0].getFeatureGroup());
1182 start.setValue(new Integer(features[0].getBegin()));
1183 end.setValue(new Integer(features[0].getEnd()));
1184 description.setText(features[0].getDescription());
1185 updateColourButton(bigPanel, colour,
1186 (oldcol = fcol = getFeatureStyle(name.getText())));
1190 options = new Object[]
1191 { "Amend", "Delete", "Cancel" };
1195 options = new Object[]
1199 String title = newFeatures ? "Create New Sequence Feature(s)"
1200 : "Amend/Delete Features for " + sequences[0].getName();
1202 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1203 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1204 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1206 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1208 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1210 // This ensures that the last sequence
1211 // is refreshed and new features are rendered
1213 lastFeatureAdded = name.getText().trim();
1214 lastFeatureGroupAdded = source.getText().trim();
1215 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1216 // TODO: determine if the null feature group is valid
1217 if (lastFeatureGroupAdded.length() < 1)
1218 lastFeatureGroupAdded = null;
1223 SequenceFeature sf = features[featureIndex];
1225 if (reply == JOptionPane.NO_OPTION)
1227 sequences[0].getDatasetSequence().deleteFeature(sf);
1229 else if (reply == JOptionPane.YES_OPTION)
1231 sf.type = lastFeatureAdded;
1232 sf.featureGroup = lastFeatureGroupAdded;
1233 sf.description = lastDescriptionAdded;
1235 setColour(sf.type, fcol);
1236 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1240 sf.begin = ((Integer) start.getValue()).intValue();
1241 sf.end = ((Integer) end.getValue()).intValue();
1242 } catch (NumberFormatException ex)
1246 ffile.parseDescriptionHTML(sf, false);
1250 // NEW FEATURES ADDED
1252 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1254 for (int i = 0; i < sequences.length; i++)
1256 features[i].type = lastFeatureAdded;
1257 if (lastFeatureGroupAdded != null)
1258 features[i].featureGroup = lastFeatureGroupAdded;
1259 features[i].description = lastDescriptionAdded;
1260 sequences[i].addSequenceFeature(features[i]);
1261 ffile.parseDescriptionHTML(features[i], false);
1264 if (av.featuresDisplayed == null)
1266 av.featuresDisplayed = new Hashtable();
1269 if (lastFeatureGroupAdded != null)
1271 if (featureGroups == null)
1272 featureGroups = new Hashtable();
1273 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1275 setColour(lastFeatureAdded, fcol);
1276 av.featuresDisplayed.put(lastFeatureAdded,
1277 getColour(lastFeatureAdded));
1279 findAllFeatures(false);
1281 ap.paintAlignment(true);
1291 ap.paintAlignment(true);
1297 * update the amend feature button dependent on the given style
1303 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1307 colour.setIcon(null);
1308 colour.setToolTipText(null);
1311 if (col2 instanceof Color)
1313 colour.setBackground((Color) col2);
1317 colour.setBackground(bigPanel.getBackground());
1318 colour.setForeground(Color.black);
1319 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1320 // colour.setForeground(colour.getBackground());
1324 public void setColour(String featureType, Object col)
1327 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1328 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1329 // Object c = featureColours.get(featureType);
1330 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1331 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1333 featureColours.put(featureType, col);
1337 public void setTransparency(float value)
1339 transparency = value;
1342 public float getTransparency()
1344 return transparency;
1348 * Replace current ordering with new ordering
1351 * { String(Type), Colour(Type), Boolean(Displayed) }
1353 public void setFeaturePriority(Object[][] data)
1355 setFeaturePriority(data, true);
1361 * { String(Type), Colour(Type), Boolean(Displayed) }
1363 * when true current featureDisplay list will be cleared
1365 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1369 if (av.featuresDisplayed != null)
1371 av.featuresDisplayed.clear();
1375 av.featuresDisplayed = new Hashtable();
1383 // The feature table will display high priority
1384 // features at the top, but theses are the ones
1385 // we need to render last, so invert the data
1386 renderOrder = new String[data.length];
1388 if (data.length > 0)
1390 for (int i = 0; i < data.length; i++)
1392 String type = data[i][0].toString();
1393 setColour(type, data[i][1]); // todo : typesafety - feature color
1395 if (((Boolean) data[i][2]).booleanValue())
1397 av.featuresDisplayed.put(type, new Integer(getColour(type)
1401 renderOrder[data.length - i - 1] = type;
1407 Map featureOrder = null;
1410 * analogous to colour - store a normalized ordering for all feature types in
1411 * this rendering context.
1414 * Feature type string
1416 * normalized priority - 0 means always appears on top, 1 means
1419 public float setOrder(String type, float position)
1421 if (featureOrder == null)
1423 featureOrder = new Hashtable();
1425 featureOrder.put(type, new Float(position));
1430 * get the global priority (0 (top) to 1 (bottom))
1433 * @return [0,1] or -1 for a type without a priority
1435 public float getOrder(String type)
1437 if (featureOrder != null)
1439 if (featureOrder.containsKey(type))
1441 return ((Float) featureOrder.get(type)).floatValue();
1449 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1451 public void addPropertyChangeListener(PropertyChangeListener listener)
1453 changeSupport.addPropertyChangeListener(listener);
1458 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1460 public void removePropertyChangeListener(PropertyChangeListener listener)
1462 changeSupport.removePropertyChangeListener(listener);