Bug 513901 - Starting Eclipse runs into a lot of "Illegal Thread Access"
SWTExceptions

Added a new SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP preference,
disabled by default. Changed IDEWorkbenchPlugin to use that preference
and do not initialize hidden problems view if preference is "false".

Additionally fixed the init code to not access Display.getDefault() in
non-UI thread to avoid SWTExceptions.

Change-Id: Ife2cf2bdfce6cd6a5f3dcd51b6c2bcc7a3131119
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/dialogs/IDEStartupPreferencePage.java b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/dialogs/IDEStartupPreferencePage.java
index 5a38c00..4d989f0 100644
--- a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/dialogs/IDEStartupPreferencePage.java
+++ b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/dialogs/IDEStartupPreferencePage.java
@@ -38,6 +38,8 @@
 
     private Button refreshButton;
 
+	private Button showProblemsButton;
+
     private Button exitPromptButton;
 
     @Override
@@ -49,6 +51,7 @@
         Composite composite = createComposite(parent);
 
         createRefreshWorkspaceOnStartupPref(composite);
+		createProblemsViewOnStartupPref(composite);
         createExitPromptPref(composite);
 
         Label space = new Label(composite,SWT.NONE);
@@ -69,6 +72,10 @@
         refreshButton
                 .setSelection(store
                         .getDefaultBoolean(IDEInternalPreferences.REFRESH_WORKSPACE_ON_STARTUP));
+
+		showProblemsButton.setSelection(
+				store.getDefaultBoolean(IDEInternalPreferences.SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP));
+
         exitPromptButton
                 .setSelection(store
                         .getDefaultBoolean(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW));
@@ -87,6 +94,9 @@
         store.setValue(IDEInternalPreferences.REFRESH_WORKSPACE_ON_STARTUP,
                 refreshButton.getSelection());
 
+		store.setValue(IDEInternalPreferences.SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP,
+				showProblemsButton.getSelection());
+
         // store the exit prompt on last window close setting
         store.setValue(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW,
                 exitPromptButton.getSelection());
@@ -104,6 +114,15 @@
                 IDEInternalPreferences.REFRESH_WORKSPACE_ON_STARTUP));
     }
 
+	protected void createProblemsViewOnStartupPref(Composite composite) {
+		showProblemsButton = new Button(composite, SWT.CHECK);
+		showProblemsButton.setText(IDEWorkbenchMessages.StartupPreferencePage_showProblemsButton);
+		showProblemsButton.setFont(composite.getFont());
+		showProblemsButton
+				.setSelection(getIDEPreferenceStore()
+						.getBoolean(IDEInternalPreferences.SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP));
+	}
+
     protected void createExitPromptPref(Composite composite) {
         exitPromptButton = new Button(composite, SWT.CHECK);
         exitPromptButton.setText(IDEWorkbenchMessages.StartupPreferencePage_exitPromptButton);
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEInternalPreferences.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEInternalPreferences.java
index 9267c91..5ee09a1 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEInternalPreferences.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEInternalPreferences.java
@@ -141,4 +141,9 @@
      */
     public static final String WARN_ABOUT_WORKSPACE_INCOMPATIBILITY = "WARN_ABOUT_WORKSPACE_INCOMPATIBILITY"; //$NON-NLS-1$
 
+    /**
+     * Show Problems view decorations on startup
+     */
+	public static final String SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP = "SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP"; //$NON-NLS-1$
+
 }
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEPreferenceInitializer.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEPreferenceInitializer.java
index 4f2febe..68ffaa7 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEPreferenceInitializer.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEPreferenceInitializer.java
@@ -91,6 +91,10 @@
 		node.putBoolean(IDEInternalPreferences.SHOW_LOCATION_NAME, true);
 		node.putBoolean(IDEInternalPreferences.SHOW_PERSPECTIVE_IN_TITLE, false);
 		node.putBoolean(IDEInternalPreferences.SHOW_PRODUCT_IN_TITLE, true);
+
+		// by default, don't start hidden problems view to show decoration on
+		// it's icon. See bug 513901
+		node.putBoolean(IDEInternalPreferences.SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP, false);
 	}
 
 	/**
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java
index db7e62b..b6b9a10 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchMessages.java
@@ -608,6 +608,7 @@
 
 	// --- Startup preferences ---
 	public static String StartupPreferencePage_refreshButton;
+	public static String StartupPreferencePage_showProblemsButton;
 	public static String StartupPreferencePage_launchPromptButton;
 	public static String StartupPreferencePage_exitPromptButton;
 
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchPlugin.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchPlugin.java
index 2ab029c..b4d2160 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchPlugin.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/IDEWorkbenchPlugin.java
@@ -21,8 +21,11 @@
 import org.eclipse.core.runtime.IBundleGroup;
 import org.eclipse.core.runtime.IBundleGroupProvider;
 import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.resource.LocalResourceManager;
@@ -342,11 +345,15 @@
 	 * up-to-date.
 	 */
 	private void createProblemsViews() {
+		if (!getDefault().getPreferenceStore()
+				.getBoolean(IDEInternalPreferences.SHOW_PROBLEMS_VIEW_DECORATIONS_ON_STARTUP)) {
+			return;
+		}
 		final Runnable r= new Runnable() {
 			@Override
 			public void run() {
 				IWorkbench workbench = PlatformUI.isWorkbenchRunning() ? PlatformUI.getWorkbench() : null;
-				if (workbench != null && (workbench.getDisplay().isDisposed() || PlatformUI.getWorkbench().isClosing()))
+				if (workbench != null && (workbench.getDisplay().isDisposed() || workbench.isClosing()))
 					return;
 
 				if (workbench == null || workbench.isStarting()) {
@@ -370,9 +377,29 @@
 			}
 		};
 		Display display = Display.getCurrent();
-		if (display != null)
+		if (display != null) {
 			display.timerExec(PROBLEMS_VIEW_CREATION_DELAY, r);
-		else
-			Display.getDefault().asyncExec(r);
+		} else {
+			Job job = new Job("Initializing Problems view") { //$NON-NLS-1$
+				@Override
+				protected IStatus run(IProgressMonitor monitor) {
+					IWorkbench workbench = PlatformUI.getWorkbench();
+					if (workbench == null) {
+						// Workbench not created yet, so avoid using display to
+						// avoid crash like in bug 513901
+						schedule(PROBLEMS_VIEW_CREATION_DELAY);
+						return Status.OK_STATUS;
+					}
+					if (workbench != null && workbench.isClosing()) {
+						return Status.CANCEL_STATUS;
+					}
+					PlatformUI.getWorkbench().getDisplay().asyncExec(r);
+					return Status.OK_STATUS;
+				}
+			};
+			job.setSystem(true);
+			job.setUser(false);
+			job.schedule(PROBLEMS_VIEW_CREATION_DELAY);
+		}
 	}
 }
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties
index cf71856..4986679 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/messages.properties
@@ -587,6 +587,7 @@
 
 # --- Startup preferences ---
 StartupPreferencePage_refreshButton = &Refresh workspace on startup
+StartupPreferencePage_showProblemsButton = &Show Problems view decorations on startup
 StartupPreferencePage_launchPromptButton = Prompt for &workspace on startup
 StartupPreferencePage_exitPromptButton = &Confirm exit when closing last window