2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
24 import java.awt.image.*;
25 import java.beans.PropertyChangeListener;
26 import java.beans.PropertyChangeSupport;
30 import jalview.datamodel.*;
31 import jalview.schemes.GraduatedColor;
39 public class FeatureRenderer implements jalview.api.FeatureRenderer
48 * global transparency for feature
50 float transparency = 1.0f;
56 Hashtable featureColours = new Hashtable();
58 // A higher level for grouping features of a
60 Hashtable featureGroups = new Hashtable();
62 // This is actually an Integer held in the hashtable,
63 // Retrieved using the key feature type
68 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
73 * Creates a new FeatureRenderer object.
78 public FeatureRenderer(AlignmentPanel ap)
84 public class FeatureRendererSettings implements Cloneable
88 Hashtable featureGroups;
90 Hashtable featureColours;
94 Hashtable featureOrder;
96 public FeatureRendererSettings(String[] renderOrder,
97 Hashtable featureGroups, Hashtable featureColours,
98 float transparency, Hashtable featureOrder)
101 this.renderOrder = renderOrder;
102 this.featureGroups = featureGroups;
103 this.featureColours = featureColours;
104 this.transparency = transparency;
105 this.featureOrder = featureOrder;
109 * create an independent instance of the feature renderer settings
113 public FeatureRendererSettings(FeatureRenderer fr)
116 featureGroups = new Hashtable();
117 featureColours = new Hashtable();
118 featureOrder = new Hashtable();
119 if (fr.renderOrder != null)
121 this.renderOrder = new String[fr.renderOrder.length];
122 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
125 if (fr.featureGroups != null)
127 this.featureGroups = new Hashtable(fr.featureGroups);
129 if (fr.featureColours != null)
131 this.featureColours = new Hashtable(fr.featureColours);
133 Enumeration en = fr.featureColours.keys();
134 while (en.hasMoreElements())
136 Object next = en.nextElement();
137 Object val = featureColours.get(next);
138 if (val instanceof GraduatedColor)
141 .put(next, new GraduatedColor((GraduatedColor) val));
144 this.transparency = fr.transparency;
145 if (fr.featureOrder != null)
147 this.featureOrder = new Hashtable(fr.featureOrder);
152 public FeatureRendererSettings getSettings()
154 return new FeatureRendererSettings(this);
157 public void transferSettings(FeatureRendererSettings fr)
159 this.renderOrder = fr.renderOrder;
160 this.featureGroups = fr.featureGroups;
161 this.featureColours = fr.featureColours;
162 this.transparency = fr.transparency;
163 this.featureOrder = fr.featureOrder;
166 public void transferSettings(FeatureRenderer fr)
168 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
169 this.renderOrder = frs.renderOrder;
170 this.featureGroups = frs.featureGroups;
171 this.featureColours = frs.featureColours;
172 this.transparency = frs.transparency;
173 this.featureOrder = frs.featureOrder;
174 if (av != null && av!=fr.av)
176 // copy over the displayed feature settings
179 if (fr.av.featuresDisplayed != null)
181 // update display settings
182 if (av.featuresDisplayed == null)
184 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
188 av.featuresDisplayed.clear();
189 Enumeration en = fr.av.featuresDisplayed.keys();
190 while (en.hasMoreElements())
192 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
201 BufferedImage offscreenImage;
203 boolean offscreenRender = false;
205 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
207 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
211 * This is used by the Molecule Viewer and Overview to get the accurate
212 * colourof the rendered sequence
214 public synchronized int findFeatureColour(int initialCol, SequenceI seq, int column)
216 if (!av.showSequenceFeatures)
224 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
225 if (sequenceFeatures != null)
227 sfSize = sequenceFeatures.length;
231 if (sequenceFeatures != lastSeq.getDatasetSequence()
232 .getSequenceFeatures())
234 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
235 if (sequenceFeatures != null)
237 sfSize = sequenceFeatures.length;
241 if (sequenceFeatures == null || sfSize == 0)
246 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
248 return Color.white.getRGB();
251 // Only bother making an offscreen image if transparency is applied
252 if (transparency != 1.0f && offscreenImage == null)
254 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
257 currentColour = null;
258 // TODO: non-threadsafe - each rendering thread needs its own instance of the feature renderer - or this should be synchronized.
259 offscreenRender = true;
261 if (offscreenImage != null)
263 offscreenImage.setRGB(0, 0, initialCol);
264 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
266 return offscreenImage.getRGB(0, 0);
270 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
272 if (currentColour == null)
278 return ((Integer) currentColour).intValue();
307 // SequenceFeature sf;
310 SequenceFeature[] sequenceFeatures;
312 int sfSize, sfindex, spos, epos;
315 * show scores as heights
317 protected boolean varyHeight = false;
319 synchronized public void drawSequence(Graphics g, SequenceI seq,
320 int start, int end, int y1)
323 if (seq.getDatasetSequence().getSequenceFeatures() == null
324 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
331 fm = g.getFontMetrics();
334 if (av.featuresDisplayed == null || renderOrder == null
338 if (av.featuresDisplayed.size() < 1)
343 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
348 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
351 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
354 if (transparency != 1 && g != null)
356 Graphics2D g2 = (Graphics2D) g;
357 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
361 if (!offscreenRender)
363 spos = lastSeq.findPosition(start);
364 epos = lastSeq.findPosition(end);
367 sfSize = sequenceFeatures.length;
369 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
371 type = renderOrder[renderIndex];
373 if (type == null || !av.featuresDisplayed.containsKey(type))
378 // loop through all features in sequence to find
379 // current feature to render
380 for (sfindex = 0; sfindex < sfSize; sfindex++)
382 if (!sequenceFeatures[sfindex].type.equals(type))
387 if (featureGroups != null
388 && sequenceFeatures[sfindex].featureGroup != null
389 && sequenceFeatures[sfindex].featureGroup.length() != 0
391 .containsKey(sequenceFeatures[sfindex].featureGroup)
392 && !((Boolean) featureGroups
393 .get(sequenceFeatures[sfindex].featureGroup))
400 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
406 if (offscreenRender && offscreenImage == null)
408 if (sequenceFeatures[sfindex].begin <= start
409 && sequenceFeatures[sfindex].end >= start)
411 // this is passed out to the overview and other sequence renderers
412 // (e.g. molecule viewer) to get displayed colour for rendered
414 currentColour = new Integer(
415 getColour(sequenceFeatures[sfindex]).getRGB());
416 // used to be retreived from av.featuresDisplayed
417 // currentColour = av.featuresDisplayed
418 // .get(sequenceFeatures[sfindex].type);
422 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
425 renderFeature(g, seq,
426 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
427 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
428 getColour(sequenceFeatures[sfindex])
429 // new Color(((Integer) av.featuresDisplayed
430 // .get(sequenceFeatures[sfindex].type)).intValue())
432 renderFeature(g, seq,
433 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
434 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
435 getColour(sequenceFeatures[sfindex])
436 // new Color(((Integer) av.featuresDisplayed
437 // .get(sequenceFeatures[sfindex].type)).intValue())
441 else if (showFeature(sequenceFeatures[sfindex]))
443 if (av.showSeqFeaturesHeight
444 && sequenceFeatures[sfindex].score != Float.NaN)
446 renderScoreFeature(g, seq,
447 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
448 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
449 getColour(sequenceFeatures[sfindex]), start, end, y1,
450 normaliseScore(sequenceFeatures[sfindex]));
454 renderFeature(g, seq,
455 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
456 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
457 getColour(sequenceFeatures[sfindex]), start, end, y1);
465 if (transparency != 1.0f && g != null)
467 Graphics2D g2 = (Graphics2D) g;
468 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
473 Hashtable minmax = new Hashtable();
476 * normalise a score against the max/min bounds for the feature type.
478 * @param sequenceFeature
479 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
482 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
484 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
485 final byte[] r = new byte[]
489 if (r[0] != 0 || mm[0] < 0.0)
492 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
496 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
506 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
507 Color featureColour, int start, int end, int y1)
510 if (((fstart <= end) && (fend >= start)))
513 { // fix for if the feature we have starts before the sequence start,
514 fstart = start; // but the feature end is still valid!!
521 int pady = (y1 + av.charHeight) - av.charHeight / 5;
522 for (i = fstart; i <= fend; i++)
524 s = seq.getCharAt(i);
526 if (jalview.util.Comparison.isGap(s))
531 g.setColor(featureColour);
533 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
536 if (offscreenRender || !av.validCharWidth)
541 g.setColor(Color.white);
542 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
543 g.drawString(String.valueOf(s), charOffset
544 + (av.charWidth * (i - start)), pady);
550 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
551 Color featureColour, int start, int end, int y1, byte[] bs)
554 if (((fstart <= end) && (fend >= start)))
557 { // fix for if the feature we have starts before the sequence start,
558 fstart = start; // but the feature end is still valid!!
565 int pady = (y1 + av.charHeight) - av.charHeight / 5;
566 int ystrt = 0, yend = av.charHeight;
569 // signed - zero is always middle of residue line.
572 yend = av.charHeight * (128 - bs[1]) / 512;
573 ystrt = av.charHeight - yend / 2;
577 ystrt = av.charHeight / 2;
578 yend = av.charHeight * (bs[1] - 128) / 512;
583 yend = av.charHeight * bs[1] / 255;
584 ystrt = av.charHeight - yend;
587 for (i = fstart; i <= fend; i++)
589 s = seq.getCharAt(i);
591 if (jalview.util.Comparison.isGap(s))
596 g.setColor(featureColour);
597 int x = (i - start) * av.charWidth;
598 g.drawRect(x, y1, av.charWidth, av.charHeight);
599 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
601 if (offscreenRender || !av.validCharWidth)
606 g.setColor(Color.black);
607 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
608 g.drawString(String.valueOf(s), charOffset
609 + (av.charWidth * (i - start)), pady);
615 boolean newFeatureAdded = false;
618 * Called when alignment in associated view has new/modified features to
619 * discover and display.
622 public void featuresAdded()
628 boolean findingFeatures = false;
631 * search the alignment for all new features, give them a colour and display
632 * them. Then fires a PropertyChangeEvent on the changeSupport object.
635 void findAllFeatures()
637 synchronized (firing)
639 if (firing.equals(Boolean.FALSE))
641 firing = Boolean.TRUE;
642 findAllFeatures(true); // add all new features as visible
643 changeSupport.firePropertyChange("changeSupport", null, null);
644 firing = Boolean.FALSE;
650 * Searches alignment for all features and updates colours
652 * @param newMadeVisible
653 * if true newly added feature types will be rendered immediatly
655 synchronized void findAllFeatures(boolean newMadeVisible)
657 newFeatureAdded = false;
661 newFeatureAdded = true;
665 findingFeatures = true;
667 if (av.featuresDisplayed == null)
669 av.featuresDisplayed = new Hashtable();
672 allfeatures = new Vector();
673 Vector oldfeatures = new Vector();
674 if (renderOrder != null)
676 for (int i = 0; i < renderOrder.length; i++)
678 if (renderOrder[i] != null)
680 oldfeatures.addElement(renderOrder[i]);
686 minmax = new Hashtable();
688 for (int i = 0; i < av.alignment.getHeight(); i++)
690 SequenceFeature[] features = av.alignment.getSequenceAt(i)
691 .getDatasetSequence().getSequenceFeatures();
693 if (features == null)
699 while (index < features.length)
701 if (!av.featuresDisplayed.containsKey(features[index].getType()))
704 if (featureGroups.containsKey(features[index].getType()))
706 boolean visible = ((Boolean) featureGroups
707 .get(features[index].featureGroup)).booleanValue();
716 if (!(features[index].begin == 0 && features[index].end == 0))
718 // If beginning and end are 0, the feature is for the whole sequence
719 // and we don't want to render the feature in the normal way
722 && !oldfeatures.contains(features[index].getType()))
724 // this is a new feature type on the alignment. Mark it for
726 av.featuresDisplayed.put(features[index].getType(),
727 new Integer(getColour(features[index].getType())
729 setOrder(features[index].getType(), 0);
733 if (!allfeatures.contains(features[index].getType()))
735 allfeatures.addElement(features[index].getType());
737 if (features[index].score != Float.NaN)
739 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
740 float[][] mm = (float[][]) minmax.get(features[index].getType());
745 minmax.put(features[index].getType(), mm);
747 if (mm[nonpos] == null)
749 mm[nonpos] = new float[]
750 { features[index].score, features[index].score };
755 if (mm[nonpos][0] > features[index].score)
757 mm[nonpos][0] = features[index].score;
759 if (mm[nonpos][1] < features[index].score)
761 mm[nonpos][1] = features[index].score;
768 updateRenderOrder(allfeatures);
769 findingFeatures = false;
772 protected Boolean firing = Boolean.FALSE;
775 * replaces the current renderOrder with the unordered features in
776 * allfeatures. The ordering of any types in both renderOrder and allfeatures
777 * is preserved, and all new feature types are rendered on top of the existing
778 * types, in the order given by getOrder or the order given in allFeatures.
779 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
780 * eliminate the float storage for computing/recalling the persistent ordering
781 * New Cability: updates min/max for colourscheme range if its dynamic
785 private void updateRenderOrder(Vector allFeatures)
787 Vector allfeatures = new Vector(allFeatures);
788 String[] oldRender = renderOrder;
789 renderOrder = new String[allfeatures.size()];
790 Object mmrange, fc = null;
791 boolean initOrders = (featureOrder == null);
793 if (oldRender != null && oldRender.length > 0)
795 for (int j = 0; j < oldRender.length; j++)
797 if (oldRender[j] != null)
801 setOrder(oldRender[j], (1 - (1 + (float) j)
802 / (float) oldRender.length));
804 if (allfeatures.contains(oldRender[j]))
806 renderOrder[opos++] = oldRender[j]; // existing features always
807 // appear below new features
808 allfeatures.removeElement(oldRender[j]);
811 mmrange = minmax.get(oldRender[j]);
814 fc = featureColours.get(oldRender[j]);
815 if (fc != null && fc instanceof GraduatedColor
816 && ((GraduatedColor) fc).isAutoScale())
818 ((GraduatedColor) fc).updateBounds(
819 ((float[][]) mmrange)[0][0],
820 ((float[][]) mmrange)[0][1]);
828 if (allfeatures.size() == 0)
830 // no new features - leave order unchanged.
833 int i = allfeatures.size() - 1;
835 boolean sort = false;
836 String[] newf = new String[allfeatures.size()];
837 float[] sortOrder = new float[allfeatures.size()];
838 Enumeration en = allfeatures.elements();
839 // sort remaining elements
840 while (en.hasMoreElements())
842 newf[i] = en.nextElement().toString();
845 // update from new features minmax if necessary
846 mmrange = minmax.get(newf[i]);
849 fc = featureColours.get(newf[i]);
850 if (fc != null && fc instanceof GraduatedColor
851 && ((GraduatedColor) fc).isAutoScale())
853 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
854 ((float[][]) mmrange)[0][1]);
858 if (initOrders || !featureOrder.containsKey(newf[i]))
860 int denom = initOrders ? allfeatures.size() : featureOrder.size();
861 // new unordered feature - compute persistent ordering at head of
862 // existing features.
863 setOrder(newf[i], i / (float) denom);
865 // set order from newly found feature from persisted ordering.
866 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
869 // only sort if we need to
870 sort = sort || sortOrder[i] > sortOrder[i + 1];
874 if (iSize > 1 && sort)
876 jalview.util.QuickSort.sort(sortOrder, newf);
879 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
883 * get a feature style object for the given type string. Creates a
884 * java.awt.Color for a featureType with no existing colourscheme. TODO:
885 * replace return type with object implementing standard abstract colour/style
889 * @return java.awt.Color or GraduatedColor
891 public Object getFeatureStyle(String featureType)
893 Object fc = featureColours.get(featureType);
896 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
897 Color col = ucs.createColourFromName(featureType);
898 featureColours.put(featureType, fc = col);
904 * return a nominal colour for this feature
907 * @return standard color, or maximum colour for graduated colourscheme
909 public Color getColour(String featureType)
911 Object fc = getFeatureStyle(featureType);
913 if (fc instanceof Color)
919 if (fc instanceof GraduatedColor)
921 return ((GraduatedColor) fc).getMaxColor();
924 throw new Error("Implementation Error: Unrecognised render object "
925 + fc.getClass() + " for features of type " + featureType);
929 * calculate the render colour for a specific feature using current feature
933 * @return render colour for the given feature
935 public Color getColour(SequenceFeature feature)
937 Object fc = getFeatureStyle(feature.getType());
938 if (fc instanceof Color)
944 if (fc instanceof GraduatedColor)
946 return ((GraduatedColor) fc).findColor(feature);
949 throw new Error("Implementation Error: Unrecognised render object "
950 + fc.getClass() + " for features of type " + feature.getType());
953 private boolean showFeature(SequenceFeature sequenceFeature)
955 Object fc = getFeatureStyle(sequenceFeature.type);
956 if (fc instanceof GraduatedColor)
958 return ((GraduatedColor) fc).isColored(sequenceFeature);
967 // // Feature Editing Dialog
968 // // Will be refactored in next release.
970 static String lastFeatureAdded;
972 static String lastFeatureGroupAdded;
974 static String lastDescriptionAdded;
978 int featureIndex = 0;
980 boolean amendFeatures(final SequenceI[] sequences,
981 final SequenceFeature[] features, boolean newFeatures,
982 final AlignmentPanel ap)
987 final JPanel bigPanel = new JPanel(new BorderLayout());
988 final JComboBox overlaps;
989 final JTextField name = new JTextField(25);
990 final JTextField source = new JTextField(25);
991 final JTextArea description = new JTextArea(3, 25);
992 final JSpinner start = new JSpinner();
993 final JSpinner end = new JSpinner();
994 start.setPreferredSize(new Dimension(80, 20));
995 end.setPreferredSize(new Dimension(80, 20));
996 final FeatureRenderer me = this;
997 final JLabel colour = new JLabel();
998 colour.setOpaque(true);
999 // colour.setBorder(BorderFactory.createEtchedBorder());
1000 colour.setMaximumSize(new Dimension(30, 16));
1001 colour.addMouseListener(new MouseAdapter()
1003 FeatureColourChooser fcc = null;
1005 public void mousePressed(MouseEvent evt)
1007 if (fcol instanceof Color)
1009 Color col = JColorChooser.showDialog(Desktop.desktop,
1010 "Select Feature Colour", ((Color) fcol));
1014 updateColourButton(bigPanel, colour, col);
1022 final String type = features[featureIndex].getType();
1023 fcc = new FeatureColourChooser(me, type);
1024 fcc.setRequestFocusEnabled(true);
1027 fcc.addActionListener(new ActionListener()
1030 public void actionPerformed(ActionEvent e)
1032 fcol = fcc.getLastColour();
1034 setColour(type, fcol);
1035 updateColourButton(bigPanel, colour, fcol);
1043 JPanel tmp = new JPanel();
1044 JPanel panel = new JPanel(new GridLayout(3, 1));
1046 // /////////////////////////////////////
1047 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1048 if (!newFeatures && features.length > 1)
1050 panel = new JPanel(new GridLayout(4, 1));
1052 tmp.add(new JLabel("Select Feature: "));
1053 overlaps = new JComboBox();
1054 for (int i = 0; i < features.length; i++)
1056 overlaps.addItem(features[i].getType() + "/"
1057 + features[i].getBegin() + "-" + features[i].getEnd()
1058 + " (" + features[i].getFeatureGroup() + ")");
1063 overlaps.addItemListener(new ItemListener()
1065 public void itemStateChanged(ItemEvent e)
1067 int index = overlaps.getSelectedIndex();
1070 featureIndex = index;
1071 name.setText(features[index].getType());
1072 description.setText(features[index].getDescription());
1073 source.setText(features[index].getFeatureGroup());
1074 start.setValue(new Integer(features[index].getBegin()));
1075 end.setValue(new Integer(features[index].getEnd()));
1077 SearchResults highlight = new SearchResults();
1078 highlight.addResult(sequences[0], features[index].getBegin(),
1079 features[index].getEnd());
1081 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1084 Object col = getFeatureStyle(name.getText());
1087 col = new jalview.schemes.UserColourScheme()
1088 .createColourFromName(name.getText());
1090 oldcol = fcol = col;
1091 updateColourButton(bigPanel, colour, col);
1098 // ////////////////////////////////////
1102 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
1107 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
1112 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
1114 colour.setPreferredSize(new Dimension(150, 15));
1115 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1116 colour.setForeground(Color.black);
1117 colour.setHorizontalAlignment(SwingConstants.CENTER);
1118 colour.setVerticalAlignment(SwingConstants.CENTER);
1119 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1120 colour.setVerticalTextPosition(SwingConstants.CENTER);
1121 bigPanel.add(panel, BorderLayout.NORTH);
1123 panel = new JPanel();
1124 panel.add(new JLabel("Description: ", JLabel.RIGHT));
1125 description.setFont(JvSwingUtils.getTextAreaFont());
1126 description.setLineWrap(true);
1127 panel.add(new JScrollPane(description));
1131 bigPanel.add(panel, BorderLayout.SOUTH);
1133 panel = new JPanel();
1134 panel.add(new JLabel(" Start:", JLabel.RIGHT));
1136 panel.add(new JLabel(" End:", JLabel.RIGHT));
1138 bigPanel.add(panel, BorderLayout.CENTER);
1142 bigPanel.add(panel, BorderLayout.CENTER);
1145 if (lastFeatureAdded == null)
1147 if (features[0].type != null)
1149 lastFeatureAdded = features[0].type;
1153 lastFeatureAdded = "feature_1";
1157 if (lastFeatureGroupAdded == null)
1159 if (features[0].featureGroup != null)
1161 lastFeatureGroupAdded = features[0].featureGroup;
1165 lastFeatureGroupAdded = "Jalview";
1171 name.setText(lastFeatureAdded);
1172 source.setText(lastFeatureGroupAdded);
1176 name.setText(features[0].getType());
1177 source.setText(features[0].getFeatureGroup());
1180 start.setValue(new Integer(features[0].getBegin()));
1181 end.setValue(new Integer(features[0].getEnd()));
1182 description.setText(features[0].getDescription());
1183 updateColourButton(bigPanel, colour,
1184 (oldcol = fcol = getFeatureStyle(name.getText())));
1188 options = new Object[]
1189 { "Amend", "Delete", "Cancel" };
1193 options = new Object[]
1197 String title = newFeatures ? "Create New Sequence Feature(s)"
1198 : "Amend/Delete Features for " + sequences[0].getName();
1200 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1201 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1202 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1204 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1206 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1208 // This ensures that the last sequence
1209 // is refreshed and new features are rendered
1211 lastFeatureAdded = name.getText().trim();
1212 lastFeatureGroupAdded = source.getText().trim();
1213 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1214 // TODO: determine if the null feature group is valid
1215 if (lastFeatureGroupAdded.length() < 1)
1216 lastFeatureGroupAdded = null;
1221 SequenceFeature sf = features[featureIndex];
1223 if (reply == JOptionPane.NO_OPTION)
1225 sequences[0].getDatasetSequence().deleteFeature(sf);
1227 else if (reply == JOptionPane.YES_OPTION)
1229 sf.type = lastFeatureAdded;
1230 sf.featureGroup = lastFeatureGroupAdded;
1231 sf.description = lastDescriptionAdded;
1233 setColour(sf.type, fcol);
1234 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1238 sf.begin = ((Integer) start.getValue()).intValue();
1239 sf.end = ((Integer) end.getValue()).intValue();
1240 } catch (NumberFormatException ex)
1244 ffile.parseDescriptionHTML(sf, false);
1248 // NEW FEATURES ADDED
1250 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1252 for (int i = 0; i < sequences.length; i++)
1254 features[i].type = lastFeatureAdded;
1255 if (lastFeatureGroupAdded != null)
1256 features[i].featureGroup = lastFeatureGroupAdded;
1257 features[i].description = lastDescriptionAdded;
1258 sequences[i].addSequenceFeature(features[i]);
1259 ffile.parseDescriptionHTML(features[i], false);
1262 if (av.featuresDisplayed == null)
1264 av.featuresDisplayed = new Hashtable();
1267 if (lastFeatureGroupAdded != null)
1269 if (featureGroups == null)
1270 featureGroups = new Hashtable();
1271 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1273 setColour(lastFeatureAdded, fcol);
1274 av.featuresDisplayed.put(lastFeatureAdded,
1275 getColour(lastFeatureAdded));
1277 findAllFeatures(false);
1279 ap.paintAlignment(true);
1289 ap.paintAlignment(true);
1295 * update the amend feature button dependent on the given style
1301 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1305 colour.setIcon(null);
1306 colour.setToolTipText(null);
1309 if (col2 instanceof Color)
1311 colour.setBackground((Color) col2);
1315 colour.setBackground(bigPanel.getBackground());
1316 colour.setForeground(Color.black);
1317 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1318 // colour.setForeground(colour.getBackground());
1322 public void setColour(String featureType, Object col)
1325 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1326 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1327 // Object c = featureColours.get(featureType);
1328 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1329 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1331 featureColours.put(featureType, col);
1335 public void setTransparency(float value)
1337 transparency = value;
1340 public float getTransparency()
1342 return transparency;
1346 * Replace current ordering with new ordering
1349 * { String(Type), Colour(Type), Boolean(Displayed) }
1351 public void setFeaturePriority(Object[][] data)
1353 setFeaturePriority(data, true);
1359 * { String(Type), Colour(Type), Boolean(Displayed) }
1361 * when true current featureDisplay list will be cleared
1363 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1367 if (av.featuresDisplayed != null)
1369 av.featuresDisplayed.clear();
1373 av.featuresDisplayed = new Hashtable();
1381 // The feature table will display high priority
1382 // features at the top, but theses are the ones
1383 // we need to render last, so invert the data
1384 renderOrder = new String[data.length];
1386 if (data.length > 0)
1388 for (int i = 0; i < data.length; i++)
1390 String type = data[i][0].toString();
1391 setColour(type, data[i][1]); // todo : typesafety - feature color
1393 if (((Boolean) data[i][2]).booleanValue())
1395 av.featuresDisplayed.put(type, new Integer(getColour(type)
1399 renderOrder[data.length - i - 1] = type;
1405 Hashtable featureOrder = null;
1408 * analogous to colour - store a normalized ordering for all feature types in
1409 * this rendering context.
1412 * Feature type string
1414 * normalized priority - 0 means always appears on top, 1 means
1417 public float setOrder(String type, float position)
1419 if (featureOrder == null)
1421 featureOrder = new Hashtable();
1423 featureOrder.put(type, new Float(position));
1428 * get the global priority (0 (top) to 1 (bottom))
1431 * @return [0,1] or -1 for a type without a priority
1433 public float getOrder(String type)
1435 if (featureOrder != null)
1437 if (featureOrder.containsKey(type))
1439 return ((Float) featureOrder.get(type)).floatValue();
1447 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1449 public void addPropertyChangeListener(PropertyChangeListener listener)
1451 changeSupport.addPropertyChangeListener(listener);
1456 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1458 public void removePropertyChangeListener(PropertyChangeListener listener)
1460 changeSupport.removePropertyChangeListener(listener);