Example usage for org.apache.wicket Component setDefaultModelObject

List of usage examples for org.apache.wicket Component setDefaultModelObject

Introduction

In this page you can find the example usage for org.apache.wicket Component setDefaultModelObject.

Prototype

@SuppressWarnings("unchecked")
public final Component setDefaultModelObject(final Object object) 

Source Link

Document

Sets the backing model object.

Usage

From source file:org.devgateway.eudevfin.ui.common.events.CofinancingField14UpdateBehavior.java

License:Open Source License

@Override
protected void onEventExtra(Component component, IEvent<?> event) {
    if (!component.getParent().getParent().getParent().getParent().isVisibleInHierarchy())
        return;/*from  ww w.ja va 2  s . c o  m*/
    Field14aChangedEventPayload eventPayload = getEventPayload(event);
    if (!eventPayload.getField14Code()) {
        component.setEnabled(false);
        component.setDefaultModelObject(null);
    } else
        component.setEnabled(true);
}

From source file:org.devgateway.eudevfin.ui.common.events.DisbursementCurrentYearUpdateBehavior.java

License:Open Source License

@Override
protected void onEventExtra(Component component, IEvent<?> event) {
    if (!component.getParent().isVisibleInHierarchy())
        return;//from  www . j a v  a  2 s . c om
    ReportingYearChangedEventPayload eventPayload = getEventPayload(event);
    if (Calendar.getInstance().get(Calendar.YEAR) == eventPayload.getReportingYear()) {
        component.setEnabled(false);
        component.setDefaultModelObject(null);
    } else
        component.setEnabled(true);
}

From source file:org.devgateway.eudevfin.ui.common.events.LoansField12UpdateBehavior.java

License:Open Source License

@Override
protected void onEventExtra(Component component, IEvent<?> event) {
    if (!component.getParent().getParent().getParent().getParent().isVisibleInHierarchy())
        return;//  w ww  . j av a2s .c  om
    Field12ChangedEventPayload eventPayload = getEventPayload(event);
    if (pattern.matcher(eventPayload.getField12Code()).matches()) {
        component.setEnabled(false);
        component.setDefaultModelObject(null);
    } else
        component.setEnabled(true);
}

From source file:org.devgateway.eudevfin.ui.common.events.MarkersField13UpdateBehavior.java

License:Open Source License

@Override
protected void onEventExtra(Component component, IEvent<?> event) {
    if (!component.getParent().getParent().getParent().getParent().isVisibleInHierarchy())
        return;//from  w w  w .  j  a  v a 2  s. com
    Field13ChangedEventPayload eventPayload = getEventPayload(event);
    if (pattern.matcher(eventPayload.getField13Code()).matches()) {
        component.setEnabled(false);
        component.setDefaultModelObject(null);
    }

    else
        component.setEnabled(true);
}

From source file:org.geoserver.backuprestore.web.BackupRestoreDataPage.java

License:Open Source License

