1 package rero.dck; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 import javax.swing.border.*; 8 import java.util.*; 9 10 public class FileLink extends JComponent 11 { 12 protected LinkedList listeners; 13 protected JComponent label; 14 15 public FileLink() 16 { 17 addMouseListener(new TakeAction()); 18 listeners = new LinkedList(); 19 20 label = this; 21 } 22 23 protected String text = ""; 24 25 public void setText(String _text) { text = _text; setToolTipText(text); repaint(); } 26 public String getText() { return text; } 27 28 public Dimension getPreferredSize() 29 { 30 return new Dimension(0, Toolkit.getDefaultToolkit().getFontMetrics(getFont()).getHeight()); 31 } 32 33 public void paint(Graphics g) 34 { 35 StringBuffer string = new StringBuffer (); 36 FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(g.getFont()); 37 38 int x; 39 for (x = 0; x < text.length() && fm.stringWidth(text.substring(0, x)) < getWidth(); x++); 40 41 if (isEnabled()) 42 { 43 g.setColor(label.getForeground()); 44 g.drawLine(0, getHeight() - fm.getDescent() + 1, fm.stringWidth(text.substring(0, x)), getHeight() - fm.getDescent() + 1); 45 } 46 else 47 { 48 g.setColor(label.getForeground().brighter()); 49 } 50 51 g.drawString(getText().substring(0, x), 0, getHeight() - fm.getDescent()); 52 } 53 54 public void addActionListener(ActionListener l) 55 { 56 listeners.add(l); 57 } 58 59 public void fireEvent() 60 { 61 ActionEvent event = new ActionEvent(this, 0, "?"); 62 63 Iterator i = listeners.iterator(); 64 while (i.hasNext()) 65 { 66 ((ActionListener)i.next()).actionPerformed(event); 67 } 68 } 69 70 public class TakeAction extends MouseAdapter 71 { 72 protected Color original; 73 74 public void mouseClicked(MouseEvent ev) 75 { 76 if (isEnabled()) 77 fireEvent(); 78 } 79 80 public void mousePressed(MouseEvent ev) 81 { 82 if (isEnabled()) 83 { 84 original = label.getForeground(); 85 label.setForeground(UIManager.getColor("TextArea.selectionBackground")); 86 label.repaint(); 87 } 88 } 89 90 public void mouseReleased(MouseEvent ev) 91 { 92 if (isEnabled()) 93 { 94 label.setForeground(original); 95 label.repaint(); 96 } 97 } 98 99 public void mouseEntered(MouseEvent ev) 100 { 101 } 102 103 public void mouseExited(MouseEvent ev) 104 { 105 if (isEnabled()) 106 { 107 label.setForeground(original); 108 label.repaint(); 109 } 110 } 111 } 112 } 113 | Popular Tags |