KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > JNEditor


1 /*
2  * $Id: JNEditor.java,v 1.2 2004/09/01 05:00:33 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc;
9
10 import java.awt.Dimension JavaDoc;
11
12 import java.net.URL JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
14
15 import javax.swing.JPanel JavaDoc;
16 import javax.swing.JEditorPane JavaDoc;
17 import javax.swing.JScrollPane JavaDoc;
18
19 import org.jdesktop.swing.Application;
20 import org.jdesktop.swing.JXEditorPane;
21
22 import org.jdesktop.swing.actions.ActionManager;
23
24 /**
25  * Implementation of a text editor
26  *
27  * @author Mark Davidson
28  *
29  * @javabean.class
30  * displayName="Editor Component"
31  * name="JNEditor"
32  * shortDesctiption="A simple html editor component"
33  *
34  * @javabean.icons
35  * color16="/javax/swing/beaninfo/images/JEditorPaneColor16.gif"
36  * color32="/javax/swing/beaninfo/images/JEditorPaneColor32.gif"
37  */

38 public class JNEditor extends JNComponent {
39
40     private JXEditorPane editor;
41     private String JavaDoc inputURL;
42     private boolean readonly;
43     private static final String JavaDoc DEFAULT_TYPE = "text/plain";
44
45     private String JavaDoc type = DEFAULT_TYPE;
46
47     public JNEditor() {
48     editor = new JXEditorPane("text/html", "");
49     // setDocumentType("text/html");
50
setComponent(editor);
51     add(new JScrollPane JavaDoc(editor));
52     }
53
54     public JXEditorPane getEditor() {
55     return editor;
56     }
57
58     public void setReadOnly(boolean ro) {
59     this.readonly = ro;
60     editor.setEditable(!readonly);
61     }
62
63     public boolean isReadOnly() {
64     return readonly;
65     }
66
67     /**
68      * @javabean.property bound="true" shortDescription="Sets the input url"
69      */

70     public void setInputURL(String JavaDoc inputURL) {
71     if (editor != null && inputURL != null) {
72         URL JavaDoc url = Application.getURL(inputURL, this);
73         if (url != null) {
74         setInputURL(url);
75         }
76     }
77     }
78
79     public void setInputURL(URL JavaDoc url) {
80     if (editor != null && url != null) {
81         this.inputURL = url.toString();
82         try {
83         editor.setPage(url);
84         } catch (Exception JavaDoc ex) {
85         System.out.println("Error Setting page url: " + url);
86         ex.printStackTrace();
87         }
88     }
89     }
90
91
92     public String JavaDoc getInputURL() {
93     return inputURL;
94     }
95
96     // XXX may be irrelevent.
97
public void setDocumentType(String JavaDoc type) {
98     if (!"text/html".equals(type)) {
99         type = DEFAULT_TYPE;
100     }
101     if (type == null || "".equals(type)) {
102         type = DEFAULT_TYPE;
103     }
104     this.type = type;
105
106     editor.setContentType(type);
107     }
108
109     public String JavaDoc getDocumentType() {
110     return type;
111     }
112
113     // XXX - The demo with bike.html seems to grow without bounds. Fix the maximim size
114
// of the component.
115
private static final Dimension JavaDoc MAX_SIZE = new Dimension JavaDoc(400, 300);
116
117     public Dimension JavaDoc getPreferredSize() {
118     return MAX_SIZE;
119     }
120 }
121
Popular Tags