Skip to content

Commit 50f89d9

Browse files
committed
Refactored OS detection subroutine.
Moved from Base into a specific utility class OSUtils. Removed unused platform constants.
1 parent e0f680b commit 50f89d9

File tree

14 files changed

+82
-102
lines changed

14 files changed

+82
-102
lines changed

app/src/cc/arduino/packages/uploaders/SerialUploader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import processing.app.SerialException;
4040
import processing.app.debug.RunnerException;
4141
import processing.app.debug.TargetPlatform;
42+
import processing.app.helpers.OSUtils;
4243
import processing.app.helpers.PreferencesMap;
4344
import processing.app.helpers.StringReplacer;
4445
import cc.arduino.packages.Uploader;
@@ -188,7 +189,7 @@ private String waitForUploadPort(String uploadPort, List<String> before) throws
188189
// come back, so use a longer time out before assuming that the
189190
// selected
190191
// port is the bootloader (not the sketch).
191-
if (((!Base.isWindows() && elapsed >= 500) || elapsed >= 5000) && now.contains(uploadPort)) {
192+
if (((!OSUtils.isWindows() && elapsed >= 500) || elapsed >= 5000) && now.contains(uploadPort)) {
192193
if (verbose)
193194
System.out.println("Uploading using selected port: " + uploadPort);
194195
return uploadPort;

app/src/processing/app/Base.java

Lines changed: 14 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
import processing.app.debug.TargetPlatform;
4343
import processing.app.debug.TargetPlatformException;
4444
import processing.app.helpers.FileUtils;
45+
import processing.app.helpers.OSUtils;
4546
import processing.app.helpers.PreferencesMap;
4647
import processing.app.helpers.filefilters.OnlyDirs;
4748
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
4849
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;
4950
import processing.app.legacy.PApplet;
50-
import processing.app.legacy.PConstants;
5151
import processing.app.packages.Library;
5252
import processing.app.packages.LibraryList;
5353
import processing.app.tools.MenuScroller;
@@ -68,19 +68,6 @@ public class Base {
6868
/** Set true if this a proper release rather than a numbered revision. */
6969
static public boolean RELEASE = false;
7070

71-
static Map<Integer, String> platformNames = new HashMap<Integer, String>();
72-
static {
73-
platformNames.put(PConstants.WINDOWS, "windows");
74-
platformNames.put(PConstants.MACOSX, "macosx");
75-
platformNames.put(PConstants.LINUX, "linux");
76-
}
77-
78-
static HashMap<String, Integer> platformIndices = new HashMap<String, Integer>();
79-
static {
80-
platformIndices.put("windows", PConstants.WINDOWS);
81-
platformIndices.put("macosx", PConstants.MACOSX);
82-
platformIndices.put("linux", PConstants.LINUX);
83-
}
8471
static Platform platform;
8572

8673
private static DiscoveryManager discoveryManager = new DiscoveryManager();
@@ -263,11 +250,11 @@ static protected boolean isCommandLine() {
263250
static protected void initPlatform() {
264251
try {
265252
Class<?> platformClass = Class.forName("processing.app.Platform");
266-
if (Base.isMacOS()) {
253+
if (OSUtils.isMacOS()) {
267254
platformClass = Class.forName("processing.app.macosx.Platform");
268-
} else if (Base.isWindows()) {
255+
} else if (OSUtils.isWindows()) {
269256
platformClass = Class.forName("processing.app.windows.Platform");
270-
} else if (Base.isLinux()) {
257+
} else if (OSUtils.isLinux()) {
271258
platformClass = Class.forName("processing.app.linux.Platform");
272259
}
273260
platform = (Platform) platformClass.newInstance();
@@ -473,7 +460,7 @@ public Base(String[] args) throws Exception {
473460
// being passed in with 8.3 syntax, which makes the sketch loader code
474461
// unhappy, since the sketch folder naming doesn't match up correctly.
475462
// http://dev.processing.org/bugs/show_bug.cgi?id=1089
476-
if (isWindows()) {
463+
if (OSUtils.isWindows()) {
477464
try {
478465
file = file.getCanonicalFile();
479466
} catch (IOException e) {
@@ -1087,7 +1074,7 @@ public boolean handleClose(Editor editor) {
10871074
// untitled sketch, just give up and let the user quit.
10881075
// if (Preferences.getBoolean("sketchbook.closing_last_window_quits") ||
10891076
// (editor.untitled && !editor.getSketch().isModified())) {
1090-
if (Base.isMacOS()) {
1077+
if (OSUtils.isMacOS()) {
10911078
Object[] options = { "OK", "Cancel" };
10921079
String prompt =
10931080
_("<html> " +
@@ -1170,7 +1157,7 @@ public boolean handleQuit() {
11701157
// Save out the current prefs state
11711158
Preferences.save();
11721159

1173-
if (!Base.isMacOS()) {
1160+
if (!OSUtils.isMacOS()) {
11741161
// If this was fired from the menu or an AppleEvent (the Finder),
11751162
// then Mac OS X will send the terminate signal itself.
11761163
System.exit(0);
@@ -1996,54 +1983,6 @@ static public String getPlatformName() {
19961983
}
19971984

19981985

1999-
/**
2000-
* Map a platform constant to its name.
2001-
* @param which PConstants.WINDOWS, PConstants.MACOSX, PConstants.LINUX
2002-
* @return one of "windows", "macosx", or "linux"
2003-
*/
2004-
static public String getPlatformName(int which) {
2005-
return platformNames.get(which);
2006-
}
2007-
2008-
2009-
static public int getPlatformIndex(String what) {
2010-
Integer entry = platformIndices.get(what);
2011-
return (entry == null) ? -1 : entry.intValue();
2012-
}
2013-
2014-
2015-
// These were changed to no longer rely on PApplet and PConstants because
2016-
// of conflicts that could happen with older versions of core.jar, where
2017-
// the MACOSX constant would instead read as the LINUX constant.
2018-
2019-
2020-
/**
2021-
* returns true if Processing is running on a Mac OS X machine.
2022-
*/
2023-
static public boolean isMacOS() {
2024-
//return PApplet.platform == PConstants.MACOSX;
2025-
return System.getProperty("os.name").indexOf("Mac") != -1;
2026-
}
2027-
2028-
2029-
/**
2030-
* returns true if running on windows.
2031-
*/
2032-
static public boolean isWindows() {
2033-
//return PApplet.platform == PConstants.WINDOWS;
2034-
return System.getProperty("os.name").indexOf("Windows") != -1;
2035-
}
2036-
2037-
2038-
/**
2039-
* true if running on linux.
2040-
*/
2041-
static public boolean isLinux() {
2042-
//return PApplet.platform == PConstants.LINUX;
2043-
return System.getProperty("os.name").indexOf("Linux") != -1;
2044-
}
2045-
2046-
20471986
// .................................................................
20481987

20491988

@@ -2176,7 +2115,7 @@ static public String getHardwarePath() {
21762115
static public String getAvrBasePath() {
21772116
String path = getHardwarePath() + File.separator + "tools" +
21782117
File.separator + "avr" + File.separator + "bin" + File.separator;
2179-
if (Base.isLinux() && !(new File(path)).exists()) {
2118+
if (OSUtils.isLinux() && !(new File(path)).exists()) {
21802119
return ""; // use distribution provided avr tools if bundled tools missing
21812120
}
21822121
return path;
@@ -2411,7 +2350,7 @@ static public File selectFolder(String prompt, File folder, Frame frame) {
24112350
static public void setIcon(Frame frame) {
24122351
// don't use the low-res icon on Mac OS X; the window should
24132352
// already have the right icon from the .app file.
2414-
if (Base.isMacOS()) return;
2353+
if (OSUtils.isMacOS()) return;
24152354

24162355
Image image = Toolkit.getDefaultToolkit().createImage(PApplet.ICON_IMAGE);
24172356
frame.setIconImage(image);
@@ -2464,9 +2403,9 @@ static public void showReference(String filename) {
24642403
}
24652404

24662405
static public void showGettingStarted() {
2467-
if (Base.isMacOS()) {
2406+
if (OSUtils.isMacOS()) {
24682407
Base.showReference(_("Guide_MacOSX.html"));
2469-
} else if (Base.isWindows()) {
2408+
} else if (OSUtils.isWindows()) {
24702409
Base.showReference(_("Guide_Windows.html"));
24712410
} else {
24722411
Base.openURL(_("http://www.arduino.cc/playground/Learning/Linux"));
@@ -2570,7 +2509,7 @@ static public void showError(String title, String message, Throwable e, int exit
25702509
// incomplete
25712510
static public int showYesNoCancelQuestion(Editor editor, String title,
25722511
String primary, String secondary) {
2573-
if (!Base.isMacOS()) {
2512+
if (!OSUtils.isMacOS()) {
25742513
int result =
25752514
JOptionPane.showConfirmDialog(null, primary + "\n" + secondary, title,
25762515
JOptionPane.YES_NO_CANCEL_OPTION,
@@ -2646,7 +2585,7 @@ static public int showYesNoCancelQuestion(Editor editor, String title,
26462585

26472586
static public int showYesNoQuestion(Frame editor, String title,
26482587
String primary, String secondary) {
2649-
if (!Base.isMacOS()) {
2588+
if (!OSUtils.isMacOS()) {
26502589
return JOptionPane.showConfirmDialog(editor,
26512590
"<html><body>" +
26522591
"<b>" + primary + "</b>" +
@@ -2730,7 +2669,7 @@ static public File getContentFile(String name) {
27302669
String path = System.getProperty("user.dir");
27312670

27322671
// Get a path to somewhere inside the .app folder
2733-
if (Base.isMacOS()) {
2672+
if (OSUtils.isMacOS()) {
27342673
// <key>javaroot</key>
27352674
// <string>$JAVAROOT</string>
27362675
String javaroot = System.getProperty("javaroot");

app/src/processing/app/Editor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import processing.app.debug.*;
3030
import processing.app.forms.PasswordAuthorizationDialog;
31+
import processing.app.helpers.OSUtils;
3132
import processing.app.helpers.PreferencesMapException;
3233
import processing.app.legacy.PApplet;
3334
import processing.app.syntax.*;
@@ -591,7 +592,7 @@ public void actionPerformed(ActionEvent e) {
591592
fileMenu.add(item);
592593

593594
// macosx already has its own preferences and quit menu
594-
if (!Base.isMacOS()) {
595+
if (!OSUtils.isMacOS()) {
595596
fileMenu.addSeparator();
596597

597598
item = newJMenuItem(_("Preferences"), ',');
@@ -1110,7 +1111,7 @@ public void actionPerformed(ActionEvent e) {
11101111
menu.add(item);
11111112

11121113
// macosx already has its own about menu
1113-
if (!Base.isMacOS()) {
1114+
if (!OSUtils.isMacOS()) {
11141115
menu.addSeparator();
11151116
item = new JMenuItem(_("About Arduino"));
11161117
item.addActionListener(new ActionListener() {
@@ -1135,7 +1136,7 @@ protected JMenu buildEditMenu() {
11351136
undoItem.addActionListener(undoAction = new UndoAction());
11361137
menu.add(undoItem);
11371138

1138-
if (!Base.isMacOS()) {
1139+
if (!OSUtils.isMacOS()) {
11391140
redoItem = newJMenuItem(_("Redo"), 'Y');
11401141
} else {
11411142
redoItem = newJMenuItemShift(_("Redo"), 'Z');
@@ -2031,7 +2032,7 @@ protected boolean checkModified() {
20312032

20322033
String prompt = I18n.format(_("Save changes to \"{0}\"? "), sketch.getName());
20332034

2034-
if (!Base.isMacOS()) {
2035+
if (!OSUtils.isMacOS()) {
20352036
int result =
20362037
JOptionPane.showConfirmDialog(this, prompt, _("Close"),
20372038
JOptionPane.YES_NO_CANCEL_OPTION,

app/src/processing/app/EditorConsole.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import javax.swing.text.SimpleAttributeSet;
4141
import javax.swing.text.StyleConstants;
4242

43+
import processing.app.helpers.OSUtils;
44+
4345

4446
/**
4547
* Message console that sits below the editing area.
@@ -118,7 +120,7 @@ public EditorConsole(Editor _editor) {
118120

119121
// to fix ugliness.. normally macosx java 1.3 puts an
120122
// ugly white border around this object, so turn it off.
121-
if (Base.isMacOS()) {
123+
if (OSUtils.isMacOS()) {
122124
setBorder(null);
123125
}
124126

app/src/processing/app/EditorHeader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
package processing.app;
25+
import processing.app.helpers.OSUtils;
2526
import processing.app.tools.MenuScroller;
2627
import static processing.app.I18n._;
2728

@@ -381,15 +382,15 @@ public Dimension getPreferredSize() {
381382

382383

383384
public Dimension getMinimumSize() {
384-
if (Base.isMacOS()) {
385+
if (OSUtils.isMacOS()) {
385386
return new Dimension(300, Preferences.GRID_SIZE);
386387
}
387388
return new Dimension(300, Preferences.GRID_SIZE - 1);
388389
}
389390

390391

391392
public Dimension getMaximumSize() {
392-
if (Base.isMacOS()) {
393+
if (OSUtils.isMacOS()) {
393394
return new Dimension(3000, Preferences.GRID_SIZE);
394395
}
395396
return new Dimension(3000, Preferences.GRID_SIZE - 1);

app/src/processing/app/EditorLineStatus.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package processing.app;
2424

25+
import processing.app.helpers.OSUtils;
2526
import processing.app.syntax.*;
2627

2728
import java.awt.*;
@@ -61,7 +62,7 @@ public EditorLineStatus(JEditTextArea textarea) {
6162
foreground = Theme.getColor("linestatus.color");
6263
high = Theme.getInteger("linestatus.height");
6364

64-
if (Base.isMacOS()) {
65+
if (OSUtils.isMacOS()) {
6566
resize = Base.getThemeImage("resize.gif", this);
6667
}
6768
//linestatus.bgcolor = #000000
@@ -118,7 +119,7 @@ public void paintComponent(Graphics g) {
118119

119120
g.drawString(tmp, size.width - (int) bounds.getWidth() -20 , baseline);
120121

121-
if (Base.isMacOS()) {
122+
if (OSUtils.isMacOS()) {
122123
g.drawImage(resize, size.width - 20, 0, this);
123124
}
124125
}

app/src/processing/app/EditorStatus.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
import java.awt.*;
2727
import java.awt.event.*;
28+
2829
import javax.swing.*;
2930

31+
import processing.app.helpers.OSUtils;
32+
3033
import java.awt.datatransfer.*;
34+
3135
import static processing.app.I18n._;
3236

3337

@@ -331,7 +335,7 @@ public void actionPerformed(ActionEvent e) {
331335

332336
// !@#(* aqua ui #($*(( that turtle-neck wearing #(** (#$@)(
333337
// os9 seems to work if bg of component is set, but x still a bastard
334-
if (Base.isMacOS()) {
338+
if (OSUtils.isMacOS()) {
335339
//yesButton.setBackground(bgcolor[EDIT]);
336340
//noButton.setBackground(bgcolor[EDIT]);
337341
cancelButton.setBackground(bgcolor[EDIT]);
@@ -444,7 +448,7 @@ public void keyTyped(KeyEvent event) {
444448

445449
progressBar = new JProgressBar(JScrollBar.HORIZONTAL);
446450
progressBar.setIndeterminate(false);
447-
if (Base.isMacOS()) {
451+
if (OSUtils.isMacOS()) {
448452
//progressBar.setBackground(bgcolor[PROGRESS]);
449453
//progressBar.putClientProperty("JProgressBar.style", "circular");
450454
}

app/src/processing/app/FindReplace.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626

2727
import java.awt.*;
2828
import java.awt.event.*;
29+
2930
import javax.swing.*;
3031

32+
import processing.app.helpers.OSUtils;
33+
3134

3235
/**
3336
* Find & Replace window for the Processing editor.
@@ -47,7 +50,7 @@
4750
@SuppressWarnings("serial")
4851
public class FindReplace extends JFrame implements ActionListener {
4952

50-
static final int EDGE = Base.isMacOS() ? 20 : 13;
53+
static final int EDGE = OSUtils.isMacOS() ? 20 : 13;
5154
static final int SMALL = 6;
5255
static final int BUTTONGAP = 12; // 12 is correct for Mac, other numbers may be required for other platofrms
5356

@@ -143,7 +146,7 @@ public void actionPerformed(ActionEvent e) {
143146
buttons.setLayout(new FlowLayout(FlowLayout.CENTER, BUTTONGAP, 0));
144147

145148
// ordering is different on mac versus pc
146-
if (Base.isMacOS()) {
149+
if (OSUtils.isMacOS()) {
147150
buttons.add(replaceAllButton = new JButton(_("Replace All")));
148151
buttons.add(replaceButton = new JButton(_("Replace")));
149152
buttons.add(replaceFindButton = new JButton(_("Replace & Find")));
@@ -161,7 +164,7 @@ public void actionPerformed(ActionEvent e) {
161164

162165
// to fix ugliness.. normally macosx java 1.3 puts an
163166
// ugly white border around this object, so turn it off.
164-
if (Base.isMacOS()) {
167+
if (OSUtils.isMacOS()) {
165168
buttons.setBorder(null);
166169
}
167170

0 commit comments

Comments
 (0)