From cf058c37ba40529afba3a6ee45b5122c0152d6e5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 23 Oct 2014 12:34:45 +0200 Subject: [PATCH] Categories and filter search on installer --- .../ui/ContributionIndexTableModel.java | 35 +++++++++++++- .../ui/ContributionManagerUI.java | 35 ++++++++------ .../ui/ContributionManagerUIListener.java | 2 - .../contributions/ui/FilterJTextField.java | 47 ++++++++++++++----- app/src/processing/app/Base.java | 11 +---- 5 files changed, 91 insertions(+), 39 deletions(-) diff --git a/app/src/cc/arduino/packages/contributions/ui/ContributionIndexTableModel.java b/app/src/cc/arduino/packages/contributions/ui/ContributionIndexTableModel.java index 8ca55606a..7cb9a762a 100644 --- a/app/src/cc/arduino/packages/contributions/ui/ContributionIndexTableModel.java +++ b/app/src/cc/arduino/packages/contributions/ui/ContributionIndexTableModel.java @@ -117,16 +117,48 @@ public class ContributionIndexTableModel extends AbstractTableModel { private Class[] columnTypes = { ContributedPlatform.class }; - public void updateIndex(ContributionsIndex index) { + private ContributionsIndex index; + + public void setIndex(ContributionsIndex _index) { + index = _index; + updateIndexFilter(null, null); + } + + public void updateIndexFilter(String category, String filters[]) { contributions.clear(); for (ContributedPackage pack : index.getPackages()) { for (ContributedPlatform platform : pack.getPlatforms()) { + if (category != null) { + if (!platform.getCategory().equals(category)) + continue; + } + if (!stringContainsAll(platform.getName(), filters)) + continue; addContribution(platform); } } fireTableDataChanged(); } + /** + * Check if string contains all the substrings in set. The + * compare is case insensitive. + * + * @param string + * @param set + * @return true if all the strings in set are contained in + * string. + */ + private boolean stringContainsAll(String string, String set[]) { + if (set == null) + return true; + for (String s : set) { + if (!string.toLowerCase().contains(s.toLowerCase())) + return false; + } + return true; + } + private void addContribution(ContributedPlatform platform) { for (ContributedPlatformReleases contribution : contributions) { if (!contribution.shouldContain(platform)) @@ -194,4 +226,5 @@ public class ContributionIndexTableModel extends AbstractTableModel { public void update() { fireTableDataChanged(); } + } diff --git a/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUI.java b/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUI.java index bdbbfb839..ba70afaac 100644 --- a/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUI.java +++ b/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUI.java @@ -73,7 +73,6 @@ public class ContributionManagerUI extends JDialog { private ContributionManagerUIListener listener = null; - private String category; private JLabel categoryLabel; private JComboBox categoryChooser; private Component categoryStrut1; @@ -89,6 +88,10 @@ public class ContributionManagerUI extends JDialog { private ContributedPlatformTableCell cellEditor; + // Currently selected category and filters + private String category; + private String[] filters; + public ContributionManagerUI(Frame parent) { super(parent, "Boards Manager", Dialog.ModalityType.APPLICATION_MODAL); setResizable(true); @@ -108,13 +111,24 @@ public class ContributionManagerUI extends JDialog { categoryChooser.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { - notifyCategoryChange(); + String selected = (String) categoryChooser.getSelectedItem(); + if (category == null || !category.equals(selected)) { + category = selected; + cellEditor.stopCellEditing(); + contribModel.updateIndexFilter(category, filters); + } } }); setCategories(new ArrayList()); - filterField = new FilterJTextField(_("Filter your search...")); + filterField = new FilterJTextField(_("Filter your search...")) { + @Override + protected void onFilter(String[] _filters) { + filters = _filters; + contribModel.updateIndexFilter(category, filters); + } + }; JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); @@ -266,18 +280,9 @@ public class ContributionManagerUI extends JDialog { categoryStrut3.setVisible(show); } - private synchronized void notifyCategoryChange() { - if (listener == null) - return; - String selected = (String) categoryChooser.getSelectedItem(); - if (category == null || !category.equals(selected)) { - category = selected; - listener.onCategoryChange(category); - } - } - - public void addContributions(ContributionsIndex index) { - contribModel.updateIndex(index); + public void setContributions(ContributionsIndex index) { + contribModel.setIndex(index); + setCategories(index.getCategories()); } public void setProgressVisible(boolean visible) { diff --git a/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUIListener.java b/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUIListener.java index dfc334a92..0ff0e57b7 100644 --- a/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUIListener.java +++ b/app/src/cc/arduino/packages/contributions/ui/ContributionManagerUIListener.java @@ -32,8 +32,6 @@ import cc.arduino.packages.contributions.ContributedPlatform; public interface ContributionManagerUIListener { - void onCategoryChange(String category); - void onInstall(ContributedPlatform selected); void onRemove(ContributedPlatform selected); diff --git a/app/src/cc/arduino/packages/contributions/ui/FilterJTextField.java b/app/src/cc/arduino/packages/contributions/ui/FilterJTextField.java index 0d6f3c11d..02c7a2b98 100644 --- a/app/src/cc/arduino/packages/contributions/ui/FilterJTextField.java +++ b/app/src/cc/arduino/packages/contributions/ui/FilterJTextField.java @@ -1,12 +1,37 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ package cc.arduino.packages.contributions.ui; import java.awt.Color; import java.awt.Font; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import javax.swing.JTextField; import javax.swing.UIManager; @@ -16,15 +41,14 @@ import javax.swing.event.DocumentListener; @SuppressWarnings("serial") public class FilterJTextField extends JTextField { private String filterHint; - boolean showingHint; - List filters; + + private boolean showingHint; public FilterJTextField(String hint) { super(hint); filterHint = hint; showingHint = true; - filters = new ArrayList(); updateStyle(); addFocusListener(new FocusListener() { @@ -59,18 +83,17 @@ public class FilterJTextField extends JTextField { }); } - public void applyFilter() { - String filter = getFilterText(); + private void applyFilter() { + String filter = showingHint ? "" : getText(); filter = filter.toLowerCase(); // Replace anything but 0-9, a-z, or : with a space filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " "); - filters = Arrays.asList(filter.split(" ")); - // filterLibraries(category, filters); + onFilter(filter.split(" ")); } - public String getFilterText() { - return showingHint ? "" : getText(); + protected void onFilter(String[] strings) { + // Empty } public void updateStyle() { diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 566f5727c..735444204 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1122,12 +1122,7 @@ public class Base { } }; managerUI.setListener(new ContributionManagerUIListener() { - @Override - public void onCategoryChange(String category) { - // TODO Auto-generated method stub - System.out.println("Selected " + category); - } - + @Override public void onUpdatePressed() { // TODO Auto-generated method stub @@ -1185,9 +1180,7 @@ public class Base { task.start(); } }); - managerUI.setCategories(Arrays.asList("Arduino", "Arduino Certified", - "Arduino@Heart")); - managerUI.addContributions(BaseNoGui.indexer.getIndex()); + managerUI.setContributions(BaseNoGui.indexer.getIndex()); managerUI.setVisible(true); }