Skip to content

Commit

Permalink
Release JTextFieldPlaceholder rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzopalazzo committed May 13, 2020
1 parent 85c18c8 commit 0977e03
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 106 deletions.
9 changes: 9 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# These are supported funding model platforms

github: [vincenzopalazzo]

open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
liberapay: vincenzopalazzo
custom: https://vincenzopalazzo.github.io/material-ui-swing-donations/
17 changes: 17 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Java CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Gradle
run: ./gradlew build
72 changes: 0 additions & 72 deletions src/main/java/io/vincenzopalazzo/placeholder/CustomTextField.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.vincenzopalazzo.placeholder;

import mdlaf.utils.MaterialColors;
import io.vincenzopalazzo.placeholder.ui.BasicTextFieldPlaceholderUI;
import io.vincenzopalazzo.placeholder.ui.InternalPasswordFieldUI;
import io.vincenzopalazzo.placeholder.ui.InternalTextFieldUI;
import io.vincenzopalazzo.placeholder.util.RoundedCornerBorder;

import javax.swing.*;
import java.awt.*;
Expand All @@ -9,17 +12,17 @@ public class JTextFieldPlaceholder extends JPanel {

private static final String uiClassID = "TextFieldPlaceholderUI";

//protected JLabel iconContainer;
protected JToggleButton iconContainer;
protected JLabel placeholder;
protected JSeparator separator;
protected JTextField textField;
protected Color colorLine;
protected boolean passwordField;

public JTextFieldPlaceholder() {
super();
super.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
this.textField = new CustomTextField(this);
this.textField = new JTextField();
initView();
initStyle();
updateUI();
Expand All @@ -28,25 +31,25 @@ public JTextFieldPlaceholder() {
public JTextFieldPlaceholder(JTextField textField) {
super(new FlowLayout());
this.textField = textField;
this.textField.setUI(new CustomTextField.CustomTextFieldUI(this));
passwordField = (textField instanceof JPasswordField);
initView();
initStyle();
updateUI();
}

//@Override
public String getUIClassID() {
return uiClassID;
@Override
public String getUIClassID() {
return uiClassID;
}

protected void initView(){
protected void initView() {
//iconContainer = new JLabel();
iconContainer = new JToggleButton();
iconContainer.setOpaque(false);
this.add(iconContainer);

placeholder = new JLabel();
placeholder.setBorder(BorderFactory.createEmptyBorder(0,0,0,2));
placeholder.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
this.add(placeholder);

separator = new JSeparator(JSeparator.VERTICAL);
Expand All @@ -70,10 +73,11 @@ protected void initView(){
super.add(textField);

textField.setBorder(BorderFactory.createEmptyBorder());
setMaximumSize(new Dimension(textField.getWidth() + 250, textField.getHeight() + 15));
setBorder(new RoundedCornerBorder(getBackground(), 6));
}

protected void initStyle(){
protected void initStyle() {
setBackground(textField.getBackground());
}

Expand All @@ -83,75 +87,87 @@ protected void paintComponent(Graphics g) {
this.paintLine(g);
}

public JTextFieldPlaceholder setIcon(Icon icon){
if(icon == null) throw new IllegalArgumentException("icon null");
@Override
public void updateUI() {
if (UIManager.get(getUIClassID()) != null) {
BasicTextFieldPlaceholderUI ui = (BasicTextFieldPlaceholderUI) UIManager.getUI(this);
setUI(ui);
} else {
BasicTextFieldPlaceholderUI ui = new BasicTextFieldPlaceholderUI();
setUI(ui);
}
this.setCorrectTextFieldUI();
}

public JTextFieldPlaceholder setIcon(Icon icon) {
if (icon == null) throw new IllegalArgumentException("icon null");
iconContainer.setIcon(icon);
return this;
}

public JTextFieldPlaceholder setSelectedIcon(Icon icon){
if(icon == null) throw new IllegalArgumentException("icon null");
public JTextFieldPlaceholder setSelectedIcon(Icon icon) {
if (icon == null) throw new IllegalArgumentException("icon null");
iconContainer.setSelectedIcon(icon);
return this;
}

public JTextFieldPlaceholder setPlaceholderText(String text){
if(text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
public JTextFieldPlaceholder setPlaceholderText(String text) {
if (text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
placeholder.setText(text);
return this;
}

public JTextFieldPlaceholder setPlaceholderTextColor(Color colorLine){
public JTextFieldPlaceholder setPlaceholderTextColor(Color colorLine) {
this.placeholder.setForeground(colorLine);
return this;
}

public JTextField getTextFiled(){
public JTextField getTextFiled() {
return this.textField;
}

public JTextFieldPlaceholder setText(String text){
if(text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
public JTextFieldPlaceholder setText(String text) {
if (text == null || text.isEmpty()) throw new IllegalArgumentException("Invalid text");
this.textField.setText(text);
return this;
}

public JTextFieldPlaceholder setDimensionComponent(Dimension dimensionComponent){
public JTextFieldPlaceholder setDimensionComponent(Dimension dimensionComponent) {
textField.setPreferredSize(dimensionComponent);
textField.setSize(dimensionComponent);
return this;
}

public String getText(){
public String getText() {
return textField.getText();
}

public String getPlaceholderText(){
public String getPlaceholderText() {
return placeholder.getText();
}

public Icon getIcon(){
public Icon getIcon() {
return this.iconContainer.getIcon();
}

protected void paintLine(Graphics graphics){
if(colorLine == null){
if(textField.isFocusOwner()){
this.colorLine = UIManager.getColor("TextFieldPlaceholder[Line].activeColor");;
}else{
protected void paintLine(Graphics graphics) {
if (colorLine == null) {
if (textField.isFocusOwner()) {
this.colorLine = UIManager.getColor("TextFieldPlaceholder[Line].activeColor");
} else {
this.colorLine = UIManager.getColor("TextFieldPlaceholder[Line].inactiveColor");
}
}
graphics.setColor(this.colorLine);
graphics.fillRect(super.getRootPane().getX() + 5, this.textField.getY() + this.textField.getHeight() + 1, this.getWidth() - 8, 1);
graphics.fillRect(super.getRootPane().getX() + 5, this.textField.getY() + this.textField.getHeight() + 1, this.getWidth() - 8, 1);
}

void doFocus(){
public void doFocus() {
this.colorLine = UIManager.getColor("TextFieldPlaceholder[Line].activeColor");
this.repaint();
}

void focusLose(){
public void focusLose() {
this.colorLine = UIManager.getColor("TextFieldPlaceholder[Line].inactiveColor");
this.repaint();
}
Expand All @@ -168,4 +184,18 @@ public JLabel getPlaceholderComponent() {
public JSeparator getSeparator() {
return separator;
}

protected void setCorrectTextFieldUI() {
if (this.textField != null) {
if (passwordField) {
this.textField.setUI(new InternalPasswordFieldUI(this));
} else {
this.textField.setUI(new InternalTextFieldUI(this));
}
}
}

public boolean isSelected() {
return this.iconContainer.isSelected();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.vincenzopalazzo.placeholder.ui;

import io.vincenzopalazzo.placeholder.JTextFieldPlaceholder;

import javax.swing.*;
import javax.swing.plaf.basic.BasicPasswordFieldUI;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class InternalPasswordFieldUI extends BasicPasswordFieldUI {


protected FocusListener focusListener = new LineFocusListener();
protected JTextFieldPlaceholder textFieldPlaceholder;

public InternalPasswordFieldUI(JTextFieldPlaceholder textFieldPlaceholder) {
this.textFieldPlaceholder = textFieldPlaceholder;
}

@Override
public void installUI(JComponent c) {
super.installUI(c);

JTextField textField = (JTextField) c;
c.setBackground(UIManager.getColor("TextFieldPlaceholder.background"));
c.setForeground(UIManager.getColor("TextFieldPlaceholder.foreground"));
textField.setCaretColor(UIManager.getColor("TextFieldPlaceholder.caret"));
}

@Override
protected void installDefaults() {
super.installDefaults();
}

@Override
protected void installListeners() {
super.installListeners();
this.getComponent().addFocusListener(focusListener);
}

@Override
protected void uninstallListeners() {
super.uninstallListeners();
this.getComponent().removeFocusListener(focusListener);
}

public class LineFocusListener implements FocusListener {

@Override
public void focusGained(FocusEvent e) {
textFieldPlaceholder.doFocus();
}

@Override
public void focusLost(FocusEvent e) {
textFieldPlaceholder.focusLose();
}
}
}
Loading

0 comments on commit 0977e03

Please sign in to comment.