Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2006

Tip: Looking for answers? Try searching our database.

HTML Editor

Thread view: 
Martin Gäckler - 23 Nov 2006 00:54 GMT
Hello,

i'm currently trying to develop a simple HTML-Editor with JTextPane and
the HTMLEditorKit provided by Swing.

Actually I want to transform a paragraph <p> to a Heading <h1>. This is
my code:

HTMLDocument    htmlDoc = (HTMLDocument) textEditor.getDocument();
int        position = textEditor.getSelectionStart();
Element        elem = htmlDoc.getParagraphElement(position);

if( elem instanceof HTMLDocument.BlockElement )
{
    HTMLDocument.BlockElement  block = (HTMLDocument.BlockElement)elem;
    int                        start, end;
    String                     text;

    try
    {
    start = block.getStartOffset();
    end = block.getEndOffset();
    text = htmlDoc.getText( start, end-start );
    htmlDoc.setOuterHTML(
       block,
       "<h1>" +
       text +
       "</h1>"
    );
    }
    catch( Exception e )
    {
    };
}

This works fine, unless the block does not contain any formating
information. Thus

    <p>Hello <u>world</u>.</p>

becomes to

    <h1>Hello world.</h1>

Is there a better way to change the type of a block element?

Thanks in advance

Martin

Signature

Firma/Company:                                              CRESD GmbH
Phone: +49-89-65 30 95 63                      Fax: +49-89-65 30 95 64
WWW:                                               http://www.cresd.de
S-Mail:                                Freibadstr. 14, D-81543 München
PGP-Key:                            http://www.cresd.de/edv/pgpkey.txt
Open BC (Einladung)            http://www.openbc.com/go/invita/4561755

Christian Kaufhold - 24 Nov 2006 00:16 GMT
Martin G??ckler <martin@gaeckler.de> wrote:

> i'm currently trying to develop a simple HTML-Editor with JTextPane and
> the HTMLEditorKit provided by Swing.

> Actually I want to transform a paragraph <p> to a Heading <h1>. This is
> my code:

Use setParagraphAttributes.

Christian
Martin Gäckler - 24 Nov 2006 09:35 GMT
Christian Kaufhold schrieb:
> Martin Gaeckler <martin@gaeckler.de> wrote:
>
[quoted text clipped - 7 lines]
>
> Christian

Thank You. This was my try:

HTMLDocument.BlockElement block = (HTMLDocument.BlockElement)elem;
int start, end;
String    text;

start = block.getStartOffset();
end = block.getEndOffset();

// thes what attributes are set
for(Enumeration e = block.getAttributeNames(); e.hasMoreElements(); )
  System.out.println(e.nextElement());

SimpleAttributeSet    newType = new SimpleAttributeSet();
newType.addAttribute( "name", "h1");
htmlDoc.setParagraphAttributes(start, end-start, newType, false );

This produces the following HTML-Code:

<p name="h1">
    hello world
</p>

And after a second call to my function. The Enumeration contained two
attributes with the name "name".

Then I have replaced the boolean parameter of setParagraphAttributes.
The produced HTML-Code was stupid:

<body name="h1">
    hello world
</body>

When I try to create an object of HTMLDocument.BlockElement instead of
SimpleAttributeSet I get an compiler error "Enclosing class required".

Any ideas?

Martin

Signature

Firma/Company:                                              CRESD GmbH
Phone: +49-89-65 30 95 63                      Fax: +49-89-65 30 95 64
WWW:                                               http://www.cresd.de
S-Mail:                                Freibadstr. 14, D-81543 München
PGP-Key:                            http://www.cresd.de/edv/pgpkey.txt
Open BC (Einladung)            http://www.openbc.com/go/invita/4561755

Christian Kaufhold - 24 Nov 2006 18:57 GMT
Martin G??ckler <martin@gaeckler.de> wrote:
> Christian Kaufhold schrieb:
> > Martin Gaeckler <martin@gaeckler.de> wrote:
[quoted text clipped - 6 lines]
> >
> > Use setParagraphAttributes.

> HTMLDocument.BlockElement block = (HTMLDocument.BlockElement)elem;

No need for the cast.

> int start, end;
> String  text;

> start = block.getStartOffset();
> end = block.getEndOffset();

> // thes what attributes are set
> for(Enumeration e = block.getAttributeNames(); e.hasMoreElements(); )
>    System.out.println(e.nextElement());

> SimpleAttributeSet      newType = new SimpleAttributeSet();

If you want to preserve the previous attributes:

 SimpleAttributeSet newType = new SimpleAttributeSet(block.getAttributes());

> newType.addAttribute( "name", "h1");

No, newType.addAttribute(AttributeSet.NameAttribute, HTML.Tag.H1);

> htmlDoc.setParagraphAttributes(start, end-start, newType, false );

> This produces the following HTML-Code:

> <p name="h1">
>         hello world
> </p>

> And after a second call to my function. The Enumeration contained two
> attributes with the name "name".

The attribute names are not necessarily Strings.

> Then I have replaced the boolean parameter of setParagraphAttributes.
> The produced HTML-Code was stupid:

> <body name="h1">
>         hello world
> </body>

This is actually a bug -- sometimes attributes are inherited from ancestor
elements.

Christian
Martin Gäckler - 24 Nov 2006 22:57 GMT
Christian Kaufhold schrieb:
>> HTMLDocument.BlockElement block = (HTMLDocument.BlockElement)elem;
>
> No need for the cast.

The compiler has another opinion. (incompatible types found)

> If you want to preserve the previous attributes:
>
>   SimpleAttributeSet newType = new SimpleAttributeSet(block.getAttributes());

That's a good idea.

>> newType.addAttribute( "name", "h1");
>
> No, newType.addAttribute(AttributeSet.NameAttribute, HTML.Tag.H1);

Thank You. That solved my problem. I'm really glad.

Here is my new source code:
void makeParagraph(HTML.Tag paragraphType)
{
  if( textEditor instanceof JEditorPane )
  {
    HTMLDocument  htmlDoc = (HTMLDocument) textEditor.getDocument();
    int           position = textEditor.getSelectionStart();
    Element       elem = htmlDoc.getParagraphElement(position);

    if( elem instanceof HTMLDocument.BlockElement )
    {
      HTMLDocument.BlockElement block = (HTMLDocument.BlockElement)elem;

      SimpleAttributeSet  newType = new SimpleAttributeSet(
        block.getAttributes()
      );
      newType.addAttribute( AttributeSet.NameAttribute, paragraphType );
      htmlDoc.setParagraphAttributes(position, 0, newType, false );
    }
  }
}

This function does now exactly what I want. 8-))))

Thank You again for your help.

> Christian

Martin

Signature

Firma/Company:                                              CRESD GmbH
Phone: +49-89-65 30 95 63                      Fax: +49-89-65 30 95 64
WWW:                                               http://www.cresd.de
S-Mail:                                Freibadstr. 14, D-81543 München
PGP-Key:                            http://www.cresd.de/edv/pgpkey.txt
Open BC (Einladung)            http://www.openbc.com/go/invita/4561755

Christian Kaufhold - 25 Nov 2006 13:41 GMT
Martin G??ckler <martin@gaeckler.de> wrote:
> Christian Kaufhold schrieb:
> >> HTMLDocument.BlockElement block = (HTMLDocument.BlockElement)elem;
> >
> > No need for the cast.

> The compiler has another opinion. (incompatible types found)

I meant "No need to mention HTMLDocument.BlockElement at all." It will
work with a plain Element.

Christian
Martin Gäckler - 26 Nov 2006 20:23 GMT
Christian Kaufhold schrieb:
> I meant "No need to mention HTMLDocument.BlockElement at all." It will
> work with a plain Element.

