|
Added
Link Here
|
| 1 |
package org.eclipse.mylar.internal.team; |
| 2 |
|
| 3 |
|
| 4 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 5 |
import org.eclipse.core.runtime.IExtension; |
| 6 |
import org.eclipse.core.runtime.IExtensionPoint; |
| 7 |
import org.eclipse.core.runtime.Platform; |
| 8 |
import org.eclipse.mylar.context.core.MylarStatusHandler; |
| 9 |
import org.eclipse.mylar.tasks.core.ITask; |
| 10 |
import org.eclipse.mylar.team.ITemplateHandler; |
| 11 |
import org.eclipse.mylar.team.MylarTeamPlugin; |
| 12 |
|
| 13 |
import java.text.MessageFormat; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collections; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* @author Eike Stepper |
| 21 |
*/ |
| 22 |
public class TemplateHandlersManager |
| 23 |
{ |
| 24 |
public static final TemplateHandlersManager INSTANCE = new TemplateHandlersManager(); |
| 25 |
|
| 26 |
private static final String ATTR_CLASS = "class"; |
| 27 |
|
| 28 |
private static final String ATTR_DESCRIPTION = "description"; |
| 29 |
|
| 30 |
private static final String ATTR_RECOGNIZED_KEYWORD = "recognizedKeyword"; |
| 31 |
|
| 32 |
private static final String ELEM_TEMPLATE_HANDLER = "templateHandler"; |
| 33 |
|
| 34 |
private static final String EXT_POINT_TEMPLATE_HANDLERS = "templateHandlers"; |
| 35 |
|
| 36 |
private List<ITemplateHandler> handlers; |
| 37 |
|
| 38 |
private TemplateHandlersManager() |
| 39 |
{ |
| 40 |
readExtensions(); |
| 41 |
} |
| 42 |
|
| 43 |
private void readExtensions() |
| 44 |
{ |
| 45 |
ArrayList<ITemplateHandler> handlerList = new ArrayList<ITemplateHandler>(); |
| 46 |
IExtensionPoint extPoint = Platform.getExtensionRegistry().getExtensionPoint( |
| 47 |
MylarTeamPlugin.PLUGIN_ID, EXT_POINT_TEMPLATE_HANDLERS); |
| 48 |
IExtension[] extensions = extPoint.getExtensions(); |
| 49 |
for (int i = 0; i < extensions.length; i++) |
| 50 |
{ |
| 51 |
IExtension extension = extensions[i]; |
| 52 |
IConfigurationElement[] elements = extension.getConfigurationElements(); |
| 53 |
for (int j = 0; j < elements.length; j++) |
| 54 |
{ |
| 55 |
IConfigurationElement element = elements[j]; |
| 56 |
if (ELEM_TEMPLATE_HANDLER.equals(element.getName())) |
| 57 |
{ |
| 58 |
try |
| 59 |
{ |
| 60 |
String description = element.getAttribute(ATTR_DESCRIPTION); |
| 61 |
String keyword = element.getAttribute(ATTR_RECOGNIZED_KEYWORD); |
| 62 |
ITemplateHandler handler = (ITemplateHandler)element |
| 63 |
.createExecutableExtension(ATTR_CLASS); |
| 64 |
if (handler instanceof AbstractTemplateHandler) |
| 65 |
{ |
| 66 |
((AbstractTemplateHandler)handler).setDescription(description); |
| 67 |
((AbstractTemplateHandler)handler).setRecognizedKeyword(keyword); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
String recognizedKeyword = handler.getRecognizedKeyword(); |
| 72 |
if (recognizedKeyword == null || !recognizedKeyword.equals(keyword)) |
| 73 |
{ |
| 74 |
throw new IllegalArgumentException( |
| 75 |
"Keyword markup does not match handler implementation"); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
handlerList.add(handler); |
| 80 |
} |
| 81 |
catch (Exception e) |
| 82 |
{ |
| 83 |
MylarStatusHandler.log(e, MessageFormat.format( |
| 84 |
"Error while initializing template handler contribution {0} from plugin {1}.", |
| 85 |
element.getAttribute(ATTR_CLASS), element.getContributor().getName())); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
handlerList.trimToSize(); |
| 92 |
this.handlers = Collections.unmodifiableList(handlerList); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the list of contributed {@link ITemplateHandler}. |
| 97 |
* |
| 98 |
* @return a list of {@link ITemplateHandler} |
| 99 |
*/ |
| 100 |
public List<ITemplateHandler> getHandlers() |
| 101 |
{ |
| 102 |
return handlers; |
| 103 |
} |
| 104 |
|
| 105 |
public String generateComment(ITask task, String completedTemplate, |
| 106 |
String progressTemplate) |
| 107 |
{ |
| 108 |
String template = task.isCompleted() ? completedTemplate : progressTemplate; |
| 109 |
return processKeywords(task, template); |
| 110 |
} |
| 111 |
|
| 112 |
public String processKeywords(ITask task, String template) |
| 113 |
{ |
| 114 |
String[] segments = template.split("\\$\\{"); |
| 115 |
StringBuffer buffer = new StringBuffer(segments[0]); |
| 116 |
|
| 117 |
for (int i = 1; i < segments.length; i++) |
| 118 |
{ |
| 119 |
String segment = segments[i]; |
| 120 |
String value = null; |
| 121 |
int brace = segment.indexOf('}'); |
| 122 |
if (brace > 0) |
| 123 |
{ |
| 124 |
String keyword = segment.substring(0, brace); |
| 125 |
value = handleKeyword(task, keyword); |
| 126 |
} |
| 127 |
|
| 128 |
if (value != null) |
| 129 |
{ |
| 130 |
buffer.append(value); |
| 131 |
buffer.append(segment.substring(brace + 1)); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
buffer.append("${"); |
| 136 |
buffer.append(segment); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return buffer.toString(); |
| 141 |
} |
| 142 |
|
| 143 |
private String handleKeyword(ITask task, String keyword) |
| 144 |
{ |
| 145 |
for (ITemplateHandler handler : getHandlers()) |
| 146 |
{ |
| 147 |
if (handler.getRecognizedKeyword().equals(keyword)) |
| 148 |
{ |
| 149 |
return handler.getValue(task); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return null; |
| 154 |
} |
| 155 |
} |