Skip to content

Commit

Permalink
Merge pull request #5 from zohaibanwer984/development
Browse files Browse the repository at this point in the history
Add Check for Update and Dependencies Update
  • Loading branch information
zohaibanwer984 authored Dec 3, 2024
2 parents 2b42354 + 3bfcb49 commit 2a80c3d
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launcher",
"request": "launch",
"mainClass": "com.zam.Launcher",
"projectName": "bitcode"
}
]
}
2 changes: 1 addition & 1 deletion App.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#Sat Jul 27 17:57:27 PKT 2024
BracketMatching=true
CodeFolding=true
DEV=true
FontFamily=Consolas
HighlightCurrentLine=true
LineNumbers=true
LineWrap=false
editorTheme=default
fontSize=16
lookAndFeel=com.formdev.flatlaf.FlatLightLaf
version=1.0.4
4 changes: 2 additions & 2 deletions src/main/java/com/zam/dialogboxes/AboutDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* ```
*
* @author Muhammed Zohaib
* @version 1.0
* @version 1.0.4
* @since 2024-01-07
*/
public class AboutDialog extends JDialog {
Expand Down Expand Up @@ -70,7 +70,7 @@ public AboutDialog(JFrame parent) {
GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));

titleLabel.setText("<html><h1>BitCode</h1>Version 1.0.2.0</html>");
titleLabel.setText("<html><h1>BitCode</h1>Version "+ App.APP_VERSION +"</html>");
titleLabel.setFont(titleLabel.getFont().deriveFont(titleLabel.getFont().getSize() + 5f));
contentPane.add(titleLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/zam/menubar/ViewMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingWorker;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

import com.zam.dialogboxes.ThemeChanger;
import com.zam.ui.App;
import com.zam.utils.UpdateChecker;

/**
* Custom menu handler for the View menu in BitCode IDE.
Expand All @@ -31,14 +33,15 @@
* ```
*
* @author Muhammed Zohaib
* @version 1.0.3
* @version 1.0.4
* @since 2023-12-12
*/
public class ViewMenuHandler extends JMenu {

private final JMenuItem increaseFontSizeItem = new JMenuItem("Increase Font Size");
private final JMenuItem decreaseFontSizeItem = new JMenuItem("Decrease Font Size");
private final JMenuItem changeThemeItem = new JMenuItem("Change Theme");
private final JMenuItem checkUpdateItem = new JMenuItem("Check for Update");

final private App mainApp;

Expand All @@ -56,6 +59,8 @@ public ViewMenuHandler(String title, App parent) {
add(increaseFontSizeItem);
add(decreaseFontSizeItem);
add(changeThemeItem);
addSeparator(); // Add a separator line
add(checkUpdateItem);

// Add action listeners and accelerators
configureMenuItems();
Expand All @@ -76,6 +81,19 @@ private void configureMenuItems() {
// Add an action listener to the "Change Theme" menu item
changeThemeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
changeThemeItem.addActionListener(e -> openThemeDialog());

// Add an action listener to the "Check for Update" menu item
checkUpdateItem.addActionListener(e -> {
SwingWorker<Void, Void> worker = new SwingWorker<>() {
@Override
protected Void doInBackground() {
UpdateChecker.checkForUpdate(mainApp);
return null;
}
};
worker.execute();
});

}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/zam/ui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@
* - Providing the main method to launch the application.
*
* @author Muhammed Zohaib
* @version 1.0.3
* @version 1.0.4
* @since 2023-11-29
*/
public class App extends JFrame {

// Constants
private static final double SCREEN_WIDTH_RATIO = 0.5;
private static final double SCREEN_HEIGHT_RATIO = 0.65;
public static final String APP_VERSION = "1.0.4";

// Private Componnets
private final JSplitPane splitPane;
Expand Down
122 changes: 122 additions & 0 deletions src/main/java/com/zam/utils/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.zam.utils;

import javax.swing.*;
import com.zam.ui.App;
import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

/**
* Utility class for checking updates for the BitCode IDE application.
*
* This class fetches the latest version information from a remote properties file
* and compares it with the current application version. If a new version is
* available, it prompts the user to download it.
*
* Usage:
* - Call `UpdateChecker.checkForUpdate(App parent)` from your application.
*
* Example:
* ```java
* UpdateChecker.checkForUpdate(mainApp);
* ```
*
* @author Muhammed Zohaib
* @version 1.0.4
* @since 2024-12-03
*/
public class UpdateChecker {

/**
* URL of the remote properties file containing version information.
*/
private static final String UPDATE_URL = "https://raw.githubusercontent.com/zohaibanwer984/BitCode-Java-IDE/development/App.properties";

/**
* Checks for updates by comparing the current application version with the latest version available online.
*
* @param parent The parent component for displaying dialog boxes.
*/
public static void checkForUpdate(App parent) {
try {
// Fetch the properties file from the remote server
Properties remoteProperties = fetchRemoteProperties();

// Retrieve the latest version from the remote properties
String latestVersion = remoteProperties.getProperty("version");
String downloadUrl = "https://sourceforge.net/projects/bitcode-java-ide/";

// Compare versions and notify the user
if (latestVersion != null && !App.APP_VERSION.equals(latestVersion)) {
promptForUpdate(parent, latestVersion, downloadUrl);
} else {
JOptionPane.showMessageDialog(
parent,
"You are already using the latest version.",
"No Updates",
JOptionPane.INFORMATION_MESSAGE
);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(
parent,
"Failed to check for updates: " + e.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE
);
e.printStackTrace();
}
}

/**
* Fetches the remote properties file containing version information.
*
* @return The loaded Properties object.
* @throws IOException If an I/O error occurs during the fetch.
*/
private static Properties fetchRemoteProperties() throws IOException, URISyntaxException {
Properties remoteProperties = new Properties();
HttpURLConnection connection = (HttpURLConnection) new URI(UPDATE_URL).toURL().openConnection();
connection.setRequestMethod("GET");

// Read the response content from the server
StringBuilder responseContent = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
responseContent.append(line).append("\n");
}
}

// Load properties from the fetched content
try (InputStream inputStream = new ByteArrayInputStream(responseContent.toString().getBytes())) {
remoteProperties.load(inputStream);
}

return remoteProperties;
}

/**
* Prompts the user with an update dialog if a new version is available.
*
* @param parent The parent component for the dialog.
* @param latestVersion The latest version available online.
* @param downloadUrl The URL to download the new version.
* @throws Exception If an error occurs while opening the download link.
*/
private static void promptForUpdate(Component parent, String latestVersion, String downloadUrl) throws Exception {
int choice = JOptionPane.showConfirmDialog(
parent,
"A new version (" + latestVersion + ") is available. Do you want to download it?",
"Update Available",
JOptionPane.YES_NO_OPTION
);

if (choice == JOptionPane.YES_OPTION) {
Desktop.getDesktop().browse(new URI(downloadUrl));
}
}
}

0 comments on commit 2a80c3d

Please sign in to comment.