PaletteRoot root = new PaletteRoot();
String[] category = getCategories();
for(int i=0;i<category.length;i++){
PaletteDrawer group = new PaletteDrawer(category[i]);
IPaletteItem[] items = getPaletteItems(category[i]);
for(int j=0;j<items.length;j++){
HTMLPaletteEntry entry = new HTMLPaletteEntry(items[j].getLabel(),null,items[j].getImageDescriptor());
tools.put(entry,items[j]);
group.add(entry);
}
root.add(group);
}
viewer.setPaletteRoot(root);
viewer.addPaletteListener(new PaletteListener(){
public void activeToolChanged(PaletteViewer palette, ToolEntry tool){
ToolEntryEditPart part = (ToolEntryEditPart)palette.getEditPartRegistry().get(tool);
if(part!=null){
// get the active editor
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = page.getActiveEditor();
// execute processing of the palette item
if(editorPart!=null){
if(editorPart instanceof HTMLSourceEditor){
IPaletteItem item = (IPaletteItem)tools.get(tool);
item.execute((HTMLSourceEditor)editorPart);
}
else if(editorPart instanceof IPaletteTarget){
IPaletteItem item = (IPaletteItem)tools.get(tool);
item.execute(((IPaletteTarget)editorPart).getPaletteTarget());
}
}
// unset palette selection
part.setToolSelected(false);
}
}
});
viewer.getControl().addMouseListener(new MouseAdapter(){
public void mouseUp(MouseEvent e) {
// set focus to the active editor
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editorPart = page.getActiveEditor();
if(editorPart!=null){
editorPart.setFocus();
}
}
});
// apply configuration (too long!!)
IPreferenceStore store = HTMLPlugin.getDefault().getPreferenceStore();
String xml = store.getString(HTMLPlugin.PREF_PALETTE_ITEMS);
if(xml!=null){
FuzzyXMLDocument doc = new FuzzyXMLParser().parse(xml);
// apply visible
FuzzyXMLNode[] groups = HTMLUtil.selectXPathNodes(doc.getDocumentElement(),"/palette/groups/group");
for(int i=0;i<groups.length;i++){
FuzzyXMLElement group = (FuzzyXMLElement)groups[i];
String name = group.getAttributeNode("name").getValue();
boolean visible = new Boolean(group.getAttributeNode("visible").getValue()).booleanValue();
List entries = viewer.getPaletteRoot().getChildren();
PaletteDrawer drawer = null;
for(int j=0;j<entries.size();j++){
drawer = (PaletteDrawer)entries.get(j);
if(drawer.getLabel().equals(name)){
drawer.setVisible(visible);
break;
} else {
drawer =null;
}
}
if(drawer==null){
drawer = new PaletteDrawer(name);
drawer.setVisible(visible);
viewer.getPaletteRoot().add(drawer);
}
}
// add user items
FuzzyXMLNode[] items = HTMLUtil.selectXPathNodes(doc.getDocumentElement(),"/palette/items/item");
String[] categories = getCategories();
for(int i=0;i<items.length;i++){
FuzzyXMLElement item = (FuzzyXMLElement)items[i];
String name = item.getAttributeNode("name").getValue();
String group = item.getAttributeNode("group").getValue();
String text = item.getValue();
if(Arrays.binarySearch(categories,group)<0){
addPaletteItem(group,new DefaultPaletteItem(name,
HTMLPlugin.getDefault().getImageRegistry().getDescriptor(HTMLPlugin.ICON_TAG),
text));
}
}
}
List entries = viewer.getPaletteRoot().getChildren();
for(int i=0;i<entries.size();i++){
PaletteDrawer group = (PaletteDrawer)entries.get(i);
if(Arrays.binarySearch(defaultCategories,group.getLabel()) < 0){
IPaletteItem[] items = getPaletteItems(group.getLabel());
ArrayList itemList = new ArrayList();
for(int j=0;j<items.length;j++){
HTMLPaletteEntry entry = new HTMLPaletteEntry(items[j].getLabel(),null,items[j].getImageDescriptor());
tools.put(entry,items[j]);
itemList.add(entry);
}
group.setChildren(itemList);
}
}
// create toolbar
createToolBar();