JEDitorPane ˂ ͟߂ џ

Rob Kenworthy :˜ߜ
JavaWorld :
ˉ ͌ :݇ͅ

ܟ ̓ ˃ ͟߂ ̓ JEDitorPane · ؟͐



Ùԛ
ܟ URL ڟ JEDitorPane ڟ HTML џ ̓ ߛ ԃ
HTML ܂ ޟ .˟ ޟ ˝ ނ ˜
HTML ܟ ˟߅ ˌ ˌ ͟ ܟ
ނ闛 ւ ̓ ͟ ڟ <IMG> ߅ ߍ .˟
ޟ ˌ ˝ߊ ܗ ˂ Ə ˜ ˂ ޟ ͟ߍ
.ŋ ܉ ܃ JEDitorPane ؟͐ ԋ߅



߂
˂ JEDitorPane ߛ · ˟߅ HTML ܟ џ ̓
΂ ΂鋞 ˖ JEDitorPane ͅ˟ ؟ ͇ ̓ ˟
.߃ XML ͕ ˂ ̓ ̓ ڟ Ŋ ߃ ΂ .
韂 ł߅ ŋ߅ ߃ HTML WYSIWYG ͘͟ ޛ ͂ ݞ ·
韂雜 ̓ JEDitorPane .˜ џ͟ XML ߅ ͋ ܃ HTML
JEDitorPane ͘ܛ ͈ ߃ ɟ HTML ˂
镂 HTML ˂ JEDitorPane .߃ ˌ
ߍ ͘ ܂ ̓ .˝ џ ͟ ͟߂ ŋ߅
JEDitorPane XML ܛ ͟ ڟ ͟ ˌ
.˝ џ ŋ ԃ ͟ߍ ŋ߅

<html><img src="images\example.gif" width=200 height=200></img>


