home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-11-28 | 4.3 KB | 170 lines |
- /*
- * @(#)MyEditor.java 1.0 99/11/27
- *
- * Copyright (c) 1999, David Griffiths. All Rights Reserved.
- *
- * This software is the proprietary information of David Griffiths.
- * This source code may not be published or redistributed without the
- * express permission of the author.
- *
- * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
- * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- */
-
- import java.awt.*;
- import java.util.*;
- import java.awt.event.*;
- import java.net.*;
- import java.io.*;
- import java.applet.*;
- import com.sun.java.swing.*;
- import com.sun.java.swing.text.*;
- import com.sun.java.swing.preview.*;
- import com.sun.java.swing.text.html.*;
- import com.sun.java.swing.event.*;
-
- /**
- * An example showing a toolbar connected to an RTF editor.
- */
- public class MyEditor extends JPanel implements ActionListener {
- private JPanel pan1;
- private JEditorPane edRTF;
- private EditorKit ekRTF;
- private JToolBar toolMain;
- private JApplet applet;
- private JButton btnChange;
-
- public MyEditor(JApplet applet) {
- setLayout(new BorderLayout());
-
- this.applet = applet;
-
- edRTF = addEditor(this, "test.rtf");
- Hashtable actions = getActions(edRTF);
-
- toolMain = new JToolBar();
-
- // Add buttons to the toolbar
- addToolButton(toolMain, "B", "Bold", "font-bold", actions);
- addToolButton(toolMain, "I", "Italic", "font-italic", actions);
- addToolButton(toolMain, "10", "10 pt", "font-size-10", actions);
- addToolButton(toolMain, "12", "12 pt", "font-size-12", actions);
- addToolButton(toolMain, "14", "14 pt", "font-size-14", actions);
- addToolButton(toolMain, "16", "16 pt", "font-size-16", actions);
- addToolButton(toolMain, "18", "18 pt", "font-size-18", actions);
- addToolButton(toolMain, "<", "Left justify", "left-justify", actions);
- addToolButton(toolMain, ">", "Right justify", "right-justify", actions);
-
- // Button to change background colour
- btnChange = new JButton("Colour");
- btnChange.addActionListener(this);
- btnChange.setToolTipText("Change background");
- toolMain.add(btnChange);
-
- add(toolMain, BorderLayout.NORTH);
- }
-
-
- /**
- * Create editor
- */
- private JEditorPane addEditor(JPanel panel, String docName) {
- JEditorPane editor = new JEditorPane();
-
- EditorKit ek = editor.getEditorKitForContentType("application/rtf");
-
- editor.setEditorKit(ek);
-
- editor.setEditable(true);
-
- try {
- ek.read(openDataFile(docName), editor.getDocument(), 0);
- }
- catch(BadLocationException e) {
- System.err.println(
- "Start position in doc doesn't exist: "
- + e
- );
- }
- catch(IOException e) {
- System.err.println(
- "Can't open file: "
- + docName
- + e
- );
- }
-
- panel.add(new JScrollPane(editor), BorderLayout.CENTER);
-
- return editor;
- }
-
- private Hashtable getActions(JEditorPane editor) {
- EditorKit ek = editor.getEditorKit();
-
- Action actionArray[] = ek.getActions();
-
- Hashtable actions = new Hashtable();
-
- for (int i = 0; i < actionArray.length; i++)
- actions.put(
- actionArray[i].getValue(Action.NAME),
- actionArray[i]
- );
-
- return actions;
- }
-
- /**
- * Add a button to a toolbar, displaying text str and with action a
- */
- private void addToolButton(JToolBar toolbar, String str,
- String desc, String a, Hashtable actions) {
- JButton btn = new JButton(str);
- btn.setToolTipText(desc);
- btn.addActionListener((Action)actions.get(a));
- toolbar.add(btn);
- }
-
- /**
- * Method to pick up clicks to the colour change button
- */
- public void actionPerformed(ActionEvent e) {
- edRTF.setBackground(
- JColorChooser.showDialog(
- this,
- "MyEditor's colour chooser",
- Color.white
- )
- );
- edRTF.repaint();
- }
-
- /**
- * Open the data file.
- *
- * @param dataFile - the name of the datafile
- * @returns the input stream for the file
- */
- private DataInputStream openDataFile(String dataFile) {
- URL fileURL;
- DataInputStream in = null;
-
- try {
- fileURL = new URL(applet.getDocumentBase(), dataFile);
- in = new DataInputStream(new BufferedInputStream(fileURL.openStream()));
- }
- catch (IOException e) {
- System.err.println("Cannot open " + dataFile + ": " + e.getMessage());
- }
- return in;
- }
- }
-
-
-