package org.jbpm.ui.editor;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.CreationToolEntry;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.jbpm.ui.JpdlVersionRegistry;
import org.jbpm.ui.SharedImages;
import org.jbpm.ui.common.ElementTypeDefinition;
public class DesignerPaletteRoot extends PaletteRoot {
private final DesignerEditor editor;
private final String jpdlVersion;
private final String notation;
public DesignerPaletteRoot(DesignerEditor editor) {
this.editor = editor;
this.jpdlVersion = editor.getDefinition().getJpdlVersion();
this.notation = editor.getDefinition().getNotation();
addControls();
}
private void addControls() {
List<PaletteGroup> categories = new ArrayList<PaletteGroup>();
categories.add(createDefaultControls());
for (String name : JpdlVersionRegistry.getPaletteCategories(jpdlVersion)) {
categories.add(createCategory(name));
}
addAll(categories);
}
private PaletteGroup createCategory(String categoryName) {
PaletteGroup controls = new PaletteGroup(categoryName);
controls.setId(categoryName);
for (ElementTypeDefinition type : JpdlVersionRegistry.getPaletteEntriesFor(jpdlVersion, categoryName).values()) {
PaletteEntry entry = createEntry(type);
if (entry != null) {
controls.add(entry);
}
}
return controls;
}
private PaletteEntry createEntry(ElementTypeDefinition elementType) {
CreationFactory factory = new GEFElementCreationFactory(elementType.getName(), editor.getDefinition());
IConfigurationElement info = elementType.getEntryConfigElement();
String id = info.getAttribute("id");
String label = info.getAttribute("label");
String type = info.getAttribute("type");
String imageName = info.getAttribute("icon");
ImageDescriptor iconSmall = SharedImages.getImageDescriptor("icons/" + notation + "/palette/" + imageName, true);
if (iconSmall == null) {
return null;
}
PaletteEntry entry = null;
if ("node".equals(type)) {
entry = new CreationToolEntry(label, null, factory, iconSmall, null);
}
if ("connection".equals(type)) {
entry = new ConnectionCreationToolEntry(label, null, factory, iconSmall, null);
}
entry.setId(id);
return entry;
}
public void refreshActionsVisibility() {
for (PaletteGroup category : (List<PaletteGroup>) getChildren()) {
for (PaletteEntry entry : (List<PaletteEntry>) category.getChildren()) {
if ("org.jbpm.ui.palette.3.Action".equals(entry.getId())) {
entry.setVisible(editor.getDefinition().isShowActions());
return;
}
}
}
}
private PaletteGroup createDefaultControls() {
PaletteGroup controls = new PaletteGroup("Default Tools");
controls.setId("org.jbpm.palette.DefaultTools");
addSelectionTool(controls);
return controls;
}
private void addSelectionTool(PaletteGroup controls) {
ToolEntry tool = new SelectionToolEntry();
tool.setId("org.jbpm.ui.palette.Selection");
controls.add(tool);
setDefaultEntry(tool);
}
}