Moved repeated code to ConfirmCloseUtil class

The code to manage confirm close dialogue has been moved to a common class.

FEA: ItEr77S03Community
This commit is contained in:
Manuel Rego Casasnovas 2012-11-26 17:14:35 +01:00
parent 25e9184c48
commit 112d990f91
3 changed files with 80 additions and 72 deletions

View file

@ -0,0 +1,68 @@
/*
* This file is part of LibrePlan
*
* Copyright (C) 2012 Igalia, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.libreplan.web.common;
import org.zkoss.ganttz.util.LongOperationFeedback;
import org.zkoss.ganttz.util.LongOperationFeedback.IBackGroundOperation;
import org.zkoss.ganttz.util.LongOperationFeedback.IDesktopUpdate;
import org.zkoss.ganttz.util.LongOperationFeedback.IDesktopUpdatesEmitter;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.util.Clients;
/**
* Utilities class to manage confirm close dialogue.
*
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
public class ConfirmCloseUtil {
private static final int WARNING_ON_EXIT_MS = 30000; // 30 seconds
public static void resetConfirmClose() {
Clients.confirmClose(null);
}
public static void setConfirmClose(Desktop desktop, final String message) {
LongOperationFeedback
.progressive(
desktop,
new IBackGroundOperation<LongOperationFeedback.IDesktopUpdate>() {
@Override
public void doOperation(
IDesktopUpdatesEmitter<IDesktopUpdate> desktopUpdateEmitter) {
try {
Thread.sleep(WARNING_ON_EXIT_MS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
desktopUpdateEmitter
.doUpdate(new IDesktopUpdate() {
@Override
public void doUpdate() {
resetConfirmClose();
Clients.confirmClose(message);
}
});
}
});
}
}

View file

@ -87,6 +87,7 @@ import org.libreplan.business.scenarios.entities.Scenario;
import org.libreplan.business.users.daos.IOrderAuthorizationDAO;
import org.libreplan.business.users.entities.OrderAuthorization;
import org.libreplan.business.workingday.IntraDayDate;
import org.libreplan.web.common.ConfirmCloseUtil;
import org.libreplan.web.common.IMessagesForUser;
import org.libreplan.web.common.MessagesForUser;
import org.libreplan.web.common.concurrentdetection.ConcurrentModificationHandling;
@ -104,13 +105,7 @@ import org.zkoss.ganttz.data.DependencyType.Point;
import org.zkoss.ganttz.data.GanttDate;
import org.zkoss.ganttz.data.constraint.Constraint;
import org.zkoss.ganttz.extensions.IContext;
import org.zkoss.ganttz.util.LongOperationFeedback;
import org.zkoss.ganttz.util.LongOperationFeedback.IBackGroundOperation;
import org.zkoss.ganttz.util.LongOperationFeedback.IDesktopUpdate;
import org.zkoss.ganttz.util.LongOperationFeedback.IDesktopUpdatesEmitter;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
@ -130,8 +125,6 @@ import org.zkoss.zul.Messagebox;
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class SaveCommandBuilder {
private static final int WARNING_ON_EXIT_MS = 3000;
private static final Log LOG = LogFactory.getLog(SaveCommandBuilder.class);
public ISaveCommand build(PlanningState planningState,
@ -285,32 +278,6 @@ public class SaveCommandBuilder {
});
}
private void confirmCloseThread(Desktop desktop) {
LongOperationFeedback
.progressive(
desktop,
new IBackGroundOperation<LongOperationFeedback.IDesktopUpdate>() {
@Override
public void doOperation(
IDesktopUpdatesEmitter<IDesktopUpdate> desktopUpdateEmitter) {
try {
Thread.sleep(WARNING_ON_EXIT_MS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
desktopUpdateEmitter
.doUpdate(new IDesktopUpdate() {
@Override
public void doUpdate() {
Clients.confirmClose(_("You are about to leave the planning edition, unsaved changes will be lost."));
}
});
}
});
}
@Override
public void save(final IBeforeSaveActions beforeSaveActions,
IAfterSaveActions afterSaveActions) {
@ -398,10 +365,12 @@ public class SaveCommandBuilder {
throw new RuntimeException(e);
}
// Reset timer of warning on leaving page
Clients.confirmClose(null);
ConfirmCloseUtil.resetConfirmClose();
if (Executions.getCurrent() != null) {
Clients.confirmClose(null);
confirmCloseThread(Executions.getCurrent().getDesktop());
ConfirmCloseUtil
.setConfirmClose(
Executions.getCurrent().getDesktop(),
_("You are about to leave the planning edition, unsaved changes will be lost."));
}
}

View file

@ -36,6 +36,7 @@ import org.libreplan.business.planner.entities.TaskElement;
import org.libreplan.business.resources.daos.IResourcesSearcher;
import org.libreplan.business.templates.entities.OrderTemplate;
import org.libreplan.business.users.entities.UserRole;
import org.libreplan.web.common.ConfirmCloseUtil;
import org.libreplan.web.common.entrypoints.EntryPointsHandler;
import org.libreplan.web.common.entrypoints.URLHandlerRegistry;
import org.libreplan.web.dashboard.DashboardController;
@ -64,16 +65,12 @@ import org.zkoss.ganttz.adapters.TabsConfiguration.ChangeableTab;
import org.zkoss.ganttz.extensions.ITab;
import org.zkoss.ganttz.extensions.TabProxy;
import org.zkoss.ganttz.util.LongOperationFeedback;
import org.zkoss.ganttz.util.LongOperationFeedback.IBackGroundOperation;
import org.zkoss.ganttz.util.LongOperationFeedback.IDesktopUpdate;
import org.zkoss.ganttz.util.LongOperationFeedback.IDesktopUpdatesEmitter;
import org.zkoss.ganttz.util.LongOperationFeedback.ILongOperation;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zk.ui.util.Composer;
/**
@ -87,8 +84,6 @@ import org.zkoss.zk.ui.util.Composer;
public class MultipleTabsPlannerController implements Composer,
IGlobalViewEntryPoints {
private static final int WARNING_ON_EXIT_MS = 30000; // 30 seconds
private final class TabWithLoadingFeedback extends TabProxy {
private boolean feedback = true;
@ -215,12 +210,15 @@ public class MultipleTabsPlannerController implements Composer,
public void typeChanged(ModeType oldType, ModeType newType) {
switch (newType) {
case GLOBAL:
Clients.confirmClose(null);
ConfirmCloseUtil.resetConfirmClose();
break;
case ORDER:
if (SecurityUtils
.isSuperuserOrUserInRoles(UserRole.ROLE_PLANNING)) {
confirmCloseThread(desktop);
ConfirmCloseUtil
.setConfirmClose(
desktop,
_("You are about to leave the planning edition, unsaved changes will be lost."));
}
break;
default:
@ -453,33 +451,6 @@ public class MultipleTabsPlannerController implements Composer,
}
}
private void confirmCloseThread(Desktop desktop) {
LongOperationFeedback
.progressive(
desktop,
new IBackGroundOperation<LongOperationFeedback.IDesktopUpdate>() {
@Override
public void doOperation(
IDesktopUpdatesEmitter<IDesktopUpdate> desktopUpdateEmitter) {
try {
Thread.sleep(WARNING_ON_EXIT_MS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
desktopUpdateEmitter
.doUpdate(new IDesktopUpdate() {
@Override
public void doUpdate() {
Clients.confirmClose(null);
Clients.confirmClose(_("You are about to leave the planning edition, unsaved changes will be lost."));
}
});
}
});
}
private TabsRegistry getTabsRegistry() {
return tabsSwitcher.getTabsRegistry();
}