<commit_before>/*
 * Copyright 2013 Corpuslinguistic working group Humboldt University Berlin.
 *
 * 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 annis.gui.components;

import annis.gui.AnnisUI;
import annis.gui.CommonUI;
import annis.libgui.Helper;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.v7.shared.ui.label.ContentMode;
import com.vaadin.v7.ui.Label;
import com.vaadin.v7.ui.themes.BaseTheme;

/**
 * A dialog that displays the message of an exception and allows to show the
 * stack trace if requested.
 * 
 * @author <NAME> {@literal <<EMAIL>>}
 */
public class ExceptionDialog extends Window implements Button.ClickListener {
    private static final long serialVersionUID = -9195384309481177718L;

    public static void show(Throwable ex, String caption, UI ui) {

      // Check if we can handle this error without showing an explicit dialog
      if (!(ui instanceof CommonUI) || !((CommonUI) ui).handleCommonError(ex, null)) {
        ExceptionDialog dlg = new ExceptionDialog(ex, caption);
        dlg.setClosable(true);
        dlg.setModal(true);
        dlg.setResizable(true);
        dlg.setWidth("500px");
        dlg.setHeight("-1");

        ui.addWindow(dlg);
        dlg.center();
      }
    }

    public static void show(Throwable ex, UI ui) {
      show(ex, null, ui);
    }

    private Panel detailsPanel;
    private Label lblStacktrace;
    private Button btDetails;
    private Button btClose;
    private Button btReportBug;
    private Throwable cause;

    private VerticalLayout layout;

    private HorizontalLayout actionsLayout;

    public ExceptionDialog(Throwable ex) {
        this(ex, null);
    }

    public ExceptionDialog(Throwable ex, String caption) {
        this.cause = ex;

        layout = new VerticalLayout();
        setContent(layout);
        layout.setWidth("100%");
        layout.setHeight("-1");

        if (caption == null) {
            setCaption("Unexpected error");
        } else {
            setCaption(caption);
        }

        Label lblInfo =
            new Label("An error occured.<br />The error message was:", ContentMode.HTML);
        lblInfo.setHeight("-1px");
        lblInfo.setWidth("100%");
        layout.addComponent(lblInfo);
        lblInfo.addStyleName("exception-message-caption");

        String message = ex != null ? ex.getMessage() : null;
        if (message == null || message.isEmpty()) {
            message = "<no message>";
        }
        Label lblMessage = new Label(message);
        lblMessage.addStyleName("exception-message-content");
        lblMessage.setHeight("-1px");
        lblMessage.setWidth("100%");
        layout.addComponent(lblMessage);

        actionsLayout = new HorizontalLayout();
        actionsLayout.addStyleName("exception-dlg-details");
        actionsLayout.setWidth("100%");
        actionsLayout.setHeight("-1px");
        layout.addComponent(actionsLayout);

        btDetails = new Button("Show Details", this);
        btDetails.setStyleName(BaseTheme.BUTTON_LINK);
        actionsLayout.addComponent(btDetails);

        btReportBug = new Button("Report Problem", this);
        btReportBug.setStyleName(BaseTheme.BUTTON_LINK);
        btReportBug.setVisible(false);
        btReportBug.setIcon(FontAwesome.ENVELOPE_O);
        UI ui = UI.getCurrent();<commit_msg>Remove usage of deprecated label and style classes<commit_after>/*
 * Copyright 2013 Corpuslinguistic working group Humboldt University Berlin.
 *
 * 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 annis.gui.components;

import annis.gui.AnnisUI;
import annis.gui.CommonUI;
import annis.libgui.Helper;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;

/**
 * A dialog that displays the message of an exception and allows to show the
 * stack trace if requested.
 * 
 * @author <NAME> {@literal <<EMAIL>>}
 */
public class ExceptionDialog extends Window implements Button.ClickListener {
    private static final long serialVersionUID = -9195384309481177718L;

    public static void show(Throwable ex, String caption, UI ui) {

      // Check if we can handle this error without showing an explicit dialog
      if (!(ui instanceof CommonUI) || !((CommonUI) ui).handleCommonError(ex, null)) {
        ExceptionDialog dlg = new ExceptionDialog(ex, caption);
        dlg.setClosable(true);
        dlg.setModal(true);
        dlg.setResizable(true);
        dlg.setWidth("500px");
        dlg.setHeight("-1");

        ui.addWindow(dlg);
        dlg.center();
      }
    }

    public static void show(Throwable ex, UI ui) {
      show(ex, null, ui);
    }

    private Panel detailsPanel;
    private Label lblStacktrace;
    private Button btDetails;
    private Button btClose;
    private Button btReportBug;
    private Throwable cause;

    private VerticalLayout layout;

    private HorizontalLayout actionsLayout;

    public ExceptionDialog(Throwable ex) {
        this(ex, null);
    }

    public ExceptionDialog(Throwable ex, String caption) {
        this.cause = ex;

        layout = new VerticalLayout();
        setContent(layout);
        layout.setWidth("100%");
        layout.setHeight("-1");

        if (caption == null) {
            setCaption("Unexpected error");
        } else {
            setCaption(caption);
        }

        Label lblInfo =
            new Label("An error occured.<br />The error message was:", ContentMode.HTML);
        lblInfo.setHeight("-1px");
        lblInfo.setWidth("100%");
        layout.addComponent(lblInfo);
        lblInfo.addStyleName("exception-message-caption");

        String message = ex != null ? ex.getMessage() : null;
        if (message == null || message.isEmpty()) {
            message = "<no message>";
        }
        Label lblMessage = new Label(message);
        lblMessage.addStyleName("exception-message-content");
        lblMessage.setHeight("-1px");
        lblMessage.setWidth("100%");
        layout.addComponent(lblMessage);

        actionsLayout = new HorizontalLayout();
        actionsLayout.addStyleName("exception-dlg-details");
        actionsLayout.setWidth("100%");
        actionsLayout.setHeight("-1px");
        layout.addComponent(actionsLayout);

        btDetails = new Button("Show Details", this);
        btDetails.setStyleName(ValoTheme.BUTTON_LINK);
        actionsLayout.addComponent(btDetails);

        btReportBug = new Button("Report Problem", this);
        btReportBug.setStyleName(ValoTheme.BUTTON_LINK);
        btReportBug.setVisible(false);
        btReportBug.setIcon(VaadinIcons.ENVELOPE_O);
        UI ui = UI.getCurrent();
{'fraction_non_alphanumeric': 0.10583179864947821, 'fraction_numerical': 0.01190914671577655, 'mean_word_length': 3.036669970267592, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5008554', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2261, 'n_words': 630}