import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.Position;
public class Format {
// Referenz //
static JFrame mainframe = Editor.mainframe;
static JTextArea textarea = Editor.textarea;
static JMenuItem b9 = Editor.b9;
static JCheckBoxMenuItem f1 = Editor.f1;
static JCheckBoxMenuItem a1 = Editor.a6;
// Singleton //
private static Format instance = null;
public static Format getInstance() {
if (instance == null) {
instance = new Format();
}
return instance;
}
// Initialisierung - FontChooser //
static String art_akt;
static int schnitt_akt;
static int grad_akt;
static String schnittstring;
static String gradstring;
static String schriftgrad[] = { "8", "9", "10", "11", "12", "14", "16",
"18", "20", "22", "24", "26", "28", "36", "48", "72" };
static String schriftschnitt[] = { "Standard", "Kursiv", "Fett",
"Fett Kursiv" };
// GUI //
static JDialog fontframe = new JDialog(mainframe,true);
static JLabel l1 = new JLabel("Schrifart:");
static JLabel l2 = new JLabel("Schriftschnitt:");
static JLabel l3 = new JLabel("Schriftgrad:");
static JLabel l5 = new JLabel("Skript:");
static JTextField t1 = new JTextField();
static JTextField t2 = new JTextField();
static JTextField t3 = new JTextField();
static JTextField t4 = new JTextField();
static JList li1 = new JList();
static JList li2 = new JList(schriftschnitt);
static JList li3 = new JList(schriftgrad);
static JScrollPane s1 = new JScrollPane(li1);
static JScrollPane s2 = new JScrollPane(li2);
static JScrollPane s3 = new JScrollPane(li3);
static JPanel p1 = new JPanel();
static JButton b1 = new JButton("OK");
static JButton b2 = new JButton("Abbrechen");
static Dimension d = new Dimension(364, 54);
// Listener //
static DocumentListener art_t = null;
static DocumentListener schnitt_t = null;
static DocumentListener grad_t = null;
static ListSelectionListener art_l = null;
static ListSelectionListener schnitt_l = null;
static ListSelectionListener grad_l = null;
public static void changepreview() {
try {
if (!li1.isSelectionEmpty())
art_akt = li1.getSelectedValue().toString();
if (!li2.isSelectionEmpty())
schnittstring = li2.getSelectedValue().toString();
if (!li3.isSelectionEmpty())
gradstring = li3.getSelectedValue().toString();
else if (t3.getText().length() > 0)
gradstring = t3.getText().toString();
switch (schnittstring.length()) {
case 8:
schnitt_akt = Font.PLAIN;
break;
case 6:
schnitt_akt = Font.ITALIC;
break;
case 4:
schnitt_akt = Font.BOLD;
break;
default:
schnitt_akt = Font.ITALIC + Font.BOLD;
}
grad_akt = Integer.parseInt(gradstring);
t4.setFont(new Font(art_akt, schnitt_akt, grad_akt));
} catch (NumberFormatException e) {
}
}
protected Format() {
// Einstellungen //
fontframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fontframe.setLocationByPlatform(true);
fontframe.setSize(500, 330);
fontframe.setResizable(false);
fontframe.setAlwaysOnTop(true);
t4.setText("AaBbYyZz");
t4.setPreferredSize(d);
t4.putClientProperty(
com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY,
Boolean.TRUE );
String f = "";
for (String fonts : GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames())
f = f + "#" + fonts;
final String fonts[] = f.substring(1).split("#");
li1.setListData(fonts);
// Listener //
art_l = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
t1.getDocument().removeDocumentListener(art_t);
if (!li1.isSelectionEmpty()) {
t1.setText(li1.getSelectedValue().toString());
changepreview();
}
t1.getDocument().addDocumentListener(art_t);
}
};
schnitt_l = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
t2.getDocument().removeDocumentListener(art_t);
if (!li2.isSelectionEmpty()) {
t2.setText(li2.getSelectedValue().toString());
changepreview();
}
t2.getDocument().addDocumentListener(art_t);
}
};
grad_l = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
t3.getDocument().removeDocumentListener(art_t);
if (!li3.isSelectionEmpty()) {
t3.setText(li3.getSelectedValue().toString());
changepreview();
}
t3.getDocument().addDocumentListener(art_t);
}
};
art_t = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
li1.removeListSelectionListener(art_l);
changedUpdate(e);
li1.addListSelectionListener(art_l);
}
public void removeUpdate(DocumentEvent e) {
li1.removeListSelectionListener(art_l);
changedUpdate(e);
li1.addListSelectionListener(art_l);
}
public void changedUpdate(DocumentEvent e) {
int i = li1
.getNextMatch(t1.getText(), 0, Position.Bias.Forward);
if (i != -1)
li1.setSelectedValue(fonts[i], true);
changepreview();
}
};
schnitt_t = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
li2.removeListSelectionListener(schnitt_l);
changedUpdate(e);
li2.addListSelectionListener(schnitt_l);
}
public void removeUpdate(DocumentEvent e) {
li2.removeListSelectionListener(schnitt_l);
changedUpdate(e);
li2.addListSelectionListener(schnitt_l);
}
public void changedUpdate(DocumentEvent e) {
int i = li2
.getNextMatch(t2.getText(), 0, Position.Bias.Forward);
if (i != -1)
li2.setSelectedValue(schriftschnitt[i], true);
changepreview();
}
};
grad_t = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
li3.removeListSelectionListener(grad_l);
changedUpdate(e);
li3.addListSelectionListener(grad_l);
}
public void removeUpdate(DocumentEvent e) {
li3.removeListSelectionListener(grad_l);
changedUpdate(e);
li3.addListSelectionListener(grad_l);
}
public void changedUpdate(DocumentEvent e) {
if (Arrays.asList(schriftgrad).contains(t3.getText()))
li3.setSelectedValue(t3.getText(), true);
else
li3.clearSelection();
changepreview();
}
};
li1.addListSelectionListener(art_l);
li2.addListSelectionListener(schnitt_l);
li3.addListSelectionListener(grad_l);
t1.getDocument().addDocumentListener(art_t);
t2.getDocument().addDocumentListener(schnitt_t);
t3.getDocument().addDocumentListener(grad_t);
ActionListener ok = new ActionListener() {
public void actionPerformed(ActionEvent e) {
textarea.setFont(new Font(art_akt, schnitt_akt, grad_akt));
fontframe.dispose();
}
};
ActionListener cancel = new ActionListener() {
public void actionPerformed(ActionEvent e) {
fontframe.dispose();
}
};
b1.addActionListener(ok);
b2.addActionListener(cancel);
// Layout //
fontframe.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.insets = new Insets(5, 5, 0, 0);
c.weightx = 3;
c.weighty = 1;
c.gridwidth = 3;
c.gridheight = 1;
fontframe.add(l1, c);
c.gridy = 1;
fontframe.add(t1, c);
c.gridy = 2;
fontframe.add(s1, c);
c.weightx = 2;
c.gridy = 0;
c.gridx = 3;
c.gridwidth = 2;
fontframe.add(l2, c);
c.gridy = 1;
fontframe.add(t2, c);
c.gridy = 2;
fontframe.add(s2, c);
c.insets = new Insets(5, 5, 0, 5);
c.weightx = 1;
c.gridy = 0;
c.gridx = 5;
c.gridwidth = 1;
fontframe.add(l3, c);
c.gridy = 1;
fontframe.add(t3, c);
c.gridy = 2;
fontframe.add(s3, c);
c.insets = new Insets(5, 5, 5, 0);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridheight = 2;
c.gridwidth = 5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 3;
fontframe.add(t4, c);
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(5, 5, 0, 5);
c.gridwidth = 1;
c.gridheight = 1;
c.gridx = 5;
fontframe.add(b1, c);
c.insets = new Insets(5, 5, 5, 5);
c.gridy = 4;
fontframe.add(b2, c);
KeyListener text = new KeyListener() {
public void keyTyped(KeyEvent e) {
if (!Character.isDigit(e.getKeyChar()))
e.consume();
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
};
t3.addKeyListener(text);
}
public void schriftarten() {
// Initialisieren //
switch (textarea.getFont().getStyle()) {
case 0:
schnittstring = "Standard";
break;
case 1:
schnittstring = "Fett";
break;
case 2:
schnittstring = "Kursiv";
break;
default:
schnittstring = "Fett Kursiv";
}
t1.setText(textarea.getFont().getFamily());
t2.setText(schnittstring);
t3.setText("" + textarea.getFont().getSize());
t4.setFont(new Font(textarea.getFont().getFamily(), textarea.getFont()
.getStyle(), textarea.getFont().getSize()));
// Anzeigen //
fontframe.setVisible(true);
}
public static void zeilenumbruch() {
if (f1.isSelected()) {
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
b9.setEnabled(false);
a1.setEnabled(false);
} else {
textarea.setLineWrap(false);
textarea.setWrapStyleWord(false);
b9.setEnabled(true);
a1.setEnabled(true);
}
}
}