Categories and filter search on installer

This commit is contained in:
Cristian Maglie 2014-10-23 12:34:45 +02:00 committed by Federico Fissore
parent 353a35942c
commit cf058c37ba
5 changed files with 91 additions and 39 deletions

View File

@ -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 <b>string</b> contains all the substrings in <b>set</b>. The
* compare is case insensitive.
*
* @param string
* @param set
* @return <b>true<b> if all the strings in <b>set</b> are contained in
* <b>string</b>.
*/
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();
}
}

View File

@ -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<String>());
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) {

View File

@ -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);

View File

@ -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<String> filters;
private boolean showingHint;
public FilterJTextField(String hint) {
super(hint);
filterHint = hint;
showingHint = true;
filters = new ArrayList<String>();
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() {

View File

@ -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);
}