Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.wicketopia.persistence.component.scaffold;
  
 
 import java.util.List;

Author(s):
James Carman
 
 public class Scaffold<T> extends Panel implements IHeaderContributor
 {
 //----------------------------------------------------------------------------------------------------------------------
 // Fields
 //----------------------------------------------------------------------------------------------------------------------
 
     private static final ResourceReference CSS_REFERENCE = new ResourceReference(Scaffold.class"scaffold.css");
     private static final String CONTENT_ID = "content";
     private final Class<T> beanType;
     private final PersistenceProvider persistenceProvider;
     private ScaffoldMode mode = .;
     private final FeedbackPanel feedback = new FeedbackPanel("feedback");
     private IModel<T> model;
     private final DisplayNameModel displayName;
 
 //----------------------------------------------------------------------------------------------------------------------
 // Constructors
 //----------------------------------------------------------------------------------------------------------------------
 
     public Scaffold(String idClass<T> beanTypePersistenceProvider persistenceProvider)
     {
         super(id);
         WicketopiaBeanFacet beanFacet = WicketopiaBeanFacet.get(WicketopiaPlugin.get().getBeanMetaData(beanType));
          = new DisplayNameModel(beanFacet);
         add(new AttributeModifier("class"truenew Model<String>("scaffold")));
         .setOutputMarkupPlaceholderTag(true);
         .setFilter(new ContainerFeedbackMessageFilter(this));
         add();
         this. = beanType;
         this. = persistenceProvider;
         refreshContent(null);
         setOutputMarkupPlaceholderTag(true);
 
     }
 
     private void refreshContent(AjaxRequestTarget target)
     {
         final Component content = createContent();
         content.setOutputMarkupPlaceholderTag(true);
         addOrReplace(content);
         if (target != null)
         {
             target.addComponent(this);
         }
     }
 
     private Component createContent()
     {
         switch ()
         {
             case :
                 return new ListFragment();
             case :
                return new ViewFragment();
            case :
                return new EditFragment();
            case :
                return new CreateFragment();
        }
        return new EmptyPanel();
    }
//----------------------------------------------------------------------------------------------------------------------
// IHeaderContributor Implementation
//----------------------------------------------------------------------------------------------------------------------
    @Override
    public void renderHead(IHeaderResponse response)
    {
        response.renderCSSReference();
    }
//----------------------------------------------------------------------------------------------------------------------
// Other Methods
//----------------------------------------------------------------------------------------------------------------------
    @Override
    protected void onDetach()
    {
        super.onDetach();
        .detach();
    }
//----------------------------------------------------------------------------------------------------------------------
// Inner Classes
//----------------------------------------------------------------------------------------------------------------------
    private class ActionsColumn extends FragmentColumn<T>
    {
        private ActionsColumn()
        {
            super(new Model<String>("Actions"));
        }
        @Override
        protected Fragment createFragment(String componentIdIModel<T> rowModel)
        {
            Fragment f = new Fragment(componentId"actions"Scaffold.this);
            f.add(new ViewLink("viewLink"rowModel));
            f.add(new EditLink("updateLink"rowModel));
            f.add(new DeleteLink("deleteLink"rowModel));
            return f;
        }
    }
    private class CancelLink extends AjaxLink<Void>
    {
        private CancelLink(String id)
        {
            super(id);
        }
        @Override
        public void onClick(AjaxRequestTarget target)
        {
             = .;
            refreshContent(target);
        }
    }
    private class ConfirmBehavior extends AbstractBehavior
    {
        private final String event;
        private final IModel<Stringmessage;
        private ConfirmBehavior(String eventIModel<Stringmessage)
        {
            this. = event;
            this. = message;
        }
        @Override
        public void detach(Component component)
        {
            super.detach(component);
            .detach();
        }
        public void onComponentTag(Component componentComponentTag tag)
        {
            StringBuilder handler = new StringBuilder(128);
            handler.append("if (!confirm('");
            handler.append(.getObject().replaceAll("'""\\\\'"));
            handler.append("')) {return false;} ");
            String script = tag.getAttributes().getString();
            if (script != null)
            {
                handler.append(script);
            }
            tag.put(.toString(), handler.toString());
        }
    }
    private class CreateFragment extends Fragment
    {
        private CreateFragment()
        {
            super("create"Scaffold.this);
             = new CreateModel();
            final ListLink listLink = new ListLink("listButton");
            add(listLink.add(new Label("nameList").setRenderBodyOnly(true)));
            add(new Label("nameCaption").setRenderBodyOnly(true));
            final Form<T> form = new Form<T>("form");
            final PropertyComponentFactory<T> editorFactory = WicketopiaPlugin.get().createEditorFactory();
            final Context context = new Context(.);
            form.add(new CssBeanViewLayoutPanel<T>("layout"contexteditorFactory));
            add(new AjaxCreateLink<T>("saveButton"form)
            {
                @Override
                protected void afterCreate(T objectAjaxRequestTarget target)
                {
                     = .;
                    Scaffold.this.info(.getObject() + " Created");
                     = new LoadableDetachableEntityModel<T>(object);
                    refreshContent(target);
                }
                @Override
                protected void onError(AjaxRequestTarget targetForm<?> form)
                {
                    target.addComponent();
                }
            });
            add(form);
        }
    }
    private class CreateLink extends AjaxLink<Void>
    {
        private CreateLink(String id)
        {
            super(id);
        }
        @Override
        public void onClick(AjaxRequestTarget target)
        {
             = .;
            refreshContent(target);
        }
    }
    private class CreateModel extends LoadableDetachableModel<T>
    {
        @Override
        protected T load()
        {
            try
            {
                return .newInstance();
            }
            catch (Exception e)
            {
                throw new WicketRuntimeException("Unable to instantiate " + .getName() + " object (" + e.getMessage() + ").");
            }
        }
    }
    private class DeleteLink extends AjaxLink<T>
    {
        private DeleteLink(String idIModel<T> tiModel)
        {
            super(idtiModel);
            add(new ConfirmBehavior("onclick"new Model<String>("Are you sure?")));
        }
        @Override
        public void onClick(AjaxRequestTarget target)
        {
            .delete(getModelObject());
            Scaffold.this.info(.getObject() + " Deleted");
             = .;
            refreshContent(target);
        }
    }
    private class EditFragment extends Fragment
    {
        private EditFragment()
        {
            super("edit"Scaffold.this);
            WicketopiaBeanFacet beanFacet = WicketopiaBeanFacet.get(WicketopiaPlugin.get().getBeanMetaData());
            add(new Label("nameCaption").setRenderBodyOnly(true));
            add(new DeleteLink("deleteButton"));
            add(new ListLink("listButton").add(new Label("nameList").setRenderBodyOnly(true)));
            add(new CreateLink("createButton").add(new Label("nameCreate").setRenderBodyOnly(true)));
            final Form<T> form = new Form<T>("form");
            final PropertyComponentFactory<T> editorFactory = WicketopiaPlugin.get().createEditorFactory();
            final Context context = new Context(.);
            form.add(new CssBeanViewLayoutPanel<T>("layout"contexteditorFactory));
            add(form);
            add(new AjaxUpdateLink<T>("saveButton"form)
            {
                @Override
                protected void afterUpdate(T objectAjaxRequestTarget target)
                {
                     = .;
                    Scaffold.this.info(.getObject() + " Updated");
                    refreshContent(target);
                }
                @Override
                protected void onError(AjaxRequestTarget targetForm<?> form)
                {
                    target.addComponent();
                }
            });
        }
    }
    private class EditLink extends AjaxLink<T>
    {
        private EditLink(String idIModel<T> model)
        {
            super(idmodel);
        }
        @Override
        public void onClick(AjaxRequestTarget target)
        {
             = getModel();
             = .;
            refreshContent(target);
        }
    }
    private class ListFragment extends Fragment
    {
        private ListFragment()
        {
            super("list"Scaffold.this);
            final CreateLink create = new CreateLink("newEntity");
            add(create);
            add(new Label("pluralName"new PluralizedModel()).setRenderBodyOnly(true));
            create.add(new Label("displayName").setRenderBodyOnly(true));
            final PropertyComponentFactory<T> viewerFactory = WicketopiaPlugin.get().createViewerFactory();
            final Context context = new Context(.);
            final List<IColumn<T>> columns = WicketopiaPlugin.get().createColumns(viewerFactorycontext);
            columns.add(new ActionsColumn());
            add(new AjaxFallbackDefaultDataTable<T>("table"columnsnew PersistenceDataProvider<T>(), 25));
        }
    }
    private class ListLink extends AjaxLink<Void>
    {
        private ListLink(String id)
        {
            super(id);
        }
        @Override
        public void onClick(AjaxRequestTarget target)
        {
             = .;
            refreshContent(target);
        }
    }
    private class ViewFragment extends Fragment
    {
        private ViewFragment()
        {
            super("view"Scaffold.this);
            WicketopiaBeanFacet beanFacet = WicketopiaBeanFacet.get(WicketopiaPlugin.get().getBeanMetaData());
            add(new Label("nameCaption").setRenderBodyOnly(true));
            add(new EditLink("editButton"));
            add(new DeleteLink("deleteButton"));
            add(new ListLink("listButton").add(new Label("nameList").setRenderBodyOnly(true)));
            add(new CreateLink("createButton").add(new Label("nameCreate").setRenderBodyOnly(true)));
            final PropertyComponentFactory<T> factory = WicketopiaPlugin.get().createViewerFactory();
            final Context context = new Context(.);
            add(new CssBeanViewLayoutPanel<T>("layout"contextfactory));
        }
    }
    private class ViewLink extends AjaxLink<T>
    {
        private ViewLink(String idIModel<T> tiModel)
        {
            super(idtiModel);
        }
        @Override
        public void onClick(AjaxRequestTarget target)
        {
             = getModel();
             = .;
            refreshContent(target);
        }
    }