Search in sources :

Example 1 with PanelRefresh

use of com.oxygenxml.git.view.refresh.PanelRefresh in project oxygen-git-client-addon by oxygenxml.

the class GitTestBase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Create the unstaged resources panel
    refreshSupport = new PanelRefresh(null) {

        @Override
        protected int getScheduleDelay() {
            // Execute refresh events immediately from tests.
            return 1;
        }
    };
    gitInit();
    ColorTheme colorTheme = Mockito.mock(ColorTheme.class);
    Mockito.when(colorTheme.isDarkTheme()).thenReturn(false);
    StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
    Mockito.when(pluginWSMock.getColorTheme()).thenReturn(colorTheme);
    PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments.length == 2) {
                urls2compare.add((URL) arguments[0]);
                urls2compare.add((URL) arguments[1]);
            }
            return null;
        }
    }).when(pluginWSMock).openDiffFilesApplication(Mockito.any(), Mockito.any());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments != null && arguments.length > 0) {
                toOpen.add((URL) invocation.getArguments()[0]);
            }
            return null;
        }
    }).when(pluginWSMock).open(Mockito.any());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments().length > 0) {
                WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
                editorChangeListeners.add(listener);
            }
            return null;
        }
    }).when(pluginWSMock).addEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
            editorChangeListeners.remove(listener);
            return null;
        }
    }).when(pluginWSMock).removeEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
    Mockito.when(pluginWSMock.getEditorAccess((URL) Mockito.any(), Mockito.anyInt())).then(new Answer<WSEditor>() {

        @Override
        public WSEditor answer(InvocationOnMock invocation) throws Throwable {
            WSEditor wsEditorMock = createWSEditorMock((URL) invocation.getArguments()[0]);
            return wsEditorMock;
        }
    });
    XMLUtilAccess xmlUtilAccess = Mockito.mock(XMLUtilAccess.class);
    Mockito.when(xmlUtilAccess.escapeTextValue(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object object = invocation.getArguments()[0];
            return object != null ? (String) object : "";
        }
    });
    Mockito.when(xmlUtilAccess.unescapeAttributeValue(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object object = invocation.getArguments()[0];
            return object != null ? (String) object : "";
        }
    });
    Mockito.doReturn(xmlUtilAccess).when(pluginWSMock).getXMLUtilAccess();
    UtilAccess utilAccessMock = Mockito.mock(UtilAccess.class);
    Mockito.when(pluginWSMock.getUtilAccess()).thenReturn(utilAccessMock);
    Mockito.when(utilAccessMock.locateFile((URL) Mockito.any())).then(new Answer<File>() {

        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            URL url = (URL) invocation.getArguments()[0];
            String path = url.getPath();
            if (PlatformDetectionUtil.isWin() && path.startsWith("/")) {
                path = path.substring(1, path.length());
            }
            return new File(url.getPath());
        }
    });
    // PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().getFileName()
    Mockito.when(utilAccessMock.getFileName(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            String file = (String) invocation.getArguments()[0];
            file = file.replace('\\', '/');
            int index = file.lastIndexOf("/");
            return index != -1 ? file.substring(index + 1) : file;
        }
    });
    Mockito.when(utilAccessMock.uncorrectURL(Mockito.anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].toString().replace("%20", " ");
        }
    });
    ImageUtilities imgUtils = Mockito.mock(ImageUtilities.class);
    Mockito.when(pluginWSMock.getImageUtilities()).thenReturn(imgUtils);
    Mockito.when(imgUtils.loadIcon((URL) Mockito.any())).thenAnswer(new Answer<ImageIcon>() {

        @Override
        public ImageIcon answer(InvocationOnMock invocation) throws Throwable {
            URL url = (URL) invocation.getArguments()[0];
            return new ImageIcon(url);
        }
    });
    ProjectController projectCtrlMock = Mockito.mock(ProjectController.class);
    Mockito.when(pluginWSMock.getProjectManager()).thenReturn(projectCtrlMock);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).when(projectCtrlMock).refreshFolders(Mockito.any());
    WSOptionsStorage wsOptions = new TestWsOptionsStorage();
    Mockito.when(pluginWSMock.getOptionsStorage()).thenReturn(wsOptions);
    installGitProtocol();
    GitAccess gitAccess = GitAccess.getInstance();
    GitController ctrl = new GitController(gitAccess);
    ctrl.addGitListener(new GitEventAdapter() {

        private Repository oldRepository;

        @Override
        public void operationAboutToStart(GitEventInfo info) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                try {
                    oldRepository = gitAccess.getRepository();
                } catch (NoRepositorySelected e) {
                // Ignore
                }
            }
        }

        @Override
        public void operationSuccessfullyEnded(GitEventInfo info) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                if (oldRepository != null) {
                    loadedRepos.remove(oldRepository);
                }
                oldRepository = null;
            }
        }

        @Override
        public void operationFailed(GitEventInfo info, Throwable t) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                oldRepository = null;
            }
        }
    });
    OptionsManager.getInstance().loadOptions(wsOptions);
    gitAccess.getStatusCache().installEditorsHook(pluginWSMock);
}
Also used : ImageUtilities(ro.sync.exml.workspace.api.images.ImageUtilities) ImageIcon(javax.swing.ImageIcon) GitController(com.oxygenxml.git.view.event.GitController) UtilAccess(ro.sync.exml.workspace.api.util.UtilAccess) XMLUtilAccess(ro.sync.exml.workspace.api.util.XMLUtilAccess) URL(java.net.URL) ColorTheme(ro.sync.exml.workspace.api.util.ColorTheme) XMLUtilAccess(ro.sync.exml.workspace.api.util.XMLUtilAccess) WSOptionsStorage(ro.sync.exml.workspace.api.options.WSOptionsStorage) ProjectController(ro.sync.exml.workspace.api.standalone.project.ProjectController) Repository(org.eclipse.jgit.lib.Repository) GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) PanelRefresh(com.oxygenxml.git.view.refresh.PanelRefresh) WSEditorChangeListener(ro.sync.exml.workspace.api.listeners.WSEditorChangeListener) WSEditor(ro.sync.exml.workspace.api.editor.WSEditor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StandalonePluginWorkspace(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace) File(java.io.File)

Example 2 with PanelRefresh

use of com.oxygenxml.git.view.refresh.PanelRefresh in project oxygen-git-client-addon by oxygenxml.

the class OxygenGitPluginExtension method applicationStarted.

/**
 * @see WorkspaceAccessPluginExtension#applicationStarted(StandalonePluginWorkspace)
 */
@Override
public void applicationStarted(final StandalonePluginWorkspace pluginWS) {
    this.pluginWorkspaceAccess = pluginWS;
    OptionsManager.getInstance().loadOptions(pluginWS.getOptionsStorage());
    gitRefreshSupport = new PanelRefresh(RemoteRepositoryChangeWatcher.createWatcher(pluginWS, gitController), () -> menuBar);
    final GitActionsManager gitActionsManager = new GitActionsManager(gitController, this, this, gitRefreshSupport);
    menuBar = new GitActionsMenuBar(gitActionsManager);
    pluginWS.addMenuBarCustomizer(menuBar);
    try {
        if (!"true".equals(System.getProperty(GitAddonSystemProperties.USE_JSCH_FOR_SSH_OPERATIONS))) {
            org.eclipse.jgit.transport.SshSessionFactory.setInstance(new org.eclipse.jgit.transport.sshd.SshdSessionFactory(null, new ResolvingProxyDataFactory()));
        }
        AuthenticationInterceptor.install();
        BlameManager.getInstance().install(gitController);
        // Add Git actions to the contextual menu of the Project view
        ProjectMenuGitActionsProvider projectMenuGitActionsProvider = new ProjectMenuGitActionsProvider(pluginWorkspaceAccess, gitController, OxygenGitPluginExtension.this);
        ProjectViewManager.addPopUpMenuCustomizer(projectMenuGitActionsProvider);
        // Add Git actions to the contextual menu of the current editor page
        pluginWorkspaceAccess.addMenusAndToolbarsContributorCustomizer(menusAndToolbarsCustomizer);
        // Customize the contributed side-views
        pluginWorkspaceAccess.addViewComponentCustomizer(viewInfo -> {
            // The constants' values are defined in plugin.xml
            if (GIT_STAGING_VIEW.equals(viewInfo.getViewID())) {
                customizeGitStagingView(viewInfo, gitActionsManager);
            } else if (GIT_HISTORY_VIEW.equals(viewInfo.getViewID())) {
                customizeHistoryView(viewInfo);
            } else if (GIT_BRANCH_VIEW.equals(viewInfo.getViewID())) {
                customizeBranchView(viewInfo);
            }
        });
        // Listens on the save event in the Oxygen editor and invalidates the cache.
        GitAccess.getInstance().getStatusCache().installEditorsHook(pluginWS);
        // Present the view to the user if it is the first run of the plugin
        final JFrame parentFrame = (JFrame) pluginWorkspaceAccess.getParentFrame();
        parentFrame.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentShown(ComponentEvent e) {
                String key = "view.presented.on.first.run";
                String firstRun = pluginWorkspaceAccess.getOptionsStorage().getOption(key, null);
                if (firstRun == null) {
                    // This is the first run of the plugin.
                    pluginWorkspaceAccess.showView(GIT_STAGING_VIEW, false);
                    pluginWorkspaceAccess.getOptionsStorage().setOption(key, "true");
                }
            }
        });
        // Call the refresh command when the Oxygen window is activated
        parentFrame.addWindowListener(panelRefreshWindowListener);
        LoggingUtil.setupLogger();
    } catch (Throwable t) {
        // NOSONAR
        // Catch Throwable - Runtime exceptions shouldn't affect Oxygen.
        pluginWorkspaceAccess.showErrorMessage(t.getMessage());
        LOGGER.error(t.getMessage(), t);
    }
    UtilAccess utilAccess = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess();
    utilAccess.addCustomEditorVariablesResolver(new GitEditorVariablesResolver(gitController));
}
Also used : GitActionsMenuBar(com.oxygenxml.git.view.actions.GitActionsMenuBar) UtilAccess(ro.sync.exml.workspace.api.util.UtilAccess) GitActionsManager(com.oxygenxml.git.view.actions.GitActionsManager) GitEditorVariablesResolver(com.oxygenxml.git.editorvars.GitEditorVariablesResolver) PanelRefresh(com.oxygenxml.git.view.refresh.PanelRefresh) JFrame(javax.swing.JFrame) ComponentEvent(java.awt.event.ComponentEvent) ResolvingProxyDataFactory(com.oxygenxml.git.auth.ResolvingProxyDataFactory) ComponentAdapter(java.awt.event.ComponentAdapter)