͟ ̓ ͕ ) ͗ ˝ߊ ܛ ß ˃ ߂ ͟ ڟ З̓
(ˌ ߇ ˌ ͟ߍ

<html><img src="file:\\c:\images\example.gif" width=200 height=200></img>

XML 韂 鋛 ߅͟ ڟ ͟߂ ޛ ̓
. ͟ ˂ ނߊ ޛ ܟ ޟ˃ .˜ˌ ˞
. ˝ߊ ɟ߅ ܉ ܗ ޟ ̓ ř



ܗ ޟ ̓ ř
韂 ߅ ř ˜ ڛ JEDitorPane ͂ ٟͅ
.ݟ ͟ ͟߂

.˜ ˂ ˟ˇ JEDitorPane ڟ JEDitorPane()

ł錛 string ڟ ̓ JEDitorPane ڟ string JEDitorPane(URL)
.˜ ˂ URL

酛 JEDitorPane ڟ string JEDitorPane(String Type, String text)
.˟ ˂ ͘ initailize ˌ

URL ł銌 Ђ ̓ JEDitorPane ڟ JEDitorPane(URL initialPage)
.˜ ˂

HTML ܟ ڟ ւ ڟ constructor ޟ͂ ޟ
JEDitorPane HTML ܟ ڟ .˜ initialize
JEDitorPane . 鐅 URL ˜΂ ͂ͅ ܉ ܉
ނ ͟ ܗ ˜߅ ͂ ԋ߅ ˌ
̓ ͟ ڟ HTML ܟ ܉ ͗ ݂ ͂ ޟ .̓
.˝ ݂ ߂ ͟ ڟ ˂

韂 џ ޅ ͟ ͖ ˂ ߛ Constructor ͘
ł鉛 String ڟ ߋ Constructor .͘ ˂ ܃
݅ ˍ ޟ ߅ .˜ initialize ˜ ߃
߃ ܟ ڟ ٟ ݜ ˟ ͅ ܟ ڟ XML ߅ ڟ
.ݟ ˂ ߋ Constructor ߃



ܗ ܉ ߘ
̈́ ͘ ڈߗ ܗ ڟ ˟ ΂ Ùԛ ܃
˂镅 JEDditorPane ܟ ͗ ̓ ޟͅߞ .ݜ ܉
韂 ݂ ˟߃ ޟ ܂ ޟ .ˌ setText(String_text)
.韜 ͂ ͟ ڟ ͝ ː ˌ џ
͟ ˗ ˟߅ .˜ߌ ߇ߛ ޅ ˟ ˟ˇ ߅ ߅
.˟ ˂ ˟ˇ ߅ ߛ ̓

private void insertHTML
  (JEditorPane editor, String html, int location)
                    throws IOException {
  //assumes editor is already set to "text/html" type
  HTMLEditorKit kit =
    (HTMLEditorKit) editor.getEditorKit();
  Document doc = editor.getDocument();
  StringReader reader = new StringReader(html);
  kit.read(reader, doc, location);
}

˜ HTML JEDditorPane ߘ :ݟ ߎߛ ܍ ܂
. ւ EditorKit ݝ ܟ ݝ JEDditorPane ߜ
韂 ڟ ߂ ˌ ˌ ؟͐ text/html ߜ JEDditorPane
tag К ˗ ˟ ޟ .߃ ˝ߊ HTML/EditorKit ڟ HTML
К 雂 EditorKit К .˟ ˜ ߅ ߇ߛ
霂ˇ ԃ create(Element_ele) ˅ ŋ HTMLFactory
˞̓ ͟߂ ş͟˛ factory К ˗ ͟ .˜

            else if (kind==HTML.Tag.IMG)
              return new ImageView(elem);

韂 ̓ .˜ߊ ͟ߍ ImageView К ˟ ˟ߊ Ԝ
:ߌ ˜ߊ ͕ getSourceURL() ˅ ͟ߍ ܉ ͗

    private URL getSourceURL( ) {
      String src = (String) fElement.getAttributes().
            getAttribute(HTML.Attribute.SRC);
      if( src==null ) return null;

      URL reference = ((HTMLDocument)getDocument()).
                    getBase();
        try {
          URL u = new URL(reference,src);
          return u;
        } catch (MalformedURLException e) {
          return null;
        }
    }

URL ˂ HTML ܟ ܉ Ђ̓ getSourceURL() ˅
ܛ ߌ Ō΂ null ˖ ˌ null ͘ .˜ ˟ˇ
.˟ override ˅ ޟ ˟ .ˌ ˝ߊ ؖ߅ ͟ߍ

ImageView К
݂ ͟ߍ ̘͂ initialize(Element_ele)
К ˟ Є .ˌ protected ߗ̛ К .˟ override
͂ ޟ ݂ ̓ ޟͅ˂ .˟΂ ˟ˇ "
К ΂ .ŋ ImageView К ˗ ł ؟͐
.ݟ MyImageView

ˌ ̓͘ ͟ ˗ .˟ ˜ ̘͂ ͟ߍ ˗ ˅
:ŋ initialize(Element_elem) ˅

        URL src = getSourceURL();
        if( src != null ) {
          Dictionary cache = (Dictionary)
getDocument().getProperty(IMAGE_CACHE_PROPERTY);
          if( cache != null )
              fImage = (Image) cache.get(src);
          else
              fImage = Toolkit.getDefaultToolkit().getImage(src);
        }

͟ߍ ̘ ͂ ˌ null ͘ .˟ ˟ ͅ URL
) Ԗ ˟ MyImageView .ˌ ˝ߊ ͇ ˃
ނ ̓ ˟߅ ŋ ˅ ڟ ͟ ˗ .˟ ͇ (ŋ URL ڟ ͟ߍ
.˟ ͟ߍ

    private boolean isURL() {
        String src =
          (String)
fElement.getAttributes().getAttribute(HTML.Attribute.SRC);
        return src.toLowerCase().startsWith("file") ||
               src.toLowerCase().startsWith("http");
    }

ͅ String ڟ Ù ͟ߍ ͛ "
file :霗 ނ ͟ URL ߜ ΂ ނ ̓
降 К ˜ߜ ؙ Μ .remote ͟߂ ̓ http ͟߂ ̓
. ˂镅 К ͟ javax.swing.text.html.imageview
韓 .ŋ ΂ ͘ܛ ͂ͅ ܂ ޟ̓
.ŋ fElement

if(isURL()){} ˟ߜ ˗ ˟߅
Ԗ .ŋ ˂ ͂ ˟ ͖ ͟ ڟ else ߅ ̓
̓ ߛ ނ ˟ ̘ ͂ ͟ߍ
.˟ ݂ ͂

    else {
       String src =
         (String) fElement.getAttributes().getAttribute
          (HTML.Attribute.SRC);
       fImage = Toolkit.getDefaultToolkit().createImage(src);
    }

creatImage(src) . 闈ߗ ڟ ŋ ͘˂ ͂ ޟ
ߍ ݂ ܃ ˖ ڟ ˜߅
. ߊ џ ݂ ͟ߍ ڟ ˝ ق ޟ ͘ .˜̓͘
雂 ߍ ˟ߌ Յ ˟߅ 싛 ޟ ܉ ̓
雟 ̓ MediaTracker ˂ ޛ ܟ ޟ .˜ߌ ͖
鍅 ˜ ͅ · ǂ MediaTracker ͂ .߃ ͟ߍ ˌ
݂͘ ݟ ͋ ڟ ͘ ͂ ޟ̓ .Ō ͂ͅ ڟ ߍ
̓ ߊ ˅ ͗ ͖ java.awt.MediaTracker ݂
!݅ߜ 싛

    private void waitForImage() throws InterruptedException {
      int w = fImage.getWidth(this);
      int h = fImage.getHeight(this);

      while (true) {
        int flags = Toolkit.getDefaultToolkit().checkImage
            (fImage, w, h, this);

        if ( ((flags & ERROR) != 0) || ((flags & ABORT) != 0 ) )
           throw new InterruptedException();
        else if ((flags & (ALLBITS | FRAMEBITS)) != 0)
        return;
      Thread.sleep(10);
      //System.out.println("rise and shine...");
      }
    }

MediaTracker waitForID(int_id) ͂ ނ雝 Ђ ˅ ޟ
ߊ͕ ڟ .˜ ˂ · ڟ ΂ ˝ ݂
.ߌ ͖ ͟ߍ ˂ Є ŋ ˜߅

ނ .霗 ͂ ܃ ݙ ߇ ڈߗ ܗ ڟ
.˜ ߇ javax.swing.text.html ǟ ImageView ͗ ·
MyImageView ݂ К ˂ ̓ ܟ ݂
ImageView .˜ ͖ ǟ ̘݅
ߌ џ ˜߅ ͟ ߇ ˏ ܟ˃ ͟ߍ
javax.swing.text.html.icons 韄 ݂
К ݂酂 ͂ . ͂
˜ ˂ class К getResourceAsStream(String_name)
:ŋ ޟ

    InputStream resource =
HTMLEditorKit.class.getResourceAsStream(MISSING_IMAGE_SRC);

where the MISSING_IMAGE_SRC parameter is a String with content:

    MISSING_IMAGE_SRC =
      "icons" + System.getProperty("file.separator", "/") +
"image-failed.gif";

˂ ނ ImageView 霛 ͟ ˟Θ
߂ 鍅 ̘͂ ̓ getResourceAsStream(String name)
:˝ ɟ߅ ݂

ܟ ޟ˃ ŋ Κ ͂ ޟ .˟ ş ߜ ڟ
. ̓ Օ ܟ˃ getResource К ߋ̈́ ߜ ˜
. 鋚 ͂ Ə ˜߅ К ޟ ͈
ˌ ̈́ ş Ԗ Class.getResourceAsStream
*/ .˜͘ ̓ ߌ ܟ˃ ͟ߍ

韚 韝 ΂ ˟͗ ԛ ˃ ъ ޟ ߜ ͘
韌 ֚ ќ ޟ ͘ .ݝ ɟ߅ 삋 ޟ ɟ͌ ̓
˂ ߌ џ ˃ ݂ ͟߂ ͈ ˌ ˟ߊ
ނ ͟߂ ˟ ܗ ߛ ߛ ̓ .˟ߌ ނߊ ˗ ݟͅ
Â銅 ܗ ܉ ̓ ˅ ޟ ޛ .˟ ̘ ͂
ř ܛ ŋ ߋ̈́ ̓ ˌ .ŋ Κ ͂
.酋 ڋ ͅ ͅ ʼn ՜ Օ ̓ ̓
˂酋 ޟ ˝ ߍ ͝ ( ֚ ͅ ˃ )
韂 ͟ߍ ̘ ͂ ߂ ˅ ˂ Є ߃ ̓ ڟ
.ߌ ͘ Ə

˟߅ ߌ ˜ߊ ͕ getResourceAsStream(String name)
镂 韂雜 Ԝ ͟ߍ ͟ ڟ
К 銌 ͟ ݂ ͟ߍ ˗ .˟
К 霂 ܂ ̘͂ HTMLEditorKit
鍅 ̘ ͂ ̓ ŋ ͖ jvax.swing.text.html HTMLEditorKit
. ˝ߊ њ javax.swing.text.icons Image-failed.gif ݂
雅 ŋ Κ ̓͂ Ο ˂ ߅͟ ̓ ޟ
韗 ڟ HTMLEditorKit ނ .˜ߌ ͖ ǟ ڟ
. ͅ getResourceAsStream ˅ ŋ protected
݂雅 ͟ߍ ͗ ˂ MyImageView К ˟߅
.ŋ ޟ Ο ˟ ͖ ߗ ͟

    InputStream resource =
MyImageView.class.getResourceAsStream(MISSING_IMAGE_SRC);

銂 ߃ ˟͘ ޛ ˅ ˂ ݟ ͘
˂ ނ ŗ͌ JDK ߇ߛ ߗ ˟߅ ˜ ͝ ˟ ނߊ
ǂ韅 ͟ ߂ ͟ ڟ ˂ ̓ ˟
߂ . 韓
.javax.swing.text.html.icons.imagename.gif



getResourceAsStream ˂ ˂
͛ ̓ ś Ο ڜ ڟ .Javadoc information forth class
.ŋ ˌ ݝ͕ ś ޟ

. 韋 ͂΂ ԃ ޟ
霗 ˂ ͗ ˂ ޛ ˗ ͘ ŋ
΂ ̓ ͟ ˂ ˂ Ԗ
Ο ˜ߌ ͖ ͟ ͟߂ ͘ .˟ ŋ ˌ
܉ ߘ ̓ ޛ .߃ ˝ߊ Ԝ џ ߊ
.ŋ ܉ ܃ ̓ ߈ (ߌ ł· ܗ
酛 ڟ ˟ ؟͐ ߊ ̓ ̓ ͟
.˟ ؟͐ ݅

酋߂ ͟ ͟ ͟ߍ ̘ ͂ ܃ MyImageView
韜 ݞ ܃ ͘ .˝ ܂ ݝ ͟ ͅ ˂
MyImageView 韂 銂 processSrcPath() ˅ ܂
.˟͘

ߘ ˟ ˟ ܂ ޟ ŋ ܛ MyImageView ˜ ͝ ͊
MyImageView JEditorPane
JEditorPane .˜ ˂ javax.swing.text.html.imageview
:˜ Ù

.HTML RTF Plain

ߛ ͟ К ڟ) Basic_HTML ˌ HTML џ ܂ JEditorPane ͘
雂 HTMLEditorKit ˅ Basic_HTML .˜ ͅ HTML (TExt_UI
К ˖ ŋ getViewFactory() ݂ ˅
݂ ڟ ܛ HTMLFactory .˜̓͘ HTMLFactory ˌ ˜ߊ
ԃ .˜͘ ̓ ߅ ك ̓ ͂ ڟ ŋ create(Element_ele)
.˜̓͘ ImageView ř ڟ ˌ IMG ߅ ڟ ߅ ͘ ͅɎ
ނ EditKit ߅ MyImageView ř ڟ ˜͘ ΂ ̓
К ڟ ނߊ MyHTMLEditorKit ܊ ŋ MyHTMLEditorKit ݂
К HTMLFactory 銂 MyHTMLFactory ݂ ˟ ˂ ˟ˇ
郌 Ο ˟΂ ނߊ create(Element_ele) ˅ ˟߅
.ŋ ޟ

    public View create(Element elem) {
      Object o =
        elem.getAttributes().getAttribute (StyleConstants.NameAttribute);
      if (o instanceof HTML.Tag) {
        HTML.Tag kind = (HTML.Tag) o;
        if (kind == HTML.Tag.IMG)
          return new MyImageView(elem);
      }
      return super.create( elem );
    }

˂ JEditorPane ˜ ݂ ̓ Ο
.ŋ ˂ ˗ .ŋ MyHTMLEditorKit

JEditorPane editor = new JEditorPane();
editor.setEditorKit(new MyHTMLEditorKit());

߅ .˟ ˂ ˟ ͗ ˂ insertTML() ˅ ܂
:˟ߜ ޟ ˜ ˗

int location = findLocation();
String html = "<img src=\"test.gif\">";

try {
insertHTML(editor, html, location);
}
catch (IOException e) { e.printStackTrace(); }

ߍ ˟߅ ݝ ނ findLocation() ˅ ޛ
.˟ ˅ Ԗ ˜ ̓ ͟

ނ HML_WYSIWYG ޅߜ ̓ ߊ ͟
.˟͘




˜ߜ
΂ ŋ ܖ ŗ͌ ڟ ڙ Rob Kenworthy
韙 ł .͗ ̓ ߂ ̓ ܉ ͛
ԋ߅ ̓ ˟߅ ͂ ̘ ܂ .ŋ ˛ ͅߟ ߙ
.ŋ ߃ ߂




Resources
  • Obtain the full source code for this article:
    http://www.javaworld.com/javatips/javatip109/javatip109.zip
  • Get the JEditorPane class documentation:
    http://java.sun.com/j2se/1.3/docs/api/javax/swing/JEditorPane.html
  • A Swing tutorial with JEditorPane information:
    http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html
  • Get the Javadoc documentation from Sun's Website:
    http://java.sun.com/j2se/javadoc/index.html


  • ՜
    ޟ ߛ ߊ ՜
    :˟ߘ Ùԛ

    -
    -
    -

    -
    Ë -
    ߗ -

    -
    ߙԛ -
    ŋ ˜ -




    :ڟͅ ŋ

    : ŗ͌ ݂








    Copyright 2001, IT World / JavaWorld / PC WORLD Iran/ International Data Group Inc. All rights reserved.