5bc4a72e77262fe972202ad1579ea109b08dbab9
[old-projects.git] / ekit / com / hexidec / ekit / action / ListAutomationAction.java
1 /*
2 GNU Lesser General Public License
3
4 ListAutomationAction
5 Copyright (C) 2000-2003 Howard Kistler
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 package com.hexidec.ekit.action;
23
24 import java.awt.event.ActionEvent;
25 import java.util.StringTokenizer;
26 import javax.swing.JEditorPane;
27 import javax.swing.text.BadLocationException;
28 import javax.swing.text.html.HTML;
29 import javax.swing.text.html.HTMLDocument;
30 import javax.swing.text.html.HTMLEditorKit;
31
32 import com.hexidec.ekit.EkitCore;
33 import com.hexidec.ekit.component.*;
34
35 import com.hexidec.util.Translatrix;
36
37 /** Class for automatically creating bulleted lists from selected text
38 */
39 public class ListAutomationAction extends HTMLEditorKit.InsertHTMLTextAction
40 {
41 protected EkitCore parentEkit;
42 private HTML.Tag baseTag;
43 private String sListType;
44 private HTMLUtilities htmlUtilities;
45
46 public ListAutomationAction(EkitCore ekit, String sLabel, HTML.Tag listType)
47 {
48 super(sLabel, "", listType, HTML.Tag.LI);
49 parentEkit = ekit;
50 baseTag = listType;
51 htmlUtilities = new HTMLUtilities(ekit);
52 }
53
54 public void actionPerformed(ActionEvent ae)
55 {
56 try
57 {
58 JEditorPane jepEditor = (JEditorPane)(parentEkit.getTextPane());
59 String selTextBase = jepEditor.getSelectedText();
60 int textLength = -1;
61 if(selTextBase != null)
62 {
63 textLength = selTextBase.length();
64 }
65 if(selTextBase == null || textLength < 1)
66 {
67 int pos = parentEkit.getCaretPosition();
68 parentEkit.setCaretPosition(pos);
69 if(ae.getActionCommand() != "newListPoint")
70 {
71 if(htmlUtilities.checkParentsTag(HTML.Tag.OL) || htmlUtilities.checkParentsTag(HTML.Tag.UL))
72 {
73 new SimpleInfoDialog(parentEkit.getFrame(), Translatrix.getTranslationString("Error"), true, Translatrix.getTranslationString("ErrorNestedListsNotSupported"));
74 return;
75 }
76 }
77 String sListType = (baseTag == HTML.Tag.OL ? "ol" : "ul");
78 StringBuffer sbNew = new StringBuffer();
79 if(htmlUtilities.checkParentsTag(baseTag))
80 {
81 sbNew.append("<li></li>");
82 insertHTML(parentEkit.getTextPane(), parentEkit.getExtendedHtmlDoc(), parentEkit.getTextPane().getCaretPosition(), sbNew.toString(), 0, 0, HTML.Tag.LI);
83 }
84 else
85 {
86 sbNew.append("<" + sListType + "><li></li></" + sListType + "><p>&nbsp;</p>");
87 insertHTML(parentEkit.getTextPane(), parentEkit.getExtendedHtmlDoc(), parentEkit.getTextPane().getCaretPosition(), sbNew.toString(), 0, 0, (sListType.equals("ol") ? HTML.Tag.OL : HTML.Tag.UL));
88 }
89 parentEkit.refreshOnUpdate();
90 }
91 else
92 {
93 String sListType = (baseTag == HTML.Tag.OL ? "ol" : "ul");
94 HTMLDocument htmlDoc = (HTMLDocument)(jepEditor.getDocument());
95 int iStart = jepEditor.getSelectionStart();
96 int iEnd = jepEditor.getSelectionEnd();
97 String selText = htmlDoc.getText(iStart, iEnd - iStart);
98 StringBuffer sbNew = new StringBuffer();
99 String sToken = ((selText.indexOf("\r") > -1) ? "\r" : "\n");
100 StringTokenizer stTokenizer = new StringTokenizer(selText, sToken);
101 sbNew.append("<" + sListType + ">");
102 while(stTokenizer.hasMoreTokens())
103 {
104 sbNew.append("<li>");
105 sbNew.append(stTokenizer.nextToken());
106 sbNew.append("</li>");
107 }
108 sbNew.append("</" + sListType + "><p>&nbsp;</p>");
109 htmlDoc.remove(iStart, iEnd - iStart);
110 insertHTML(jepEditor, htmlDoc, iStart, sbNew.toString(), 1, 1, null);
111 }
112 }
113 catch (BadLocationException ble) {}
114 }
115 }