下面列出了javafx.scene.control.ButtonType#CANCEL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean closeEditor(IEditor e) {
if (e == null) {
return true;
}
if (e.isDirty()) {
Optional<ButtonType> result = FXUIUtils.showConfirmDialog(DisplayWindow.this,
"File \"" + e.getName() + "\" Modified. Do you want to save the changes ",
"File \"" + e.getName() + "\" Modified", AlertType.CONFIRMATION, ButtonType.YES, ButtonType.NO,
ButtonType.CANCEL);
ButtonType shouldSaveFile = result.get();
if (shouldSaveFile == ButtonType.CANCEL) {
return false;
}
if (shouldSaveFile == ButtonType.YES) {
File file = save(e);
if (file == null) {
return false;
}
EditorDockable dockable = (EditorDockable) e.getData("dockable");
dockable.updateKey();
}
}
return true;
}
public static Group createGroup(GroupType type, Path path, String name) {
List<Group> groups = getGroups(type);
for (Group g : groups) {
if (g.getName().equals(name)) {
Optional<ButtonType> option = FXUIUtils.showConfirmDialog(null,
type.fileType() + " `" + g.getName() + "` name is already used.", "Duplicate " + type.fileType() + " Name",
AlertType.CONFIRMATION);
if (!option.isPresent() || option.get() == ButtonType.CANCEL) {
return null;
}
}
}
Group group = new Group(name);
try {
Files.write(path, (type.fileCommentHeader() + group.toJSONString()).getBytes());
return new Group(path.toFile());
} catch (IOException e) {
return null;
}
}
public ConfirmationDialog(Stage primaryStage, Translation title, Translation headerText, String cssPrefix) {
super(
AlertType.WARNING,
"",
ButtonType.OK,
ButtonType.CANCEL
);
initStyle(StageStyle.UTILITY);
getDialogPane().getStyleClass().add(cssPrefix + "-confirmation-dialog-pane");
getDialogPane().getStylesheets().addAll(primaryStage.getScene().getStylesheets());
titleProperty().bind(title.getProperty());
headerTextProperty().bind(headerText.getProperty());
contentTextProperty().bind(Translation.DIALOG_CONFIRMATION_QUESTION.getProperty());
}
private void delete(ObservableList<TreeItem<Resource>> selectedItems) {
Optional<ButtonType> option = Optional.empty();
ArrayList<TreeItem<Resource>> items = new ArrayList<>(selectedItems);
for (TreeItem<Resource> treeItem : items) {
option = treeItem.getValue().delete(option);
if (option.isPresent() && option.get() == ButtonType.CANCEL) {
break;
}
}
}
@Override
public File saveAs(String script, Window parent, String filename) throws IOException {
boolean saved = false;
while (!saved) {
File file = askForFile(parent, filename);
if (file == null) {
return null;
}
ButtonType option = ButtonType.YES;
if (file.exists()) {
if (nameValidateChecker != null && !nameValidateChecker.okToOverwrite(file)) {
return null;
}
Optional<ButtonType> result = FXUIUtils.showConfirmDialog(parent,
"File " + file.getName() + " already exists. Do you want to overwrite?", "File exists",
AlertType.CONFIRMATION, ButtonType.YES, ButtonType.NO);
option = result.get();
}
if (option == ButtonType.YES) {
setCurrentFile(file);
saveToFile(currentFile, script);
return file;
}
if (option == ButtonType.CANCEL) {
return null;
}
}
return null;
}
/** Called when user tries to close the tab
*
* <p>Derived class may override.
*
* @return Should the tab close? Otherwise it stays open.
*/
protected boolean okToClose()
{
if (! isDirty())
return true;
final String text = MessageFormat.format(Messages.DockAlertMsg, getLabel());
final Alert prompt = new Alert(AlertType.NONE,
text,
ButtonType.NO, ButtonType.CANCEL, ButtonType.YES);
prompt.setTitle(Messages.DockAlertTitle);
prompt.getDialogPane().setMinSize(300, 100);
prompt.setResizable(true);
DialogHelper.positionDialog(prompt, getTabPane(), -200, -100);
final ButtonType result = prompt.showAndWait().orElse(ButtonType.CANCEL);
// Cancel the close request
if (result == ButtonType.CANCEL)
return false;
// Close without saving?
if (result == ButtonType.NO)
return true;
// Save in background job ...
JobManager.schedule(Messages.Save, monitor -> save(monitor));
// .. and leave the tab open, so user can then try to close again
return false;
}
private boolean checkUnsavedChanges() {
clearSelection();
if(editHistory.editStackSize() != savedEditStackSize) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.initOwner(stage);
alert.initModality(Modality.WINDOW_MODAL);
alert.setTitle("Unsaved changes");
alert.setHeaderText("Unsaved changes");
alert.setContentText("There are unsaved changes, do you want to save them?");
ButtonType discard = new ButtonType("Discard", ButtonData.NO);
alert.getButtonTypes().add(discard);
Optional<ButtonType> result = alert.showAndWait();
if(result.isPresent()) {
if(result.get() == ButtonType.OK) {
saveCircuitsInternal();
return saveFile == null;
} else {
return result.get() == ButtonType.CANCEL;
}
}
}
return false;
}
public AlertDialog() {
super(AlertType.WARNING, null, ButtonType.YES, ButtonType.CANCEL);
super.setTitle("Warning");
super.initModality(Modality.WINDOW_MODAL);
setDefaultIcon(super.getDialogPane());
showAlwaysOnTop(this);
}
/**
* Show a modal dialog to select a {@link CoordinateReferenceSystem}.
*
* @param parent parent frame of widget.
* @param crs {@link CoordinateReferenceSystem} to edit.
* @return modified {@link CoordinateReferenceSystem}.
*/
public static CoordinateReferenceSystem showDialog(Object parent, CoordinateReferenceSystem crs) {
final CRSChooser chooser = new CRSChooser();
chooser.crsProperty.set(crs);
final Alert alert = new Alert(Alert.AlertType.NONE);
final DialogPane pane = alert.getDialogPane();
pane.setContent(chooser);
alert.getButtonTypes().setAll(ButtonType.OK,ButtonType.CANCEL);
alert.setResizable(true);
final ButtonType res = alert.showAndWait().orElse(ButtonType.CANCEL);
return res == ButtonType.CANCEL ? null : chooser.crsProperty.get();
}
static void newReportableError(String msg) {
Alert alert = new Alert(Alert.AlertType.ERROR, msg + "\n\nPlease create a new issue on Github or PM me on Reddit.", githubIssue, redditPM, ButtonType.CANCEL);
alert.showAndWait();
reportError(alert);
}
static void newReportableError(String msg, String toCopy) {
Alert alert = new Alert(Alert.AlertType.ERROR, msg + "\n\nPlease create a new issue on Github or PM me on Reddit. The log has been copied to your clipboard.", githubIssue, redditPM, ButtonType.CANCEL);
alert.showAndWait();
reportError(alert, toCopy);
}
private void closeThisDialogWithCancelStatus() {
returnButtonType = ButtonType.CANCEL;
stage.close();
}
private void closeThisDialogWithCancelStatus() {
returnButtonType = ButtonType.CANCEL;
stage.close();
}
private void onCancel() {
selection = ButtonType.CANCEL;
dispose();
}
private void promptForUpdate(final Node node, final Instant new_version)
{
final File install_location = Locations.install();
// Want to update install_location, but that's locked on Windows,
// and replacing the jars of a running application might be bad.
// So download into a stage area.
// The start script needs to be aware of this stage area
// and move it to the install location on startup.
final File stage_area = new File(install_location, "update");
final StringBuilder buf = new StringBuilder();
buf.append("You are running version ")
.append(TimestampFormats.DATETIME_FORMAT.format(Update.current_version))
.append("\n")
.append("The new version is dated ")
.append(TimestampFormats.DATETIME_FORMAT.format(new_version))
.append("\n\n")
.append("The update will replace the installation in\n")
.append(install_location)
.append("\n(").append(stage_area).append(")")
.append("\nwith the content of ")
.append(Update.update_url)
.append("\n\n")
.append("Do you want to update?\n");
final Alert prompt = new Alert(AlertType.INFORMATION,
buf.toString(),
ButtonType.OK, ButtonType.CANCEL);
prompt.setTitle(NAME);
prompt.setHeaderText("A new version of this software is available");
prompt.setResizable(true);
DialogHelper.positionDialog(prompt, node, -600, -350);
prompt.getDialogPane().setPrefSize(600, 300);
if (prompt.showAndWait().orElse(ButtonType.CANCEL) == ButtonType.OK)
{
// Show job manager to display progress
ApplicationService.findApplication(JobViewerApplication.NAME).create();
// Perform update
JobManager.schedule(NAME, monitor ->
{
Update.downloadAndUpdate(monitor, stage_area);
Update.adjustCurrentVersion();
if (! monitor.isCanceled())
Platform.runLater(() -> restart(node));
});
}
else
{
// Remove the update button
StatusBar.getInstance().removeItem(start_update);
start_update = null;
}
}
static Alert buildDeleteAlertDialog(List<Path> pathsLabel) {
Alert deleteAlert = new WindowModalAlert(Alert.AlertType.WARNING, null, ButtonType.YES, ButtonType.CANCEL);
deleteAlert.setHeaderText("Do you want to delete selected path(s)?");
DialogPane dialogPane = deleteAlert.getDialogPane();
ObservableList<Path> paths = Optional.ofNullable(pathsLabel)
.map(FXCollections::observableList)
.orElse(FXCollections.emptyObservableList());
if (paths.isEmpty()) {
dialogPane.setContentText("There are no files selected.");
deleteAlert.getButtonTypes().clear();
deleteAlert.getButtonTypes().add(ButtonType.CANCEL);
return deleteAlert;
}
ListView<Path> listView = new ListView<>(paths);
listView.setId("listOfPaths");
GridPane gridPane = new GridPane();
gridPane.addRow(0, listView);
GridPane.setHgrow(listView, Priority.ALWAYS);
double minWidth = 200.0;
double maxWidth = Screen.getScreens().stream()
.mapToDouble(s -> s.getBounds().getWidth() / 3)
.min().orElse(minWidth);
double prefWidth = paths.stream()
.map(String::valueOf)
.mapToDouble(s -> s.length() * 7)
.max()
.orElse(maxWidth);
double minHeight = IntStream.of(paths.size())
.map(e -> e * 70)
.filter(e -> e <= 300 && e >= 70)
.findFirst()
.orElse(200);
gridPane.setMinWidth(minWidth);
gridPane.setPrefWidth(prefWidth);
gridPane.setPrefHeight(minHeight);
dialogPane.setContent(gridPane);
return deleteAlert;
}
public static Optional<ButtonType> showAlert(String alertMessage) {
AlertDialog deleteAlert = new AlertDialog(AlertType.WARNING, null, ButtonType.YES, ButtonType.CANCEL);
deleteAlert.setHeaderText(alertMessage);
return deleteAlert.showAndWait();
}
private synchronized void save() {
FileTime latestModifiedTime = IOHelper.getLastModifiedTime(getPath());
if (Objects.nonNull(latestModifiedTime) && Objects.nonNull(getLastModifiedTime())) {
if (latestModifiedTime.compareTo(getLastModifiedTime()) > 0) {
this.select();
ButtonType buttonType = AlertHelper.conflictAlert(getPath()).orElse(ButtonType.CANCEL);
if (buttonType == ButtonType.CANCEL) {
return;
}
if (buttonType == AlertHelper.LOAD_FILE_SYSTEM_CHANGES) {
load();
}
} else {
if (!isNew() && !isChanged()) {
return;
}
}
}
if (Objects.isNull(getPath())) {
final FileChooser fileChooser = directoryService.newFileChooser(String.format("Save file"));
fileChooser.getExtensionFilters().addAll(ExtensionFilters.ASCIIDOC);
fileChooser.getExtensionFilters().addAll(ExtensionFilters.MARKDOWN);
fileChooser.getExtensionFilters().addAll(ExtensionFilters.ALL);
File file = fileChooser.showSaveDialog(null);
if (Objects.isNull(file))
return;
setPath(file.toPath());
setTabText(file.toPath().getFileName().toString());
}
String editorValue = editorPane.getEditorValue();
IOHelper.createDirectories(getPath().getParent());
Optional<Exception> exception =
IOHelper.writeToFile(getPath(), editorValue, TRUNCATE_EXISTING, CREATE, SYNC);
if (exception.isPresent()) {
return;
}
setLastModifiedTime(IOHelper.getLastModifiedTime(getPath()));
setChangedProperty(false);
ObservableList<Item> recentFiles = storedConfigBean.getRecentFiles();
recentFiles.remove(new Item(getPath()));
recentFiles.add(0, new Item(getPath()));
directoryService.setInitialDirectory(Optional.ofNullable(getPath().toFile()));
}