1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00
This commit is contained in:
Ricardo JL Rufino 2024-11-28 04:19:13 +01:00 committed by GitHub
commit 01192ed6e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 89 additions and 1 deletions

View File

@ -40,13 +40,17 @@ import processing.app.PreferencesData;
import processing.app.Theme; import processing.app.Theme;
import processing.app.Theme.ZippedTheme; import processing.app.Theme.ZippedTheme;
import processing.app.helpers.FileUtils; import processing.app.helpers.FileUtils;
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;
import processing.app.legacy.PApplet; import processing.app.legacy.PApplet;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
@ -792,7 +796,10 @@ public class Preferences extends javax.swing.JDialog {
if (comboTheme.getSelectedIndex() == 0) { if (comboTheme.getSelectedIndex() == 0) {
PreferencesData.set("theme.file", ""); PreferencesData.set("theme.file", "");
} else { } else {
PreferencesData.set("theme.file", ((ZippedTheme) comboTheme.getSelectedItem()).getKey()); Object selectedItem = comboTheme.getSelectedItem();
if(selectedItem instanceof ZippedTheme) {
PreferencesData.set("theme.file", ((ZippedTheme) selectedItem).getKey());
}
} }
String newSizeText = fontSizeField.getText(); String newSizeText = fontSizeField.getText();
@ -870,6 +877,22 @@ public class Preferences extends javax.swing.JDialog {
} }
} }
// Allow install new themes.
String installItem = "-- " + tr( "Install from Zip" + " --");
comboTheme.addItem(installItem);
comboTheme.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object item = comboTheme.getSelectedItem();
if (item != null && item.toString().startsWith(installItem)) {
SwingUtilities.invokeLater(() -> comboTheme.getUI().setPopupVisible(comboTheme, false));
handleInstallTheme();
}
}
});
Font editorFont = PreferencesData.getFont("editor.font"); Font editorFont = PreferencesData.getFont("editor.font");
fontSizeField.setText(String.valueOf(editorFont.getSize())); fontSizeField.setText(String.valueOf(editorFont.getSize()));
@ -975,4 +998,40 @@ public class Preferences extends javax.swing.JDialog {
autoProxyFieldsSetEnabled(false); autoProxyFieldsSetEnabled(false);
manualProxyFieldsSetEnabled(false); manualProxyFieldsSetEnabled(false);
} }
public void handleInstallTheme() {
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
fileChooser.setDialogTitle(tr("Select a zip file containing the theme you'd like to add"));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(new FileNameExtensionFilter(tr("ZIP files"), "zip"));
Dimension preferredSize = fileChooser.getPreferredSize();
fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
File sourceFile = fileChooser.getSelectedFile();
if (!sourceFile.isDirectory()) {
try {
ZippedTheme theme = Theme.install(sourceFile);
if(theme != null) {
comboTheme.addItem(theme);
comboTheme.setSelectedItem(theme);
}
} catch (IOException e) {
base.getActiveEditor().statusError(e);
return;
}
}
}
} }

View File

@ -47,6 +47,8 @@ import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -66,6 +68,7 @@ import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder; import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import processing.app.helpers.OSUtils; import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesHelper; import processing.app.helpers.PreferencesHelper;
import processing.app.helpers.PreferencesMap; import processing.app.helpers.PreferencesMap;
@ -724,4 +727,30 @@ public class Theme {
return null; return null;
} }
} }
/**
* Intalll theme in skecthboot
* @param zipTheme
*/
static public ZippedTheme install(File zipTheme) throws IOException{
// Validate...
ZippedTheme theme = ZippedTheme.load(NAMESPACE_USER, zipTheme);
if(theme == null) {
throw new IOException(format(tr("Error loading theme: {0}"), zipTheme.getAbsolutePath()));
}
// Create themes folder.
File themesFolder = new File(PreferencesData.get("sketchbook.path"), "theme");
if (!themesFolder.exists()) {
themesFolder.mkdir();
}
// Copy
File dest = new File(themesFolder, zipTheme.getName());
Files.copy(zipTheme.toPath(), dest.toPath(),StandardCopyOption.REPLACE_EXISTING);
return theme;
}
} }