Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit 97397fc

Browse files
author
jcbvm
committed
Added ability to check for new version.
1 parent 85aada6 commit 97397fc

File tree

6 files changed

+112
-4
lines changed

6 files changed

+112
-4
lines changed

src/main/java/com/jvms/i18neditor/Editor.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.jvms.i18neditor;
22

33
import java.awt.BorderLayout;
4+
import java.awt.Component;
45
import java.awt.Container;
6+
import java.awt.Desktop;
57
import java.awt.Dimension;
8+
import java.awt.Font;
69
import java.awt.Image;
710
import java.awt.event.KeyAdapter;
811
import java.awt.event.KeyEvent;
@@ -21,6 +24,7 @@
2124
import javax.swing.Box;
2225
import javax.swing.BoxLayout;
2326
import javax.swing.ImageIcon;
27+
import javax.swing.JEditorPane;
2428
import javax.swing.JFileChooser;
2529
import javax.swing.JFrame;
2630
import javax.swing.JLabel;
@@ -30,6 +34,7 @@
3034
import javax.swing.JSplitPane;
3135
import javax.swing.SwingUtilities;
3236
import javax.swing.UIManager;
37+
import javax.swing.event.HyperlinkEvent;
3338
import javax.swing.event.TreeSelectionEvent;
3439
import javax.swing.event.TreeSelectionListener;
3540

@@ -41,6 +46,8 @@
4146
import com.jvms.i18neditor.swing.JFileDrop;
4247
import com.jvms.i18neditor.swing.JScrollablePanel;
4348
import com.jvms.i18neditor.util.ExtendedProperties;
49+
import com.jvms.i18neditor.util.GithubRepoUtils;
50+
import com.jvms.i18neditor.util.GithubRepoUtils.GithubReleaseData;
4451
import com.jvms.i18neditor.util.MessageBundle;
4552
import com.jvms.i18neditor.util.Resources;
4653
import com.jvms.i18neditor.util.TranslationKeys;
@@ -57,6 +64,7 @@ public class Editor extends JFrame {
5764
public final static String TITLE = "i18n Editor";
5865
public final static String VERSION = "0.6.0";
5966
public final static String COPYRIGHT_YEAR = "2016";
67+
public final static String GITHUB_REPO = "jcbvm/ember-i18n-editor";
6068
public final static int DEFAULT_WIDTH = 1024;
6169
public final static int DEFAULT_HEIGHT = 768;
6270

@@ -223,10 +231,18 @@ public void showMessage(String title, String message) {
223231
showMessageDialog(title, message, JOptionPane.PLAIN_MESSAGE);
224232
}
225233

234+
public void showMessage(String title, Component component) {
235+
showMessageDialog(title, component, JOptionPane.PLAIN_MESSAGE);
236+
}
237+
226238
public void showMessageDialog(String title, String message, int type) {
227239
JOptionPane.showMessageDialog(this, message, title, type);
228240
}
229241

242+
public void showMessageDialog(String title, Component component, int type) {
243+
JOptionPane.showMessageDialog(this, component, title, type);
244+
}
245+
230246
public void showImportDialog() {
231247
String path = null;
232248
if (resourcesDir != null) {
@@ -344,7 +360,7 @@ public void showFindTranslationDialog() {
344360

345361
public void showAboutDialog() {
346362
showMessage(MessageBundle.get("dialogs.about.title", TITLE),
347-
"<html><body style=\"text-align:center;width:200px;\"><br>" +
363+
"<html><body style=\"text-align:center;width:200px;\">" +
348364
"<span style=\"font-weight:bold;font-size:1.2em;\">" + TITLE + "</span><br>" +
349365
"v" + VERSION + "<br><br>" +
350366
"(c) Copyright " + COPYRIGHT_YEAR + "<br>" +
@@ -353,6 +369,31 @@ public void showAboutDialog() {
353369
"</body></html>");
354370
}
355371

372+
public void showVersionDialog() {
373+
GithubReleaseData data = GithubRepoUtils.getLatestRelease(GITHUB_REPO);
374+
String content = "";
375+
if (data != null && !VERSION.equals(data.getTagName())) {
376+
content = MessageBundle.get("dialogs.version.new", data.getTagName()) + "<br>" +
377+
"<a href=\"" + data.getHtmlUrl() + "\">" + MessageBundle.get("dialogs.version.link") + "</a>";
378+
} else {
379+
content = MessageBundle.get("dialogs.version.uptodate");
380+
}
381+
Font font = getFont();
382+
JEditorPane pane = new JEditorPane("text/html", "<html><body style=\"font-family:" + font.getFamily() + ";font-size:" + font.getSize() + "pt;text-align:center;width:200px;\">" + content + "</body></html>");
383+
pane.addHyperlinkListener(e -> {
384+
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
385+
try {
386+
Desktop.getDesktop().browse(e.getURL().toURI());
387+
} catch (Exception e1) {
388+
//
389+
}
390+
}
391+
});
392+
pane.setBackground(getBackground());
393+
pane.setEditable(false);
394+
showMessage(MessageBundle.get("dialogs.version.title"), pane);
395+
}
396+
356397
public boolean closeCurrentSession() {
357398
if (isDirty()) {
358399
int result = JOptionPane.showConfirmDialog(this,

src/main/java/com/jvms/i18neditor/EditorMenu.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ private void setupUI() {
171171

172172
JMenu helpMenu = new JMenu(MessageBundle.get("menu.help.title"));
173173
helpMenu.setMnemonic(MessageBundle.getMnemonic("menu.help.vk"));
174+
175+
JMenuItem versionMenuItem = new JMenuItem(MessageBundle.get("menu.help.version.title"), MessageBundle.getMnemonic("menu.help.version.vk"));
176+
versionMenuItem.addActionListener(e -> editor.showVersionDialog());
177+
helpMenu.add(versionMenuItem);
178+
174179
JMenuItem aboutMenuItem = new JMenuItem(MessageBundle.get("menu.help.about.title", Editor.TITLE), MessageBundle.getMnemonic("menu.help.about.vk"));
175180
aboutMenuItem.addActionListener(e -> editor.showAboutDialog());
176181
helpMenu.add(aboutMenuItem);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.jvms.i18neditor.util;
2+
3+
import java.io.InputStreamReader;
4+
import java.net.HttpURLConnection;
5+
import java.net.URL;
6+
7+
import com.google.common.base.Charsets;
8+
import com.google.gson.Gson;
9+
import com.google.gson.annotations.SerializedName;
10+
11+
/**
12+
* This class provides utility functions for retrieving Github repo data.
13+
*
14+
* @author Jacob
15+
*/
16+
public final class GithubRepoUtils {
17+
private final static Gson gson = new Gson();
18+
19+
public static GithubReleaseData getLatestRelease(String repo) {
20+
HttpURLConnection connection = null;
21+
try {
22+
URL url = new URL("https://api.github.com/repos/" + repo + "/releases/latest");
23+
connection = (HttpURLConnection)url.openConnection();
24+
try (InputStreamReader reader = new InputStreamReader(connection.getInputStream(), Charsets.UTF_8)) {
25+
return gson.fromJson(reader, GithubReleaseData.class);
26+
}
27+
} catch (Exception e) {
28+
return null;
29+
} finally {
30+
if (connection != null) {
31+
connection.disconnect();
32+
}
33+
}
34+
}
35+
36+
public class GithubReleaseData {
37+
@SerializedName("tag_name")
38+
private String tagName;
39+
@SerializedName("html_url")
40+
private String htmlUrl;
41+
42+
public String getTagName() {
43+
return tagName;
44+
}
45+
46+
public String getHtmlUrl() {
47+
return htmlUrl;
48+
}
49+
}
50+
}

src/main/java/com/jvms/i18neditor/util/MessageBundle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.ResourceBundle;
66

77
/**
8-
* This class retrieves translations from a resource bundle.<br>
8+
* This class provides utility functions for retrieving translations from a resource bundle.<br>
99
* By default it loads translations from {@value #RESOURCES_PATH}.
1010
*
1111
* <p>The locale used is the current value of the default locale for this instance of the Java Virtual Machine.</p>

src/main/resources/bundles/messages.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ dialogs.translation.find.title = Find Translation
2020
dialogs.translation.rename.error = The key you entered is invalid.
2121
dialogs.translation.rename.text = Enter new key:
2222
dialogs.translation.rename.title = Rename Translation
23+
dialogs.version.link = Click here to download
24+
dialogs.version.new = A new version is available: v{0}
25+
dialogs.version.title = Check for Version
26+
dialogs.version.uptodate = You are using the latest version.
2327

2428
menu.edit.add.locale.es6.title = ES6 Format...
2529
menu.edit.add.locale.es6.vk = E
@@ -52,8 +56,10 @@ menu.file.vk = F
5256
menu.help.about.title = About {0}
5357
menu.help.about.vk = A
5458
menu.help.title = Help
59+
menu.help.version.title = Check for Version
60+
menu.help.version.vk = C
5561
menu.help.vk = H
56-
menu.settings.minify.title = Minify translations on save
62+
menu.settings.minify.title = Minify Translations on Save
5763
menu.settings.title = Settings
5864
menu.settings.vk = S
5965
menu.view.collapse.title = Collapse All Translations

src/main/resources/bundles/messages_nl.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ dialogs.translation.find.title = Vertaling Zoeken
2020
dialogs.translation.rename.error = De opgegeven naam is niet geldig.
2121
dialogs.translation.rename.text = Nieuwe naam:
2222
dialogs.translation.rename.title = Vertaling Hernoemen
23+
dialogs.version.link = Klik hier om te downloaden
24+
dialogs.version.new = Een nieuwe versie is beschikbaar: v{0}
25+
dialogs.version.title = Controleer Versie
26+
dialogs.version.uptodate = Je gebruikt de nieuwste versie.
2327

2428
menu.edit.add.locale.es6.title = ES6 Formaat...
2529
menu.edit.add.locale.es6.vk = E
@@ -52,8 +56,10 @@ menu.file.vk = B
5256
menu.help.about.title = Over {0}
5357
menu.help.about.vk = O
5458
menu.help.title = Help
59+
menu.help.version.title = Controleer op Update
60+
menu.help.version.vk = C
5561
menu.help.vk = H
56-
menu.settings.minify.title = Comprimeer vertalingen bij opslaan
62+
menu.settings.minify.title = Comprimeer Vertalingen bij Opslaan
5763
menu.settings.title = Instellingen
5864
menu.settings.vk = I
5965
menu.view.collapse.title = Alle Vertalingen Invouwen

0 commit comments

Comments
 (0)