.restoreState(actionBarAdvisorState));
}
// Read window's bounds and state.
final Rectangle [] displayBounds = new Rectangle[1];
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() {
displayBounds[0] = getShell().getDisplay().getBounds();
}});
final Rectangle shellBounds = new Rectangle(0, 0, 0, 0);
final IMemento fastViewMem = memento
.getChild(IWorkbenchConstants.TAG_FAST_VIEW_DATA);
if (fastViewMem != null) {
if (fastViewBar != null) {
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() {
fastViewBar.restoreState(fastViewMem);
}});
}
}
Integer bigInt = memento.getInteger(IWorkbenchConstants.TAG_X);
shellBounds.x = bigInt == null ? 0 : bigInt.intValue();
bigInt = memento.getInteger(IWorkbenchConstants.TAG_Y);
shellBounds.y = bigInt == null ? 0 : bigInt.intValue();
bigInt = memento.getInteger(IWorkbenchConstants.TAG_WIDTH);
shellBounds.width = bigInt == null ? 0 : bigInt.intValue();
bigInt = memento.getInteger(IWorkbenchConstants.TAG_HEIGHT);
shellBounds.height = bigInt == null ? 0 : bigInt.intValue();
if (!shellBounds.isEmpty()) {
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() {
if (!shellBounds.intersects(displayBounds[0])) {
Rectangle clientArea = getShell().getDisplay().getClientArea();
shellBounds.x = clientArea.x;
shellBounds.y = clientArea.y;
}
getShell().setBounds(shellBounds);
}});
}
if ("true".equals(memento.getString(IWorkbenchConstants.TAG_MAXIMIZED))) { //$NON-NLS-1$
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() {
getShell().setMaximized(true);
}});
}
if ("true".equals(memento.getString(IWorkbenchConstants.TAG_MINIMIZED))) { //$NON-NLS-1$
// getShell().setMinimized(true);
}
// restore the width of the perspective bar
if (perspectiveSwitcher != null) {
perspectiveSwitcher.restoreState(memento);
}
// Restore the cool bar order by creating all the tool bar contribution
// items
// This needs to be done before pages are created to ensure proper
// canonical creation
// of cool items
final ICoolBarManager2 coolBarMgr = (ICoolBarManager2) getCoolBarManager2();
if (coolBarMgr != null) {
IMemento coolBarMem = memento
.getChild(IWorkbenchConstants.TAG_COOLBAR_LAYOUT);
if (coolBarMem != null) {
// Check if the layout is locked
final Integer lockedInt = coolBarMem
.getInteger(IWorkbenchConstants.TAG_LOCKED);
StartupThreading.runWithoutExceptions(new StartupRunnable(){
public void runWithException() {
if ((lockedInt != null) && (lockedInt.intValue() == 1)) {
coolBarMgr.setLockLayout(true);
} else {
coolBarMgr.setLockLayout(false);
}
}});
// The new layout of the cool bar manager
ArrayList coolBarLayout = new ArrayList();
// Traverse through all the cool item in the memento
IMemento contributionMems[] = coolBarMem
.getChildren(IWorkbenchConstants.TAG_COOLITEM);
for (int i = 0; i < contributionMems.length; i++) {
IMemento contributionMem = contributionMems[i];
String type = contributionMem
.getString(IWorkbenchConstants.TAG_ITEM_TYPE);
if (type == null) {
// Do not recognize that type
continue;
}
String id = contributionMem
.getString(IWorkbenchConstants.TAG_ID);
// Prevent duplicate items from being read back in.
IContributionItem existingItem = coolBarMgr.find(id);
if ((id != null) && (existingItem != null)) {
if (Policy.DEBUG_TOOLBAR_DISPOSAL) {
System.out
.println("Not loading duplicate cool bar item: " + id); //$NON-NLS-1$
}
coolBarLayout.add(existingItem);
continue;
}
IContributionItem newItem = null;
if (type.equals(IWorkbenchConstants.TAG_TYPE_SEPARATOR)) {
if (id != null) {
newItem = new Separator(id);
} else {
newItem = new Separator();
}
} else if (id != null) {
if (type
.equals(IWorkbenchConstants.TAG_TYPE_GROUPMARKER)) {
newItem = new GroupMarker(id);
} else if (type
.equals(IWorkbenchConstants.TAG_TYPE_TOOLBARCONTRIBUTION)
|| type
.equals(IWorkbenchConstants.TAG_TYPE_PLACEHOLDER)) {
// Get Width and height
Integer width = contributionMem
.getInteger(IWorkbenchConstants.TAG_ITEM_X);
Integer height = contributionMem
.getInteger(IWorkbenchConstants.TAG_ITEM_Y);
// Look for the object in the current cool bar
// manager
IContributionItem oldItem = coolBarMgr.find(id);
// If a tool bar contribution item already exists
// for this id then use the old object
if (oldItem != null) {
newItem = oldItem;
} else {
IActionBarPresentationFactory actionBarPresentation = getActionBarPresentationFactory();
newItem = actionBarPresentation.createToolBarContributionItem(
actionBarPresentation.createToolBarManager(), id);
if (type
.equals(IWorkbenchConstants.TAG_TYPE_PLACEHOLDER)) {
IToolBarContributionItem newToolBarItem = (IToolBarContributionItem) newItem;
if (height != null) {
newToolBarItem.setCurrentHeight(height
.intValue());
}
if (width != null) {
newToolBarItem.setCurrentWidth(width
.intValue());
}
newItem = new PlaceholderContributionItem(
newToolBarItem);
}
// 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);
}
}
// Set the current height and width
if ((width != null)
&& (newItem instanceof IToolBarContributionItem)) {
((IToolBarContributionItem) newItem)
.setCurrentWidth(width.intValue());
}
if ((height != null)
&& (newItem instanceof IToolBarContributionItem)) {
((IToolBarContributionItem) newItem)
.setCurrentHeight(height.intValue());
}
}
}
// Add new item into cool bar manager
if (newItem != null) {
coolBarLayout.add(newItem);
newItem.setParent(coolBarMgr);
coolBarMgr.markDirty();
}
}
// We need to check if we have everything we need in the layout.
final ArrayList finalLayout = new ArrayList();
IContributionItem[] existingItems = coolBarMgr.getItems();
for (int i = 0; i < existingItems.length; i++) {
IContributionItem existingItem = existingItems[i];
/*
* This line shouldn't be necessary, but is here for
* robustness.
*/
if (existingItem == null) {
continue;
}
boolean found = false;
Iterator layoutItemItr = coolBarLayout.iterator();
while (layoutItemItr.hasNext()) {
IContributionItem layoutItem = (IContributionItem) layoutItemItr
.next();
if ((layoutItem != null)
&& (layoutItem.equals(existingItem))) {
found = true;
break;
}
}
if (!found) {
if (existingItem != null) {
finalLayout.add(existingItem);
}
}
}
// Set the cool bar layout to the given layout.
finalLayout.addAll(coolBarLayout);
final IContributionItem[] itemsToSet = new IContributionItem[finalLayout
.size()];
finalLayout.toArray(itemsToSet);
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() {
coolBarMgr.setItems(itemsToSet);
}});
} else {
// For older workbenchs
coolBarMem = memento
.getChild(IWorkbenchConstants.TAG_TOOLBAR_LAYOUT);
if (coolBarMem != null) {
// Restore an older layout
restoreOldCoolBar(coolBarMem);
}
}
}
// Recreate each page in the window.
IWorkbenchPage newActivePage = null;
IMemento[] pageArray = memento
.getChildren(IWorkbenchConstants.TAG_PAGE);
for (int i = 0; i < pageArray.length; i++) {
final IMemento pageMem = pageArray[i];
String strFocus = pageMem.getString(IWorkbenchConstants.TAG_FOCUS);
if (strFocus == null || strFocus.length() == 0) {
continue;
}
// Get the input factory.
final IAdaptable [] input = new IAdaptable[1];
final IMemento inputMem = pageMem.getChild(IWorkbenchConstants.TAG_INPUT);
if (inputMem != null) {
final String factoryID = inputMem
.getString(IWorkbenchConstants.TAG_FACTORY_ID);
if (factoryID == null) {
WorkbenchPlugin
.log("Unable to restore page - no input factory ID."); //$NON-NLS-1$
result.add(unableToRestorePage(pageMem));
continue;
}
try {
UIStats.start(UIStats.RESTORE_WORKBENCH,
"WorkbenchPageFactory"); //$NON-NLS-1$
StartupThreading
.runWithoutExceptions(new StartupRunnable() {
public void runWithException() throws Throwable {
IElementFactory factory = PlatformUI
.getWorkbench().getElementFactory(
factoryID);
if (factory == null) {
WorkbenchPlugin
.log("Unable to restore page - cannot instantiate input factory: " + factoryID); //$NON-NLS-1$
result
.add(unableToRestorePage(pageMem));
return;
}
// Get the input element.
input[0] = factory.createElement(inputMem);
}
});
if (input[0] == null) {
WorkbenchPlugin
.log("Unable to restore page - cannot instantiate input element: " + factoryID); //$NON-NLS-1$
result.add(unableToRestorePage(pageMem));
continue;
}
} finally {
UIStats.end(UIStats.RESTORE_WORKBENCH, factoryID,
"WorkbenchPageFactory"); //$NON-NLS-1$
}
}
// Open the perspective.
final IAdaptable finalInput = input[0];
final WorkbenchPage [] newPage = new WorkbenchPage[1];
try {
StartupThreading.runWithWorkbenchExceptions(new StartupRunnable(){
public void runWithException() throws WorkbenchException {
newPage[0] = new WorkbenchPage(WorkbenchWindow.this, finalInput);
}});
result.add(newPage[0].restoreState(pageMem, activeDescriptor));
pageList.add(newPage[0]);
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() throws Throwable {
firePageOpened(newPage[0]);
}});
} catch (WorkbenchException e) {
WorkbenchPlugin
.log(
"Unable to restore perspective - constructor failed.", e); //$NON-NLS-1$
result.add(e.getStatus());
continue;
}
if (strFocus != null && strFocus.length() > 0) {
newActivePage = newPage[0];
}
}
// If there are no pages create a default.
if (pageList.isEmpty()) {
try {
final String defPerspID = getWorkbenchImpl().getPerspectiveRegistry()
.getDefaultPerspective();
if (defPerspID != null) {
final WorkbenchPage [] newPage = new WorkbenchPage[1];
StartupThreading.runWithWorkbenchExceptions(new StartupRunnable() {
public void runWithException() throws Throwable {
newPage[0] = new WorkbenchPage(WorkbenchWindow.this, defPerspID,
getDefaultPageInput());
}});
pageList.add(newPage[0]);
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() throws Throwable {
firePageOpened(newPage[0]);
}});
}
} catch (WorkbenchException e) {
WorkbenchPlugin
.log(
"Unable to create default perspective - constructor failed.", e); //$NON-NLS-1$
result.add(e.getStatus());
String productName = WorkbenchPlugin.getDefault()
.getProductName();
if (productName == null) {
productName = ""; //$NON-NLS-1$
}
getShell().setText(productName);
}
}
// Set active page.
if (newActivePage == null) {
newActivePage = pageList.getNextActive();
}
final IWorkbenchPage myPage = newActivePage;
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() throws Throwable {
setActivePage(myPage);
}});
final IMemento introMem = memento.getChild(IWorkbenchConstants.TAG_INTRO);
if (introMem != null) {
StartupThreading.runWithoutExceptions(new StartupRunnable() {
public void runWithException() throws Throwable {
getWorkbench()
.getIntroManager()
.showIntro(