package frame;

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;


public class JLink extends JLabel{

    public File file;
    public Color LINK_COLOR;
    public Cursor Rollover_Cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);



    public JLink(){
        this(Color.RED);
    }
    public JLink(Color color){
        super();
        this.LINK_COLOR = color;
        this.setForeground(LINK_COLOR);
        this.file = null;
    }
    public JLink(String file)throws FileNotFoundException{
        this(new File(file), file, Color.RED);
    }
    public JLink(String file, Color color)throws FileNotFoundException{
        this(new File(file), file, color);
    }
    public JLink(String file, String title)throws FileNotFoundException{
        this(new File(file), title, Color.RED);
    }
    public JLink(String file, String title, Color color)throws FileNotFoundException{
        this(new File(file), title, color);
    }
    public JLink(File file)throws FileNotFoundException{
        this(file, file.getAbsolutePath(), Color.RED);
    }
    public JLink(File file, Color color)throws FileNotFoundException{
        this(file, file.getAbsolutePath(), color);
    }
    public JLink(File file, String title)throws FileNotFoundException{
        this(file, title, Color.RED);
    }
    public JLink(File file, String title, Color color)throws FileNotFoundException{
        super(title);
        this.LINK_COLOR = color;
        this.setForeground(LINK_COLOR);
        this.file = file;
        this.addMouseListener(new Linklistener());
        if(this.file.exists() == false){throw new FileNotFoundException();}
    }
    public void setCursor(Cursor cursor){
        this.Rollover_Cursor = cursor;
        super.setCursor(cursor);
    }
    public File getFile(){
        return this.file;
    }
    public void setFile(File file)throws IOException{
        if(file.exists()){this.file = file;}
        else {
            this.file = null;
            throw new IOException();
        }
        if(this.isEnabled() == true){
            MouseListener[] listeners = this.getMouseListeners();
            try{
                MouseListener listener = listeners[0];
            }
            catch(Exception exc){
                this.addMouseListener(new Linklistener());
            }
        }
    }
    public void setFile(String file)throws IOException{
        if(new File(file).exists()){this.file = new File(file);}
        else {
            this.file = null;
            throw new IOException();
        }
        if(this.isEnabled() == true){
            MouseListener[] listeners = this.getMouseListeners();
            try{
                MouseListener listener = listeners[0];
            }
            catch(Exception exc){
                this.addMouseListener(new Linklistener());
            }
        }
    }
    public void setColor(Color color){
        this.LINK_COLOR = color;
        super.setForeground(color);
    }
    public void setColor(int r, int g, int b){
        Color color = new Color(r, g, b);
        this.setColor(color);
    }
    public void setColor(int r, int g, int b, int a){
        Color color = new Color(r, g, b, a);
        this.setColor(color);
    }
    public Color getColor(){
        return this.LINK_COLOR;
    }
    @Override
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
        if(enabled == true){
            MouseListener[] listeners = this.getMouseListeners();
            try{
                MouseListener listener = listeners[0];
            }
            catch(Exception exc){
                this.addMouseListener(new Linklistener());
            }
        }
        if(enabled == false){
            MouseListener[] listeners = this.getMouseListeners();
        for(int i = 0;; i++){
            try{
                MouseListener listener = listeners[i];
                this.removeMouseListener(listener);
            }
            catch(Exception exc){
                break;
            }
        }
        }
    }
    public void setText(String text, Color color){
        super.setText(text);
        this.setColor(color);
    }
    public void setText(String text, int r, int g, int b){
        super.setText(text);
        this.setColor(r, g, b);
    }
    public void setText(String text, int r, int g, int b, int a){
        super.setText(text);
        this.setColor(r, g, b, a);
    }
    protected void click(MouseEvent e){
        JLink link = (JLink)e.getComponent();
            try{
                Desktop.getDesktop().open(link.file);
            }
            catch(java.io.IOException exc){}
    }

}

class Linklistener implements MouseListener{

    public void mouseClicked(MouseEvent e) {
        JLink temp = (JLink)e.getComponent();
        temp.click(e);
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {
        JLink link = (JLink)e.getComponent();
        link.setCursor(link.Rollover_Cursor);
    }

    public void mouseExited(MouseEvent e) {

    }

}