2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.util.concurrent.ConcurrentHashMap;
27 import java.awt.event.*;
28 import java.awt.image.*;
29 import java.beans.PropertyChangeListener;
30 import java.beans.PropertyChangeSupport;
34 import jalview.datamodel.*;
35 import jalview.schemes.GraduatedColor;
36 import jalview.util.MessageManager;
44 public class FeatureRenderer implements jalview.api.FeatureRenderer
53 * global transparency for feature
55 float transparency = 1.0f;
61 Map featureColours = new ConcurrentHashMap();
63 // A higher level for grouping features of a
65 Map featureGroups = new ConcurrentHashMap();
67 // This is actually an Integer held in the hashtable,
68 // Retrieved using the key feature type
73 PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
78 * Creates a new FeatureRenderer object.
83 public FeatureRenderer(AlignmentPanel ap)
87 if (ap != null && ap.seqPanel != null && ap.seqPanel.seqCanvas != null
88 && ap.seqPanel.seqCanvas.fr != null)
90 transferSettings(ap.seqPanel.seqCanvas.fr);
94 public class FeatureRendererSettings implements Cloneable
106 public FeatureRendererSettings(String[] renderOrder,
107 Hashtable featureGroups, Hashtable featureColours,
108 float transparency, Hashtable featureOrder)
111 this.renderOrder = renderOrder;
112 this.featureGroups = featureGroups;
113 this.featureColours = featureColours;
114 this.transparency = transparency;
115 this.featureOrder = featureOrder;
119 * create an independent instance of the feature renderer settings
123 public FeatureRendererSettings(FeatureRenderer fr)
126 featureGroups = new ConcurrentHashMap();
127 featureColours = new ConcurrentHashMap();
128 featureOrder = new ConcurrentHashMap();
129 if (fr.renderOrder != null)
131 this.renderOrder = new String[fr.renderOrder.length];
132 System.arraycopy(fr.renderOrder, 0, renderOrder, 0,
133 fr.renderOrder.length);
135 if (fr.featureGroups != null)
137 this.featureGroups = new ConcurrentHashMap(fr.featureGroups);
139 if (fr.featureColours != null)
141 this.featureColours = new ConcurrentHashMap(fr.featureColours);
143 Iterator en = fr.featureColours.keySet().iterator();
146 Object next = en.next();
147 Object val = featureColours.get(next);
148 if (val instanceof GraduatedColor)
151 .put(next, new GraduatedColor((GraduatedColor) val));
154 this.transparency = fr.transparency;
155 if (fr.featureOrder != null)
157 this.featureOrder = new ConcurrentHashMap(fr.featureOrder);
162 public FeatureRendererSettings getSettings()
164 return new FeatureRendererSettings(this);
167 public void transferSettings(FeatureRendererSettings fr)
169 this.renderOrder = fr.renderOrder;
170 this.featureGroups = fr.featureGroups;
171 this.featureColours = fr.featureColours;
172 this.transparency = fr.transparency;
173 this.featureOrder = fr.featureOrder;
177 * update from another feature renderer
182 public void transferSettings(FeatureRenderer fr)
184 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
185 this.renderOrder = frs.renderOrder;
186 this.featureGroups = frs.featureGroups;
187 this.featureColours = frs.featureColours;
188 this.transparency = frs.transparency;
189 this.featureOrder = frs.featureOrder;
190 if (av != null && av != fr.av)
192 // copy over the displayed feature settings
195 if (fr.av.featuresDisplayed != null)
197 // update display settings
198 if (av.featuresDisplayed == null)
200 av.featuresDisplayed = new Hashtable(fr.av.featuresDisplayed);
204 av.featuresDisplayed.clear();
205 Enumeration en = fr.av.featuresDisplayed.keys();
206 while (en.hasMoreElements())
208 av.featuresDisplayed.put(en.nextElement(), Boolean.TRUE);
217 BufferedImage offscreenImage;
219 boolean offscreenRender = false;
221 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
223 return new Color(findFeatureColour(initialCol.getRGB(), seq, res));
227 * This is used by the Molecule Viewer and Overview to get the accurate
228 * colourof the rendered sequence
230 public synchronized int findFeatureColour(int initialCol, SequenceI seq,
233 if (!av.showSequenceFeatures)
241 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
242 if (sequenceFeatures != null)
244 sfSize = sequenceFeatures.length;
248 if (sequenceFeatures != lastSeq.getDatasetSequence()
249 .getSequenceFeatures())
251 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
252 if (sequenceFeatures != null)
254 sfSize = sequenceFeatures.length;
258 if (sequenceFeatures == null || sfSize == 0)
263 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
265 return Color.white.getRGB();
268 // Only bother making an offscreen image if transparency is applied
269 if (transparency != 1.0f && offscreenImage == null)
271 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
274 currentColour = null;
275 // TODO: non-threadsafe - each rendering thread needs its own instance of
276 // the feature renderer - or this should be synchronized.
277 offscreenRender = true;
279 if (offscreenImage != null)
281 offscreenImage.setRGB(0, 0, initialCol);
282 drawSequence(offscreenImage.getGraphics(), lastSeq, column, column, 0);
284 return offscreenImage.getRGB(0, 0);
288 drawSequence(null, lastSeq, lastSeq.findPosition(column), -1, -1);
290 if (currentColour == null)
296 return ((Integer) currentColour).intValue();
325 // SequenceFeature sf;
328 SequenceFeature[] sequenceFeatures;
330 int sfSize, sfindex, spos, epos;
333 * show scores as heights
335 protected boolean varyHeight = false;
337 synchronized public void drawSequence(Graphics g, SequenceI seq,
338 int start, int end, int y1)
341 if (seq.getDatasetSequence().getSequenceFeatures() == null
342 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
349 fm = g.getFontMetrics();
352 if (av.featuresDisplayed == null || renderOrder == null
356 if (av.featuresDisplayed.size() < 1)
361 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
366 || seq.getDatasetSequence().getSequenceFeatures() != sequenceFeatures)
369 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
372 if (transparency != 1 && g != null)
374 Graphics2D g2 = (Graphics2D) g;
375 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
379 if (!offscreenRender)
381 spos = lastSeq.findPosition(start);
382 epos = lastSeq.findPosition(end);
385 sfSize = sequenceFeatures.length;
387 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
389 type = renderOrder[renderIndex];
391 if (type == null || !av.featuresDisplayed.containsKey(type))
396 // loop through all features in sequence to find
397 // current feature to render
398 for (sfindex = 0; sfindex < sfSize; sfindex++)
400 if (!sequenceFeatures[sfindex].type.equals(type))
405 if (featureGroups != null
406 && sequenceFeatures[sfindex].featureGroup != null
407 && sequenceFeatures[sfindex].featureGroup.length() != 0
409 .containsKey(sequenceFeatures[sfindex].featureGroup)
410 && !((Boolean) featureGroups
411 .get(sequenceFeatures[sfindex].featureGroup))
418 && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex]
424 if (offscreenRender && offscreenImage == null)
426 if (sequenceFeatures[sfindex].begin <= start
427 && sequenceFeatures[sfindex].end >= start)
429 // this is passed out to the overview and other sequence renderers
430 // (e.g. molecule viewer) to get displayed colour for rendered
432 currentColour = new Integer(
433 getColour(sequenceFeatures[sfindex]).getRGB());
434 // used to be retreived from av.featuresDisplayed
435 // currentColour = av.featuresDisplayed
436 // .get(sequenceFeatures[sfindex].type);
440 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
443 renderFeature(g, seq,
444 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
445 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
446 getColour(sequenceFeatures[sfindex])
447 // new Color(((Integer) av.featuresDisplayed
448 // .get(sequenceFeatures[sfindex].type)).intValue())
450 renderFeature(g, seq,
451 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
452 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
453 getColour(sequenceFeatures[sfindex])
454 // new Color(((Integer) av.featuresDisplayed
455 // .get(sequenceFeatures[sfindex].type)).intValue())
459 else if (showFeature(sequenceFeatures[sfindex]))
461 if (av.showSeqFeaturesHeight
462 && sequenceFeatures[sfindex].score != Float.NaN)
464 renderScoreFeature(g, seq,
465 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
466 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
467 getColour(sequenceFeatures[sfindex]), start, end, y1,
468 normaliseScore(sequenceFeatures[sfindex]));
472 renderFeature(g, seq,
473 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
474 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
475 getColour(sequenceFeatures[sfindex]), start, end, y1);
483 if (transparency != 1.0f && g != null)
485 Graphics2D g2 = (Graphics2D) g;
486 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
491 Hashtable minmax = new Hashtable();
494 * normalise a score against the max/min bounds for the feature type.
496 * @param sequenceFeature
497 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
500 private final byte[] normaliseScore(SequenceFeature sequenceFeature)
502 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
503 final byte[] r = new byte[]
507 if (r[0] != 0 || mm[0] < 0.0)
510 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
514 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
524 void renderFeature(Graphics g, SequenceI seq, int fstart, int fend,
525 Color featureColour, int start, int end, int y1)
528 if (((fstart <= end) && (fend >= start)))
531 { // fix for if the feature we have starts before the sequence start,
532 fstart = start; // but the feature end is still valid!!
539 int pady = (y1 + av.charHeight) - av.charHeight / 5;
540 for (i = fstart; i <= fend; i++)
542 s = seq.getCharAt(i);
544 if (jalview.util.Comparison.isGap(s))
549 g.setColor(featureColour);
551 g.fillRect((i - start) * av.charWidth, y1, av.charWidth,
554 if (offscreenRender || !av.validCharWidth)
559 g.setColor(Color.white);
560 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
561 g.drawString(String.valueOf(s), charOffset
562 + (av.charWidth * (i - start)), pady);
568 void renderScoreFeature(Graphics g, SequenceI seq, int fstart, int fend,
569 Color featureColour, int start, int end, int y1, byte[] bs)
572 if (((fstart <= end) && (fend >= start)))
575 { // fix for if the feature we have starts before the sequence start,
576 fstart = start; // but the feature end is still valid!!
583 int pady = (y1 + av.charHeight) - av.charHeight / 5;
584 int ystrt = 0, yend = av.charHeight;
587 // signed - zero is always middle of residue line.
590 yend = av.charHeight * (128 - bs[1]) / 512;
591 ystrt = av.charHeight - yend / 2;
595 ystrt = av.charHeight / 2;
596 yend = av.charHeight * (bs[1] - 128) / 512;
601 yend = av.charHeight * bs[1] / 255;
602 ystrt = av.charHeight - yend;
605 for (i = fstart; i <= fend; i++)
607 s = seq.getCharAt(i);
609 if (jalview.util.Comparison.isGap(s))
614 g.setColor(featureColour);
615 int x = (i - start) * av.charWidth;
616 g.drawRect(x, y1, av.charWidth, av.charHeight);
617 g.fillRect(x, y1 + ystrt, av.charWidth, yend);
619 if (offscreenRender || !av.validCharWidth)
624 g.setColor(Color.black);
625 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
626 g.drawString(String.valueOf(s), charOffset
627 + (av.charWidth * (i - start)), pady);
633 boolean newFeatureAdded = false;
636 * Called when alignment in associated view has new/modified features to
637 * discover and display.
640 public void featuresAdded()
646 boolean findingFeatures = false;
649 * search the alignment for all new features, give them a colour and display
650 * them. Then fires a PropertyChangeEvent on the changeSupport object.
653 void findAllFeatures()
655 synchronized (firing)
657 if (firing.equals(Boolean.FALSE))
659 firing = Boolean.TRUE;
660 findAllFeatures(true); // add all new features as visible
661 changeSupport.firePropertyChange("changeSupport", null, null);
662 firing = Boolean.FALSE;
668 * Searches alignment for all features and updates colours
670 * @param newMadeVisible
671 * if true newly added feature types will be rendered immediatly
673 synchronized void findAllFeatures(boolean newMadeVisible)
675 newFeatureAdded = false;
679 newFeatureAdded = true;
683 findingFeatures = true;
685 if (av.featuresDisplayed == null)
687 av.featuresDisplayed = new Hashtable();
690 allfeatures = new Vector();
691 Vector oldfeatures = new Vector();
692 if (renderOrder != null)
694 for (int i = 0; i < renderOrder.length; i++)
696 if (renderOrder[i] != null)
698 oldfeatures.addElement(renderOrder[i]);
704 minmax = new Hashtable();
706 AlignmentI alignment = av.getAlignment();
707 for (int i = 0; i < alignment.getHeight(); i++)
709 SequenceFeature[] features = alignment.getSequenceAt(i)
710 .getDatasetSequence().getSequenceFeatures();
712 if (features == null)
718 while (index < features.length)
720 if (!av.featuresDisplayed.containsKey(features[index].getType()))
723 if (featureGroups.containsKey(features[index].getType()))
725 boolean visible = ((Boolean) featureGroups
726 .get(features[index].featureGroup)).booleanValue();
735 if (!(features[index].begin == 0 && features[index].end == 0))
737 // If beginning and end are 0, the feature is for the whole sequence
738 // and we don't want to render the feature in the normal way
741 && !oldfeatures.contains(features[index].getType()))
743 // this is a new feature type on the alignment. Mark it for
745 av.featuresDisplayed.put(features[index].getType(),
746 new Integer(getColour(features[index].getType())
748 setOrder(features[index].getType(), 0);
752 if (!allfeatures.contains(features[index].getType()))
754 allfeatures.addElement(features[index].getType());
756 if (features[index].score != Float.NaN)
758 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
759 float[][] mm = (float[][]) minmax.get(features[index].getType());
764 minmax.put(features[index].getType(), mm);
766 if (mm[nonpos] == null)
768 mm[nonpos] = new float[]
769 { features[index].score, features[index].score };
774 if (mm[nonpos][0] > features[index].score)
776 mm[nonpos][0] = features[index].score;
778 if (mm[nonpos][1] < features[index].score)
780 mm[nonpos][1] = features[index].score;
787 updateRenderOrder(allfeatures);
788 findingFeatures = false;
791 protected Boolean firing = Boolean.FALSE;
794 * replaces the current renderOrder with the unordered features in
795 * allfeatures. The ordering of any types in both renderOrder and allfeatures
796 * is preserved, and all new feature types are rendered on top of the existing
797 * types, in the order given by getOrder or the order given in allFeatures.
798 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
799 * eliminate the float storage for computing/recalling the persistent ordering
800 * New Cability: updates min/max for colourscheme range if its dynamic
804 private void updateRenderOrder(Vector allFeatures)
806 Vector allfeatures = new Vector(allFeatures);
807 String[] oldRender = renderOrder;
808 renderOrder = new String[allfeatures.size()];
809 Object mmrange, fc = null;
810 boolean initOrders = (featureOrder == null);
812 if (oldRender != null && oldRender.length > 0)
814 for (int j = 0; j < oldRender.length; j++)
816 if (oldRender[j] != null)
820 setOrder(oldRender[j], (1 - (1 + (float) j)
821 / (float) oldRender.length));
823 if (allfeatures.contains(oldRender[j]))
825 renderOrder[opos++] = oldRender[j]; // existing features always
826 // appear below new features
827 allfeatures.removeElement(oldRender[j]);
830 mmrange = minmax.get(oldRender[j]);
833 fc = featureColours.get(oldRender[j]);
834 if (fc != null && fc instanceof GraduatedColor
835 && ((GraduatedColor) fc).isAutoScale())
837 ((GraduatedColor) fc).updateBounds(
838 ((float[][]) mmrange)[0][0],
839 ((float[][]) mmrange)[0][1]);
847 if (allfeatures.size() == 0)
849 // no new features - leave order unchanged.
852 int i = allfeatures.size() - 1;
854 boolean sort = false;
855 String[] newf = new String[allfeatures.size()];
856 float[] sortOrder = new float[allfeatures.size()];
857 Enumeration en = allfeatures.elements();
858 // sort remaining elements
859 while (en.hasMoreElements())
861 newf[i] = en.nextElement().toString();
864 // update from new features minmax if necessary
865 mmrange = minmax.get(newf[i]);
868 fc = featureColours.get(newf[i]);
869 if (fc != null && fc instanceof GraduatedColor
870 && ((GraduatedColor) fc).isAutoScale())
872 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
873 ((float[][]) mmrange)[0][1]);
877 if (initOrders || !featureOrder.containsKey(newf[i]))
879 int denom = initOrders ? allfeatures.size() : featureOrder.size();
880 // new unordered feature - compute persistent ordering at head of
881 // existing features.
882 setOrder(newf[i], i / (float) denom);
884 // set order from newly found feature from persisted ordering.
885 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
888 // only sort if we need to
889 sort = sort || sortOrder[i] > sortOrder[i + 1];
893 if (iSize > 1 && sort)
895 jalview.util.QuickSort.sort(sortOrder, newf);
898 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
902 * get a feature style object for the given type string. Creates a
903 * java.awt.Color for a featureType with no existing colourscheme. TODO:
904 * replace return type with object implementing standard abstract colour/style
908 * @return java.awt.Color or GraduatedColor
910 public Object getFeatureStyle(String featureType)
912 Object fc = featureColours.get(featureType);
915 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
916 Color col = ucs.createColourFromName(featureType);
917 featureColours.put(featureType, fc = col);
923 * return a nominal colour for this feature
926 * @return standard color, or maximum colour for graduated colourscheme
928 public Color getColour(String featureType)
930 Object fc = getFeatureStyle(featureType);
932 if (fc instanceof Color)
938 if (fc instanceof GraduatedColor)
940 return ((GraduatedColor) fc).getMaxColor();
943 throw new Error("Implementation Error: Unrecognised render object "
944 + fc.getClass() + " for features of type " + featureType);
948 * calculate the render colour for a specific feature using current feature
952 * @return render colour for the given feature
954 public Color getColour(SequenceFeature feature)
956 Object fc = getFeatureStyle(feature.getType());
957 if (fc instanceof Color)
963 if (fc instanceof GraduatedColor)
965 return ((GraduatedColor) fc).findColor(feature);
968 throw new Error("Implementation Error: Unrecognised render object "
969 + fc.getClass() + " for features of type " + feature.getType());
972 private boolean showFeature(SequenceFeature sequenceFeature)
974 Object fc = getFeatureStyle(sequenceFeature.type);
975 if (fc instanceof GraduatedColor)
977 return ((GraduatedColor) fc).isColored(sequenceFeature);
986 // // Feature Editing Dialog
987 // // Will be refactored in next release.
989 static String lastFeatureAdded;
991 static String lastFeatureGroupAdded;
993 static String lastDescriptionAdded;
997 int featureIndex = 0;
999 boolean amendFeatures(final SequenceI[] sequences,
1000 final SequenceFeature[] features, boolean newFeatures,
1001 final AlignmentPanel ap)
1006 final JPanel bigPanel = new JPanel(new BorderLayout());
1007 final JComboBox overlaps;
1008 final JTextField name = new JTextField(25);
1009 final JTextField source = new JTextField(25);
1010 final JTextArea description = new JTextArea(3, 25);
1011 final JSpinner start = new JSpinner();
1012 final JSpinner end = new JSpinner();
1013 start.setPreferredSize(new Dimension(80, 20));
1014 end.setPreferredSize(new Dimension(80, 20));
1015 final FeatureRenderer me = this;
1016 final JLabel colour = new JLabel();
1017 colour.setOpaque(true);
1018 // colour.setBorder(BorderFactory.createEtchedBorder());
1019 colour.setMaximumSize(new Dimension(30, 16));
1020 colour.addMouseListener(new MouseAdapter()
1022 FeatureColourChooser fcc = null;
1024 public void mousePressed(MouseEvent evt)
1026 if (fcol instanceof Color)
1028 Color col = JColorChooser.showDialog(Desktop.desktop,
1029 "Select Feature Colour", ((Color) fcol));
1033 updateColourButton(bigPanel, colour, col);
1041 final String type = features[featureIndex].getType();
1042 fcc = new FeatureColourChooser(me, type);
1043 fcc.setRequestFocusEnabled(true);
1046 fcc.addActionListener(new ActionListener()
1049 public void actionPerformed(ActionEvent e)
1051 fcol = fcc.getLastColour();
1053 setColour(type, fcol);
1054 updateColourButton(bigPanel, colour, fcol);
1062 JPanel tmp = new JPanel();
1063 JPanel panel = new JPanel(new GridLayout(3, 1));
1065 // /////////////////////////////////////
1066 // /MULTIPLE FEATURES AT SELECTED RESIDUE
1067 if (!newFeatures && features.length > 1)
1069 panel = new JPanel(new GridLayout(4, 1));
1071 tmp.add(new JLabel(MessageManager.getString("label.select_feature")));
1072 overlaps = new JComboBox();
1073 for (int i = 0; i < features.length; i++)
1075 overlaps.addItem(features[i].getType() + "/"
1076 + features[i].getBegin() + "-" + features[i].getEnd()
1077 + " (" + features[i].getFeatureGroup() + ")");
1082 overlaps.addItemListener(new ItemListener()
1084 public void itemStateChanged(ItemEvent e)
1086 int index = overlaps.getSelectedIndex();
1089 featureIndex = index;
1090 name.setText(features[index].getType());
1091 description.setText(features[index].getDescription());
1092 source.setText(features[index].getFeatureGroup());
1093 start.setValue(new Integer(features[index].getBegin()));
1094 end.setValue(new Integer(features[index].getEnd()));
1096 SearchResults highlight = new SearchResults();
1097 highlight.addResult(sequences[0], features[index].getBegin(),
1098 features[index].getEnd());
1100 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
1103 Object col = getFeatureStyle(name.getText());
1106 col = new jalview.schemes.UserColourScheme()
1107 .createColourFromName(name.getText());
1109 oldcol = fcol = col;
1110 updateColourButton(bigPanel, colour, col);
1117 // ////////////////////////////////////
1121 tmp.add(new JLabel(MessageManager.getString("label.name"), JLabel.RIGHT));
1126 tmp.add(new JLabel(MessageManager.getString("label.group") + ":",
1132 tmp.add(new JLabel(MessageManager.getString("label.colour"),
1135 colour.setPreferredSize(new Dimension(150, 15));
1136 colour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 9));
1137 colour.setForeground(Color.black);
1138 colour.setHorizontalAlignment(SwingConstants.CENTER);
1139 colour.setVerticalAlignment(SwingConstants.CENTER);
1140 colour.setHorizontalTextPosition(SwingConstants.CENTER);
1141 colour.setVerticalTextPosition(SwingConstants.CENTER);
1142 bigPanel.add(panel, BorderLayout.NORTH);
1144 panel = new JPanel();
1145 panel.add(new JLabel(MessageManager.getString("label.description"),
1147 description.setFont(JvSwingUtils.getTextAreaFont());
1148 description.setLineWrap(true);
1149 panel.add(new JScrollPane(description));
1153 bigPanel.add(panel, BorderLayout.SOUTH);
1155 panel = new JPanel();
1156 panel.add(new JLabel(MessageManager.getString("label.start"),
1159 panel.add(new JLabel(MessageManager.getString("label.end"),
1162 bigPanel.add(panel, BorderLayout.CENTER);
1166 bigPanel.add(panel, BorderLayout.CENTER);
1169 if (lastFeatureAdded == null)
1171 if (features[0].type != null)
1173 lastFeatureAdded = features[0].type;
1177 lastFeatureAdded = "feature_1";
1181 if (lastFeatureGroupAdded == null)
1183 if (features[0].featureGroup != null)
1185 lastFeatureGroupAdded = features[0].featureGroup;
1189 lastFeatureGroupAdded = "Jalview";
1195 name.setText(lastFeatureAdded);
1196 source.setText(lastFeatureGroupAdded);
1200 name.setText(features[0].getType());
1201 source.setText(features[0].getFeatureGroup());
1204 start.setValue(new Integer(features[0].getBegin()));
1205 end.setValue(new Integer(features[0].getEnd()));
1206 description.setText(features[0].getDescription());
1207 updateColourButton(bigPanel, colour,
1208 (oldcol = fcol = getFeatureStyle(name.getText())));
1212 options = new Object[]
1213 { "Amend", "Delete", "Cancel" };
1217 options = new Object[]
1221 String title = newFeatures ? "Create New Sequence Feature(s)"
1222 : "Amend/Delete Features for " + sequences[0].getName();
1224 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
1225 bigPanel, title, JOptionPane.YES_NO_CANCEL_OPTION,
1226 JOptionPane.QUESTION_MESSAGE, null, options, "OK");
1228 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
1230 if (reply == JOptionPane.OK_OPTION && name.getText().length() > 0)
1232 // This ensures that the last sequence
1233 // is refreshed and new features are rendered
1235 lastFeatureAdded = name.getText().trim();
1236 lastFeatureGroupAdded = source.getText().trim();
1237 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
1238 // TODO: determine if the null feature group is valid
1239 if (lastFeatureGroupAdded.length() < 1)
1240 lastFeatureGroupAdded = null;
1245 SequenceFeature sf = features[featureIndex];
1247 if (reply == JOptionPane.NO_OPTION)
1249 sequences[0].getDatasetSequence().deleteFeature(sf);
1251 else if (reply == JOptionPane.YES_OPTION)
1253 sf.type = lastFeatureAdded;
1254 sf.featureGroup = lastFeatureGroupAdded;
1255 sf.description = lastDescriptionAdded;
1257 setColour(sf.type, fcol);
1258 av.featuresDisplayed.put(sf.type, getColour(sf.type));
1262 sf.begin = ((Integer) start.getValue()).intValue();
1263 sf.end = ((Integer) end.getValue()).intValue();
1264 } catch (NumberFormatException ex)
1268 ffile.parseDescriptionHTML(sf, false);
1272 // NEW FEATURES ADDED
1274 if (reply == JOptionPane.OK_OPTION && lastFeatureAdded.length() > 0)
1276 for (int i = 0; i < sequences.length; i++)
1278 features[i].type = lastFeatureAdded;
1279 if (lastFeatureGroupAdded != null)
1280 features[i].featureGroup = lastFeatureGroupAdded;
1281 features[i].description = lastDescriptionAdded;
1282 sequences[i].addSequenceFeature(features[i]);
1283 ffile.parseDescriptionHTML(features[i], false);
1286 if (av.featuresDisplayed == null)
1288 av.featuresDisplayed = new Hashtable();
1291 if (lastFeatureGroupAdded != null)
1293 if (featureGroups == null)
1294 featureGroups = new Hashtable();
1295 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
1297 setColour(lastFeatureAdded, fcol);
1298 av.featuresDisplayed.put(lastFeatureAdded,
1299 getColour(lastFeatureAdded));
1301 findAllFeatures(false);
1303 ap.paintAlignment(true);
1313 ap.paintAlignment(true);
1319 * update the amend feature button dependent on the given style
1325 protected void updateColourButton(JPanel bigPanel, JLabel colour,
1329 colour.setIcon(null);
1330 colour.setToolTipText(null);
1333 if (col2 instanceof Color)
1335 colour.setBackground((Color) col2);
1339 colour.setBackground(bigPanel.getBackground());
1340 colour.setForeground(Color.black);
1341 FeatureSettings.renderGraduatedColor(colour, (GraduatedColor) col2);
1342 // colour.setForeground(colour.getBackground());
1346 public void setColour(String featureType, Object col)
1349 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
1350 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
1351 // Object c = featureColours.get(featureType);
1352 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
1353 // !((GraduatedColor)c).getMaxColor().equals(_col)))
1355 featureColours.put(featureType, col);
1359 public void setTransparency(float value)
1361 transparency = value;
1364 public float getTransparency()
1366 return transparency;
1370 * Replace current ordering with new ordering
1373 * { String(Type), Colour(Type), Boolean(Displayed) }
1375 public void setFeaturePriority(Object[][] data)
1377 setFeaturePriority(data, true);
1383 * { String(Type), Colour(Type), Boolean(Displayed) }
1385 * when true current featureDisplay list will be cleared
1387 public void setFeaturePriority(Object[][] data, boolean visibleNew)
1391 if (av.featuresDisplayed != null)
1393 av.featuresDisplayed.clear();
1397 av.featuresDisplayed = new Hashtable();
1405 // The feature table will display high priority
1406 // features at the top, but theses are the ones
1407 // we need to render last, so invert the data
1408 renderOrder = new String[data.length];
1410 if (data.length > 0)
1412 for (int i = 0; i < data.length; i++)
1414 String type = data[i][0].toString();
1415 setColour(type, data[i][1]); // todo : typesafety - feature color
1417 if (((Boolean) data[i][2]).booleanValue())
1419 av.featuresDisplayed.put(type, new Integer(getColour(type)
1423 renderOrder[data.length - i - 1] = type;
1429 Map featureOrder = null;
1432 * analogous to colour - store a normalized ordering for all feature types in
1433 * this rendering context.
1436 * Feature type string
1438 * normalized priority - 0 means always appears on top, 1 means
1441 public float setOrder(String type, float position)
1443 if (featureOrder == null)
1445 featureOrder = new Hashtable();
1447 featureOrder.put(type, new Float(position));
1452 * get the global priority (0 (top) to 1 (bottom))
1455 * @return [0,1] or -1 for a type without a priority
1457 public float getOrder(String type)
1459 if (featureOrder != null)
1461 if (featureOrder.containsKey(type))
1463 return ((Float) featureOrder.get(type)).floatValue();
1471 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1473 public void addPropertyChangeListener(PropertyChangeListener listener)
1475 changeSupport.addPropertyChangeListener(listener);
1480 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1482 public void removePropertyChangeListener(PropertyChangeListener listener)
1484 changeSupport.removePropertyChangeListener(listener);