14e37af5955b05a1864dda50b0359dc2d66b37a6
[jalview.git] / src / jalview / gui / FeatureSettings.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.api.FeatureColourI;
24 import jalview.api.FeatureSettingsControllerI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.features.FeatureMatcher;
28 import jalview.datamodel.features.FeatureMatcherI;
29 import jalview.datamodel.features.FeatureMatcherSet;
30 import jalview.datamodel.features.FeatureMatcherSetI;
31 import jalview.gui.Help.HelpId;
32 import jalview.io.JalviewFileChooser;
33 import jalview.io.JalviewFileView;
34 import jalview.schemabinding.version2.Filter;
35 import jalview.schemabinding.version2.JalviewUserColours;
36 import jalview.schemabinding.version2.MatcherSet;
37 import jalview.schemes.FeatureColour;
38 import jalview.util.MessageManager;
39 import jalview.util.Platform;
40 import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
41
42 import java.awt.BorderLayout;
43 import java.awt.Color;
44 import java.awt.Component;
45 import java.awt.Dimension;
46 import java.awt.Font;
47 import java.awt.Graphics;
48 import java.awt.GridLayout;
49 import java.awt.Point;
50 import java.awt.Rectangle;
51 import java.awt.event.ActionEvent;
52 import java.awt.event.ActionListener;
53 import java.awt.event.ItemEvent;
54 import java.awt.event.ItemListener;
55 import java.awt.event.MouseAdapter;
56 import java.awt.event.MouseEvent;
57 import java.awt.event.MouseMotionAdapter;
58 import java.beans.PropertyChangeEvent;
59 import java.beans.PropertyChangeListener;
60 import java.io.File;
61 import java.io.FileInputStream;
62 import java.io.FileOutputStream;
63 import java.io.InputStreamReader;
64 import java.io.OutputStreamWriter;
65 import java.io.PrintWriter;
66 import java.util.Arrays;
67 import java.util.Comparator;
68 import java.util.HashMap;
69 import java.util.HashSet;
70 import java.util.Hashtable;
71 import java.util.Iterator;
72 import java.util.List;
73 import java.util.Map;
74 import java.util.Set;
75
76 import javax.help.HelpSetException;
77 import javax.swing.AbstractCellEditor;
78 import javax.swing.BorderFactory;
79 import javax.swing.Icon;
80 import javax.swing.JButton;
81 import javax.swing.JCheckBox;
82 import javax.swing.JCheckBoxMenuItem;
83 import javax.swing.JColorChooser;
84 import javax.swing.JDialog;
85 import javax.swing.JInternalFrame;
86 import javax.swing.JLabel;
87 import javax.swing.JLayeredPane;
88 import javax.swing.JMenuItem;
89 import javax.swing.JPanel;
90 import javax.swing.JPopupMenu;
91 import javax.swing.JScrollPane;
92 import javax.swing.JSlider;
93 import javax.swing.JTable;
94 import javax.swing.ListSelectionModel;
95 import javax.swing.SwingConstants;
96 import javax.swing.border.Border;
97 import javax.swing.event.ChangeEvent;
98 import javax.swing.event.ChangeListener;
99 import javax.swing.table.AbstractTableModel;
100 import javax.swing.table.TableCellEditor;
101 import javax.swing.table.TableCellRenderer;
102 import javax.swing.table.TableColumn;
103
104 public class FeatureSettings extends JPanel
105         implements FeatureSettingsControllerI
106 {
107   private static final String SEQUENCE_FEATURE_COLOURS = MessageManager
108           .getString("label.sequence_feature_colours");
109
110   /*
111    * column indices of fields in Feature Settings table
112    */
113   static final int TYPE_COLUMN = 0;
114
115   static final int COLOUR_COLUMN = 1;
116
117   static final int FILTER_COLUMN = 2;
118
119   static final int SHOW_COLUMN = 3;
120
121   private static final int COLUMN_COUNT = 4;
122
123   private static final int MIN_WIDTH = 400;
124
125   private static final int MIN_HEIGHT = 400;
126
127   private final static String BASE_TOOLTIP = "Click to edit, right-click for menu";
128
129   final FeatureRenderer fr;
130
131   public final AlignFrame af;
132
133   /*
134    * 'original' fields hold settings to restore on Cancel
135    */
136   Object[][] originalData;
137
138   private float originalTransparency;
139
140   private Map<String, FeatureMatcherSetI> originalFilters;
141
142   final JInternalFrame frame;
143
144   JScrollPane scrollPane = new JScrollPane();
145
146   JTable table;
147
148   JPanel groupPanel;
149
150   JSlider transparency = new JSlider();
151
152   /*
153    * when true, constructor is still executing - so ignore UI events
154    */
155   protected volatile boolean inConstruction = true;
156
157   int selectedRow = -1;
158
159   JButton fetchDAS = new JButton();
160
161   JButton saveDAS = new JButton();
162
163   JButton cancelDAS = new JButton();
164
165   boolean resettingTable = false;
166
167   /*
168    * true when Feature Settings are updating from feature renderer
169    */
170   private boolean handlingUpdate = false;
171
172   /*
173    * holds {featureCount, totalExtent} for each feature type
174    */
175   Map<String, float[]> typeWidth = null;
176
177   /**
178    * Constructor
179    * 
180    * @param af
181    */
182   public FeatureSettings(AlignFrame alignFrame)
183   {
184     this.af = alignFrame;
185     fr = af.getFeatureRenderer();
186
187     // save transparency for restore on Cancel
188     originalTransparency = fr.getTransparency();
189     int originalTransparencyAsPercent = (int) (originalTransparency * 100);
190     transparency.setMaximum(100 - originalTransparencyAsPercent);
191
192     originalFilters = new HashMap<>(fr.getFeatureFilters()); // shallow copy
193
194     try
195     {
196       jbInit();
197     } catch (Exception ex)
198     {
199       ex.printStackTrace();
200     }
201
202     table = new JTable()
203     {
204       static final String tt = "Click to edit, right-click for menu"; // todo i18n
205       
206       @Override
207       public String getToolTipText(MouseEvent e)
208       {
209         String tip = null;
210         int column = table.columnAtPoint(e.getPoint());
211         int row = table.rowAtPoint(e.getPoint());
212
213         switch (column)
214         {
215         case TYPE_COLUMN:
216           tip = JvSwingUtils.wrapTooltip(true, MessageManager
217                   .getString("label.feature_settings_click_drag"));
218           break;
219         case COLOUR_COLUMN:
220           FeatureColourI colour = (FeatureColourI) table.getValueAt(row,
221                   column);
222           tip = getColorTooltip(colour);
223           break;
224         case FILTER_COLUMN:
225           FeatureMatcherSet o = (FeatureMatcherSet) table.getValueAt(row,
226                   column);
227           tip = o.isEmpty()
228                   ? MessageManager.getString("label.filters_tooltip")
229                   : o.toString();
230           break;
231         default:
232           break;
233         }
234         return tip;
235       }
236     };
237     table.getTableHeader().setFont(new Font("Verdana", Font.PLAIN, 12));
238     table.setFont(new Font("Verdana", Font.PLAIN, 12));
239
240     // table.setDefaultRenderer(Color.class, new ColorRenderer());
241     // table.setDefaultEditor(Color.class, new ColorEditor(this));
242     //
243     table.setDefaultEditor(FeatureColour.class, new ColorEditor(this));
244     table.setDefaultRenderer(FeatureColour.class, new ColorRenderer());
245
246     table.setDefaultEditor(FeatureMatcherSet.class, new FilterEditor(this));
247     table.setDefaultRenderer(FeatureMatcherSet.class, new FilterRenderer());
248
249     TableColumn colourColumn = new TableColumn(COLOUR_COLUMN, 75,
250             new ColorRenderer(), new ColorEditor(this));
251     table.addColumn(colourColumn);
252
253     TableColumn filterColumn = new TableColumn(FILTER_COLUMN, 75,
254             new FilterRenderer(), new FilterEditor(this));
255     table.addColumn(filterColumn);
256
257     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
258
259     table.addMouseListener(new MouseAdapter()
260     {
261       @Override
262       public void mousePressed(MouseEvent evt)
263       {
264         selectedRow = table.rowAtPoint(evt.getPoint());
265         String type = (String) table.getValueAt(selectedRow, TYPE_COLUMN);
266         if (evt.isPopupTrigger())
267         {
268           Object colour = table.getValueAt(selectedRow, COLOUR_COLUMN);
269           popupSort(selectedRow, type, colour, fr.getMinMax(), evt.getX(),
270                   evt.getY());
271         }
272         else if (evt.getClickCount() == 2)
273         {
274           boolean invertSelection = evt.isAltDown();
275           boolean toggleSelection = Platform.isControlDown(evt);
276           boolean extendSelection = evt.isShiftDown();
277           fr.ap.alignFrame.avc.markColumnsContainingFeatures(
278                   invertSelection, extendSelection, toggleSelection, type);
279         }
280       }
281
282       // isPopupTrigger fires on mouseReleased on Windows
283       @Override
284       public void mouseReleased(MouseEvent evt)
285       {
286         selectedRow = table.rowAtPoint(evt.getPoint());
287         if (evt.isPopupTrigger())
288         {
289           String type = (String) table.getValueAt(selectedRow, TYPE_COLUMN);
290           Object colour = table.getValueAt(selectedRow, COLOUR_COLUMN);
291           popupSort(selectedRow, type, colour, fr.getMinMax(), evt.getX(),
292                   evt.getY());
293         }
294       }
295     });
296
297     table.addMouseMotionListener(new MouseMotionAdapter()
298     {
299       @Override
300       public void mouseDragged(MouseEvent evt)
301       {
302         int newRow = table.rowAtPoint(evt.getPoint());
303         if (newRow != selectedRow && selectedRow != -1 && newRow != -1)
304         {
305           /*
306            * reposition 'selectedRow' to 'newRow' (the dragged to location)
307            * this could be more than one row away for a very fast drag action
308            * so just swap it with adjacent rows until we get it there
309            */
310           Object[][] data = ((FeatureTableModel) table.getModel())
311                   .getData();
312           int direction = newRow < selectedRow ? -1 : 1;
313           for (int i = selectedRow; i != newRow; i += direction)
314           {
315             Object[] temp = data[i];
316             data[i] = data[i + direction];
317             data[i + direction] = temp;
318           }
319           updateFeatureRenderer(data);
320           table.repaint();
321           selectedRow = newRow;
322         }
323       }
324     });
325     // table.setToolTipText(JvSwingUtils.wrapTooltip(true,
326     // MessageManager.getString("label.feature_settings_click_drag")));
327     scrollPane.setViewportView(table);
328
329     if (af.getViewport().isShowSequenceFeatures() || !fr.hasRenderOrder())
330     {
331       fr.findAllFeatures(true); // display everything!
332     }
333
334     discoverAllFeatureData();
335     final PropertyChangeListener change;
336     final FeatureSettings fs = this;
337     fr.addPropertyChangeListener(change = new PropertyChangeListener()
338     {
339       @Override
340       public void propertyChange(PropertyChangeEvent evt)
341       {
342         if (!fs.resettingTable && !fs.handlingUpdate)
343         {
344           fs.handlingUpdate = true;
345           fs.resetTable(null);
346           // new groups may be added with new sequence feature types only
347           fs.handlingUpdate = false;
348         }
349       }
350
351     });
352
353     frame = new JInternalFrame();
354     frame.setContentPane(this);
355     if (Platform.isAMac())
356     {
357       Desktop.addInternalFrame(frame,
358               MessageManager.getString("label.sequence_feature_settings"),
359               600, 480);
360     }
361     else
362     {
363       Desktop.addInternalFrame(frame,
364               MessageManager.getString("label.sequence_feature_settings"),
365               600, 450);
366     }
367     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
368
369     frame.addInternalFrameListener(
370             new javax.swing.event.InternalFrameAdapter()
371             {
372               @Override
373               public void internalFrameClosed(
374                       javax.swing.event.InternalFrameEvent evt)
375               {
376                 fr.removePropertyChangeListener(change);
377               };
378             });
379     frame.setLayer(JLayeredPane.PALETTE_LAYER);
380     inConstruction = false;
381   }
382
383   protected void popupSort(final int rowSelected, final String type,
384           final Object typeCol, final Map<String, float[][]> minmax, int x,
385           int y)
386   {
387     final FeatureColourI featureColour = (FeatureColourI) typeCol;
388
389     JPopupMenu men = new JPopupMenu(MessageManager
390             .formatMessage("label.settings_for_param", new String[]
391             { type }));
392     JMenuItem scr = new JMenuItem(
393             MessageManager.getString("label.sort_by_score"));
394     men.add(scr);
395     final FeatureSettings me = this;
396     scr.addActionListener(new ActionListener()
397     {
398
399       @Override
400       public void actionPerformed(ActionEvent e)
401       {
402         me.af.avc
403                 .sortAlignmentByFeatureScore(Arrays.asList(new String[]
404                 { type }));
405       }
406
407     });
408     JMenuItem dens = new JMenuItem(
409             MessageManager.getString("label.sort_by_density"));
410     dens.addActionListener(new ActionListener()
411     {
412
413       @Override
414       public void actionPerformed(ActionEvent e)
415       {
416         me.af.avc
417                 .sortAlignmentByFeatureDensity(Arrays.asList(new String[]
418                 { type }));
419       }
420
421     });
422     men.add(dens);
423
424     /*
425      * variable colour options include colour by label, by score,
426      * by selected attribute text, or attribute value
427      */
428     final JCheckBoxMenuItem mxcol = new JCheckBoxMenuItem(
429             MessageManager.getString("label.variable_colour"));
430     mxcol.setSelected(!featureColour.isSimpleColour());
431     men.add(mxcol);
432     mxcol.addActionListener(new ActionListener()
433     {
434       JColorChooser colorChooser;
435
436       @Override
437       public void actionPerformed(ActionEvent e)
438       {
439         if (e.getSource() == mxcol)
440         {
441           if (featureColour.isSimpleColour())
442           {
443             FeatureTypeSettings fc = new FeatureTypeSettings(me.fr, type);
444             fc.addActionListener(this);
445           }
446           else
447           {
448             // bring up simple color chooser
449             colorChooser = new JColorChooser();
450             String title = MessageManager
451                     .getString("label.select_colour");
452             JDialog dialog = JColorChooser.createDialog(me,
453                     title, true, // modal
454                     colorChooser, this, // OK button handler
455                     null); // no CANCEL button handler
456             colorChooser.setColor(featureColour.getMaxColour());
457             dialog.setVisible(true);
458           }
459         }
460         else
461         {
462           if (e.getSource() instanceof FeatureTypeSettings)
463           {
464             /*
465              * update after OK in feature colour dialog; the updated
466              * colour will have already been set in the FeatureRenderer
467              */
468             FeatureColourI fci = fr.getFeatureColours().get(type);
469             table.setValueAt(fci, rowSelected, 1);
470             table.validate();
471           }
472           else
473           {
474             // probably the color chooser!
475             table.setValueAt(new FeatureColour(colorChooser.getColor()),
476                     rowSelected, 1);
477             table.validate();
478             me.updateFeatureRenderer(
479                     ((FeatureTableModel) table.getModel()).getData(),
480                     false);
481           }
482         }
483       }
484
485     });
486
487     JMenuItem selCols = new JMenuItem(
488             MessageManager.getString("label.select_columns_containing"));
489     selCols.addActionListener(new ActionListener()
490     {
491       @Override
492       public void actionPerformed(ActionEvent arg0)
493       {
494         fr.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
495                 false, type);
496       }
497     });
498     JMenuItem clearCols = new JMenuItem(MessageManager
499             .getString("label.select_columns_not_containing"));
500     clearCols.addActionListener(new ActionListener()
501     {
502       @Override
503       public void actionPerformed(ActionEvent arg0)
504       {
505         fr.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
506                 false, type);
507       }
508     });
509     JMenuItem hideCols = new JMenuItem(
510             MessageManager.getString("label.hide_columns_containing"));
511     hideCols.addActionListener(new ActionListener()
512     {
513       @Override
514       public void actionPerformed(ActionEvent arg0)
515       {
516         fr.ap.alignFrame.hideFeatureColumns(type, true);
517       }
518     });
519     JMenuItem hideOtherCols = new JMenuItem(
520             MessageManager.getString("label.hide_columns_not_containing"));
521     hideOtherCols.addActionListener(new ActionListener()
522     {
523       @Override
524       public void actionPerformed(ActionEvent arg0)
525       {
526         fr.ap.alignFrame.hideFeatureColumns(type, false);
527       }
528     });
529     men.add(selCols);
530     men.add(clearCols);
531     men.add(hideCols);
532     men.add(hideOtherCols);
533     men.show(table, x, y);
534   }
535
536   @Override
537   synchronized public void discoverAllFeatureData()
538   {
539     Set<String> allGroups = new HashSet<>();
540     AlignmentI alignment = af.getViewport().getAlignment();
541
542     for (int i = 0; i < alignment.getHeight(); i++)
543     {
544       SequenceI seq = alignment.getSequenceAt(i);
545       for (String group : seq.getFeatures().getFeatureGroups(true))
546       {
547         if (group != null && !allGroups.contains(group))
548         {
549           allGroups.add(group);
550           checkGroupState(group);
551         }
552       }
553     }
554
555     resetTable(null);
556
557     validate();
558   }
559
560   /**
561    * Synchronise gui group list and check visibility of group
562    * 
563    * @param group
564    * @return true if group is visible
565    */
566   private boolean checkGroupState(String group)
567   {
568     boolean visible = fr.checkGroupVisibility(group, true);
569
570     for (int g = 0; g < groupPanel.getComponentCount(); g++)
571     {
572       if (((JCheckBox) groupPanel.getComponent(g)).getText().equals(group))
573       {
574         ((JCheckBox) groupPanel.getComponent(g)).setSelected(visible);
575         return visible;
576       }
577     }
578
579     final String grp = group;
580     final JCheckBox check = new JCheckBox(group, visible);
581     check.setFont(new Font("Serif", Font.BOLD, 12));
582     check.setToolTipText(group);
583     check.addItemListener(new ItemListener()
584     {
585       @Override
586       public void itemStateChanged(ItemEvent evt)
587       {
588         fr.setGroupVisibility(check.getText(), check.isSelected());
589         resetTable(new String[] { grp });
590         af.alignPanel.paintAlignment(true, true);
591       }
592     });
593     groupPanel.add(check);
594     return visible;
595   }
596
597   synchronized void resetTable(String[] groupChanged)
598   {
599     if (resettingTable)
600     {
601       return;
602     }
603     resettingTable = true;
604     typeWidth = new Hashtable<>();
605     // TODO: change avWidth calculation to 'per-sequence' average and use long
606     // rather than float
607
608     Set<String> displayableTypes = new HashSet<>();
609     Set<String> foundGroups = new HashSet<>();
610
611     /*
612      * determine which feature types may be visible depending on 
613      * which groups are selected, and recompute average width data
614      */
615     for (int i = 0; i < af.getViewport().getAlignment().getHeight(); i++)
616     {
617
618       SequenceI seq = af.getViewport().getAlignment().getSequenceAt(i);
619
620       /*
621        * get the sequence's groups for positional features
622        * and keep track of which groups are visible
623        */
624       Set<String> groups = seq.getFeatures().getFeatureGroups(true);
625       Set<String> visibleGroups = new HashSet<>();
626       for (String group : groups)
627       {
628         if (group == null || checkGroupState(group))
629         {
630           visibleGroups.add(group);
631         }
632       }
633       foundGroups.addAll(groups);
634
635       /*
636        * get distinct feature types for visible groups
637        * record distinct visible types, and their count and total length
638        */
639       Set<String> types = seq.getFeatures().getFeatureTypesForGroups(true,
640               visibleGroups.toArray(new String[visibleGroups.size()]));
641       for (String type : types)
642       {
643         displayableTypes.add(type);
644         float[] avWidth = typeWidth.get(type);
645         if (avWidth == null)
646         {
647           avWidth = new float[2];
648           typeWidth.put(type, avWidth);
649         }
650         // todo this could include features with a non-visible group
651         // - do we greatly care?
652         // todo should we include non-displayable features here, and only
653         // update when features are added?
654         avWidth[0] += seq.getFeatures().getFeatureCount(true, type);
655         avWidth[1] += seq.getFeatures().getTotalFeatureLength(type);
656       }
657     }
658
659     Object[][] data = new Object[displayableTypes.size()][COLUMN_COUNT];
660     int dataIndex = 0;
661
662     if (fr.hasRenderOrder())
663     {
664       if (!handlingUpdate)
665       {
666         fr.findAllFeatures(groupChanged != null); // prod to update
667         // colourschemes. but don't
668         // affect display
669         // First add the checks in the previous render order,
670         // in case the window has been closed and reopened
671       }
672       List<String> frl = fr.getRenderOrder();
673       for (int ro = frl.size() - 1; ro > -1; ro--)
674       {
675         String type = frl.get(ro);
676
677         if (!displayableTypes.contains(type))
678         {
679           continue;
680         }
681
682         data[dataIndex][TYPE_COLUMN] = type;
683         data[dataIndex][COLOUR_COLUMN] = fr.getFeatureStyle(type);
684         FeatureMatcherSetI featureFilter = fr.getFeatureFilter(type);
685         data[dataIndex][FILTER_COLUMN] = featureFilter == null
686                 ? new FeatureMatcherSet()
687                 : featureFilter;
688         data[dataIndex][SHOW_COLUMN] = new Boolean(
689                 af.getViewport().getFeaturesDisplayed().isVisible(type));
690         dataIndex++;
691         displayableTypes.remove(type);
692       }
693     }
694
695     /*
696      * process any extra features belonging only to 
697      * a group which was just selected
698      */
699     while (!displayableTypes.isEmpty())
700     {
701       String type = displayableTypes.iterator().next();
702       data[dataIndex][TYPE_COLUMN] = type;
703
704       data[dataIndex][COLOUR_COLUMN] = fr.getFeatureStyle(type);
705       if (data[dataIndex][COLOUR_COLUMN] == null)
706       {
707         // "Colour has been updated in another view!!"
708         fr.clearRenderOrder();
709         return;
710       }
711       FeatureMatcherSetI featureFilter = fr.getFeatureFilter(type);
712       data[dataIndex][FILTER_COLUMN] = featureFilter == null
713               ? new FeatureMatcherSet()
714               : featureFilter;
715       data[dataIndex][SHOW_COLUMN] = new Boolean(true);
716       dataIndex++;
717       displayableTypes.remove(type);
718     }
719
720     if (originalData == null)
721     {
722       originalData = new Object[data.length][COLUMN_COUNT];
723       for (int i = 0; i < data.length; i++)
724       {
725         System.arraycopy(data[i], 0, originalData[i], 0, COLUMN_COUNT);
726       }
727     }
728     else
729     {
730       updateOriginalData(data);
731     }
732
733     table.setModel(new FeatureTableModel(data));
734     table.getColumnModel().getColumn(0).setPreferredWidth(200);
735
736     groupPanel.setLayout(
737             new GridLayout(fr.getFeatureGroupsSize() / 4 + 1, 4));
738     pruneGroups(foundGroups);
739     groupPanel.validate();
740
741     updateFeatureRenderer(data, groupChanged != null);
742     resettingTable = false;
743   }
744
745   /**
746    * Updates 'originalData' (used for restore on Cancel) if we detect that changes
747    * have been made outwith this dialog
748    * <ul>
749    * <li>a new feature type added (and made visible)</li>
750    * <li>a feature colour changed (in the Amend Features dialog)</li>
751    * </ul>
752    * 
753    * @param foundData
754    */
755   protected void updateOriginalData(Object[][] foundData)
756   {
757     // todo LinkedHashMap instead of Object[][] would be nice
758
759     Object[][] currentData = ((FeatureTableModel) table.getModel())
760             .getData();
761     for (Object[] row : foundData)
762     {
763       String type = (String) row[TYPE_COLUMN];
764       boolean found = false;
765       for (Object[] current : currentData)
766       {
767         if (type.equals(current[TYPE_COLUMN]))
768         {
769           found = true;
770           /*
771            * currently dependent on object equality here;
772            * really need an equals method on FeatureColour
773            */
774           if (!row[COLOUR_COLUMN].equals(current[COLOUR_COLUMN]))
775           {
776             /*
777              * feature colour has changed externally - update originalData
778              */
779             for (Object[] original : originalData)
780             {
781               if (type.equals(original[TYPE_COLUMN]))
782               {
783                 original[COLOUR_COLUMN] = row[COLOUR_COLUMN];
784                 break;
785               }
786             }
787           }
788           break;
789         }
790       }
791       if (!found)
792       {
793         /*
794          * new feature detected - add to original data (on top)
795          */
796         Object[][] newData = new Object[originalData.length
797                 + 1][COLUMN_COUNT];
798         for (int i = 0; i < originalData.length; i++)
799         {
800           System.arraycopy(originalData[i], 0, newData[i + 1], 0,
801                   COLUMN_COUNT);
802         }
803         newData[0] = row;
804         originalData = newData;
805       }
806     }
807   }
808
809   /**
810    * Remove from the groups panel any checkboxes for groups that are not in the
811    * foundGroups set. This enables removing a group from the display when the last
812    * feature in that group is deleted.
813    * 
814    * @param foundGroups
815    */
816   protected void pruneGroups(Set<String> foundGroups)
817   {
818     for (int g = 0; g < groupPanel.getComponentCount(); g++)
819     {
820       JCheckBox checkbox = (JCheckBox) groupPanel.getComponent(g);
821       if (!foundGroups.contains(checkbox.getText()))
822       {
823         groupPanel.remove(checkbox);
824       }
825     }
826   }
827
828   /**
829    * reorder data based on the featureRenderers global priority list.
830    * 
831    * @param data
832    */
833   private void ensureOrder(Object[][] data)
834   {
835     boolean sort = false;
836     float[] order = new float[data.length];
837     for (int i = 0; i < order.length; i++)
838     {
839       order[i] = fr.getOrder(data[i][0].toString());
840       if (order[i] < 0)
841       {
842         order[i] = fr.setOrder(data[i][0].toString(), i / order.length);
843       }
844       if (i > 1)
845       {
846         sort = sort || order[i - 1] > order[i];
847       }
848     }
849     if (sort)
850     {
851       jalview.util.QuickSort.sort(order, data);
852     }
853   }
854
855   /**
856    * Offers a file chooser dialog, and then loads the feature colours and
857    * filters from file in XML format and unmarshals to Jalview feature settings
858    */
859   void load()
860   {
861     JalviewFileChooser chooser = new JalviewFileChooser("fc",
862             SEQUENCE_FEATURE_COLOURS);
863     chooser.setFileView(new JalviewFileView());
864     chooser.setDialogTitle(
865             MessageManager.getString("label.load_feature_colours"));
866     chooser.setToolTipText(MessageManager.getString("action.load"));
867
868     int value = chooser.showOpenDialog(this);
869
870     if (value == JalviewFileChooser.APPROVE_OPTION)
871     {
872       File file = chooser.getSelectedFile();
873       load(file);
874     }
875   }
876
877   /**
878    * Loads feature colours and filters from XML stored in the given file
879    * 
880    * @param file
881    */
882   void load(File file)
883   {
884     try
885     {
886       InputStreamReader in = new InputStreamReader(
887               new FileInputStream(file), "UTF-8");
888
889       JalviewUserColours jucs = JalviewUserColours.unmarshal(in);
890
891       /*
892        * load feature colours
893        */
894       for (int i = jucs.getColourCount() - 1; i >= 0; i--)
895       {
896         jalview.schemabinding.version2.Colour newcol = jucs.getColour(i);
897         FeatureColourI colour = Jalview2XML.unmarshalColour(newcol);
898         fr.setColour(newcol.getName(), colour);
899         fr.setOrder(newcol.getName(), i / (float) jucs.getColourCount());
900       }
901
902       /*
903        * load feature filters; loaded filters will replace any that are
904        * currently defined, other defined filters are left unchanged 
905        */
906       for (int i = 0; i < jucs.getFilterCount(); i++)
907       {
908         jalview.schemabinding.version2.Filter filterModel = jucs
909                 .getFilter(i);
910         String featureType = filterModel.getFeatureType();
911         FeatureMatcherSetI filter = Jalview2XML.unmarshalFilter(featureType,
912                 filterModel.getMatcherSet());
913         if (!filter.isEmpty())
914         {
915           fr.setFeatureFilter(featureType, filter);
916         }
917       }
918
919       /*
920        * update feature settings table
921        */
922       if (table != null)
923       {
924         resetTable(null);
925         Object[][] data = ((FeatureTableModel) table.getModel())
926                 .getData();
927         ensureOrder(data);
928         updateFeatureRenderer(data, false);
929         table.repaint();
930       }
931     } catch (Exception ex)
932     {
933       System.out.println("Error loading User Colour File\n" + ex);
934     }
935   }
936
937   /**
938    * Offers a file chooser dialog, and then saves the current feature colours
939    * and any filters to the selected file in XML format
940    */
941   void save()
942   {
943     JalviewFileChooser chooser = new JalviewFileChooser("fc",
944             SEQUENCE_FEATURE_COLOURS);
945     chooser.setFileView(new JalviewFileView());
946     chooser.setDialogTitle(
947             MessageManager.getString("label.save_feature_colours"));
948     chooser.setToolTipText(MessageManager.getString("action.save"));
949
950     int value = chooser.showSaveDialog(this);
951
952     if (value == JalviewFileChooser.APPROVE_OPTION)
953     {
954       save(chooser.getSelectedFile());
955     }
956   }
957
958   /**
959    * Saves feature colours and filters to the given file
960    * 
961    * @param file
962    */
963   void save(File file)
964   {
965     JalviewUserColours ucs = new JalviewUserColours();
966     ucs.setSchemeName("Sequence Features");
967     try
968     {
969       PrintWriter out = new PrintWriter(new OutputStreamWriter(
970               new FileOutputStream(file), "UTF-8"));
971
972       /*
973        * sort feature types by colour order, from 0 (highest)
974        * to 1 (lowest)
975        */
976       Set<String> fr_colours = fr.getAllFeatureColours();
977       String[] sortedTypes = fr_colours
978               .toArray(new String[fr_colours.size()]);
979       Arrays.sort(sortedTypes, new Comparator<String>()
980       {
981         @Override
982         public int compare(String type1, String type2)
983         {
984           return Float.compare(fr.getOrder(type1), fr.getOrder(type2));
985         }
986       });
987
988       /*
989        * save feature colours
990        */
991       for (String featureType : sortedTypes)
992       {
993         FeatureColourI fcol = fr.getFeatureStyle(featureType);
994         jalview.schemabinding.version2.Colour col = Jalview2XML.marshalColour(
995                 featureType, fcol);
996         ucs.addColour(col);
997       }
998
999       /*
1000        * save any feature filters
1001        */
1002       for (String featureType : sortedTypes)
1003       {
1004         FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1005         if (filter != null && !filter.isEmpty())
1006         {
1007           Iterator<FeatureMatcherI> iterator = filter.getMatchers().iterator();
1008           FeatureMatcherI firstMatcher = iterator.next();
1009           MatcherSet ms = Jalview2XML.marshalFilter(firstMatcher, iterator,
1010                   filter.isAnded());
1011           Filter filterModel = new Filter();
1012           filterModel.setFeatureType(featureType);
1013           filterModel.setMatcherSet(ms);
1014           ucs.addFilter(filterModel);
1015         }
1016       }
1017
1018       ucs.marshal(out);
1019       out.close();
1020     } catch (Exception ex)
1021     {
1022       ex.printStackTrace();
1023     }
1024   }
1025
1026   public void invertSelection()
1027   {
1028     Object[][] data = ((FeatureTableModel) table.getModel()).getData();
1029     for (int i = 0; i < data.length; i++)
1030     {
1031       data[i][SHOW_COLUMN] = !(Boolean) data[i][SHOW_COLUMN];
1032     }
1033     updateFeatureRenderer(data, true);
1034     table.repaint();
1035   }
1036
1037   public void orderByAvWidth()
1038   {
1039     if (table == null || table.getModel() == null)
1040     {
1041       return;
1042     }
1043     Object[][] data = ((FeatureTableModel) table.getModel()).getData();
1044     float[] width = new float[data.length];
1045     float[] awidth;
1046     float max = 0;
1047
1048     for (int i = 0; i < data.length; i++)
1049     {
1050       awidth = typeWidth.get(data[i][TYPE_COLUMN]);
1051       if (awidth[0] > 0)
1052       {
1053         width[i] = awidth[1] / awidth[0];// *awidth[0]*awidth[2]; - better
1054         // weight - but have to make per
1055         // sequence, too (awidth[2])
1056         // if (width[i]==1) // hack to distinguish single width sequences.
1057       }
1058       else
1059       {
1060         width[i] = 0;
1061       }
1062       if (max < width[i])
1063       {
1064         max = width[i];
1065       }
1066     }
1067     boolean sort = false;
1068     for (int i = 0; i < width.length; i++)
1069     {
1070       // awidth = (float[]) typeWidth.get(data[i][0]);
1071       if (width[i] == 0)
1072       {
1073         width[i] = fr.getOrder(data[i][TYPE_COLUMN].toString());
1074         if (width[i] < 0)
1075         {
1076           width[i] = fr.setOrder(data[i][TYPE_COLUMN].toString(),
1077                   i / data.length);
1078         }
1079       }
1080       else
1081       {
1082         width[i] /= max; // normalize
1083         fr.setOrder(data[i][TYPE_COLUMN].toString(), width[i]); // store for later
1084       }
1085       if (i > 0)
1086       {
1087         sort = sort || width[i - 1] > width[i];
1088       }
1089     }
1090     if (sort)
1091     {
1092       jalview.util.QuickSort.sort(width, data);
1093       // update global priority order
1094     }
1095
1096     updateFeatureRenderer(data, false);
1097     table.repaint();
1098   }
1099
1100   public void close()
1101   {
1102     try
1103     {
1104       frame.setClosed(true);
1105     } catch (Exception exe)
1106     {
1107     }
1108
1109   }
1110
1111   public void updateFeatureRenderer(Object[][] data)
1112   {
1113     updateFeatureRenderer(data, true);
1114   }
1115
1116   /**
1117    * Update the priority order of features; only repaint if this changed the order
1118    * of visible features
1119    * 
1120    * @param data
1121    * @param visibleNew
1122    */
1123   private void updateFeatureRenderer(Object[][] data, boolean visibleNew)
1124   {
1125     FeatureSettingsBean[] rowData = getTableAsBeans(data);
1126
1127     if (fr.setFeaturePriority(rowData, visibleNew))
1128     {
1129       af.alignPanel.paintAlignment(true, true);
1130     }
1131   }
1132
1133   /**
1134    * Converts table data into an array of data beans
1135    */
1136   private FeatureSettingsBean[] getTableAsBeans(Object[][] data)
1137   {
1138     FeatureSettingsBean[] rowData = new FeatureSettingsBean[data.length];
1139     for (int i = 0; i < data.length; i++)
1140     {
1141       String type = (String) data[i][TYPE_COLUMN];
1142       FeatureColourI colour = (FeatureColourI) data[i][COLOUR_COLUMN];
1143       FeatureMatcherSetI theFilter = (FeatureMatcherSetI) data[i][FILTER_COLUMN];
1144       Boolean isShown = (Boolean) data[i][SHOW_COLUMN];
1145       rowData[i] = new FeatureSettingsBean(type, colour, theFilter,
1146               isShown);
1147     }
1148     return rowData;
1149   }
1150
1151   private void jbInit() throws Exception
1152   {
1153     this.setLayout(new BorderLayout());
1154
1155     JPanel settingsPane = new JPanel();
1156     settingsPane.setLayout(new BorderLayout());
1157
1158     JPanel bigPanel = new JPanel();
1159     bigPanel.setLayout(new BorderLayout());
1160
1161     groupPanel = new JPanel();
1162     bigPanel.add(groupPanel, BorderLayout.NORTH);
1163
1164     JButton invert = new JButton(
1165             MessageManager.getString("label.invert_selection"));
1166     invert.setFont(JvSwingUtils.getLabelFont());
1167     invert.addActionListener(new ActionListener()
1168     {
1169       @Override
1170       public void actionPerformed(ActionEvent e)
1171       {
1172         invertSelection();
1173       }
1174     });
1175
1176     JButton optimizeOrder = new JButton(
1177             MessageManager.getString("label.optimise_order"));
1178     optimizeOrder.setFont(JvSwingUtils.getLabelFont());
1179     optimizeOrder.addActionListener(new ActionListener()
1180     {
1181       @Override
1182       public void actionPerformed(ActionEvent e)
1183       {
1184         orderByAvWidth();
1185       }
1186     });
1187
1188     JButton sortByScore = new JButton(
1189             MessageManager.getString("label.seq_sort_by_score"));
1190     sortByScore.setFont(JvSwingUtils.getLabelFont());
1191     sortByScore.addActionListener(new ActionListener()
1192     {
1193       @Override
1194       public void actionPerformed(ActionEvent e)
1195       {
1196         af.avc.sortAlignmentByFeatureScore(null);
1197       }
1198     });
1199     JButton sortByDens = new JButton(
1200             MessageManager.getString("label.sequence_sort_by_density"));
1201     sortByDens.setFont(JvSwingUtils.getLabelFont());
1202     sortByDens.addActionListener(new ActionListener()
1203     {
1204       @Override
1205       public void actionPerformed(ActionEvent e)
1206       {
1207         af.avc.sortAlignmentByFeatureDensity(null);
1208       }
1209     });
1210
1211     JButton help = new JButton(MessageManager.getString("action.help"));
1212     help.setFont(JvSwingUtils.getLabelFont());
1213     help.addActionListener(new ActionListener()
1214     {
1215       @Override
1216       public void actionPerformed(ActionEvent e)
1217       {
1218         try
1219         {
1220           Help.showHelpWindow(HelpId.SequenceFeatureSettings);
1221         } catch (HelpSetException e1)
1222         {
1223           e1.printStackTrace();
1224         }
1225       }
1226     });
1227     help.setFont(JvSwingUtils.getLabelFont());
1228     help.setText(MessageManager.getString("action.help"));
1229     help.addActionListener(new ActionListener()
1230     {
1231       @Override
1232       public void actionPerformed(ActionEvent e)
1233       {
1234         try
1235         {
1236           Help.showHelpWindow(HelpId.SequenceFeatureSettings);
1237         } catch (HelpSetException e1)
1238         {
1239           e1.printStackTrace();
1240         }
1241       }
1242     });
1243
1244     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
1245     cancel.setFont(JvSwingUtils.getLabelFont());
1246     cancel.addActionListener(new ActionListener()
1247     {
1248       @Override
1249       public void actionPerformed(ActionEvent e)
1250       {
1251         fr.setTransparency(originalTransparency);
1252         fr.setFeatureFilters(originalFilters);
1253         updateFeatureRenderer(originalData);
1254         close();
1255       }
1256     });
1257
1258     JButton ok = new JButton(MessageManager.getString("action.ok"));
1259     ok.setFont(JvSwingUtils.getLabelFont());
1260     ok.addActionListener(new ActionListener()
1261     {
1262       @Override
1263       public void actionPerformed(ActionEvent e)
1264       {
1265         close();
1266       }
1267     });
1268
1269     JButton loadColours = new JButton(
1270             MessageManager.getString("label.load_colours"));
1271     loadColours.setFont(JvSwingUtils.getLabelFont());
1272     loadColours.setToolTipText(
1273             MessageManager.getString("label.load_colours_tooltip"));
1274     loadColours.addActionListener(new ActionListener()
1275     {
1276       @Override
1277       public void actionPerformed(ActionEvent e)
1278       {
1279         load();
1280       }
1281     });
1282
1283     JButton saveColours = new JButton(
1284             MessageManager.getString("label.save_colours"));
1285     saveColours.setFont(JvSwingUtils.getLabelFont());
1286     saveColours.setToolTipText(
1287             MessageManager.getString("label.save_colours_tooltip"));
1288     saveColours.addActionListener(new ActionListener()
1289     {
1290       @Override
1291       public void actionPerformed(ActionEvent e)
1292       {
1293         save();
1294       }
1295     });
1296     transparency.addChangeListener(new ChangeListener()
1297     {
1298       @Override
1299       public void stateChanged(ChangeEvent evt)
1300       {
1301         if (!inConstruction)
1302         {
1303           fr.setTransparency((100 - transparency.getValue()) / 100f);
1304           af.alignPanel.paintAlignment(true, true);
1305         }
1306       }
1307     });
1308
1309     transparency.setMaximum(70);
1310     transparency.setToolTipText(
1311             MessageManager.getString("label.transparency_tip"));
1312
1313     JPanel transPanel = new JPanel(new GridLayout(1, 2));
1314     bigPanel.add(transPanel, BorderLayout.SOUTH);
1315
1316     JPanel transbuttons = new JPanel(new GridLayout(5, 1));
1317     transbuttons.add(optimizeOrder);
1318     transbuttons.add(invert);
1319     transbuttons.add(sortByScore);
1320     transbuttons.add(sortByDens);
1321     transbuttons.add(help);
1322     transPanel.add(transparency);
1323     transPanel.add(transbuttons);
1324
1325     JPanel buttonPanel = new JPanel();
1326     buttonPanel.add(ok);
1327     buttonPanel.add(cancel);
1328     buttonPanel.add(loadColours);
1329     buttonPanel.add(saveColours);
1330     bigPanel.add(scrollPane, BorderLayout.CENTER);
1331     settingsPane.add(bigPanel, BorderLayout.CENTER);
1332     settingsPane.add(buttonPanel, BorderLayout.SOUTH);
1333     this.add(settingsPane);
1334   }
1335
1336   /**
1337    * Answers a suitable tooltip to show on the colour cell of the table
1338    * 
1339    * @param fcol
1340    * @return
1341    */
1342   public static String getColorTooltip(FeatureColourI fcol)
1343   {
1344     if (fcol == null)
1345     {
1346       return null;
1347     }
1348     if (fcol.isSimpleColour())
1349     {
1350       return BASE_TOOLTIP;
1351     }
1352     String description = fcol.getDescription();
1353     description = description.replaceAll("<", "&lt;");
1354     description = description.replaceAll(">", "&gt;");
1355     StringBuilder tt = new StringBuilder(description);
1356     tt.append("<br>").append(BASE_TOOLTIP).append("</br>");
1357     return JvSwingUtils.wrapTooltip(true, tt.toString());
1358   }
1359
1360   public static void renderGraduatedColor(JLabel comp, FeatureColourI gcol,
1361           int w, int h)
1362   {
1363     boolean thr = false;
1364     StringBuilder tx = new StringBuilder();
1365   
1366     if (gcol.isColourByAttribute())
1367     {
1368       tx.append(FeatureMatcher
1369               .toAttributeDisplayName(gcol.getAttributeName()));
1370     }
1371     else if (!gcol.isColourByLabel())
1372     {
1373       tx.append(MessageManager.getString("label.score"));
1374     }
1375     tx.append(" ");
1376     if (gcol.isAboveThreshold())
1377     {
1378       thr = true;
1379       tx.append(">");
1380     }
1381     if (gcol.isBelowThreshold())
1382     {
1383       thr = true;
1384       tx.append("<");
1385     }
1386     if (gcol.isColourByLabel())
1387     {
1388       if (thr)
1389       {
1390         tx.append(" ");
1391       }
1392       if (!gcol.isColourByAttribute())
1393       {
1394         tx.append("Label");
1395       }
1396       comp.setIcon(null);
1397     }
1398     else
1399     {
1400       Color newColor = gcol.getMaxColour();
1401       comp.setBackground(newColor);
1402       // System.err.println("Width is " + w / 2);
1403       Icon ficon = new FeatureIcon(gcol, comp.getBackground(), w, h, thr);
1404       comp.setIcon(ficon);
1405       // tt+="RGB value: Max (" + newColor.getRed() + ", "
1406       // + newColor.getGreen() + ", " + newColor.getBlue()
1407       // + ")\nMin (" + minCol.getRed() + ", " + minCol.getGreen()
1408       // + ", " + minCol.getBlue() + ")");
1409     }
1410     comp.setHorizontalAlignment(SwingConstants.CENTER);
1411     comp.setText(tx.toString());
1412   }
1413
1414   // ///////////////////////////////////////////////////////////////////////
1415   // http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
1416   // ///////////////////////////////////////////////////////////////////////
1417   class FeatureTableModel extends AbstractTableModel
1418   {
1419     private String[] columnNames = {
1420         MessageManager.getString("label.feature_type"),
1421         MessageManager.getString("action.colour"),
1422         MessageManager.getString("label.filter"),
1423         MessageManager.getString("label.show") };
1424
1425     private Object[][] data;
1426
1427     FeatureTableModel(Object[][] data)
1428     {
1429       this.data = data;
1430     }
1431
1432     public Object[][] getData()
1433     {
1434       return data;
1435     }
1436
1437     public void setData(Object[][] data)
1438     {
1439       this.data = data;
1440     }
1441
1442     @Override
1443     public int getColumnCount()
1444     {
1445       return columnNames.length;
1446     }
1447
1448     public Object[] getRow(int row)
1449     {
1450       return data[row];
1451     }
1452
1453     @Override
1454     public int getRowCount()
1455     {
1456       return data.length;
1457     }
1458
1459     @Override
1460     public String getColumnName(int col)
1461     {
1462       return columnNames[col];
1463     }
1464
1465     @Override
1466     public Object getValueAt(int row, int col)
1467     {
1468       return data[row][col];
1469     }
1470
1471     /**
1472      * Answers the class of the object in column c of the first row of the table
1473      */
1474     @Override
1475     public Class<?> getColumnClass(int c)
1476     {
1477       Object v = getValueAt(0, c);
1478       return v == null ? null : v.getClass();
1479     }
1480
1481     @Override
1482     public boolean isCellEditable(int row, int col)
1483     {
1484       return col == 0 ? false : true;
1485     }
1486
1487     @Override
1488     public void setValueAt(Object value, int row, int col)
1489     {
1490       data[row][col] = value;
1491       fireTableCellUpdated(row, col);
1492       updateFeatureRenderer(data);
1493     }
1494
1495   }
1496
1497   class ColorRenderer extends JLabel implements TableCellRenderer
1498   {
1499     Border unselectedBorder = null;
1500
1501     Border selectedBorder = null;
1502
1503     public ColorRenderer()
1504     {
1505       setOpaque(true); // MUST do this for background to show up.
1506       setHorizontalTextPosition(SwingConstants.CENTER);
1507       setVerticalTextPosition(SwingConstants.CENTER);
1508     }
1509
1510     @Override
1511     public Component getTableCellRendererComponent(JTable tbl, Object color,
1512             boolean isSelected, boolean hasFocus, int row, int column)
1513     {
1514       FeatureColourI cellColour = (FeatureColourI) color;
1515       setOpaque(true);
1516       setBackground(tbl.getBackground());
1517       if (!cellColour.isSimpleColour())
1518       {
1519         Rectangle cr = tbl.getCellRect(row, column, false);
1520         FeatureSettings.renderGraduatedColor(this, cellColour,
1521                 (int) cr.getWidth(), (int) cr.getHeight());
1522       }
1523       else
1524       {
1525         this.setText("");
1526         this.setIcon(null);
1527         setBackground(cellColour.getColour());
1528       }
1529       if (isSelected)
1530       {
1531         if (selectedBorder == null)
1532         {
1533           selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5,
1534                   tbl.getSelectionBackground());
1535         }
1536         setBorder(selectedBorder);
1537       }
1538       else
1539       {
1540         if (unselectedBorder == null)
1541         {
1542           unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5,
1543                   tbl.getBackground());
1544         }
1545         setBorder(unselectedBorder);
1546       }
1547
1548       return this;
1549     }
1550   }
1551
1552   class FilterRenderer extends JLabel implements TableCellRenderer
1553   {
1554     javax.swing.border.Border unselectedBorder = null;
1555
1556     javax.swing.border.Border selectedBorder = null;
1557
1558     public FilterRenderer()
1559     {
1560       setOpaque(true); // MUST do this for background to show up.
1561       setHorizontalTextPosition(SwingConstants.CENTER);
1562       setVerticalTextPosition(SwingConstants.CENTER);
1563     }
1564
1565     @Override
1566     public Component getTableCellRendererComponent(JTable tbl,
1567             Object filter, boolean isSelected, boolean hasFocus, int row,
1568             int column)
1569     {
1570       FeatureMatcherSetI theFilter = (FeatureMatcherSetI) filter;
1571       setOpaque(true);
1572       String asText = theFilter.toString();
1573       setBackground(tbl.getBackground());
1574       this.setText(asText);
1575       this.setIcon(null);
1576
1577       if (isSelected)
1578       {
1579         if (selectedBorder == null)
1580         {
1581           selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5,
1582                   tbl.getSelectionBackground());
1583         }
1584         setBorder(selectedBorder);
1585       }
1586       else
1587       {
1588         if (unselectedBorder == null)
1589         {
1590           unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5,
1591                   tbl.getBackground());
1592         }
1593         setBorder(unselectedBorder);
1594       }
1595
1596       return this;
1597     }
1598   }
1599
1600   /**
1601    * update comp using rendering settings from gcol
1602    * 
1603    * @param comp
1604    * @param gcol
1605    */
1606   public static void renderGraduatedColor(JLabel comp, FeatureColourI gcol)
1607   {
1608     int w = comp.getWidth(), h = comp.getHeight();
1609     if (w < 20)
1610     {
1611       w = (int) comp.getPreferredSize().getWidth();
1612       h = (int) comp.getPreferredSize().getHeight();
1613       if (w < 20)
1614       {
1615         w = 80;
1616         h = 12;
1617       }
1618     }
1619     renderGraduatedColor(comp, gcol, w, h);
1620   }
1621
1622   class ColorEditor extends AbstractCellEditor
1623           implements TableCellEditor, ActionListener
1624   {
1625     FeatureSettings me;
1626
1627     FeatureColourI currentColor;
1628
1629     FeatureTypeSettings chooser;
1630
1631     String type;
1632
1633     JButton button;
1634
1635     JColorChooser colorChooser;
1636
1637     JDialog dialog;
1638
1639     protected static final String EDIT = "edit";
1640
1641     int rowSelected = 0;
1642
1643     public ColorEditor(FeatureSettings me)
1644     {
1645       this.me = me;
1646       // Set up the editor (from the table's point of view),
1647       // which is a button.
1648       // This button brings up the color chooser dialog,
1649       // which is the editor from the user's point of view.
1650       button = new JButton();
1651       button.setActionCommand(EDIT);
1652       button.addActionListener(this);
1653       button.setBorderPainted(false);
1654       // Set up the dialog that the button brings up.
1655       colorChooser = new JColorChooser();
1656       dialog = JColorChooser.createDialog(button,
1657               MessageManager.getString("label.select_colour"), true, // modal
1658               colorChooser, this, // OK button handler
1659               null); // no CANCEL button handler
1660     }
1661
1662     /**
1663      * Handles events from the editor button and from the dialog's OK button.
1664      */
1665     @Override
1666     public void actionPerformed(ActionEvent e)
1667     {
1668       // todo test e.getSource() instead here
1669       if (EDIT.equals(e.getActionCommand()))
1670       {
1671         // The user has clicked the cell, so
1672         // bring up the dialog.
1673         if (currentColor.isSimpleColour())
1674         {
1675           // bring up simple color chooser
1676           button.setBackground(currentColor.getColour());
1677           colorChooser.setColor(currentColor.getColour());
1678           dialog.setVisible(true);
1679         }
1680         else
1681         {
1682           // bring up graduated chooser.
1683           chooser = new FeatureTypeSettings(me.fr, type);
1684           chooser.setRequestFocusEnabled(true);
1685           chooser.requestFocus();
1686           chooser.addActionListener(this);
1687           chooser.showTab(true);
1688         }
1689         // Make the renderer reappear.
1690         fireEditingStopped();
1691
1692       }
1693       else
1694       {
1695         if (currentColor.isSimpleColour())
1696         {
1697           /*
1698            * read off colour picked in colour chooser after OK pressed
1699            */
1700           currentColor = new FeatureColour(colorChooser.getColor());
1701           me.table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN);
1702         }
1703         else
1704         {
1705           /*
1706            * after OK in variable colour dialog, any changes to colour 
1707            * (or filters!) are already set in FeatureRenderer, so just
1708            * update table data without triggering updateFeatureRenderer
1709            */
1710           currentColor = fr.getFeatureColours().get(type);
1711           FeatureMatcherSetI currentFilter = me.fr.getFeatureFilter(type);
1712           if (currentFilter == null)
1713           {
1714             currentFilter = new FeatureMatcherSet();
1715           }
1716           Object[] data = ((FeatureTableModel) table.getModel())
1717                   .getData()[rowSelected];
1718           data[COLOUR_COLUMN] = currentColor;
1719           data[FILTER_COLUMN] = currentFilter;
1720         }
1721         fireEditingStopped();
1722         me.table.validate();
1723       }
1724     }
1725
1726     // Implement the one CellEditor method that AbstractCellEditor doesn't.
1727     @Override
1728     public Object getCellEditorValue()
1729     {
1730       return currentColor;
1731     }
1732
1733     // Implement the one method defined by TableCellEditor.
1734     @Override
1735     public Component getTableCellEditorComponent(JTable theTable, Object value,
1736             boolean isSelected, int row, int column)
1737     {
1738       currentColor = (FeatureColourI) value;
1739       this.rowSelected = row;
1740       type = me.table.getValueAt(row, TYPE_COLUMN).toString();
1741       button.setOpaque(true);
1742       button.setBackground(me.getBackground());
1743       if (!currentColor.isSimpleColour())
1744       {
1745         JLabel btn = new JLabel();
1746         btn.setSize(button.getSize());
1747         FeatureSettings.renderGraduatedColor(btn, currentColor);
1748         button.setBackground(btn.getBackground());
1749         button.setIcon(btn.getIcon());
1750         button.setText(btn.getText());
1751       }
1752       else
1753       {
1754         button.setText("");
1755         button.setIcon(null);
1756         button.setBackground(currentColor.getColour());
1757       }
1758       return button;
1759     }
1760   }
1761
1762   /**
1763    * The cell editor for the Filter column. It displays the text of any filters
1764    * for the feature type in that row (in full as a tooltip, possible abbreviated
1765    * as display text). On click in the cell, opens the Feature Display Settings
1766    * dialog at the Filters tab.
1767    */
1768   class FilterEditor extends AbstractCellEditor
1769           implements TableCellEditor, ActionListener
1770   {
1771     FeatureSettings me;
1772
1773     FeatureMatcherSetI currentFilter;
1774
1775     Point lastLocation;
1776
1777     String type;
1778
1779     JButton button;
1780
1781     protected static final String EDIT = "edit";
1782
1783     int rowSelected = 0;
1784
1785     public FilterEditor(FeatureSettings me)
1786     {
1787       this.me = me;
1788       button = new JButton();
1789       button.setActionCommand(EDIT);
1790       button.addActionListener(this);
1791       button.setBorderPainted(false);
1792     }
1793
1794     /**
1795      * Handles events from the editor button
1796      */
1797     @Override
1798     public void actionPerformed(ActionEvent e)
1799     {
1800       if (button == e.getSource())
1801       {
1802         FeatureTypeSettings chooser = new FeatureTypeSettings(me.fr, type);
1803         chooser.addActionListener(this);
1804         chooser.setRequestFocusEnabled(true);
1805         chooser.requestFocus();
1806         if (lastLocation != null)
1807         {
1808           // todo open at its last position on screen
1809           chooser.setBounds(lastLocation.x, lastLocation.y,
1810                   chooser.getWidth(), chooser.getHeight());
1811           chooser.validate();
1812         }
1813         chooser.showTab(false);
1814         fireEditingStopped();
1815       }
1816       else if (e.getSource() instanceof Component)
1817       {
1818
1819         /*
1820          * after OK in variable colour dialog, any changes to filter
1821          * (or colours!) are already set in FeatureRenderer, so just
1822          * update table data without triggering updateFeatureRenderer
1823          */
1824         FeatureColourI currentColor = fr.getFeatureColours().get(type);
1825         currentFilter = me.fr.getFeatureFilter(type);
1826         if (currentFilter == null)
1827         {
1828           currentFilter = new FeatureMatcherSet();
1829         }
1830         Object[] data = ((FeatureTableModel) table.getModel())
1831                 .getData()[rowSelected];
1832         data[COLOUR_COLUMN] = currentColor;
1833         data[FILTER_COLUMN] = currentFilter;
1834         fireEditingStopped();
1835         me.table.validate();
1836       }
1837     }
1838
1839     @Override
1840     public Object getCellEditorValue()
1841     {
1842       return currentFilter;
1843     }
1844
1845     @Override
1846     public Component getTableCellEditorComponent(JTable theTable, Object value,
1847             boolean isSelected, int row, int column)
1848     {
1849       currentFilter = (FeatureMatcherSetI) value;
1850       this.rowSelected = row;
1851       type = me.table.getValueAt(row, TYPE_COLUMN).toString();
1852       button.setOpaque(true);
1853       button.setBackground(me.getBackground());
1854       button.setText(currentFilter.toString());
1855       button.setToolTipText(currentFilter.toString());
1856       button.setIcon(null);
1857       return button;
1858     }
1859   }
1860 }
1861
1862 class FeatureIcon implements Icon
1863 {
1864   FeatureColourI gcol;
1865
1866   Color backg;
1867
1868   boolean midspace = false;
1869
1870   int width = 50, height = 20;
1871
1872   int s1, e1; // start and end of midpoint band for thresholded symbol
1873
1874   Color mpcolour = Color.white;
1875
1876   FeatureIcon(FeatureColourI gfc, Color bg, int w, int h, boolean mspace)
1877   {
1878     gcol = gfc;
1879     backg = bg;
1880     width = w;
1881     height = h;
1882     midspace = mspace;
1883     if (midspace)
1884     {
1885       s1 = width / 3;
1886       e1 = s1 * 2;
1887     }
1888     else
1889     {
1890       s1 = width / 2;
1891       e1 = s1;
1892     }
1893   }
1894
1895   @Override
1896   public int getIconWidth()
1897   {
1898     return width;
1899   }
1900
1901   @Override
1902   public int getIconHeight()
1903   {
1904     return height;
1905   }
1906
1907   @Override
1908   public void paintIcon(Component c, Graphics g, int x, int y)
1909   {
1910
1911     if (gcol.isColourByLabel())
1912     {
1913       g.setColor(backg);
1914       g.fillRect(0, 0, width, height);
1915       // need an icon here.
1916       g.setColor(gcol.getMaxColour());
1917
1918       g.setFont(new Font("Verdana", Font.PLAIN, 9));
1919
1920       // g.setFont(g.getFont().deriveFont(
1921       // AffineTransform.getScaleInstance(
1922       // width/g.getFontMetrics().stringWidth("Label"),
1923       // height/g.getFontMetrics().getHeight())));
1924
1925       g.drawString(MessageManager.getString("label.label"), 0, 0);
1926
1927     }
1928     else
1929     {
1930       Color minCol = gcol.getMinColour();
1931       g.setColor(minCol);
1932       g.fillRect(0, 0, s1, height);
1933       if (midspace)
1934       {
1935         g.setColor(Color.white);
1936         g.fillRect(s1, 0, e1 - s1, height);
1937       }
1938       g.setColor(gcol.getMaxColour());
1939       g.fillRect(0, e1, width - e1, height);
1940     }
1941   }
1942 }