0ed45f499a813fe62951b83aa947e835d6c941c0
[old-projects.git] / ekit / com / hexidec / ekit / action / CustomAction.java
1 /*
2 GNU Lesser General Public License
3
4 CustomAction
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.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;
33
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;
38
39 import com.hexidec.util.Translatrix;
40
41 /** Class for implementing custom HTML insertion actions
42 */
43 public class CustomAction extends StyledEditorKit.StyledTextAction
44 {
45 protected EkitCore parentEkit;
46 private HTML.Tag htmlTag;
47 private Hashtable htmlAttribs;
48
49 public CustomAction(EkitCore ekit, String actionName, HTML.Tag inTag, Hashtable attribs)
50 {
51 super(actionName);
52 parentEkit = ekit;
53 htmlTag = inTag;
54 htmlAttribs = attribs;
55 }
56
57 public CustomAction(EkitCore ekit, String actionName, HTML.Tag inTag)
58 {
59 this(ekit, actionName, inTag, new Hashtable());
60 }
61
62 public void actionPerformed(ActionEvent ae)
63 {
64 Hashtable htmlAttribs2 = new Hashtable();
65 JTextPane parentTextPane = parentEkit.getTextPane();
66 String selText = parentTextPane.getSelectedText();
67 int textLength = -1;
68 if(selText != null)
69 {
70 textLength = selText.length();
71 }
72 if(selText == null || textLength < 1)
73 {
74 SimpleInfoDialog sidWarn = new SimpleInfoDialog(parentEkit.getFrame(), "", true, Translatrix.getTranslationString("ErrorNoTextSelected"), SimpleInfoDialog.ERROR);
75 }
76 else
77 {
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()))
84 {
85 SimpleAttributeSet sasText = null;
86 for(int i = caretOffset; i < caretOffset + internalTextLength; i++)
87 {
88 parentTextPane.select(i, i + 1);
89 sasText = new SimpleAttributeSet(parentTextPane.getCharacterAttributes());
90 Enumeration attribEntries1 = sasText.getAttributeNames();
91 while(attribEntries1.hasMoreElements() && currentAnchor.equals(""))
92 {
93 Object entryKey = attribEntries1.nextElement();
94 Object entryValue = sasText.getAttribute(entryKey);
95 if(entryKey.toString().equals(HTML.Tag.A.toString()))
96 {
97 if(entryValue instanceof SimpleAttributeSet)
98 {
99 Enumeration subAttributes = ((SimpleAttributeSet)entryValue).getAttributeNames();
100 while(subAttributes.hasMoreElements() && currentAnchor.equals(""))
101 {
102 Object subKey = subAttributes.nextElement();
103 if(subKey.toString().toLowerCase().equals("href"))
104 {
105 currentAnchor = ((SimpleAttributeSet)entryValue).getAttribute(subKey).toString();
106 break;
107 }
108 }
109 }
110 }
111 }
112 if(!currentAnchor.equals("")) { break; }
113 }
114 }
115
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()))
120 {
121 if(!htmlAttribs.containsKey("href"))
122 {
123 UserInputAnchorDialog uidInput = new UserInputAnchorDialog(parentEkit, Translatrix.getTranslationString("AnchorDialogTitle"), true, currentAnchor);
124 String newAnchor = uidInput.getInputText();
125 uidInput.dispose();
126 if(newAnchor != null)
127 {
128 htmlAttribs2.put("href", newAnchor);
129 }
130 else
131 {
132 parentEkit.repaint();
133 return;
134 }
135 }
136 }
137 else if(htmlTag.toString().equals(HTML.Tag.FONT.toString()))
138 {
139 if(htmlAttribs.containsKey("face"))
140 {
141 FontSelectorDialog fsdInput = new FontSelectorDialog(parentEkit.getFrame(), Translatrix.getTranslationString("FontDialogTitle"), true, "face", parentTextPane.getSelectedText());
142 String newFace = fsdInput.getFontName();
143 if(newFace != null)
144 {
145 htmlAttribs2.put("face", newFace);
146 }
147 else
148 {
149 parentEkit.repaint();
150 return;
151 }
152 }
153
154 else if(htmlAttribs.containsKey("size"))
155 {
156 htmlAttribs2.put("size", new String((String)htmlAttribs.get("size")));
157 }
158
159 else if(htmlAttribs.containsKey("color"))
160 {
161 Color color = new JColorChooser().showDialog(parentEkit.getFrame(),"Choose Text Color",Color.black);
162 if(color != null)
163 {
164 String redHex = Integer.toHexString(color.getRed());
165 if(redHex.length() < 2)
166 {
167 redHex = "0" + redHex;
168 }
169 String greenHex = Integer.toHexString(color.getGreen());
170 if(greenHex.length() < 2)
171 {
172 greenHex = "0" + greenHex;
173 }
174 String blueHex = Integer.toHexString(color.getBlue());
175 if(blueHex.length() < 2)
176 {
177 blueHex = "0" + blueHex;
178 }
179 htmlAttribs2.put("color", "#" + redHex + greenHex + blueHex);
180 }
181 else
182 {
183 parentEkit.repaint();
184 return;
185 }
186 }
187
188 }
189
190 if(htmlAttribs2.size() > 0)
191 {
192 Enumeration attribEntries = htmlAttribs2.keys();
193 while(attribEntries.hasMoreElements())
194 {
195 Object entryKey = attribEntries.nextElement();
196 Object entryValue = htmlAttribs2.get(entryKey);
197 sasAttr.addAttribute(entryKey, entryValue);
198 }
199 sasTag.addAttribute(htmlTag, sasAttr);
200 parentTextPane.setCharacterAttributes(sasTag, false);
201 parentEkit.refreshOnUpdate();
202 }
203 parentTextPane.select(caretOffset, caretOffset + internalTextLength);
204 parentTextPane.requestFocus();
205 }
206 }
207 }
208