package com.alexander.plugins.commitmessage;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.CheckinProjectPanel;
import com.intellij.openapi.vcs.VcsConfiguration;
import com.intellij.openapi.vcs.changes.CommitExecutor;
import com.intellij.openapi.vcs.checkin.CheckinHandler;
import com.intellij.openapi.vcs.ui.RefreshableOnComponent;
import com.intellij.ui.NonFocusableCheckBox;
import com.intellij.util.PairConsumer;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class CheckCommitMessageCheckinHandler extends CheckinHandler {

	protected final Project myProject;
	private final CheckinProjectPanel myPanel;

	public CheckCommitMessageCheckinHandler(final Project project, final CheckinProjectPanel panel){
		myProject = project;
		myPanel = panel;
	}

	@Override
	@Nullable
	public RefreshableOnComponent getBeforeCheckinConfigurationPanel() {
		final JCheckBox checkMessageBox = new NonFocusableCheckBox("Check commit message");

		return new RefreshableOnComponent() {
			@Override
			public JComponent getComponent() {
				final JPanel panel = new JPanel(new GridLayout(1, 0));
				panel.add(checkMessageBox);
				return panel;
			}

			@Override
			public void refresh() {
			}

			@Override
			public void saveState() {
				getSettings().CHECK_COMMIT_MESSAGE = checkMessageBox.isSelected();
			}

			@Override
			public void restoreState() {
				checkMessageBox.setSelected(getSettings().CHECK_COMMIT_MESSAGE);
			}
		};
	}

	protected CommitMessageConfiguration getSettings() {
		return CommitMessageConfiguration.getInstance(myProject);
	}

	@Override
	public ReturnResult beforeCheckin(CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) {
		return ReturnResult.COMMIT;
	}
}

@@ -71,30 +71,22 @@
 api.
-components.ServiceMana
+diagnostic.Log
 ger;
@@ -131,16 +131,57 @@
 roject;%0A
+import com.intellij.openapi.ui.Messages;%0A
 import c
@@ -229,58 +229,8 @@
 el;%0A
-import com.intellij.openapi.vcs.VcsConfiguration;%0A
 impo
@@ -717,16 +717,238 @@
 myPanel;
+%0A%09private static final Logger LOG = Logger.getInstance(%22#%22 + CheckCommitMessageCheckinHandler.class.getName());%0A%0A%09private static final int COMMIT_TITLE_LENGTH = 50;%0A%09private static final int COMMIT_DESCRIPTION_LENGHT = 72;
 %0A%0A%09publi
@@ -1037,16 +1037,17 @@
 l panel)
+ 
 %7B%0A%09%09myPr
@@ -2014,35 +2014,464 @@
 %7B%0A%09%09
-return ReturnResult.COMMIT;
+if (getSettings().CHECK_COMMIT_MESSAGE) %7B%0A%09%09%09String wholeCommitMessage = myPanel.getCommitMessage();%0A%09%09%09String%5B%5D commitParts = wholeCommitMessage.split(%22%5Cn%5Cr%22);%0A%09%09%09String commitTitle = commitParts%5B0%5D;%0A%0A%09%09%09if (commitTitle.length() %3E COMMIT_TITLE_LENGTH) %7B%0A%09%09%09%09Messages.showErrorDialog(myProject, %22Commit title is too long%22, %22Commit Message%22);%0A%09%09%09%09return ReturnResult.CANCEL;%0A%09%09%09%7D%0A%0A%09%09%09return ReturnResult.COMMIT;%0A%09%09%7D else %7B%0A%09%09%09return ReturnResult.COMMIT;%0A%09%09%7D
 %0A%09%7D%0A

Add commit title length validation