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

com.github.asteraether.tomlib.swing.JGhostTextField Maven / Gradle / Ivy

The newest version!
/*
 * 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 com.github.asteraether.tomlib.swing;

import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextField;
import javax.swing.text.Document;

/**
 *
 * @author Thomas
 */
public class JGhostTextField extends JTextField {

    private String ghostText;
    private boolean isGhost;
    private boolean shouldBeGhost;
    private Color ghostTextColor;
    private Color foreGround;

    /**
     *
     * @param ghostText
     */
    public JGhostTextField(String ghostText) {
        this.ghostText = ghostText;
        isGhost = true;
        super.setText(ghostText);
        ghostTextColor = super.getForeground();
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param text
     */
    public JGhostTextField(String ghostText, String text) {
        super(text);
        this.ghostText = ghostText;
        ghostTextColor = super.getForeground();
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param columns
     */
    public JGhostTextField(String ghostText, int columns) {
        super(columns);
        this.ghostText = ghostText;
        isGhost = true;
        super.setText(ghostText);
        ghostTextColor = super.getForeground();
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param text
     * @param columns
     */
    public JGhostTextField(String ghostText, String text, int columns) {
        super(text, columns);
        this.ghostText = ghostText;
        ghostTextColor = super.getForeground();
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param doc
     * @param text
     * @param columns
     */
    public JGhostTextField(String ghostText, Document doc, String text, int columns) {
        super(doc, text, columns);
        this.ghostText = ghostText;
        ghostTextColor = super.getForeground();
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param ghostTextColor
     */
    public JGhostTextField(String ghostText, Color ghostTextColor) {
        this.ghostText = ghostText;
        isGhost = true;
        super.setText(ghostText);
        this.ghostTextColor = ghostTextColor;
        foreGround = super.getForeground();
        super.setForeground(ghostTextColor);
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param ghostTextColor
     * @param text
     */
    public JGhostTextField(String ghostText, Color ghostTextColor, String text) {
        super(text);
        this.ghostText = ghostText;
        this.ghostTextColor = ghostTextColor;
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param ghostTextColor
     * @param columns
     */
    public JGhostTextField(String ghostText, Color ghostTextColor, int columns) {
        super(columns);
        this.ghostText = ghostText;
        isGhost = true;
        super.setText(ghostText);
        this.ghostTextColor = ghostTextColor;
        foreGround = super.getForeground();
        super.setForeground(ghostTextColor);
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param ghostTextColor
     * @param text
     * @param columns
     */
    public JGhostTextField(String ghostText, Color ghostTextColor, String text, int columns) {
        super(text, columns);
        this.ghostText = ghostText;
        this.ghostTextColor = ghostTextColor;
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param ghostText
     * @param ghostTextColor
     * @param doc
     * @param text
     * @param columns
     */
    public JGhostTextField(String ghostText, Color ghostTextColor, Document doc, String text, int columns) {
        super(doc, text, columns);
        this.ghostText = ghostText;
        this.ghostTextColor = ghostTextColor;
        foreGround = super.getForeground();
        registerHandlers();
    }

    /**
     *
     * @param fg
     */
    @Override
    public void setForeground(Color fg) {
        foreGround = fg;
        if (!isGhost) {
            super.setForeground(foreGround);
        }
    }

    /**
     *
     * @return
     */
    public Color getGhostTextColor() {
        return ghostTextColor;
    }

    /**
     *
     * @param ghostTextColor
     */
    public void setGhostTextColor(Color ghostTextColor) {
        this.ghostTextColor = ghostTextColor;
    }

    /**
     *
     * @return
     */
    public String getGhostText() {
        return ghostText;
    }

    /**
     *
     * @param ghostText
     */
    public void setGhostText(String ghostText) {
        this.ghostText = ghostText;
    }

    private void onFocusGained() {
        if (isGhost) {
            super.setText("");
            super.setForeground(foreGround);
        }
    }

    /**
     *
     * @param t
     */
    @Override
    public void setText(String t) {
        if (t.isEmpty()) {
            super.setText(ghostText);
            isGhost = shouldBeGhost = true;
            super.setForeground(ghostTextColor);
        } else {
            super.setText(t);
            super.setForeground(foreGround);
            isGhost = shouldBeGhost = false;
        }
    }

    /**
     *
     * @return
     */
    @Override
    public String getText() {
        if (isGhost && super.getText().equals(ghostText)) {
            return "";
        } else {
            return super.getText();
        }
    }

    private void onFocusLost() {
        if (!isEditable()) {
            return;
        }
        if (super.getText().isEmpty() || shouldBeGhost) {
            super.setText(ghostText);
            super.setForeground(ghostTextColor);
            isGhost = true;
        } else {
            isGhost = shouldBeGhost = false;
            super.setForeground(foreGround);
        }
    }

    /**
     *
     * @return
     */
    public boolean isGhost() {
        return isGhost;
    }

    private String super_getText() {
        return super.getText();
    }

    private void registerHandlers() {
        this.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                onFocusGained();
            }

            @Override
            public void focusLost(FocusEvent e) {
                onFocusLost();
            }
        });

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (super_getText().isEmpty()) {
                    isGhost = shouldBeGhost = true;
                } else {
                    isGhost = shouldBeGhost = false;
                }
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy