brainleg.app.intellij.ui.SubmitSolutionDialog.java Source code

Java tutorial

Introduction

Here is the source code for brainleg.app.intellij.ui.SubmitSolutionDialog.java

Source

/*
 * Copyright 2011 Roman Stepanenko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package brainleg.app.intellij.ui;

import brainleg.app.engine.EData;
import brainleg.app.intellij.BLSettingsService;
import brainleg.app.util.AppWeb;
import brainleg.app.util.ParseUtil;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author Roman Stepanenko
 */
public class SubmitSolutionDialog extends DialogWrapper {
    private final SubmitSolutionPanel myPanel;
    private JButton submitSolutionButton;
    private JLabel submissionCountLabel;
    private EData exception;

    public SubmitSolutionDialog(EData exception, JButton submitSolutionButton, JLabel submissionCountLabel) {
        super(true);
        this.exception = exception;
        String exceptionName = ParseUtil.getExceptionName(exception.getHeaders().get(0).rawText);
        setTitle("Submit solution for this " + exceptionName);
        myPanel = new SubmitSolutionPanel(exception);
        this.submitSolutionButton = submitSolutionButton;
        this.submissionCountLabel = submissionCountLabel;
        setModal(false);
        init();
    }

    @Nullable
    protected JComponent createCenterPanel() {
        return myPanel.getPanel();
    }

    @Override
    protected Action[] createActions() {
        return new Action[] { getOKAction(), getCancelAction() };
    }

    public static void askUser(EData exception, JButton submitSolutionButton, JLabel submissionCountLabel) {
        SubmitSolutionDialog dialog = new SubmitSolutionDialog(exception, submitSolutionButton,
                submissionCountLabel);
        dialog.show();
        if (!dialog.isOK()) {
            return;
        }
        dialog.dispose();
    }

    @Override
    protected void doOKAction() {
        String solution = myPanel.solutionTextArea.getText();
        String snippet = myPanel.snippetTextArea.getText();
        if (solution.trim().length() + snippet.trim().length() < 15) {
            Messages.showMessageDialog("Please provide solution text or code snippet", "Please provide solution",
                    Messages.getInformationIcon());
            return;
        }

        boolean success = submitSolution(solution, snippet, exception);
        if (success) {
            //update submitted solution count
            //            BLSettingsService.getSettings().submittedSolutionCount = BLSettingsService.getSettings().submittedSolutionCount + 1;

            submitSolutionButton.setEnabled(false); //disable button so that the user does not submit it again by mistake
        }
        super.doOKAction();
    }

    /**
     * Returns true if success, false otherwise.
     *
     * @param solution
     * @param snippet
     * @param exception
     */
    private boolean submitSolution(String solution, String snippet, EData exception) {
        return AppWeb.submitSolution(solution, snippet, exception);
    }

    //  @Override @NonNls
    //  protected String getDimensionServiceKey() {
    //    return "IgnoredSettingsDialog";
    //  }

}