Skip to content

Commit cf058c3

Browse files
cmaglieFederico Fissore
authored and
Federico Fissore
committed
Categories and filter search on installer
1 parent 353a359 commit cf058c3

File tree

5 files changed

+91
-39
lines changed

5 files changed

+91
-39
lines changed

app/src/cc/arduino/packages/contributions/ui/ContributionIndexTableModel.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,48 @@ public void select(ContributedPlatform value) {
117117

118118
private Class<?>[] columnTypes = { ContributedPlatform.class };
119119

120-
public void updateIndex(ContributionsIndex index) {
120+
private ContributionsIndex index;
121+
122+
public void setIndex(ContributionsIndex _index) {
123+
index = _index;
124+
updateIndexFilter(null, null);
125+
}
126+
127+
public void updateIndexFilter(String category, String filters[]) {
121128
contributions.clear();
122129
for (ContributedPackage pack : index.getPackages()) {
123130
for (ContributedPlatform platform : pack.getPlatforms()) {
131+
if (category != null) {
132+
if (!platform.getCategory().equals(category))
133+
continue;
134+
}
135+
if (!stringContainsAll(platform.getName(), filters))
136+
continue;
124137
addContribution(platform);
125138
}
126139
}
127140
fireTableDataChanged();
128141
}
129142

143+
/**
144+
* Check if <b>string</b> contains all the substrings in <b>set</b>. The
145+
* compare is case insensitive.
146+
*
147+
* @param string
148+
* @param set
149+
* @return <b>true<b> if all the strings in <b>set</b> are contained in
150+
* <b>string</b>.
151+
*/
152+
private boolean stringContainsAll(String string, String set[]) {
153+
if (set == null)
154+
return true;
155+
for (String s : set) {
156+
if (!string.toLowerCase().contains(s.toLowerCase()))
157+
return false;
158+
}
159+
return true;
160+
}
161+
130162
private void addContribution(ContributedPlatform platform) {
131163
for (ContributedPlatformReleases contribution : contributions) {
132164
if (!contribution.shouldContain(platform))
@@ -194,4 +226,5 @@ public ContributedPlatform getSelectedRelease(int row) {
194226
public void update() {
195227
fireTableDataChanged();
196228
}
229+
197230
}

app/src/cc/arduino/packages/contributions/ui/ContributionManagerUI.java

+20-15
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class ContributionManagerUI extends JDialog {
7373

7474
private ContributionManagerUIListener listener = null;
7575

76-
private String category;
7776
private JLabel categoryLabel;
7877
private JComboBox categoryChooser;
7978
private Component categoryStrut1;
@@ -89,6 +88,10 @@ public class ContributionManagerUI extends JDialog {
8988

9089
private ContributedPlatformTableCell cellEditor;
9190

91+
// Currently selected category and filters
92+
private String category;
93+
private String[] filters;
94+
9295
public ContributionManagerUI(Frame parent) {
9396
super(parent, "Boards Manager", Dialog.ModalityType.APPLICATION_MODAL);
9497
setResizable(true);
@@ -108,13 +111,24 @@ public ContributionManagerUI(Frame parent) {
108111
categoryChooser.addActionListener(new ActionListener() {
109112
@Override
110113
public void actionPerformed(ActionEvent arg0) {
111-
notifyCategoryChange();
114+
String selected = (String) categoryChooser.getSelectedItem();
115+
if (category == null || !category.equals(selected)) {
116+
category = selected;
117+
cellEditor.stopCellEditing();
118+
contribModel.updateIndexFilter(category, filters);
119+
}
112120
}
113121
});
114122

115123
setCategories(new ArrayList<String>());
116124

117-
filterField = new FilterJTextField(_("Filter your search..."));
125+
filterField = new FilterJTextField(_("Filter your search...")) {
126+
@Override
127+
protected void onFilter(String[] _filters) {
128+
filters = _filters;
129+
contribModel.updateIndexFilter(category, filters);
130+
}
131+
};
118132

119133
JPanel panel = new JPanel();
120134
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
@@ -266,18 +280,9 @@ public void setCategories(Collection<String> categories) {
266280
categoryStrut3.setVisible(show);
267281
}
268282

269-
private synchronized void notifyCategoryChange() {
270-
if (listener == null)
271-
return;
272-
String selected = (String) categoryChooser.getSelectedItem();
273-
if (category == null || !category.equals(selected)) {
274-
category = selected;
275-
listener.onCategoryChange(category);
276-
}
277-
}
278-
279-
public void addContributions(ContributionsIndex index) {
280-
contribModel.updateIndex(index);
283+
public void setContributions(ContributionsIndex index) {
284+
contribModel.setIndex(index);
285+
setCategories(index.getCategories());
281286
}
282287

283288
public void setProgressVisible(boolean visible) {

app/src/cc/arduino/packages/contributions/ui/ContributionManagerUIListener.java

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
public interface ContributionManagerUIListener {
3434

35-
void onCategoryChange(String category);
36-
3735
void onInstall(ContributedPlatform selected);
3836

3937
void onRemove(ContributedPlatform selected);

app/src/cc/arduino/packages/contributions/ui/FilterJTextField.java

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2014 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
129
package cc.arduino.packages.contributions.ui;
230

331
import java.awt.Color;
432
import java.awt.Font;
533
import java.awt.event.FocusEvent;
634
import java.awt.event.FocusListener;
7-
import java.util.ArrayList;
8-
import java.util.Arrays;
9-
import java.util.List;
1035

1136
import javax.swing.JTextField;
1237
import javax.swing.UIManager;
@@ -16,15 +41,14 @@
1641
@SuppressWarnings("serial")
1742
public class FilterJTextField extends JTextField {
1843
private String filterHint;
19-
boolean showingHint;
20-
List<String> filters;
44+
45+
private boolean showingHint;
2146

2247
public FilterJTextField(String hint) {
2348
super(hint);
2449
filterHint = hint;
2550

2651
showingHint = true;
27-
filters = new ArrayList<String>();
2852
updateStyle();
2953

3054
addFocusListener(new FocusListener() {
@@ -59,18 +83,17 @@ public void changedUpdate(DocumentEvent e) {
5983
});
6084
}
6185

62-
public void applyFilter() {
63-
String filter = getFilterText();
86+
private void applyFilter() {
87+
String filter = showingHint ? "" : getText();
6488
filter = filter.toLowerCase();
6589

6690
// Replace anything but 0-9, a-z, or : with a space
6791
filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " ");
68-
filters = Arrays.asList(filter.split(" "));
69-
// filterLibraries(category, filters);
92+
onFilter(filter.split(" "));
7093
}
7194

72-
public String getFilterText() {
73-
return showingHint ? "" : getText();
95+
protected void onFilter(String[] strings) {
96+
// Empty
7497
}
7598

7699
public void updateStyle() {

app/src/processing/app/Base.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,7 @@ public void onProgress(double progress, String message) {
11221122
}
11231123
};
11241124
managerUI.setListener(new ContributionManagerUIListener() {
1125-
@Override
1126-
public void onCategoryChange(String category) {
1127-
// TODO Auto-generated method stub
1128-
System.out.println("Selected " + category);
1129-
}
1130-
1125+
11311126
@Override
11321127
public void onUpdatePressed() {
11331128
// TODO Auto-generated method stub
@@ -1185,9 +1180,7 @@ public void run() {
11851180
task.start();
11861181
}
11871182
});
1188-
managerUI.setCategories(Arrays.asList("Arduino", "Arduino Certified",
1189-
"Arduino@Heart"));
1190-
managerUI.addContributions(BaseNoGui.indexer.getIndex());
1183+
managerUI.setContributions(BaseNoGui.indexer.getIndex());
11911184
managerUI.setVisible(true);
11921185
}
11931186

0 commit comments

Comments
 (0)