Search in sources :

Example 1 with RepoNotInitializedException

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

the class CommitAndStatusPanel method addAmendLastCommitToggle.

/**
 * Add the toggle that allows amending the last commit.
 *
 * @param toolbar The toolbar to which to add.
 */
private void addAmendLastCommitToggle(JToolBar toolbar) {
    amendLastCommitToggle = new JideToggleButton(Icons.getIcon(Icons.AMEND_COMMIT));
    amendLastCommitToggle.setFocusPainted(false);
    amendLastCommitToggle.setToolTipText(translator.getTranslation(Tags.AMEND_LAST_COMMIT));
    amendLastCommitToggle.addItemListener(new ItemListener() {

        String previousText = "";

        @Override
        public void itemStateChanged(ItemEvent ev) {
            if (ev.getStateChange() == ItemEvent.SELECTED) {
                treatAmendEnabled();
            } else {
                commitMessageArea.setText(previousText);
                toggleCommitButtonAndUpdateMessageArea(false);
                commitButton.setText(translator.getTranslation(Tags.COMMIT));
            }
        }

        /**
         * Amend was enabled. Treat the event.
         */
        private void treatAmendEnabled() {
            previousText = commitMessageArea.getText();
            try {
                if (gitAccess.getPushesAhead() == 0) {
                    int result = PluginWorkspaceProvider.getPluginWorkspace().showConfirmDialog(translator.getTranslation(Tags.AMEND_LAST_COMMIT), translator.getTranslation(Tags.AMEND_PUSHED_COMMIT_WARNING), new String[] { "   " + translator.getTranslation(Tags.YES) + "   ", "   " + translator.getTranslation(Tags.NO) + "   " }, new int[] { 1, 0 });
                    if (result == 1) {
                        prepareAmend();
                    } else {
                        amendLastCommitToggle.setSelected(false);
                    }
                } else {
                    prepareAmend();
                }
            } catch (RepoNotInitializedException e) {
                PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(e.getMessage());
            }
        }

        /**
         * Prepare amend.
         */
        private void prepareAmend() {
            RevCommit latestCommitOnBranch = null;
            try {
                latestCommitOnBranch = GitAccess.getInstance().getLatestCommitOnCurrentBranch();
            } catch (GitAPIException | IOException | NoRepositorySelected e) {
                LOGGER.error(e.getMessage(), e);
            }
            if (latestCommitOnBranch != null) {
                String text = latestCommitOnBranch.getFullMessage();
                commitMessageArea.setText(text);
                toggleCommitButtonAndUpdateMessageArea(false);
                commitButton.setText(translator.getTranslation(Tags.AMEND_LAST_COMMIT));
            }
        }
    });
    toolbar.add(amendLastCommitToggle);
}
Also used : ItemEvent(java.awt.event.ItemEvent) JideToggleButton(com.jidesoft.swing.JideToggleButton) ItemListener(java.awt.event.ItemListener) RepoNotInitializedException(com.oxygenxml.git.service.RepoNotInitializedException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with RepoNotInitializedException

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

the class ToolbarPanel method updateButtonsStates.

/**
 * Updates the presented information, like the Pull-behind, Pushes-ahead
 * and branch status.
 */
public void updateButtonsStates() {
    this.pullsBehind = GIT_ACCESS.getPullsBehind();
    try {
        this.pushesAhead = GIT_ACCESS.getPushesAhead();
    } catch (RepoNotInitializedException e) {
        this.pushesAhead = -1;
        LOGGER.debug(e.getMessage(), e);
    }
    Repository repo = null;
    try {
        repo = GIT_ACCESS.getRepository();
    } catch (NoRepositorySelected e) {
        LOGGER.debug(e.getMessage(), e);
    }
    final Collection<RevCommit> stashes = GIT_ACCESS.listStashes();
    if (stashes != null) {
        noOfStashes = stashes.size();
    }
    final boolean isPullMenuEnabled = isPullButtonEnabled();
    final boolean isStashButtonEnabled = isStashButtonEnabled();
    SwingUtilities.invokeLater(() -> {
        /* for @pushButton, @historyButton, @showBranchesButton ---> 
       * it is necessary to refresh, even if it has a single action, 
       * because there are other code sequences that directly change their state by calling the setEnabled(false) method
       */
        pushButton.setEnabled(gitActionsManager.getPushAction().isEnabled());
        pullMenuButton.setEnabled(isPullMenuEnabled);
        stashButton.setEnabled(isStashButtonEnabled);
        historyButton.setEnabled(gitActionsManager.getShowHistoryAction().isEnabled());
        showBranchesButton.setEnabled(gitActionsManager.getShowBranchesAction().isEnabled());
        pullMenuButton.repaint();
        pushButton.repaint();
        stashButton.repaint();
    });
    BranchInfo branchInfo = GIT_ACCESS.getBranchInfo();
    String currentBranchName = branchInfo.getBranchName();
    if (branchInfo.isDetached()) {
        SwingUtilities.invokeLater(() -> {
            pushButton.setToolTipText(TRANSLATOR.getTranslation(Tags.PUSH_BUTTON_TOOLTIP));
            pullMenuButton.setToolTipText(TRANSLATOR.getTranslation(Tags.PULL_BUTTON_TOOLTIP));
        });
        String tooltipText = TRANSLATOR.getTranslation(Tags.TOOLBAR_PANEL_INFORMATION_STATUS_DETACHED_HEAD) + " " + currentBranchName;
        if (repo != null && repo.getRepositoryState() == RepositoryState.REBASING_MERGE) {
            tooltipText += "<br>" + TRANSLATOR.getTranslation(Tags.REBASE_IN_PROGRESS) + ".";
        }
        tooltipText = TextFormatUtil.toHTML(tooltipText);
    } else {
        if (currentBranchName != null && !currentBranchName.isEmpty()) {
            String upstreamBranchFromConfig = GIT_ACCESS.getUpstreamBranchShortNameFromConfig(currentBranchName);
            boolean isAnUpstreamBranchDefinedInConfig = upstreamBranchFromConfig != null;
            String upstreamShortestName = isAnUpstreamBranchDefinedInConfig ? upstreamBranchFromConfig.substring(upstreamBranchFromConfig.lastIndexOf('/') + 1) : null;
            Ref remoteBranchRefForUpstreamFromConfig = isAnUpstreamBranchDefinedInConfig ? RepoUtil.getRemoteBranch(upstreamShortestName) : null;
            boolean existsRemoteBranchForUpstreamDefinedInConfig = remoteBranchRefForUpstreamFromConfig != null;
            String commitsBehindMessage = "";
            String commitsAheadMessage = "";
            if (isAnUpstreamBranchDefinedInConfig && existsRemoteBranchForUpstreamDefinedInConfig) {
                if (pullsBehind == 0) {
                    commitsBehindMessage = TRANSLATOR.getTranslation(Tags.TOOLBAR_PANEL_INFORMATION_STATUS_UP_TO_DATE);
                } else if (pullsBehind == 1) {
                    commitsBehindMessage = TRANSLATOR.getTranslation(Tags.ONE_COMMIT_BEHIND);
                } else {
                    commitsBehindMessage = MessageFormat.format(TRANSLATOR.getTranslation(Tags.COMMITS_BEHIND), pullsBehind);
                }
                if (pushesAhead == 0) {
                    commitsAheadMessage = TRANSLATOR.getTranslation(Tags.NOTHING_TO_PUSH);
                } else if (pushesAhead == 1) {
                    commitsAheadMessage = TRANSLATOR.getTranslation(Tags.ONE_COMMIT_AHEAD);
                } else {
                    commitsAheadMessage = MessageFormat.format(TRANSLATOR.getTranslation(Tags.COMMITS_AHEAD), pushesAhead);
                }
            }
            // ===================== Push button tooltip =====================
            String pushButtonTooltipFinal = updatePushToolTip(isAnUpstreamBranchDefinedInConfig, existsRemoteBranchForUpstreamDefinedInConfig, upstreamBranchFromConfig, commitsAheadMessage, currentBranchName, repo);
            SwingUtilities.invokeLater(() -> pushButton.setToolTipText(pushButtonTooltipFinal));
            // ===================== Pull button tooltip =====================
            String pullButtonTooltipFinal = updatePullToolTip(isAnUpstreamBranchDefinedInConfig, existsRemoteBranchForUpstreamDefinedInConfig, upstreamBranchFromConfig, commitsBehindMessage, remoteBranchRefForUpstreamFromConfig, repo);
            SwingUtilities.invokeLater(() -> pullMenuButton.setToolTipText(pullButtonTooltipFinal));
        }
    }
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) BranchInfo(com.oxygenxml.git.service.BranchInfo) RepoNotInitializedException(com.oxygenxml.git.service.RepoNotInitializedException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with RepoNotInitializedException

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

the class BranchSelectionCombo method refresh.

/**
 * Refresh.
 */
public void refresh() {
    int pullsBehind = GIT_ACCESS.getPullsBehind();
    int pushesAhead = -1;
    try {
        pushesAhead = GIT_ACCESS.getPushesAhead();
    } catch (RepoNotInitializedException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    // update pop up
    updateBranchesPopup();
    Repository repo = null;
    try {
        repo = GIT_ACCESS.getRepository();
    } catch (NoRepositorySelected e) {
        LOGGER.debug(e.getMessage(), e);
    }
    this.setEnabled(repo != null);
    final BranchInfo branchInfo = GIT_ACCESS.getBranchInfo();
    final String currentBranchName = branchInfo.getBranchName();
    if (branchInfo.isDetached()) {
        detachedHeadId = currentBranchName;
        String tooltipText = TRANSLATOR.getTranslation(Tags.TOOLBAR_PANEL_INFORMATION_STATUS_DETACHED_HEAD) + " " + currentBranchName;
        if (repo != null && repo.getRepositoryState() == RepositoryState.REBASING_MERGE) {
            tooltipText += "<br>" + TRANSLATOR.getTranslation(Tags.REBASE_IN_PROGRESS) + ".";
        }
        tooltipText = TextFormatUtil.toHTML(tooltipText);
        String finalText = tooltipText;
        SwingUtilities.invokeLater(() -> this.setToolTipText(finalText));
    } else {
        detachedHeadId = null;
        String branchTooltip = null;
        if (currentBranchName != null && !currentBranchName.isEmpty()) {
            branchTooltip = getBranchTooltip(pullsBehind, pushesAhead, currentBranchName);
        }
        String branchTooltipFinal = branchTooltip;
        this.setToolTipText(branchTooltipFinal);
    }
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) Repository(org.eclipse.jgit.lib.Repository) BranchInfo(com.oxygenxml.git.service.BranchInfo) RepoNotInitializedException(com.oxygenxml.git.service.RepoNotInitializedException)

Aggregations

RepoNotInitializedException (com.oxygenxml.git.service.RepoNotInitializedException)3 BranchInfo (com.oxygenxml.git.service.BranchInfo)2 NoRepositorySelected (com.oxygenxml.git.service.NoRepositorySelected)2 Repository (org.eclipse.jgit.lib.Repository)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 JideToggleButton (com.jidesoft.swing.JideToggleButton)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 Ref (org.eclipse.jgit.lib.Ref)1