Skip to content

Commit

Permalink
Merge pull request #32 from xpdota/update-notifier
Browse files Browse the repository at this point in the history
Notification for an update
  • Loading branch information
xpdota authored Mar 6, 2022
2 parents 9223635 + 3c6b736 commit 54eabc3
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 15 deletions.
5 changes: 3 additions & 2 deletions xivsupport/src/main/java/gg/xp/xivsupport/gui/GuiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import gg.xp.xivsupport.gui.tables.renderers.NameJobRenderer;
import gg.xp.xivsupport.gui.tabs.AdvancedTab;
import gg.xp.xivsupport.gui.tabs.LibraryTab;
import gg.xp.xivsupport.gui.tabs.SmartTabbedPane;
import gg.xp.xivsupport.gui.util.CatchFatalError;
import gg.xp.xivsupport.gui.util.GuiUtil;
import gg.xp.xivsupport.models.XivCombatant;
Expand Down Expand Up @@ -129,7 +130,7 @@ public GuiMain(EventMaster master, MutablePicoContainer container) {
replay = container.getComponent(ReplayController.class);
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Triggevent");
tabPane = new JTabbedPane();
tabPane = new SmartTabbedPane();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
Expand Down Expand Up @@ -407,7 +408,7 @@ private class PluginSettingsPanel extends JPanel {
public PluginSettingsPanel() {
// super("Plugin Settings");
setLayout(new BorderLayout());
tabPanel = new JTabbedPane(SwingConstants.LEFT);
tabPanel = new SmartTabbedPane(SwingConstants.LEFT);
add(tabPanel);
exs.submit(this::getAndAddTabs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AdvancedTab extends JTabbedPane implements Refreshable {
public class AdvancedTab extends SmartTabbedPane implements Refreshable, TabAware {

private static final ExecutorService exs = Executors.newCachedThreadPool(Threading.namedDaemonThreadFactory("AdvancedTab"));
private final KeyValueDisplaySet displayed;
Expand All @@ -52,7 +52,7 @@ public AdvancedTab(PicoContainer container) {
JPanel statsPanel = new TitleBorderFullsizePanel("Stats");
statsPanel.setLayout(new BoxLayout(statsPanel, BoxLayout.PAGE_AXIS));
JButton refreshButton = new JButton("Refresh");
refreshButton.addActionListener(e -> refresh());
refreshButton.addActionListener(e -> recheckTabs());
List<KeyValuePairDisplay<?, ?>> leftItems = List.of(
new KeyValuePairDisplay<>(
"Duration",
Expand Down Expand Up @@ -132,7 +132,7 @@ public AdvancedTab(PicoContainer container) {
JButton forceGcButton = new JButton("Force GC");
forceGcButton.addActionListener(l -> {
exs.submit(System::gc);
exs.submit(() -> SwingUtilities.invokeLater(this::refresh));
exs.submit(() -> SwingUtilities.invokeLater(this::recheckTabs));
});
memoryPanel.add(new WrapperPanel(forceGcButton));
}
Expand Down Expand Up @@ -224,12 +224,11 @@ public AdvancedTab(PicoContainer container) {
{
addTab("Java", new JavaPanel());
}
refresh();
new Timer(5000, l -> this.refresh()).start();
recheckTabs();
new Timer(5000, l -> this.recheckTabs()).start();
}

private JTextArea makeTextArea() {
// JTextArea textArea = new ReadOnlyText("");
JTextArea textArea = new JTextArea(1, 15);
textArea.setPreferredSize(textArea.getPreferredSize());
return textArea;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gg.xp.xivsupport.gui.tabs;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SmartTabbedPane extends JTabbedPane implements TabAware {

private static final Color warningTabColor = new Color(62, 27, 27);

public SmartTabbedPane() {
}

public SmartTabbedPane(int tabPlacement) {
super(tabPlacement);
}

public SmartTabbedPane(int tabPlacement, int tabLayoutPolicy) {
super(tabPlacement, tabLayoutPolicy);
}

public List<Component> getTabs() {
int count = getTabCount();
List<Component> out = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
out.add(getComponent(i));
}
return out;
}

@Override
public boolean hasWarning() {
return Arrays.stream(getComponents()).anyMatch(tab -> (tab instanceof TabAware aware && aware.hasWarning()));
}

public void recheckTabs() {
SwingUtilities.invokeLater(this::repaint);
notifyParents();
}

@Override
public Color getBackgroundAt(int index) {
Component comp = getComponentAt(index);
if (comp instanceof TabAware tabAware && tabAware.hasWarning()) {
return warningTabColor;
}
return super.getBackgroundAt(index);
}
}
26 changes: 26 additions & 0 deletions xivsupport/src/main/java/gg/xp/xivsupport/gui/tabs/TabAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gg.xp.xivsupport.gui.tabs;

import java.awt.*;

public interface TabAware {

default boolean hasWarning() {
return false;
}

Component getParent();

default void notifyParents() {
Component parent = getParent();
while (parent != null) {
if (parent instanceof SmartTabbedPane stp) {
stp.recheckTabs();
break;
}
else {
parent = parent.getParent();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.xp.xivsupport.gui.tabs;

import gg.xp.reevent.scan.ScanMe;
import gg.xp.xivsupport.gui.TitleBorderFullsizePanel;
import gg.xp.xivsupport.gui.util.GuiUtil;
import gg.xp.xivsupport.persistence.PersistenceProvider;
Expand All @@ -19,14 +20,24 @@
import java.util.concurrent.Executors;
import java.util.function.Consumer;

public class UpdatesPanel extends TitleBorderFullsizePanel {
public class UpdatesPanel extends TitleBorderFullsizePanel implements TabAware {
private static final Logger log = LoggerFactory.getLogger(UpdatesPanel.class);
private static final String propsOverrideFileName = "update.properties";
private static final ExecutorService exs = Executors.newCachedThreadPool(Threading.namedDaemonThreadFactory("UpdateCheck"));
private volatile UpdateCheckStatus updateCheckStatus = UpdateCheckStatus.NOT_STARTED;
private JLabel checkingLabel;
private File installDir;
private File propsOverride;
private PersistenceProvider updatePropsFilePers;
private static final boolean isIde = Platform.isInIDE();

private enum UpdateCheckStatus {
NOT_STARTED,
IN_PROGRESS,
UPDATE_AVAILABLE,
NO_UPDATE,
ERROR
}

public UpdatesPanel() {
super("Updates");
Expand Down Expand Up @@ -95,17 +106,41 @@ public UpdatesPanel() {
add(new JPanel(), c);
}

private void setUpdateCheckStatus(UpdateCheckStatus updateCheckStatus) {
this.updateCheckStatus = updateCheckStatus;
checkingLabel.setText(
switch (updateCheckStatus) {
case NOT_STARTED -> "Update Status";
case IN_PROGRESS -> "Checking for updates...";
case NO_UPDATE -> "It looks like you are up to date.";
case UPDATE_AVAILABLE -> "There are updates available!";
case ERROR -> "Automatic Check Failed, but you can try updating anyway. Perhaps the branch does not exist?";
}
);
if (updateCheckStatus != UpdateCheckStatus.IN_PROGRESS) {
notifyParents();
}
}

@Override
public boolean hasWarning() {
if (isIde) {
return false;
}
return updateCheckStatus == UpdateCheckStatus.UPDATE_AVAILABLE || updateCheckStatus == UpdateCheckStatus.ERROR;
}

private void doUpdateCheckInBackground() {
exs.submit(() -> {
checkingLabel.setText("Checking for updates...");
setUpdateCheckStatus(UpdateCheckStatus.IN_PROGRESS);
try {
Class<?> clazz = Class.forName("gg.xp.xivsupport.gui.Update");
boolean result = (boolean) clazz.getMethod("justCheck", Consumer.class).invoke(null, (Consumer<String>) s -> log.info("From Updater: {}", s));
if (result) {
checkingLabel.setText("There are updates available!");
setUpdateCheckStatus(UpdateCheckStatus.UPDATE_AVAILABLE);
}
else {
checkingLabel.setText("It looks like you are up to date.");
setUpdateCheckStatus(UpdateCheckStatus.NO_UPDATE);
}
}
catch (Throwable firstError) {
Expand All @@ -114,14 +149,14 @@ private void doUpdateCheckInBackground() {
Class<?> clazz = Class.forName("gg.xp.xivsupport.gui.UpdateCopyForLegacyMigration");
boolean result = (boolean) clazz.getMethod("justCheck", Consumer.class).invoke(null, (Consumer<String>) s -> log.info("From Updater: {}", s));
if (result) {
checkingLabel.setText("There are updates available!");
setUpdateCheckStatus(UpdateCheckStatus.UPDATE_AVAILABLE);
}
else {
checkingLabel.setText("It looks like you are up to date.");
setUpdateCheckStatus(UpdateCheckStatus.NO_UPDATE);
}
} catch (Throwable e) {
log.error("Error checking for updates - you may not have a recent enough version.", e);
checkingLabel.setText("Automatic Check Failed, but you can try updating anyway. Perhaps the branch does not exist?");
setUpdateCheckStatus(UpdateCheckStatus.ERROR);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows");
}

public static boolean isInIDE() {
File jarLocation;
try {
jarLocation = new File(UpdatesPanel.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
return jarLocation.isDirectory();
}
catch (Throwable e) {
return false;
}
}

public static File getInstallDir() {
File jarLocation;
try {
Expand Down

0 comments on commit 54eabc3

Please sign in to comment.