Import of Ekit 0.9h
[old-projects.git] / ekit / com / hexidec / ekit / action / CustomAction.java
CommitLineData
c2da4d40
JL
1/*
2GNU Lesser General Public License
3
4CustomAction
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.Color;
25import java.awt.event.ActionEvent;
26import java.util.Enumeration;
27import java.util.Hashtable;
28import javax.swing.JColorChooser;
29import javax.swing.JTextPane;
30import javax.swing.text.SimpleAttributeSet;
31import javax.swing.text.StyledEditorKit;
32import javax.swing.text.html.HTML;
33
34import com.hexidec.ekit.EkitCore;
35import com.hexidec.ekit.component.FontSelectorDialog;
36import com.hexidec.ekit.component.SimpleInfoDialog;
37import com.hexidec.ekit.component.UserInputAnchorDialog;
38
39import com.hexidec.util.Translatrix;
40
41/** Class for implementing custom HTML insertion actions
42*/
43public 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