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