home *** CD-ROM | disk | FTP | other *** search
- import com.sun.java.swing.AbstractButton;
- import com.sun.java.swing.Action;
- import com.sun.java.swing.JApplet;
- import com.sun.java.swing.JButton;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JEditorPane;
- import com.sun.java.swing.JPanel;
- import com.sun.java.swing.JScrollPane;
- import com.sun.java.swing.JToolBar;
- import com.sun.java.swing.preview.JColorChooser;
- import com.sun.java.swing.text.BadLocationException;
- import com.sun.java.swing.text.EditorKit;
- import com.sun.java.swing.text.JTextComponent;
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedInputStream;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.net.URL;
- import java.util.Hashtable;
-
- public class ToolExample extends JApplet implements ActionListener {
- private JPanel pan1;
- private JEditorPane edRTF;
- private EditorKit ekRTF;
- private JToolBar toolMain;
- private JButton btnChange;
-
- public void init() {
- this.pan1 = new JPanel();
- this.pan1.setLayout(new BorderLayout());
- this.edRTF = this.addEditor(this.pan1, "test.rtf");
- Hashtable var1 = this.getActions(this.edRTF);
- this.toolMain = new JToolBar();
- this.addToolButton(this.toolMain, "B", "Bold", "font-bold", var1);
- this.addToolButton(this.toolMain, "I", "Italic", "font-italic", var1);
- this.addToolButton(this.toolMain, "10", "10 pt", "font-size-10", var1);
- this.addToolButton(this.toolMain, "12", "12 pt", "font-size-12", var1);
- this.addToolButton(this.toolMain, "14", "14 pt", "font-size-14", var1);
- this.addToolButton(this.toolMain, "16", "16 pt", "font-size-16", var1);
- this.addToolButton(this.toolMain, "18", "18 pt", "font-size-18", var1);
- this.addToolButton(this.toolMain, "<", "Left justify", "left-justify", var1);
- this.addToolButton(this.toolMain, ">", "Right justify", "right-justify", var1);
- this.btnChange = new JButton("Colour");
- this.btnChange.addActionListener(this);
- this.btnChange.setToolTipText("Change background");
- this.toolMain.add(this.btnChange);
- this.pan1.add(this.toolMain, "North");
- ((JApplet)this).getContentPane().add(this.pan1);
- }
-
- private JEditorPane addEditor(JPanel var1, String var2) {
- JEditorPane var3 = new JEditorPane();
- EditorKit var4 = var3.getEditorKitForContentType("application/rtf");
- var3.setEditorKit(var4);
- ((JTextComponent)var3).setEditable(true);
-
- try {
- var4.read(this.openDataFile(var2), ((JTextComponent)var3).getDocument(), 0);
- } catch (BadLocationException var6) {
- System.err.println("Start position in doc doesn't exist: " + var6);
- } catch (IOException var7) {
- System.err.println("Can't open file: " + var2 + var7);
- }
-
- ((Container)var1).add(new JScrollPane(var3), "Center");
- return var3;
- }
-
- private Hashtable getActions(JEditorPane var1) {
- EditorKit var2 = var1.getEditorKit();
- Action[] var3 = var2.getActions();
- Hashtable var4 = new Hashtable();
-
- for(int var5 = 0; var5 < var3.length; ++var5) {
- var4.put(var3[var5].getValue("Name"), var3[var5]);
- }
-
- return var4;
- }
-
- private void addToolButton(JToolBar var1, String var2, String var3, String var4, Hashtable var5) {
- JButton var6 = new JButton(var2);
- ((JComponent)var6).setToolTipText(var3);
- ((AbstractButton)var6).addActionListener((Action)var5.get(var4));
- ((Container)var1).add(var6);
- }
-
- public void actionPerformed(ActionEvent var1) {
- this.edRTF.setBackground(JColorChooser.showDialog(this, "ToolExample's colour chooser", Color.white));
- this.edRTF.repaint();
- }
-
- private DataInputStream openDataFile(String var1) {
- DataInputStream var3 = null;
-
- try {
- URL var2 = new URL(((Applet)this).getDocumentBase(), var1);
- var3 = new DataInputStream(new BufferedInputStream(var2.openStream()));
- } catch (IOException var5) {
- System.err.println("Cannot open " + var1 + ": " + ((Throwable)var5).getMessage());
- }
-
- return var3;
- }
- }
-