private boolean restoreOldCoolBar(IMemento coolbarMem) {
// Make sure the tag exist
if (coolbarMem == null) {
return false;
}
ICoolBarManager2 coolBarMgr = (ICoolBarManager2) getCoolBarManager2();
// Check to see if layout is locked
Integer locked = coolbarMem.getInteger(IWorkbenchConstants.TAG_LOCKED);
boolean state = (locked != null) && (locked.intValue() == 1);
coolBarMgr.setLockLayout(state);
// Get the visual layout
IMemento visibleLayout = coolbarMem
.getChild(IWorkbenchConstants.TAG_TOOLBAR_LAYOUT);
ArrayList visibleWrapIndicies = new ArrayList();
ArrayList visibleItems = new ArrayList();
if (visibleLayout != null) {
if (readLayout(visibleLayout, visibleItems, visibleWrapIndicies) == false) {
return false;
}
}
// Get the remembered layout
IMemento rememberedLayout = coolbarMem
.getChild(IWorkbenchConstants.TAG_LAYOUT);
ArrayList rememberedWrapIndicies = new ArrayList();
ArrayList rememberedItems = new ArrayList();
if (rememberedLayout != null) {
if (readLayout(rememberedLayout, rememberedItems,
rememberedWrapIndicies) == false) {
return false;
}
}
// Create the objects
if (visibleItems != null) {
// Merge remembered layout into visible layout
if (rememberedItems != null) {
// Traverse through all the remembered items
int currentIndex = 0;
for (Iterator i = rememberedItems.iterator(); i.hasNext(); currentIndex++) {
String id = (String) i.next();
int index = -1;
for (Iterator iter = visibleItems.iterator(); iter
.hasNext();) {
String visibleId = (String) iter.next();
if (visibleId.equals(id)) {
index = visibleItems.indexOf(visibleId);
break;
}
}
// The item is not in the visible list
if (index == -1) {
int insertAt = Math.max(0, Math.min(currentIndex,
visibleItems.size()));
boolean separateLine = false;
// Check whether this item is on a separate line
for (Iterator iter = rememberedWrapIndicies.iterator(); iter
.hasNext();) {
Integer wrapIndex = (Integer) iter.next();
if (wrapIndex.intValue() <= insertAt) {
insertAt = visibleItems.size();
// Add new wrap index for this Item
visibleWrapIndicies.add(new Integer(insertAt));
separateLine = true;
}
}
// Add item to array list
visibleItems.add(insertAt, id);
// If the item was not on a separate line then adjust
// the visible wrap indicies
if (!separateLine) {
// Adjust visible wrap indicies
for (int j = 0; j < visibleWrapIndicies.size(); j++) {
Integer index2 = (Integer) visibleWrapIndicies
.get(j);
if (index2.intValue() >= insertAt) {
visibleWrapIndicies.set(j, new Integer(
index2.intValue() + 1));
}
}
}
}
}
}
// The new layout of the cool bar manager
ArrayList coolBarLayout = new ArrayList(visibleItems.size());
// Add all visible items to the layout object
for (Iterator i = visibleItems.iterator(); i.hasNext();) {
String id = (String) i.next();
// Look for the object in the current cool bar manager
IContributionItem oldItem = null;
IContributionItem newItem = null;
if (id != null) {
oldItem = coolBarMgr.find(id);
}
// If a tool bar contribution item already exists for this id
// then use the old object
if (oldItem instanceof IToolBarContributionItem) {
newItem = oldItem;
} else {
IActionBarPresentationFactory actionBarPresentaiton = getActionBarPresentationFactory();
newItem = actionBarPresentaiton.createToolBarContributionItem(
actionBarPresentaiton.createToolBarManager(), id);
// make it invisible by default
newItem.setVisible(false);
// Need to add the item to the cool bar manager so that its
// canonical order can be preserved
IContributionItem refItem = findAlphabeticalOrder(
IWorkbenchActionConstants.MB_ADDITIONS, id,
coolBarMgr);
if (refItem != null) {
coolBarMgr.insertAfter(refItem.getId(), newItem);
} else {
coolBarMgr.add(newItem);
}
}
// Add new item into cool bar manager
if (newItem != null) {
coolBarLayout.add(newItem);
newItem.setParent(coolBarMgr);
coolBarMgr.markDirty();
}
}
// Add separators to the displayed Items data structure
int offset = 0;
for (int i = 1; i < visibleWrapIndicies.size(); i++) {
int insertAt = ((Integer) visibleWrapIndicies.get(i))
.intValue()
+ offset;
coolBarLayout.add(insertAt, new Separator(
CoolBarManager.USER_SEPARATOR));
offset++;
}
// Add any group markers in their appropriate places
IContributionItem[] items = coolBarMgr.getItems();
for (int i = 0; i < items.length; i++) {
IContributionItem item = items[i];
if (item.isGroupMarker()) {
coolBarLayout.add(Math.max(Math
.min(i, coolBarLayout.size()), 0), item);
}
}
IContributionItem[] itemsToSet = new IContributionItem[coolBarLayout
.size()];
coolBarLayout.toArray(itemsToSet);
coolBarMgr.setItems(itemsToSet);
}
return true;
}