Import of Ekit 0.9h
[old-projects.git] / ekit / com / hexidec / ekit / action / FormatAction.java
CommitLineData
c2da4d40
JL
1/*
2GNU Lesser General Public License
3
4FormatAction
6ce136da 5Copyright (C) 2000 Howard Kistler
c2da4d40
JL
6
7This library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22package com.hexidec.ekit.action;
23
24import java.awt.event.ActionEvent;
25import javax.swing.JTextPane;
26import javax.swing.text.SimpleAttributeSet;
27import javax.swing.text.StyledEditorKit;
28import javax.swing.text.html.HTML;
29
30import com.hexidec.ekit.EkitCore;
31import com.hexidec.ekit.component.*;
32
33import 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 */
38public 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