2 GNU Lesser General Public License
5 Copyright (C) 2000 Howard Kistler
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.
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.
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
22 package com
.hexidec
.ekit
.action
;
24 import java
.awt
.Color
;
25 import java
.awt
.event
.ActionEvent
;
26 import java
.util
.Enumeration
;
27 import java
.util
.Hashtable
;
28 import javax
.swing
.JColorChooser
;
29 import javax
.swing
.JTextPane
;
30 import javax
.swing
.text
.SimpleAttributeSet
;
31 import javax
.swing
.text
.StyledEditorKit
;
32 import javax
.swing
.text
.html
.HTML
;
34 import com
.hexidec
.ekit
.EkitCore
;
35 import com
.hexidec
.ekit
.component
.FontSelectorDialog
;
36 import com
.hexidec
.ekit
.component
.SimpleInfoDialog
;
37 import com
.hexidec
.ekit
.component
.UserInputAnchorDialog
;
39 import com
.hexidec
.util
.Translatrix
;
41 /** Class for implementing custom HTML insertion actions
43 public class CustomAction
extends StyledEditorKit
.StyledTextAction
45 protected EkitCore parentEkit
;
46 private HTML
.Tag htmlTag
;
47 private Hashtable htmlAttribs
;
49 public CustomAction(EkitCore ekit
, String actionName
, HTML
.Tag inTag
, Hashtable attribs
)
54 htmlAttribs
= attribs
;
57 public CustomAction(EkitCore ekit
, String actionName
, HTML
.Tag inTag
)
59 this(ekit
, actionName
, inTag
, new Hashtable());
62 public void actionPerformed(ActionEvent ae
)
64 Hashtable htmlAttribs2
= new Hashtable();
65 JTextPane parentTextPane
= parentEkit
.getTextPane();
66 String selText
= parentTextPane
.getSelectedText();
70 textLength
= selText
.length();
72 if(selText
== null
|| textLength
< 1)
74 SimpleInfoDialog sidWarn
= new SimpleInfoDialog(parentEkit
.getFrame(), "", true
, Translatrix
.getTranslationString("ErrorNoTextSelected"), SimpleInfoDialog
.ERROR
);
78 int caretOffset
= parentTextPane
.getSelectionStart();
79 int internalTextLength
= selText
.length();
80 String currentAnchor
= "";
81 // Somewhat ham-fisted code to obtain the first HREF in the selected text,
82 // which (if found) is passed to the URL HREF request dialog.
83 if(htmlTag
.toString().equals(HTML
.Tag
.A
.toString()))
85 SimpleAttributeSet sasText
= null
;
86 for(int i
= caretOffset
; i
< caretOffset
+ internalTextLength
; i
++)
88 parentTextPane
.select(i
, i
+ 1);
89 sasText
= new SimpleAttributeSet(parentTextPane
.getCharacterAttributes());
90 Enumeration attribEntries1
= sasText
.getAttributeNames();
91 while(attribEntries1
.hasMoreElements() && currentAnchor
.equals(""))
93 Object entryKey
= attribEntries1
.nextElement();
94 Object entryValue
= sasText
.getAttribute(entryKey
);
95 if(entryKey
.toString().equals(HTML
.Tag
.A
.toString()))
97 if(entryValue
instanceof SimpleAttributeSet
)
99 Enumeration subAttributes
= ((SimpleAttributeSet
)entryValue
).getAttributeNames();
100 while(subAttributes
.hasMoreElements() && currentAnchor
.equals(""))
102 Object subKey
= subAttributes
.nextElement();
103 if(subKey
.toString().toLowerCase().equals("href"))
105 currentAnchor
= ((SimpleAttributeSet
)entryValue
).getAttribute(subKey
).toString();
112 if(!currentAnchor
.equals("")) { break; }
116 parentTextPane
.select(caretOffset
, caretOffset
+ internalTextLength
);
117 SimpleAttributeSet sasTag
= new SimpleAttributeSet();
118 SimpleAttributeSet sasAttr
= new SimpleAttributeSet();
119 if(htmlTag
.toString().equals(HTML
.Tag
.A
.toString()))
121 if(!htmlAttribs
.containsKey("href"))
123 UserInputAnchorDialog uidInput
= new UserInputAnchorDialog(parentEkit
, Translatrix
.getTranslationString("AnchorDialogTitle"), true
, currentAnchor
);
124 String newAnchor
= uidInput
.getInputText();
126 if(newAnchor
!= null
)
128 htmlAttribs2
.put("href", newAnchor
);
132 parentEkit
.repaint();
137 else if(htmlTag
.toString().equals(HTML
.Tag
.FONT
.toString()))
139 if(htmlAttribs
.containsKey("face"))
141 FontSelectorDialog fsdInput
= new FontSelectorDialog(parentEkit
.getFrame(), Translatrix
.getTranslationString("FontDialogTitle"), true
, "face", parentTextPane
.getSelectedText());
142 String newFace
= fsdInput
.getFontName();
145 htmlAttribs2
.put("face", newFace
);
149 parentEkit
.repaint();
154 else if(htmlAttribs
.containsKey("size"))
156 htmlAttribs2
.put("size", new String((String
)htmlAttribs
.get("size")));
159 else if(htmlAttribs
.containsKey("color"))
161 Color color
= new JColorChooser().showDialog(parentEkit
.getFrame(),"Choose Text Color",Color
.black
);
164 String redHex
= Integer
.toHexString(color
.getRed());
165 if(redHex
.length() < 2)
167 redHex
= "0" + redHex
;
169 String greenHex
= Integer
.toHexString(color
.getGreen());
170 if(greenHex
.length() < 2)
172 greenHex
= "0" + greenHex
;
174 String blueHex
= Integer
.toHexString(color
.getBlue());
175 if(blueHex
.length() < 2)
177 blueHex
= "0" + blueHex
;
179 htmlAttribs2
.put("color", "#" + redHex
+ greenHex
+ blueHex
);
183 parentEkit
.repaint();
190 if(htmlAttribs2
.size() > 0)
192 Enumeration attribEntries
= htmlAttribs2
.keys();
193 while(attribEntries
.hasMoreElements())
195 Object entryKey
= attribEntries
.nextElement();
196 Object entryValue
= htmlAttribs2
.get(entryKey
);
197 sasAttr
.addAttribute(entryKey
, entryValue
);
199 sasTag
.addAttribute(htmlTag
, sasAttr
);
200 parentTextPane
.setCharacterAttributes(sasTag
, false
);
201 parentEkit
.refreshOnUpdate();
203 parentTextPane
.select(caretOffset
, caretOffset
+ internalTextLength
);
204 parentTextPane
.requestFocus();