first commit
[jalview-fx.git] / src / jalviewfx / customcomponent / AlertDialog.java
1 package jalviewfx.customcomponent;
2
3 import javafx.event.Event;
4 import javafx.event.EventHandler;
5 import javafx.geometry.Pos;
6 import javafx.scene.Scene;
7 import javafx.scene.control.Button;
8 import javafx.scene.control.Label;
9 import javafx.scene.image.Image;
10 import javafx.scene.image.ImageView;
11 import javafx.scene.layout.BorderPane;
12 import javafx.scene.layout.HBox;
13 import javafx.scene.paint.Color;
14 import javafx.scene.text.Text;
15 import javafx.stage.Modality;
16 import javafx.stage.Stage;
17 import javafx.stage.StageStyle;
18
19 public class AlertDialog extends Stage{
20          private final int WIDTH_DEFAULT = 300;
21
22             public static final int ICON_INFO = 0;
23             public static final int ICON_ERROR = 1;    
24
25             @SuppressWarnings({ "unchecked", "rawtypes" })
26                 public AlertDialog(Stage owner, String msg, int type) {
27                 setResizable(false);
28                 initModality(Modality.APPLICATION_MODAL);
29                 initStyle(StageStyle.TRANSPARENT);
30
31                 Label label = new Label(msg);
32                 label.setWrapText(true);
33                 label.setGraphicTextGap(20);
34                 label.setGraphic(new ImageView(getImage(type)));
35
36                 Button button = new Button("OK");
37                 button.setOnAction(new EventHandler(){
38                     @Override
39                     public void handle(Event arg0) {
40                         AlertDialog.this.close();
41                     }
42                 });
43
44                 BorderPane borderPane = new BorderPane();
45                 borderPane.getStylesheets().add(getClass().getResource("alert.css").toExternalForm());        
46                 borderPane.setTop(label);
47
48                 HBox hbox2 = new HBox();
49                 hbox2.setAlignment(Pos.CENTER);
50                 hbox2.getChildren().add(button);
51                 borderPane.setBottom(hbox2);
52
53                 // calculate width of string
54                 final Text text = new Text(msg);
55                 text.snapshot(null, null);
56                 // + 20 because there is padding 10 left and right
57                 int width = (int) text.getLayoutBounds().getWidth() + 40;
58
59                 if (width < WIDTH_DEFAULT)
60                     width = WIDTH_DEFAULT;
61
62                 int height = 100;
63
64                 final Scene scene = new Scene(borderPane, width, height);
65                 scene.setFill(Color.TRANSPARENT);
66                 setScene(scene);
67
68                 // make sure this stage is centered on top of its owner
69                 setX(owner.getX() + (owner.getWidth() / 2 - width / 2));
70                 setY(owner.getY() + (owner.getHeight() / 2 - height / 2));
71             }
72
73             private Image getImage(int type) {
74                 if (type == ICON_ERROR)
75                     return new Image(getClass().getResourceAsStream("/images/error.png"));
76                 else
77                     return new Image(getClass().getResourceAsStream("/images/info.png"));
78             }
79 }