1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+ /*
4
+ Part of the Processing project - http://processing.org
5
+
6
+ Copyright (c) 2017 The Processing Foundation
7
+
8
+ This program is free software; you can redistribute it and/or modify
9
+ it under the terms of the GNU General Public License version 2
10
+ as published by the Free Software Foundation.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program; if not, write to the Free Software Foundation,
19
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ */
21
+
1
22
package processing .mode .android ;
2
23
3
24
import com .android .repository .api .*;
14
35
import javax .swing .*;
15
36
import javax .swing .border .EmptyBorder ;
16
37
import javax .swing .table .DefaultTableModel ;
38
+
17
39
import java .awt .*;
18
40
import java .awt .event .ActionEvent ;
19
41
import java .awt .event .ActionListener ;
32
54
33
55
@ SuppressWarnings ("serial" )
34
56
public class SDKUpdater extends JFrame implements PropertyChangeListener {
57
+ final static private int DEF_NUM_ROWS = 10 ;
58
+ final static private int DEF_COL_WIDTH = 200 ;
35
59
private final Vector <String > columns = new Vector <>(Arrays .asList (
36
60
"Package name" , "Installed version" , "New version" ));
37
- // private final Class[] columnClassI = new Class[]{
38
- // String.class, String.class, String.class
39
- // };
40
-
41
- // private final Vector<String> columnsUpdates = new Vector<>(Arrays.asList("ID", "Installed", "Available"));
42
- // private final Class[] columnClassU = new Class[]{
43
- // String.class, String.class, String.class
44
- // };
45
-
46
61
private static final String PROPERTY_CHANGE_QUERY = "query" ;
47
62
48
63
private AndroidSDK sdk ;
49
- private Vector <Vector <String >> installedList ;
50
- // private Vector<Vector<String>> updatesList;
51
64
52
65
private QueryTask queryTask ;
53
66
private DownloadTask downloadTask ;
54
67
private boolean downloadTaskRunning ;
55
68
56
- // private Vector<Vector<String>> packageInfo;
57
- // private DefaultTableModel packageTable;
58
-
59
- private DefaultTableModel modelInstalled ;
69
+ private Vector <Vector <String >> packageList ;
70
+ private DefaultTableModel packageTable ;
60
71
private int numUpdates ;
61
- // private DefaultTableModel modelUpdates;
62
72
63
73
private JProgressBar progressBar ;
64
74
private JLabel status ;
65
75
private JButton actionButton ;
76
+ private JTable table ;
66
77
67
78
public SDKUpdater (Editor editor , AndroidMode androidMode ) {
68
79
super ("SDK Updater" );
@@ -113,7 +124,7 @@ class QueryTask extends SwingWorker<Object, Object> {
113
124
@ Override
114
125
protected Object doInBackground () throws Exception {
115
126
numUpdates = 0 ;
116
- installedList = new Vector <>();
127
+ packageList = new Vector <>();
117
128
118
129
/* Following code is from listPackages() of com.android.sdklib.tool.SdkManagerCli
119
130
with some changes
@@ -140,11 +151,12 @@ public Channel getChannel() {
140
151
}), null );
141
152
142
153
RepositoryPackages packages = mRepoManager .getPackages ();
143
- HashMap <String , String []> local = new HashMap ();
144
- for (LocalPackage pck : packages .getLocalPackages ().values ()) {
145
- String path = pck .getPath ();
146
- String name = pck .getDisplayName ();
147
- String ver = pck .getVersion ().toString ();
154
+ HashMap <String , List <String >> installed = new HashMap <String , List <String >>();
155
+ for (LocalPackage local : packages .getLocalPackages ().values ()) {
156
+ String path = local .getPath ();
157
+ String name = local .getDisplayName ();
158
+ String ver = local .getVersion ().toString ();
159
+ // Remove version from the display name
148
160
int rev = name .indexOf (", rev" );
149
161
if (-1 < rev ) {
150
162
name = name .substring (0 , rev );
@@ -157,74 +169,32 @@ public Channel getChannel() {
157
169
name = name .substring (0 , pos ).trim ();
158
170
}
159
171
}
160
- local .put (path , new String []{ name , ver } );
172
+ installed .put (path , Arrays . asList ( name , ver ) );
161
173
}
162
174
163
- HashMap <String , String []> update = new HashMap ();
164
- for (UpdatablePackage pck : packages .getUpdatedPkgs ()) {
165
- String path = pck .getPath ();
166
- String inst = pck .getLocal ().getVersion ().toString ();
167
- String updt = pck .getRemote ().getVersion ().toString ();
168
- update .put (path , new String []{ inst , updt } );
175
+ HashMap <String , List < String >> updated = new HashMap < String , List < String >> ();
176
+ for (UpdatablePackage update : packages .getUpdatedPkgs ()) {
177
+ String path = update .getPath ();
178
+ String loc = update .getLocal ().getVersion ().toString ();
179
+ String rem = update .getRemote ().getVersion ().toString ();
180
+ updated .put (path , Arrays . asList ( loc , rem ) );
169
181
}
170
182
171
- for (String path : local .keySet ()) {
183
+ for (String path : installed .keySet ()) {
172
184
Vector <String > info = new Vector <>();
173
- String [] locInfo = local .get (path );
174
- info .add (locInfo [ 0 ] );
175
- info .add (locInfo [ 1 ] );
176
- if (update .containsKey (path )) {
177
- String [] updInfo = update .get (path );
178
- info .add (updInfo [ 1 ] );
185
+ List < String > locInfo = installed .get (path );
186
+ info .add (locInfo . get ( 0 ) );
187
+ info .add (locInfo . get ( 1 ) );
188
+ if (updated .containsKey (path )) {
189
+ String upVer = updated .get (path ). get ( 1 );
190
+ info .add (upVer );
179
191
numUpdates ++;
180
192
} else {
181
193
info .add ("" );
182
194
}
183
- installedList .add (info );
195
+ packageList .add (info );
184
196
}
185
197
186
-
187
- /*
188
- for (LocalPackage local : packages.getLocalPackages().values()) {
189
- Vector<String> localPackages = new Vector<>();
190
- String name = local.getDisplayName();
191
- String ver = local.getVersion().toString();
192
- int rev = name.indexOf(", rev");
193
- if (-1 < rev) {
194
- name = name.substring(0, rev);
195
- }
196
- int maj = ver.indexOf(".");
197
- if (-1 < maj) {
198
- String major = ver.substring(0, maj);
199
- int pos = name.indexOf(major);
200
- if (-1 < pos) {
201
- name = name.substring(0, pos).trim();
202
- }
203
- }
204
-
205
- localPackages.add(local.getPath());
206
- localPackages.add(local.getVersion().toString());
207
- localPackages.add(name);
208
-
209
- System.out.println("*************************");
210
- System.out.println(local.getDisplayName());
211
- System.out.println(name);
212
-
213
-
214
- installedList.add(localPackages);
215
- }
216
-
217
- for (UpdatablePackage update : packages.getUpdatedPkgs()) {
218
- Vector<String> updatePackages = new Vector<>();
219
- updatePackages.add(update.getPath());
220
- updatePackages.add(update.getLocal().getVersion().toString());
221
- updatePackages.add(update.getRemote().getVersion().toString());
222
-
223
- updatesList.add(updatePackages);
224
- }
225
- */
226
-
227
-
228
198
return null ;
229
199
}
230
200
@@ -235,14 +205,10 @@ protected void done() {
235
205
try {
236
206
get ();
237
207
firePropertyChange (PROPERTY_CHANGE_QUERY , "query" , "SUCCESS" );
238
-
239
- if (installedList != null ) {
240
- modelInstalled .setDataVector (installedList , columns );
241
- modelInstalled .fireTableDataChanged ();
242
-
243
- // modelUpdates.setDataVector(updatesList, columnsUpdates);
244
- // modelUpdates.fireTableDataChanged();
245
-
208
+
209
+ if (packageList != null ) {
210
+ packageTable .setDataVector (packageList , columns );
211
+ packageTable .fireTableDataChanged ();
246
212
}
247
213
} catch (InterruptedException | CancellationException e ) {
248
214
this .cancel (false );
@@ -307,7 +273,6 @@ protected void done() {
307
273
308
274
try {
309
275
get ();
310
- // actionButton.setVisible(false); //Hide button after update completes
311
276
actionButton .setEnabled (false );
312
277
status .setText ("Refreshing packages..." );
313
278
@@ -366,19 +331,12 @@ private void createLayout() {
366
331
367
332
/* Packages panel */
368
333
JPanel packagesPanel = new JPanel ();
369
- // packagesPanel.setBorder(BorderFactory.createTitledBorder(
370
- // BorderFactory.createEtchedBorder(), "Packages"));
371
334
372
335
BoxLayout boxLayout = new BoxLayout (packagesPanel , BoxLayout .Y_AXIS );
373
336
packagesPanel .setLayout (boxLayout );
374
337
375
- /* Installed Packages panel */
376
- // JPanel installedPanel = new JPanel(new BorderLayout());
377
- // installedPanel.setBorder(BorderFactory.createTitledBorder(
378
- // BorderFactory.createEtchedBorder(), "Installed"));
379
-
380
- //Installed Packages table
381
- modelInstalled = new DefaultTableModel (15 , columns .size ()) {
338
+ // Packages table
339
+ packageTable = new DefaultTableModel (DEF_NUM_ROWS , columns .size ()) {
382
340
@ Override
383
341
public boolean isCellEditable (int row , int column ) {
384
342
return false ;
@@ -389,53 +347,21 @@ public Class<?> getColumnClass(int columnIndex) {
389
347
return String .class ;
390
348
}
391
349
};
392
- JTable tInstalled = new JTable (modelInstalled ) {
350
+
351
+ table = new JTable (packageTable ) {
393
352
@ Override
394
353
public String getColumnName (int column ) {
395
354
return columns .get (column );
396
355
}
397
- };
398
- tInstalled .setFillsViewportHeight (true );
399
-
400
- tInstalled .setPreferredScrollableViewportSize (new Dimension (tInstalled .getPreferredSize ().width ,
401
- 15 * tInstalled .getRowHeight ()));
402
- packagesPanel .add (new JScrollPane (tInstalled ));
403
-
404
-
405
- /* Updates panel */
406
- // JPanel updatesPanel = new JPanel(new BorderLayout());
407
- // updatesPanel.setBorder(BorderFactory.createTitledBorder(
408
- // BorderFactory.createEtchedBorder(), "Available Updates"));
409
-
410
- /*
411
- //Updates table
412
- modelUpdates = new DefaultTableModel(5, columnClassU.length) {
413
- @Override
414
- public boolean isCellEditable(int row, int column) {
415
- return false;
416
- }
417
-
418
- @Override
419
- public Class<?> getColumnClass(int columnIndex) {
420
- return columnClassU[columnIndex];
421
- }
422
- };
423
- JTable tUpdates = new JTable(modelUpdates) {
424
- @Override
425
- public String getColumnName(int column) {
426
- return columnsUpdates.get(column);
427
- }
428
- };
429
- tUpdates.setFillsViewportHeight(true);
430
- tUpdates.setPreferredScrollableViewportSize(new Dimension(tUpdates.getPreferredSize().width,
431
- 5 * tUpdates.getRowHeight()));
432
- updatesPanel.add(new JScrollPane(tUpdates), BorderLayout.CENTER);
433
- */
434
-
435
-
436
-
437
- // packagesPanel.add(installedPanel);
438
- // packagesPanel.add(updatesPanel);
356
+ };
357
+ table .setFillsViewportHeight (true );
358
+ table .setAutoResizeMode (JTable .AUTO_RESIZE_ALL_COLUMNS );
359
+ Dimension dim = new Dimension (table .getColumnCount () * DEF_COL_WIDTH ,
360
+ table .getRowHeight () * DEF_NUM_ROWS );
361
+ table .setPreferredSize (dim );
362
+ table .setPreferredScrollableViewportSize (dim );
363
+
364
+ packagesPanel .add (new JScrollPane (table ));
439
365
440
366
JPanel controlPanel = new JPanel ();
441
367
GridBagLayout gridBagLayout = new GridBagLayout ();
@@ -473,7 +399,6 @@ public void actionPerformed(ActionEvent e) {
473
399
}
474
400
}
475
401
});
476
- // actionButton.setVisible(false);
477
402
actionButton .setEnabled (false );
478
403
gbc .gridx = 1 ;
479
404
gbc .gridy = 0 ;
@@ -498,6 +423,7 @@ public void actionPerformed(ActionEvent actionEvent) {
498
423
controlPanel .add (closeButton , gbc );
499
424
500
425
verticalBox .add (packagesPanel );
426
+ verticalBox .add (Box .createVerticalStrut (13 ));
501
427
verticalBox .add (controlPanel );
502
428
pack ();
503
429
@@ -515,10 +441,10 @@ public void windowClosing(WindowEvent e) {
515
441
});
516
442
517
443
setLocationRelativeTo (null );
518
- setResizable (true );
444
+ setResizable (false );
519
445
setVisible (true );
520
446
}
521
-
447
+
522
448
public void cancelTasks () {
523
449
queryTask .cancel (true );
524
450
if (downloadTaskRunning ) {
0 commit comments