2e679ac6238f6626875f9af601e87c89775f7a46
[old-projects.git] / ekit / com / hexidec / ekit / action / FormatAction.java
1 /*
2 GNU Lesser General Public License
3
4 FormatAction
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 javax.swing.JTextPane;
26 import javax.swing.text.SimpleAttributeSet;
27 import javax.swing.text.StyledEditorKit;
28 import javax.swing.text.html.HTML;
29
30 import com.hexidec.ekit.EkitCore;
31 import com.hexidec.ekit.component.*;
32
33 import com.hexidec.util.Translatrix;
34
35 /** Class for implementing HTML format actions
36 * (NOTE : Does not toggle. User must use the "Clear Format" option to remove formatting correctly.)
37 */
38 public class FormatAction extends StyledEditorKit.StyledTextAction
39 {
40 protected EkitCore parentEkit;
41 HTML.Tag htmlTag;
42
43 public FormatAction(EkitCore ekit, String actionName, HTML.Tag inTag)
44 {
45 super(actionName);
46 parentEkit = ekit;
47 htmlTag = inTag;
48 }
49
50 public void actionPerformed(ActionEvent ae)
51 {
52 JTextPane parentTextPane = parentEkit.getTextPane();
53 String selText = parentTextPane.getSelectedText();
54 int textLength = -1;
55 if(selText != null)
56 {
57 textLength = selText.length();
58 }
59 if(selText == null || textLength < 1)
60 {
61 SimpleInfoDialog sidWarn = new SimpleInfoDialog(parentEkit.getFrame(), "", true, Translatrix.getTranslationString("ErrorNoTextSelected"), SimpleInfoDialog.ERROR);
62 }
63 else
64 {
65 SimpleAttributeSet sasText = new SimpleAttributeSet(parentTextPane.getCharacterAttributes());
66 sasText.addAttribute(htmlTag, new SimpleAttributeSet());
67 int caretOffset = parentTextPane.getSelectionStart();
68 parentTextPane.select(caretOffset, caretOffset + textLength);
69 parentTextPane.setCharacterAttributes(sasText, false);
70 parentEkit.refreshOnUpdate();
71 parentTextPane.select(caretOffset, caretOffset + textLength);
72 }
73 }
74 }
75