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)
83 if (ap != null && ap.seqPanel != null && ap.seqPanel.seqCanvas != null
84 && ap.seqPanel.seqCanvas.fr != null)
86 transferSettings(ap.seqPanel.seqCanvas.fr);
90 public class FeatureRendererSettings implements Cloneable
102 public FeatureRendererSettings(String[] renderOrder,
103 Hashtable featureGroups, Hashtable featureColours,
104 float transparency, Hashtable featureOrder)
107 this.renderOrder = renderOrder;
108 this.featureGroups = featureGroups;
109 this.featureColours = featureColours;
110 this.transparency = transparency;
111 this.featureOrder = featureOrder;
115 * create an independent instance of the feature renderer settings
119 public FeatureRendererSettings(FeatureRenderer fr)
122 featureGroups = new ConcurrentHashMap();
123 featureColours = new ConcurrentHashMap();
124 featureOrder = new ConcurrentHashMap();
125 if (fr.renderOrder != null)
127 this.renderOrder = new String[fr.renderOrder.length];
128 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
129 fr.renderOrder.length);
131 if (fr.featureGroups != null)
133 this.featureGroups = new ConcurrentHashMap(fr.featureGroups);
135 if (fr.featureColours != null)
137 this.featureColours = new ConcurrentHashMap(fr.featureColours);
139 Iterator en = fr.featureColours.keySet().iterator();
142 Object next = en.next();
143 Object val = featureColours.get(next);
144 if (val instanceof GraduatedColor)
147 .put(next, new GraduatedColor((GraduatedColor) val));
150 this.transparency = fr.transparency;
151 if (fr.featureOrder != null)
153 this.featureOrder = new ConcurrentHashMap(fr.featureOrder);
158 public FeatureRendererSettings getSettings()
160 return new FeatureRendererSettings(this);
163 public void transferSettings(FeatureRendererSettings fr)
165 this.renderOrder = fr.renderOrder;
166 this.featureGroups = fr.featureGroups;
167 this.featureColours = fr.featureColours;
168 this.transparency = fr.transparency;
169 this.featureOrder = fr.featureOrder;
172 * update from another feature renderer
173 * @param fr settings to copy
175 public void transferSettings(FeatureRenderer fr)
177 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
178 this.renderOrder = frs.renderOrder;
179 this.featureGroups = frs.featureGroups;
180 this.featureColours = frs.featureColours;
181 this.transparency = frs.transparency;
182 this.featureOrder = frs.featureOrder;
183 if (av != null && av != fr.av)
185 // copy over the displayed feature settings
188 if (fr.av.featuresDisplayed != null)
190 // update display settings
191 if (av.featuresDisplayed == null)
193 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
197 av.featuresDisplayed.clear();
198 Enumeration en = fr.av.featuresDisplayed.keys();
199 while (en.hasMoreElements())
201 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
210 BufferedImage offscreenImage;
212 boolean offscreenRender = false;
214 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
216 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
220 * This is used by the Molecule Viewer and Overview to get the accurate
221 * colourof the rendered sequence
223 public synchronized int findFeatureColour(int initialCol, SequenceI seq,
226 if (!av.showSequenceFeatures)
234 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
235 if (sequenceFeatures != null)
237 sfSize = sequenceFeatures.length;
241 if (sequenceFeatures != lastSeq.getDatasetSequence()
242 .getSequenceFeatures())
244 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
245 if (sequenceFeatures != null)
247 sfSize = sequenceFeatures.length;
251 if (sequenceFeatures == null || sfSize == 0)
256 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
258 return Color.white.getRGB();
261 // Only bother making an offscreen image if transparency is applied
262 if (transparency != 1.0f && offscreenImage == null)
264 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
267 currentColour = null;
268 // TODO: non-threadsafe - each rendering thread needs its own instance of
269 // the feature renderer - or this should be synchronized.
270 offscreenRender = true;
272 if (offscreenImage != null)
274 offscreenImage.setRGB(0, 0, initialCol);
275 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
277 return offscreenImage.getRGB(0, 0);
281 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
283 if (currentColour == null)
289 return ((Integer) currentColour).intValue();
318 // SequenceFeature sf;
321 SequenceFeature[] sequenceFeatures;
323 int sfSize, sfindex, spos, epos;
326 * show scores as heights
328 protected boolean varyHeight = false;
330 synchronized public void drawSequence(Graphics g, SequenceI seq,
331 int start, int end, int y1)
334 if (seq.getDatasetSequence().getSequenceFeatures() == null
335 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
342 fm = g.getFontMetrics();
345 if (av.featuresDisplayed == null || renderOrder == null
349 if (av.featuresDisplayed.size() < 1)
354 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
359 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
362 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
365 if (transparency != 1 && g != null)
367 Graphics2D g2 = (Graphics2D) g;
368 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
372 if (!offscreenRender)
374 spos = lastSeq.findPosition(start);
375 epos = lastSeq.findPosition(end);
378 sfSize = sequenceFeatures.length;
380 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
382 type = renderOrder[renderIndex];
384 if (type == null || !av.featuresDisplayed.containsKey(type))
389 // loop through all features in sequence to find
390 // current feature to render
391 for (sfindex = 0; sfindex < sfSize; sfindex++)
393 if (!sequenceFeatures[sfindex].type.equals(type))
398 if (featureGroups != null
399 && sequenceFeatures[sfindex].featureGroup != null
400 && sequenceFeatures[sfindex].featureGroup.length() != 0
402 .containsKey(sequenceFeatures[sfindex].featureGroup)
403 && !((Boolean) featureGroups
404 .get(sequenceFeatures[sfindex].featureGroup))
411 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
417 if (offscreenRender && offscreenImage == null)
419 if (sequenceFeatures[sfindex].begin <= start
420 && sequenceFeatures[sfindex].end >= start)
422 // this is passed out to the overview and other sequence renderers
423 // (e.g. molecule viewer) to get displayed colour for rendered
425 currentColour = new Integer(
426 getColour(sequenceFeatures[sfindex]).getRGB());
427 // used to be retreived from av.featuresDisplayed
428 // currentColour = av.featuresDisplayed
429 // .get(sequenceFeatures[sfindex].type);
433 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
436 renderFeature(g, seq,
437 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
438 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
439 getColour(sequenceFeatures[sfindex])
440 // new Color(((Integer) av.featuresDisplayed
441 // .get(sequenceFeatures[sfindex].type)).intValue())
443 renderFeature(g, seq,
444 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
445 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
446 getColour(sequenceFeatures[sfindex])
447 // new Color(((Integer) av.featuresDisplayed
448 // .get(sequenceFeatures[sfindex].type)).intValue())
452 else if (showFeature(sequenceFeatures[sfindex]))
454 if (av.showSeqFeaturesHeight
455 && sequenceFeatures[sfindex].score != Float.NaN)
457 renderScoreFeature(g, seq,
458 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
459 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
460 getColour(sequenceFeatures[sfindex]), start, end, y1,
461 normaliseScore(sequenceFeatures[sfindex]));
465 renderFeature(g, seq,
466 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
467 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
468 getColour(sequenceFeatures[sfindex]), start, end, y1);
476 if (transparency != 1.0f && g != null)
478 Graphics2D g2 = (Graphics2D) g;
479 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
484 Hashtable minmax = new Hashtable();
487 * normalise a score against the max/min bounds for the feature type.
489 * @param sequenceFeature
490 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
493 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
495 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
496 final byte[] r = new byte[]
500 if (r[0] != 0 || mm[0] < 0.0)
503 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
507 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
517 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
518 Color featureColour, int start, int end, int y1)
521 if (((fstart <= end) && (fend >= start)))
524 { // fix for if the feature we have starts before the sequence start,
525 fstart = start; // but the feature end is still valid!!
532 int pady = (y1 + av.charHeight) - av.charHeight / 5;
533 for (i = fstart; i <= fend; i++)
535 s = seq.getCharAt(i);
537 if (jalview.util.Comparison.isGap(s))
542 g.setColor(featureColour);
544 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
547 if (offscreenRender || !av.validCharWidth)
552 g.setColor(Color.white);
553 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
554 g.drawString(String.valueOf(s), charOffset
555 + (av.charWidth * (i - start)), pady);
561 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
562 Color featureColour, int start, int end, int y1, byte[] bs)
565 if (((fstart <= end) && (fend >= start)))
568 { // fix for if the feature we have starts before the sequence start,
569 fstart = start; // but the feature end is still valid!!
576 int pady = (y1 + av.charHeight) - av.charHeight / 5;
577 int ystrt = 0, yend = av.charHeight;
580 // signed - zero is always middle of residue line.
583 yend = av.charHeight * (128 - bs[1]) / 512;
584 ystrt = av.charHeight - yend / 2;
588 ystrt = av.charHeight / 2;
589 yend = av.charHeight * (bs[1] - 128) / 512;
594 yend = av.charHeight * bs[1] / 255;
595 ystrt = av.charHeight - yend;
598 for (i = fstart; i <= fend; i++)
600 s = seq.getCharAt(i);
602 if (jalview.util.Comparison.isGap(s))
607 g.setColor(featureColour);
608 int x = (i - start) * av.charWidth;
609 g.drawRect(x, y1, av.charWidth, av.charHeight);
610 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
612 if (offscreenRender || !av.validCharWidth)
617 g.setColor(Color.black);
618 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
619 g.drawString(String.valueOf(s), charOffset
620 + (av.charWidth * (i - start)), pady);
626 boolean newFeatureAdded = false;
629 * Called when alignment in associated view has new/modified features to
630 * discover and display.
633 public void featuresAdded()
639 boolean findingFeatures = false;
642 * search the alignment for all new features, give them a colour and display
643 * them. Then fires a PropertyChangeEvent on the changeSupport object.
646 void findAllFeatures()
648 synchronized (firing)
650 if (firing.equals(Boolean.FALSE))
652 firing = Boolean.TRUE;
653 findAllFeatures(true); // add all new features as visible
654 changeSupport.firePropertyChange("changeSupport", null, null);
655 firing = Boolean.FALSE;
661 * Searches alignment for all features and updates colours
663 * @param newMadeVisible
664 * if true newly added feature types will be rendered immediatly
666 synchronized void findAllFeatures(boolean newMadeVisible)
668 newFeatureAdded = false;
672 newFeatureAdded = true;
676 findingFeatures = true;
678 if (av.featuresDisplayed == null)
680 av.featuresDisplayed = new Hashtable();
683 allfeatures = new Vector();
684 Vector oldfeatures = new Vector();
685 if (renderOrder != null)
687 for (int i = 0; i < renderOrder.length; i++)
689 if (renderOrder[i] != null)
691 oldfeatures.addElement(renderOrder[i]);
697 minmax = new Hashtable();
699 AlignmentI alignment = av.getAlignment();
700 for (int i = 0; i < alignment.getHeight(); i++)
702 SequenceFeature[] features = alignment.getSequenceAt(i)
703 .getDatasetSequence().getSequenceFeatures();
705 if (features == null)
711 while (index < features.length)
713 if (!av.featuresDisplayed.containsKey(features[index].getType()))
716 if (featureGroups.containsKey(features[index].getType()))
718 boolean visible = ((Boolean) featureGroups
719 .get(features[index].featureGroup)).booleanValue();
728 if (!(features[index].begin == 0 && features[index].end == 0))
730 // If beginning and end are 0, the feature is for the whole sequence
731 // and we don't want to render the feature in the normal way
734 && !oldfeatures.contains(features[index].getType()))
736 // this is a new feature type on the alignment. Mark it for
738 av.featuresDisplayed.put(features[index].getType(),
739 new Integer(getColour(features[index].getType())
741 setOrder(features[index].getType(), 0);
745 if (!allfeatures.contains(features[index].getType()))
747 allfeatures.addElement(features[index].getType());
749 if (features[index].score != Float.NaN)
751 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
752 float[][] mm = (float[][]) minmax.get(features[index].getType());
757 minmax.put(features[index].getType(), mm);
759 if (mm[nonpos] == null)
761 mm[nonpos] = new float[]
762 { features[index].score, features[index].score };
767 if (mm[nonpos][0] > features[index].score)
769 mm[nonpos][0] = features[index].score;
771 if (mm[nonpos][1] < features[index].score)
773 mm[nonpos][1] = features[index].score;
780 updateRenderOrder(allfeatures);
781 findingFeatures = false;
784 protected Boolean firing = Boolean.FALSE;
787 * replaces the current renderOrder with the unordered features in
788 * allfeatures. The ordering of any types in both renderOrder and allfeatures
789 * is preserved, and all new feature types are rendered on top of the existing
790 * types, in the order given by getOrder or the order given in allFeatures.
791 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
792 * eliminate the float storage for computing/recalling the persistent ordering
793 * New Cability: updates min/max for colourscheme range if its dynamic
797 private void updateRenderOrder(Vector allFeatures)
799 Vector allfeatures = new Vector(allFeatures);
800 String[] oldRender = renderOrder;
801 renderOrder = new String[allfeatures.size()];
802 Object mmrange, fc = null;
803 boolean initOrders = (featureOrder == null);
805 if (oldRender != null && oldRender.length > 0)
807 for (int j = 0; j < oldRender.length; j++)
809 if (oldRender[j] != null)
813 setOrder(oldRender[j], (1 - (1 + (float) j)
814 / (float) oldRender.length));
816 if (allfeatures.contains(oldRender[j]))
818 renderOrder[opos++] = oldRender[j]; // existing features always
819 // appear below new features
820 allfeatures.removeElement(oldRender[j]);
823 mmrange = minmax.get(oldRender[j]);
826 fc = featureColours.get(oldRender[j]);
827 if (fc != null && fc instanceof GraduatedColor
828 && ((GraduatedColor) fc).isAutoScale())
830 ((GraduatedColor) fc).updateBounds(
831 ((float[][]) mmrange)[0][0],
832 ((float[][]) mmrange)[0][1]);
840 if (allfeatures.size() == 0)
842 // no new features - leave order unchanged.
845 int i = allfeatures.size() - 1;
847 boolean sort = false;
848 String[] newf = new String[allfeatures.size()];
849 float[] sortOrder = new float[allfeatures.size()];
850 Enumeration en = allfeatures.elements();
851 // sort remaining elements
852 while (en.hasMoreElements())
854 newf[i] = en.nextElement().toString();
857 // update from new features minmax if necessary
858 mmrange = minmax.get(newf[i]);
861 fc = featureColours.get(newf[i]);
862 if (fc != null && fc instanceof GraduatedColor
863 && ((GraduatedColor) fc).isAutoScale())
865 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
866 ((float[][]) mmrange)[0][1]);
870 if (initOrders || !featureOrder.containsKey(newf[i]))
872 int denom = initOrders ? allfeatures.size() : featureOrder.size();
873 // new unordered feature - compute persistent ordering at head of
874 // existing features.
875 setOrder(newf[i], i / (float) denom);
877 // set order from newly found feature from persisted ordering.
878 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
881 // only sort if we need to
882 sort = sort || sortOrder[i] > sortOrder[i + 1];
886 if (iSize > 1 && sort)
888 jalview.util.QuickSort.sort(sortOrder, newf);
891 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
895 * get a feature style object for the given type string. Creates a
896 * java.awt.Color for a featureType with no existing colourscheme. TODO:
897 * replace return type with object implementing standard abstract colour/style
901 * @return java.awt.Color or GraduatedColor
903 public Object getFeatureStyle(String featureType)
905 Object fc = featureColours.get(featureType);
908 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
909 Color col = ucs.createColourFromName(featureType);
910 featureColours.put(featureType, fc = col);
916 * return a nominal colour for this feature
919 * @return standard color, or maximum colour for graduated colourscheme
921 public Color getColour(String featureType)
923 Object fc = getFeatureStyle(featureType);
925 if (fc instanceof Color)
931 if (fc instanceof GraduatedColor)
933 return ((GraduatedColor) fc).getMaxColor();
936 throw new Error("Implementation Error: Unrecognised render object "
937 + fc.getClass() + " for features of type " + featureType);
941 * calculate the render colour for a specific feature using current feature
945 * @return render colour for the given feature
947 public Color getColour(SequenceFeature feature)
949 Object fc = getFeatureStyle(feature.getType());
950 if (fc instanceof Color)
956 if (fc instanceof GraduatedColor)
958 return ((GraduatedColor) fc).findColor(feature);
961 throw new Error("Implementation Error: Unrecognised render object "
962 + fc.getClass() + " for features of type " + feature.getType());
965 private boolean showFeature(SequenceFeature sequenceFeature)
967 Object fc = getFeatureStyle(sequenceFeature.type);
968 if (fc instanceof GraduatedColor)
970 return ((GraduatedColor) fc).isColored(sequenceFeature);
979 // // Feature Editing Dialog
980 // // Will be refactored in next release.
982 static String lastFeatureAdded;
984 static String lastFeatureGroupAdded;
986 static String lastDescriptionAdded;
990 int featureIndex = 0;
992 boolean amendFeatures(final SequenceI[] sequences,
993 final SequenceFeature[] features, boolean newFeatures,
994 final AlignmentPanel ap)
999 final JPanel bigPanel = new JPanel(new BorderLayout());
1000 final JComboBox overlaps;
1001 final JTextField name = new JTextField(25);
1002 final JTextField source = new JTextField(25);
1003 final JTextArea description = new JTextArea(3, 25);
1004 final JSpinner start = new JSpinner();
1005 final JSpinner end = new JSpinner();
1006 start.setPreferredSize(new Dimension(80, 20));
1007 end.setPreferredSize(new Dimension(80, 20));
1008 final FeatureRenderer me = this;
1009 final JLabel colour = new JLabel();
1010 colour.setOpaque(true);
1011 // colour.setBorder(BorderFactory.createEtchedBorder());
1012 colour.setMaximumSize(new Dimension(30, 16));
1013 colour.addMouseListener(new MouseAdapter()
1015 FeatureColourChooser fcc = null;
1017 public void mousePressed(MouseEvent evt)
1019 if (fcol instanceof Color)
1021 Color col = JColorChooser.showDialog(Desktop.desktop,
1022 "Select Feature Colour", ((Color) fcol));
1026 updateColourButton(bigPanel, colour, col);
1034 final String type = features[featureIndex].getType();
1035 fcc = new FeatureColourChooser(me, type);
1036 fcc.setRequestFocusEnabled(true);
1039 fcc.addActionListener(new ActionListener()
1042 public void actionPerformed(ActionEvent e)
1044 fcol = fcc.getLastColour();
1046 setColour(type, fcol);
1047 updateColourButton(bigPanel, colour, fcol);
1055 JPanel tmp = new JPanel();
1056 JPanel panel = new JPanel(new GridLayout(3, 1));
1058 // /////////////////////////////////////
1059 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1060 if (!newFeatures && features.length > 1)
1062 panel = new JPanel(new GridLayout(4, 1));
1064 tmp.add(new JLabel("Select Feature: "));
1065 overlaps = new JComboBox();
1066 for (int i = 0; i < features.length; i++)
1068 overlaps.addItem(features[i].getType() + "/"
1069 + features[i].getBegin() + "-" + features[i].getEnd()
1070 + " (" + features[i].getFeatureGroup() + ")");
1075 overlaps.addItemListener(new ItemListener()
1077 public void itemStateChanged(ItemEvent e)
1079 int index = overlaps.getSelectedIndex();
1082 featureIndex = index;
1083 name.setText(features[index].getType());
1084 description.setText(features[index].getDescription());
1085 source.setText(features[index].getFeatureGroup());
1086 start.setValue(new Integer(features[index].getBegin()));
1087 end.setValue(new Integer(features[index].getEnd()));
1089 SearchResults highlight = new SearchResults();
1090 highlight.addResult(sequences[0], features[index].getBegin(),
1091 features[index].getEnd());
1093 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1096 Object col = getFeatureStyle(name.getText());
1099 col = new jalview.schemes.UserColourScheme()
1100 .createColourFromName(name.getText());
1102 oldcol = fcol = col;
1103 updateColourButton(bigPanel, colour, col);
1110 // ////////////////////////////////////
1114 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1119 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1124 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1126 colour.setPreferredSize(new Dimension(150, 15));
1127 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1128 colour.setForeground(Color.black);
1129 colour.setHorizontalAlignment(SwingConstants.CENTER);
1130 colour.setVerticalAlignment(SwingConstants.CENTER);
1131 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1132 colour.setVerticalTextPosition(SwingConstants.CENTER);
1133 bigPanel.add(panel, BorderLayout.NORTH);
1135 panel = new JPanel();
1136 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1137 description.setFont(JvSwingUtils.getTextAreaFont());
1138 description.setLineWrap(true);
1139 panel.add(new JScrollPane(description));
1143 bigPanel.add(panel, BorderLayout.SOUTH);
1145 panel = new JPanel();
1146 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1148 panel.add(new JLabel(" End:", JLabel.RIGHT));
1150 bigPanel.add(panel, BorderLayout.CENTER);
1154 bigPanel.add(panel, BorderLayout.CENTER);
1157 if (lastFeatureAdded == null)
1159 if (features[0].type != null)
1161 lastFeatureAdded = features[0].type;
1165 lastFeatureAdded = "feature_1";
1169 if (lastFeatureGroupAdded == null)
1171 if (features[0].featureGroup != null)
1173 lastFeatureGroupAdded = features[0].featureGroup;
1177 lastFeatureGroupAdded = "Jalview";
1183 name.setText(lastFeatureAdded);
1184 source.setText(lastFeatureGroupAdded);
1188 name.setText(features[0].getType());
1189 source.setText(features[0].getFeatureGroup());
1192 start.setValue(new Integer(features[0].getBegin()));
1193 end.setValue(new Integer(features[0].getEnd()));
1194 description.setText(features[0].getDescription());
1195 updateColourButton(bigPanel, colour,
1196 (oldcol = fcol = getFeatureStyle(name.getText())));
1200 options = new Object[]
1201 { "Amend", "Delete", "Cancel" };
1205 options = new Object[]
1209 String title = newFeatures ? "Create New Sequence Feature(s)"
1210 : "Amend/Delete Features for " + sequences[0].getName();
1212 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1213 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1214 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1216 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1218 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1220 // This ensures that the last sequence
1221 // is refreshed and new features are rendered
1223 lastFeatureAdded = name.getText().trim();
1224 lastFeatureGroupAdded = source.getText().trim();
1225 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1226 // TODO: determine if the null feature group is valid
1227 if (lastFeatureGroupAdded.length() < 1)
1228 lastFeatureGroupAdded = null;
1233 SequenceFeature sf = features[featureIndex];
1235 if (reply == JOptionPane.NO_OPTION)
1237 sequences[0].getDatasetSequence().deleteFeature(sf);
1239 else if (reply == JOptionPane.YES_OPTION)
1241 sf.type = lastFeatureAdded;
1242 sf.featureGroup = lastFeatureGroupAdded;
1243 sf.description = lastDescriptionAdded;
1245 setColour(sf.type, fcol);
1246 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1250 sf.begin = ((Integer) start.getValue()).intValue();
1251 sf.end = ((Integer) end.getValue()).intValue();
1252 } catch (NumberFormatException ex)
1256 ffile.parseDescriptionHTML(sf, false);
1260 // NEW FEATURES ADDED
1262 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1264 for (int i = 0; i < sequences.length; i++)
1266 features[i].type = lastFeatureAdded;
1267 if (lastFeatureGroupAdded != null)
1268 features[i].featureGroup = lastFeatureGroupAdded;
1269 features[i].description = lastDescriptionAdded;
1270 sequences[i].addSequenceFeature(features[i]);
1271 ffile.parseDescriptionHTML(features[i], false);
1274 if (av.featuresDisplayed == null)
1276 av.featuresDisplayed = new Hashtable();
1279 if (lastFeatureGroupAdded != null)
1281 if (featureGroups == null)
1282 featureGroups = new Hashtable();
1283 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1285 setColour(lastFeatureAdded, fcol);
1286 av.featuresDisplayed.put(lastFeatureAdded,
1287 getColour(lastFeatureAdded));
1289 findAllFeatures(false);
1291 ap.paintAlignment(true);
1301 ap.paintAlignment(true);
1307 * update the amend feature button dependent on the given style
1313 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1317 colour.setIcon(null);
1318 colour.setToolTipText(null);
1321 if (col2 instanceof Color)
1323 colour.setBackground((Color) col2);
1327 colour.setBackground(bigPanel.getBackground());
1328 colour.setForeground(Color.black);
1329 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1330 // colour.setForeground(colour.getBackground());
1334 public void setColour(String featureType, Object col)
1337 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1338 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1339 // Object c = featureColours.get(featureType);
1340 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1341 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1343 featureColours.put(featureType, col);
1347 public void setTransparency(float value)
1349 transparency = value;
1352 public float getTransparency()
1354 return transparency;
1358 * Replace current ordering with new ordering
1361 * { String(Type), Colour(Type), Boolean(Displayed) }
1363 public void setFeaturePriority(Object[][] data)
1365 setFeaturePriority(data, true);
1371 * { String(Type), Colour(Type), Boolean(Displayed) }
1373 * when true current featureDisplay list will be cleared
1375 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1379 if (av.featuresDisplayed != null)
1381 av.featuresDisplayed.clear();
1385 av.featuresDisplayed = new Hashtable();
1393 // The feature table will display high priority
1394 // features at the top, but theses are the ones
1395 // we need to render last, so invert the data
1396 renderOrder = new String[data.length];
1398 if (data.length > 0)
1400 for (int i = 0; i < data.length; i++)
1402 String type = data[i][0].toString();
1403 setColour(type, data[i][1]); // todo : typesafety - feature color
1405 if (((Boolean) data[i][2]).booleanValue())
1407 av.featuresDisplayed.put(type, new Integer(getColour(type)
1411 renderOrder[data.length - i - 1] = type;
1417 Map featureOrder = null;
1420 * analogous to colour - store a normalized ordering for all feature types in
1421 * this rendering context.
1424 * Feature type string
1426 * normalized priority - 0 means always appears on top, 1 means
1429 public float setOrder(String type, float position)
1431 if (featureOrder == null)
1433 featureOrder = new Hashtable();
1435 featureOrder.put(type, new Float(position));
1440 * get the global priority (0 (top) to 1 (bottom))
1443 * @return [0,1] or -1 for a type without a priority
1445 public float getOrder(String type)
1447 if (featureOrder != null)
1449 if (featureOrder.containsKey(type))
1451 return ((Float) featureOrder.get(type)).floatValue();
1459 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1461 public void addPropertyChangeListener(PropertyChangeListener listener)
1463 changeSupport.addPropertyChangeListener(listener);
1468 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1470 public void removePropertyChangeListener(PropertyChangeListener listener)
1472 changeSupport.removePropertyChangeListener(listener);