Add ability to disable automatic screen settings save #PL-5191

This commit is contained in:
Yuriy Artamonov 2015-04-07 09:30:44 +00:00
parent 66c935bca1
commit bdb06146ca
20 changed files with 227 additions and 56 deletions

View File

@ -232,4 +232,8 @@ public interface ClientConfig extends Config {
@DefaultBoolean(true)
boolean getSystemInfoScriptsEnabled();
void setSystemInfoScriptsEnabled(boolean enabled);
@Property("cuba.manualSaveScreenSettings")
@DefaultBoolean(false)
boolean getManualSaveScreenSettings();
}

View File

@ -14,6 +14,7 @@ import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
@ -46,22 +47,11 @@ public class UserSettingServiceBean implements UserSettingService {
String value;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
UserSetting us = findUserSettings(clientType, name);
TypedQuery<UserSetting> q = em.createQuery(
"select s from sec$UserSetting s where s.user.id = ?1 and s.name =?2 and s.clientType = ?3",
UserSetting.class);
q.setParameter(1, userSessionSource.getUserSession().getUser().getId());
q.setParameter(2, name);
q.setParameter(3, clientType == null ? null : clientType.getId());
q.setViewName("userSetting.value");
List<UserSetting> list = q.getResultList();
value = list.isEmpty() ? null : list.get(0).getValue();
value = us == null ? null : us.getValue();
tx.commit();
} finally {
tx.end();
}
@ -79,23 +69,16 @@ public class UserSettingServiceBean implements UserSettingService {
try {
EntityManager em = persistence.getEntityManager();
TypedQuery<UserSetting> q = em.createQuery(
"select s from sec$UserSetting s where s.user.id = ?1 and s.name =?2 and s.clientType = ?3",
UserSetting.class);
q.setParameter(1, userSessionSource.getUserSession().getUser().getId());
q.setParameter(2, name);
q.setParameter(3, clientType == null ? null : clientType.getId());
List<UserSetting> list = q.getResultList();
if (list.isEmpty()) {
UserSetting us = new UserSetting();
UserSetting us = findUserSettings(clientType, name);
if (us == null) {
us = new UserSetting();
us.setUser(em.getReference(User.class, userSessionSource.getUserSession().getUser().getId()));
us.setName(name);
us.setClientType(clientType);
us.setValue(value);
em.persist(us);
} else {
UserSetting us = list.get(0);
us.setValue(value);
}
@ -105,6 +88,22 @@ public class UserSettingServiceBean implements UserSettingService {
}
}
@Override
public void deleteSettings(ClientType clientType, String name) {
Transaction tx = persistence.createTransaction();
try {
UserSetting us = findUserSettings(clientType, name);
EntityManager em = persistence.getEntityManager();
if(us!=null){
em.remove(us);
}
tx.commit();
} finally {
tx.end();
}
}
@Override
public void copySettings(User fromUser, User toUser) {
MetaClass metaClass = metadata.getClassNN(UserSetting.class);
@ -175,6 +174,20 @@ public class UserSettingServiceBean implements UserSettingService {
}
}
@Nullable
protected UserSetting findUserSettings(ClientType clientType, String name) {
EntityManager em = persistence.getEntityManager();
TypedQuery<UserSetting> q = em.createQuery(
"select s from sec$UserSetting s where s.user.id = ?1 and s.name =?2 and s.clientType = ?3",
UserSetting.class);
q.setParameter(1, userSessionSource.getUserSession().getUser().getId());
q.setParameter(2, name);
q.setParameter(3, clientType == null ? null : clientType.getId());
return q.getFirstResult();
}
protected Map<UUID, Presentation> copyPresentations(User fromUser, User toUser) {
Map<UUID, Presentation> presentationMap = new HashMap<>();
Transaction tx = persistence.createTransaction();

View File

@ -154,6 +154,11 @@ public class DesktopWindow implements Window, Component.Disposable,
delegate.saveSettings();
}
@Override
public void deleteSettings() {
delegate.deleteSettings();
}
@Override
public void setFocusComponent(String componentId) {
this.focusComponentId = componentId;
@ -232,10 +237,12 @@ public class DesktopWindow implements Window, Component.Disposable,
return false;
}
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
if (!forceClose && isModified()) {
final Committable committable = (getWrapper() instanceof Committable) ? (Committable) getWrapper() :
(this instanceof Committable) ? (Committable) this : null;
if ((committable != null) && configuration.getConfig(ClientConfig.class).getUseSaveConfirmation()) {
if ((committable != null) && clientConfig.getUseSaveConfirmation()) {
windowManager.showOptionDialog(
messages.getMainMessage("closeUnsaved.caption"),
messages.getMainMessage("saveUnsaved"),
@ -303,10 +310,13 @@ public class DesktopWindow implements Window, Component.Disposable,
return false;
}
if (delegate.getWrapper() != null)
delegate.getWrapper().saveSettings();
else
saveSettings();
if (!clientConfig.getManualSaveScreenSettings()) {
if (delegate.getWrapper() != null) {
delegate.getWrapper().saveSettings();
} else {
saveSettings();
}
}
delegate.disposeComponents();

View File

@ -51,6 +51,12 @@ public class DesktopSettingsClient implements SettingsClient {
userSettingService.saveSetting(ClientType.DESKTOP, name, value);
}
@Override
public void deleteSettings(String name) {
getCache().put(name, Optional.<String>absent());
userSettingService.deleteSettings(ClientType.DESKTOP, name);
}
protected Map<String, Optional<String>> getCache() {
ApplicationSession session = App.getInstance().getApplicationSession();
if (session == null) {

View File

@ -852,6 +852,25 @@ public class DesktopWindowManager extends WindowManager {
Configuration configuration = AppBeans.get(Configuration.NAME);
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
if (clientConfig.getManualSaveScreenSettings()) {
JMenuItem saveSettingsItem = new JMenuItem(messages.getMainMessage("actions.saveSettings"));
saveSettingsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.saveSettings();
}
});
popupMenu.add(saveSettingsItem);
JMenuItem restoreToDefaultsItem = new JMenuItem(messages.getMainMessage("actions.restoreToDefaults"));
restoreToDefaultsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window.deleteSettings();
}
});
popupMenu.add(restoreToDefaultsItem);
}
if (clientConfig.getLayoutAnalyzerEnabled()) {
JMenuItem analyzeLayoutItem = new JMenuItem(messages.getMainMessage("actions.analyzeLayout"));
analyzeLayoutItem.addActionListener(new ActionListener() {

View File

@ -31,6 +31,9 @@ public interface UserSettingService {
/** Save settings for the current user */
void saveSetting(ClientType clientType, String name, String value);
/** Delete settings for the current user */
void deleteSettings(ClientType clientType, String name);
/** Copy user settings to another user */
void copySettings(User fromUser, User toUser);
}
}

View File

@ -111,6 +111,11 @@ public class AbstractWindow extends AbstractFrame
((Window) frame).saveSettings();
}
@Override
public void deleteSettings() {
((Window) frame).deleteSettings();
}
@Override
public void setFocusComponent(String componentId) {
((Window) frame).setFocusComponent(componentId);

View File

@ -74,6 +74,9 @@ public interface Window extends IFrame, Component.HasCaption {
* to save user settings if they have been changed. */
void saveSettings();
/** This method is called by the framework on reset to defaults action */
void deleteSettings();
/**
* Set a component to be focused after the screen is opened.
* @param componentId component's ID in XML. If null, then first focusable component will be focused

View File

@ -132,6 +132,10 @@ public class WindowDelegate {
}
}
public void deleteSettings() {
settings.delete();
}
public void applySettings(Settings settings) {
this.settings = settings;
ComponentsHelper.walkComponents(

View File

@ -287,6 +287,9 @@ unableToLoadControllerClass=Unable to load controller class
searchSelect.notFound=Not found items for filter: %s
searchSelect.minimumLengthOfFilter=Minimum length of search string is %s
actions.saveSettings=Save settings
actions.restoreToDefaults=Restore to defaults
actions.analyzeLayout=Analyze layout
actions.exportSelectedTitle=Confirm export

View File

@ -285,6 +285,8 @@ unableToLoadControllerClass=Невозможно загрузить класс
searchSelect.notFound=Не найдено записей для фильтра: %s
searchSelect.minimumLengthOfFilter=Минимальная длина строки для поиска %s
actions.saveSettings=Сохранить настройки
actions.restoreToDefaults=Сбросить настройки
actions.analyzeLayout=Проанализировать компоновку экрана
timeZone.auto=Авто

View File

@ -8,6 +8,9 @@ import org.dom4j.Element;
/**
* Interface to user settings of a window
*
* @author krivopustov
* @version $Id$
*/
public interface Settings {
@ -18,4 +21,6 @@ public interface Settings {
void setModified(boolean modified);
void commit();
}
void delete();
}

View File

@ -19,4 +19,6 @@ public interface SettingsClient {
String getSetting(String name);
void setSetting(String name, @Nullable String value);
void deleteSettings(String name);
}

View File

@ -86,4 +86,10 @@ public class SettingsImpl implements Settings {
modified = false;
}
}
@Override
public void delete() {
getSettingsClient().deleteSettings(name);
modified = false;
}
}

View File

@ -1074,15 +1074,16 @@ public class AppWindow extends UIView implements UserSubstitutionListener, CubaH
protected Map<Component, TabCloseHandler> closeHandlers = null;
protected com.vaadin.event.Action closeAllTabs;
protected com.vaadin.event.Action closeOtherTabs;
protected com.vaadin.event.Action closeCurrentTab;
protected com.vaadin.event.Action showInfo;
protected com.vaadin.event.Action analyzeLayout;
protected com.vaadin.event.Action saveSettings;
protected com.vaadin.event.Action restoreToDefaults;
public AppTabSheet() {
setCloseHandler(new CloseHandler() {
@Override
@ -1096,16 +1097,19 @@ public class AppWindow extends UIView implements UserSubstitutionListener, CubaH
}
});
addActionHandler(this);
Messages messages = AppBeans.get(Messages.NAME);
closeAllTabs = new com.vaadin.event.Action(messages.getMainMessage("actions.closeAllTabs"));
closeOtherTabs = new com.vaadin.event.Action(messages.getMainMessage("actions.closeOtherTabs"));
closeCurrentTab = new com.vaadin.event.Action(messages.getMainMessage("actions.closeCurrentTab"));
showInfo = new com.vaadin.event.Action(messages.getMainMessage("actions.showInfo"));
analyzeLayout = new com.vaadin.event.Action(messages.getMainMessage("actions.analyzeLayout"));
saveSettings = new com.vaadin.event.Action(messages.getMainMessage("actions.saveSettings"));
restoreToDefaults = new com.vaadin.event.Action(messages.getMainMessage("actions.restoreToDefaults"));
addStyleName("cuba-main-tabsheet");
addActionHandler(this);
}
@Override
@ -1175,15 +1179,19 @@ public class AppWindow extends UIView implements UserSubstitutionListener, CubaH
actions.add(closeAllTabs);
if (target != null) {
Configuration configuration = AppBeans.get(Configuration.NAME);
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
if (clientConfig.getManualSaveScreenSettings()) {
actions.add(saveSettings);
actions.add(restoreToDefaults);
}
UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
UserSession userSession = sessionSource.getUserSession();
if (userSession.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION) &&
findEditor((Layout) target) != null) {
actions.add(showInfo);
}
Configuration configuration = AppBeans.get(Configuration.NAME);
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
if (clientConfig.getLayoutAnalyzerEnabled()) {
actions.add(analyzeLayout);
}
@ -1204,6 +1212,10 @@ public class AppWindow extends UIView implements UserSubstitutionListener, CubaH
showInfo(target);
} else if (analyzeLayout == action) {
analyzeLayout(target);
} else if (saveSettings == action) {
saveSettings(target);
} else if (restoreToDefaults == action) {
restoreToDefaults(target);
}
}
@ -1246,6 +1258,35 @@ public class AppWindow extends UIView implements UserSubstitutionListener, CubaH
}
}
@Nullable
protected com.haulmont.cuba.gui.components.Window getWindow(Object target) {
if (target instanceof Layout) {
Layout layout = (Layout) target;
for (Component component : layout) {
if (component instanceof WindowBreadCrumbs) {
WindowBreadCrumbs breadCrumbs = (WindowBreadCrumbs) component;
return breadCrumbs.getCurrentWindow();
}
}
}
return null;
}
public void restoreToDefaults(Object target) {
com.haulmont.cuba.gui.components.Window window = getWindow(target);
if (window != null) {
window.deleteSettings();
}
}
public void saveSettings(Object target) {
com.haulmont.cuba.gui.components.Window window = getWindow(target);
if (window != null) {
window.saveSettings();
}
}
public interface TabCloseHandler {
void onClose(TabSheet tabSheet, Component tabContent);
}

View File

@ -12,6 +12,7 @@ import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.core.global.SilentException;
import com.haulmont.cuba.gui.*;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.config.WindowInfo;
import com.haulmont.cuba.gui.dev.LayoutAnalyzer;
@ -25,8 +26,7 @@ import com.haulmont.cuba.web.sys.WindowBreadCrumbs;
import com.haulmont.cuba.web.toolkit.ui.CubaLabel;
import com.haulmont.cuba.web.toolkit.ui.CubaTabSheet;
import com.haulmont.cuba.web.toolkit.ui.CubaWindow;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutListener;
import com.vaadin.event.*;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.BorderStyle;
import com.vaadin.shared.ui.label.ContentMode;
@ -774,10 +774,7 @@ public class WebWindowManager extends WindowManager {
protected CubaWindow createDialogWindow(Window window) {
CubaWindow dialogWindow = new CubaWindow(window.getCaption());
dialogWindow.setErrorHandler(ui);
// if layout analyzer allowed
if (clientConfig.getLayoutAnalyzerEnabled()) {
dialogWindow.addContextActionHandler(new LayoutAnalyzerOpener(window));
}
dialogWindow.addContextActionHandler(new DialogWindowActionHandler(window));
return dialogWindow;
}
@ -1276,23 +1273,43 @@ public class WebWindowManager extends WindowManager {
}
}
protected class LayoutAnalyzerOpener implements com.vaadin.event.Action.Handler {
protected class DialogWindowActionHandler implements com.vaadin.event.Action.Handler {
protected Window window;
protected com.vaadin.event.Action analyzeAction =
new com.vaadin.event.Action(messages.getMainMessage("actions.analyzeLayout"));
protected com.vaadin.event.Action saveSettingsAction;
protected com.vaadin.event.Action restoreToDefaultsAction;
public LayoutAnalyzerOpener(Window window) {
protected com.vaadin.event.Action analyzeAction;
public DialogWindowActionHandler(Window window) {
this.window = window;
saveSettingsAction = new com.vaadin.event.Action(messages.getMainMessage("actions.saveSettings"));
restoreToDefaultsAction = new com.vaadin.event.Action(messages.getMainMessage("actions.restoreToDefaults"));
analyzeAction = new com.vaadin.event.Action(messages.getMainMessage("actions.analyzeLayout"));
}
@Override
public com.vaadin.event.Action[] getActions(Object target, Object sender) {
return new com.vaadin.event.Action[]{analyzeAction};
List<com.vaadin.event.Action> actions = new ArrayList<>(3);
if (clientConfig.getManualSaveScreenSettings()) {
actions.add(saveSettingsAction);
actions.add(restoreToDefaultsAction);
}
if (clientConfig.getLayoutAnalyzerEnabled()) {
actions.add(analyzeAction);
}
return actions.toArray(new com.vaadin.event.Action[actions.size()]);
}
@Override
public void handleAction(com.vaadin.event.Action action, Object sender, Object target) {
if (analyzeAction == action) {
if (saveSettingsAction == action) {
window.saveSettings();
} else if (restoreToDefaultsAction == action) {
window.deleteSettings();
} else if (analyzeAction == action) {
LayoutAnalyzer analyzer = new LayoutAnalyzer();
List<LayoutTip> tipsList = analyzer.analyze(window);

View File

@ -864,13 +864,16 @@ public class WebWindow implements Window, Component.Wrapper,
return false;
}
if (closing)
if (closing) {
return true;
}
ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
if (!forceClose && isModified()) {
final Committable committable = (getWrapper() instanceof Committable) ? (Committable) getWrapper() :
(this instanceof Committable) ? (Committable) this : null;
if ((committable != null) && configuration.getConfig(ClientConfig.class).getUseSaveConfirmation()) {
if ((committable != null) && clientConfig.getUseSaveConfirmation()) {
windowManager.showOptionDialog(
messages.getMainMessage("closeUnsaved.caption"),
messages.getMainMessage("saveUnsaved"),
@ -942,10 +945,13 @@ public class WebWindow implements Window, Component.Wrapper,
return false;
}
if (getWrapper() != null)
getWrapper().saveSettings();
else
saveSettings();
if (!clientConfig.getManualSaveScreenSettings()) {
if (getWrapper() != null) {
getWrapper().saveSettings();
} else {
saveSettings();
}
}
delegate.disposeComponents();
@ -976,6 +982,11 @@ public class WebWindow implements Window, Component.Wrapper,
delegate.saveSettings();
}
@Override
public void deleteSettings() {
delegate.deleteSettings();
}
@Override
public String getCaption() {
return caption;

View File

@ -49,6 +49,12 @@ public class WebSettingsClient implements SettingsClient {
userSettingService.saveSetting(ClientType.WEB, name, value);
}
@Override
public void deleteSettings(String name) {
getCache().put(name, Optional.<String>absent());
userSettingService.deleteSettings(ClientType.WEB, name);
}
public void clearCache() {
VaadinSession session = VaadinSession.getCurrent();
session.setAttribute(SettingsClient.NAME, null);

View File

@ -942,6 +942,11 @@ public class WebWindow implements Window, Component.Wrapper, Component.HasXmlDes
delegate.saveSettings();
}
@Override
public void deleteSettings() {
delegate.deleteSettings();
}
@Override
public String getCaption() {
return caption;

View File

@ -50,6 +50,12 @@ public class WebSettingsClient implements SettingsClient {
userSettingService.saveSetting(ClientType.WEB, name, value);
}
@Override
public void deleteSettings(String name) {
getCache().put(name, Optional.<String>absent());
userSettingService.deleteSettings(ClientType.WEB, name);
}
protected Map<String, Optional<String>> getCache() {
HttpSession session = RequestContext.get().getSession();
@SuppressWarnings("unchecked")