Search in sources :

Example 21 with NoRepositorySelected

use of com.oxygenxml.git.service.NoRepositorySelected in project oxygen-git-client-addon by oxygenxml.

the class HistoryViewContextualMenuPresenter method createOpenFileAction.

/**
 * Creates an action to open a file at a given revision.
 *
 * @param revisionID Revision ID.
 * @param fileStatus File path, relative to the working copy.
 * @param addFileName <code>true</code> to append the name of the file to the name of the action.
 *
 * @return The action that will open the file when invoked.
 */
private Action createOpenFileAction(String revisionID, FileStatus fileStatus, boolean addFileName) {
    String tooltipText = addFileName ? null : TRANSLATOR.getTranslation(Tags.HISTORY_RESOURCE_OPEN_ACTION_TOOLTIP);
    AbstractAction openFileAction = new AbstractAction(getOpenFileActionName(fileStatus, addFileName)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Optional<URL> fileURL = getFileURL(revisionID, fileStatus);
                fileURL.ifPresent(url -> PluginWorkspaceProvider.getPluginWorkspace().open(url));
            } catch (NoRepositorySelected | IOException e1) {
                LOGGER.error(e1.getMessage(), e1);
                PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(UNABLE_TO_OPEN_REVISION + e1.getMessage());
            }
        }
    };
    openFileAction.putValue(Action.SHORT_DESCRIPTION, tooltipText);
    return openFileAction;
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) AbstractAction(javax.swing.AbstractAction) URL(java.net.URL)

Example 22 with NoRepositorySelected

use of com.oxygenxml.git.service.NoRepositorySelected in project oxygen-git-client-addon by oxygenxml.

the class HistoryViewContextualMenuPresenter method existsLocalFile.

/**
 * Used to verify if a file exists on local disk.
 *
 * @param filePath the file path
 *
 * @return <code>true</code> if the file exists
 */
private boolean existsLocalFile(String filePath) {
    String selectedRepository = "";
    boolean toReturn = true;
    try {
        selectedRepository = GitAccess.getInstance().getWorkingCopy().getAbsolutePath();
        // NOSONAR findsecbugs:PATH_TRAVERSAL_IN - false pozitive
        File file = new File(selectedRepository, filePath);
        toReturn = file.exists();
    } catch (NoRepositorySelected e) {
        toReturn = false;
    }
    return toReturn;
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) File(java.io.File)

Example 23 with NoRepositorySelected

use of com.oxygenxml.git.service.NoRepositorySelected in project oxygen-git-client-addon by oxygenxml.

the class HistoryViewContextualMenuPresenter method populateActions4MultipleSelection.

/**
 * We have multiple revisions selected for a given path.
 *
 * @param jPopupMenu Menu to populate.
 * @param filePath Selected path.
 * @param commitCharacteristics Revisions.
 */
private void populateActions4MultipleSelection(JPopupMenu jPopupMenu, String filePath, CommitCharacteristics... commitCharacteristics) {
    // Add open actions.
    String fileName = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().getFileName(filePath);
    String actionName = MessageFormat.format(TRANSLATOR.getTranslation(Tags.OPEN_FILE), fileName);
    Action open = new AbstractAction(actionName) {

        @Override
        public void actionPerformed(ActionEvent e) {
            for (CommitCharacteristics commitCharacteristic : commitCharacteristics) {
                try {
                    Optional<FileStatus> fileStatus = getFileStatus(filePath, commitCharacteristic);
                    checkIfValidForOpen(filePath, commitCharacteristic, fileStatus);
                    Optional<URL> fileURL = getFileURL(commitCharacteristic.getCommitId(), fileStatus.get());
                    fileURL.ifPresent(url -> PluginWorkspaceProvider.getPluginWorkspace().open(url));
                } catch (IOException | GitAPIException | NoRepositorySelected e1) {
                    LOGGER.debug(e1.getMessage(), e1);
                    PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(UNABLE_TO_OPEN_REVISION + e1.getMessage());
                }
            }
        }
    };
    // Add Compare action.
    if (commitCharacteristics.length == 2) {
        CommitCharacteristics c1 = commitCharacteristics[0];
        CommitCharacteristics c2 = commitCharacteristics[1];
        addCompareWithEachOtherAction(jPopupMenu, filePath, c1, c2);
    }
    jPopupMenu.add(open);
}
Also used : CreateBranchFromCommitAction(com.oxygenxml.git.view.history.actions.CreateBranchFromCommitAction) Action(javax.swing.Action) ResetBranchToCommitAction(com.oxygenxml.git.view.history.actions.ResetBranchToCommitAction) RevertCommitAction(com.oxygenxml.git.view.history.actions.RevertCommitAction) AbstractAction(javax.swing.AbstractAction) CreateTagAction(com.oxygenxml.git.view.history.actions.CreateTagAction) CheckoutCommitAction(com.oxygenxml.git.view.history.actions.CheckoutCommitAction) FileStatus(com.oxygenxml.git.service.entities.FileStatus) ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) URL(java.net.URL) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) AbstractAction(javax.swing.AbstractAction)

Example 24 with NoRepositorySelected

