I have a Wicket Panel that contains a ListView and then sub-items (Form controls), but when I press an inner CheckBox, the visibility of some of the sub-items should change.
However, calling WebMarkupContainer.setVisible(false) does not hide the items within the ListView after the ListView is redrawn during the AJAX update.
Code below:
public class ImagePanel extends Panel {
private ArrayList<ImageEntry> imageEntryList;
public class ImageEntry implements Serializable {
private static final long serialVersionUID = -3987685200930059655L;
public String thumbnail;
public String filename;
public boolean webDownloaded;
public WebMarkupContainer fileUpload;
public WebMarkupContainer webDownload;
}
public ImagePanel(String id) {
this(id, IMAGE_NORMAL);
}
public ImagePanel(String id, int type) {
super(id);
this.type = type;
wmc = new WebMarkupContainer ("wmc");
wmc.setOutputMarkupId(true);
add(wmc);
imageEntryList = new ArrayList<ImageEntry>();
ImageEntry imageEntry = new ImageEntry();
imageEntry.thumbnail = "blah";
imageEntry.filename = "blah";
imageEntryList.add(imageEntry);
ListView<ImageEntry> llv = new LargeImageListView("large_image_list", imageEntryList);
wmc.add(llv);
SmallImageListView slv = new SmallImageListView("small_image_list", imageEntryList);
wmc.add(slv);
}
private final class SmallImageListView extends ListView<ImageEntry> {
private SmallImageListView(String id, List<? extends ImageEntry> list) {
super(id, list);
}
#Override
protected void populateItem(final ListItem<ImageEntry> item) {
...
if (type == IMAGE_WIZARD) {
item.getModelObject().fileUpload = showWizardFileUpload(item);
item.getModelObject().webDownload = showWizardWebDownload(item);
showSortUpDown(item);
showWebCheckbox(item);
}
}
}
private void showWebCheckbox(final ListItem<ImageEntry> item) {
AjaxCheckBox checkbox = new AjaxCheckBox("use_web_image", new PropertyModel<Boolean>(item.getModelObject(), "webDownloaded")) {
public void onUpdate(AjaxRequestTarget target) {
if (getModelObject()) {
System.out.println("Show");
item.getModelObject().fileUpload.setVisible(false);
item.getModelObject().webDownload.setVisible(false);
} else {
System.out.println("Hide");
item.getModelObject().fileUpload.setVisible(false);
item.getModelObject().webDownload.setVisible(false);
}
target.add(wmc);
}
};
item.add(checkbox);
}
...
}
Use ListView.setReuseItems(true) to ensure that the objects within the ListView are serialized correctly... Otherwise, you will receive a different object each time and the .setVisible() property will be reset to its default value (e.g. 'true').
slv.setReuseItems(true);
Try
WebMarkupContainer.add(new AttributeModifier("style", new Model("display:none")));
or
WebMarkupContainer.add(new AttributeAppender("style", new Model("display:none"), "="));
instead.
Related
So I have been working on a project and I have a CharmListView that populates with the name of the task to be used. I can login through my login screen, get to the CharmListView and click on the task I want to have open. It opens on the Desktop when I am testing it, but in Android it fails, saying the location is not found, and that:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.gluonhq.charm.glisten.mvc.View.setName(java.lang.String)' on a null object reference
Here are my charm classes that I have:
This one is the task model essentially.
public class CharmHomeNavTask {
private String taskName;
private String taskDesc;
private static final Image IMAGE_ADMIN = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/admin.png"));
private static final Image IMAGE_AUDIT_TOOL = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/auditTool.png"));
private static final Image IMAGE_CONSOLIDATE_PACKAGE = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/consolidatePackage.png"));
private static final Image IMAGE_DISPOSAL = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/disposal.png"));
private static final Image IMAGE_EQUIP_MANGAGE = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/equipmentManagement.png"));
private static final Image IMAGE_INTRA_TRANSFER = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/intratransfer.png"));
private static final Image IMAGE_PICKUP = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/pickup.png"));
private static final Image IMAGE_TRU_WASTE_PREP = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/truWastePrep.png"));
private static final Image IMAGE_VISUAL_INSPECTION = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/visualInspection.png"));
private static final Image IMAGE_WALL2WALL = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/wall2wall.png"));
private static final Image IMAGE_WASTE_ID = new Image(WcatsAndroidDemo.class.getResourceAsStream("/gov/lanl/taskImages/wasteIdentification.png"));
private static Image[] listOfImages = {IMAGE_WASTE_ID, IMAGE_VISUAL_INSPECTION, IMAGE_TRU_WASTE_PREP, IMAGE_CONSOLIDATE_PACKAGE, IMAGE_INTRA_TRANSFER, IMAGE_PICKUP, IMAGE_DISPOSAL, IMAGE_ADMIN, IMAGE_WALL2WALL,
IMAGE_EQUIP_MANGAGE, IMAGE_AUDIT_TOOL };
public CharmHomeNavTask(String taskName, String taskDesc){
this.taskName = taskName;
this.taskDesc = taskDesc;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getTaskDesc() {
return taskDesc;
}
public void setTaskDesc(String taskDesc) {
this.taskDesc = taskDesc;
}
public static void setListOfImages(Image[] listOfImages) {
listOfImages = listOfImages;
}
public static Image[] getListOfImages(){
return listOfImages;
}
public static Image getSingleImageFromList(int index){
return listOfImages[index];
}
}
This one is the creation of the tasks into an ObservableList
public class CharmHomeNavTasks {
public static ObservableList<CharmHomeNavTask> tasksList = FXCollections.observableArrayList(
new CharmHomeNavTask("Waste Identification", "Identify you waste from this screen."),
new CharmHomeNavTask("TRU Visual Inspection", "Visual Inspection of TRU waste."),
new CharmHomeNavTask("TRU Drum Preparation", "Prepare your TRU drums here."),
new CharmHomeNavTask("Consolidation/Packaging", "Consolidate and package your waste."),
new CharmHomeNavTask("Intra-Facility Transfer", "Transferring of waste within the same facility."),
new CharmHomeNavTask("Inter-Facility Pickup", "Picking up waste within the same facility."),
new CharmHomeNavTask("Disposal Tasks", "Disposal of waste tasks."),
new CharmHomeNavTask("Administrative Tasks", "Administrators have special tasks they can perform here."),
new CharmHomeNavTask("Wall-Wall Inventory", "Wall to Wall inventory tasks."),
new CharmHomeNavTask("Equipment Management", "Waste related equipment tasks."),
new CharmHomeNavTask("Audit Support Tool", "Tool for audit related tasks."));
public static ObservableList<CharmHomeNavTask> getTasksList() {
return tasksList;
}
}
This is the one that is giving me trouble because I have my onMouseClicked event here and I set the item(or task) to selected. This seems to work fine even on Android as I can the the system to tell me which item was clicked.
public class CharmHomeNavTaskCell extends CharmListCell<CharmHomeNavTask> {
private final ListTile tile;
private final ImageView imageView;
private CharmHomeNavTask item;
public CharmHomeNavTask carryOverItem;
public CharmHomeNavTaskCell(CharmListView listView){
tile = new ListTile();
imageView = new ImageView();
tile.setPrimaryGraphic(imageView);
carryOverItem = item;
tile.setOnMouseClicked(e -> {
System.out.println("******************* Item clicked " + item.getTaskName());
listView.setSelectedItem(item);
});
setText(null);
}
#Override
public void updateItem(CharmHomeNavTask item, boolean empty){
super.updateItem(item, empty);
this.item = item;
imageView.setFitWidth(32);
imageView.setFitHeight(32);
if (item != null && !empty) {
tile.textProperty().setAll(item.getTaskName() + " ", item.getTaskDesc());
tile.setWrapText(true);
final Image[] image = CharmHomeNavTask.getListOfImages();
super.setStyle("-fx-font-weight: bold");
switch (item.getTaskName()) {
case "Waste Identification":
imageView.setImage(image[0]);
break;
case "TRU Visual Inspection":
imageView.setImage(image[1]);
break;
case "TRU Drum Preparation":
imageView.setImage(image[2]);
break;
case "Consolidation/Packaging":
imageView.setImage(image[3]);
break;
case "Intra-Facility Transfer":
imageView.setImage(image[4]);
break;
case "Inter-Facility Pickup":
imageView.setImage(image[5]);
break;
case "Disposal Tasks":
imageView.setImage(image[6]);
break;
case "Administrative Tasks":
imageView.setImage(image[7]);
break;
case "Wall-Wall Inventory":
imageView.setImage(image[8]);
break;
case "Equipment Management":
imageView.setImage(image[9]);
break;
case "Audit Support Tool":
imageView.setImage(image[10]);
break;
}
setGraphic(tile);
} else {
setGraphic(null);
}
}
}
Here is the AppViewManager Class. I am using Glisten and Afterburner just so that you know.
public class AppViewManager {
private static String getLoggedInUser(User user){
if (user != null) {
return user.getId();
} else {
return "Not Logged In";
}
}
public static final AppViewRegistry REGISTRY = new AppViewRegistry();
public static final AppView PRIMARY_VIEW = view("Home", PrimaryPresenter.class, MaterialDesignIcon.HOME, SHOW_IN_DRAWER, HOME_VIEW);
public static final AppView SECONDARY_VIEW = view("Task List", SecondaryPresenter.class, MaterialDesignIcon.LIST, SHOW_IN_DRAWER);
public static final AppView SETTINGS_VIEW = view("Settings", SettingsView.class, MaterialDesignIcon.SETTINGS_APPLICATIONS, SHOW_IN_DRAWER);
public static final AppView INTRAFACILITYTRANSFER_VIEW = view("Intra-Facility Transfer", IntraFacilityView.class, MaterialDesignIcon.EDIT_LOCATION);
private static AppView view(String title, Class<? extends GluonPresenter<?>> presenterClass, MaterialDesignIcon menuIcon, AppView.Flag... flags ) {
return REGISTRY.createView(name(presenterClass), title, presenterClass, menuIcon, flags);
}
private static String name(Class<? extends GluonPresenter<?>> presenterClass) {
return presenterClass.getSimpleName().toUpperCase(Locale.ROOT).replace("PRESENTER", "");
}
public static void registerViewsAndDrawer(MobileApplication app) {
for (AppView view : REGISTRY.getViews()) {
view.registerView(app);
}
Image image = new Image(WcatsAndroidDemo.class.getResourceAsStream("/icon.png"));
NavigationDrawer.Header header = new NavigationDrawer.Header("\nWCATS" + "- " + getLoggedInUser(WcatsAndroidDemo.getInstance().getLoggedUser()),
"Waste Management System", new ImageView(image));
// TODO: Add a footer to the drawer that contains settings, help & feedback, and About
NavigationDrawer.Footer footer = new NavigationDrawer.Footer("No tasks currently need to be synchronized.", null);
//Create the sub items for the drawer
NavigationDrawer.Item about = new NavigationDrawer.Item("About", MaterialDesignIcon.INFO.graphic());
NavigationDrawer.Item logOut = new NavigationDrawer.Item("Logout", MaterialDesignIcon.EXIT_TO_APP.graphic());
// TODO: make the rest of the submenu items that go in the header.
DefaultDrawerManager drawerManager = new DefaultDrawerManager(app, header, REGISTRY.getViews()) {
{
NavigationDrawer drawer = getDrawer();
drawer.visibleProperty().addListener((observable, oldValue, newValue) -> {
if (newValue){
header.setTitle("\nWCATS - " + getLoggedInUser(WcatsAndroidDemo.getInstance().getLoggedUser()));
}
});
// Add items
drawer.setFooter(footer);
footer.setStyle("-fx-text-size: 6");
drawer.getItems().addAll(new Separator(), about, logOut, new Separator());
// TODO: provide action based on item selected
drawer.selectedItemProperty().addListener(((observable, oldValue, newValue) -> {
if(newValue.equals(about)) {
System.out.println("test");
} else if (newValue.equals(logOut)){
WcatsAndroidDemo.getInstance().userLogout();
} else if (newValue.equals(SECONDARY_VIEW.getMenuItem())){
if (getLoggedInUser(WcatsAndroidDemo.getInstance().getLoggedUser()).equals("Not Logged In")) {
AppViewManager.PRIMARY_VIEW.switchView();
} else {
AppViewManager.SECONDARY_VIEW.switchView();
}
}
} ));
}
};
drawerManager.installDrawer();
}
}
Lastly, here is the Presenter class for the view that I am navigation from and want to go to the task that is selected.
public class SecondaryPresenter extends GluonPresenter<WcatsAndroidDemo> {
#FXML
private View homeView;
#FXML
public CharmListView<CharmHomeNavTask, Integer> charmListView;
public void initialize() {
homeView.setShowTransitionFactory(BounceInRightTransition::new);
AppBar appBar = getApp().getAppBar();
homeView.showingProperty().addListener((observable, oldValue, newValue) -> {
appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> getApp().showLayer(DRAWER_LAYER)));
appBar.setTitleText("Task Selection");
});
charmListView.setFloatingHeaderVisible(false);
charmListView.setItems(CharmHomeNavTasks.getTasksList());
charmListView.setCellFactory(param -> new CharmHomeNavTaskCell(charmListView));
charmListView.selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.getTaskName().equals("Intra-Facility Transfer")){
AppViewManager.INTRAFACILITYTRANSFER_VIEW.switchView(ViewStackPolicy.SKIP);
}
});
}
}
Just in case you do need it to figure anything out here is the view that I am trying to navigate to.
public class IntraFacilityView extends GluonPresenter<WcatsAndroidDemo> {
#FXML
public ChoiceBox facilityCMBox;
#FXML
public ChoiceBox storageUnitOrgCMBox;
#FXML
public ChoiceBox storageUnitDestCMBox;
#FXML
public ChoiceBox gridXCMBox;
#FXML
public ChoiceBox gridYCMBox;
#FXML
public ChoiceBox gridZCMBox;
#FXML
public CheckBox organizeUnitCHKBox;
#FXML
public Button viewMoreReqsBTN;
#FXML
public Button viewPendingMovesBTN;
#FXML
public Button resumeTaskBTN;
#FXML
private View intrafacility;
public void initialize(){
intrafacility.setShowTransitionFactory(BounceInRightTransition::new);
intrafacility.showingProperty().addListener((observable, oldValue, newValue) -> {
if (newValue) {
AppBar appBar = getApp().getAppBar();
appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> {
getApp().showLayer(DRAWER_LAYER);
}));
appBar.setTitleText("Intra-Facility Transfer");
appBar.getActionItems().add(MaterialDesignIcon.CLOSE.button(e -> {
AppViewManager.SECONDARY_VIEW.switchView();
}));
}
});
}
public void handleOrganizeUnitsCHKBox(ActionEvent actionEvent) {
}
public void handleViewMoreReqs(ActionEvent actionEvent) {
}
public void handleResumeTask(ActionEvent actionEvent) {
}
public void handlevVewPendingMoves(ActionEvent actionEvent) {
}
}
I know that some Android devices are not the best with JavaFxPorts, but I haven't seen anything about Panasonic toughpads having known issues like Samsung. I did have to do some janky stuff to get the abdroid devices to register the touches as clicks, but that seems to be working fine now.
This has me as a standstill as I can not figure out why Android can not find the location but it works fine on the desktop.
Here is the file structure:
Project structure
I am trying to find the best way to hide a UI component in a specific sub View but let it be visible in others.
This is the code of my UI class:
public class DCSAdminUI extends UI {
private static final long serialVersionUID = 1L;
VerticalLayout root = new VerticalLayout();
CssLayout content = new CssLayout();
private MenuBar nav = new MenuBar();
private MenuBar userNav = new MenuBar();
private Navigator navigator;
public static final String PERSISTENCE_UNIT = "mystery";
public static DCSAdminUI getCurrent() {
return (DCSAdminUI) UI.getCurrent();
}
public static DCSAdminUI getApplication() {
return (DCSAdminUI) getApplication();
}
#Override
protected void init(VaadinRequest request) {
Page.getCurrent().setTitle("Admin");
initLayout();
initNav();
initUserNav();
getUserOrg();
navigator = new Navigator(this, content);
LoginUI loginView = new LoginUI();
navigator.addView("", loginView);
navigator.addView(ActivitiesUI.VIEW_NAME.toLowerCase(), new ActivitiesUI());
navigator.addView(BookingsUI.VIEW_NAME.toLowerCase(), new BookingsUI());
navigator.addView(OperatorsUI.VIEW_NAME.toLowerCase(), new OperatorsUI());
navigator.addView(TeamUI.VIEW_NAME.toLowerCase(), new TeamUI());
navigator.addView(OrganisationsUI.VIEW_NAME.toLowerCase(), new OrganisationsUI());
navigator.addView(PlayersUI.VIEW_NAME.toLowerCase(), new PlayersUI());
navigator.addViewChangeListener(new ViewChangeListener() {
private static final long serialVersionUID = 1L;
#Override
public boolean beforeViewChange(ViewChangeEvent event) {
return true;
}
#Override
public void afterViewChange(ViewChangeEvent event) {
if (event.getViewName() == null || event.getViewName().equals("")) {
updateNavSelection("activities");
} else {
updateNavSelection(event.getViewName());
}
}
});
}
private void initLayout() {
root.setSizeFull();
setContent(root);
HorizontalLayout topbar = new HorizontalLayout();
topbar.addStyleName("topbar");
topbar.setWidth("100%");
topbar.setSpacing(true);
root.addComponent(topbar);
content.setSizeFull();
root.addComponent(content);
root.setExpandRatio(content, 1);
nav.addStyleName("nav");
topbar.addComponent(nav);
userNav.addStyleName("user-nav");
topbar.addComponent(userNav);
topbar.setComponentAlignment(userNav, Alignment.TOP_CENTER);
}
private void initNav() {
nav.addItem(ActivitiesUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
nav.addItem(BookingsUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
nav.addItem(PlayersUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
nav.addItem(TeamUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
nav.addItem(OperatorsUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
nav.addItem(OrganisationsUI.VIEW_NAME.toUpperCase(Locale.GERMANY), navCommand).setCheckable(true);
}
private void initUserNav() {
MenuItem username = userNav.addItem("DCS User", FontAwesome.CHEVRON_DOWN, null);
username.addItem("Edit Profile", null);
username.addItem("Settings", null);
username.addSeparator();
username.addItem("Logout", null);
}
private Command navCommand = new Command() {
private static final long serialVersionUID = 1L;
#Override
public void menuSelected(MenuItem selectedItem) {
updateNavSelection(selectedItem.getText());
navigator.navigateTo(selectedItem.getText().toLowerCase());
}
};
private void updateNavSelection(String selectedItem) {
for (MenuItem item : nav.getItems()) {
item.setChecked(item.getText().toLowerCase()
.equals(selectedItem.toLowerCase()));
}
}
void navigateTo(String menuItem) {
for (MenuItem item : nav.getItems()) {
if (item.getText().toLowerCase().equals(menuItem.toLowerCase())) {
navCommand.menuSelected(item);
return;
}
}
}
}
The UI component in question here is topbar in the initLayout() method which is the app's main MenuBar() but I want this to appear only for authenticated users, meaning that it should be hidden in LoginUI() (the default View class for now). My search efforts have led me to understand that I can use multiple UI classes but that this would eventually become tedious to maintain and that is why I would ideally like to find a way of hiding the topbar component in specific View classes
You can implement a ViewChangeListener in your UI and deal with your navbar in the beforeViewChange. Since this can also reject entering a View you can send anon users to the login too.
I have the following implementation:
class CustomTableRow<T> implements CallBack <Tableview<T> TableRow<T>>{
private T selectedItem;
public CustomTableRow(T selectedItem){
this.selectedItem = selectedItem;
}
#Override
public TableRow<T> call(TableView<T> param){
TableRow<T> row = new TableRow<>΄
row.setOnMouseClicked(event ->{
if (!row.isEmpty()){
try{
//Using BeanUtils.copyProperties();
}catch (Exception e){
}
}
});
}
}
I have a class that uses this CustomTableRow
class SomeClass{
private Tableview myTableView;
private MyCustomModelClass selectedItem;
private setUpTableView(){
selectedItem = new MyCustomModelClass();
tableView.setRowFactory(new CustomTableRow<MyCustomModelClass>(selectedItem));
}
private thisNeedsToBeExecutedOnRowDoubleClick(){
}
}
To make things worst this CustomTableRow is used by other TableViews that don't need to implement the double click function.
Should I create two different CustomTableRow classes or is it possible to pass a callback function (like javascript) to execute when double click is detected and selectedItem is of MyCustomModelClass type? I am using java version 8 Update 45.
Define your CustomTableRow to take a Consumer<T> as a parameter, and invoke that object's accept(...) method when the double-click occurs. Here's the basic idea (I renamed CustomTableRow to DoubleClickTableRowFactory as I think it's a better description of what this does):
public class MouseClickTableRowFactory<T>
implements Callback<TableView<T>, TableRow<T>> {
private final Consumer<T> doubleClickHandler ;
private final int numClicks ;
public MouseClickTableRowFactory(Consumer<T> doubleClickHandler, int numClicks) {
this.doubleClickHandler = doubleClickHandler ;
this.numClicks = numClicks ;
}
public MouseClickTableRowFactory(Consumer<T> doubleClickHandler) {
// default to double-click...
this(doubleClickHandler, 2);
}
#Override
public TableRow<T> call(TableView<T> tv) {
TableRow<T> row = new TableRow<>();
row.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
if (event.getClickCount() == numClicks && ! row.isEmpty()) {
doubleClickHandler.accept(row.getItem());
}
});
return row ;
}
}
Now you can do
public class SomeClass {
private TableView<MyCustomModelClass> myTableView ;
// ...
private void setupTable() {
myTableView.setRowFactory(
// invokes rowDoubleClicked when user double-clicks on a row:
new MouseClickTableRowFactory<>(this::rowDoubleClicked));
// for single click, do
// myTableView.setRowFactory(
// new MouseClickTableRowFactory(item -> { ... }, 1));
// ...
}
private void rowDoubleClicked(MyCustomModelClass item) {
// handle double-click on row displaying item
}
}
I have a ValueAwareEditor that contains a couple of sub editors:
Essentially, an OfferDto is composed of a TariffDto and a Commission. The Commission can be one of 4 sub-types, but there is only ever one. Usually this list of possible commissions inside the TariffDto will only contain one element, but it can sometimes contain two.
public class OfferDto
{
private TariffDto tariff;
// selected from the list in the tariff
private Commission commission;
}
public class TariffDto extends EntityDto
{
// omitted for brevity...
protected List<Commission> commissions = new ArrayList<Commission>();
}
When commissions contains more than one item, I want to display a dropdown with the two optiions, and add allow the user to choose between them, each time resetting the commission in the OfferDto and the CommissionEditor.
The problem is that, when call commission.setValue() for the second time, the editor does not change. What should I be doing here?
public class OfferEditor extends Composite implements ValueAwareEditor<OfferDto>
{
#UiField
TariffRenderer tariff;
#Ignore
#UiField
HTMLPanel panel;
#UiField
CommissionEditor commission;
#Override
public void setValue(final OfferDto value)
{
panel.clear();
List<Commission> commissions = value.getTariff().getCommissions();
if(commissions.size() == 1)
{
value.setCommission(commissions.get(0));
}
else
{
// multiple commissions
ValueListBox<Commission> dropdown = new ValueListBox<Commission>(new Renderer<Commission>()
{
#Override
public String render(Commission object)
{
return object == null ? "" : object.getName();
}
#Override
public void render(Commission object, Appendable appendable) throws IOException
{
appendable.append(render(object));
}
});
dropdown.setValue(value.getCommission());
dropdown.setAcceptableValues(commissions);
dropdown.addValueChangeHandler(new ValueChangeHandler<Commission>()
{
#Override
public void onValueChange(ValueChangeEvent<Commission> event)
{
Commission selected = event.getValue();
// this works, but the CommissionEditor that was first rendered remains
value.setCommission(selected);
}
});
panel.add(dropdown);
}
}
}
Currently, I am rendering the list of commissions in a ValueListBox, then when the value changes I am pushing that value to the OfferDto. The Commission seems to get set right, but the subEditor does not change.
Any help greatly appreciated.
EDIT:
CommissionEditor shows the relevant sub-editor depending on the type.
public class CommissionEditor extends Composite implements Editor<Commission>
{
private static CommissionEditorUiBinder uiBinder = GWT.create(CommissionEditorUiBinder.class);
interface CommissionEditorUiBinder extends UiBinder<Widget, CommissionEditor>
{
}
#UiField
Panel subEditorPanel;
public CommissionEditor()
{
initWidget(uiBinder.createAndBindUi(this));
}
#Ignore
final UnitRateCommissionEditor unitRateCommissionEditor = new UnitRateCommissionEditor();
#Path("")
final AbstractSubTypeEditor<Commission, UnitRateCommission, UnitRateCommissionEditor> unitRateCommissionEditorWrapper = new AbstractSubTypeEditor<Commission, UnitRateCommission, UnitRateCommissionEditor>(
unitRateCommissionEditor)
{
#Override
public void setValue(final Commission value)
{
if(value instanceof UnitRateCommission)
{
setValue(value, value instanceof UnitRateCommission);
System.out.println("UnitRateCommission setValue");
subEditorPanel.clear();
subEditorPanel.add(unitRateCommissionEditor);
}
}
};
#Ignore
final StandingChargeCommissionEditor standingChargeCommissionEditor = new StandingChargeCommissionEditor();
#Path("")
final AbstractSubTypeEditor<Commission, StandingChargeCommission, StandingChargeCommissionEditor> standingChargeCommissionEditorWrapper = new AbstractSubTypeEditor<Commission, StandingChargeCommission, StandingChargeCommissionEditor>(
standingChargeCommissionEditor)
{
#Override
public void setValue(final Commission value)
{
if(value instanceof StandingChargeCommission)
{
setValue(value, value instanceof StandingChargeCommission);
System.out.println("StandingChargeCommission setValue");
subEditorPanel.clear();
subEditorPanel.add(standingChargeCommissionEditor);
}
}
};
#Ignore
final PerMwhCommissionEditor perMwhCommissionEditor = new PerMwhCommissionEditor();
#Path("")
final AbstractSubTypeEditor<Commission, PerMwhCommission, PerMwhCommissionEditor> perMwhCommissionEditorWrapper = new AbstractSubTypeEditor<Commission, PerMwhCommission, PerMwhCommissionEditor>(
perMwhCommissionEditor)
{
#Override
public void setValue(final Commission value)
{
if(value instanceof PerMwhCommission)
{
setValue(value, value instanceof PerMwhCommission);
System.out.println("PerMwhCommission setValue");
subEditorPanel.clear();
subEditorPanel.add(perMwhCommissionEditor);
}
}
};
}
Possible Solution:
I changed OfferEditor as so:
public class OfferEditor extends Composite implements Editor<OfferDto>
{
#UiField
TariffRenderer tariff;
#Path("tariff.commissions")
#UiField
CommissionsEditor commission;
}
New editor CommissionsEditor is a CompositeEditor. It needs to take List tariff.commissions and set the chosen Commission into offer.commission:
public class CommissionsEditor extends Composite implements CompositeEditor<List<Commission>, Commission, CommissionEditor>
{
private static CommissionsEditorUiBinder uiBinder = GWT.create(CommissionsEditorUiBinder.class);
interface CommissionsEditorUiBinder extends UiBinder<Widget, CommissionsEditor>
{
}
private EditorChain<Commission, CommissionEditor> chain;
#UiField
FlowPanel dropdownPanel, subEditorPanel;
#Ignore
CommissionEditor subEditor;
public CommissionsEditor()
{
initWidget(uiBinder.createAndBindUi(this));
}
#Override
public void setValue(List<Commission> valueList)
{
// clear both panels
dropdownPanel.clear();
subEditorPanel.clear();
if(valueList.size() == 1)
{
// set the commission to the first in the list
Commission selected = valueList.get(0);
subEditor = new CommissionEditor();
subEditorPanel.add(subEditor);
chain.attach(selected, subEditor);
}
else if(valueList.size() > 1)
{
ValueListBox<Commission> dropdown = new ValueListBox<Commission>(new Renderer<Commission>()
{
#Override
public String render(Commission object)
{
return object == null ? "" : object.getName();
}
#Override
public void render(Commission object, Appendable appendable) throws IOException
{
appendable.append(render(object));
}
});
dropdownPanel.add(dropdown);
dropdown.setValue(valueList.get(0));
dropdown.setAcceptableValues(valueList);
dropdown.addValueChangeHandler(new ValueChangeHandler<Commission>()
{
#Override
public void onValueChange(ValueChangeEvent<Commission> event)
{
Commission selected = event.getValue();
subEditorPanel.clear();
CommissionEditor subEditor = new CommissionEditor();
subEditorPanel.add(subEditor);
chain.attach(selected, subEditor);
}
});
}
}
#Override
public void flush()
{
}
#Override
public void onPropertyChange(String... paths)
{
// TODO Auto-generated method stub
}
#Override
public void setDelegate(EditorDelegate<List<Commission>> delegate)
{
// TODO Auto-generated method stub
}
#Override
public CommissionEditor createEditorForTraversal()
{
return new CommissionEditor();
}
#Override
public String getPathElement(CommissionEditor subEditor)
{
return null;
}
#Override
public void setEditorChain(EditorChain<Commission, CommissionEditor> chain)
{
this.chain = chain;
}
}
When the CommissionsEditor renders the dropdown and onValueChange() is called, the new editor gets created, but the value for the commission never seems to get set.
For some reason the selected subEditor's value is not pushed into offer.setCommission(). I thought chain.attach() would perform this for me?
I have a ButtonField on MainScreen, from which I am pushing a PopupScreen where I have added an ObjectListfield. What I want to do is to update the label of ButtonField on MainScreen with the element selected from ObjectListfield of PopupScreen.
Please tell me if it is possible to do without using Dialog class (I really want to use PopupScreen and not Dialog class) and the method by which this can be done. I'd appreciate if some sample code will be provided.
I have added my code.
public final class MyScreen extends MainScreen {
HorizontalFieldManager hfm;
ButtonField btn;
public MyScreen() {
// Set the displayed title of the screen
super();
setTitle("MyTitle");
btn = new ButtonField("label",ButtonField.CONSUME_CLICK);
final mypopup mps = new mypopup();
btn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field,int context) {
UiApplication.getUiApplication().pushModalScreen(mps);
}
});
hfm = new HorizontalFieldManager();
hfm.add(btn);
add(hfm);
}
public void setlabel(String labelnew) {
btn.setLabel(labelnew);
}
public String getlabel() {
return this.btn.getLabel();
}
}
class mypopup extends PopupScreen implements FieldChangeListener {
String it;
ObjectListField obj = new ObjectListField() {
public boolean navigationClick(int status,int time) {
int selectedindex=obj.getSelectedIndex();
it=(String)obj.get(obj, selectedindex);
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
/*MyScreen my=new MyScreen();
my.btn.setLabel(it);
my.invalidate(); */
//close();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
/* This im doing to see that setlabel and getlabel are
working properly */
MyScreen my=new MyScreen();
my.setlabel(it);
String gt=my.getlabel();
Dialog.alert(gt);
my.hfm.invalidate();
//the label of button is changed but not updating in mainscreen.
}
});
return true;
}
};
public mypopup() {
super(new VerticalFieldManager());
String[] type=new String[] {"a","b","c","d"};
obj.set(type);
add(obj);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
}
}
You need to change following block of code,
MyScreen my = new MyScreen();
my.setlabel(it);
String gt = my.getlabel();
Dialog.alert(gt);
my.hfm.invalidate();
With the code block,
Screen scr = UiApplication.getUiApplication().getActiveScreen();
if (scr instanceof MyScreen) {
MyScreen my = (MyScreen) scr;
my.setlabel(it);
my.invalidate();
}
Add the button in one Manager either HorizontalFieldManager or VerticalFieldManager and after setting text on button invalidate the managerlike this way
public final class MyScreen extends MainScreen
{
ButtonField btn;
public MyScreen()
{
// Set the displayed title of the screen
super();
setTitle("MyTitle");
btn=new ButtonField("label",ButtonField.CONSUME_CLICK);
final mypopup mps=new mypopup();
btn.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field,int context){
UiApplication.getUiApplication().pushModalScreen(mps);
}
});
HorizontalFieldManager hfmToholdButtons = new HorizontalFieldManager();
btn.setLabel(mps.gettext());
hfmToholdButtons.add(btn);
hfmToholdButtons.invalidate();
}
}