29
29
import processing .core .PApplet ;
30
30
31
31
import javax .swing .*;
32
+ import javax .swing .event .HyperlinkEvent ;
33
+ import javax .swing .event .HyperlinkListener ;
34
+
32
35
import java .awt .*;
33
36
import java .io .BufferedReader ;
34
37
import java .io .File ;
@@ -53,21 +56,36 @@ class AndroidSDK {
53
56
private final File buildTools ;
54
57
private final File androidTool ;
55
58
56
- static final String DOWNLOAD_URL ="https://developer.android.com/studio/index.html#downloads" ;
59
+ private static final String SDK_DOWNLOAD_URL =
60
+ "https://developer.android.com/studio/index.html#downloads" ;
57
61
58
- private static final String ANDROID_SDK_PRIMARY =
62
+ private static final String MISSING_SDK_TITLE =
59
63
"Is the Android SDK installed?" ;
60
-
61
- private static final String ANDROID_SDK_SECONDARY =
62
- "The Android SDK does not appear to be installed, <br> " +
63
- "because the ANDROID_SDK variable is not set. <br> " +
64
- "If it is installed, click “Locate SDK path” to select the <br> " +
65
- "location of the SDK, or “Download SDK” to let <br> " +
64
+
65
+ private static final String MISSING_SDK_MESSAGE =
66
+ "The Android SDK does not appear to be installed, " +
67
+ "because the ANDROID_SDK variable is not set. " +
68
+ "If it is installed, click “Locate SDK path” to select the" +
69
+ "location of the SDK, or “Download SDK” to let " +
66
70
"Processing download the SDK automatically.<br><br>" +
67
- "If you want to download the SDK manually, you can get <br> " +
68
- "the command line tools from <a href=\" " + DOWNLOAD_URL + "\" >here</a>. Make sure to install<br> " +
69
- "the SDK platform for API " + AndroidBuild .target_sdk + "." ;
71
+ "If you want to download the SDK manually, you can get " +
72
+ "the command line tools from <a href=\" " + SDK_DOWNLOAD_URL + "\" >here</a>. " +
73
+ "Make sure to install the SDK platform for API " + AndroidBuild .target_sdk + "." ;
70
74
75
+ private static final String INVALID_SDK_TITLE =
76
+ "Is the required Android API installed?" ;
77
+
78
+ private static final String INVALID_SDK_MESSAGE =
79
+ "The Android SDK appears to be installed, " +
80
+ "however the SDK platform for API " + AndroidBuild .target_sdk +
81
+ " was not found. If it is available in a different location, " +
82
+ "click “Locate SDK path” to select the " +
83
+ "location of the alternative SDK, or “Download SDK” to let " +
84
+ "Processing download the SDK automatically.<br><br>" +
85
+ "If you want to download the SDK manually, you can get " +
86
+ "the command line tools from <a href=\" " + SDK_DOWNLOAD_URL + "\" >here</a>. " +
87
+ "Make sure to install the SDK platform for API " + AndroidBuild .target_sdk + "." ;
88
+
71
89
private static final String ANDROID_SYS_IMAGE_PRIMARY =
72
90
"Download emulator?" ;
73
91
@@ -86,6 +104,11 @@ class AndroidSDK {
86
104
87
105
private static final String SELECT_ANDROID_SDK_FOLDER =
88
106
"Choose the location of the Android SDK" ;
107
+
108
+ private static final int NO_ERROR = 0 ;
109
+ private static final int MISSING_SDK = 1 ;
110
+ private static final int INVALID_SDK = 2 ;
111
+ private static int SDK_LOAD_ERROR = NO_ERROR ;
89
112
90
113
public AndroidSDK (File folder ) throws BadSDKException , IOException {
91
114
this .folder = folder ;
@@ -263,6 +286,8 @@ private static File findAndroidTool(final File tools) throws BadSDKException {
263
286
* @throws IOException
264
287
*/
265
288
public static AndroidSDK load () throws IOException {
289
+ SDK_LOAD_ERROR = NO_ERROR ;
290
+
266
291
// The environment variable is king. The preferences.txt entry is a page.
267
292
final String sdkEnvPath = Platform .getenv ("ANDROID_SDK" );
268
293
if (sdkEnvPath != null ) {
@@ -289,7 +314,10 @@ public static AndroidSDK load() throws IOException {
289
314
return androidSDK ;
290
315
} catch (final BadSDKException wellThatsThat ) {
291
316
Preferences .unset ("android.sdk.path" );
317
+ SDK_LOAD_ERROR = INVALID_SDK ;
292
318
}
319
+ } else {
320
+ SDK_LOAD_ERROR = MISSING_SDK ;
293
321
}
294
322
return null ;
295
323
}
@@ -359,36 +387,44 @@ static public boolean downloadSysImage(final Frame editor,
359
387
return res ;
360
388
}
361
389
390
+
362
391
static public int showLocateDialog (Frame editor ) {
363
- // Pane formatting adapted from the Quaqua guide
364
- // http://www.randelshofer.ch/quaqua/guide/joptionpane.html
365
- JOptionPane pane =
366
- new JOptionPane ("<html> " +
367
- "<head> <style type=\" text/css\" >" +
368
- "b { font: 13pt \" Lucida Grande\" }" +
369
- "p { font: 11pt \" Lucida Grande\" ; margin-top: 8px; width: 300px }" +
370
- "</style> </head>" +
371
- "<b>" + ANDROID_SDK_PRIMARY + "</b>" +
372
- "<p>" + ANDROID_SDK_SECONDARY + "</p>" ,
373
- JOptionPane .QUESTION_MESSAGE );
374
-
392
+ // How to show a option dialog containing clickable links:
393
+ // http://stackoverflow.com/questions/8348063/clickable-links-in-joptionpane
394
+ String htmlString = "<html> " +
395
+ "<head> <style type=\" text/css\" >" +
396
+ "p { font: 11pt \" Lucida Grande\" ; margin-top: 8px; width: 300px }" +
397
+ "</style> </head>" ;
398
+ String title = "" ;
399
+ if (SDK_LOAD_ERROR == MISSING_SDK ) {
400
+ htmlString += "<body> <p>" + MISSING_SDK_MESSAGE + "</p> </body> </html>" ;
401
+ title = MISSING_SDK_TITLE ;
402
+ } else if (SDK_LOAD_ERROR == INVALID_SDK ) {
403
+ htmlString += "<body> <p>" + INVALID_SDK_MESSAGE + "</p> </body> </html>" ;
404
+ title = INVALID_SDK_TITLE ;
405
+ }
406
+ JEditorPane pane = new JEditorPane ("text/html" , htmlString );
407
+ pane .addHyperlinkListener (new HyperlinkListener () {
408
+ @ Override
409
+ public void hyperlinkUpdate (HyperlinkEvent e ) {
410
+ if (e .getEventType ().equals (HyperlinkEvent .EventType .ACTIVATED )) {
411
+ Platform .openURL (e .getURL ().toString ());
412
+ }
413
+ }
414
+ });
415
+ pane .setEditable (false );
416
+ JLabel label = new JLabel ();
417
+ pane .setBackground (label .getBackground ());
418
+
375
419
String [] options = new String [] {
376
- "Download SDK automatically" , "Locate SDK path manually"
377
- };
378
- pane .setOptions (options );
379
-
380
- // highlight the safest option ala apple hig
381
- pane .setInitialValue (options [0 ]);
382
-
383
- JDialog dialog = pane .createDialog (editor , null );
384
- dialog .setTitle ("" );
385
- dialog .setModalityType (Dialog .ModalityType .APPLICATION_MODAL );
386
- dialog .setVisible (true );
387
-
388
- Object result = pane .getValue ();
389
- if (result == options [0 ]) {
420
+ "Download SDK automatically" , "Locate SDK path manually"
421
+ };
422
+ int result = JOptionPane .showOptionDialog (null , pane , title ,
423
+ JOptionPane .DEFAULT_OPTION , JOptionPane .QUESTION_MESSAGE ,
424
+ null , options , options [0 ]);
425
+ if (result == JOptionPane .YES_OPTION ) {
390
426
return JOptionPane .YES_OPTION ;
391
- } else if (result == options [ 1 ] ) {
427
+ } else if (result == JOptionPane . NO_OPTION ) {
392
428
return JOptionPane .NO_OPTION ;
393
429
} else {
394
430
return JOptionPane .CLOSED_OPTION ;
0 commit comments