View Javadoc
1   /*
2    * Copyright 2013-2023 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package cz.zcu.mre.sparkle.gui.dialogs.other;
21  
22  import cz.zcu.mre.sparkle.Messages;
23  import cz.zcu.mre.sparkle.gui.dialogs.TextContentDialog;
24  import cz.zcu.mre.sparkle.gui.tools.AbstractDialogController;
25  import cz.zcu.mre.sparkle.gui.tools.FormControllerFactory;
26  import cz.zcu.mre.sparkle.tools.Utils;
27  import javafx.fxml.FXML;
28  import javafx.scene.control.Button;
29  import javafx.scene.control.Label;
30  import javafx.stage.Window;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.io.StringWriter;
34  import java.util.logging.Level;
35  import java.util.logging.Logger;
36  
37  /**
38   * Kontroler dialogu zobrazujího chybu.
39   *
40   * @author Jan Smucr
41   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
42   */
43  public final class ErrorDialog
44          extends AbstractDialogController {
45  
46      private static final Logger LOG = Logger.getLogger(ErrorDialog.class.getName());
47  
48      @FXML
49      private Label messageLabel;
50      @FXML
51      private Button detailsButton;
52  
53      private Throwable details;
54  
55      @FXML
56      private void okButtonOnAction() {
57          close();
58      }
59  
60      @FXML
61      private void detailsButtonOnAction() {
62          final StringWriter sw = new StringWriter();
63          final PrintWriter pw = new PrintWriter(sw);
64          details.printStackTrace(pw);
65          TextContentDialog.open(getStage(), Messages.getString("ERROR_DETAILS"), sw.toString(),
66                  details.getClass().getSimpleName()); //$NON-NLS-1$
67      }
68  
69      public final String getMessage() {
70          return messageLabel.getText();
71      }
72  
73      public final void setMessage(final String message) {
74          messageLabel.setText(message);
75      }
76  
77      @Override
78      protected final void onDialogInitialized() {
79          setMessage(""); //$NON-NLS-1$
80          Utils.makeInvisibleUnmanaged(detailsButton);
81      }
82  
83      public static final void open(final Window owner, final String title, final String message) {
84          open(owner, title, message, null);
85      }
86  
87      public static final void open(final Window owner, final String title, final String message,
88              final Throwable details) {
89          try {
90              final ErrorDialog dlg = FormControllerFactory.load(owner, ErrorDialog.class);
91              dlg.setTitle(title);
92              dlg.setMessage(message);
93              dlg.setDetails(details);
94              dlg.showAndWait();
95          } catch (final IOException e) {
96              LOG.log(Level.SEVERE, "Exception: ", e);
97          }
98      }
99  
100     public final Throwable getDetails() {
101         return details;
102     }
103 
104     public final void setDetails(final Throwable details) {
105         this.details = details;
106         detailsButton.setVisible(details != null);
107     }
108 }