| 1 |
// Copyright 2002, SLAC, Stanford University, U.S.A. |
| 2 |
package org.freehep.graphicsio.test; |
| 3 |
|
| 4 |
import java.awt.BorderLayout; |
| 5 |
import java.awt.Font; |
| 6 |
import java.awt.event.ActionEvent; |
| 7 |
import java.awt.event.ActionListener; |
| 8 |
import java.awt.event.FocusAdapter; |
| 9 |
import java.awt.event.FocusEvent; |
| 10 |
import java.awt.event.MouseEvent; |
| 11 |
|
| 12 |
import javax.swing.BorderFactory; |
| 13 |
import javax.swing.JButton; |
| 14 |
import javax.swing.JComponent; |
| 15 |
import javax.swing.JLabel; |
| 16 |
import javax.swing.JPanel; |
| 17 |
import javax.swing.JTextField; |
| 18 |
import javax.swing.border.BevelBorder; |
| 19 |
|
| 20 |
/** |
| 21 |
* |
| 22 |
* @author Mark Donszelmann |
| 23 |
* @version $Id: freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestLabels.java f493ff6e61b2 2005/12/01 18:46:43 duns $ |
| 24 |
*/ |
| 25 |
public class TestLabels extends TestingPanel { |
| 26 |
|
| 27 |
public TestLabels(String[] args) throws Exception { |
| 28 |
|
| 29 |
super(args); |
| 30 |
setName("Labels"); |
| 31 |
|
| 32 |
JPanel testPanel = this; |
| 33 |
|
| 34 |
JLabel label1 = new JLabel("TestLabel1"); |
| 35 |
// label1.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 36 |
testPanel.add(label1); |
| 37 |
|
| 38 |
JLabel label2 = new JLabel("TestLabel2"); |
| 39 |
// label2.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 40 |
testPanel.add(label2); |
| 41 |
label2.setBorder(BorderFactory.createEtchedBorder()); |
| 42 |
|
| 43 |
JLabel label3 = new JLabel("TestLabel3"); |
| 44 |
// label3.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 45 |
testPanel.add(label3); |
| 46 |
label3.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
| 47 |
|
| 48 |
JComponent label4 = new SpecialLabel("TestLabel4", "Prefix"); |
| 49 |
// label4.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 50 |
testPanel.add(label4); |
| 51 |
|
| 52 |
JButton button1 = new JButton("TestButton1"); |
| 53 |
// button1.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 54 |
testPanel.add(button1); |
| 55 |
|
| 56 |
JButton button2 = new JButton("TestButton2"); |
| 57 |
// button2.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 58 |
testPanel.add(button2); |
| 59 |
button2.setBorder(BorderFactory.createEtchedBorder()); |
| 60 |
|
| 61 |
JButton button3 = new JButton("TestButton3"); |
| 62 |
// button3.setFont(new Font("Lucida Sans", Font.BOLD, 12)); |
| 63 |
testPanel.add(button3); |
| 64 |
button3.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); |
| 65 |
} |
| 66 |
|
| 67 |
public static void main(String[] args) throws Exception { |
| 68 |
new TestLabels(args).runTest(); |
| 69 |
} |
| 70 |
|
| 71 |
class SpecialLabel extends JComponent { |
| 72 |
|
| 73 |
public SpecialLabel(final String text, final String prefix, |
| 74 |
final int align) { |
| 75 |
sourceText = text; |
| 76 |
// this.prefix = prefix; |
| 77 |
setLayout(new BorderLayout()); |
| 78 |
label = new JLabel(text, align); |
| 79 |
label.setForeground(getForeground()); |
| 80 |
add(label, BorderLayout.CENTER); |
| 81 |
} |
| 82 |
|
| 83 |
public SpecialLabel(final String text, final String prefix) { |
| 84 |
this(text, prefix, JLabel.CENTER); |
| 85 |
} |
| 86 |
|
| 87 |
void edit() { |
| 88 |
textField = new JTextField(sourceText); |
| 89 |
textField.setFont(label.getFont()); |
| 90 |
textField.setBorder(null); |
| 91 |
textField.setHorizontalAlignment(label.getHorizontalAlignment()); |
| 92 |
textField.addActionListener(new ActionListener() { |
| 93 |
public void actionPerformed(final ActionEvent a) { |
| 94 |
finishTextEdit(); |
| 95 |
} |
| 96 |
}); |
| 97 |
textField.addFocusListener(new FocusAdapter() { |
| 98 |
public void focusLost(final FocusEvent fe) { |
| 99 |
finishTextEdit(); |
| 100 |
} |
| 101 |
}); |
| 102 |
remove(label); |
| 103 |
add(textField, BorderLayout.CENTER); |
| 104 |
textField.requestFocus(); |
| 105 |
revalidate(); |
| 106 |
textField.getCaret().setVisible(true); |
| 107 |
} |
| 108 |
|
| 109 |
public void mouseEventNotify(final MouseEvent me) { |
| 110 |
if (me.getID() == MouseEvent.MOUSE_CLICKED && textField == null |
| 111 |
&& me.getClickCount() == 2) { |
| 112 |
edit(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private void finishTextEdit() { |
| 117 |
if (textField != null) { |
| 118 |
JTextField text = textField; |
| 119 |
textField = null; // protect against recursive call |
| 120 |
|
| 121 |
setText(text.getText()); |
| 122 |
remove(text); |
| 123 |
add(label, BorderLayout.CENTER); |
| 124 |
invalidate(); |
| 125 |
getParent().validate(); |
| 126 |
repaint(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public Font getFont() { |
| 131 |
return label.getFont(); |
| 132 |
} |
| 133 |
|
| 134 |
public void setFont(final Font f) { |
| 135 |
label.setFont(f); |
| 136 |
} |
| 137 |
|
| 138 |
public String getText() { |
| 139 |
return label.getText(); |
| 140 |
} |
| 141 |
|
| 142 |
public void setText(final String text) { |
| 143 |
sourceText = text; |
| 144 |
label.setText(text); |
| 145 |
} |
| 146 |
|
| 147 |
private String sourceText; |
| 148 |
|
| 149 |
private JLabel label; |
| 150 |
|
| 151 |
private JTextField textField; |
| 152 |
|
| 153 |
// private String prefix; |
| 154 |
} |
| 155 |
} |