Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/hexidec/ekit/action/FormatAction.java


1   /*
2   GNU Lesser General Public License
3   
4   FormatAction
5   Copyright (C) 2000-2002  Howard A 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  import com.hexidec.ekit.dialog.*;
33  
34  import com.hexidec.util.Translatrix;
35  
36  /** Class for implementing HTML format actions
37  * (NOTE : Does not toggle. User must use the "Clear Format" option to remove formatting correctly.)
38  */
39  public class FormatAction extends StyledEditorKit.StyledTextAction
40  {
41    protected EkitCore parentEkit;
42    HTML.Tag htmlTag;
43          private String strAlign;
44          
45    public FormatAction(EkitCore ekit, String actionName, HTML.Tag inTag)
46    {            
47                  super(actionName); 
48      parentEkit = ekit;
49      htmlTag    = inTag;
50    }
51        
52    public void actionPerformed(ActionEvent ae)
53    {
54      JTextPane parentTextPane = parentEkit.getTextPane();
55      String selText = parentTextPane.getSelectedText();
56      int textLength = -1;
57                  
58                  
59      if(selText != null)
60      {
61        textLength = selText.length();
62      }
63      if(selText == null || textLength < 1)
64      {
65        SimpleInfoDialog sidWarn = new SimpleInfoDialog(parentEkit.getFrame(), "", true, Translatrix.getTranslationString("ErrorNoTextSelected"), SimpleInfoDialog.ERROR);
66      }
67      else
68      {
69      
70        SimpleAttributeSet sasText = new SimpleAttributeSet();
71                          SimpleAttributeSet sasAttr = new SimpleAttributeSet();     
72        sasText.addAttribute(htmlTag, sasAttr);
73        int caretOffset = parentTextPane.getSelectionStart();
74        parentTextPane.select(caretOffset, caretOffset + textLength);
75        parentTextPane.setCharacterAttributes(sasText, false);
76                          parentEkit.refreshOnUpdate();
77        parentTextPane.select(caretOffset, caretOffset + textLength);
78      }
79    }
80  }
81