All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becausetesting.commonswindow.utils.ui.JLabelUtils Maven / Gradle / Ivy

package com.github.becausetesting.commonswindow.utils.ui;

/*-
 * #%L
 * commons-window
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2016 Alter Hu
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;

import org.apache.log4j.Logger;

public class JLabelUtils {

	private static Logger log = Logger.getLogger(JLabelUtils.class);

	/**
	 * @param label The jlabel object
	 * @param text the content in jlabel
	 * @param url the url you can open
	 */
	public static void openUrl(JLabel label, String text, String url) {
		label.setText(text);
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//log.info("click object text is: " + findHyperlink);
				if (isBrowsingSupported()) {
					try {
						Desktop.getDesktop().browse(new URI(url));
					} catch (IOException | URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});

	}

	public static void email(JLabel label, String text, String receiver, String subject, String body) {
		String url = "emailto:" + receiver + "?subject=" + subject + "&body=" + body;

		label.setText(text);
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				JLabel labelObject = (JLabel) e.getSource();
				String findHyperlink = labelObject.getText();
				log.info("click object text is: " + findHyperlink);
				if (isEmailOpenSupported()) {
					try {
						Desktop.getDesktop().mail(new URI(url));
					} catch (IOException | URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
	}

	private static boolean isBrowsingSupported() {
		if (!Desktop.isDesktopSupported()) {
			return false;
		}
		boolean result = false;
		Desktop desktop = Desktop.getDesktop();
		if (desktop.isSupported(Desktop.Action.BROWSE)) {
			result = true;
		}
		return result;

	}

	private static boolean isEmailOpenSupported() {
		if (!Desktop.isDesktopSupported()) {
			return false;
		}
		boolean result = false;
		Desktop desktop = Desktop.getDesktop();
		if (desktop.isSupported(Desktop.Action.MAIL)) {
			result = true;
		}
		return result;

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy