Clarify shortcuts for option dialog #PL-6088

This commit is contained in:
Yuriy Artamonov 2015-10-02 11:39:39 +00:00
parent 5acd3fd670
commit 1aee022dd9

View File

@ -1080,7 +1080,7 @@ public class WebWindowManager extends WindowManager {
window.setResizable(false); window.setResizable(false);
window.setModal(true); window.setModal(true);
final VerticalLayout layout = new VerticalLayout(); VerticalLayout layout = new VerticalLayout();
layout.setStyleName("cuba-app-option-dialog"); layout.setStyleName("cuba-app-option-dialog");
layout.setSpacing(true); layout.setSpacing(true);
window.setContent(layout); window.setContent(layout);
@ -1091,31 +1091,15 @@ public class WebWindowManager extends WindowManager {
HorizontalLayout buttonsContainer = new HorizontalLayout(); HorizontalLayout buttonsContainer = new HorizontalLayout();
buttonsContainer.setSpacing(true); buttonsContainer.setSpacing(true);
for (final Action action : actions) { Map<Action, Button> buttonMap = new HashMap<>();
final Button button = WebComponentsHelper.createButton(); for (Action action : actions) {
Button button = WebComponentsHelper.createButton();
button.setCaption(action.getCaption()); button.setCaption(action.getCaption());
button.addClickListener(new Button.ClickListener() { button.addClickListener(event -> {
@Override
public void buttonClick(Button.ClickEvent event) {
action.actionPerform(null); action.actionPerform(null);
ui.removeWindow(window); ui.removeWindow(window);
}
}); });
if (action instanceof DialogAction) {
switch (((DialogAction) action).getType()) {
case OK:
case YES:
WebComponentsHelper.setClickShortcut(button, clientConfig.getCommitShortcut());
break;
case NO:
case CANCEL:
case CLOSE:
WebComponentsHelper.setClickShortcut(button, clientConfig.getCloseShortcut());
break;
}
}
if (StringUtils.isNotEmpty(action.getIcon())) { if (StringUtils.isNotEmpty(action.getIcon())) {
button.setIcon(WebComponentsHelper.getIcon(action.getIcon())); button.setIcon(WebComponentsHelper.getIcon(action.getIcon()));
button.addStyleName(WebButton.ICON_STYLE); button.addStyleName(WebButton.ICON_STYLE);
@ -1132,7 +1116,11 @@ public class WebWindowManager extends WindowManager {
} }
buttonsContainer.addComponent(button); buttonsContainer.addComponent(button);
buttonMap.put(action, button);
} }
assingDialogShortcuts(buttonMap);
if (buttonsContainer.getComponentCount() == 1) { if (buttonsContainer.getComponentCount() == 1) {
((Button) buttonsContainer.getComponent(0)).focus(); ((Button) buttonsContainer.getComponent(0)).focus();
} }
@ -1150,6 +1138,49 @@ public class WebWindowManager extends WindowManager {
window.center(); window.center();
} }
protected void assingDialogShortcuts(Map<Action, Button> buttonMap) {
List<DialogAction> dialogActions = new ArrayList<>();
for (Action action : buttonMap.keySet()) {
if (action instanceof DialogAction) {
dialogActions.add((DialogAction) action);
}
}
// find action for commit shortcut
Action firstOkAction = dialogActions.stream()
.filter(action -> action.getType() == Type.OK)
.findFirst().get();
if (firstOkAction == null) {
firstOkAction = dialogActions.stream()
.filter(action -> action.getType() == Type.YES)
.findFirst().get();
}
if (firstOkAction != null) {
WebComponentsHelper.setClickShortcut(buttonMap.get(firstOkAction), clientConfig.getCommitShortcut());
}
// find action for close shortcut
Action firstCancelAction = dialogActions.stream()
.filter(action -> action.getType() == Type.CANCEL)
.findFirst().get();
if (firstCancelAction == null) {
firstCancelAction = dialogActions.stream()
.filter(action -> action.getType() == Type.CLOSE)
.findFirst().get();
if (firstCancelAction == null) {
firstCancelAction = dialogActions.stream()
.filter(action -> action.getType() == Type.NO)
.findFirst().get();
}
}
if (firstCancelAction != null) {
WebComponentsHelper.setClickShortcut(buttonMap.get(firstCancelAction), clientConfig.getCloseShortcut());
}
}
@Override @Override
public void showWebPage(String url, @Nullable Map<String, Object> params) { public void showWebPage(String url, @Nullable Map<String, Object> params) {
String target = null; String target = null;
@ -1232,8 +1263,8 @@ public class WebWindowManager extends WindowManager {
} }
@Override @Override
protected void checkCanOpenWindow(WindowInfo windowInfo, WindowManager.OpenType openType, Map<String, Object> params) { protected void checkCanOpenWindow(WindowInfo windowInfo, OpenType openType, Map<String, Object> params) {
if (WindowManager.OpenType.NEW_TAB.equals(openType)) { if (OpenType.NEW_TAB.equals(openType)) {
//noinspection StatementWithEmptyBody //noinspection StatementWithEmptyBody
if (!windowInfo.getMultipleOpen() && getWindow(getHash(windowInfo, params)) != null) { if (!windowInfo.getMultipleOpen() && getWindow(getHash(windowInfo, params)) != null) {
//window is already open //window is already open
@ -1254,7 +1285,7 @@ public class WebWindowManager extends WindowManager {
protected void initMainWindow(WindowInfo windowInfo) { protected void initMainWindow(WindowInfo windowInfo) {
String template = windowInfo.getTemplate(); String template = windowInfo.getTemplate();
final Window mainWindow; Window mainWindow;
Map<String, Object> params = Collections.emptyMap(); Map<String, Object> params = Collections.emptyMap();
if (template != null) { if (template != null) {
//noinspection unchecked //noinspection unchecked