Updatefunktion und Dateimanager

Status
Nicht offen für weitere Antworten.

gentleL

Aktives Mitglied
Hallo wollte in mein Programm ein sozusagen Updatemechanismus reinbauen d.h der User kann gelegentlich mal gucken ob auf dem server eine höhere version drauf ist wenn ja saugen.

2 Punkt

Wenn ich etwas speichern will will ich das nicht unbedingt immer ins gleiche Verzeichnis haben und wollte fragen wie das funktioniert, dass ich auch verzeichnise wählen kann usw.

LG gentlel
 

HoaX

Top Contributor
was meinst du mit wählen? JFileChooser?

irgendwo gibst du doch an wo du hin schreibst ...
 

Wildcard

Top Contributor
Updates lassen sich am besten über den Deployment Mechanismus Java Web Start oder eine RCP Lösung wie Eclipse RCP durchführen.
 

gentleL

Aktives Mitglied
Leider benutze ich kein Eclipse weil der bei mir Fehler anzeigt...

JFileChooser ? Kann ich damit Ordner auswählen wo ich etwas hinspeichern kann ?

Ich guck mir mal den Deployment Mechanismus an :)

Danke erstmals
 

Wolfgang Lenhard

Bekanntes Mitglied
Hi,
ich möchte gerne meinen Code mal zur Diskussion stellen. Die Klasse, die das bei mir macht heißt UpdateManager und stellt einen Abgleich mit einer im Netz gespeicherten Datei her. Die Klasse verwendet noch eine andere (Message), die aber nur eine etwas stylischere JOptionPane darstellt, mit aufklappbaren Inhaltsbereich. Dieser Code-Teil muss also ersetzt werden.
Übergeben wird die aktuelle Programmversion, z. B. 1 5 2 und die Internetadresse als String. Die dortige Datei muss in der ersten Zeile folgendes Format haben: major\tmedium\tminor\tInternetadresse, wo neuere Version geladen werden kann. Alle weiteren Zeilen werden als Nachricht eingeblendet, Zeilen, die mit ### beginnen werden ignoriert.
Der UpdateManager:
Code:
import java.awt.Component;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JOptionPane;

import de.psychometrica.tools.gui.Message;

/**
 * @author Wolfgang Lenhard
 * 
 */
public class UpdateManager {
	private int[] internal = { -1, -1, -1 };
	private int[] external = { -1, -1, -1 };
	private String url = "";
	private boolean connection = false;
	private boolean dataIntegrity = false;
	private boolean isNewerVersion = false;
	private String updateMessage = "";

	/**
	 * Constructor for update manager
	 * 
	 * @param major
	 *            major version information of currently installed program
	 * @param medium
	 *            medium version information of currently installed program
	 * @param minor
	 *            minor version information of currently installed program
	 * @param location
	 *            location of update information file
	 */
	public UpdateManager(int major, int medium, int minor, String location) {
		internal[0] = major;
		internal[1] = medium;
		internal[2] = minor;
		connection = checkUpdates(location);
		if ((external[0] == -1) || (external[1] == -1) || (external[2] == -1)
				|| (url.length() == 0))
			dataIntegrity = false;
		else
			dataIntegrity = true;

		isNewerVersion = compareVersion();
	}

	/**
	 * @return if external version is higher than internal version
	 */
	private boolean compareVersion() {
		if (external[0] > internal[0]) {
			return true;
		} else if (external[0] == internal[0]) {
			if (external[1] > internal[1]) {
				return true;
			} else if (external[1] == internal[1]) {
				if (external[2] > internal[2])
					return true;
			}
		}
		return false;
	}

