35
35
import java .awt .Frame ;
36
36
import java .awt .event .ActionEvent ;
37
37
import java .awt .event .ActionListener ;
38
+ import java .util .ArrayList ;
38
39
import java .util .Collection ;
39
40
import java .util .Collections ;
40
41
import java .util .LinkedList ;
41
42
import java .util .List ;
42
43
import java .util .Optional ;
43
- import java .util .function .Predicate ;
44
44
45
45
import javax .swing .Box ;
46
46
import javax .swing .JComboBox ;
@@ -66,13 +66,16 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibraryRelease
66
66
67
67
private final JComboBox typeChooser ;
68
68
private final LibraryInstaller installer ;
69
- private Predicate <ContributedLibraryReleases > typeFilter ;
70
69
71
70
@ Override
72
71
protected FilteredAbstractTableModel createContribModel () {
73
72
return new LibrariesIndexTableModel ();
74
73
}
75
74
75
+ private LibrariesIndexTableModel getContribModel () {
76
+ return (LibrariesIndexTableModel ) contribModel ;
77
+ }
78
+
76
79
@ Override
77
80
protected TableCellRenderer createCellRenderer () {
78
81
return new ContributedLibraryTableCellRenderer ();
@@ -115,63 +118,60 @@ public LibraryManagerUI(Frame parent, LibraryInstaller installer) {
115
118
}
116
119
117
120
protected final ActionListener typeChooserActionListener = new ActionListener () {
118
-
119
121
@ Override
120
122
public void actionPerformed (ActionEvent event ) {
121
123
DropdownItem <ContributedLibraryReleases > selected = (DropdownItem <ContributedLibraryReleases >) typeChooser .getSelectedItem ();
122
124
previousRowAtPoint = -1 ;
123
- if (selected != null && typeFilter != selected .getFilterPredicate ()) {
124
- typeFilter = selected .getFilterPredicate ();
125
+ if (selected != null && extraFilter != selected .getFilterPredicate ()) {
126
+ extraFilter = selected .getFilterPredicate ();
125
127
if (contribTable .getCellEditor () != null ) {
126
128
contribTable .getCellEditor ().stopCellEditing ();
127
129
}
128
- updateIndexFilter (filters , categoryFilter .and (typeFilter ));
130
+ updateIndexFilter (filters , categoryFilter .and (extraFilter ));
129
131
}
130
132
}
131
133
};
132
134
135
+ private Collection <String > oldCategories = new ArrayList <>();
136
+ private Collection <String > oldTypes = new ArrayList <>();
137
+
133
138
public void updateUI () {
134
- DropdownItem <ContributedLibraryReleases > previouslySelectedCategory = (DropdownItem <ContributedLibraryReleases >) categoryChooser .getSelectedItem ();
135
- DropdownItem <ContributedLibraryReleases > previouslySelectedType = (DropdownItem <ContributedLibraryReleases >) typeChooser .getSelectedItem ();
139
+ // Check if categories or types have changed
140
+ Collection <String > categories = BaseNoGui .librariesIndexer .getIndex ().getCategories ();
141
+ List <String > types = new LinkedList <>(BaseNoGui .librariesIndexer .getIndex ().getTypes ());
142
+ Collections .sort (types , new LibraryTypeComparator ());
136
143
137
- categoryChooser .removeActionListener (categoryChooserActionListener );
138
- typeChooser .removeActionListener (typeChooserActionListener );
144
+ if (categories .equals (oldCategories ) && types .equals (oldTypes )) {
145
+ return ;
146
+ }
147
+ oldCategories = categories ;
148
+ oldTypes = types ;
139
149
140
150
// Load categories
141
151
categoryFilter = x -> true ;
152
+ categoryChooser .removeActionListener (categoryChooserActionListener );
142
153
categoryChooser .removeAllItems ();
143
154
categoryChooser .addItem (new DropdownAllLibraries ());
144
- Collection <String > categories = BaseNoGui .librariesIndexer .getIndex ().getCategories ();
145
155
for (String category : categories ) {
146
156
categoryChooser .addItem (new DropdownLibraryOfCategoryItem (category ));
147
157
}
148
-
149
158
categoryChooser .setEnabled (categoryChooser .getItemCount () > 1 );
150
-
151
159
categoryChooser .addActionListener (categoryChooserActionListener );
152
- if (previouslySelectedCategory != null ) {
153
- categoryChooser .setSelectedItem (previouslySelectedCategory );
154
- } else {
155
- categoryChooser .setSelectedIndex (0 );
156
- }
160
+ categoryChooser .setSelectedIndex (0 );
157
161
158
- typeFilter = x -> true ;
162
+ // Load types
163
+ extraFilter = x -> true ;
164
+ typeChooser .removeActionListener (typeChooserActionListener );
159
165
typeChooser .removeAllItems ();
160
166
typeChooser .addItem (new DropdownAllLibraries ());
161
167
typeChooser .addItem (new DropdownUpdatableLibrariesItem ());
162
168
typeChooser .addItem (new DropdownInstalledLibraryItem ());
163
- List <String > types = new LinkedList <>(BaseNoGui .librariesIndexer .getIndex ().getTypes ());
164
- Collections .sort (types , new LibraryTypeComparator ());
165
169
for (String type : types ) {
166
170
typeChooser .addItem (new DropdownLibraryOfTypeItem (type ));
167
171
}
168
172
typeChooser .setEnabled (typeChooser .getItemCount () > 1 );
169
173
typeChooser .addActionListener (typeChooserActionListener );
170
- if (previouslySelectedType != null ) {
171
- typeChooser .setSelectedItem (previouslySelectedType );
172
- } else {
173
- typeChooser .setSelectedIndex (0 );
174
- }
174
+ typeChooser .setSelectedIndex (0 );
175
175
176
176
filterField .setEnabled (contribModel .getRowCount () > 0 );
177
177
}
@@ -201,8 +201,11 @@ protected void onUpdatePressed() {
201
201
try {
202
202
setProgressVisible (true , "" );
203
203
installer .updateIndex (this ::setProgress );
204
- ((LibrariesIndexTableModel ) contribModel ).update ();
205
204
onIndexesUpdated ();
205
+ if (contribTable .getCellEditor () != null ) {
206
+ contribTable .getCellEditor ().stopCellEditing ();
207
+ }
208
+ getContribModel ().update ();
206
209
} catch (Exception e ) {
207
210
throw new RuntimeException (e );
208
211
} finally {
@@ -238,12 +241,11 @@ public void onInstallPressed(final ContributedLibrary lib) {
238
241
} else {
239
242
installer .install (lib , this ::setProgress );
240
243
}
241
- // TODO: Do a better job in refreshing only the needed element
244
+ onIndexesUpdated ();
242
245
if (contribTable .getCellEditor () != null ) {
243
246
contribTable .getCellEditor ().stopCellEditing ();
244
247
}
245
- ((LibrariesIndexTableModel ) contribModel ).update ();
246
- onIndexesUpdated ();
248
+ getContribModel ().update ();
247
249
} catch (Exception e ) {
248
250
throw new RuntimeException (e );
249
251
} finally {
@@ -270,12 +272,11 @@ public void onRemovePressed(final ContributedLibrary lib) {
270
272
try {
271
273
setProgressVisible (true , tr ("Removing..." ));
272
274
installer .remove (lib , this ::setProgress );
273
- // TODO: Do a better job in refreshing only the needed element
275
+ onIndexesUpdated ();
274
276
if (contribTable .getCellEditor () != null ) {
275
277
contribTable .getCellEditor ().stopCellEditing ();
276
278
}
277
- ((LibrariesIndexTableModel ) contribModel ).update ();
278
- onIndexesUpdated ();
279
+ getContribModel ().update ();
279
280
} catch (Exception e ) {
280
281
throw new RuntimeException (e );
281
282
} finally {
0 commit comments