Projet gus05 de développement Java
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Le deal à ne pas rater :
Disque dur SSD CRUCIAL P3 1 To (3D NAND NVMe PCIe M.2)
65.91 €
Voir le deal

Un JTextField avec un texte grisé par défaut (2)

Aller en bas

Un JTextField avec un texte grisé par défaut (2) Empty Un JTextField avec un texte grisé par défaut (2)

Message  Gus Mar 2 Fév - 12:34

Bon, voici une autre version de mon entité qui fabrique des JTextFields avec un texte grisé par défaut, dont l'implémentation est cette fois plus sérieuse, utilisant un TextUI personnalisé pour modifier l'apparence du composant.

En fait, l'entité précédente buggait lorsqu'on souhaitait taper du texte en japonais avec l'IME (Input Method Editor) de Windows. Le problème est maintenant résolu. ^^

gus.graphic.textfield.textfieldtyping2

Le code source de cette entité se compose de deux classes :

Code:
package gus05.entity.gus.graphic.textfield.textfieldtyping2;

import gus05.framework.core.Entity;
import gus05.framework.features.Graphic;
import javax.swing.JComponent;

public class TextFieldTyping2 implements Entity, Graphic {

    public String getName()         {return "gus.graphic.textfield.textfieldtyping2";}
    public String getCreationDate()      {return "2010.02.02";}

    public JComponent gui()
    {return new JTextField0();}
}

Code:
package gus05.entity.gus.graphic.textfield.textfieldtyping2;

import gus05.framework.features.Register;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;

import javax.accessibility.Accessible;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.plaf.TextUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;
import javax.swing.text.View;
import javax.swing.text.Position.Bias;


public class JTextField0 extends JTextField implements Register {

   public static final KeyStroke resetKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
   public static final String DEFAULT_EMPTY_VALUE = "Enter text";
   
   
   private AbstractAction resetAction = new AbstractAction(){
        public void actionPerformed(ActionEvent e)
        {setText("");}
    };
   
    private JTextField fieldEmpty;
   
   public JTextField0()
   {
      super();
      init();
   }
   
   public JTextField0(String text)
   {
      super(text);
      init();
   }
   
   private void init()
   {
      setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
      getInputMap().put(resetKeyStroke,resetAction);
      
      fieldEmpty = new JTextField(DEFAULT_EMPTY_VALUE);
      fieldEmpty.setForeground(Color.LIGHT_GRAY);
      fieldEmpty.setFont(fieldEmpty.getFont().deriveFont(Font.ITALIC));
      
      setUI(new TextUI0(getUI()));
   }
   
   public void register(String key, Object obj) throws Exception
   {
      if(key.equals("emptyValue"))
      {
         fieldEmpty.setText((String)obj);
         repaint();
         return;
      }
      throw new Exception("Unknown key: "+key);
   }
   
   
   private boolean isEmpty()
   {
      if(isFocusOwner()) return false;
      return getText()==null || getText().length()==0;
   }
   
   
   private void updateField0()
   {
      fieldEmpty.setEnabled(isEnabled());
      fieldEmpty.setOpaque(isOpaque());
      fieldEmpty.setBounds(getBounds());
      fieldEmpty.setBorder(getBorder());
      fieldEmpty.setSelectedTextColor(getSelectedTextColor());
      fieldEmpty.setSelectionColor(getSelectionColor());
      fieldEmpty.setEditable(isEditable());
      fieldEmpty.setMargin(getMargin());
   }

   
   private class TextUI0 extends TextUI
   {
      private TextUI previousUI;
      private FocusAdapter0 focusHandler = new FocusAdapter0();
      
      public TextUI0(TextUI previousUI)
      {this.previousUI = previousUI;}
      
      public void installUI(JComponent c)
      {
         previousUI.installUI(c);
         c.addFocusListener(focusHandler);
      }
      
      public void uninstallUI(JComponent c)
      {
         previousUI.uninstallUI(c);
         c.removeFocusListener(focusHandler);
      }
      
      public Rectangle modelToView(JTextComponent t, int pos, Bias bias) throws BadLocationException
      {
         if(isEmpty()) return fieldEmpty.getUI().modelToView(t,pos,bias);
         return previousUI.modelToView(t,pos,bias);
      }

      public Rectangle modelToView(JTextComponent t, int pos) throws BadLocationException
      {return modelToView(t,pos,Position.Bias.Forward);}
      
      public void paint(Graphics g, JComponent c)
      {
         if(isEmpty()) paintWhenEmpty(g,c);
         else previousUI.paint(g,c);
      }

      protected void paintWhenEmpty(Graphics g, JComponent c)
      {
         updateField0();
         fieldEmpty.paint(g);
         JTextComponent textComp = (JTextComponent) c;
         if(textComp.getCaret()!=null)
            textComp.getCaret().paint(g);
      }
      
      public Dimension getPreferredSize(JComponent c)
      {return previousUI.getPreferredSize(c);}
      
      public boolean contains(JComponent c, int x, int y)
      {return previousUI.contains(c, x, y);}

      public void damageRange(JTextComponent t, int p0, int p1, Bias firstBias, Bias secondBias)
      {previousUI.damageRange(t, p0, p1, firstBias, secondBias);}

      public void damageRange(JTextComponent t, int p0, int p1)
      {previousUI.damageRange(t, p0, p1);}

      public boolean equals(Object obj)
      {return previousUI.equals(obj);}

      public Accessible getAccessibleChild(JComponent c, int i)
      {return previousUI.getAccessibleChild(c, i);}

      public int getAccessibleChildrenCount(JComponent c)
      {return previousUI.getAccessibleChildrenCount(c);}

      public EditorKit getEditorKit(JTextComponent t)
      {return previousUI.getEditorKit(t);}

      public Dimension getMaximumSize(JComponent c)
      {return previousUI.getMaximumSize(c);}

      public Dimension getMinimumSize(JComponent c)
      {return previousUI.getMinimumSize(c);}

      public int getNextVisualPositionFrom(JTextComponent t, int pos, Bias b, int direction, Bias[] biasRet) throws BadLocationException
      {return previousUI.getNextVisualPositionFrom(t, pos, b, direction, biasRet);}

      public View getRootView(JTextComponent t)
      {return previousUI.getRootView(t);}

      public String getToolTipText(JTextComponent t, Point pt)
      {return previousUI.getToolTipText(t, pt);}

      public int hashCode()
      {return previousUI.hashCode();}

      public String toString()
      {return previousUI.toString();}

      public int viewToModel(JTextComponent t, Point pt, Bias[] biasReturn)
      {return previousUI.viewToModel(t, pt, biasReturn);}

      public int viewToModel(JTextComponent t, Point pt)
      {return previousUI.viewToModel(t, pt);}
   }
   
   private class FocusAdapter0 extends FocusAdapter
   {
      public void focusGained(FocusEvent e)
      {e.getComponent().repaint();}

      public void focusLost(FocusEvent e)
      {e.getComponent().repaint();}
   }
}
Gus
Gus
Admin

Messages : 249
Date d'inscription : 01/09/2009

http://www.gus05.com

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum