/* * The MIT License * * Copyright 2010 Georgios Migdos <cyberpython@gmail.com>. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package bmach.ui.gui.components; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.UIManager; /** * * @author Georgios Migdos <cyberpython@gmail.com> */ public class JAddressTextField extends JIconTextField{ private String textWhenNotFocused; private Icon acceptIcon; private Icon errorIcon; private Icon pencilIcon; public JAddressTextField() { super(); this.textWhenNotFocused = "Memory address..."; this.acceptIcon = new ImageIcon(JAddressTextField.class.getResource("/bmach/ui/gui/resources/accept.png")); this.errorIcon = new ImageIcon(JAddressTextField.class.getResource("/bmach/ui/gui/resources/cancel.png")); this.pencilIcon = new ImageIcon(JAddressTextField.class.getResource("/bmach/ui/gui/resources/pencil.png")); this.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { repaint(); } public void focusLost(FocusEvent e) { repaint(); } }); } public String getTextWhenNotFocused() { return this.textWhenNotFocused; } public void setTextWhenNotFocused(String newText) { this.textWhenNotFocused = newText; } @Override protected void paintComponent(Graphics g) { String text = getText(); if(text.equals("")){ this.setIcon(pencilIcon); }else if(text.matches("0x[0-9a-fA-F]{2}")){ this.setIcon(acceptIcon); }else{ this.setIcon(errorIcon); } super.paintComponent(g); if (!this.hasFocus() && this.getText().equals("")) { int height = this.getHeight(); Font prev = g.getFont(); Font italic = prev.deriveFont(Font.ITALIC); Color prevColor = g.getColor(); g.setFont(italic); g.setColor(UIManager.getColor("textInactiveText")); int h = g.getFontMetrics().getHeight(); int textBottom = (height - h) / 2 + h - 4; int x = this.getInsets().left; Graphics2D g2d = (Graphics2D) g; RenderingHints hints = g2d.getRenderingHints(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.drawString(textWhenNotFocused, x, textBottom); g2d.setRenderingHints(hints); g.setFont(prev); g.setColor(prevColor); } } }