- JPanel filterRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
- filterRow.setBackground(Color.white);
-
- /*
- * drop-down choice of attribute, with description as a tooltip
- * if we can obtain it
- */
- String featureType = (String) filteredFeatureChoice.getSelectedItem();
- final JComboBox<String> attCombo = populateAttributesDropdown(
- featureType, attNames);
- JComboBox<Condition> condCombo = new JComboBox<>();
- JTextField patternField = new JTextField(8);
-
- /*
- * action handlers that validate and (if valid) apply changes
- */
- ActionListener actionListener = new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if (attCombo.getSelectedItem() != null)
- {
- if (validateFilter(patternField, condCombo))
- {
- updateFilter(attCombo, condCombo, patternField, filterIndex);
- filtersChanged();
- }
- }
- }
- };
- ItemListener itemListener = new ItemListener()
- {
- @Override
- public void itemStateChanged(ItemEvent e)
- {
- actionListener.actionPerformed(null);
- }
- };
-
- if ("".equals(attribute))
- {
- attCombo.setSelectedItem(null);
- }
- else
- {
- attCombo.setSelectedItem(attribute);
- }
- attCombo.addItemListener(itemListener);
-
- filterRow.add(attCombo);
-
- /*
- * drop-down choice of test condition
- */
- for (Condition c : Condition.values())
- {
- condCombo.addItem(c);
- }
- if (cond != null)
- {
- condCombo.setSelectedItem(cond);
- }
- condCombo.addItemListener(itemListener);
- filterRow.add(condCombo);
-
- /*
- * pattern to match against
- */
- patternField.setText(pattern);
- patternField.addActionListener(actionListener);
- patternField.addFocusListener(new FocusAdapter()
- {
- @Override
- public void focusLost(FocusEvent e)
- {
- actionListener.actionPerformed(null);
- }
- });
- filterRow.add(patternField);
-
- /*
- * add remove button if filter is populated (non-empty pattern)
- */
- if (pattern != null && pattern.trim().length() > 0)
- {
- // todo: gif for button drawing '-' or 'x'
- JButton removeCondition = new BasicArrowButton(SwingConstants.WEST);
- removeCondition.setToolTipText(MessageManager
- .getString("label.delete_row"));
- removeCondition.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- filters.remove(filterIndex);
- filtersChanged();
- }
- });
- filterRow.add(removeCondition);
- }
-
- return filterRow;
- }
-
- /**
- * A helper method to build the drop-down choice of attributes for a feature.
- * Where metadata is available with a description for an attribute, that is
- * added as a tooltip.
- *
- * @param featureType
- * @param attNames
- */
- protected JComboBox<String> populateAttributesDropdown(
- String featureType, List<String> attNames)
- {
- List<String> tooltips = new ArrayList<>();
- FeatureAttributes fa = FeatureAttributes.getInstance();
- for (String attName : attNames)
- {
- String desc = fa.getDescription(featureType, attName);
- if (desc != null && desc.length() > MAX_TOOLTIP_LENGTH)
- {
- desc = desc.substring(0, MAX_TOOLTIP_LENGTH) + "...";
- }
- tooltips.add(desc == null ? "" : desc);
- }
-
- JComboBox<String> attCombo = JvSwingUtils.buildComboWithTooltips(
- attNames, tooltips);
- if (attNames.isEmpty())
- {
- attCombo.setToolTipText(MessageManager
- .getString("label.no_attributes"));
- }
- return attCombo;
- }
-
- /**
- * Action on any change to feature filtering, namely
- * <ul>
- * <li>change of selected attribute</li>
- * <li>change of selected condition</li>
- * <li>change of match pattern</li>
- * <li>removal of a condition</li>
- * </ul>
- * The action should be to
- * <ul>
- * <li>parse and validate the filters</li>
- * <li>if valid, update the filter text box</li>
- * <li>and apply the filters to the viewport</li>
- * </ul>
- */
- protected void filtersChanged()
- {
- /*
- * update the filter conditions for the feature type
- */
- String featureType = (String) filteredFeatureChoice.getSelectedItem();
- boolean anded = andFilters.isSelected();
- KeyedMatcherSetI combined = new KeyedMatcherSet();
-
- for (KeyedMatcherI filter : filters)
- {
- String pattern = filter.getMatcher().getPattern();
- if (pattern.trim().length() > 0)
- {
- if (anded)
- {
- combined.and(filter);
- }
- else
- {
- combined.or(filter);
- }
- }
- }
-
- /*
- * save the filter conditions in the FeatureRenderer
- * (note this might now be an empty filter with no conditions)
- */
- fr.setFeatureFilter(featureType, combined);
-
- filtersAsText.setText(combined.toString());
-
- refreshFiltersDisplay();
-
- af.alignPanel.paintAlignment(true, true);
- }
-
- /**
- * Constructs a filter condition from the given input fields, and replaces the
- * condition at filterIndex with the new one
- *
- * @param attCombo
- * @param condCombo
- * @param valueField
- * @param filterIndex
- */
- protected void updateFilter(JComboBox<String> attCombo,
- JComboBox<Condition> condCombo, JTextField valueField,
- int filterIndex)
- {
- String attName = (String) attCombo.getSelectedItem();
- Condition cond = (Condition) condCombo.getSelectedItem();
- String pattern = valueField.getText();
- KeyedMatcherI km = new KeyedMatcher(attName, cond, pattern);
-
- filters.set(filterIndex, km);
- }
-
- public void fetchDAS_actionPerformed(ActionEvent e)
- {
- fetchDAS.setEnabled(false);
- cancelDAS.setEnabled(true);
- dassourceBrowser.setGuiEnabled(false);
- Vector<jalviewSourceI> 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<jalviewSourceI> 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<jalviewSourceI> resolveSourceNicknames(Vector<String> 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<jalviewSourceI> getSelectedSources()
- {
- return dassourceBrowser.getSelectedSources();
- }