Develop and Download Open Source Software

Browse Subversion Repository

Contents of /fx-util/trunk/src/main/java/net/osdn/util/javafx/scene/control/Dialogs.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 259 - (show annotations) (download) (as text)
Sun Jan 8 08:22:23 2023 UTC (14 months, 1 week ago) by hirukawa_ryo
File MIME type: text/x-java
File size: 7537 byte(s)
* fx-util 0.5.0
Dialogsクラスににフォントサイズを設定する setFontSize メソッドを追加しました。
1 package net.osdn.util.javafx.scene.control;
2
3 import java.util.HashMap;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6 import java.util.MissingResourceException;
7 import java.util.Optional;
8 import java.util.ResourceBundle;
9
10 import javafx.beans.value.ChangeListener;
11 import javafx.beans.value.ObservableValue;
12 import javafx.collections.ObservableList;
13 import javafx.geometry.Bounds;
14 import javafx.scene.Node;
15 import javafx.scene.control.Alert;
16 import javafx.scene.control.Alert.AlertType;
17 import javafx.scene.control.Button;
18 import javafx.scene.control.ButtonType;
19 import javafx.scene.control.DialogPane;
20 import javafx.scene.image.Image;
21 import javafx.scene.text.Font;
22 import javafx.stage.Stage;
23 import javafx.stage.Window;
24
25 public class Dialogs {
26
27 protected static final Map<ButtonType, String> buttonTexts = new HashMap<ButtonType, String>();
28 private static double fontSize = 0;
29
30 static {
31 String BASE_NAME = "com/sun/javafx/scene/control/skin/resources/controls";
32 Map<ButtonType, String> keys = new LinkedHashMap<ButtonType, String>();
33 keys.put(ButtonType.APPLY, "Dialog.apply.button");
34 keys.put(ButtonType.OK, "Dialog.ok.button");
35 keys.put(ButtonType.CANCEL, "Dialog.cancel.button");
36 keys.put(ButtonType.CLOSE, "Dialog.close.button");
37 keys.put(ButtonType.YES, "Dialog.yes.button");
38 keys.put(ButtonType.NO, "Dialog.no.button");
39 keys.put(ButtonType.FINISH, "Dialog.finish.button");
40 keys.put(ButtonType.NEXT, "Dialog.next.button");
41 keys.put(ButtonType.PREVIOUS, "Dialog.previous.button");
42
43 ResourceBundle bundle = null;
44 try {
45 bundle = ResourceBundle.getBundle(BASE_NAME);
46 } catch(MissingResourceException ignore) {}
47
48 if(bundle != null) {
49 for(Map.Entry<ButtonType, String> entry : keys.entrySet()) {
50 try {
51 buttonTexts.put(entry.getKey(), bundle.getString(entry.getValue()));
52 } catch(MissingResourceException ignore) {}
53 }
54 }
55 }
56
57 public static void setFontSize(double fontSize) {
58 Dialogs.fontSize = fontSize;
59 }
60
61 public static ButtonType showInformation(String message) {
62 return show(AlertType.INFORMATION, null, null, null, message, new ButtonType[] { ButtonType.OK });
63 }
64
65 public static ButtonType showInformation(String title, String message) {
66 return show(AlertType.INFORMATION, null, null, title, message, new ButtonType[] { ButtonType.OK });
67 }
68
69 public static ButtonType showInformation(Window owner, String message) {
70 return show(AlertType.INFORMATION, owner, null, null, message, new ButtonType[] { ButtonType.OK });
71 }
72
73 public static ButtonType showInformation(Window owner, String title, String message) {
74 return show(AlertType.INFORMATION, owner, null, title, message, new ButtonType[] { ButtonType.OK });
75 }
76
77 public static ButtonType showConfirmation(String message) {
78 return show(AlertType.CONFIRMATION, null, null, null, message, new ButtonType[] { ButtonType.YES, ButtonType.NO });
79 }
80
81 public static ButtonType showConfirmation(String title, String message) {
82 return show(AlertType.CONFIRMATION, null, null, title, message, new ButtonType[] { ButtonType.YES, ButtonType.NO });
83 }
84
85 public static ButtonType showConfirmation(Window owner, String message) {
86 return show(AlertType.CONFIRMATION, owner, null, null, message, new ButtonType[] { ButtonType.YES, ButtonType.NO });
87 }
88
89 public static ButtonType showConfirmation(Window owner, String title, String message) {
90 return show(AlertType.CONFIRMATION, owner, null, title, message, new ButtonType[] { ButtonType.YES, ButtonType.NO });
91 }
92
93 public static ButtonType showWarning(String message) {
94 return show(AlertType.WARNING, null, null, null, message, new ButtonType[] { ButtonType.OK });
95 }
96
97 public static ButtonType showWarning(String title, String message) {
98 return show(AlertType.WARNING, null, null, title, message, new ButtonType[] { ButtonType.OK });
99 }
100
101 public static ButtonType showWarning(Window owner, String message) {
102 return show(AlertType.WARNING, owner, null, null, message, new ButtonType[] { ButtonType.OK });
103 }
104
105 public static ButtonType showWarning(Window owner, String title, String message) {
106 return show(AlertType.WARNING, owner, null, title, message, new ButtonType[] { ButtonType.OK });
107 }
108
109 public static ButtonType showError(String message) {
110 return show(AlertType.ERROR, null, null, null, message, new ButtonType[] { ButtonType.OK });
111 }
112
113 public static ButtonType showError(String title, String message) {
114 return show(AlertType.ERROR, null, null, title, message, new ButtonType[] { ButtonType.OK });
115 }
116
117 public static ButtonType showError(Window owner, String message) {
118 return show(AlertType.ERROR, owner, null, null, message, new ButtonType[] { ButtonType.OK });
119 }
120
121 public static ButtonType showError(Window owner, String title, String message) {
122 return show(AlertType.ERROR, owner, null, title, message, new ButtonType[] { ButtonType.OK });
123 }
124
125 public static ButtonType show(AlertType type, final Window owner, Node icon, String title, String message, ButtonType... buttons) {
126 final Alert dialog = new Alert(type, message, buttons);
127
128 if(owner != null && owner.getScene() != null) {
129 dialog.initOwner(owner);
130 }
131
132 if(owner instanceof Stage) {
133 ObservableList<Image> icons = ((Stage)owner).getIcons();
134 if(icons != null && icons.size() > 0) {
135 Stage stage = (Stage)dialog.getDialogPane().getScene().getWindow();
136 stage.getIcons().add(icons.get(0));
137 }
138 if(title == null) {
139 title = ((Stage)owner).getTitle();
140 }
141 }
142
143 boolean isWindows = System.getProperty("os.name", "").toLowerCase().startsWith("windows");
144 if(isWindows) {
145 if(fontSize > 0) {
146 dialog.getDialogPane().setStyle("-fx-font-family: Meiryo; -fx-font-size: " + fontSize);
147 } else {
148 dialog.getDialogPane().setStyle("-fx-font-family: Meiryo");
149 }
150 } else {
151 if(fontSize > 0) {
152 dialog.getDialogPane().setStyle("-fx-font-size: " + fontSize);
153 } else {
154 dialog.getDialogPane().setStyle("");
155 }
156 }
157
158 dialog.setTitle(title);
159 dialog.setHeaderText(null);
160 if(icon != null) {
161 dialog.setGraphic(icon);
162 }
163
164 if(owner != null) {
165 dialog.getDialogPane().layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
166 @Override
167 public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
168 if(newValue != null && newValue.getWidth() > 0 && newValue.getHeight() > 0) {
169 double x = owner.getX() + owner.getWidth() / 2;
170 double y = owner.getY() + owner.getHeight() / 2;
171 dialog.setX(x - newValue.getWidth() / 2);
172 dialog.setY(y - newValue.getHeight() / 2);
173 dialog.getDialogPane().layoutBoundsProperty().removeListener(this);
174 }
175 }
176 });
177 }
178
179 dialog.showingProperty().addListener(new ChangeListener<Boolean>() {
180 @Override
181 public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
182 if(newValue) {
183 DialogPane dialogPane = dialog.getDialogPane();
184 for(ButtonType buttonType : dialogPane.getButtonTypes()) {
185 String text = buttonTexts.get(buttonType);
186 if(text != null) {
187 Button button = (Button)dialogPane.lookupButton(buttonType);
188 button.setText(text);
189 }
190 }
191 dialog.showingProperty().removeListener(this);
192 }
193 }
194 });
195
196 Optional<ButtonType> result = dialog.showAndWait();
197 return result.isPresent() ? result.get() : null;
198 }
199 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26