Aggregations

PanelRefresh (com.oxygenxml.git.view.refresh.PanelRefresh)2 UtilAccess (ro.sync.exml.workspace.api.util.UtilAccess)2 ResolvingProxyDataFactory (com.oxygenxml.git.auth.ResolvingProxyDataFactory)1 GitEditorVariablesResolver (com.oxygenxml.git.editorvars.GitEditorVariablesResolver)1 GitActionsManager (com.oxygenxml.git.view.actions.GitActionsManager)1 GitActionsMenuBar (com.oxygenxml.git.view.actions.GitActionsMenuBar)1 GitController (com.oxygenxml.git.view.event.GitController)1 GitEventInfo (com.oxygenxml.git.view.event.GitEventInfo)1 ComponentAdapter (java.awt.event.ComponentAdapter)1 ComponentEvent (java.awt.event.ComponentEvent)1 File (java.io.File)1 URL (java.net.URL)1 ImageIcon (javax.swing.ImageIcon)1 JFrame (javax.swing.JFrame)1 Repository (org.eclipse.jgit.lib.Repository)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 WSEditor (ro.sync.exml.workspace.api.editor.WSEditor)1 ImageUtilities (ro.sync.exml.workspace.api.images.ImageUtilities)1 WSEditorChangeListener (ro.sync.exml.workspace.api.listeners.WSEditorChangeListener)1 WSOptionsStorage (ro.sync.exml.workspace.api.options.WSOptionsStorage)1