Refactored OS detection subroutine.

Moved from Base into a specific utility class OSUtils.
Removed unused platform constants.
This commit is contained in:
Cristian Maglie 2014-08-19 16:48:34 +02:00
parent e0f680be5b
commit 50f89d9665
14 changed files with 82 additions and 102 deletions

View File

@ -39,6 +39,7 @@ import processing.app.Serial;
import processing.app.SerialException;
import processing.app.debug.RunnerException;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.StringReplacer;
import cc.arduino.packages.Uploader;
@ -188,7 +189,7 @@ public class SerialUploader extends Uploader {
// come back, so use a longer time out before assuming that the
// selected
// port is the bootloader (not the sketch).
if (((!Base.isWindows() && elapsed >= 500) || elapsed >= 5000) && now.contains(uploadPort)) {
if (((!OSUtils.isWindows() && elapsed >= 500) || elapsed >= 5000) && now.contains(uploadPort)) {
if (verbose)
System.out.println("Uploading using selected port: " + uploadPort);
return uploadPort;

View File

@ -42,12 +42,12 @@ import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.debug.TargetPlatformException;
import processing.app.helpers.FileUtils;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.filefilters.OnlyDirs;
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;
import processing.app.legacy.PApplet;
import processing.app.legacy.PConstants;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
import processing.app.tools.MenuScroller;
@ -68,19 +68,6 @@ public class Base {
/** Set true if this a proper release rather than a numbered revision. */
static public boolean RELEASE = false;
static Map<Integer, String> platformNames = new HashMap<Integer, String>();
static {
platformNames.put(PConstants.WINDOWS, "windows");
platformNames.put(PConstants.MACOSX, "macosx");
platformNames.put(PConstants.LINUX, "linux");
}
static HashMap<String, Integer> platformIndices = new HashMap<String, Integer>();
static {
platformIndices.put("windows", PConstants.WINDOWS);
platformIndices.put("macosx", PConstants.MACOSX);
platformIndices.put("linux", PConstants.LINUX);
}
static Platform platform;
private static DiscoveryManager discoveryManager = new DiscoveryManager();
@ -263,11 +250,11 @@ public class Base {
static protected void initPlatform() {
try {
Class<?> platformClass = Class.forName("processing.app.Platform");
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
platformClass = Class.forName("processing.app.macosx.Platform");
} else if (Base.isWindows()) {
} else if (OSUtils.isWindows()) {
platformClass = Class.forName("processing.app.windows.Platform");
} else if (Base.isLinux()) {
} else if (OSUtils.isLinux()) {
platformClass = Class.forName("processing.app.linux.Platform");
}
platform = (Platform) platformClass.newInstance();
@ -473,7 +460,7 @@ public class Base {
// being passed in with 8.3 syntax, which makes the sketch loader code
// unhappy, since the sketch folder naming doesn't match up correctly.
// http://dev.processing.org/bugs/show_bug.cgi?id=1089
if (isWindows()) {
if (OSUtils.isWindows()) {
try {
file = file.getCanonicalFile();
} catch (IOException e) {
@ -1087,7 +1074,7 @@ public class Base {
// untitled sketch, just give up and let the user quit.
// if (Preferences.getBoolean("sketchbook.closing_last_window_quits") ||
// (editor.untitled && !editor.getSketch().isModified())) {
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
Object[] options = { "OK", "Cancel" };
String prompt =
_("<html> " +
@ -1170,7 +1157,7 @@ public class Base {
// Save out the current prefs state
Preferences.save();
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
// If this was fired from the menu or an AppleEvent (the Finder),
// then Mac OS X will send the terminate signal itself.
System.exit(0);
@ -1996,54 +1983,6 @@ public class Base {
}
/**
* Map a platform constant to its name.
* @param which PConstants.WINDOWS, PConstants.MACOSX, PConstants.LINUX
* @return one of "windows", "macosx", or "linux"
*/
static public String getPlatformName(int which) {
return platformNames.get(which);
}
static public int getPlatformIndex(String what) {
Integer entry = platformIndices.get(what);
return (entry == null) ? -1 : entry.intValue();
}
// These were changed to no longer rely on PApplet and PConstants because
// of conflicts that could happen with older versions of core.jar, where
// the MACOSX constant would instead read as the LINUX constant.
/**
* returns true if Processing is running on a Mac OS X machine.
*/
static public boolean isMacOS() {
//return PApplet.platform == PConstants.MACOSX;
return System.getProperty("os.name").indexOf("Mac") != -1;
}
/**
* returns true if running on windows.
*/
static public boolean isWindows() {
//return PApplet.platform == PConstants.WINDOWS;
return System.getProperty("os.name").indexOf("Windows") != -1;
}
/**
* true if running on linux.
*/
static public boolean isLinux() {
//return PApplet.platform == PConstants.LINUX;
return System.getProperty("os.name").indexOf("Linux") != -1;
}
// .................................................................
@ -2176,7 +2115,7 @@ public class Base {
static public String getAvrBasePath() {
String path = getHardwarePath() + File.separator + "tools" +
File.separator + "avr" + File.separator + "bin" + File.separator;
if (Base.isLinux() && !(new File(path)).exists()) {
if (OSUtils.isLinux() && !(new File(path)).exists()) {
return ""; // use distribution provided avr tools if bundled tools missing
}
return path;
@ -2411,7 +2350,7 @@ public class Base {
static public void setIcon(Frame frame) {
// don't use the low-res icon on Mac OS X; the window should
// already have the right icon from the .app file.
if (Base.isMacOS()) return;
if (OSUtils.isMacOS()) return;
Image image = Toolkit.getDefaultToolkit().createImage(PApplet.ICON_IMAGE);
frame.setIconImage(image);
@ -2464,9 +2403,9 @@ public class Base {
}
static public void showGettingStarted() {
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
Base.showReference(_("Guide_MacOSX.html"));
} else if (Base.isWindows()) {
} else if (OSUtils.isWindows()) {
Base.showReference(_("Guide_Windows.html"));
} else {
Base.openURL(_("http://www.arduino.cc/playground/Learning/Linux"));
@ -2570,7 +2509,7 @@ public class Base {
// incomplete
static public int showYesNoCancelQuestion(Editor editor, String title,
String primary, String secondary) {
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
int result =
JOptionPane.showConfirmDialog(null, primary + "\n" + secondary, title,
JOptionPane.YES_NO_CANCEL_OPTION,
@ -2646,7 +2585,7 @@ public class Base {
static public int showYesNoQuestion(Frame editor, String title,
String primary, String secondary) {
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
return JOptionPane.showConfirmDialog(editor,
"<html><body>" +
"<b>" + primary + "</b>" +
@ -2730,7 +2669,7 @@ public class Base {
String path = System.getProperty("user.dir");
// Get a path to somewhere inside the .app folder
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
// <key>javaroot</key>
// <string>$JAVAROOT</string>
String javaroot = System.getProperty("javaroot");

View File

@ -28,6 +28,7 @@ import com.jcraft.jsch.JSchException;
import processing.app.debug.*;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMapException;
import processing.app.legacy.PApplet;
import processing.app.syntax.*;
@ -591,7 +592,7 @@ public class Editor extends JFrame implements RunnerListener {
fileMenu.add(item);
// macosx already has its own preferences and quit menu
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
fileMenu.addSeparator();
item = newJMenuItem(_("Preferences"), ',');
@ -1110,7 +1111,7 @@ public class Editor extends JFrame implements RunnerListener {
menu.add(item);
// macosx already has its own about menu
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
menu.addSeparator();
item = new JMenuItem(_("About Arduino"));
item.addActionListener(new ActionListener() {
@ -1135,7 +1136,7 @@ public class Editor extends JFrame implements RunnerListener {
undoItem.addActionListener(undoAction = new UndoAction());
menu.add(undoItem);
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
redoItem = newJMenuItem(_("Redo"), 'Y');
} else {
redoItem = newJMenuItemShift(_("Redo"), 'Z');
@ -2031,7 +2032,7 @@ public class Editor extends JFrame implements RunnerListener {
String prompt = I18n.format(_("Save changes to \"{0}\"? "), sketch.getName());
if (!Base.isMacOS()) {
if (!OSUtils.isMacOS()) {
int result =
JOptionPane.showConfirmDialog(this, prompt, _("Close"),
JOptionPane.YES_NO_CANCEL_OPTION,

View File

@ -40,6 +40,8 @@ import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import processing.app.helpers.OSUtils;
/**
* Message console that sits below the editing area.
@ -118,7 +120,7 @@ public class EditorConsole extends JScrollPane {
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
setBorder(null);
}

View File

@ -22,6 +22,7 @@
*/
package processing.app;
import processing.app.helpers.OSUtils;
import processing.app.tools.MenuScroller;
import static processing.app.I18n._;
@ -381,7 +382,7 @@ public class EditorHeader extends JComponent {
public Dimension getMinimumSize() {
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
return new Dimension(300, Preferences.GRID_SIZE);
}
return new Dimension(300, Preferences.GRID_SIZE - 1);
@ -389,7 +390,7 @@ public class EditorHeader extends JComponent {
public Dimension getMaximumSize() {
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
return new Dimension(3000, Preferences.GRID_SIZE);
}
return new Dimension(3000, Preferences.GRID_SIZE - 1);

View File

@ -22,6 +22,7 @@
package processing.app;
import processing.app.helpers.OSUtils;
import processing.app.syntax.*;
import java.awt.*;
@ -61,7 +62,7 @@ public class EditorLineStatus extends JComponent {
foreground = Theme.getColor("linestatus.color");
high = Theme.getInteger("linestatus.height");
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
resize = Base.getThemeImage("resize.gif", this);
}
//linestatus.bgcolor = #000000
@ -118,7 +119,7 @@ public class EditorLineStatus extends JComponent {
g.drawString(tmp, size.width - (int) bounds.getWidth() -20 , baseline);
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
g.drawImage(resize, size.width - 20, 0, this);
}
}

View File

@ -25,9 +25,13 @@ package processing.app;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import processing.app.helpers.OSUtils;
import java.awt.datatransfer.*;
import static processing.app.I18n._;
@ -331,7 +335,7 @@ public class EditorStatus extends JPanel /*implements ActionListener*/ {
// !@#(* aqua ui #($*(( that turtle-neck wearing #(** (#$@)(
// os9 seems to work if bg of component is set, but x still a bastard
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
//yesButton.setBackground(bgcolor[EDIT]);
//noButton.setBackground(bgcolor[EDIT]);
cancelButton.setBackground(bgcolor[EDIT]);
@ -444,7 +448,7 @@ public class EditorStatus extends JPanel /*implements ActionListener*/ {
progressBar = new JProgressBar(JScrollBar.HORIZONTAL);
progressBar.setIndeterminate(false);
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
//progressBar.setBackground(bgcolor[PROGRESS]);
//progressBar.putClientProperty("JProgressBar.style", "circular");
}

View File

@ -26,8 +26,11 @@ import static processing.app.I18n._;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import processing.app.helpers.OSUtils;
/**
* Find & Replace window for the Processing editor.
@ -47,7 +50,7 @@ import javax.swing.*;
@SuppressWarnings("serial")
public class FindReplace extends JFrame implements ActionListener {
static final int EDGE = Base.isMacOS() ? 20 : 13;
static final int EDGE = OSUtils.isMacOS() ? 20 : 13;
static final int SMALL = 6;
static final int BUTTONGAP = 12; // 12 is correct for Mac, other numbers may be required for other platofrms
@ -143,7 +146,7 @@ public class FindReplace extends JFrame implements ActionListener {
buttons.setLayout(new FlowLayout(FlowLayout.CENTER, BUTTONGAP, 0));
// ordering is different on mac versus pc
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
buttons.add(replaceAllButton = new JButton(_("Replace All")));
buttons.add(replaceButton = new JButton(_("Replace")));
buttons.add(replaceFindButton = new JButton(_("Replace & Find")));
@ -161,7 +164,7 @@ public class FindReplace extends JFrame implements ActionListener {
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
buttons.setBorder(null);
}

View File

@ -50,6 +50,7 @@ import javax.swing.JTextField;
import javax.swing.KeyStroke;
import processing.app.helpers.FileUtils;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesHelper;
import processing.app.helpers.PreferencesMap;
import processing.app.legacy.PApplet;
@ -405,7 +406,7 @@ public class Preferences {
// [ ] Automatically associate .pde files with Processing
if (Base.isWindows()) {
if (OSUtils.isWindows()) {
autoAssociateBox =
new JCheckBox(_("Automatically associate .ino files with Arduino"));
pain.add(autoAssociateBox);

View File

@ -25,12 +25,12 @@ package processing.app;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.UploaderAndMonitorFactory;
import cc.arduino.packages.Uploader;
import processing.app.debug.*;
import processing.app.debug.Compiler;
import processing.app.debug.Compiler.ProgressListener;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.FileUtils;
import processing.app.packages.Library;
@ -529,7 +529,7 @@ public class Sketch {
}
editor.header.repaint();
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
// http://developer.apple.com/qa/qa2001/qa1146.html
Object modifiedParam = modified ? Boolean.TRUE : Boolean.FALSE;
editor.getRootPane().putClientProperty("windowModified", modifiedParam);

View File

@ -0,0 +1,29 @@
package processing.app.helpers;
public class OSUtils {
/**
* returns true if running on windows.
*/
static public boolean isWindows() {
//return PApplet.platform == PConstants.WINDOWS;
return System.getProperty("os.name").indexOf("Windows") != -1;
}
/**
* true if running on linux.
*/
static public boolean isLinux() {
//return PApplet.platform == PConstants.LINUX;
return System.getProperty("os.name").indexOf("Linux") != -1;
}
/**
* returns true if Processing is running on a Mac OS X machine.
*/
static public boolean isMacOS() {
//return PApplet.platform == PConstants.MACOSX;
return System.getProperty("os.name").indexOf("Mac") != -1;
}
}

View File

@ -31,7 +31,6 @@ import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import processing.app.Base;
import processing.app.legacy.PApplet;
@SuppressWarnings("serial")
@ -105,9 +104,9 @@ public class PreferencesMap extends LinkedHashMap<String, String> {
String key = line.substring(0, equals).trim();
String value = line.substring(equals + 1).trim();
key = processPlatformSuffix(key, ".linux", Base.isLinux());
key = processPlatformSuffix(key, ".windows", Base.isWindows());
key = processPlatformSuffix(key, ".macosx", Base.isMacOS());
key = processPlatformSuffix(key, ".linux", OSUtils.isLinux());
key = processPlatformSuffix(key, ".windows", OSUtils.isWindows());
key = processPlatformSuffix(key, ".macosx", OSUtils.isMacOS());
if (key != null)
put(key, value);

View File

@ -1,7 +1,5 @@
package processing.app.helpers;
import processing.app.Base;
import java.io.IOException;
import java.util.Map;
@ -9,7 +7,7 @@ public class ProcessUtils {
public static Process exec(String[] command) throws IOException {
// No problems on linux and mac
if (!Base.isWindows()) {
if (!OSUtils.isWindows()) {
return Runtime.getRuntime().exec(command);
}

View File

@ -25,6 +25,7 @@
package processing.app.syntax;
import processing.app.*;
import processing.app.helpers.OSUtils;
public class PdeTextAreaDefaults extends TextAreaDefaults {
@ -35,7 +36,7 @@ public class PdeTextAreaDefaults extends TextAreaDefaults {
//inputHandler.addDefaultKeyBindings(); // 0122
// use option on mac for text edit controls that are ctrl on windows/linux
String mod = Base.isMacOS() ? "A" : "C";
String mod = OSUtils.isMacOS() ? "A" : "C";
// right now, ctrl-up/down is select up/down, but mod should be
// used instead, because the mac expects it to be option(alt)
@ -94,7 +95,7 @@ public class PdeTextAreaDefaults extends TextAreaDefaults {
inputHandler.addKeyBinding("CS+END", InputHandler.SELECT_DOC_END);
}
if (Base.isMacOS()) {
if (OSUtils.isMacOS()) {
inputHandler.addKeyBinding("M+LEFT", InputHandler.HOME);
inputHandler.addKeyBinding("M+RIGHT", InputHandler.END);
inputHandler.addKeyBinding("MS+LEFT", InputHandler.SELECT_HOME); // 0122