*/
private void processReplacement(final String messageID,
final String chatString,
final String contentType)
{
SwingWorker worker = new SwingWorker()
{
/**
* Called on the event dispatching thread (not on the worker thread)
* after the <code>construct</code> method has returned.
*/
public void finished()
{
String newMessage = (String) get();
if (newMessage != null && !newMessage.equals(chatString))
{
synchronized (scrollToBottomRunnable)
{
scrollToBottomIsPending = true;
try
{
Element elem = document.getElement(messageID);
document.setOuterHTML(elem, newMessage);
}
catch (BadLocationException ex)
{
logger.error("Could not replace chat message", ex);
}
catch (IOException ex)
{
logger.error("Could not replace chat message", ex);
}
}
}
}
public Object construct() throws Exception
{
ConfigurationService cfg
= GuiActivator.getConfigurationService();
boolean isEnabled
= cfg.getBoolean(
ReplacementProperty.REPLACEMENT_ENABLE,
true);
Matcher divMatcher = DIV_PATTERN.matcher(chatString);
String openingTag = "";
String msgStore = chatString;
String closingTag = "";
if (divMatcher.find())
{
openingTag = divMatcher.group(1);
msgStore = divMatcher.group(2);
closingTag = divMatcher.group(3);
}
for (Map.Entry<String, ReplacementService> entry
: GuiActivator.getReplacementSources().entrySet())
{
ReplacementService source = entry.getValue();
boolean isSmiley
= source instanceof SmiliesReplacementService;
if (!(cfg.getBoolean(
ReplacementProperty.getPropertyName(
source.getSourceName()),
true)
&& (isEnabled || isSmiley)))
continue;
String sourcePattern = source.getPattern();
Pattern p
= Pattern.compile(
sourcePattern,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(msgStore);
StringBuilder msgBuff = new StringBuilder();
int startPos = 0;
while (m.find())
{
msgBuff.append(msgStore.substring(startPos, m.start()));
startPos = m.end();
String group = m.group();
String temp = source.getReplacement(group);
String group0 = m.group(0);
if(!temp.equals(group0)
|| source.getSourceName().equals("DIRECTIMAGE"))
{
if(isSmiley)
{
msgBuff.append(
ChatHtmlUtils.createEndPlainTextTag(
contentType));
msgBuff.append("<IMG SRC=\"");
}
else
{
msgBuff.append(
"<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\"");
}
msgBuff.append(temp);
msgBuff.append("\" BORDER=\"0\" ALT=\"");
msgBuff.append(group0);
msgBuff.append("\"></IMG>");
if(isSmiley)
msgBuff.append(
ChatHtmlUtils.createStartPlainTextTag(
contentType));
}
else
{
msgBuff.append(group);
}
}
msgBuff.append(msgStore.substring(startPos));
/*
* replace the msgStore variable with the current replaced
* message before next iteration
*/
String msgBuffString = msgBuff.toString();
if (!msgBuffString.equals(msgStore))
msgStore = msgBuffString;
}
return openingTag + msgStore + closingTag;
}
};
worker.start();
}