diff --git a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java index b0e68ac05305..97a7961f7bd6 100644 --- a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java +++ b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java @@ -21,17 +21,16 @@ import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.actionSystem.Separator; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.PersistentStateComponent; -import com.intellij.openapi.project.*; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; +import com.intellij.openapi.project.ProjectManagerAdapter; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.registry.Registry; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.wm.IdeFrame; -import com.intellij.openapi.wm.WelcomeScreen; -import com.intellij.openapi.wm.impl.IdeRootPane; -import com.intellij.openapi.wm.impl.welcomeScreen.DefaultWelcomeScreen; +import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.messages.MessageBus; import org.jetbrains.annotations.NotNull; @@ -208,14 +207,7 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon synchronized (myState) { myState.recentPaths.clear(); } - IdeFrame frame = e.getData(IdeFrame.KEY); - if (frame != null) { - IdeRootPane rootPane = (IdeRootPane) frame.getComponent(); - final WelcomeScreen welcomeScreen = rootPane.getWelcomeScreen(); - if (welcomeScreen instanceof DefaultWelcomeScreen) { - ((DefaultWelcomeScreen)welcomeScreen).hideRecentProjectsPanel(); - } - } + WelcomeFrame.clearRecents(); } } }; diff --git a/platform/platform-impl/src/com/intellij/ide/actions/CloseProjectAction.java b/platform/platform-impl/src/com/intellij/ide/actions/CloseProjectAction.java index 8933c8a86c39..93ce95b6d151 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/CloseProjectAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/CloseProjectAction.java @@ -24,6 +24,7 @@ import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; +import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; import com.intellij.projectImport.ProjectAttachProcessor; public class CloseProjectAction extends AnAction implements DumbAware { @@ -33,6 +34,7 @@ public class CloseProjectAction extends AnAction implements DumbAware { ProjectUtil.closeAndDispose(project); RecentProjectsManagerBase.getInstance().updateLastProjectPath(); + WelcomeFrame.showIfNoProjectOpened(); } public void update(AnActionEvent event){ diff --git a/platform/platform-impl/src/com/intellij/idea/IdeaApplication.java b/platform/platform-impl/src/com/intellij/idea/IdeaApplication.java index ce2fe3dc895d..9becc362dc80 100644 --- a/platform/platform-impl/src/com/intellij/idea/IdeaApplication.java +++ b/platform/platform-impl/src/com/intellij/idea/IdeaApplication.java @@ -35,13 +35,11 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.updateSettings.impl.UpdateChecker; import com.intellij.openapi.updateSettings.impl.UpdateSettings; -import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.IconLoader; -import com.intellij.openapi.util.InvalidDataException; -import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.*; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.wm.WindowManager; -import com.intellij.openapi.wm.impl.WindowManagerImpl; +import com.intellij.openapi.wm.ex.WindowManagerEx; +import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; import com.intellij.ui.Splash; import com.intellij.util.SystemProperties; import com.intellij.util.messages.MessageBus; @@ -289,7 +287,17 @@ public class IdeaApplication { ApplicationEx app = ApplicationManagerEx.getApplicationEx(); // app.setupIdeQueue(IdeEventQueue.getInstance()); try { - ((WindowManagerImpl)WindowManager.getInstance()).showFrame(args); + IdeEventQueue.getInstance().setWindowManager((WindowManagerEx)WindowManager.getInstance()); + + final Ref willOpenProject = new Ref(Boolean.FALSE); + final AppLifecycleListener lifecyclePublisher = app.getMessageBus().syncPublisher(AppLifecycleListener.TOPIC); + lifecyclePublisher.appFrameCreated(args, willOpenProject); + LOG.info("App initialization took " + (System.nanoTime() - PluginManager.startupStart) / 1000000 + " ms"); + PluginManager.dumpPluginClassStatistics(); + if (!willOpenProject.get()) { + WelcomeFrame.showNow(); + lifecyclePublisher.welcomeScreenDisplayed(); + } } catch (PluginException e) { Messages.showErrorDialog("Plugin " + e.getPluginId() + " couldn't be loaded, the IDE will now exit.\n" + diff --git a/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java b/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java index c294cc8ae8f6..869592ff675b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java @@ -862,7 +862,7 @@ public class ApplicationImpl extends ComponentManagerImpl implements Application DialogWrapper.DoNotAskOption option = new DialogWrapper.DoNotAskOption() { @Override public boolean isToBeShown() { - return GeneralSettings.getInstance().isConfirmExit(); + return GeneralSettings.getInstance().isConfirmExit() && ProjectManager.getInstance().getOpenProjects().length > 0; } @Override diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java index cac68ea602c4..6af2d0deca31 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java @@ -54,9 +54,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileEvent; import com.intellij.openapi.vfs.VirtualFileManagerListener; import com.intellij.openapi.vfs.ex.VirtualFileManagerEx; -import com.intellij.openapi.wm.WindowManager; -import com.intellij.openapi.wm.impl.IdeFrameImpl; -import com.intellij.openapi.wm.impl.WindowManagerImpl; +import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; import com.intellij.util.Alarm; import com.intellij.util.ArrayUtil; import com.intellij.util.TimeoutUtil; @@ -468,12 +466,12 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt public Project loadAndOpenProject(@NotNull final String filePath) throws IOException { final Project project = convertAndLoadProject(filePath); if (project == null) { - showWelcomeScreenIfNoProjectOpened(); + WelcomeFrame.showIfNoProjectOpened(); return null; } if (!openProject(project)) { - showWelcomeScreenIfNoProjectOpened(); + WelcomeFrame.showIfNoProjectOpened(); ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { @@ -485,15 +483,6 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt return project; } - private void showWelcomeScreenIfNoProjectOpened() { - if (getOpenProjects().length == 0) { - IdeFrameImpl[] frames = ((WindowManagerImpl)WindowManager.getInstance()).getAllFrames(); - if (frames.length == 1) { - frames[0].showWelcomeScreen(); - } - } - } - /** * Converts and loads the project at the specified path. * @@ -554,6 +543,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt private static void notifyProjectOpenFailed() { ApplicationManager.getApplication().getMessageBus().syncPublisher(AppLifecycleListener.TOPIC).projectOpenFailed(); + WelcomeFrame.showIfNoProjectOpened(); } private void registerExternalProjectFileListener(VirtualFileManagerEx virtualFileManager) { diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeFrameImpl.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeFrameImpl.java index 2705e0ad188e..bd38fea68a19 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeFrameImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeFrameImpl.java @@ -50,6 +50,7 @@ import com.intellij.openapi.wm.impl.status.EncodingPanel; import com.intellij.openapi.wm.impl.status.InsertOverwritePanel; import com.intellij.openapi.wm.impl.status.PositionPanel; import com.intellij.openapi.wm.impl.status.ToggleReadOnlyAttributePanel; +import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; import com.intellij.ui.AppUIUtil; import com.intellij.ui.BalloonLayout; import com.intellij.ui.FocusTrackback; @@ -89,9 +90,9 @@ public class IdeFrameImpl extends JFrame implements IdeFrame, DataProvider { private boolean myRestoreFullscreen; public IdeFrameImpl(ApplicationInfoEx applicationInfoEx, ActionManagerEx actionManager, UISettings uiSettings, DataManager dataManager, - final Application application, final String[] commandLineArgs) { + final Application application) { super(applicationInfoEx.getFullApplicationName()); - myRootPane = new IdeRootPane(actionManager, uiSettings, dataManager, application, commandLineArgs, this); + myRootPane = new IdeRootPane(actionManager, uiSettings, dataManager, application, this); setRootPane(myRootPane); AppUIUtil.updateFrameIcon(this); @@ -184,6 +185,7 @@ public class IdeFrameImpl extends JFrame implements IdeFrame, DataProvider { ProjectUtil.closeAndDispose(myProject); } app.getMessageBus().syncPublisher(AppLifecycleListener.TOPIC).projectFrameClosed(); + WelcomeFrame.showIfNoProjectOpened(); } else { ApplicationManagerEx.getApplicationEx().exit(); @@ -467,10 +469,6 @@ public class IdeFrameImpl extends JFrame implements IdeFrame, DataProvider { return myFrameDecorator != null && myFrameDecorator.isInFullScreen(); } - public void showWelcomeScreen() { - myRootPane.showWelcomeScreen(); - } - @Override public void toFront() { super.toFront(); diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeRootPane.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeRootPane.java index 89aa13f780b5..bfe0e0d2193c 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeRootPane.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/IdeRootPane.java @@ -17,11 +17,9 @@ package com.intellij.openapi.wm.impl; import com.intellij.diagnostic.IdeMessagePanel; import com.intellij.diagnostic.MessagePool; -import com.intellij.ide.AppLifecycleListener; import com.intellij.ide.DataManager; import com.intellij.ide.actions.CustomizeUIAction; import com.intellij.ide.actions.ViewToolbarAction; -import com.intellij.ide.plugins.PluginManager; import com.intellij.ide.ui.UISettings; import com.intellij.ide.ui.UISettingsListener; import com.intellij.ide.ui.customization.CustomActionsSchema; @@ -29,22 +27,16 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.actionSystem.ex.ActionManagerEx; import com.intellij.openapi.application.Application; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.impl.ProjectLifecycleListener; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.wm.*; import com.intellij.openapi.wm.impl.status.IdeStatusBarImpl; import com.intellij.openapi.wm.impl.status.MemoryUsagePanel; -import com.intellij.openapi.wm.impl.welcomeScreen.DefaultWelcomeScreen; import com.intellij.ui.PopupHandler; import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.messages.MessageBusConnection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -60,7 +52,6 @@ import java.util.List; // Made public and non-final for Fabrique public class IdeRootPane extends JRootPane implements UISettingsListener { - private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.wm.impl.IdeRootPane"); /** * Toolbar and status bar. @@ -80,8 +71,6 @@ public class IdeRootPane extends JRootPane implements UISettingsListener { private final ActionManager myActionManager; private final UISettings myUISettings; - private WelcomeScreen myWelcomeScreen; - private JComponent myWelcomePane; private final boolean myGlassPaneInitialized; private final IdeGlassPaneImpl myGlassPane; @@ -91,7 +80,7 @@ public class IdeRootPane extends JRootPane implements UISettingsListener { private final Disposable myDisposable= Disposer.newDisposable(); IdeRootPane(ActionManagerEx actionManager, UISettings uiSettings, DataManager dataManager, - final Application application, final String[] commandLineArgs, IdeFrame frame){ + final Application application, IdeFrame frame){ myActionManager = actionManager; myUISettings = uiSettings; @@ -110,15 +99,6 @@ public class IdeRootPane extends JRootPane implements UISettingsListener { myUISettingsListener=new MyUISettingsListenerImpl(); setJMenuBar(new IdeMenuBar(actionManager, dataManager)); - final Ref willOpenProject = new Ref(Boolean.FALSE); - final AppLifecycleListener lifecyclePublisher = application.getMessageBus().syncPublisher(AppLifecycleListener.TOPIC); - lifecyclePublisher.appFrameCreated(commandLineArgs, willOpenProject); - LOG.info("App initialization took " + (System.nanoTime() - PluginManager.startupStart) / 1000000 + " ms"); - PluginManager.dumpPluginClassStatistics(); - if (!willOpenProject.get()) { - showWelcomeScreen(); - lifecyclePublisher.welcomeScreenDisplayed(); - } myGlassPane = new IdeGlassPaneImpl(this); setGlassPane(myGlassPane); @@ -128,34 +108,6 @@ public class IdeRootPane extends JRootPane implements UISettingsListener { Disposer.register(application, myDisposable); } - void showWelcomeScreen() { - for (WelcomeScreenProvider provider : WelcomeScreenProvider.EP_NAME.getExtensions()) { - myWelcomeScreen = provider.createWelcomeScreen(this); - if (myWelcomeScreen != null) break; - } - if (myWelcomeScreen == null) { - myWelcomeScreen = new DefaultWelcomeScreen(this); - } - Disposer.register(myDisposable, myWelcomeScreen); - myWelcomePane = myWelcomeScreen.getWelcomePanel(); - myContentPane.add(myWelcomePane); - updateToolbarVisibility(); - final MessageBusConnection connect = ApplicationManager.getApplication().getMessageBus().connect(); - connect.subscribe(ProjectLifecycleListener.TOPIC, new ProjectLifecycleListener.Adapter() { - @Override - public void projectComponentsInitialized(Project project) { - Disposer.dispose(myWelcomeScreen); - myWelcomeScreen = null; - myWelcomePane = null; - connect.disconnect(); - } - }); - } - - public WelcomeScreen getWelcomeScreen() { - return myWelcomeScreen; - } - public void setGlassPane(final Component glass) { if (myGlassPaneInitialized) throw new IllegalStateException("Setting of glass pane for IdeFrame is prohibited"); super.setGlassPane(glass); @@ -187,29 +139,14 @@ public class IdeRootPane extends JRootPane implements UISettingsListener { contentPane.remove(myToolWindowsPane); } - hideWelcomeScreen(contentPane); - myToolWindowsPane = toolWindowsPane; if(myToolWindowsPane != null) { contentPane.add(myToolWindowsPane,BorderLayout.CENTER); } - else if (!myApplication.isDisposeInProgress()) { - showWelcomeScreen(); - } contentPane.revalidate(); } - private void hideWelcomeScreen(JComponent contentPane) { - if (myWelcomePane != null) { - Disposer.dispose(myWelcomeScreen); - contentPane.remove(myWelcomePane); - myWelcomeScreen = null; - myWelcomePane = null; - updateToolbarVisibility(); - } - } - protected final Container createContentPane(){ myContentPane = new JPanel(new BorderLayout()); myContentPane.setBackground(Color.GRAY); @@ -305,7 +242,7 @@ public class IdeRootPane extends JRootPane implements UISettingsListener { } private void updateToolbarVisibility(){ - myToolbar.setVisible(myUISettings.SHOW_MAIN_TOOLBAR && myWelcomeScreen == null); + myToolbar.setVisible(myUISettings.SHOW_MAIN_TOOLBAR); } private void updateStatusBarVisibility(){ diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/WindowManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/WindowManagerImpl.java index 6288ccea52a5..dbfb48c6e0d8 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/WindowManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/WindowManagerImpl.java @@ -19,7 +19,6 @@ import com.intellij.Patches; import com.intellij.ide.AppLifecycleListener; import com.intellij.ide.DataManager; import com.intellij.ide.GeneralSettings; -import com.intellij.ide.IdeEventQueue; import com.intellij.ide.impl.DataManagerImpl; import com.intellij.ide.ui.UISettings; import com.intellij.openapi.Disposable; @@ -40,9 +39,7 @@ import com.intellij.openapi.wm.IdeFrame; import com.intellij.openapi.wm.StatusBar; import com.intellij.openapi.wm.WindowManagerListener; import com.intellij.openapi.wm.ex.WindowManagerEx; -import com.intellij.ui.ScreenUtil; import com.intellij.util.Alarm; -import com.intellij.util.ArrayUtil; import com.intellij.util.EventDispatcher; import com.intellij.util.messages.MessageBus; import com.intellij.util.ui.UIUtil; @@ -222,25 +219,6 @@ public final class WindowManagerImpl extends WindowManagerEx implements Applicat } } - public void showFrame(final String[] args) { - IdeEventQueue.getInstance().setWindowManager(this); - final IdeFrameImpl frame = new IdeFrameImpl(myApplicationInfoEx, myActionManager, myUiSettings, myDataManager, - ApplicationManager.getApplication(), args); - myProject2Frame.put(null, frame); - - if (myFrameBounds == null - || !ScreenUtil.isVisible(myFrameBounds)) { //avoid situations when IdeFrame is out of all screens - Rectangle rect = ScreenUtil.getScreenRectangle(0, 0); - int yParts = rect.height / 6; - int xParts = rect.width / 5; - myFrameBounds = new Rectangle(xParts, yParts, xParts * 3, yParts * 4); - } - - frame.setBounds(myFrameBounds); - frame.setVisible(true); - frame.setExtendedState(myFrameExtendedState); - } - public IdeFrameImpl[] getAllFrames() { final Collection ideFrames = myProject2Frame.values(); return ideFrames.toArray(new IdeFrameImpl[ideFrames.size()]); @@ -558,7 +536,7 @@ public final class WindowManagerImpl extends WindowManagerEx implements Applicat } else { frame = new IdeFrameImpl((ApplicationInfoEx)ApplicationInfo.getInstance(), ActionManagerEx.getInstanceEx(), UISettings.getInstance(), - DataManager.getInstance(), ApplicationManager.getApplication(), ArrayUtil.EMPTY_STRING_ARRAY); + DataManager.getInstance(), ApplicationManager.getApplication()); final Rectangle bounds = ProjectFrameBounds.getInstance(project).getBounds(); if (bounds != null) { frame.setBounds(bounds); @@ -612,13 +590,8 @@ public final class WindowManagerImpl extends WindowManagerEx implements Applicat frame.setFileTitle(null, null); myProject2Frame.remove(project); - if (myProject2Frame.isEmpty()) { - myProject2Frame.put(null, frame); - } - else { - Disposer.dispose(frame.getStatusBar()); - frame.dispose(); - } + Disposer.dispose(frame.getStatusBar()); + frame.dispose(); } public final void disposeRootFrame() { diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/DefaultWelcomeScreen.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/DefaultWelcomeScreen.java index 5c80ae70f0fa..2db6ec695fdc 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/DefaultWelcomeScreen.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/DefaultWelcomeScreen.java @@ -35,7 +35,6 @@ import com.intellij.openapi.util.IconLoader; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.wm.WelcomeScreen; -import com.intellij.openapi.wm.impl.IdeRootPane; import com.intellij.ui.*; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.EmptyIcon; @@ -121,7 +120,7 @@ public class DefaultWelcomeScreen implements WelcomeScreen { return myWelcomePanel; } - public DefaultWelcomeScreen(IdeRootPane rootPane) { + public DefaultWelcomeScreen(JComponent rootPane) { initApplicationSpecificImages(); myWelcomePanel = new JPanel(new GridBagLayout()); @@ -188,7 +187,7 @@ public class DefaultWelcomeScreen implements WelcomeScreen { return topPanel; } - private void setUpMainPanel(IdeRootPane rootPane) { + private void setUpMainPanel(JComponent rootPane) { final ActionManager actionManager = ActionManager.getInstance(); // Create QuickStarts group of actions @@ -228,7 +227,7 @@ public class DefaultWelcomeScreen implements WelcomeScreen { myWelcomePanel.setTransferHandler(new OpenProjectTransferHandler(myWelcomePanel)); } - private void setUpRecentProjectsPanel(final IdeRootPane rootPane, final AnAction[] recentProjectsActions) { + private void setUpRecentProjectsPanel(final JComponent rootPane, final AnAction[] recentProjectsActions) { myRecentProjectsPanel.setBackground(MAIN_PANEL_BACKGROUND); JLabel caption = new JLabel("Recent Projects"); diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/WelcomeFrame.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/WelcomeFrame.java new file mode 100644 index 000000000000..f6e56a9b7e4b --- /dev/null +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/WelcomeFrame.java @@ -0,0 +1,140 @@ +/* + * @author max + */ +package com.intellij.openapi.wm.impl.welcomeScreen; + +import com.intellij.openapi.application.Application; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ModalityState; +import com.intellij.openapi.application.ex.ApplicationManagerEx; +import com.intellij.openapi.project.DumbAwareRunnable; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; +import com.intellij.openapi.project.ProjectManagerAdapter; +import com.intellij.openapi.util.DimensionService; +import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.wm.WelcomeScreen; +import com.intellij.openapi.wm.WelcomeScreenProvider; +import com.intellij.openapi.wm.WindowManager; +import com.intellij.openapi.wm.impl.IdeFrameImpl; +import com.intellij.openapi.wm.impl.WindowManagerImpl; +import com.intellij.ui.ScreenUtil; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +public class WelcomeFrame extends JFrame { + private static final String DIMENSION_KEY = "WELCOME_SCREEN"; + private static WelcomeFrame ourInstance; + private final WelcomeScreen myScreen; + + public WelcomeFrame() { + JRootPane rootPane = getRootPane(); + final WelcomeScreen screen = createScreen(rootPane); + + setContentPane(screen.getWelcomePanel()); + + ProjectManager.getInstance().addProjectManagerListener(new ProjectManagerAdapter() { + @Override + public void projectOpened(Project project) { + dispose(); + } + }); + + myScreen = screen; + setupCloseAction(); + } + + @Override + public void dispose() { + super.dispose(); + + Disposer.dispose(myScreen); + + //noinspection AssignmentToStaticFieldFromInstanceMethod + ourInstance = null; + } + + private void setupCloseAction() { + setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + addWindowListener( + new WindowAdapter() { + public void windowClosing(final WindowEvent e) { + dispose(); + + final Application app = ApplicationManager.getApplication(); + app.invokeLater(new DumbAwareRunnable() { + public void run() { + if (app.isDisposed()) { + ApplicationManagerEx.getApplicationEx().exit(); + return; + } + + final Project[] openProjects = ProjectManager.getInstance().getOpenProjects(); + if (openProjects.length == 0) { + ApplicationManagerEx.getApplicationEx().exit(); + } + } + }, ModalityState.NON_MODAL); + } + } + ); + } + + public static void clearRecents() { + if (ourInstance != null) { + WelcomeScreen screen = ourInstance.myScreen; + if (screen instanceof DefaultWelcomeScreen) { + ((DefaultWelcomeScreen)screen).hideRecentProjectsPanel(); + } + } + } + + private static WelcomeScreen createScreen(JRootPane rootPane) { + WelcomeScreen screen = null; + for (WelcomeScreenProvider provider : WelcomeScreenProvider.EP_NAME.getExtensions()) { + screen = provider.createWelcomeScreen(rootPane); + if (screen != null) break; + } + if (screen == null) { + screen = new DefaultWelcomeScreen(rootPane); + } + return screen; + } + + + public static void showNow() { + if (ourInstance == null) { + WelcomeFrame frame = new WelcomeFrame(); + frame.pack(); + DimensionService dimensionService = DimensionService.getInstance(); + Point location = dimensionService.getLocation(DIMENSION_KEY); + if (location == null) { + Rectangle screenBounds = ScreenUtil.getScreenRectangle(new Point(0, 0)); + location = new Point( + screenBounds.x + (screenBounds.width - frame.getWidth()) / 2, + screenBounds.y + (screenBounds.height - frame.getHeight()) / 3 + ); + } + frame.setLocation(location); + dimensionService.setLocation(DIMENSION_KEY, location); + frame.setVisible(true); + + ourInstance = frame; + } + } + + public static void showIfNoProjectOpened() { + ApplicationManager.getApplication().invokeLater(new DumbAwareRunnable() { + @Override + public void run() { + IdeFrameImpl[] frames = ((WindowManagerImpl)WindowManager.getInstance()).getAllFrames(); + if (frames.length == 0) { + showNow(); + } + } + }, ModalityState.NON_MODAL); + } +}