Thank You again. Now, I understand. Here my new implementation:

void makeParagraph(HTML.Tag paragraphType)
{
  if( textEditor instanceof JEditorPane )
  {
      HTMLDocument  htmlDoc = (HTMLDocument) textEditor.getDocument();
      int           position = textEditor.getSelectionStart();
      Element       elem = htmlDoc.getParagraphElement(position);

      SimpleAttributeSet  newType = new SimpleAttributeSet(
        elem.getAttributes()
      );
      newType.addAttribute( AttributeSet.NameAttribute, paragraphType );
      htmlDoc.setParagraphAttributes(position, 0, newType, false );
  }
}

Now, it's a very simple implementation, I love it. 8-) Thank You again.

> Christian

Martin

Signature

Firma/Company:                                              CRESD GmbH
Phone: +49-89-65 30 95 63                      Fax: +49-89-65 30 95 64
WWW:                                               http://www.cresd.de
S-Mail:                                Freibadstr. 14, D-81543 München
PGP-Key:                            http://www.cresd.de/edv/pgpkey.txt
Open BC (Einladung)            http://www.openbc.com/go/invita/4561755

Christian Kaufhold - 26 Nov 2006 21:45 GMT
Martin G??ckler <martin@gaeckler.de> wrote:

> void makeParagraph(HTML.Tag paragraphType)

package access?

> {
>    if( textEditor instanceof JEditorPane )
>    {
>        HTMLDocument  htmlDoc = (HTMLDocument) textEditor.getDocument();
>        int           position = textEditor.getSelectionStart();
>        Element       elem = htmlDoc.getParagraphElement(position);

>        SimpleAttributeSet  newType = new SimpleAttributeSet(

>          elem.getAttributes()

Note: I was confused about, this -- for an 'false' argument (add instead of
set, you don't need this).

>        );
>        newType.addAttribute( AttributeSet.NameAttribute, paragraphType );
>        htmlDoc.setParagraphAttributes(position, 0, newType, false );

Then you can also use

        htmlDoc.setParagraphAttributes(textEditor.getSelectionStart(), textEditor.getSelectionEnd() - textEditor.getSelectionStart(), newType, false);

Christian
Martin Gäckler - 28 Nov 2006 20:52 GMT
Christian Kaufhold schrieb:
> Martin G??ckler <martin@gaeckler.de> wrote:
>
[quoted text clipped - 25 lines]
>
> Christian

Christian,

Thank You again. That's an excellent idea. And "Yes" you are right, the
wrong boolean parameter replace was a little bit confusing. Now, I can
change the type of more than one paragraph at a time without loosing any
other attribute:

void makeParagraph(HTML.Tag paragraphType)
{
  if( textEditor instanceof JEditorPane )
  {
    HTMLDocument  htmlDoc = (HTMLDocument) textEditor.getDocument();
    int           start = textEditor.getSelectionStart();
    int           end = textEditor.getSelectionEnd();

    SimpleAttributeSet  newType = new SimpleAttributeSet();

    newType.addAttribute( AttributeSet.NameAttribute, paragraphType );
    htmlDoc.setParagraphAttributes(start, end-start, newType, false );
  }
}

For example I got this change from

<p align="center">hello <u>world</u></p>
<p align="right">hello <u>world</u></p>

to

<h1 align="center">hello <u>world</u></h1>
<h1 align="right">hello <u>world</u></h1>

Yeah, yeah, yeah. Excellent! 8-))))))) (= big smile)

Have a nice day and thank you again.

Martin

Signature

Firma/Company:                                              CRESD GmbH
Phone: +49-89-65 30 95 63                      Fax: +49-89-65 30 95 64
WWW:                                               http://www.cresd.de
S-Mail:                                Freibadstr. 14, D-81543 München
PGP-Key:                            http://www.cresd.de/edv/pgpkey.txt
Open BC (Einladung)            http://www.openbc.com/go/invita/4561755



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.