/**
 * @param form//from   ww  w . j  a  va 2 s. c  o m
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void populateBackupForm(Form form) {
    form.add(new CheckBox("backupOptOverwirte", new Model<Boolean>(false)));
    form.add(new CheckBox("backupOptBestEffort", new Model<Boolean>(false)));
    form.add(statusLabel = new Label("status", new Model()).setOutputMarkupId(true));
    form.add(new AjaxSubmitLink("newBackupStart", form) {
        @Override
        protected void disableLink(ComponentTag tag) {
            super.disableLink(tag);
            tag.setName("a");
            tag.addBehavior(AttributeModifier.replace("class", "disabled"));
        }

        protected void onError(AjaxRequestTarget target, Form<?> form) {
            target.add(feedbackPanel);
        }

        protected void onSubmit(AjaxRequestTarget target, final Form<?> form) {

            //update status to indicate we are working
            statusLabel.add(AttributeModifier.replace("class", "working-link"));
            statusLabel.setDefaultModelObject("Working");
            target.add(statusLabel);

            //enable cancel and disable this
            Component cancel = form.get("cancel");
            cancel.setEnabled(true);
            target.add(cancel);

            setEnabled(false);
            target.add(this);

            final AjaxSubmitLink self = this;

            final Long jobid;
            try {
                jobid = launchBackupExecution(form);
            } catch (Exception e) {
                error(e);
                LOGGER.log(Level.WARNING, "Error starting a new Backup", e);
                return;
            } finally {
                //update the button back to original state
                resetButtons(form, target, "newBackupStart");

                target.add(feedbackPanel);
            }

            cancel.setDefaultModelObject(jobid);
            this.add(new AbstractAjaxTimerBehavior(Duration.milliseconds(100)) {
                @Override
                protected void onTimer(AjaxRequestTarget target) {
                    Backup backupFacade = BackupRestoreWebUtils.backupFacade();
                    BackupExecutionAdapter exec = backupFacade.getBackupExecutions().get(jobid);

                    if (!exec.isRunning()) {
                        try {
                            if (exec.getAllFailureExceptions() != null
                                    && !exec.getAllFailureExceptions().isEmpty()) {
                                getSession().error(exec.getAllFailureExceptions().get(0));
                                setResponsePage(BackupRestoreDataPage.class);
                            } else if (exec.isStopping()) {
                                //do nothing
                            } else {
                                PageParameters pp = new PageParameters();
                                pp.add("id", exec.getId());
                                pp.add("clazz", BackupExecutionAdapter.class.getSimpleName());

                                setResponsePage(BackupRestorePage.class, pp);
                            }
                        } catch (Exception e) {
                            error(e);
                            LOGGER.log(Level.WARNING, "", e);
                        } finally {
                            stop(null);

                            //update the button back to original state
                            resetButtons(form, target, "newBackupStart");

                            target.add(feedbackPanel);
                        }
                        return;
                    }

                    String msg = exec != null ? exec.getStatus().toString() : "Working";

                    statusLabel.setDefaultModelObject(msg);
                    target.add(statusLabel);
                };

                @Override
                public boolean canCallListenerInterface(Component component, Method method) {
                    if (self.equals(component)
                            && method.getDeclaringClass()
                                    .equals(org.apache.wicket.behavior.IBehaviorListener.class)
                            && method.getName().equals("onRequest")) {
                        return true;
                    }
                    return super.canCallListenerInterface(component, method);
                }
            });
        }

        private Long launchBackupExecution(Form<?> form) throws Exception {
            ResourceFilePanel panel = (ResourceFilePanel) newBackupRestorePanel.get("backupResource");
            Resource archiveFile = null;
            try {
                archiveFile = panel.getResource();
            } catch (NullPointerException e) {
                throw new Exception("Backup Archive File is Mandatory!");
            }

            if (archiveFile == null || archiveFile.getType() == Type.DIRECTORY
                    || FilenameUtils.getExtension(archiveFile.name()).isEmpty()) {
                throw new Exception("Backup Archive File is Mandatory and should not be a Directory or URI.");
            }

            Filter filter = null;
            WorkspaceInfo ws = (WorkspaceInfo) workspace.getObject();
            if (ws != null) {
                filter = ECQL.toFilter("name = '" + ws.getName() + "'");
            }

            Hints hints = new Hints(new HashMap(2));

            Boolean backupOptOverwirte = ((CheckBox) form.get("backupOptOverwirte")).getModelObject();
            Boolean backupOptBestEffort = ((CheckBox) form.get("backupOptBestEffort")).getModelObject();

            if (backupOptBestEffort) {
                hints.add(new Hints(new Hints.OptionKey(Backup.PARAM_BEST_EFFORT_MODE),
                        Backup.PARAM_BEST_EFFORT_MODE));
            }

            Backup backupFacade = BackupRestoreWebUtils.backupFacade();

            return backupFacade.runBackupAsync(archiveFile, backupOptOverwirte, filter, hints).getId();
        }
    });

    form.add(new AjaxLink<Long>("cancel", new Model<Long>()) {
        protected void disableLink(ComponentTag tag) {
            super.disableLink(tag);
            tag.setName("a");
            tag.addBehavior(AttributeModifier.replace("class", "disabled"));
        };

        @Override
        public void onClick(AjaxRequestTarget target) {
            Long jobid = getModelObject();

            if (jobid != null) {
                try {
                    BackupRestoreWebUtils.backupFacade().stopExecution(jobid);
                    setResponsePage(BackupRestoreDataPage.class);
                } catch (NoSuchJobExecutionException | JobExecutionNotRunningException e) {
                    LOGGER.log(Level.WARNING, "", e);
                }
            }

            setEnabled(false);

            target.add(this);
        }
    }.setOutputMarkupId(true).setEnabled(false));

    backupRestoreExecutionsTable = new BackupRestoreExecutionsTable("backups",
            new BackupRestoreExecutionsProvider(true, BackupExecutionAdapter.class) {
                @Override
                protected List<org.geoserver.web.wicket.GeoServerDataProvider.Property<AbstractExecutionAdapter>> getProperties() {
                    return Arrays.asList(ID, STATE, STARTED, PROGRESS, ARCHIVEFILE, OPTIONS);
                }
            }, true, BackupExecutionAdapter.class) {
        protected void onSelectionUpdate(AjaxRequestTarget target) {
        };
    };
    backupRestoreExecutionsTable.setOutputMarkupId(true);
    backupRestoreExecutionsTable.setFilterable(false);
    backupRestoreExecutionsTable.setSortable(false);
    form.add(backupRestoreExecutionsTable);
}

From source file:org.geoserver.backuprestore.web.BackupRestoreDataPage.java

License:Open Source License

/**
 * @param form/*  w  w w  .  j a va 2  s  .c  o m*/
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private void populateRestoreForm(Form form) {
    form.add(new CheckBox("restoreOptDryRun", new Model<Boolean>(false)));
    form.add(new CheckBox("restoreOptBestEffort", new Model<Boolean>(false)));
    form.add(statusLabel = new Label("status", new Model()).setOutputMarkupId(true));
    form.add(new AjaxSubmitLink("newRestoreStart", form) {
        @Override
        protected void disableLink(ComponentTag tag) {
            super.disableLink(tag);
            tag.setName("a");
            tag.addBehavior(AttributeModifier.replace("class", "disabled"));
        }

        protected void onError(AjaxRequestTarget target, Form<?> form) {
            target.add(feedbackPanel);
        }

        protected void onSubmit(AjaxRequestTarget target, final Form<?> form) {

            //update status to indicate we are working
            statusLabel.add(AttributeModifier.replace("class", "working-link"));
            statusLabel.setDefaultModelObject("Working");
            target.add(statusLabel);

            //enable cancel and disable this
            Component cancel = form.get("cancel");
            cancel.setEnabled(true);
            target.add(cancel);

            setEnabled(false);
            target.add(this);

            final AjaxSubmitLink self = this;

            final Long jobid;
            try {
                jobid = launchRestoreExecution(form);
            } catch (Exception e) {
                error(e);
                LOGGER.log(Level.WARNING, "Error starting a new Restore", e);
                return;
            } finally {
                //update the button back to original state
                resetButtons(form, target, "newRestoreStart");

                target.add(feedbackPanel);
            }

            cancel.setDefaultModelObject(jobid);
            this.add(new AbstractAjaxTimerBehavior(Duration.milliseconds(100)) {
                @Override
                protected void onTimer(AjaxRequestTarget target) {
                    Backup backupFacade = BackupRestoreWebUtils.backupFacade();
                    RestoreExecutionAdapter exec = backupFacade.getRestoreExecutions().get(jobid);

                    if (!exec.isRunning()) {
                        try {
                            if (exec.getAllFailureExceptions() != null
                                    && !exec.getAllFailureExceptions().isEmpty()) {
                                getSession().error(exec.getAllFailureExceptions().get(0));
                                setResponsePage(BackupRestoreDataPage.class);
                            } else if (exec.isStopping()) {
                                //do nothing
                            } else {
                                PageParameters pp = new PageParameters();
                                pp.add("id", exec.getId());
                                pp.add("clazz", RestoreExecutionAdapter.class.getSimpleName());

                                setResponsePage(BackupRestorePage.class, pp);
                            }
                        } catch (Exception e) {
                            error(e);
                            LOGGER.log(Level.WARNING, "", e);
                        } finally {
                            stop(null);

                            //update the button back to original state
                            resetButtons(form, target, "newRestoreStart");

                            target.add(feedbackPanel);
                        }
                        return;
                    }

                    String msg = exec != null ? exec.getStatus().toString() : "Working";

                    statusLabel.setDefaultModelObject(msg);
                    target.add(statusLabel);
                };

                @Override
                public boolean canCallListenerInterface(Component component, Method method) {
                    if (self.equals(component)
                            && method.getDeclaringClass()
                                    .equals(org.apache.wicket.behavior.IBehaviorListener.class)
                            && method.getName().equals("onRequest")) {
                        return true;
                    }
                    return super.canCallListenerInterface(component, method);
                }
            });
        }

        private Long launchRestoreExecution(Form<?> form) throws Exception {
            ResourceFilePanel panel = (ResourceFilePanel) newBackupRestorePanel.get("backupResource");
            Resource archiveFile = null;
            try {
                archiveFile = panel.getResource();
            } catch (NullPointerException e) {
                throw new Exception("Restore Archive File is Mandatory!");
            }

            if (archiveFile == null || !Resources.exists(archiveFile) || archiveFile.getType() == Type.DIRECTORY
                    || FilenameUtils.getExtension(archiveFile.name()).isEmpty()) {
                throw new Exception(
                        "Restore Archive File is Mandatory, must exists and should not be a Directory or URI.");
            }

            Filter filter = null;
            WorkspaceInfo ws = (WorkspaceInfo) workspace.getObject();
            if (ws != null) {
                filter = ECQL.toFilter("name = '" + ws.getName() + "'");
            }

            Hints hints = new Hints(new HashMap(2));

            Boolean restoreOptDryRun = ((CheckBox) form.get("restoreOptDryRun")).getModelObject();

            if (restoreOptDryRun) {
                hints.add(new Hints(new Hints.OptionKey(Backup.PARAM_DRY_RUN_MODE), Backup.PARAM_DRY_RUN_MODE));
            }

            Boolean restoreOptBestEffort = ((CheckBox) form.get("restoreOptBestEffort")).getModelObject();

            if (restoreOptBestEffort) {
                hints.add(new Hints(new Hints.OptionKey(Backup.PARAM_BEST_EFFORT_MODE),
                        Backup.PARAM_BEST_EFFORT_MODE));
            }

            Backup backupFacade = BackupRestoreWebUtils.backupFacade();

            return backupFacade.runRestoreAsync(archiveFile, filter, hints).getId();
        }
    });

    form.add(new AjaxLink<Long>("cancel", new Model<Long>()) {
        protected void disableLink(ComponentTag tag) {
            super.disableLink(tag);
            tag.setName("a");
            tag.addBehavior(AttributeModifier.replace("class", "disabled"));
        };

        @Override
        public void onClick(AjaxRequestTarget target) {
            Long jobid = getModelObject();

            if (jobid != null) {
                try {
                    BackupRestoreWebUtils.backupFacade().stopExecution(jobid);
                    setResponsePage(BackupRestoreDataPage.class);
                } catch (NoSuchJobExecutionException | JobExecutionNotRunningException e) {
                    LOGGER.log(Level.WARNING, "", e);
                }
            }

            setEnabled(false);

            target.add(this);
        }
    }.setOutputMarkupId(true).setEnabled(false));

    restoreExecutionsTable = new BackupRestoreExecutionsTable("restores",
            new BackupRestoreExecutionsProvider(true, RestoreExecutionAdapter.class) {
                @Override
                protected List<org.geoserver.web.wicket.GeoServerDataProvider.Property<AbstractExecutionAdapter>> getProperties() {
                    return Arrays.asList(ID, STATE, STARTED, PROGRESS, ARCHIVEFILE, OPTIONS);
                }
            }, true, RestoreExecutionAdapter.class) {
        protected void onSelectionUpdate(AjaxRequestTarget target) {
        };
    };
    restoreExecutionsTable.setOutputMarkupId(true);
    restoreExecutionsTable.setFilterable(false);
    restoreExecutionsTable.setSortable(false);
    form.add(restoreExecutionsTable);
}

From source file:org.geoserver.importer.web.ImportDataPage.java

License:Open Source License

public ImportDataPage(PageParameters params) {
    Form form = new Form("form");
    add(form);/*from  w w w  . j  a  v  a 2  s  .  c o m*/

    sourceList = new AjaxRadioPanel<Source>("sources", Arrays.asList(Source.values()), Source.SPATIAL_FILES) {
        @Override
        protected void onRadioSelect(AjaxRequestTarget target, Source newSelection) {
            updateSourcePanel(newSelection, target);
        }

        @Override
        protected AjaxRadio<Source> newRadioCell(RadioGroup<Source> group, ListItem<Source> item) {
            AjaxRadio<Source> radio = super.newRadioCell(group, item);
            if (!item.getModelObject().isAvailable()) {
                radio.setEnabled(false);
            }
            return radio;
        }

        @Override
        protected Component createLabel(String id, ListItem<Source> item) {
            return new SourceLabelPanel(id, item.getModelObject());
        }
    };

    form.add(sourceList);

    sourcePanel = new WebMarkupContainer("panel");
    sourcePanel.setOutputMarkupId(true);
    form.add(sourcePanel);

    Catalog catalog = GeoServerApplication.get().getCatalog();

    // workspace chooser
    workspace = new WorkspaceDetachableModel(catalog.getDefaultWorkspace());
    workspaceChoice = new DropDownChoice("workspace", workspace, new WorkspacesModel(),
            new WorkspaceChoiceRenderer());
    workspaceChoice.setOutputMarkupId(true);
    workspaceChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            updateTargetStore(target);
        }
    });
    workspaceChoice.setNullValid(true);
    form.add(workspaceChoice);

    WebMarkupContainer workspaceNameContainer = new WebMarkupContainer("workspaceNameContainer");
    workspaceNameContainer.setOutputMarkupId(true);
    form.add(workspaceNameContainer);

    workspaceNameTextField = new TextField("workspaceName", new Model());
    workspaceNameTextField.setOutputMarkupId(true);
    boolean defaultWorkspace = catalog.getDefaultWorkspace() != null;
    workspaceNameTextField.setVisible(!defaultWorkspace);
    workspaceNameTextField.setRequired(!defaultWorkspace);
    workspaceNameContainer.add(workspaceNameTextField);

    //store chooser
    WorkspaceInfo ws = (WorkspaceInfo) workspace.getObject();
    store = new StoreModel(ws != null ? catalog.getDefaultDataStore(ws) : null);
    storeChoice = new DropDownChoice("store", store, new EnabledStoresModel(workspace),
            new StoreChoiceRenderer()) {
        protected String getNullValidKey() {
            return ImportDataPage.class.getSimpleName() + "." + super.getNullValidKey();
        };
    };
    storeChoice.setOutputMarkupId(true);

    storeChoice.setNullValid(true);
    form.add(storeChoice);

    form.add(statusLabel = new Label("status", new Model()).setOutputMarkupId(true));
    form.add(new AjaxSubmitLink("next", form) {
        @Override
        protected void disableLink(ComponentTag tag) {
            super.disableLink(tag);
            tag.setName("a");
            tag.addBehavior(new SimpleAttributeModifier("class", "disabled"));
        }

        protected void onError(AjaxRequestTarget target, Form<?> form) {
            target.addComponent(feedbackPanel);
        }

        protected void onSubmit(AjaxRequestTarget target, final Form<?> form) {

            //update status to indicate we are working
            statusLabel.add(new SimpleAttributeModifier("class", "working-link"));
            statusLabel.setDefaultModelObject("Working");
            target.addComponent(statusLabel);

            //enable cancel and disable this
            Component cancel = form.get("cancel");
            cancel.setEnabled(true);
            target.addComponent(cancel);

            setEnabled(false);
            target.addComponent(this);

            final AjaxSubmitLink self = this;

            final Long jobid;
            try {
                jobid = createContext();
            } catch (Exception e) {
                error(e);
                LOGGER.log(Level.WARNING, "Error creating import", e);
                resetButtons(form, target);
                return;
            }

            cancel.setDefaultModelObject(jobid);
            this.add(new AbstractAjaxTimerBehavior(Duration.seconds(3)) {
                protected void onTimer(AjaxRequestTarget target) {
                    Importer importer = ImporterWebUtils.importer();
                    Task<ImportContext> t = importer.getTask(jobid);

                    if (t.isDone()) {
                        try {
                            if (t.getError() != null) {
                                error(t.getError());
                            } else if (t.isCancelled()) {
                                //do nothing
                            } else {
                                ImportContext imp = t.get();

                                //check the import for actual things to do
                                boolean proceed = !imp.getTasks().isEmpty();

                                if (proceed) {
                                    imp.setArchive(false);
                                    importer.changed(imp);

                                    PageParameters pp = new PageParameters();
                                    pp.put("id", imp.getId());

                                    setResponsePage(ImportPage.class, pp);
                                } else {
                                    info("No data to import was found");
                                    importer.delete(imp);
                                }
                            }
                        } catch (Exception e) {
                            error(e);
                            LOGGER.log(Level.WARNING, "", e);
                        } finally {
                            stop();

                            //update the button back to original state
                            resetButtons(form, target);

                            target.addComponent(feedbackPanel);
                        }
                        return;
                    }

                    ProgressMonitor m = t.getMonitor();
                    String msg = m.getTask() != null ? m.getTask().toString() : "Working";

                    statusLabel.setDefaultModelObject(msg);
                    target.addComponent(statusLabel);
                };
            });
        }
    });

    form.add(new AjaxLink<Long>("cancel", new Model<Long>()) {
        protected void disableLink(ComponentTag tag) {
            super.disableLink(tag);
            ImporterWebUtils.disableLink(tag);
        };

        @Override
        public void onClick(AjaxRequestTarget target) {
            Importer importer = ImporterWebUtils.importer();
            Long jobid = getModelObject();
            Task<ImportContext> task = importer.getTask(jobid);
            if (task != null && !task.isDone() && !task.isCancelled()) {
                task.getMonitor().setCanceled(true);
                task.cancel(false);
                try {
                    task.get();
                } catch (Exception e) {
                }
            }

            setEnabled(false);

            Component next = getParent().get("next");
            next.setEnabled(true);

            target.addComponent(this);
            target.addComponent(next);
        }
    }.setOutputMarkupId(true).setEnabled(false));

    importTable = new ImportContextTable("imports", new ImportContextProvider(true) {
        @Override
        protected List<org.geoserver.web.wicket.GeoServerDataProvider.Property<ImportContext>> getProperties() {
            return Arrays.asList(ID, STATE, UPDATED);
        }
    }, true) {
        protected void onSelectionUpdate(AjaxRequestTarget target) {
            removeImportLink.setEnabled(!getSelection().isEmpty());
            target.addComponent(removeImportLink);
        };
    };
    importTable.setOutputMarkupId(true);
    importTable.setFilterable(false);
    importTable.setSortable(false);
    form.add(importTable);

    form.add(removeImportLink = new AjaxLink("remove") {
        @Override
        public void onClick(AjaxRequestTarget target) {
            Importer importer = ImporterWebUtils.importer();
            for (ImportContext c : importTable.getSelection()) {
                try {
                    importer.delete(c);
                } catch (IOException e) {
                    LOGGER.log(Level.WARNING, "Error deleting context", c);
                }
            }
            importTable.clearSelection();
            target.addComponent(importTable);
        }
    });
    removeImportLink.setOutputMarkupId(true).setEnabled(false);

    AjaxLink jobLink = new AjaxLink("jobs") {
        @Override
        public void onClick(AjaxRequestTarget target) {
            dialog.showOkCancel(target, new DialogDelegate() {
                @Override
                protected boolean onSubmit(AjaxRequestTarget target, Component contents) {
                    return true;
                }

                @Override
                protected Component getContents(String id) {
                    return new JobQueuePanel(id);
                }
            });
        }
    };
    jobLink.setVisible(ImporterWebUtils.isDevMode());
    form.add(jobLink);

    add(dialog = new GeoServerDialog("dialog"));
    dialog.setInitialWidth(600);
    dialog.setInitialHeight(400);
    dialog.setMinimalHeight(150);

    updateSourcePanel(Source.SPATIAL_FILES, null);
    updateTargetStore(null);
}