Added the URLHandler which makes the URL's in the message of the day clickable.

At the moment when there are 2 URL's in 1 message, it only makes the last one clickable.
This commit is contained in:
Danny Plenge 2018-03-28 13:49:04 +02:00
parent 7d7cd64b1e
commit 98c2b171de
2 changed files with 124 additions and 2 deletions

View file

@ -60,6 +60,7 @@ import mage.client.util.ButtonColumn;
import mage.client.util.GUISizeHelper;
import mage.client.util.IgnoreList;
import mage.client.util.MageTableRowSorter;
import mage.client.util.URLHandler;
import mage.client.util.gui.GuiDisplayUtil;
import mage.client.util.gui.TableUtil;
import mage.constants.*;
@ -579,7 +580,7 @@ public class TablesPanel extends javax.swing.JPanel {
this.jPanelBottom.setVisible(false);
} else {
this.jPanelBottom.setVisible(true);
this.jLabelFooterText.setText(serverMessages.get(0));
URLHandler.handleMessage(serverMessages.get(0), this.jLabelFooterText);
this.jButtonFooterNext.setVisible(serverMessages.size() > 1);
}
}
@ -1283,7 +1284,9 @@ public class TablesPanel extends javax.swing.JPanel {
if (currentMessage >= messages.size()) {
currentMessage = 0;
}
this.jLabelFooterText.setText(messages.get(currentMessage));
URLHandler.RemoveMouseAdapter(jLabelFooterText);
URLHandler.handleMessage(messages.get(currentMessage), this.jLabelFooterText);
}
}
}//GEN-LAST:event_jButtonFooterNextActionPerformed

View file

@ -0,0 +1,119 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mage.client.util;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JLabel;
/**
*
* @author Dahny
*/
public class URLHandler {
private static MouseAdapter currentMouseAdapter;
/**
* This method makes a URL in a message click-able and converts the message
* into HTML.
*
* @param message: The selected message
* @param label: The message of the day label
*/
public static void handleMessage(String message, JLabel label) {
String url = detectURL(message);
if (!url.equals("")) {
label.addMouseListener(createMouseAdapter(url));
}
label.setText(convertToHTML(message));
}
public static void RemoveMouseAdapter(JLabel label) {
label.removeMouseListener(currentMouseAdapter);
currentMouseAdapter = null;
}
private static MouseAdapter createMouseAdapter(String url) {
currentMouseAdapter = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
URI uri = new URI(url);
desktop.browse(uri);
} catch (IOException | URISyntaxException ex) {
// do nothing
}
} else {
//do nothing
}
}
}
};
return currentMouseAdapter;
}
public static String convertToHTML(String input) {
String s = input;
String output = "<html>";
// separate the input by spaces
String[] parts = s.split("\\s+");
for (String item : parts) {
try {
URL url = new URL(item);
// The item is a valid URL
output = output + "<a href=\"" + url + "\">" + url + "</a> ";
} catch (MalformedURLException e) {
//The item might still be an URL
if (item.startsWith("www.")) {
output = output + "<a href=\"" + item + "\">" + item + "</a> ";
} else {
output = output + item + " ";
}
}
}
output = output + "</html>";
return output;
}
public static String detectURL(String input) {
String s = input;
String output = "";
// separate the input by spaces
String[] parts = s.split("\\s+");
for (String item : parts) {
try {
URL url = new URL(item);
// The item is a valid URL
output = url.toString();
} catch (MalformedURLException e) {
//The item might still be an URL
if (item.startsWith("www.")) {
output = "http://" + item;
}
}
}
return output;
}
}