package org.sigmah.client.ui.presenter.project.logframe.menu;
/*
* #%L
* Sigmah
* %%
* Copyright (C) 2010 - 2016 URD
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.sigmah.client.i18n.I18N;
import org.sigmah.client.ui.res.icon.IconImageBundle;
import org.sigmah.client.ui.view.project.logframe.grid.FlexTableView;
import org.sigmah.client.ui.view.project.logframe.grid.Row;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
/**
* Represents an actions menu for a row in the log frame grid.
*
* @author tmi (v1.3)
* @author Denis Colliot (dcolliot@ideia.fr) (v2.0)
*/
public abstract class RowActionsMenu extends ActionsMenu {
/**
* The row.
*/
private final Row<?> row;
/**
* Builds this menu.
*
* @param view
* The view where this menu is displayed.
* @param row
* The row managed by this menu.
* @param isActivity
* indicate if the view is a log frame activity.
*/
public RowActionsMenu(final FlexTableView view, final Row<?> row, final boolean isActivity) {
super(view);
// Sets local attributes.
this.row = row;
// Move up action.
final MenuItem upMenuItem = createMoveUpAction();
// Move down action.
final MenuItem downMenuItem = createMoveDownAction();
// Delete action.
final MenuItem deleteMenuItem = createDeleteAction();
// Menu.
menu.add(upMenuItem);
menu.add(downMenuItem);
// Add the modify item for the log frame activities elements.
if (isActivity) {
// Modify action
final MenuItem modifyMenuItem = createModifyAction();
menu.add(modifyMenuItem);
}
menu.add(deleteMenuItem);
}
/**
* Builds and returns the move up action.
*
* @return The move up action.
*/
private MenuItem createMoveUpAction() {
final MenuAction action = new MenuAction() {
private final int movesCount = 1;
@Override
public void perform() {
// Tries to move up the element.
if (beforeMoveDown()) {
view.moveRow(row.getParent(), row.getId(), movesCount);
}
}
@Override
public String getText() {
return I18N.CONSTANTS.logFrameActionUp();
}
@Override
public AbstractImagePrototype getIcon() {
return IconImageBundle.ICONS.up();
}
@Override
public String canBePerformed() {
final boolean canBeMoved = canBeMovedUp() && view.canBeMoved(row.getParent(), row.getId(), movesCount);
if (canBeMoved) {
return null;
} else {
return I18N.CONSTANTS.logFrameActionUpUnavailable();
}
}
};
action.setInactivationPolicy(inactivationPolicy);
// Adds it locally.
actions.add(action);
return action.getMenuItem();
}
/**
* Builds and returns the move down action.
*
* @return The move down action.
*/
private MenuItem createMoveDownAction() {
final MenuAction action = new MenuAction() {
private final int movesCount = -1;
@Override
public void perform() {
// Tries to move down the element.
if (beforeMoveDown()) {
view.moveRow(row.getParent(), row.getId(), movesCount);
}
}
@Override
public String getText() {
return I18N.CONSTANTS.logFrameActionDown();
}
@Override
public AbstractImagePrototype getIcon() {
return IconImageBundle.ICONS.down();
}
@Override
public String canBePerformed() {
final boolean canBeMoved = canBeMovedDown() && view.canBeMoved(row.getParent(), row.getId(), movesCount);
if (canBeMoved) {
return null;
} else {
return I18N.CONSTANTS.logFrameActionDownUnavailable();
}
}
};
action.setInactivationPolicy(inactivationPolicy);
// Adds it locally.
actions.add(action);
return action.getMenuItem();
}
/**
* Builds and returns the delete action.
*
* @return The delete action.
*/
private MenuItem createDeleteAction() {
final MenuAction action = new MenuAction() {
@Override
public void perform() {
// Tries to remove the element.
if (beforeRemove()) {
// Removes the row from the view.
view.removeRow(row.getParent(), row.getId());
}
}
@Override
public String getText() {
return I18N.CONSTANTS.logFrameActionDelete();
}
@Override
public AbstractImagePrototype getIcon() {
return IconImageBundle.ICONS.delete();
}
@Override
public String canBePerformed() {
final boolean canBeRemoved = canBeRemoved();
if (canBeRemoved) {
return null;
} else {
return I18N.CONSTANTS.logFrameActionDeleteUnavailable();
}
}
};
action.setInactivationPolicy(inactivationPolicy);
// Adds it locally.
actions.add(action);
return action.getMenuItem();
}
/**
* Builds and returns the modify action.
*
* @return The modify action.
*/
private MenuItem createModifyAction() {
final MenuAction action = new MenuAction() {
@Override
public void perform() {
// Show the action property window
buidModifyActionPopUp();
}
@Override
public String getText() {
return I18N.CONSTANTS.edit();
}
@Override
public AbstractImagePrototype getIcon() {
return IconImageBundle.ICONS.editActivity();
}
@Override
public String canBePerformed() {
// the update action can be performed
return null;
}
};
action.setInactivationPolicy(inactivationPolicy);
// Adds it locally.
actions.add(action);
return action.getMenuItem();
}
/**
* Returns if the element managed by this menu can be removed.
*
* @return If the element can be removed.
*/
public abstract boolean canBeRemoved();
/**
* Method called just before removing the element managed by this menu. If this method returns <code>true</code>, the
* corresponding row will be removed in the view. Otherwise, this method has no effect.
*
* @return If the element has been removed.
*/
public abstract boolean beforeRemove();
/**
* Returns if the element managed by this menu can be moved up.
*
* @return If the element can be moved up.
*/
public abstract boolean canBeMovedUp();
/**
* Method called just before moving up the element managed by this menu. If this method returns <code>true</code>, the
* corresponding row will be moved up in the view. Otherwise, this method has no effect.
*
* @return If the element has been moved up.
*/
public abstract boolean beforeMoveUp();
/**
* Returns if the element managed by this menu can be moved down.
*
* @return If the element can be moved down.
*/
public abstract boolean canBeMovedDown();
/**
* Method called just before moving down the element managed by this menu. If this method returns <code>true</code>,
* the corresponding row will be moved down in the view. Otherwise, this method has no effect.
*
* @return If the element has been moved up.
*/
public abstract boolean beforeMoveDown();
/**
* Method called while selecting activity modify item.
*/
public abstract void buidModifyActionPopUp();
}