Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sourceSets{
srcDirs("src")
}
resources{
srcDirs("resources", listOf("languages", "fonts", "theme").map { "../build/shared/lib/$it" })
srcDirs("resources", listOf("fonts", "theme").map { "../build/shared/lib/$it" })
}
}
test{
Expand Down
54 changes: 31 additions & 23 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,30 @@

package processing.app;

import java.awt.*;
import java.awt.event.ActionListener;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;
import org.jetbrains.annotations.NotNull;
import processing.app.contrib.*;
import processing.app.tools.Tool;
import processing.app.ui.*;
import processing.app.ui.Toolkit;
import processing.core.*;
import processing.core.PApplet;
import processing.data.StringList;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.List;
import java.util.Map.Entry;

/**
* The base class for the main processing application.
* Primary role of this class is for platform identification and
Expand Down Expand Up @@ -127,9 +130,6 @@ public class Base {
// https://github.com/processing/processing/pull/2366
private JFileChooser openChooser;

static protected File sketchbookFolder;


static public void main(final String[] args) {
Messages.log("Starting Processing version" + VERSION_NAME + " revision "+ REVISION);
EventQueue.invokeLater(() -> {
Expand Down Expand Up @@ -1948,7 +1948,7 @@ public void populateSketchbookMenu(JMenu menu) {
new Thread(() -> {
boolean found = false;
try {
found = addSketches(menu, sketchbookFolder);
found = addSketches(menu, getSketchbookFolder());
} catch (Exception e) {
Messages.showWarning("Sketchbook Menu Error",
"An error occurred while trying to list the sketchbook.", e);
Expand Down Expand Up @@ -1987,7 +1987,7 @@ protected boolean addSketches(JMenu menu, File folder) {
if (folder.getName().equals("sdk")) {
// This could be Android's SDK folder. Let's double-check:
File suspectSDKPath = new File(folder.getParent(), folder.getName());
File expectedSDKPath = new File(sketchbookFolder, "android" + File.separator + "sdk");
File expectedSDKPath = new File(getSketchbookFolder(), "android" + File.separator + "sdk");
if (expectedSDKPath.getAbsolutePath().equals(suspectSDKPath.getAbsolutePath())) {
return false; // Most likely the SDK folder, skip it
}
Expand Down Expand Up @@ -2258,7 +2258,7 @@ static public File getToolsFolder() {
return Platform.getContentFile("tools");
}


static protected File sketchbookFolder;
static public void locateSketchbookFolder() {
// If a value is at least set, first check to see if the folder exists.
// If it doesn't, warn the user that the sketchbook folder is being reset.
Expand Down Expand Up @@ -2325,35 +2325,43 @@ static protected void makeSketchbookSubfolders() {


static public File getSketchbookFolder() {
var sketchbookPathOverride = System.getProperty("processing.sketchbook.folder");
if (sketchbookPathOverride != null && !sketchbookPathOverride.isEmpty()) {
return new File(sketchbookPathOverride);
}
if (sketchbookFolder == null) {
locateSketchbookFolder();
}
return sketchbookFolder;
}


static public File getSketchbookLibrariesFolder() {
return new File(sketchbookFolder, "libraries");
return new File(getSketchbookFolder(), "libraries");
}


static public File getSketchbookToolsFolder() {
return new File(sketchbookFolder, "tools");
return new File(getSketchbookFolder(), "tools");
}


static public File getSketchbookModesFolder() {
return new File(sketchbookFolder, "modes");
return new File(getSketchbookFolder(), "modes");
}


static public File getSketchbookExamplesFolder() {
return new File(sketchbookFolder, "examples");
return new File(getSketchbookFolder(), "examples");
}


static public File getSketchbookTemplatesFolder() {
return new File(sketchbookFolder, "templates");
return new File(getSketchbookFolder(), "templates");
}


@NotNull
static protected File getDefaultSketchbookFolder() {
File sketchbookFolder = null;
try {
Expand Down
69 changes: 41 additions & 28 deletions app/src/processing/app/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@

package processing.app;

import java.io.*;
import java.util.*;

import processing.core.PApplet;
import processing.data.StringList;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;


/**
* Internationalization (I18N) and Localization (L10N)
Expand Down Expand Up @@ -110,25 +115,17 @@ private Language() {

static private String[] listSupported() {
StringList supported = new StringList();
try {
File baseFolder = Base.getLibFile("languages");
String[] names = baseFolder.list();
if (names != null) {
for (String filename : names) {
if (filename.startsWith("PDE_") && filename.endsWith(".properties")) {
int dotIndex = filename.lastIndexOf(".properties");
String language = filename.substring(4, dotIndex);
supported.append(language);
var locales = Locale.getAvailableLocales();
var loader = Language.class.getClassLoader();
for (var locale : locales) {
var language = locale.toLanguageTag();
var baseFilename = "languages/PDE_" + language + ".properties";
var file = loader.getResource(baseFilename);
if (file == null) {
continue;
}
}
} else {
throw new IOException("Could not read list of files inside " + baseFolder);
supported.append(language);
}
} catch (IOException e) {
Messages.showError("Translation Trouble",
"There was a problem reading the language translations folder.\n" +
"You may need to reinstall, or report if the problem persists.", e);
}
return supported.toArray();
}

Expand Down Expand Up @@ -358,10 +355,12 @@ static class LanguageBundle {
// language files in the download (i.e. still would not help
// with adding new language codes.)

String baseFilename = "languages/PDE.properties";
String langFilename = "languages/PDE_" + language + ".properties";
var loader = Language.class.getClassLoader();

File baseFile = Base.getLibFile(baseFilename);
String baseFilename = "languages/PDE.properties";
String langFilename = "languages/PDE_" + language + ".properties";

var baseFile = loader.getResourceAsStream(baseFilename);
/*
// Also check to see if the user is working on localization,
// and has their own .properties files in their sketchbook.
Expand All @@ -372,7 +371,7 @@ static class LanguageBundle {
}
*/

File langFile = Base.getLibFile(langFilename);
var langFile = loader.getResourceAsStream(langFilename);
/*
File userLangFile = new File(Base.getSketchbookFolder(), langFilename);
if (userLangFile.exists()) {
Expand All @@ -384,11 +383,25 @@ static class LanguageBundle {
read(langFile);
}

void read(File additions) {
read(additions, false);
}
void read(File additions, boolean enforcePrefix) {
try {
InputStream in = PApplet.createInput(additions);
if (in != null) {
read(in, enforcePrefix);
in.close();
} else {
System.err.println("Unable to read " + additions);
}
} catch (IOException e) {
e.printStackTrace();
}
}

void read(InputStream additions) {
read(additions, false);
}

void read(File additions, boolean enforcePrefix) {
void read(InputStream additions, boolean enforcePrefix) {
String prefix = null;

String[] lines = PApplet.loadStrings(additions);
Expand Down
23 changes: 14 additions & 9 deletions app/src/processing/app/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@

package processing.app;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;

import com.sun.jna.platform.FileUtils;

import processing.app.platform.DefaultPlatform;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.data.StringDict;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Platform {
static DefaultPlatform inst;
Expand Down Expand Up @@ -83,8 +85,11 @@ static public boolean isAvailable() {
return inst != null;
}

static {
init();
}

static public void init() {
static public void init() {
try {
// Start with DefaultPlatform, but try to upgrade to a known platform
final String packageName = DefaultPlatform.class.getPackageName();
Expand Down
26 changes: 13 additions & 13 deletions app/src/processing/app/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

package processing.app;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.io.*;
import java.util.*;

import processing.app.ui.Toolkit;
import processing.core.*;
import processing.core.PApplet;
import processing.core.PConstants;

import java.awt.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;


/**
Expand Down Expand Up @@ -65,9 +65,11 @@ static public void init() {
// start by loading the defaults, in case something
// important was deleted from the user prefs
try {
// Name changed for 2.1b2 to avoid problems with users modifying or
// replacing the file after doing a search for "preferences.txt".
load(Base.getLibStream(DEFAULTS_FILE));
var defaultsStream = Preferences
.class
.getClassLoader()
.getResourceAsStream(DEFAULTS_FILE);
load(defaultsStream);
} catch (Exception e) {
Messages.showError(null, "Could not read default settings.\n" +
"You'll need to reinstall Processing.", e);
Expand Down Expand Up @@ -273,9 +275,7 @@ static public void save() {

static public String get(String attribute /*, String defaultValue */) {
if (!initialized) {
throw new RuntimeException(
"Tried reading preferences prior to initialization."
);
init();
}
return table.get(attribute);
}
Expand Down
25 changes: 25 additions & 0 deletions app/test/processing/app/SketchbookTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package processing.app

import kotlin.io.path.createTempDirectory
import kotlin.test.Test
import kotlin.test.assertEquals

class SketchbookTest {
Comment on lines +1 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a test a test!

@Test
fun sketchbookTest() {
val result = Base.getSketchbookFolder()
assert(result != null)
}

@Test
fun sketchbookIsOverridableTest() {
val directory = createTempDirectory("scaffolding")
val sketchbook = directory.resolve("sketchbook")
sketchbook.toFile().mkdirs()
val sketchbookAbs = sketchbook.toAbsolutePath().toString()
System.setProperty("processing.sketchbook.folder", sketchbookAbs)

val result = Base.getSketchbookFolder()
assertEquals(sketchbookAbs, result.absolutePath)
}
}
Loading