1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

Removed lots of calls to BaseNoGui.getPlatform(): static is evil

This commit is contained in:
Federico Fissore 2015-05-22 15:42:05 +02:00
parent 46e065b76d
commit ab7b7351f5
11 changed files with 84 additions and 136 deletions

View File

@ -37,6 +37,8 @@ import cc.arduino.contributions.ui.InstallerJDialogUncaughtExceptionHandler;
import cc.arduino.contributions.ui.*;
import cc.arduino.utils.Progress;
import com.google.common.base.Predicate;
import processing.app.BaseNoGui;
import processing.app.Platform;
import javax.swing.*;
import java.awt.*;
@ -49,8 +51,9 @@ import static processing.app.I18n._;
@SuppressWarnings("serial")
public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
private LibrariesIndexer indexer;
private final JComboBox typeChooser;
private final Platform platform;
private LibrariesIndexer indexer;
private Predicate<ContributedLibrary> typeFilter;
@Override
@ -86,8 +89,9 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
};
}
public LibraryManagerUI(Frame parent) {
public LibraryManagerUI(Frame parent, Platform platform) {
super(parent, "Library Manager", Dialog.ModalityType.APPLICATION_MODAL, _("Unable to reach Arduino.cc due to possible network issues."));
this.platform = platform;
filtersContainer.add(new JLabel(_("Topic")), 1);
filtersContainer.remove(2);
@ -177,7 +181,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
filterField.setEnabled(contribModel.getRowCount() > 0);
// Create LibrariesInstaller tied with the provided index
installer = new LibraryInstaller(indexer) {
installer = new LibraryInstaller(indexer, platform) {
@Override
public void onProgress(Progress progress) {
setProgress(progress);

View File

@ -34,7 +34,9 @@ import cc.arduino.contributions.packages.ContributionsIndexer;
import cc.arduino.contributions.DownloadableContribution;
import cc.arduino.contributions.ui.*;
import cc.arduino.utils.Progress;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.Platform;
import javax.swing.*;
import java.awt.*;
@ -47,7 +49,7 @@ import static processing.app.I18n._;
@SuppressWarnings("serial")
public class ContributionManagerUI extends InstallerJDialog {
// private ContributedPlatformTableCell cellEditor;
private final Platform platform;
@Override
protected FilteredAbstractTableModel createContribModel() {
@ -82,8 +84,9 @@ public class ContributionManagerUI extends InstallerJDialog {
};
}
public ContributionManagerUI(Frame parent) {
public ContributionManagerUI(Frame parent, Platform platform) {
super(parent, _("Boards Manager"), Dialog.ModalityType.APPLICATION_MODAL, _("Unable to reach Arduino.cc due to possible network issues."));
this.platform = platform;
}
public void setIndexer(ContributionsIndexer indexer) {
@ -113,7 +116,7 @@ public class ContributionManagerUI extends InstallerJDialog {
}
// Create ConstributionInstaller tied with the provided index
installer = new ContributionInstaller(indexer) {
installer = new ContributionInstaller(indexer, platform) {
@Override
public void onProgress(Progress progress) {
setProgress(progress);

View File

@ -145,9 +145,9 @@ public class Base {
BaseNoGui.notifier = new GUIUserNotifier();
initPlatform();
BaseNoGui.initPlatform();
getPlatform().init();
BaseNoGui.getPlatform().init();
BaseNoGui.initPortableFolder();
@ -206,7 +206,7 @@ public class Base {
// Set the look and feel before opening the window
try {
getPlatform().setLookAndFeel();
BaseNoGui.getPlatform().setLookAndFeel();
} catch (Exception e) {
String mess = e.getMessage();
if (mess.indexOf("ch.randelshofer.quaqua.QuaquaLookAndFeel") == -1) {
@ -261,23 +261,6 @@ public class Base {
return commandLine;
}
static protected void initPlatform() {
BaseNoGui.initPlatform();
}
static protected void initRequirements() {
try {
Class.forName("com.sun.jdi.VirtualMachine");
} catch (ClassNotFoundException cnfe) {
showError(_("Please install JDK 1.5 or later"),
_("Arduino requires a full JDK (not just a JRE)\n" +
"to run. Please install JDK 1.5 or later.\n" +
"More information can be found in the reference."), cnfe);
}
}
// Returns a File object for the given pathname. If the pathname
// is not absolute, it is interpreted relative to the current
// directory when starting the IDE (which is not the same as the
@ -349,8 +332,8 @@ public class Base {
PreferencesData.save();
if (parser.isInstallBoard()) {
ContributionsIndexer indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
ContributionInstaller installer = new ContributionInstaller(indexer) {
ContributionsIndexer indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform());
ContributionInstaller installer = new ContributionInstaller(indexer, BaseNoGui.getPlatform()) {
private String lastStatus = "";
@Override
@ -396,8 +379,8 @@ public class Base {
System.exit(0);
} else if (parser.isInstallLibrary()) {
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), new ContributionsIndexer(BaseNoGui.getSettingsFolder()));
LibraryInstaller installer = new LibraryInstaller(indexer) {
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform()));
LibraryInstaller installer = new LibraryInstaller(indexer, BaseNoGui.getPlatform()) {
private String lastStatus = "";
@Override
@ -929,7 +912,7 @@ public class Base {
// }
// System.err.println(" creating new editor");
Editor editor = new Editor(this, file, location);
Editor editor = new Editor(this, file, location, BaseNoGui.getPlatform());
// Editor editor = null;
// try {
// editor = new Editor(this, path, location);
@ -1302,7 +1285,7 @@ public class Base {
private void openManageLibrariesDialog() {
@SuppressWarnings("serial")
LibraryManagerUI managerUI = new LibraryManagerUI(activeEditor) {
LibraryManagerUI managerUI = new LibraryManagerUI(activeEditor, BaseNoGui.getPlatform()) {
@Override
protected void onIndexesUpdated() throws Exception {
BaseNoGui.initPackages();
@ -1325,7 +1308,7 @@ public class Base {
private void openInstallBoardDialog(final String filterText) throws Exception {
// Create dialog for contribution manager
@SuppressWarnings("serial")
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor) {
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor, BaseNoGui.getPlatform()) {
@Override
protected void onIndexesUpdated() throws Exception {
BaseNoGui.initPackages();
@ -1829,65 +1812,6 @@ public class Base {
dialog.setVisible(true);
}
// ...................................................................
/**
* Get list of platform constants.
*/
// static public int[] getPlatforms() {
// return platforms;
// }
// static public int getPlatform() {
// String osname = System.getProperty("os.name");
//
// if (osname.indexOf("Mac") != -1) {
// return PConstants.MACOSX;
//
// } else if (osname.indexOf("Windows") != -1) {
// return PConstants.WINDOWS;
//
// } else if (osname.equals("Linux")) { // true for the ibm vm
// return PConstants.LINUX;
//
// } else {
// return PConstants.OTHER;
// }
// }
static public Platform getPlatform() {
return BaseNoGui.getPlatform();
}
static public String getPlatformName() {
String osname = System.getProperty("os.name");
if (osname.indexOf("Mac") != -1) {
return "macosx";
} else if (osname.indexOf("Windows") != -1) {
return "windows";
} else if (osname.equals("Linux")) { // true for the ibm vm
return "linux";
} else {
return "other";
}
}
// .................................................................
static public File getSettingsFolder() {
return BaseNoGui.getSettingsFolder();
}
/**
* Convenience method to get a File object for the specified filename inside
* the settings folder.
@ -2092,7 +2016,7 @@ public class Base {
*/
static public void openURL(String url) {
try {
getPlatform().openURL(url);
BaseNoGui.getPlatform().openURL(url);
} catch (Exception e) {
showWarning(_("Problem Opening URL"),

View File

@ -70,6 +70,8 @@ import cc.arduino.packages.uploaders.SerialUploader;
@SuppressWarnings("serial")
public class Editor extends JFrame implements RunnerListener {
private final Platform platform;
private static class ShouldSaveIfModified implements Predicate<Sketch> {
@Override
@ -184,9 +186,10 @@ public class Editor extends JFrame implements RunnerListener {
Runnable exportAppHandler;
public Editor(Base ibase, File file, int[] location) throws Exception {
public Editor(Base ibase, File file, int[] location, Platform platform) throws Exception {
super("Arduino");
this.base = ibase;
this.platform = platform;
Base.setIcon(this);
@ -992,7 +995,7 @@ public class Editor extends JFrame implements RunnerListener {
@Override
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
try {
base.getPlatform().openURL(hyperlinkEvent.getURL().toExternalForm());
platform.openURL(hyperlinkEvent.getURL().toExternalForm());
} catch (Exception e) {
Base.showWarning(e.getMessage(), e.getMessage(), e);
}
@ -1129,7 +1132,7 @@ public class Editor extends JFrame implements RunnerListener {
List<BoardPort> ports = Base.getDiscoveryManager().discovery();
ports = Base.getPlatform().filterPorts(ports, PreferencesData.getBoolean("serial.ports.showall"));
ports = platform.filterPorts(ports, PreferencesData.getBoolean("serial.ports.showall"));
Collections.sort(ports, new Comparator<BoardPort>() {
@Override

View File

@ -50,11 +50,11 @@ public abstract class AbstractGUITest {
FailOnThreadViolationRepaintManager.install();
Base.initPlatform();
BaseNoGui.initPlatform();
Preferences.init(null);
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
Theme.init();
Base.getPlatform().setLookAndFeel();
BaseNoGui.getPlatform().setLookAndFeel();
Base.untitledFolder = Base.createTempFolder("untitled");
DeleteFilesOnShutdown.add(Base.untitledFolder);

View File

@ -37,8 +37,8 @@ public abstract class AbstractWithPreferencesTest {
@Before
public void init() throws Exception {
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
Base.initPlatform();
Base.getPlatform().init();
BaseNoGui.initPlatform();
BaseNoGui.getPlatform().init();
Preferences.init(null);
Theme.init();

View File

@ -33,8 +33,8 @@ import cc.arduino.contributions.GZippedJsonDownloader;
import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.helpers.FileUtils;
import java.io.File;
@ -60,10 +60,12 @@ public class LibraryInstaller {
private final LibrariesIndexer indexer;
private final DownloadableContributionsDownloader downloader;
private final Platform platform;
public LibraryInstaller(LibrariesIndexer _indexer) {
indexer = _indexer;
File stagingFolder = _indexer.getStagingFolder();
public LibraryInstaller(LibrariesIndexer indexer, Platform platform) {
this.indexer = indexer;
this.platform = platform;
File stagingFolder = indexer.getStagingFolder();
downloader = new DownloadableContributionsDownloader(stagingFolder) {
@Override
protected void onProgress(Progress progress) {
@ -126,7 +128,7 @@ public class LibraryInstaller {
File libsFolder = indexer.getSketchbookLibrariesFolder();
File tmpFolder = FileUtils.createTempFolderIn(libsFolder);
try {
new ArchiveExtractor(BaseNoGui.getPlatform()).extract(lib.getDownloadedFile(), tmpFolder, 1);
new ArchiveExtractor(platform).extract(lib.getDownloadedFile(), tmpFolder, 1);
} catch (Exception e) {
if (tmpFolder.exists())
FileUtils.recursiveDelete(tmpFolder);

View File

@ -29,7 +29,7 @@
package cc.arduino.contributions.packages;
import cc.arduino.contributions.DownloadableContribution;
import processing.app.BaseNoGui;
import processing.app.Platform;
import java.util.List;
@ -41,9 +41,9 @@ public abstract class ContributedTool {
public abstract List<HostDependentDownloadableContribution> getSystems();
public DownloadableContribution getDownloadableContribution() {
public DownloadableContribution getDownloadableContribution(Platform platform) {
for (HostDependentDownloadableContribution c : getSystems()) {
if (c.isCompatible(BaseNoGui.getPlatform()))
if (c.isCompatible(platform))
return c;
}
return null;
@ -51,11 +51,17 @@ public abstract class ContributedTool {
@Override
public String toString() {
return toString(null);
}
public String toString(Platform platform) {
String res;
res = "Tool name : " + getName() + " " + getVersion() + "\n";
for (HostDependentDownloadableContribution sys : getSystems()) {
res += " sys";
res += sys.isCompatible(BaseNoGui.getPlatform()) ? "*" : " ";
if (platform != null) {
res += sys.isCompatible(platform) ? "*" : " ";
}
res += " : " + sys + "\n";
}
return res;

View File

@ -40,6 +40,7 @@ import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.Executor;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.PreferencesData;
import processing.app.helpers.FileUtils;
import processing.app.helpers.filefilters.OnlyDirs;
@ -58,8 +59,10 @@ public class ContributionInstaller {
private final ContributionsIndexer indexer;
private final DownloadableContributionsDownloader downloader;
private final Platform platform;
public ContributionInstaller(ContributionsIndexer contributionsIndexer) {
public ContributionInstaller(ContributionsIndexer contributionsIndexer, Platform platform) {
this.platform = platform;
File stagingFolder = contributionsIndexer.getStagingFolder();
indexer = contributionsIndexer;
downloader = new DownloadableContributionsDownloader(stagingFolder) {
@ -70,18 +73,18 @@ public class ContributionInstaller {
};
}
public List<String> install(ContributedPlatform platform) throws Exception {
public List<String> install(ContributedPlatform contributedPlatform) throws Exception {
List<String> errors = new LinkedList<String>();
if (platform.isInstalled()) {
if (contributedPlatform.isInstalled()) {
throw new Exception("Platform is already installed!");
}
// Do not download already installed tools
List<ContributedTool> tools = new LinkedList<ContributedTool>(platform.getResolvedTools());
List<ContributedTool> tools = new LinkedList<ContributedTool>(contributedPlatform.getResolvedTools());
Iterator<ContributedTool> toolsIterator = tools.iterator();
while (toolsIterator.hasNext()) {
ContributedTool tool = toolsIterator.next();
DownloadableContribution downloadable = tool.getDownloadableContribution();
DownloadableContribution downloadable = tool.getDownloadableContribution(platform);
if (downloadable == null) {
throw new Exception(format(_("Tool {0} is not available for your operating system."), tool.getName()));
}
@ -96,7 +99,7 @@ public class ContributionInstaller {
// Download all
try {
// Download platform
downloader.download(platform, progress, _("Downloading boards definitions."));
downloader.download(contributedPlatform, progress, _("Downloading boards definitions."));
progress.stepDone();
// Download tools
@ -104,7 +107,7 @@ public class ContributionInstaller {
for (ContributedTool tool : tools) {
String msg = format(_("Downloading tools ({0}/{1})."), i, tools.size());
i++;
downloader.download(tool.getDownloadableContribution(), progress, msg);
downloader.download(tool.getDownloadableContribution(platform), progress, msg);
progress.stepDone();
}
} catch (InterruptedException e) {
@ -112,7 +115,7 @@ public class ContributionInstaller {
return errors;
}
ContributedPackage pack = platform.getParentPackage();
ContributedPackage pack = contributedPlatform.getParentPackage();
File packageFolder = new File(indexer.getPackagesFolder(), pack.getName());
// TODO: Extract to temporary folders and move to the final destination only
@ -126,12 +129,12 @@ public class ContributionInstaller {
progress.setStatus(format(_("Installing tools ({0}/{1})..."), i, tools.size()));
onProgress(progress);
i++;
DownloadableContribution toolContrib = tool.getDownloadableContribution();
DownloadableContribution toolContrib = tool.getDownloadableContribution(platform);
File destFolder = new File(toolsFolder, tool.getName() + File.separator + tool.getVersion());
destFolder.mkdirs();
assert toolContrib.getDownloadedFile() != null;
new ArchiveExtractor(BaseNoGui.getPlatform()).extract(toolContrib.getDownloadedFile(), destFolder, 1);
new ArchiveExtractor(platform).extract(toolContrib.getDownloadedFile(), destFolder, 1);
try {
executePostInstallScriptIfAny(destFolder);
} catch (IOException e) {
@ -145,12 +148,12 @@ public class ContributionInstaller {
// Unpack platform on the correct location
progress.setStatus(_("Installing boards..."));
onProgress(progress);
File platformFolder = new File(packageFolder, "hardware" + File.separator + platform.getArchitecture());
File destFolder = new File(platformFolder, platform.getParsedVersion());
File platformFolder = new File(packageFolder, "hardware" + File.separator + contributedPlatform.getArchitecture());
File destFolder = new File(platformFolder, contributedPlatform.getParsedVersion());
destFolder.mkdirs();
new ArchiveExtractor(BaseNoGui.getPlatform()).extract(platform.getDownloadedFile(), destFolder, 1);
platform.setInstalled(true);
platform.setInstalledFolder(destFolder);
new ArchiveExtractor(platform).extract(contributedPlatform.getDownloadedFile(), destFolder, 1);
contributedPlatform.setInstalled(true);
contributedPlatform.setInstalledFolder(destFolder);
progress.stepDone();
progress.setStatus(_("Installation completed!"));
@ -160,7 +163,7 @@ public class ContributionInstaller {
}
private void executePostInstallScriptIfAny(File folder) throws IOException {
Collection<File> postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new FileExecutablePredicate());
Collection<File> postInstallScripts = Collections2.filter(platform.postInstallScripts(folder), new FileExecutablePredicate());
if (postInstallScripts.isEmpty()) {
String[] subfolders = folder.list(new OnlyDirs());
@ -190,22 +193,22 @@ public class ContributionInstaller {
}
}
public List<String> remove(ContributedPlatform platform) {
if (platform == null || platform.isReadOnly()) {
public List<String> remove(ContributedPlatform contributedPlatform) {
if (contributedPlatform == null || contributedPlatform.isReadOnly()) {
return new LinkedList<String>();
}
List<String> errors = new LinkedList<String>();
FileUtils.recursiveDelete(platform.getInstalledFolder());
platform.setInstalled(false);
platform.setInstalledFolder(null);
FileUtils.recursiveDelete(contributedPlatform.getInstalledFolder());
contributedPlatform.setInstalled(false);
contributedPlatform.setInstalledFolder(null);
// Check if the tools are no longer needed
for (ContributedTool tool : platform.getResolvedTools()) {
for (ContributedTool tool : contributedPlatform.getResolvedTools()) {
if (indexer.isContributedToolUsed(tool)) {
continue;
}
DownloadableContribution toolContrib = tool.getDownloadableContribution();
DownloadableContribution toolContrib = tool.getDownloadableContribution(platform);
File destFolder = toolContrib.getInstalledFolder();
FileUtils.recursiveDelete(destFolder);
toolContrib.setInstalled(false);

View File

@ -46,6 +46,7 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Multimaps;
import org.apache.commons.compress.utils.IOUtils;
import processing.app.BaseNoGui;
import processing.app.Platform;
import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.debug.TargetPlatformException;
@ -65,10 +66,12 @@ public class ContributionsIndexer {
private final File packagesFolder;
private final File stagingFolder;
private final File preferencesFolder;
private final Platform platform;
private ContributionsIndex index;
public ContributionsIndexer(File preferencesFolder) {
public ContributionsIndexer(File preferencesFolder, Platform platform) {
this.preferencesFolder = preferencesFolder;
this.platform = platform;
packagesFolder = new File(preferencesFolder, "packages");
stagingFolder = new File(preferencesFolder, "staging" + File.separator + "packages");
}
@ -266,7 +269,7 @@ public class ContributionsIndexer {
if (tool == null) {
return;
}
DownloadableContribution contrib = tool.getDownloadableContribution();
DownloadableContribution contrib = tool.getDownloadableContribution(platform);
if (contrib == null) {
System.err.println(tool + " seems to have no downloadable contributions for your operating system, but it is installed in\n" + installationFolder);
return;

View File

@ -586,7 +586,7 @@ public class BaseNoGui {
}
static public void initPackages() throws Exception {
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform());
File indexFile = indexer.getIndexFile("package_index.json");
File defaultPackageJsonFile = new File(getContentFile("dist"), "package_index.json");
if (!indexFile.isFile() || (defaultPackageJsonFile.isFile() && defaultPackageJsonFile.lastModified() > indexFile.lastModified())) {
@ -797,7 +797,7 @@ public class BaseNoGui {
PreferencesData.removeAllKeysWithPrefix(prefix);
for (ContributedTool tool : indexer.getInstalledTools()) {
File installedFolder = tool.getDownloadableContribution().getInstalledFolder();
File installedFolder = tool.getDownloadableContribution(getPlatform()).getInstalledFolder();
if (installedFolder != null) {
PreferencesData.set(prefix + tool.getName() + ".path", installedFolder.getAbsolutePath());
PreferencesData.set(prefix + tool.getName() + "-" + tool.getVersion() + ".path", installedFolder.getAbsolutePath());