/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jalview. If not, see .
* The Jalview Authors are detailed in the 'AUTHORS' file.
*/
package jalview.gui;
import jalview.api.FeatureColourI;
import jalview.api.FeatureSettingsControllerI;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.SequenceI;
import jalview.datamodel.features.FeatureMatcherI;
import jalview.datamodel.features.FeatureMatcherSet;
import jalview.datamodel.features.FeatureMatcherSetI;
import jalview.gui.Help.HelpId;
import jalview.io.JalviewFileChooser;
import jalview.io.JalviewFileView;
import jalview.schemabinding.version2.Filter;
import jalview.schemabinding.version2.JalviewUserColours;
import jalview.schemabinding.version2.MatcherSet;
import jalview.schemes.FeatureColour;
import jalview.util.MessageManager;
import jalview.util.Platform;
import jalview.viewmodel.AlignmentViewport;
import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
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;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
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;
import javax.help.HelpSetException;
import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
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
{
private static final String SEQUENCE_FEATURE_COLOURS = MessageManager
.getString("label.sequence_feature_colours");
/*
* column indices of fields in Feature Settings table
*/
static final int TYPE_COLUMN = 0;
static final int COLOUR_COLUMN = 1;
static final int FILTER_COLUMN = 2;
static final int SHOW_COLUMN = 3;
private static final int COLUMN_COUNT = 4;
private static final int MIN_WIDTH = 400;
private static final int MIN_HEIGHT = 400;
DasSourceBrowser dassourceBrowser;
DasSequenceFeatureFetcher dasFeatureFetcher;
JPanel dasSettingsPane = new JPanel();
final FeatureRenderer fr;
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();
JTable table;
JPanel groupPanel;
JSlider transparency = new JSlider();
/*
* 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;
/*
* 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 = alignFrame;
fr = af.getFeatureRenderer();
// save transparency for restore on Cancel
originalTransparency = fr.getTransparency();
int originalTransparencyAsPercent = (int) (originalTransparency * 100);
transparency.setMaximum(100 - originalTransparencyAsPercent);
originalFilters = new HashMap<>(fr.getFeatureFilters()); // shallow copy
try
{
jbInit();
} catch (Exception ex)
{
ex.printStackTrace();
}
table = new JTable()
{
@Override
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());
FeatureMatcherSet o = (FeatureMatcherSet) table.getValueAt(row,
column);
tip = o.isEmpty()
? MessageManager.getString("label.filters_tooltip")
: o.toString();
break;
default:
break;
}
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.setDefaultEditor(FeatureColour.class, new ColorEditor(this));
table.setDefaultRenderer(FeatureColour.class, new ColorRenderer());
table.setDefaultEditor(FeatureMatcherSet.class, new FilterEditor(this));
table.setDefaultRenderer(FeatureMatcherSet.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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent evt)
{
selectedRow = table.rowAtPoint(evt.getPoint());
String type = (String) table.getValueAt(selectedRow, TYPE_COLUMN);
if (evt.isPopupTrigger())
{
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(
invertSelection, extendSelection, toggleSelection, type);
}
}
// isPopupTrigger fires on mouseReleased on Windows
@Override
public void mouseReleased(MouseEvent evt)
{
selectedRow = table.rowAtPoint(evt.getPoint());
if (evt.isPopupTrigger())
{
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());
}
}
});
table.addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent evt)
{
int newRow = table.rowAtPoint(evt.getPoint());
if (newRow != selectedRow && selectedRow != -1 && newRow != -1)
{
/*
* 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")));
scrollPane.setViewportView(table);
dassourceBrowser = new DasSourceBrowser(this);
dasSettingsPane.add(dassourceBrowser, BorderLayout.CENTER);
if (af.getViewport().isShowSequenceFeatures() || !fr.hasRenderOrder())
{
fr.findAllFeatures(true); // display everything!
}
discoverAllFeatureData();
final PropertyChangeListener change;
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 sequence feature types only
fs.handlingUpdate = false;
}
}
});
frame = new JInternalFrame();
frame.setContentPane(this);
if (Platform.isAMac())
{
Desktop.addInternalFrame(frame,
MessageManager.getString("label.sequence_feature_settings"),
600, 480);
}
else
{
Desktop.addInternalFrame(frame,
MessageManager.getString("label.sequence_feature_settings"),
600, 450);
}
frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
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 rowSelected, final String type,
final Object typeCol, final Map minmax, int x,
int y)
{
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"));
men.add(scr);
final FeatureSettings me = this;
scr.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
me.af.avc
.sortAlignmentByFeatureScore(Arrays.asList(new String[]
{ type }));
}
});
JMenuItem dens = new JMenuItem(
MessageManager.getString("label.sort_by_density"));
dens.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
me.af.avc
.sortAlignmentByFeatureDensity(Arrays.asList(new String[]
{ type }));
}
});
men.add(dens);
/*
* 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()
{
JColorChooser colorChooser;
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == mxcol)
{
if (featureColour.isSimpleColour())
{
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)
{
fr.ap.alignFrame.avc.markColumnsContainingFeatures(false, false,
false, type);
}
});
JMenuItem clearCols = new JMenuItem(MessageManager
.getString("label.select_columns_not_containing"));
clearCols.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
fr.ap.alignFrame.avc.markColumnsContainingFeatures(true, false,
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);
}
@Override
synchronized public void discoverAllFeatureData()
{
Set allGroups = new HashSet<>();
AlignmentI alignment = af.getViewport().getAlignment();
for (int i = 0; i < alignment.getHeight(); i++)
{
SequenceI seq = alignment.getSequenceAt(i);
for (String group : seq.getFeatures().getFeatureGroups(true))
{
if (group != null && !allGroups.contains(group))
{
allGroups.add(group);
checkGroupState(group);
}
}
}
resetTable(null);
validate();
}
/**
* Synchronise gui group list and check visibility of group
*
* @param group
* @return true if group is visible
*/
private boolean checkGroupState(String group)
{
boolean visible = fr.checkGroupVisibility(group, true);
for (int g = 0; g < groupPanel.getComponentCount(); g++)
{
if (((JCheckBox) groupPanel.getComponent(g)).getText().equals(group))
{
((JCheckBox) groupPanel.getComponent(g)).setSelected(visible);
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());
resetTable(new String[] { grp });
af.alignPanel.paintAlignment(true, true);
}
});
groupPanel.add(check);
return visible;
}
synchronized void resetTable(String[] groupChanged)
{
if (resettingTable)
{
return;
}
resettingTable = true;
typeWidth = new Hashtable<>();
// TODO: change avWidth calculation to 'per-sequence' average and use long
// rather than float
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++)
{
SequenceI seq = af.getViewport().getAlignment().getSequenceAt(i);
/*
* 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)
{
if (group == null || checkGroupState(group))
{
visibleGroups.add(group);
}
}
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 = new float[2];
typeWidth.put(type, avWidth);
}
// 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);
}
}
Object[][] data = new Object[displayableTypes.size()][COLUMN_COUNT];
int dataIndex = 0;
if (fr.hasRenderOrder())
{
if (!handlingUpdate)
{
fr.findAllFeatures(groupChanged != null); // prod to update
// colourschemes. but don't
// affect display
// First add the checks in the previous render order,
// in case the window has been closed and reopened
}
List frl = fr.getRenderOrder();
for (int ro = frl.size() - 1; ro > -1; ro--)
{
String type = frl.get(ro);
if (!displayableTypes.contains(type))
{
continue;
}
data[dataIndex][TYPE_COLUMN] = type;
data[dataIndex][COLOUR_COLUMN] = fr.getFeatureStyle(type);
FeatureMatcherSetI featureFilter = fr.getFeatureFilter(type);
data[dataIndex][FILTER_COLUMN] = featureFilter == null
? new FeatureMatcherSet()
: featureFilter;
data[dataIndex][SHOW_COLUMN] = new Boolean(
af.getViewport().getFeaturesDisplayed().isVisible(type));
dataIndex++;
displayableTypes.remove(type);
}
}
/*
* process any extra features belonging only to
* a group which was just selected
*/
while (!displayableTypes.isEmpty())
{
String type = displayableTypes.iterator().next();
data[dataIndex][TYPE_COLUMN] = type;
data[dataIndex][COLOUR_COLUMN] = fr.getFeatureStyle(type);
if (data[dataIndex][COLOUR_COLUMN] == null)
{
// "Colour has been updated in another view!!"
fr.clearRenderOrder();
return;
}
FeatureMatcherSetI featureFilter = fr.getFeatureFilter(type);
data[dataIndex][FILTER_COLUMN] = featureFilter == null
? new FeatureMatcherSet()
: featureFilter;
data[dataIndex][SHOW_COLUMN] = new Boolean(true);
dataIndex++;
displayableTypes.remove(type);
}
if (originalData == null)
{
originalData = new Object[data.length][COLUMN_COUNT];
for (int i = 0; i < data.length; i++)
{
System.arraycopy(data[i], 0, originalData[i], 0, COLUMN_COUNT);
}
}
else
{
updateOriginalData(data);
}
table.setModel(new FeatureTableModel(data));
table.getColumnModel().getColumn(0).setPreferredWidth(200);
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)
*/
Object[][] newData = new Object[originalData.length
+ 1][COLUMN_COUNT];
for (int i = 0; i < originalData.length; i++)
{
System.arraycopy(originalData[i], 0, newData[i + 1], 0,
COLUMN_COUNT);
}
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
*/
private void ensureOrder(Object[][] data)
{
boolean sort = false;
float[] order = new float[data.length];
for (int i = 0; i < order.length; i++)
{
order[i] = fr.getOrder(data[i][0].toString());
if (order[i] < 0)
{
order[i] = fr.setOrder(data[i][0].toString(), i / order.length);
}
if (i > 1)
{
sort = sort || order[i - 1] > order[i];
}
}
if (sort)
{
jalview.util.QuickSort.sort(order, data);
}
}
/**
* Offers a file chooser dialog, and then loads the feature colours and
* filters from file in XML format and unmarshals to Jalview feature settings
*/
void load()
{
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);
if (value == JalviewFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
load(file);
}
}
/**
* Loads feature colours and filters from XML stored in the given file
*
* @param file
*/
void load(File file)
{
try
{
InputStreamReader in = new InputStreamReader(
new FileInputStream(file), "UTF-8");
JalviewUserColours jucs = JalviewUserColours.unmarshal(in);
/*
* load feature colours
*/
for (int i = jucs.getColourCount() - 1; i >= 0; i--)
{
jalview.schemabinding.version2.Colour newcol = jucs.getColour(i);
FeatureColourI colour = Jalview2XML.unmarshalColour(newcol);
fr.setColour(newcol.getName(), colour);
fr.setOrder(newcol.getName(), i / (float) jucs.getColourCount());
}
/*
* load feature filters; loaded filters will replace any that are
* currently defined, other defined filters are left unchanged
*/
for (int i = 0; i < jucs.getFilterCount(); i++)
{
jalview.schemabinding.version2.Filter filterModel = jucs
.getFilter(i);
String featureType = filterModel.getFeatureType();
FeatureMatcherSetI filter = Jalview2XML.unmarshalFilter(featureType,
filterModel.getMatcherSet());
if (!filter.isEmpty())
{
fr.setFeatureFilter(featureType, filter);
}
}
/*
* update feature settings table
*/
if (table != null)
{
resetTable(null);
Object[][] data = ((FeatureTableModel) table.getModel())
.getData();
ensureOrder(data);
updateFeatureRenderer(data, false);
table.repaint();
}
} catch (Exception ex)
{
System.out.println("Error loading User Colour File\n" + ex);
}
}
/**
* Offers a file chooser dialog, and then saves the current feature colours
* and any filters to the selected file in XML format
*/
void save()
{
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);
if (value == JalviewFileChooser.APPROVE_OPTION)
{
save(chooser.getSelectedFile());
}
}
/**
* Saves feature colours and filters to the given file
*
* @param file
*/
void save(File file)
{
JalviewUserColours ucs = new JalviewUserColours();
ucs.setSchemeName("Sequence Features");
try
{
PrintWriter out = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(file), "UTF-8"));
/*
* sort feature types by colour order, from 0 (highest)
* to 1 (lowest)
*/
Set fr_colours = fr.getAllFeatureColours();
String[] sortedTypes = fr_colours
.toArray(new String[fr_colours.size()]);
Arrays.sort(sortedTypes, new Comparator()
{
@Override
public int compare(String type1, String type2)
{
return Float.compare(fr.getOrder(type1), fr.getOrder(type2));
}
});
/*
* save feature colours
*/
for (String featureType : sortedTypes)
{
FeatureColourI fcol = fr.getFeatureStyle(featureType);
jalview.schemabinding.version2.Colour col = Jalview2XML.marshalColour(
featureType, fcol);
ucs.addColour(col);
}
/*
* save any feature filters
*/
for (String featureType : sortedTypes)
{
FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
if (filter != null && !filter.isEmpty())
{
Iterator iterator = filter.getMatchers().iterator();
FeatureMatcherI firstMatcher = iterator.next();
MatcherSet ms = Jalview2XML.marshalFilter(firstMatcher, iterator,
filter.isAnded());
Filter filterModel = new Filter();
filterModel.setFeatureType(featureType);
filterModel.setMatcherSet(ms);
ucs.addFilter(filterModel);
}
}
ucs.marshal(out);
out.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
public void invertSelection()
{
for (int i = 0; i < table.getRowCount(); i++)
{
Boolean value = (Boolean) table.getValueAt(i, SHOW_COLUMN);
table.setValueAt(new Boolean(!value.booleanValue()), i, SHOW_COLUMN);
}
}
public void orderByAvWidth()
{
if (table == null || table.getModel() == null)
{
return;
}
Object[][] data = ((FeatureTableModel) table.getModel()).getData();
float[] width = new float[data.length];
float[] awidth;
float max = 0;
for (int i = 0; i < data.length; i++)
{
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.
}
else
{
width[i] = 0;
}
if (max < width[i])
{
max = width[i];
}
}
boolean sort = false;
for (int i = 0; i < width.length; i++)
{
// awidth = (float[]) typeWidth.get(data[i][0]);
if (width[i] == 0)
{
width[i] = fr.getOrder(data[i][TYPE_COLUMN].toString());
if (width[i] < 0)
{
width[i] = fr.setOrder(data[i][TYPE_COLUMN].toString(),
i / data.length);
}
}
else
{
width[i] /= max; // normalize
fr.setOrder(data[i][TYPE_COLUMN].toString(), width[i]); // store for later
}
if (i > 0)
{
sort = sort || width[i - 1] > width[i];
}
}
if (sort)
{
jalview.util.QuickSort.sort(width, data);
// update global priority order
}
updateFeatureRenderer(data, false);
table.repaint();
}
public void close()
{
try
{
frame.setClosed(true);
} catch (Exception exe)
{
}
}
public void updateFeatureRenderer(Object[][] data)
{
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)
{
FeatureSettingsBean[] rowData = getTableAsBeans(data);
if (fr.setFeaturePriority(rowData, visibleNew))
{
af.alignPanel.paintAlignment(true, true);
}
}
/**
* Converts table data into an array of data beans
*/
private FeatureSettingsBean[] getTableAsBeans(Object[][] data)
{
FeatureSettingsBean[] rowData = new FeatureSettingsBean[data.length];
for (int i = 0; i < data.length; i++)
{
String type = (String) data[i][TYPE_COLUMN];
FeatureColourI colour = (FeatureColourI) data[i][COLOUR_COLUMN];
FeatureMatcherSetI theFilter = (FeatureMatcherSetI) data[i][FILTER_COLUMN];
Boolean isShown = (Boolean) data[i][SHOW_COLUMN];
rowData[i] = new FeatureSettingsBean(type, colour, theFilter,
isShown);
}
return rowData;
}
private void jbInit() throws Exception
{
this.setLayout(new BorderLayout());
JPanel settingsPane = new JPanel();
settingsPane.setLayout(new BorderLayout());
dasSettingsPane.setLayout(new BorderLayout());
JPanel bigPanel = new JPanel();
bigPanel.setLayout(new BorderLayout());
groupPanel = new JPanel();
bigPanel.add(groupPanel, BorderLayout.NORTH);
JButton invert = new JButton(
MessageManager.getString("label.invert_selection"));
invert.setFont(JvSwingUtils.getLabelFont());
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.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.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.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.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
Help.showHelpWindow(HelpId.SequenceFeatureSettings);
} catch (HelpSetException e1)
{
e1.printStackTrace();
}
}
});
help.setFont(JvSwingUtils.getLabelFont());
help.setText(MessageManager.getString("action.help"));
help.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
Help.showHelpWindow(HelpId.SequenceFeatureSettings);
} catch (HelpSetException e1)
{
e1.printStackTrace();
}
}
});
JButton cancel = new JButton(MessageManager.getString("action.cancel"));
cancel.setFont(JvSwingUtils.getLabelFont());
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.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
close();
}
});
JButton loadColours = new JButton(
MessageManager.getString("label.load_colours"));
loadColours.setFont(JvSwingUtils.getLabelFont());
loadColours.setToolTipText(
MessageManager.getString("label.load_colours_tooltip"));
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.setToolTipText(
MessageManager.getString("label.save_colours_tooltip"));
saveColours.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
save();
}
});
transparency.addChangeListener(new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent evt)
{
if (!inConstruction)
{
fr.setTransparency((100 - transparency.getValue()) / 100f);
af.alignPanel.paintAlignment(true, true);
}
}
});
transparency.setMaximum(70);
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);
}
});
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);
}
});
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);
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, BorderLayout.CENTER);
dasSettingsPane.add(dasButtonPanel, BorderLayout.SOUTH);
dasButtonPanel.add(fetchDAS);
dasButtonPanel.add(cancelDAS);
dasButtonPanel.add(saveDAS);
settingsPane.add(bigPanel, BorderLayout.CENTER);
settingsPane.add(buttonPanel, BorderLayout.SOUTH);
this.add(settingsPane);
}
public void fetchDAS_actionPerformed(ActionEvent e)
{
fetchDAS.setEnabled(false);
cancelDAS.setEnabled(true);
dassourceBrowser.setGuiEnabled(false);
Vector selectedSources = dassourceBrowser
.getSelectedSources();
doDasFeatureFetch(selectedSources, true, true);
}
/**
* get the features from selectedSources for all or the current selection
*
* @param selectedSources
* @param checkDbRefs
* @param promptFetchDbRefs
*/
private void doDasFeatureFetch(List selectedSources,
boolean checkDbRefs, boolean promptFetchDbRefs)
{
SequenceI[] dataset, seqs;
int iSize;
AlignmentViewport vp = af.getViewport();
if (vp.getSelectionGroup() != null
&& vp.getSelectionGroup().getSize() > 0)
{
iSize = vp.getSelectionGroup().getSize();
dataset = new SequenceI[iSize];
seqs = vp.getSelectionGroup().getSequencesInOrder(vp.getAlignment());
}
else
{
iSize = vp.getAlignment().getHeight();
seqs = vp.getAlignment().getSequencesArray();
}
dataset = new SequenceI[iSize];
for (int i = 0; i < iSize; i++)
{
dataset[i] = seqs[i].getDatasetSequence();
}
cancelDAS.setEnabled(true);
dasFeatureFetcher = new jalview.ws.DasSequenceFeatureFetcher(dataset,
this, selectedSources, checkDbRefs, promptFetchDbRefs);
af.getViewport().setShowSequenceFeatures(true);
af.showSeqFeatures.setSelected(true);
}
/**
* blocking call to initialise the das source browser
*/
public void initDasSources()
{
dassourceBrowser.initDasSources();
}
/**
* examine the current list of das sources and return any matching the given
* nicknames in sources
*
* @param sources
* Vector of Strings to resolve to DAS source nicknames.
* @return sources that are present in source list.
*/
public List resolveSourceNicknames(Vector sources)
{
return dassourceBrowser.sourceRegistry.resolveSourceNicknames(sources);
}
/**
* get currently selected das sources. ensure you have called initDasSources
* before calling this.
*
* @return vector of selected das source nicknames
*/
public Vector getSelectedSources()
{
return dassourceBrowser.getSelectedSources();
}
/**
* properly initialise DAS fetcher and then initiate a new thread to fetch
* features from the named sources (rather than any turned on by default)
*
* @param sources
* @param block
* if true then runs in same thread, otherwise passes to the Swing
* executor
*/
public void fetchDasFeatures(Vector sources, boolean block)
{
initDasSources();
List resolved = dassourceBrowser.sourceRegistry
.resolveSourceNicknames(sources);
if (resolved.size() == 0)
{
resolved = dassourceBrowser.getSelectedSources();
}
if (resolved.size() > 0)
{
final List dassources = resolved;
fetchDAS.setEnabled(false);
// cancelDAS.setEnabled(true); doDasFetch does this.
Runnable fetcher = new Runnable()
{
@Override
public void run()
{
doDasFeatureFetch(dassources, true, false);
}
};
if (block)
{
fetcher.run();
}
else
{
SwingUtilities.invokeLater(fetcher);
}
}
}
public void saveDAS_actionPerformed(ActionEvent e)
{
dassourceBrowser
.saveProperties(jalview.bin.Cache.applicationProperties);
}
public void complete()
{
fetchDAS.setEnabled(true);
cancelDAS.setEnabled(false);
dassourceBrowser.setGuiEnabled(true);
}
public void cancelDAS_actionPerformed(ActionEvent e)
{
if (dasFeatureFetcher != null)
{
dasFeatureFetcher.cancel();
}
complete();
}
public void noDasSourceActive()
{
complete();
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);
}
// ///////////////////////////////////////////////////////////////////////
// http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
// ///////////////////////////////////////////////////////////////////////
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;
}
public Object[][] getData()
{
return data;
}
public void setData(Object[][] data)
{
this.data = data;
}
@Override
public int getColumnCount()
{
return columnNames.length;
}
public Object[] getRow(int row)
{
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];
}
/**
* Answers the class of the object in column c of the first row of the table
*/
@Override
public Class> getColumnClass(int c)
{
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;
fireTableCellUpdated(row, col);
updateFeatureRenderer(data);
}
}
class ColorRenderer extends JLabel implements TableCellRenderer
{
javax.swing.border.Border unselectedBorder = null;
javax.swing.border.Border selectedBorder = null;
final String baseTT = "Click to edit, right/apple click for menu.";
public ColorRenderer()
{
setOpaque(true); // MUST do this for background to show up.
setHorizontalTextPosition(SwingConstants.CENTER);
setVerticalTextPosition(SwingConstants.CENTER);
}
@Override
public Component getTableCellRendererComponent(JTable tbl, Object color,
boolean isSelected, boolean hasFocus, int row, int column)
{
FeatureColourI cellColour = (FeatureColourI) color;
setOpaque(true);
setToolTipText(baseTT);
setBackground(tbl.getBackground());
if (!cellColour.isSimpleColour())
{
Rectangle cr = tbl.getCellRect(row, column, false);
FeatureSettings.renderGraduatedColor(this, cellColour,
(int) cr.getWidth(), (int) cr.getHeight());
}
else
{
this.setText("");
this.setIcon(null);
setBackground(cellColour.getColour());
}
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());
}
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)
{
FeatureMatcherSetI theFilter = (FeatureMatcherSetI) 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());
}
setBorder(unselectedBorder);
}
return this;
}
}
/**
* update comp using rendering settings from gcol
*
* @param comp
* @param gcol
*/
public static void renderGraduatedColor(JLabel comp, FeatureColourI gcol)
{
int w = comp.getWidth(), h = comp.getHeight();
if (w < 20)
{
w = (int) comp.getPreferredSize().getWidth();
h = (int) comp.getPreferredSize().getHeight();
if (w < 20)
{
w = 80;
h = 12;
}
}
renderGraduatedColor(comp, gcol, w, h);
}
public static void renderGraduatedColor(JLabel comp, FeatureColourI gcol,
int w, int h)
{
boolean thr = false;
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.append(">");
tt.append("Thresholded (Above ").append(gcol.getThreshold())
.append(") ");
}
if (gcol.isBelowThreshold())
{
thr = true;
tx.append("<");
tt.append("Thresholded (Below ").append(gcol.getThreshold())
.append(") ");
}
if (gcol.isColourByLabel())
{
tt.append("Coloured by label text. ").append(tt);
if (thr)
{
tx.append(" ");
}
if (!gcol.isColourByAttribute())
{
tx.append("Label");
}
comp.setIcon(null);
}
else
{
Color newColor = gcol.getMaxColour();
comp.setBackground(newColor);
// System.err.println("Width is " + w / 2);
Icon ficon = new FeatureIcon(gcol, comp.getBackground(), w, h, thr);
comp.setIcon(ficon);
// tt+="RGB value: Max (" + newColor.getRed() + ", "
// + newColor.getGreen() + ", " + newColor.getBlue()
// + ")\nMin (" + minCol.getRed() + ", " + minCol.getGreen()
// + ", " + minCol.getBlue() + ")");
}
comp.setHorizontalAlignment(SwingConstants.CENTER);
comp.setText(tx.toString());
if (tt.length() > 0)
{
if (comp.getToolTipText() == null)
{
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);
FeatureMatcherSetI currentFilter = me.fr.getFeatureFilter(type);
if (currentFilter == null)
{
currentFilter = new FeatureMatcherSet();
}
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
{
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;
FeatureMatcherSetI 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 FeatureMatcherSet();
}
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 = (FeatureMatcherSetI) 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
{
FeatureColourI gcol;
Color backg;
boolean midspace = false;
int width = 50, height = 20;
int s1, e1; // start and end of midpoint band for thresholded symbol
Color mpcolour = Color.white;
FeatureIcon(FeatureColourI gfc, Color bg, int w, int h, boolean mspace)
{
gcol = gfc;
backg = bg;
width = w;
height = h;
midspace = mspace;
if (midspace)
{
s1 = width / 3;
e1 = s1 * 2;
}
else
{
s1 = width / 2;
e1 = s1;
}
}
@Override
public int getIconWidth()
{
return width;
}
@Override
public int getIconHeight()
{
return height;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
if (gcol.isColourByLabel())
{
g.setColor(backg);
g.fillRect(0, 0, width, height);
// need an icon here.
g.setColor(gcol.getMaxColour());
g.setFont(new Font("Verdana", Font.PLAIN, 9));
// g.setFont(g.getFont().deriveFont(
// AffineTransform.getScaleInstance(
// width/g.getFontMetrics().stringWidth("Label"),
// height/g.getFontMetrics().getHeight())));
g.drawString(MessageManager.getString("label.label"), 0, 0);
}
else
{
Color minCol = gcol.getMinColour();
g.setColor(minCol);
g.fillRect(0, 0, s1, height);
if (midspace)
{
g.setColor(Color.white);
g.fillRect(s1, 0, e1 - s1, height);
}
g.setColor(gcol.getMaxColour());
g.fillRect(0, e1, width - e1, height);
}
}
}