use of com.oxygenxml.git.service.NoRepositorySelected in project oxygen-git-client-addon by oxygenxml.

the class HistoryViewContextualMenuPresenter method createOpenWorkingCopyFileAction.

/**
 * Creates an action to open a working copy file.
 *
 * @author Alex_Smarandache
 *
 * @param fileStatus  File path, relative to the working copy.
 * @param commitID    Current commit ID.
 * @param addFileName <code>true</code> to append the name of the file to the name of the action.
 *
 * @return The action that will open the file when invoked.
 */
protected Action createOpenWorkingCopyFileAction(FileStatus fileStatus, String commitID, boolean addFileName) {
    return new AbstractAction(getOpenFileWorkingCopyActionName(fileStatus, addFileName)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                String localFilePath = RevCommitUtil.getNewPathInWorkingCopy(GitAccess.getInstance().getGit(), fileStatus.getFileLocation(), commitID);
                URL fileURL = FileUtil.getFileURL(localFilePath);
                boolean isProjectExt = false;
                int index = localFilePath.lastIndexOf('.');
                if (index != -1) {
                    String ext = localFilePath.substring(index + 1);
                    isProjectExt = "xpr".equals(ext);
                }
                PluginWorkspaceProvider.getPluginWorkspace().open(fileURL, isProjectExt ? EditorPageConstants.PAGE_TEXT : null, isProjectExt ? "text/xml" : null);
            } catch (FileNotFoundException ex) {
                PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(UNABLE_TO_OPEN_REVISION + ex.getMessage());
                LOGGER.debug(ex.getMessage(), ex);
            } catch (NoRepositorySelected | IOException | GitAPIException ex) {
                PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(UNABLE_TO_OPEN_REVISION + ex.getMessage());
                LOGGER.error(ex.getMessage(), ex);
            }
        }
    };
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) ActionEvent(java.awt.event.ActionEvent) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) AbstractAction(javax.swing.AbstractAction) URL(java.net.URL)

Example 25 with NoRepositorySelected

use of com.oxygenxml.git.service.NoRepositorySelected in project oxygen-git-client-addon by oxygenxml.

the class PanelRefresh method tryToSwitchToRepo.

/**
 * Try to switch to repo, if the user will agree.
 *
 * @param repoDir Repository directory.
 *
 * @return <code>true</code> if repo changed.
 */
private boolean tryToSwitchToRepo(File repoDir) {
    boolean repoChanged = false;
    try {
        File currentRepo = null;
        if (gitAccess.isRepoInitialized()) {
            currentRepo = gitAccess.getRepository().getDirectory().getParentFile();
        }
        if (currentRepo == null || !same(currentRepo, repoDir)) {
            JComboBox<String> wcComboBox = stagingPanel.getWorkingCopySelectionPanel().getWorkingCopyCombo();
            if (wcComboBox.isPopupVisible()) {
                wcComboBox.setPopupVisible(false);
            }
            WhenRepoDetectedInProject whatToDo = OptionsManager.getInstance().getWhenRepoDetectedInProject();
            String projectDirPath = getCanonicalPath(repoDir);
            if (whatToDo == WhenRepoDetectedInProject.ASK_TO_SWITCH_TO_WC) {
                repoChanged = switchToProjectRepoIfUserAgrees(projectDirPath);
            } else if (whatToDo == WhenRepoDetectedInProject.AUTO_SWITCH_TO_WC) {
                GitAccess.getInstance().setRepositoryAsync(projectDirPath);
                repoChanged = true;
            }
        }
    } catch (NoRepositorySelected e) {
        LOGGER.warn(e.getMessage(), e);
    }
    return repoChanged;
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) WhenRepoDetectedInProject(com.oxygenxml.git.OxygenGitOptionPagePluginExtension.WhenRepoDetectedInProject) File(java.io.File)

Aggregations

NoRepositorySelected (com.oxygenxml.git.service.NoRepositorySelected)28 IOException (java.io.IOException)15 File (java.io.File)10 URL (java.net.URL)8 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)8 Repository (org.eclipse.jgit.lib.Repository)6 FileStatus (com.oxygenxml.git.service.entities.FileStatus)4 MalformedURLException (java.net.MalformedURLException)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 RepoNotInitializedException (com.oxygenxml.git.service.RepoNotInitializedException)3 ActionEvent (java.awt.event.ActionEvent)3 AbstractAction (javax.swing.AbstractAction)3 BranchInfo (com.oxygenxml.git.service.BranchInfo)2 GitChangeType (com.oxygenxml.git.service.entities.GitChangeType)2 Translator (com.oxygenxml.git.translator.Translator)2 CheckoutCommitAction (com.oxygenxml.git.view.history.actions.CheckoutCommitAction)2 CreateBranchFromCommitAction (com.oxygenxml.git.view.history.actions.CreateBranchFromCommitAction)2 CreateTagAction (com.oxygenxml.git.view.history.actions.CreateTagAction)2 ResetBranchToCommitAction (com.oxygenxml.git.view.history.actions.ResetBranchToCommitAction)2 RevertCommitAction (com.oxygenxml.git.view.history.actions.RevertCommitAction)2