	/**
	 * check for update information at the given location
	 * 
	 * @param location
	 *            the location to look at
	 * @return if connection was successful
	 */
	private boolean checkUpdates(String location) {
		String nextLine;
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		String updateInfo = "";
		String message = "";
		try {
			url = new URL(location);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);
			updateInfo = buff.readLine();
			while (true) {
				nextLine = buff.readLine();
				if (nextLine != null) {
					if (!nextLine.startsWith("###"))
						message += nextLine;
				} else {
					break;
				}

			}

			String[] data = updateInfo.split("\t");
			external[0] = Integer.parseInt(data[0]);
			external[1] = Integer.parseInt(data[1]);
			external[2] = Integer.parseInt(data[2]);
			this.url = data[3];
			this.updateMessage = message;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e1) {
			dataIntegrity = false;
		}
		return true;
	}

	/**
	 * Open the system standard browser and navigate to the given homepage
	 * 
	 * @return the success (true)
	 */
	public boolean browseURL() {
		Desktop desktop = null;
		if (Desktop.isDesktopSupported()) {
			desktop = Desktop.getDesktop();
			try {
				URI uri = new URI(this.url);
				desktop.browse(uri);
			} catch (URISyntaxException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		} else
			return false;
	}

	/**
	 * Open the system standard browser and navigate to the given homepage
	 * 
	 * @param url
	 *            the page to navigate to
	 * @return the success (true)
	 */
	public boolean browseURL(String url) {
		Desktop desktop = null;
		if (Desktop.isDesktopSupported()) {
			desktop = Desktop.getDesktop();
			try {
				URI uri = new URI(url);
				desktop.browse(uri);
			} catch (URISyntaxException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		} else
			return false;
	}

	/**
	 * @return the connection
	 */
	public boolean isConnection() {
		return connection;
	}

	/**
	 * @return the dataIntegrity
	 */
	public boolean isDataIntegrity() {
		return dataIntegrity;
	}

	/**
	 * @return the isNewerVersion
	 */
	public boolean isNewerVersion() {
		return isNewerVersion;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * Display the update message
	 * 
	 * @param frame
	 *            The component to display message on
	 * @param HOMEPAGE
	 *            The homepage url
	 */
	public void message(Component frame, String HOMEPAGE) {
		if (!isConnection()) {
			Message
					.displayAlert(
							frame,
							"Keine Verbindung",
							"<html>Die Update-Informationen konnten nicht abgerufen werden."
									+ " Weiterführende Informationen finden Sie im Internet unter\n"
									+ HOMEPAGE);
		} else if (!isDataIntegrity()) {
			int r = JOptionPane
					.showConfirmDialog(
							frame,
							"Die Update-Informationen konnten nicht ermittelt werden.\n"
									+ "Weiterführende Informationen finden Sie im Internet unter\n"
									+ HOMEPAGE
									+ "\n\nSoll diese Seite nun in Ihrem Browser aufgerufen werden?",
							"Fehler!", JOptionPane.YES_NO_OPTION,
							JOptionPane.INFORMATION_MESSAGE);
			if (r == JOptionPane.YES_OPTION) {
				boolean success = browseURL(HOMEPAGE);
				if (!success) {
					JOptionPane.showMessageDialog(frame,
							"Der Browser konnte nicht gestartet werden.\n"
									+ "Das Update finden Sie unter " + getUrl()
									+ ".");
				}
			}
		} else if (isNewerVersion()) {
			int r = JOptionPane.CANCEL_OPTION;

			if (updateMessage.length() == 0) {
				r = JOptionPane.showConfirmDialog(frame,
						"Ein Programm-Update (Version " + external[0] + "."
								+ external[1] + "." + external[2]
								+ ") ist verfügbar.\n"
								+ "Soll die Update-Seite (" + getUrl()
								+ ")\nnun in Ihrem Browser aufgerufen werden?",
						"Update verfügbar", JOptionPane.YES_NO_OPTION,
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				r = Message.displayMessage(frame, "Update verfügbar",
						"Ein Programm-Update (Version " + external[0] + "."
								+ external[1] + "." + external[2]
								+ ") ist verfügbar. "
								+ "Soll die Update-Seite\n(" + getUrl()
								+ ") nun in Ihrem Browser\naufgerufen werden?",
						updateMessage);
			}

			if (r == JOptionPane.YES_OPTION) {
				boolean success = browseURL();
				if (!success) {
					JOptionPane.showMessageDialog(frame,
							"Der Browser konnte nicht gestartet werden.\n"
									+ "Das Update finden Sie unter\n"
									+ getUrl() + ".");
				}
			}
		} else if (!isNewerVersion()) {
			JOptionPane.showMessageDialog(frame,
					"Die installierte Programmversion ist aktuell.");
		}

	}
}

Was letztendlich auf der Homepage mit den Update-Infos zu finden ist, das kann jeder selbst entscheiden. Denkbar wäre auch, eine Datei direkt zu laden und zu installieren. Ich wollte es zunächst aber möglichst offen halten. Verbesserungsvorschläge sind herzlich willkommen.

Viele Grüße,
Wolfgang
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben