Example usage for org.apache.wicket.markup.html.link Link isEnabled

List of usage examples for org.apache.wicket.markup.html.link Link isEnabled

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html.link Link isEnabled.

Prototype

@Override
public boolean isEnabled() 

Source Link

Usage

From source file:dk.teachus.frontend.components.list.ListPanel.java

License:Apache License

private AjaxNavigationToolbar createNavigationToolbar(DataTable<T> dataTable) {
    return new AjaxNavigationToolbar(dataTable) {
        private static final long serialVersionUID = 1L;

        @Override/*from   ww  w  .j  a  va  2s . co m*/
        protected WebComponent newNavigatorLabel(String navigatorId, final DataTable<?> table) {
            Label label = new Label(navigatorId, new AbstractReadOnlyModel<String>() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getObject() {
                    int of = table.getRowCount();
                    int from = table.getCurrentPage() * table.getItemsPerPage();
                    int to = Math.min(of, from + table.getItemsPerPage());

                    from++;

                    if (of == 0) {
                        from = 0;
                        to = 0;
                    }

                    String label = TeachUsSession.get().getString("ListPanel.navigatorLabel");
                    label = label.replace("${from}", "" + from);
                    label = label.replace("${to}", "" + to);
                    label = label.replace("${of}", "" + of);

                    return label;
                }
            });
            label.setRenderBodyOnly(true);
            return label;
        }

        @Override
        protected PagingNavigator newPagingNavigator(String navigatorId, DataTable<?> table) {
            return new AjaxPagingNavigator(navigatorId, table) {
                private static final long serialVersionUID = 1L;

                @Override
                protected void onAjaxEvent(final AjaxRequestTarget target) {
                    target.add(getTable());
                }

                @Override
                protected Link<?> newPagingNavigationLink(String id, IPageable pageable, final int pageNumber) {
                    final Link<?> pagingNavigationLink = super.newPagingNavigationLink(id, pageable,
                            pageNumber);
                    pagingNavigationLink.setBody(Model.of(""));
                    pagingNavigationLink
                            .add(AttributeModifier.append("class", new AbstractReadOnlyModel<String>() {
                                private static final long serialVersionUID = 1L;

                                @Override
                                public String getObject() {
                                    String cls = "btn btn-mini";
                                    if (pageNumber == 0) {
                                        cls += " icon-fast-backward";
                                    } else {
                                        cls += " icon-fast-forward";
                                    }
                                    if (false == pagingNavigationLink.isEnabled()) {
                                        cls += " disabled";
                                    }
                                    return cls;
                                }
                            }));
                    return pagingNavigationLink;
                }

                @Override
                protected Link<?> newPagingNavigationIncrementLink(String id, IPageable pageable,
                        final int increment) {
                    final Link<?> pagingNavigationIncrementLink = super.newPagingNavigationIncrementLink(id,
                            pageable, increment);
                    pagingNavigationIncrementLink.setBody(Model.of(""));
                    pagingNavigationIncrementLink
                            .add(AttributeModifier.append("class", new AbstractReadOnlyModel<String>() {
                                private static final long serialVersionUID = 1L;

                                @Override
                                public String getObject() {
                                    String cls = "btn btn-mini";
                                    if (increment < 0) {
                                        cls += " icon-backward";
                                    } else {
                                        cls += " icon-forward";
                                    }
                                    if (false == pagingNavigationIncrementLink.isEnabled()) {
                                        cls += " disabled";
                                    }
                                    return cls;
                                }
                            }));
                    return pagingNavigationIncrementLink;
                }

                @Override
                protected PagingNavigation newNavigation(String id, IPageable pageable,
                        IPagingLabelProvider labelProvider) {
                    return new AjaxPagingNavigation(id, pageable, labelProvider) {
                        private static final long serialVersionUID = 1L;

                        @Override
                        protected Link<?> newPagingNavigationLink(String id, IPageable pageable,
                                int pageIndex) {
                            final Link<?> pagingNavigationLink = super.newPagingNavigationLink(id, pageable,
                                    pageIndex);
                            pagingNavigationLink
                                    .add(AttributeModifier.append("class", new AbstractReadOnlyModel<String>() {
                                        private static final long serialVersionUID = 1L;

                                        @Override
                                        public String getObject() {
                                            StringBuilder cls = new StringBuilder();
                                            cls.append("btn btn-mini");
                                            if (false == pagingNavigationLink.isEnabled()) {
                                                cls.append(" btn-primary disabled");
                                            }
                                            return cls.toString();
                                        }
                                    }));
                            return pagingNavigationLink;
                        }
                    };
                }
            };
        }
    };
}

From source file:org.brixcms.plugin.snapshot.ManageSnapshotsPanel.java

License:Apache License

public ManageSnapshotsPanel(String id, final IModel<Workspace> model) {
    super(id, model);

    add(new FeedbackPanel("feedback"));

    IModel<List<Workspace>> snapshotsModel = new LoadableDetachableModel<List<Workspace>>() {
        @Override/*w  w  w .  j a va  2  s .co m*/
        protected List<Workspace> load() {
            List<Workspace> list = SnapshotPlugin.get().getSnapshotsForWorkspace(getModelObject());
            return getBrix().filterVisibleWorkspaces(list, Context.ADMINISTRATION);
        }
    };

    add(new ListView<Workspace>("snapshots", snapshotsModel) {
        @Override
        protected IModel<Workspace> getListItemModel(IModel<? extends List<Workspace>> listViewModel,
                int index) {
            return new WorkspaceModel(listViewModel.getObject().get(index));
        }

        @Override
        protected void populateItem(final ListItem<Workspace> item) {
            Workspace workspace = item.getModelObject();
            final String name = SnapshotPlugin.get().getUserVisibleName(workspace, true);
            final String comment = SnapshotPlugin.get().getComment(workspace);

            Link<Object> link = new Link<Object>("browse") {
                @Override
                public void onClick() {
                    Workspace workspace = item.getModelObject();
                    model.setObject(workspace);
                }
            };
            item.add(link);

            Link restoreLink = new Link<Void>("restore") {
                @Override
                public void onClick() {
                    Workspace target = ManageSnapshotsPanel.this.getModelObject();
                    SnapshotPlugin.get().restoreSnapshot(item.getModelObject(), target);
                    getSession().info(ManageSnapshotsPanel.this.getString("restoreSuccessful"));
                }

                /**
                 * Take care that restoring is only allowed in case the workspaces aren't the same
                 */
                @Override
                public boolean isEnabled() {
                    if (item.getModelObject().getId()
                            .equals(ManageSnapshotsPanel.this.getModelObject().getId())) {
                        return false;
                    }
                    return true;
                }

                @Override
                public boolean isVisible() {
                    Workspace target = ManageSnapshotsPanel.this.getModelObject();
                    Action action = new RestoreSnapshotAction(Context.ADMINISTRATION, item.getModelObject(),
                            target);
                    return getBrix().getAuthorizationStrategy().isActionAuthorized(action);
                }
            };

            /*
             * in case the link is enabled, make sure it is intended...
             */
            if (restoreLink.isEnabled()) {
                restoreLink.add(new SimpleAttributeModifier("onClick",
                        "return confirm('" + getLocalizer().getString("restoreOnClick", this) + "')"));
            }

            item.add(restoreLink);

            item.add(new Link<Void>("delete") {
                @Override
                public void onClick() {
                    Workspace snapshot = item.getModelObject();
                    snapshot.delete();
                }

                @Override
                public boolean isVisible() {
                    Action action = new DeleteSnapshotAction(Context.ADMINISTRATION, item.getModelObject());
                    return getBrix().getAuthorizationStrategy().isActionAuthorized(action);
                }
            });

            item.add(new Label("label", name));

            item.add(new Label("commentlabel", comment));
        }
    });

    add(new Link<Object>("downloadWorkspace") {
        @Override
        public void onClick() {
            getRequestCycle().scheduleRequestHandlerAfterCurrent(new IRequestHandler() {
                public void detach(IRequestCycle requestCycle) {
                }

                public void respond(IRequestCycle requestCycle) {
                    WebResponse resp = (WebResponse) requestCycle.getResponse();
                    resp.setAttachmentHeader("workspace.xml");
                    String id = ManageSnapshotsPanel.this.getModelObject().getId();
                    Brix brix = getBrix();
                    JcrSession session = brix.getCurrentSession(id);
                    HttpServletResponse containerResponse = (HttpServletResponse) resp.getContainerResponse();
                    ServletOutputStream containerResponseOutputStream = null;
                    try {
                        containerResponseOutputStream = containerResponse.getOutputStream();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    session.exportSystemView(brix.getRootPath(), containerResponseOutputStream, false, false);
                }
            });
        }
    });

    /**
     * Form to create a new Snapshot and put any comment to it
     */
    Form<Object> commentForm = new Form<Object>("commentForm") {
        @Override
        public boolean isVisible() {
            Workspace target = ManageSnapshotsPanel.this.getModelObject();
            Action action = new CreateSnapshotAction(Context.ADMINISTRATION, target);
            return getBrix().getAuthorizationStrategy().isActionAuthorized(action);
        }
    };

    final TextArea<String> area = new TextArea<String>("area", new Model<String>());
    commentForm.add(area);

    commentForm.add(new SubmitLink("createSnapshot") {
        /**
         * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onSubmit()
         */
        @Override
        public void onSubmit() {
            String comment = area.getModelObject();
            SnapshotPlugin.get().createSnapshot(ManageSnapshotsPanel.this.getModelObject(), comment);
            area.setModelObject("");
        }
    });
    add(commentForm);

    Form<Object> uploadForm = new Form<Object>("uploadForm") {
        @Override
        public boolean isVisible() {
            Workspace target = ManageSnapshotsPanel.this.getModelObject();
            Action action = new RestoreSnapshotAction(Context.ADMINISTRATION, target);
            return getBrix().getAuthorizationStrategy().isActionAuthorized(action);
        }
    };

    final FileUploadField upload = new FileUploadField("upload", new Model<FileUpload>());
    uploadForm.add(upload);

    uploadForm.add(new SubmitLink("submit") {
        @Override
        public void onSubmit() {
            List<FileUpload> uploadList = upload.getModelObject();
            if (uploadList != null) {
                for (FileUpload u : uploadList) {
                    try {
                        InputStream s = u.getInputStream();
                        String id = ManageSnapshotsPanel.this.getModelObject().getId();
                        Brix brix = getBrix();
                        JcrSession session = brix.getCurrentSession(id);

                        if (session.itemExists(brix.getRootPath())) {
                            session.getItem(brix.getRootPath()).remove();
                        }
                        session.importXML("/", s, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
                        session.save();

                        brix.initWorkspace(ManageSnapshotsPanel.this.getModelObject(), session);

                        getSession().info(ManageSnapshotsPanel.this.getString("restoreSuccessful"));
                    } catch (IOException e) {
                        throw new BrixException(e);
                    }
                }
            }
        }
    });

    add(uploadForm);
}