001: /*
002: GNU Lesser General Public License
003:
004: ListAutomationAction
005: Copyright (C) 2000 Howard Kistler
006:
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public
009: License as published by the Free Software Foundation; either
010: version 2.1 of the License, or (at your option) any later version.
011:
012: This library is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: Lesser General Public License for more details.
016:
017: You should have received a copy of the GNU Lesser General Public
018: License along with this library; if not, write to the Free Software
019: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.objectweb.salome_tmf.ihm.main.htmleditor.action;
023:
024: import java.awt.event.ActionEvent;
025: import java.util.StringTokenizer;
026:
027: import javax.swing.JEditorPane;
028: import javax.swing.text.BadLocationException;
029: import javax.swing.text.html.HTML;
030: import javax.swing.text.html.HTMLDocument;
031: import javax.swing.text.html.HTMLEditorKit;
032:
033: import org.objectweb.salome_tmf.ihm.languages.Language;
034: import org.objectweb.salome_tmf.ihm.main.htmleditor.EkitCore;
035: import org.objectweb.salome_tmf.ihm.main.htmleditor.component.HTMLUtilities;
036: import org.objectweb.salome_tmf.ihm.main.htmleditor.component.SimpleInfoDialog;
037:
038: /**
039: * Class for automatically creating bulleted lists from selected text
040: */
041: public class ListAutomationAction extends
042: HTMLEditorKit.InsertHTMLTextAction {
043:
044: protected EkitCore parentEkit;
045: private HTML.Tag baseTag;
046: private HTMLUtilities htmlUtilities;
047:
048: public ListAutomationAction(EkitCore ekit, String sLabel,
049: HTML.Tag listType) {
050: super (sLabel, "", listType, HTML.Tag.LI);
051: parentEkit = ekit;
052: baseTag = listType;
053: htmlUtilities = new HTMLUtilities(ekit);
054: }
055:
056: public void actionPerformed(ActionEvent ae) {
057: try {
058: JEditorPane jepEditor = (JEditorPane) (parentEkit
059: .getTextPane());
060: String selTextBase = jepEditor.getSelectedText();
061: int textLength = -1;
062: if (selTextBase != null) {
063: textLength = selTextBase.length();
064: }
065: if (selTextBase == null || textLength < 1) {
066: int pos = parentEkit.getCaretPosition();
067: parentEkit.setCaretPosition(pos);
068: if (ae.getActionCommand() != "newListPoint") {
069: if (htmlUtilities.checkParentsTag(HTML.Tag.OL)
070: || htmlUtilities
071: .checkParentsTag(HTML.Tag.UL)) {
072: new SimpleInfoDialog(null, Language
073: .getInstance().getText("Error"), true,
074: Language.getInstance().getText(
075: "ErrorNestedListsNotSupported"));
076: return;
077: }
078: }
079: String sListType = (baseTag == HTML.Tag.OL ? "ol"
080: : "ul");
081: StringBuffer sbNew = new StringBuffer();
082: if (htmlUtilities.checkParentsTag(baseTag)) {
083: sbNew.append("<li></li>");
084: insertHTML(parentEkit.getTextPane(), parentEkit
085: .getExtendedHtmlDoc(), parentEkit
086: .getTextPane().getCaretPosition(), sbNew
087: .toString(), 0, 0, HTML.Tag.LI);
088: } else {
089: sbNew.append("<" + sListType
090: + " class=\"htmldesc\"><li></li></"
091: + sListType + "><p> </p>");
092: insertHTML(parentEkit.getTextPane(), parentEkit
093: .getExtendedHtmlDoc(), parentEkit
094: .getTextPane().getCaretPosition(), sbNew
095: .toString(), 0, 0,
096: (sListType.equals("ol") ? HTML.Tag.OL
097: : HTML.Tag.UL));
098: }
099: parentEkit.refreshOnUpdate();
100: } else {
101: String sListType = (baseTag == HTML.Tag.OL ? "ol"
102: : "ul");
103: HTMLDocument htmlDoc = (HTMLDocument) (jepEditor
104: .getDocument());
105: int iStart = jepEditor.getSelectionStart();
106: int iEnd = jepEditor.getSelectionEnd();
107: String selText = htmlDoc.getText(iStart, iEnd - iStart);
108: StringBuffer sbNew = new StringBuffer();
109: String sToken = ((selText.indexOf("\r") > -1) ? "\r"
110: : "\n");
111: StringTokenizer stTokenizer = new StringTokenizer(
112: selText, sToken);
113: sbNew.append("<" + sListType + " class=\"htmldesc\">");
114: while (stTokenizer.hasMoreTokens()) {
115: sbNew.append("<li>");
116: sbNew.append(stTokenizer.nextToken());
117: sbNew.append("</li>");
118: }
119: sbNew.append("</" + sListType + "><p> </p>");
120: htmlDoc.remove(iStart, iEnd - iStart);
121: insertHTML(jepEditor, htmlDoc, iStart,
122: sbNew.toString(), 1, 1, null);
123: }
124: } catch (BadLocationException ble) {
125: }
126: }
127: }
|