Bi-Directional Text
/*
Core SWING Advanced Programming
By Kim Topley
ISBN: 0 13 083292 8
Publisher: Prentice Hall
*/
/*
Bi-Directional Text. Model Order and Visual Order. Caret Position and the Selection.
How Bi-Directional Text Is Handled in the Swing Text Components.
*/
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
public class BidiTextExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
JFrame f = new JFrame("Bi-directional Text");
final JTextPane tp = new JTextPane();
if (args.length > 0 && args[0].equalsIgnoreCase("Arabic")) {
GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
System.out.println("Using physical font");
Font font = new Font("Lucida Sans Regular", Font.PLAIN, 32);
tp.setFont(font);
} else {
Font font = new Font("Serif", Font.PLAIN, 32);
tp.setFont(font);
}
String s = new String(new char[] { (char) 'A', (char) 0xFE76,
(char) 0xFE77, (char) 0xFE78, (char) 'Z' });
final AbstractDocument doc = (AbstractDocument) tp.getDocument();
System.out.println("i18n property is " + doc.getProperty("i18n"));
// Turn on bi-directional text
doc.putProperty("i18n", Boolean.TRUE);
try {
doc.insertString(0, s, null);
} catch (Exception e) {
}
f.getContentPane().add(new JScrollPane(tp));
f.setSize(300, 300);
f.setVisible(true);
tp.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent evt) {
System.out.println("Caret position is " + evt.getDot());
}
});
try {
Thread.sleep(10000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Dump the model
doc.dump(System.out);
// Show the views
ViewDisplayer.displayViews(tp, System.out);
}
});
} catch (Exception e) {
}
}
}
class ViewDisplayer {
public static void displayViews(JTextComponent comp, PrintStream out) {
View rootView = comp.getUI().getRootView(comp);
displayView(rootView, 0, comp.getDocument(), out);
}
public static void displayView(View view, int indent, Document doc,
PrintStream out) {
String name = view.getClass().getName();
for (int i = 0; i < indent; i++) {
out.print("\t");
}
int start = view.getStartOffset();
int end = view.getEndOffset();
out.println(name + "; offsets [" + start + ", " + end + "]");
int viewCount = view.getViewCount();
if (viewCount == 0) {
int length = Math.min(32, end - start);
try {
String txt = doc.getText(start, length);
for (int i = 0; i < indent + 1; i++) {
out.print("\t");
}
out.println("[" + txt + "]");
} catch (BadLocationException e) {
}
} else {
for (int i = 0; i < viewCount; i++) {
displayView(view.getView(i), indent + 1, doc, out);
}
}
}
}
Related examples in the same category