X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FFeatureSettings.java;h=fedfe3fc851015f42cdd6490d54fe29b151d809e;hb=e16e9699b56f3db4e42dc694aaafad76dae66c4b;hp=940a216e5aee502f9d394d4571cd9d40379845be;hpb=7ec8c23d92c88099a6378cc60e896e4c90ada5e7;p=jalview.git diff --git a/src/jalview/gui/FeatureSettings.java b/src/jalview/gui/FeatureSettings.java index 940a216..fedfe3f 100644 --- a/src/jalview/gui/FeatureSettings.java +++ b/src/jalview/gui/FeatureSettings.java @@ -20,24 +20,39 @@ */ package jalview.gui; +import static jalview.viewmodel.seqfeatures.FeatureRendererModel.COLOUR_COLUMN; +import static jalview.viewmodel.seqfeatures.FeatureRendererModel.FILTER_COLUMN; +import static jalview.viewmodel.seqfeatures.FeatureRendererModel.SHOW_COLUMN; +import static jalview.viewmodel.seqfeatures.FeatureRendererModel.TYPE_COLUMN; + +import jalview.api.FeatureColourI; import jalview.api.FeatureSettingsControllerI; import jalview.bin.Cache; -import jalview.datamodel.SequenceFeature; +import jalview.datamodel.AlignmentI; import jalview.datamodel.SequenceI; import jalview.gui.Help.HelpId; import jalview.io.JalviewFileChooser; -import jalview.schemes.AnnotationColourGradient; -import jalview.schemes.GraduatedColor; +import jalview.io.JalviewFileView; +import jalview.schemabinding.version2.JalviewUserColours; +import jalview.schemes.FeatureColour; +import jalview.util.Format; import jalview.util.MessageManager; +import jalview.util.Platform; +import jalview.util.QuickSort; +import jalview.util.matcher.KeyedMatcherSet; +import jalview.util.matcher.KeyedMatcherSetI; import jalview.viewmodel.AlignmentViewport; +import jalview.ws.DasSequenceFeatureFetcher; import jalview.ws.dbsources.das.api.jalviewSourceI; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; +import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; +import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -54,9 +69,12 @@ import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.util.Arrays; +import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.Vector; @@ -73,12 +91,10 @@ import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JSlider; -import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; @@ -88,15 +104,24 @@ import javax.swing.event.ChangeListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableColumn; -public class FeatureSettings extends JPanel implements - FeatureSettingsControllerI +public class FeatureSettings extends JPanel + implements FeatureSettingsControllerI { - DasSourceBrowser dassourceBrowser; + private static final int COLUMN_COUNT = 4; + + private static final String COLON = ":"; + + private static final int MIN_WIDTH = 400; - jalview.ws.DasSequenceFeatureFetcher dasFeatureFetcher; + private static final int MIN_HEIGHT = 400; - JPanel settingsPane = new JPanel(); + private static final int MAX_TOOLTIP_LENGTH = 50; + + DasSourceBrowser dassourceBrowser; + + DasSequenceFeatureFetcher dasFeatureFetcher; JPanel dasSettingsPane = new JPanel(); @@ -104,10 +129,15 @@ public class FeatureSettings extends JPanel implements public final AlignFrame af; + /* + * 'original' fields hold settings to restore on Cancel + */ Object[][] originalData; private float originalTransparency; + private Map originalFilters; + final JInternalFrame frame; JScrollPane scrollPane = new JScrollPane(); @@ -118,14 +148,47 @@ public class FeatureSettings extends JPanel implements JSlider transparency = new JSlider(); - JPanel transPanel = new JPanel(new GridLayout(1, 2)); + /* + * when true, constructor is still executing - so ignore UI events + */ + protected volatile boolean inConstruction = true; + + int selectedRow = -1; + + JButton fetchDAS = new JButton(); + + JButton saveDAS = new JButton(); + + JButton cancelDAS = new JButton(); + + boolean resettingTable = false; - public FeatureSettings(AlignFrame af) + /* + * true when Feature Settings are updating from feature renderer + */ + private boolean handlingUpdate = false; + + /* + * holds {featureCount, totalExtent} for each feature type + */ + Map typeWidth = null; + + /** + * Constructor + * + * @param af + */ + public FeatureSettings(AlignFrame alignFrame) { - this.af = af; + this.af = alignFrame; fr = af.getFeatureRenderer(); - // allow transparency to be recovered - transparency.setMaximum(100 - (int) ((originalTransparency=fr.getTransparency()) * 100)); + + // save transparency for restore on Cancel + originalTransparency = fr.getTransparency(); + int originalTransparencyAsPercent = (int) (originalTransparency * 100); + transparency.setMaximum(100 - originalTransparencyAsPercent); + + originalFilters = fr.getFeatureFilters(); try { @@ -135,59 +198,88 @@ public class FeatureSettings extends JPanel implements ex.printStackTrace(); } - table = new JTable() { + table = new JTable() + { @Override - public String getToolTipText(MouseEvent e) { - if (table.columnAtPoint(e.getPoint()) == 0) { - /* - * Tooltip for feature name only - */ - return JvSwingUtils.wrapTooltip(true, - MessageManager.getString("label.feature_settings_click_drag")); + public String getToolTipText(MouseEvent e) + { + String tip = null; + int column = table.columnAtPoint(e.getPoint()); + switch (column) + { + case TYPE_COLUMN: + tip = JvSwingUtils.wrapTooltip(true, MessageManager + .getString("label.feature_settings_click_drag")); + break; + case FILTER_COLUMN: + int row = table.rowAtPoint(e.getPoint()); + KeyedMatcherSet o = (KeyedMatcherSet) table.getValueAt(row, + column); + tip = o.isEmpty() + ? MessageManager.getString("label.filters_tooltip") + : o.toString(); + break; + default: + break; } - return null; + return tip; } }; table.getTableHeader().setFont(new Font("Verdana", Font.PLAIN, 12)); table.setFont(new Font("Verdana", Font.PLAIN, 12)); - table.setDefaultRenderer(Color.class, new ColorRenderer()); - table.setDefaultEditor(Color.class, new ColorEditor(this)); + // table.setDefaultRenderer(Color.class, new ColorRenderer()); + // table.setDefaultEditor(Color.class, new ColorEditor(this)); + // + table.setDefaultEditor(FeatureColour.class, new ColorEditor(this)); + table.setDefaultRenderer(FeatureColour.class, new ColorRenderer()); + + table.setDefaultEditor(KeyedMatcherSet.class, new FilterEditor(this)); + table.setDefaultRenderer(KeyedMatcherSet.class, new FilterRenderer()); + + TableColumn colourColumn = new TableColumn(COLOUR_COLUMN, 75, + new ColorRenderer(), new ColorEditor(this)); + table.addColumn(colourColumn); + + TableColumn filterColumn = new TableColumn(FILTER_COLUMN, 75, + new FilterRenderer(), new FilterEditor(this)); + table.addColumn(filterColumn); - table.setDefaultEditor(GraduatedColor.class, new ColorEditor(this)); - table.setDefaultRenderer(GraduatedColor.class, new ColorRenderer()); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.addMouseListener(new MouseAdapter() { + @Override public void mousePressed(MouseEvent evt) { selectedRow = table.rowAtPoint(evt.getPoint()); - if (SwingUtilities.isRightMouseButton(evt)) + String type = (String) table.getValueAt(selectedRow, TYPE_COLUMN); + if (evt.isPopupTrigger()) { - popupSort(selectedRow, (String) table.getValueAt(selectedRow, 0), - table.getValueAt(selectedRow, 1), fr.getMinMax(), - evt.getX(), evt.getY()); + Object colour = table.getValueAt(selectedRow, COLOUR_COLUMN); + popupSort(selectedRow, type, colour, fr.getMinMax(), evt.getX(), + evt.getY()); } else if (evt.getClickCount() == 2) { + boolean invertSelection = evt.isAltDown(); + boolean toggleSelection = Platform.isControlDown(evt); + boolean extendSelection = evt.isShiftDown(); fr.ap.alignFrame.avc.markColumnsContainingFeatures( - evt.isAltDown(), evt.isShiftDown() || evt.isMetaDown(), - evt.isMetaDown(), - (String) table.getValueAt(selectedRow, 0)); + invertSelection, extendSelection, toggleSelection, type); } } - // isPopupTrigger fires on mouseReleased on Mac + // isPopupTrigger fires on mouseReleased on Windows @Override public void mouseReleased(MouseEvent evt) { selectedRow = table.rowAtPoint(evt.getPoint()); if (evt.isPopupTrigger()) { - popupSort(selectedRow, (String) table.getValueAt(selectedRow, 0), - table.getValueAt(selectedRow, 1), fr.getMinMax(), - evt.getX(), + String type = (String) table.getValueAt(selectedRow, TYPE_COLUMN); + Object colour = table.getValueAt(selectedRow, COLOUR_COLUMN); + popupSort(selectedRow, type, colour, fr.getMinMax(), evt.getX(), evt.getY()); } } @@ -195,30 +287,34 @@ public class FeatureSettings extends JPanel implements table.addMouseMotionListener(new MouseMotionAdapter() { + @Override public void mouseDragged(MouseEvent evt) { int newRow = table.rowAtPoint(evt.getPoint()); if (newRow != selectedRow && selectedRow != -1 && newRow != -1) { - Object[] temp = new Object[3]; - temp[0] = table.getValueAt(selectedRow, 0); - temp[1] = table.getValueAt(selectedRow, 1); - temp[2] = table.getValueAt(selectedRow, 2); - - table.setValueAt(table.getValueAt(newRow, 0), selectedRow, 0); - table.setValueAt(table.getValueAt(newRow, 1), selectedRow, 1); - table.setValueAt(table.getValueAt(newRow, 2), selectedRow, 2); - - table.setValueAt(temp[0], newRow, 0); - table.setValueAt(temp[1], newRow, 1); - table.setValueAt(temp[2], newRow, 2); - + /* + * reposition 'selectedRow' to 'newRow' (the dragged to location) + * this could be more than one row away for a very fast drag action + * so just swap it with adjacent rows until we get it there + */ + Object[][] data = ((FeatureTableModel) table.getModel()) + .getData(); + int direction = newRow < selectedRow ? -1 : 1; + for (int i = selectedRow; i != newRow; i += direction) + { + Object[] temp = data[i]; + data[i] = data[i + direction]; + data[i + direction] = temp; + } + updateFeatureRenderer(data); + table.repaint(); selectedRow = newRow; } } }); -// table.setToolTipText(JvSwingUtils.wrapTooltip(true, -// MessageManager.getString("label.feature_settings_click_drag"))); + // table.setToolTipText(JvSwingUtils.wrapTooltip(true, + // MessageManager.getString("label.feature_settings_click_drag"))); scrollPane.setViewportView(table); dassourceBrowser = new DasSourceBrowser(this); @@ -234,13 +330,14 @@ public class FeatureSettings extends JPanel implements final FeatureSettings fs = this; fr.addPropertyChangeListener(change = new PropertyChangeListener() { + @Override public void propertyChange(PropertyChangeEvent evt) { if (!fs.resettingTable && !fs.handlingUpdate) { fs.handlingUpdate = true; - fs.resetTable(null); // new groups may be added with new seuqence - // feature types only + fs.resetTable(null); + // new groups may be added with new sequence feature types only fs.handlingUpdate = false; } } @@ -249,36 +346,43 @@ public class FeatureSettings extends JPanel implements frame = new JInternalFrame(); frame.setContentPane(this); - if (new jalview.util.Platform().isAMac()) + if (Platform.isAMac()) { Desktop.addInternalFrame(frame, MessageManager.getString("label.sequence_feature_settings"), - 475, 480); + 600, 480); } else { Desktop.addInternalFrame(frame, MessageManager.getString("label.sequence_feature_settings"), - 400, 450); + 600, 450); } + frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT)); - frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() - { - public void internalFrameClosed( - javax.swing.event.InternalFrameEvent evt) - { - fr.removePropertyChangeListener(change); - dassourceBrowser.fs = null; - }; - }); + frame.addInternalFrameListener( + new javax.swing.event.InternalFrameAdapter() + { + @Override + public void internalFrameClosed( + javax.swing.event.InternalFrameEvent evt) + { + fr.removePropertyChangeListener(change); + dassourceBrowser.fs = null; + }; + }); frame.setLayer(JLayeredPane.PALETTE_LAYER); + inConstruction = false; } - protected void popupSort(final int selectedRow, final String type, - final Object typeCol, final Hashtable minmax, int x, int y) + protected void popupSort(final int rowSelected, final String type, + final Object typeCol, final Map minmax, int x, + int y) { - JPopupMenu men = new JPopupMenu(MessageManager.formatMessage( - "label.settings_for_param", new String[] + final FeatureColourI featureColour = (FeatureColourI) typeCol; + + JPopupMenu men = new JPopupMenu(MessageManager + .formatMessage("label.settings_for_param", new String[] { type })); JMenuItem scr = new JMenuItem( MessageManager.getString("label.sort_by_score")); @@ -287,10 +391,12 @@ public class FeatureSettings extends JPanel implements scr.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { - me.af.avc.sortAlignmentByFeatureScore(new String[] - { type }); + me.af.avc + .sortAlignmentByFeatureScore(Arrays.asList(new String[] + { type })); } }); @@ -299,96 +405,84 @@ public class FeatureSettings extends JPanel implements dens.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { - me.af.avc.sortAlignmentByFeatureDensity(new String[] - { type }); + me.af.avc + .sortAlignmentByFeatureDensity(Arrays.asList(new String[] + { type })); } }); men.add(dens); - if (minmax != null) + + /* + * variable colour options include colour by label, by score, + * by selected attribute text, or attribute value + */ + final JCheckBoxMenuItem mxcol = new JCheckBoxMenuItem( + MessageManager.getString("label.variable_colour")); + mxcol.setSelected(!featureColour.isSimpleColour()); + men.add(mxcol); + mxcol.addActionListener(new ActionListener() { - final Object typeMinMax = minmax.get(type); - /* - * final JCheckBoxMenuItem chb = new JCheckBoxMenuItem("Vary Height"); // - * this is broken at the moment and isn't that useful anyway! - * chb.setSelected(minmax.get(type) != null); chb.addActionListener(new - * ActionListener() { - * - * public void actionPerformed(ActionEvent e) { - * chb.setState(chb.getState()); if (chb.getState()) { minmax.put(type, - * null); } else { minmax.put(type, typeMinMax); } } - * - * }); - * - * men.add(chb); - */ - if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null) - { - // if (table.getValueAt(row, column)); - // graduated colourschemes for those where minmax exists for the - // positional features - final JCheckBoxMenuItem mxcol = new JCheckBoxMenuItem( - "Graduated Colour"); - mxcol.setSelected(!(typeCol instanceof Color)); - men.add(mxcol); - mxcol.addActionListener(new ActionListener() - { - JColorChooser colorChooser; + JColorChooser colorChooser; - public void actionPerformed(ActionEvent e) + @Override + public void actionPerformed(ActionEvent e) + { + if (e.getSource() == mxcol) + { + if (featureColour.isSimpleColour()) { - if (e.getSource() == mxcol) - { - if (typeCol instanceof Color) - { - FeatureColourChooser fc = new FeatureColourChooser(me.fr, - type); - fc.addActionListener(this); - } - else - { - // bring up simple color chooser - colorChooser = new JColorChooser(); - JDialog dialog = JColorChooser.createDialog(me, - "Select new Colour", true, // modal - colorChooser, this, // OK button handler - null); // no CANCEL button handler - colorChooser.setColor(((GraduatedColor) typeCol) - .getMaxColor()); - dialog.setVisible(true); - } - } - else - { - if (e.getSource() instanceof FeatureColourChooser) - { - FeatureColourChooser fc = (FeatureColourChooser) e - .getSource(); - table.setValueAt(fc.getLastColour(), selectedRow, 1); - table.validate(); - } - else - { - // probably the color chooser! - table.setValueAt(colorChooser.getColor(), selectedRow, 1); - table.validate(); - me.updateFeatureRenderer( - ((FeatureTableModel) table.getModel()).getData(), - false); - } - } + FeatureTypeSettings fc = new FeatureTypeSettings(me.fr, type); + fc.addActionListener(this); } - - }); + else + { + // bring up simple color chooser + colorChooser = new JColorChooser(); + String title = MessageManager + .getString("label.select_colour"); + JDialog dialog = JColorChooser.createDialog(me, + title, true, // modal + colorChooser, this, // OK button handler + null); // no CANCEL button handler + colorChooser.setColor(featureColour.getMaxColour()); + dialog.setVisible(true); + } + } + else + { + if (e.getSource() instanceof FeatureTypeSettings) + { + /* + * update after OK in feature colour dialog; the updated + * colour will have already been set in the FeatureRenderer + */ + FeatureColourI fci = fr.getFeatureColours().get(type); + table.setValueAt(fci, rowSelected, 1); + table.validate(); + } + else + { + // probably the color chooser! + table.setValueAt(new FeatureColour(colorChooser.getColor()), + rowSelected, 1); + table.validate(); + me.updateFeatureRenderer( + ((FeatureTableModel) table.getModel()).getData(), + false); + } + } } - } + + }); + JMenuItem selCols = new JMenuItem( MessageManager.getString("label.select_columns_containing")); selCols.addActionListener(new ActionListener() { - @Override public void actionPerformed(ActionEvent arg0) { @@ -396,11 +490,10 @@ public class FeatureSettings extends JPanel implements false, type); } }); - JMenuItem clearCols = new JMenuItem( - MessageManager.getString("label.select_columns_not_containing")); + JMenuItem clearCols = new JMenuItem(MessageManager + .getString("label.select_columns_not_containing")); clearCols.addActionListener(new ActionListener() { - @Override public void actionPerformed(ActionEvent arg0) { @@ -408,61 +501,49 @@ public class FeatureSettings extends JPanel implements false, type); } }); + JMenuItem hideCols = new JMenuItem( + MessageManager.getString("label.hide_columns_containing")); + hideCols.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent arg0) + { + fr.ap.alignFrame.hideFeatureColumns(type, true); + } + }); + JMenuItem hideOtherCols = new JMenuItem( + MessageManager.getString("label.hide_columns_not_containing")); + hideOtherCols.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent arg0) + { + fr.ap.alignFrame.hideFeatureColumns(type, false); + } + }); men.add(selCols); men.add(clearCols); + men.add(hideCols); + men.add(hideOtherCols); men.show(table, x, y); } - /** - * true when Feature Settings are updating from feature renderer - */ - private boolean handlingUpdate = false; - - /** - * contains a float[3] for each feature type string. created by setTableData - */ - Hashtable typeWidth = null; - @Override synchronized public void discoverAllFeatureData() { - Vector allFeatures = new Vector(); - Vector allGroups = new Vector(); - SequenceFeature[] tmpfeatures; - String group; - for (int i = 0; i < af.getViewport().getAlignment().getHeight(); i++) - { - tmpfeatures = af.getViewport().getAlignment().getSequenceAt(i) - .getSequenceFeatures(); - if (tmpfeatures == null) - { - continue; - } + Set allGroups = new HashSet<>(); + AlignmentI alignment = af.getViewport().getAlignment(); - int index = 0; - while (index < tmpfeatures.length) + for (int i = 0; i < alignment.getHeight(); i++) + { + SequenceI seq = alignment.getSequenceAt(i); + for (String group : seq.getFeatures().getFeatureGroups(true)) { - if (tmpfeatures[index].begin == 0 && tmpfeatures[index].end == 0) - { - index++; - continue; - } - - if (tmpfeatures[index].getFeatureGroup() != null) - { - group = tmpfeatures[index].featureGroup; - if (!allGroups.contains(group)) - { - allGroups.addElement(group); - checkGroupState(group); - } - } - - if (!allFeatures.contains(tmpfeatures[index].getType())) + if (group != null && !allGroups.contains(group)) { - allFeatures.addElement(tmpfeatures[index].getType()); + allGroups.add(group); + checkGroupState(group); } - index++; } } @@ -481,124 +562,97 @@ public class FeatureSettings extends JPanel implements { boolean visible = fr.checkGroupVisibility(group, true); - if (groupPanel == null) - { - groupPanel = new JPanel(); - } - - boolean alreadyAdded = false; for (int g = 0; g < groupPanel.getComponentCount(); g++) { if (((JCheckBox) groupPanel.getComponent(g)).getText().equals(group)) { - alreadyAdded = true; ((JCheckBox) groupPanel.getComponent(g)).setSelected(visible); - break; + return visible; } } - if (alreadyAdded) - { - - return visible; - } final String grp = group; final JCheckBox check = new JCheckBox(group, visible); check.setFont(new Font("Serif", Font.BOLD, 12)); + check.setToolTipText(group); check.addItemListener(new ItemListener() { + @Override public void itemStateChanged(ItemEvent evt) { fr.setGroupVisibility(check.getText(), check.isSelected()); - af.alignPanel.getSeqPanel().seqCanvas.repaint(); - if (af.alignPanel.overviewPanel != null) - { - af.alignPanel.overviewPanel.updateOverviewImage(); - } - - resetTable(new String[] - { grp }); + resetTable(new String[] { grp }); + af.alignPanel.paintAlignment(true, true); } }); groupPanel.add(check); return visible; } - boolean resettingTable = false; - synchronized void resetTable(String[] groupChanged) { - if (resettingTable == true) + if (resettingTable) { return; } resettingTable = true; - typeWidth = new Hashtable(); + typeWidth = new Hashtable<>(); // TODO: change avWidth calculation to 'per-sequence' average and use long // rather than float - float[] avWidth = null; - SequenceFeature[] tmpfeatures; - String group = null, type; - Vector visibleChecks = new Vector(); - - // Find out which features should be visible depending on which groups - // are selected / deselected - // and recompute average width ordering + + Set displayableTypes = new HashSet<>(); + Set foundGroups = new HashSet<>(); + + /* + * determine which feature types may be visible depending on + * which groups are selected, and recompute average width data + */ for (int i = 0; i < af.getViewport().getAlignment().getHeight(); i++) { - tmpfeatures = af.getViewport().getAlignment().getSequenceAt(i) - .getSequenceFeatures(); - if (tmpfeatures == null) - { - continue; - } + SequenceI seq = af.getViewport().getAlignment().getSequenceAt(i); - int index = 0; - while (index < tmpfeatures.length) + /* + * get the sequence's groups for positional features + * and keep track of which groups are visible + */ + Set groups = seq.getFeatures().getFeatureGroups(true); + Set visibleGroups = new HashSet<>(); + for (String group : groups) { - group = tmpfeatures[index].featureGroup; - - if (tmpfeatures[index].begin == 0 && tmpfeatures[index].end == 0) - { - index++; - continue; - } - if (group == null || checkGroupState(group)) { - type = tmpfeatures[index].getType(); - if (!visibleChecks.contains(type)) - { - visibleChecks.addElement(type); - } - } - if (!typeWidth.containsKey(tmpfeatures[index].getType())) - { - typeWidth.put(tmpfeatures[index].getType(), - avWidth = new float[3]); - } - else - { - avWidth = (float[]) typeWidth.get(tmpfeatures[index].getType()); - } - avWidth[0]++; - if (tmpfeatures[index].getBegin() > tmpfeatures[index].getEnd()) - { - avWidth[1] += 1 + tmpfeatures[index].getBegin() - - tmpfeatures[index].getEnd(); + visibleGroups.add(group); } - else + } + foundGroups.addAll(groups); + + /* + * get distinct feature types for visible groups + * record distinct visible types, and their count and total length + */ + Set types = seq.getFeatures().getFeatureTypesForGroups(true, + visibleGroups.toArray(new String[visibleGroups.size()])); + for (String type : types) + { + displayableTypes.add(type); + float[] avWidth = typeWidth.get(type); + if (avWidth == null) { - avWidth[1] += 1 + tmpfeatures[index].getEnd() - - tmpfeatures[index].getBegin(); + avWidth = new float[2]; + typeWidth.put(type, avWidth); } - index++; + // todo this could include features with a non-visible group + // - do we greatly care? + // todo should we include non-displayable features here, and only + // update when features are added? + avWidth[0] += seq.getFeatures().getFeatureCount(true, type); + avWidth[1] += seq.getFeatures().getTotalFeatureLength(type); } } - int fSize = visibleChecks.size(); - Object[][] data = new Object[fSize][3]; + int columnCount = COLUMN_COUNT; + Object[][] data = new Object[displayableTypes.size()][columnCount]; int dataIndex = 0; if (fr.hasRenderOrder()) @@ -614,68 +668,160 @@ public class FeatureSettings extends JPanel implements List frl = fr.getRenderOrder(); for (int ro = frl.size() - 1; ro > -1; ro--) { - type = frl.get(ro); + String type = frl.get(ro); - if (!visibleChecks.contains(type)) + if (!displayableTypes.contains(type)) { continue; } - data[dataIndex][0] = type; - data[dataIndex][1] = fr.getFeatureStyle(type); - data[dataIndex][2] = new Boolean(af.getViewport() - .getFeaturesDisplayed().isVisible(type)); + data[dataIndex][TYPE_COLUMN] = type; + data[dataIndex][COLOUR_COLUMN] = fr.getFeatureStyle(type); + KeyedMatcherSetI featureFilter = fr.getFeatureFilter(type); + data[dataIndex][FILTER_COLUMN] = featureFilter == null + ? new KeyedMatcherSet() + : featureFilter; + data[dataIndex][SHOW_COLUMN] = new Boolean( + af.getViewport().getFeaturesDisplayed().isVisible(type)); dataIndex++; - visibleChecks.removeElement(type); + displayableTypes.remove(type); } } - fSize = visibleChecks.size(); - for (int i = 0; i < fSize; i++) + /* + * process any extra features belonging only to + * a group which was just selected + */ + while (!displayableTypes.isEmpty()) { - // These must be extra features belonging to the group - // which was just selected - type = visibleChecks.elementAt(i).toString(); - data[dataIndex][0] = type; + String type = displayableTypes.iterator().next(); + data[dataIndex][TYPE_COLUMN] = type; - data[dataIndex][1] = fr.getFeatureStyle(type); - if (data[dataIndex][1] == null) + data[dataIndex][COLOUR_COLUMN] = fr.getFeatureStyle(type); + if (data[dataIndex][COLOUR_COLUMN] == null) { // "Colour has been updated in another view!!" fr.clearRenderOrder(); return; } - - data[dataIndex][2] = new Boolean(true); + KeyedMatcherSetI featureFilter = fr.getFeatureFilter(type); + data[dataIndex][FILTER_COLUMN] = featureFilter == null + ? new KeyedMatcherSet() + : featureFilter; + data[dataIndex][SHOW_COLUMN] = new Boolean(true); dataIndex++; + displayableTypes.remove(type); } if (originalData == null) { - originalData = new Object[data.length][3]; + int size = data[0].length; + originalData = new Object[data.length][size]; for (int i = 0; i < data.length; i++) { - System.arraycopy(data[i], 0, originalData[i], 0, 3); + System.arraycopy(data[i], 0, originalData[i], 0, size); } } + else + { + updateOriginalData(data); + } table.setModel(new FeatureTableModel(data)); table.getColumnModel().getColumn(0).setPreferredWidth(200); - if (groupPanel != null) - { - groupPanel.setLayout(new GridLayout( - fr.getFeatureGroupsSize() / 4 + 1, 4)); - - groupPanel.validate(); - bigPanel.add(groupPanel, BorderLayout.NORTH); - } + groupPanel.setLayout( + new GridLayout(fr.getFeatureGroupsSize() / 4 + 1, 4)); + pruneGroups(foundGroups); + groupPanel.validate(); updateFeatureRenderer(data, groupChanged != null); resettingTable = false; } /** + * Updates 'originalData' (used for restore on Cancel) if we detect that changes + * have been made outwith this dialog + *
    + *
  • a new feature type added (and made visible)
  • + *
  • a feature colour changed (in the Amend Features dialog)
  • + *
+ * + * @param foundData + */ + protected void updateOriginalData(Object[][] foundData) + { + // todo LinkedHashMap instead of Object[][] would be nice + + Object[][] currentData = ((FeatureTableModel) table.getModel()) + .getData(); + for (Object[] row : foundData) + { + String type = (String) row[TYPE_COLUMN]; + boolean found = false; + for (Object[] current : currentData) + { + if (type.equals(current[TYPE_COLUMN])) + { + found = true; + /* + * currently dependent on object equality here; + * really need an equals method on FeatureColour + */ + if (!row[COLOUR_COLUMN].equals(current[COLOUR_COLUMN])) + { + /* + * feature colour has changed externally - update originalData + */ + for (Object[] original : originalData) + { + if (type.equals(original[TYPE_COLUMN])) + { + original[COLOUR_COLUMN] = row[COLOUR_COLUMN]; + break; + } + } + } + break; + } + } + if (!found) + { + /* + * new feature detected - add to original data (on top) + */ + int size = currentData[0].length; + Object[][] newData = new Object[originalData.length + 1][size]; + for (int i = 0; i < originalData.length; i++) + { + System.arraycopy(originalData[i], 0, newData[i + 1], 0, size); + } + newData[0] = row; + originalData = newData; + } + } + } + + /** + * Remove from the groups panel any checkboxes for groups that are not in the + * foundGroups set. This enables removing a group from the display when the last + * feature in that group is deleted. + * + * @param foundGroups + */ + protected void pruneGroups(Set foundGroups) + { + for (int g = 0; g < groupPanel.getComponentCount(); g++) + { + JCheckBox checkbox = (JCheckBox) groupPanel.getComponent(g); + if (!foundGroups.contains(checkbox.getText())) + { + groupPanel.remove(checkbox); + } + } + } + + /** * reorder data based on the featureRenderers global priority list. * * @param data @@ -704,12 +850,11 @@ public class FeatureSettings extends JPanel implements void load() { - JalviewFileChooser chooser = new JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] - { "fc" }, new String[] - { "Sequence Feature Colours" }, "Sequence Feature Colours"); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle(MessageManager.getString("label.load_feature_colours")); + JalviewFileChooser chooser = new JalviewFileChooser("fc", + "Sequence Feature Colours"); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle( + MessageManager.getString("label.load_feature_colours")); chooser.setToolTipText(MessageManager.getString("action.load")); int value = chooser.showOpenDialog(this); @@ -720,12 +865,10 @@ public class FeatureSettings extends JPanel implements try { - InputStreamReader in = new InputStreamReader(new FileInputStream( - file), "UTF-8"); + InputStreamReader in = new InputStreamReader( + new FileInputStream(file), "UTF-8"); - jalview.schemabinding.version2.JalviewUserColours jucs = new jalview.schemabinding.version2.JalviewUserColours(); - jucs = jucs - .unmarshal(in); + JalviewUserColours jucs = JalviewUserColours.unmarshal(in); for (int i = jucs.getColourCount() - 1; i >= 0; i--) { @@ -744,7 +887,7 @@ public class FeatureSettings extends JPanel implements Cache.log.warn("Couldn't parse out graduated feature color.", e); } - GraduatedColor gcol = new GraduatedColor(mincol, maxcol, + FeatureColourI gcol = new FeatureColour(mincol, maxcol, newcol.getMin(), newcol.getMax()); if (newcol.hasAutoScale()) { @@ -756,31 +899,28 @@ public class FeatureSettings extends JPanel implements } if (newcol.hasThreshold()) { - gcol.setThresh(newcol.getThreshold()); - gcol.setThreshType(AnnotationColourGradient.NO_THRESHOLD); // default + gcol.setThreshold(newcol.getThreshold()); } if (newcol.getThreshType().length() > 0) { String ttyp = newcol.getThreshType(); - if (ttyp.equalsIgnoreCase("NONE")) - { - gcol.setThreshType(AnnotationColourGradient.NO_THRESHOLD); - } if (ttyp.equalsIgnoreCase("ABOVE")) { - gcol.setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD); + gcol.setAboveThreshold(true); } if (ttyp.equalsIgnoreCase("BELOW")) { - gcol.setThreshType(AnnotationColourGradient.BELOW_THRESHOLD); + gcol.setBelowThreshold(true); } } fr.setColour(name = newcol.getName(), gcol); } else { - fr.setColour(name = jucs.getColour(i).getName(), new Color( - Integer.parseInt(jucs.getColour(i).getRGB(), 16))); + Color color = new Color( + Integer.parseInt(jucs.getColour(i).getRGB(), 16)); + fr.setColour(name = jucs.getColour(i).getName(), + new FeatureColour(color)); } fr.setOrder(name, (i == 0) ? 0 : i / jucs.getColourCount()); } @@ -802,12 +942,11 @@ public class FeatureSettings extends JPanel implements void save() { - JalviewFileChooser chooser = new JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] - { "fc" }, new String[] - { "Sequence Feature Colours" }, "Sequence Feature Colours"); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle(MessageManager.getString("label.save_feature_colours")); + JalviewFileChooser chooser = new JalviewFileChooser("fc", + "Sequence Feature Colours"); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle( + MessageManager.getString("label.save_feature_colours")); chooser.setToolTipText(MessageManager.getString("action.save")); int value = chooser.showSaveDialog(this); @@ -822,50 +961,40 @@ public class FeatureSettings extends JPanel implements PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream(choice), "UTF-8")); - Set fr_colours = fr.getAllFeatureColours(); - Iterator e = fr_colours.iterator(); + Set fr_colours = fr.getAllFeatureColours(); + Iterator e = fr_colours.iterator(); float[] sortOrder = new float[fr_colours.size()]; String[] sortTypes = new String[fr_colours.size()]; int i = 0; while (e.hasNext()) { - sortTypes[i] = e.next().toString(); + sortTypes[i] = e.next(); sortOrder[i] = fr.getOrder(sortTypes[i]); i++; } - jalview.util.QuickSort.sort(sortOrder, sortTypes); + QuickSort.sort(sortOrder, sortTypes); sortOrder = null; - Object fcol; - GraduatedColor gcol; for (i = 0; i < sortTypes.length; i++) { jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour(); col.setName(sortTypes[i]); - col.setRGB(jalview.util.Format.getHexString(fr.getColour(col - .getName()))); - fcol = fr.getFeatureStyle(sortTypes[i]); - if (fcol instanceof GraduatedColor) + FeatureColourI fcol = fr.getFeatureStyle(sortTypes[i]); + if (fcol.isSimpleColour()) { - gcol = (GraduatedColor) fcol; - col.setMin(gcol.getMin()); - col.setMax(gcol.getMax()); - col.setMinRGB(jalview.util.Format.getHexString(gcol - .getMinColor())); - col.setAutoScale(gcol.isAutoScale()); - col.setThreshold(gcol.getThresh()); - col.setColourByLabel(gcol.isColourByLabel()); - switch (gcol.getThreshType()) - { - case AnnotationColourGradient.NO_THRESHOLD: - col.setThreshType("NONE"); - break; - case AnnotationColourGradient.ABOVE_THRESHOLD: - col.setThreshType("ABOVE"); - break; - case AnnotationColourGradient.BELOW_THRESHOLD: - col.setThreshType("BELOW"); - break; - } + col.setRGB(Format.getHexString(fcol.getColour())); + } + else + { + col.setRGB(Format.getHexString(fcol.getMaxColour())); + col.setMin(fcol.getMin()); + col.setMax(fcol.getMax()); + col.setMinRGB( + jalview.util.Format.getHexString(fcol.getMinColour())); + col.setAutoScale(fcol.isAutoScaled()); + col.setThreshold(fcol.getThreshold()); + col.setColourByLabel(fcol.isColourByLabel()); + col.setThreshType(fcol.isAboveThreshold() ? "ABOVE" + : (fcol.isBelowThreshold() ? "BELOW" : "NONE")); } ucs.addColour(col); } @@ -882,9 +1011,9 @@ public class FeatureSettings extends JPanel implements { for (int i = 0; i < table.getRowCount(); i++) { - Boolean value = (Boolean) table.getValueAt(i, 2); + Boolean value = (Boolean) table.getValueAt(i, SHOW_COLUMN); - table.setValueAt(new Boolean(!value.booleanValue()), i, 2); + table.setValueAt(new Boolean(!value.booleanValue()), i, SHOW_COLUMN); } } @@ -898,17 +1027,16 @@ public class FeatureSettings extends JPanel implements float[] width = new float[data.length]; float[] awidth; float max = 0; - int num = 0; + for (int i = 0; i < data.length; i++) { - awidth = (float[]) typeWidth.get(data[i][0]); + awidth = typeWidth.get(data[i][TYPE_COLUMN]); if (awidth[0] > 0) { width[i] = awidth[1] / awidth[0];// *awidth[0]*awidth[2]; - better // weight - but have to make per // sequence, too (awidth[2]) // if (width[i]==1) // hack to distinguish single width sequences. - num++; } else { @@ -925,16 +1053,17 @@ public class FeatureSettings extends JPanel implements // awidth = (float[]) typeWidth.get(data[i][0]); if (width[i] == 0) { - width[i] = fr.getOrder(data[i][0].toString()); + width[i] = fr.getOrder(data[i][TYPE_COLUMN].toString()); if (width[i] < 0) { - width[i] = fr.setOrder(data[i][0].toString(), i / data.length); + width[i] = fr.setOrder(data[i][TYPE_COLUMN].toString(), + i / data.length); } } else { width[i] /= max; // normalize - fr.setOrder(data[i][0].toString(), width[i]); // store for later + fr.setOrder(data[i][TYPE_COLUMN].toString(), width[i]); // store for later } if (i > 0) { @@ -942,9 +1071,9 @@ public class FeatureSettings extends JPanel implements } } if (sort) - { + { jalview.util.QuickSort.sort(width, data); - // update global priority order + // update global priority order } updateFeatureRenderer(data, false); @@ -967,104 +1096,88 @@ public class FeatureSettings extends JPanel implements updateFeatureRenderer(data, true); } + /** + * Update the priority order of features; only repaint if this changed the order + * of visible features + * + * @param data + * @param visibleNew + */ private void updateFeatureRenderer(Object[][] data, boolean visibleNew) { - fr.setFeaturePriority(data, visibleNew); - af.alignPanel.paintAlignment(true); + if (fr.setFeaturePriority(data, visibleNew)) + { + af.alignPanel.paintAlignment(true, true); + } } - int selectedRow = -1; - - JTabbedPane tabbedPane = new JTabbedPane(); - - BorderLayout borderLayout1 = new BorderLayout(); - - BorderLayout borderLayout2 = new BorderLayout(); - - BorderLayout borderLayout3 = new BorderLayout(); - - JPanel bigPanel = new JPanel(); - - BorderLayout borderLayout4 = new BorderLayout(); - - JButton invert = new JButton(); - - JPanel buttonPanel = new JPanel(); - - JButton cancel = new JButton(); - - JButton ok = new JButton(); - - JButton loadColours = new JButton(); - - JButton saveColours = new JButton(); - - JPanel dasButtonPanel = new JPanel(); - - JButton fetchDAS = new JButton(); - - JButton saveDAS = new JButton(); - - JButton cancelDAS = new JButton(); - - JButton optimizeOrder = new JButton(); + private void jbInit() throws Exception + { + this.setLayout(new BorderLayout()); - JButton sortByScore = new JButton(); + JPanel settingsPane = new JPanel(); + settingsPane.setLayout(new BorderLayout()); - JButton sortByDens = new JButton(); + dasSettingsPane.setLayout(new BorderLayout()); - JButton help = new JButton(); + JPanel bigPanel = new JPanel(); + bigPanel.setLayout(new BorderLayout()); - JPanel transbuttons = new JPanel(new GridLayout(5, 1)); + groupPanel = new JPanel(); + bigPanel.add(groupPanel, BorderLayout.NORTH); - private void jbInit() throws Exception - { - this.setLayout(borderLayout1); - settingsPane.setLayout(borderLayout2); - dasSettingsPane.setLayout(borderLayout3); - bigPanel.setLayout(borderLayout4); + JButton invert = new JButton( + MessageManager.getString("label.invert_selection")); invert.setFont(JvSwingUtils.getLabelFont()); - invert.setText(MessageManager.getString("label.invert_selection")); invert.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { invertSelection(); } }); + + JButton optimizeOrder = new JButton( + MessageManager.getString("label.optimise_order")); optimizeOrder.setFont(JvSwingUtils.getLabelFont()); - optimizeOrder.setText(MessageManager.getString("label.optimise_order")); optimizeOrder.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { orderByAvWidth(); } }); + + JButton sortByScore = new JButton( + MessageManager.getString("label.seq_sort_by_score")); sortByScore.setFont(JvSwingUtils.getLabelFont()); - sortByScore - .setText(MessageManager.getString("label.seq_sort_by_score")); sortByScore.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { af.avc.sortAlignmentByFeatureScore(null); } }); + JButton sortByDens = new JButton( + MessageManager.getString("label.sequence_sort_by_density")); sortByDens.setFont(JvSwingUtils.getLabelFont()); - sortByDens.setText(MessageManager - .getString("label.sequence_sort_by_density")); sortByDens.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { af.avc.sortAlignmentByFeatureDensity(null); } }); + + JButton help = new JButton(MessageManager.getString("action.help")); help.setFont(JvSwingUtils.getLabelFont()); - help.setText(MessageManager.getString("action.help")); help.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { try @@ -1080,6 +1193,7 @@ public class FeatureSettings extends JPanel implements help.setText(MessageManager.getString("action.help")); help.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { try @@ -1091,39 +1205,50 @@ public class FeatureSettings extends JPanel implements } } }); + + JButton cancel = new JButton(MessageManager.getString("action.cancel")); cancel.setFont(JvSwingUtils.getLabelFont()); - cancel.setText(MessageManager.getString("action.cancel")); cancel.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { fr.setTransparency(originalTransparency); + fr.setFeatureFilters(originalFilters); updateFeatureRenderer(originalData); close(); } }); + + JButton ok = new JButton(MessageManager.getString("action.ok")); ok.setFont(JvSwingUtils.getLabelFont()); - ok.setText(MessageManager.getString("action.ok")); ok.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { close(); } }); + + JButton loadColours = new JButton( + MessageManager.getString("label.load_colours")); loadColours.setFont(JvSwingUtils.getLabelFont()); - loadColours.setText(MessageManager.getString("label.load_colours")); loadColours.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { load(); } }); + + JButton saveColours = new JButton( + MessageManager.getString("label.save_colours")); saveColours.setFont(JvSwingUtils.getLabelFont()); - saveColours.setText(MessageManager.getString("label.save_colours")); saveColours.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { save(); @@ -1131,19 +1256,24 @@ public class FeatureSettings extends JPanel implements }); transparency.addChangeListener(new ChangeListener() { + @Override public void stateChanged(ChangeEvent evt) { - fr.setTransparency((100 - transparency.getValue()) / 100f); - af.alignPanel.paintAlignment(true); + if (!inConstruction) + { + fr.setTransparency((100 - transparency.getValue()) / 100f); + af.alignPanel.paintAlignment(true, true); + } } }); transparency.setMaximum(70); - transparency.setToolTipText(MessageManager - .getString("label.transparency_tip")); + transparency.setToolTipText( + MessageManager.getString("label.transparency_tip")); fetchDAS.setText(MessageManager.getString("label.fetch_das_features")); fetchDAS.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { fetchDAS_actionPerformed(e); @@ -1152,46 +1282,52 @@ public class FeatureSettings extends JPanel implements saveDAS.setText(MessageManager.getString("action.save_as_default")); saveDAS.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { saveDAS_actionPerformed(e); } }); + + JPanel dasButtonPanel = new JPanel(); dasButtonPanel.setBorder(BorderFactory.createEtchedBorder()); dasSettingsPane.setBorder(null); cancelDAS.setEnabled(false); cancelDAS.setText(MessageManager.getString("action.cancel_fetch")); cancelDAS.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { cancelDAS_actionPerformed(e); } }); - this.add(tabbedPane, java.awt.BorderLayout.CENTER); - tabbedPane.addTab(MessageManager.getString("label.feature_settings"), settingsPane); - tabbedPane.addTab(MessageManager.getString("label.das_settings"), dasSettingsPane); - bigPanel.add(transPanel, java.awt.BorderLayout.SOUTH); + + JPanel transPanel = new JPanel(new GridLayout(1, 2)); + bigPanel.add(transPanel, BorderLayout.SOUTH); + + JPanel transbuttons = new JPanel(new GridLayout(5, 1)); transbuttons.add(optimizeOrder); transbuttons.add(invert); transbuttons.add(sortByScore); transbuttons.add(sortByDens); transbuttons.add(help); - JPanel sliderPanel = new JPanel(); - sliderPanel.add(transparency); transPanel.add(transparency); transPanel.add(transbuttons); + + JPanel buttonPanel = new JPanel(); buttonPanel.add(ok); buttonPanel.add(cancel); buttonPanel.add(loadColours); buttonPanel.add(saveColours); - bigPanel.add(scrollPane, java.awt.BorderLayout.CENTER); - dasSettingsPane.add(dasButtonPanel, java.awt.BorderLayout.SOUTH); + bigPanel.add(scrollPane, BorderLayout.CENTER); + dasSettingsPane.add(dasButtonPanel, BorderLayout.SOUTH); dasButtonPanel.add(fetchDAS); dasButtonPanel.add(cancelDAS); dasButtonPanel.add(saveDAS); - settingsPane.add(bigPanel, java.awt.BorderLayout.CENTER); - settingsPane.add(buttonPanel, java.awt.BorderLayout.SOUTH); + settingsPane.add(bigPanel, BorderLayout.CENTER); + settingsPane.add(buttonPanel, BorderLayout.SOUTH); + this.add(settingsPane); } public void fetchDAS_actionPerformed(ActionEvent e) @@ -1199,7 +1335,8 @@ public class FeatureSettings extends JPanel implements fetchDAS.setEnabled(false); cancelDAS.setEnabled(true); dassourceBrowser.setGuiEnabled(false); - Vector selectedSources = dassourceBrowser.getSelectedSources(); + Vector selectedSources = dassourceBrowser + .getSelectedSources(); doDasFeatureFetch(selectedSources, true, true); } @@ -1258,7 +1395,7 @@ public class FeatureSettings extends JPanel implements * Vector of Strings to resolve to DAS source nicknames. * @return sources that are present in source list. */ - public List resolveSourceNicknames(Vector sources) + public List resolveSourceNicknames(Vector sources) { return dassourceBrowser.sourceRegistry.resolveSourceNicknames(sources); } @@ -1269,7 +1406,7 @@ public class FeatureSettings extends JPanel implements * * @return vector of selected das source nicknames */ - public Vector getSelectedSources() + public Vector getSelectedSources() { return dassourceBrowser.getSelectedSources(); } @@ -1283,7 +1420,7 @@ public class FeatureSettings extends JPanel implements * if true then runs in same thread, otherwise passes to the Swing * executor */ - public void fetchDasFeatures(Vector sources, boolean block) + public void fetchDasFeatures(Vector sources, boolean block) { initDasSources(); List resolved = dassourceBrowser.sourceRegistry @@ -1300,6 +1437,7 @@ public class FeatureSettings extends JPanel implements Runnable fetcher = new Runnable() { + @Override public void run() { doDasFeatureFetch(dassources, true, false); @@ -1343,15 +1481,10 @@ public class FeatureSettings extends JPanel implements public void noDasSourceActive() { complete(); - JOptionPane - .showInternalConfirmDialog( - Desktop.desktop, - MessageManager - .getString("label.no_das_sources_selected_warn"), - MessageManager - .getString("label.no_das_sources_selected_title"), - JOptionPane.DEFAULT_OPTION, - JOptionPane.INFORMATION_MESSAGE); + JvOptionPane.showInternalConfirmDialog(Desktop.desktop, + MessageManager.getString("label.no_das_sources_selected_warn"), + MessageManager.getString("label.no_das_sources_selected_title"), + JvOptionPane.DEFAULT_OPTION, JvOptionPane.INFORMATION_MESSAGE); } // /////////////////////////////////////////////////////////////////////// @@ -1359,16 +1492,20 @@ public class FeatureSettings extends JPanel implements // /////////////////////////////////////////////////////////////////////// class FeatureTableModel extends AbstractTableModel { + + private String[] columnNames = { + MessageManager.getString("label.feature_type"), + MessageManager.getString("action.colour"), + MessageManager.getString("label.filter"), + MessageManager.getString("label.show") }; + + private Object[][] data; + FeatureTableModel(Object[][] data) { this.data = data; } - private String[] columnNames = - { MessageManager.getString("label.feature_type"), MessageManager.getString("action.colour"), MessageManager.getString("label.display") }; - - private Object[][] data; - public Object[][] getData() { return data; @@ -1379,6 +1516,7 @@ public class FeatureSettings extends JPanel implements this.data = data; } + @Override public int getColumnCount() { return columnNames.length; @@ -1389,31 +1527,41 @@ public class FeatureSettings extends JPanel implements return data[row]; } + @Override public int getRowCount() { return data.length; } + @Override public String getColumnName(int col) { return columnNames[col]; } + @Override public Object getValueAt(int row, int col) { return data[row][col]; } - public Class getColumnClass(int c) + /** + * Answers the class of the object in column c of the first row of the table + */ + @Override + public Class getColumnClass(int c) { - return getValueAt(0, c).getClass(); + Object v = getValueAt(0, c); + return v == null ? null : v.getClass(); } + @Override public boolean isCellEditable(int row, int col) { return col == 0 ? false : true; } + @Override public void setValueAt(Object value, int row, int col) { data[row][col] = value; @@ -1438,43 +1586,33 @@ public class FeatureSettings extends JPanel implements setVerticalTextPosition(SwingConstants.CENTER); } - public Component getTableCellRendererComponent(JTable table, - Object color, boolean isSelected, boolean hasFocus, int row, - int column) + @Override + public Component getTableCellRendererComponent(JTable tbl, Object color, + boolean isSelected, boolean hasFocus, int row, int column) { - // JLabel comp = new JLabel(); - // comp. + FeatureColourI cellColour = (FeatureColourI) color; setOpaque(true); - // comp. - // setBounds(getBounds()); - Color newColor; setToolTipText(baseTT); - setBackground(table.getBackground()); - if (color instanceof GraduatedColor) + setBackground(tbl.getBackground()); + if (!cellColour.isSimpleColour()) { - Rectangle cr = table.getCellRect(row, column, false); - FeatureSettings.renderGraduatedColor(this, (GraduatedColor) color, + Rectangle cr = tbl.getCellRect(row, column, false); + FeatureSettings.renderGraduatedColor(this, cellColour, (int) cr.getWidth(), (int) cr.getHeight()); - } else { this.setText(""); this.setIcon(null); - newColor = (Color) color; - // comp. - setBackground(newColor); - // comp.setToolTipText("RGB value: " + newColor.getRed() + ", " - // + newColor.getGreen() + ", " + newColor.getBlue()); + setBackground(cellColour.getColour()); } if (isSelected) { if (selectedBorder == null) { selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, - table.getSelectionBackground()); + tbl.getSelectionBackground()); } - // comp. setBorder(selectedBorder); } else @@ -1482,9 +1620,56 @@ public class FeatureSettings extends JPanel implements if (unselectedBorder == null) { unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, - table.getBackground()); + tbl.getBackground()); + } + setBorder(unselectedBorder); + } + + return this; + } + } + + class FilterRenderer extends JLabel implements TableCellRenderer + { + javax.swing.border.Border unselectedBorder = null; + + javax.swing.border.Border selectedBorder = null; + + public FilterRenderer() + { + setOpaque(true); // MUST do this for background to show up. + setHorizontalTextPosition(SwingConstants.CENTER); + setVerticalTextPosition(SwingConstants.CENTER); + } + + @Override + public Component getTableCellRendererComponent(JTable tbl, + Object filter, boolean isSelected, boolean hasFocus, int row, + int column) + { + KeyedMatcherSetI theFilter = (KeyedMatcherSetI) filter; + setOpaque(true); + String asText = theFilter.toString(); + setBackground(tbl.getBackground()); + this.setText(asText); + this.setIcon(null); + + if (isSelected) + { + if (selectedBorder == null) + { + selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, + tbl.getSelectionBackground()); + } + setBorder(selectedBorder); + } + else + { + if (unselectedBorder == null) + { + unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, + tbl.getBackground()); } - // comp. setBorder(unselectedBorder); } @@ -1498,7 +1683,7 @@ public class FeatureSettings extends JPanel implements * @param comp * @param gcol */ - public static void renderGraduatedColor(JLabel comp, GraduatedColor gcol) + public static void renderGraduatedColor(JLabel comp, FeatureColourI gcol) { int w = comp.getWidth(), h = comp.getHeight(); if (w < 20) @@ -1514,37 +1699,52 @@ public class FeatureSettings extends JPanel implements renderGraduatedColor(comp, gcol, w, h); } - public static void renderGraduatedColor(JLabel comp, GraduatedColor gcol, + public static void renderGraduatedColor(JLabel comp, FeatureColourI gcol, int w, int h) { boolean thr = false; - String tt = ""; - String tx = ""; - if (gcol.getThreshType() == AnnotationColourGradient.ABOVE_THRESHOLD) + StringBuilder tt = new StringBuilder(); + StringBuilder tx = new StringBuilder(); + + if (gcol.isColourByAttribute()) + { + tx.append(String.join(":", gcol.getAttributeName())); + } + else if (!gcol.isColourByLabel()) + { + tx.append(MessageManager.getString("label.score")); + } + tx.append(" "); + if (gcol.isAboveThreshold()) { thr = true; - tx += ">"; - tt += "Thresholded (Above " + gcol.getThresh() + ") "; + tx.append(">"); + tt.append("Thresholded (Above ").append(gcol.getThreshold()) + .append(") "); } - if (gcol.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD) + if (gcol.isBelowThreshold()) { thr = true; - tx += "<"; - tt += "Thresholded (Below " + gcol.getThresh() + ") "; + tx.append("<"); + tt.append("Thresholded (Below ").append(gcol.getThreshold()) + .append(") "); } if (gcol.isColourByLabel()) { - tt = "Coloured by label text. " + tt; + tt.append("Coloured by label text. ").append(tt); if (thr) { - tx += " "; + tx.append(" "); + } + if (!gcol.isColourByAttribute()) + { + tx.append("Label"); } - tx += "Label"; comp.setIcon(null); } else { - Color newColor = gcol.getMaxColor(); + Color newColor = gcol.getMaxColour(); comp.setBackground(newColor); // System.err.println("Width is " + w / 2); Icon ficon = new FeatureIcon(gcol, comp.getBackground(), w, h, thr); @@ -1555,24 +1755,264 @@ public class FeatureSettings extends JPanel implements // + ", " + minCol.getBlue() + ")"); } comp.setHorizontalAlignment(SwingConstants.CENTER); - comp.setText(tx); + comp.setText(tx.toString()); if (tt.length() > 0) { if (comp.getToolTipText() == null) { - comp.setToolTipText(tt); + comp.setToolTipText(tt.toString()); + } + else + { + comp.setToolTipText( + tt.append(" ").append(comp.getToolTipText()).toString()); + } + } + } + + class ColorEditor extends AbstractCellEditor + implements TableCellEditor, ActionListener + { + FeatureSettings me; + + FeatureColourI currentColor; + + FeatureTypeSettings chooser; + + String type; + + JButton button; + + JColorChooser colorChooser; + + JDialog dialog; + + protected static final String EDIT = "edit"; + + int rowSelected = 0; + + public ColorEditor(FeatureSettings me) + { + this.me = me; + // Set up the editor (from the table's point of view), + // which is a button. + // This button brings up the color chooser dialog, + // which is the editor from the user's point of view. + button = new JButton(); + button.setActionCommand(EDIT); + button.addActionListener(this); + button.setBorderPainted(false); + // Set up the dialog that the button brings up. + colorChooser = new JColorChooser(); + dialog = JColorChooser.createDialog(button, + MessageManager.getString("label.select_colour"), true, // modal + colorChooser, this, // OK button handler + null); // no CANCEL button handler + } + + /** + * Handles events from the editor button and from the dialog's OK button. + */ + @Override + public void actionPerformed(ActionEvent e) + { + // todo test e.getSource() instead here + if (EDIT.equals(e.getActionCommand())) + { + // The user has clicked the cell, so + // bring up the dialog. + if (currentColor.isSimpleColour()) + { + // bring up simple color chooser + button.setBackground(currentColor.getColour()); + colorChooser.setColor(currentColor.getColour()); + dialog.setVisible(true); + } + else + { + // bring up graduated chooser. + chooser = new FeatureTypeSettings(me.fr, type); + chooser.setRequestFocusEnabled(true); + chooser.requestFocus(); + chooser.addActionListener(this); + chooser.showTab(true); + } + // Make the renderer reappear. + fireEditingStopped(); + + } + else + { + if (currentColor.isSimpleColour()) + { + /* + * read off colour picked in colour chooser after OK pressed + */ + currentColor = new FeatureColour(colorChooser.getColor()); + me.table.setValueAt(currentColor, rowSelected, COLOUR_COLUMN); + } + else + { + /* + * after OK in variable colour dialog, any changes to colour + * (or filters!) are already set in FeatureRenderer, so just + * update table data without triggering updateFeatureRenderer + */ + currentColor = fr.getFeatureColours().get(type); + KeyedMatcherSetI currentFilter = me.fr.getFeatureFilter(type); + if (currentFilter == null) + { + currentFilter = new KeyedMatcherSet(); + } + Object[] data = ((FeatureTableModel) table.getModel()) + .getData()[rowSelected]; + data[COLOUR_COLUMN] = currentColor; + data[FILTER_COLUMN] = currentFilter; + } + fireEditingStopped(); + me.table.validate(); + } + } + + // Implement the one CellEditor method that AbstractCellEditor doesn't. + @Override + public Object getCellEditorValue() + { + return currentColor; + } + + // Implement the one method defined by TableCellEditor. + @Override + public Component getTableCellEditorComponent(JTable theTable, Object value, + boolean isSelected, int row, int column) + { + currentColor = (FeatureColourI) value; + this.rowSelected = row; + type = me.table.getValueAt(row, TYPE_COLUMN).toString(); + button.setOpaque(true); + button.setBackground(me.getBackground()); + if (!currentColor.isSimpleColour()) + { + JLabel btn = new JLabel(); + btn.setSize(button.getSize()); + FeatureSettings.renderGraduatedColor(btn, currentColor); + button.setBackground(btn.getBackground()); + button.setIcon(btn.getIcon()); + button.setText(btn.getText()); } else { - comp.setToolTipText(tt + " " + comp.getToolTipText()); + button.setText(""); + button.setIcon(null); + button.setBackground(currentColor.getColour()); } + return button; + } + } + + /** + * The cell editor for the Filter column. It displays the text of any filters + * for the feature type in that row (in full as a tooltip, possible abbreviated + * as display text). On click in the cell, opens the Feature Display Settings + * dialog at the Filters tab. + */ + class FilterEditor extends AbstractCellEditor + implements TableCellEditor, ActionListener + { + FeatureSettings me; + + KeyedMatcherSetI currentFilter; + + Point lastLocation; + + String type; + + JButton button; + + protected static final String EDIT = "edit"; + + int rowSelected = 0; + + public FilterEditor(FeatureSettings me) + { + this.me = me; + button = new JButton(); + button.setActionCommand(EDIT); + button.addActionListener(this); + button.setBorderPainted(false); + } + + /** + * Handles events from the editor button + */ + @Override + public void actionPerformed(ActionEvent e) + { + if (button == e.getSource()) + { + FeatureTypeSettings chooser = new FeatureTypeSettings(me.fr, type); + chooser.addActionListener(this); + chooser.setRequestFocusEnabled(true); + chooser.requestFocus(); + if (lastLocation != null) + { + // todo open at its last position on screen + chooser.setBounds(lastLocation.x, lastLocation.y, + chooser.getWidth(), chooser.getHeight()); + chooser.validate(); + } + chooser.showTab(false); + fireEditingStopped(); + } + else if (e.getSource() instanceof Component) + { + + /* + * after OK in variable colour dialog, any changes to filter + * (or colours!) are already set in FeatureRenderer, so just + * update table data without triggering updateFeatureRenderer + */ + FeatureColourI currentColor = fr.getFeatureColours().get(type); + currentFilter = me.fr.getFeatureFilter(type); + if (currentFilter == null) + { + currentFilter = new KeyedMatcherSet(); + } + Object[] data = ((FeatureTableModel) table.getModel()) + .getData()[rowSelected]; + data[COLOUR_COLUMN] = currentColor; + data[FILTER_COLUMN] = currentFilter; + fireEditingStopped(); + me.table.validate(); + } + } + + @Override + public Object getCellEditorValue() + { + return currentFilter; + } + + @Override + public Component getTableCellEditorComponent(JTable theTable, Object value, + boolean isSelected, int row, int column) + { + currentFilter = (KeyedMatcherSetI) value; + this.rowSelected = row; + type = me.table.getValueAt(row, TYPE_COLUMN).toString(); + button.setOpaque(true); + button.setBackground(me.getBackground()); + button.setText(currentFilter.toString()); + button.setToolTipText(currentFilter.toString()); + button.setIcon(null); + return button; } } } class FeatureIcon implements Icon { - GraduatedColor gcol; + FeatureColourI gcol; Color backg; @@ -1584,7 +2024,7 @@ class FeatureIcon implements Icon Color mpcolour = Color.white; - FeatureIcon(GraduatedColor gfc, Color bg, int w, int h, boolean mspace) + FeatureIcon(FeatureColourI gfc, Color bg, int w, int h, boolean mspace) { gcol = gfc; backg = bg; @@ -1603,16 +2043,19 @@ class FeatureIcon implements Icon } } + @Override public int getIconWidth() { return width; } + @Override public int getIconHeight() { return height; } + @Override public void paintIcon(Component c, Graphics g, int x, int y) { @@ -1621,7 +2064,7 @@ class FeatureIcon implements Icon g.setColor(backg); g.fillRect(0, 0, width, height); // need an icon here. - g.setColor(gcol.getMaxColor()); + g.setColor(gcol.getMaxColour()); g.setFont(new Font("Verdana", Font.PLAIN, 9)); @@ -1635,7 +2078,7 @@ class FeatureIcon implements Icon } else { - Color minCol = gcol.getMinColor(); + Color minCol = gcol.getMinColour(); g.setColor(minCol); g.fillRect(0, 0, s1, height); if (midspace) @@ -1643,137 +2086,8 @@ class FeatureIcon implements Icon g.setColor(Color.white); g.fillRect(s1, 0, e1 - s1, height); } - g.setColor(gcol.getMaxColor()); + g.setColor(gcol.getMaxColour()); g.fillRect(0, e1, width - e1, height); } } } - -class ColorEditor extends AbstractCellEditor implements TableCellEditor, - ActionListener -{ - FeatureSettings me; - - GraduatedColor currentGColor; - - FeatureColourChooser chooser; - - String type; - - Color currentColor; - - JButton button; - - JColorChooser colorChooser; - - JDialog dialog; - - protected static final String EDIT = "edit"; - - int selectedRow = 0; - - public ColorEditor(FeatureSettings me) - { - this.me = me; - // Set up the editor (from the table's point of view), - // which is a button. - // This button brings up the color chooser dialog, - // which is the editor from the user's point of view. - button = new JButton(); - button.setActionCommand(EDIT); - button.addActionListener(this); - button.setBorderPainted(false); - // Set up the dialog that the button brings up. - colorChooser = new JColorChooser(); - dialog = JColorChooser.createDialog(button, "Select new Colour", true, // modal - colorChooser, this, // OK button handler - null); // no CANCEL button handler - } - - /** - * Handles events from the editor button and from the dialog's OK button. - */ - public void actionPerformed(ActionEvent e) - { - - if (EDIT.equals(e.getActionCommand())) - { - // The user has clicked the cell, so - // bring up the dialog. - if (currentColor != null) - { - // bring up simple color chooser - button.setBackground(currentColor); - colorChooser.setColor(currentColor); - dialog.setVisible(true); - } - else - { - // bring up graduated chooser. - chooser = new FeatureColourChooser(me.fr, type); - chooser.setRequestFocusEnabled(true); - chooser.requestFocus(); - chooser.addActionListener(this); - } - // Make the renderer reappear. - fireEditingStopped(); - - } - else - { // User pressed dialog's "OK" button. - if (currentColor != null) - { - currentColor = colorChooser.getColor(); - } - else - { - // class cast exceptions may be raised if the chooser created on a - // non-graduated color - currentGColor = (GraduatedColor) chooser.getLastColour(); - } - me.table.setValueAt(getCellEditorValue(), selectedRow, 1); - fireEditingStopped(); - me.table.validate(); - } - } - - // Implement the one CellEditor method that AbstractCellEditor doesn't. - public Object getCellEditorValue() - { - if (currentColor == null) - { - return currentGColor; - } - return currentColor; - } - - // Implement the one method defined by TableCellEditor. - public Component getTableCellEditorComponent(JTable table, Object value, - boolean isSelected, int row, int column) - { - currentGColor = null; - currentColor = null; - this.selectedRow = row; - type = me.table.getValueAt(row, 0).toString(); - button.setOpaque(true); - button.setBackground(me.getBackground()); - if (value instanceof GraduatedColor) - { - currentGColor = (GraduatedColor) value; - JLabel btn = new JLabel(); - btn.setSize(button.getSize()); - FeatureSettings.renderGraduatedColor(btn, currentGColor); - button.setBackground(btn.getBackground()); - button.setIcon(btn.getIcon()); - button.setText(btn.getText()); - } - else - { - button.setText(""); - button.setIcon(null); - currentColor = (Color) value; - button.setBackground(currentColor); - } - return button; - } -}