I am trying to make use of Editor in a GWT application, so I have read the official documentation. I have also looked at he question enter link description here and its answers, but I still cannot figure out the ultimate "purpose" of Editors. As an example case, supposing a UiBinder with some fields:
#UiField
TextBox name;
#UiField
TextArea comment;
#UiField
ListBox type;
...
I create additionally a method editCustomer as:
private void editCustomer(CustomerProxy entity) {
MyRequestFactory requestFactory = MyRequest.getRequestFactory();
CustomerRequestContext requestContext = requestFactory.customerRequest();
entity = requestContext.edit(entity);
editorDriver.edit(entity, requestContext);
}
I think the approach with Editor makes for connecting UiBinder fields with the Database. How is this done, based on the common way of sending data in the database through a "Save" Buttton?
#UiHandler("saveButton")
void onSaveButtonClick(ClickEvent event){
????
}
I have been using the MVP pattern for a while and have some more complicated editors. I found that it is good put put your EditorDriver in your view becuase when you initialize it you can bind it to your specific view. My examples require an activity / view interface / view implementation.
This is an abstract activity that can be extended by other activities but I included the relevant content. I have stripped quite a bit of code out but this should give you an idea of a useful way to use editors. My editor is quite complex and has quite a few sub editors. I have only included the name and description. We have found this to be a quite useful design pattern for handling Editors.
public abstract class AbstractTaskBuilderActivity extends <E extends AnalyticsTaskProxy, R extends DaoRequest<E>> implements TaskBuilderView {
/**
* Create a new task. This will initialize any new lists.
* #param context The RequestContext to use to create the task.
* #param clazz The class type to be created.
* #return
*/
protected E create(R context, Class<E> clazz) {
// This is implemented in my inherited classes.
E editableAnalyticsTask = context.create(clazz);
// More initialization code expecially initializing arrays to empty so that
// any ListEditor sub editors will work.
return editableAnalyticsTask;
}
/**
* Call to edit the task and update the dashboards.
* #param context
* #param task
*/
protected void doEdit(R context, E task) {
RequestFactoryEditorDriver<? super AnalyticsTaskProxy, ?> driver = display.getEditorDriver();
E editable = context.edit(task);
context.save(editable).with(driver.getPaths()).to(new Receiver<Long>() {
#Override
public void onFailure(ServerFailure error) {
display.showError(error.getMessage());
super.onFailure(error);
}
public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
display.getEditorDriver().setConstraintViolations(violations);
}
#Override
public void onSuccess(Long response) {
clientFactory.getPlaceController().goTo(getSavePlace());
}
});
driver.edit(editable, context);
}
/**
* Save the task.
*/
#Override
public void onSaveTask() {
RequestFactoryEditorDriver<? super AnalyticsTaskProxy, ?> driver = display.getEditorDriver();
RequestContext request = driver.flush();
request.fire();
}
}
My view interface
public interface TaskBuilderView extends View {
public interface Presenter {
void onSaveTask();
}
public RequestFactoryEditorDriver<AnalyticsTaskProxy, ?> getFactoryEditorDriver();
public void setPresenter(Presenter presenter);
}
My view implementation
public class AnalyticsTaskBuilderViewImpl extends ViewImpl implements AnalyticsTaskBuilderView, Editor<AnalyticsTaskProxy> {
interface AnalyticsTaskBuilderDriver extends RequestFactoryEditorDriver<AnalyticsTaskProxy, AnalyticsTaskBuilderViewImpl> {
}
/**
* UiBinder implementation.
*
* #author chinshaw
*
*/
#UiTemplate("AnalyticsTaskBuilderView.ui.xml")
interface Binder extends UiBinder<Widget, AnalyticsTaskBuilderViewImpl> {
}
/**
* Name component for the name of the analytics operation.
* This also implements {#link HasEditorErrors so it can show
* constraint violations when an error occurs.
*/
#UiField
ValueBoxEditorDecorator<String> name;
/**
* Description component that edits analytics operation description.
* This also implements {#link HasEditorErrors} so it can show
* constraint violations when an error occurs
*/
#UiField
ValueBoxEditorDecorator<String> description;
public AnalyticsTaskBuilderViewImpl(Resources resources) {
super(resources);
// Must initialize the view before calling driver initialize
initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this));
driver.initialize(this);
}
#Override
public void setPresenter(Presenter presenter) {
this.presenter = presenter;
bindToPresenter();
}
// Save the task
#UiHandler("saveTask")
void handleClick(ClickEvent clickEvent) {
presenter.onSaveTask();
}
#Override
public RequestFactoryEditorDriver<AnalyticsTaskProxy, ?> getEditorDriver() {
return driver;
}
}
Related
i have problem, i would to add prototype array to database but this show me this error:
Expected argument of type "AppBundle\Entity\Tag", "array" given
...
Post ->setTag (array(array('value' => 'test'), array('value' => 'tess')))
here is my setter for tag:
public function setTag(\AppBundle\Entity\Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
I Have two entities with relation, here relation:
class Post
{
/**
* #ORM\ManyToMany(targetEntity="Tag", inversedBy="post")
* #ORM\JoinColumn(name="tag_id", referencedColumnName="id")
*/
private $tag;
public function setTag(\AppBundle\Entity\Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
}
and tag:
class Tag
{
/**
* #ORM\ManyToMany(targetEntity="Post", mappedBy="tag")
*/
private $post;
}
Source:
http://snipet.co.uk/kR
http://snipet.co.uk/gcf
http://snipet.co.uk/0VI
You're trying to model a bidirectional many-to-many relation between Post and Tag.
So, first of all, your getters need to return a collection of objects, and your setters need to accept a collection of objects - not only one single object as in your code (your setTag method accepts a parameter of type Tag - but you need an array-like parameter).
Secondly, the Doctrine framework does not work with simple PHP arrays, but with implementations of \Doctrine\Common\Collections\Collection.
Next, you need to initialize your collection fields in the constructors of your entity classes with an implementation of the Collection class - you can use \Doctrine\Common\Collections\ArrayCollection.
So your entity classes should look rather like this:
/**
* #ORM\Entity
*/
class Post
{
/**
* #ORM\ManyToMany(targetEntity="Tag", inversedBy="posts")
* #ORM\JoinTable(name="posts_tags")
*/
private $tags;
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getTags()
{
return $this->tags;
}
public function setTags(\Doctrine\Common\Collections\Collection $tags)
{
$this->tags = $tags;
}
}
/**
* #ORM\Entity
*/
class Tag
{
/**
* #ORM\ManyToMany(targetEntity="Post", mappedBy="tags")
*/
private $posts;
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getPosts()
{
return $this->posts;
}
public function setPosts(\Doctrine\Common\Collections\Collection $posts)
{
$this->posts = $posts;
}
}
I strongly advise you to read once again the documentation of the Doctrine framework, how to annotate your entities, and how to model relations: http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html
I am building an Android POS application using codenameone. I want to use the CMSoft BT-Printer SDK from here http://www.cm-soft.com/AndroidPrinterSDK.htm. This uses an AIDL interface. How would I access it from Codenameone project?
1)Create in your project a regular interface that extends NativeInterface to communicate with the printer service.
2)
interface PrinterInterface extends NativeInterface{
public void bindService();
public void startScan();
public void stopScan();
}
3)right click on the interface and select "Generate Native Access" - this will create implementation files under the native directory in the project.
4)under the native/android dir you will get a PrinterInterfaceImpl class make sure the isSupported() method returns true and now simply implement your android code in this class.
use AndroidNativeUtil.getActivity() to gain access to your Activity.
for example:
AndroidNativeUtil.getActivity().registerReceiver(mReceiver, new IntentFilter(RECEIVER));
AndroidNativeUtil.getActivity().unregisterReceiver(mReceiver);
5)in the impl class you can bind your receiver:
final class ScannerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String data = null;
if (intent.getAction().equals(RECEIVER)) {
data = intent.getStringExtra(DATA);
}
if (data != null) {
String msg;
if (data.startsWith("S:")) {
msg = data.substring(data.indexOf(':', 2) + 1);
}
if (data.startsWith("D:")) {
msg = data.substring(data.indexOf(':', 2) + 1);
}
}
}
}
private final ScannerReceiver mReceiver = new ScannerReceiver();
private final Intent mService = new Intent(SERVICE);
I'm using (and new to) RequestFactory in GWT 2.5, with JDO entities with a one-to-many relationship, on AppEngine datastore. I've just started using the GWT RequestFactoryEditorDriver to display/edit my objects.
The Driver traverses my objects fine, and displays them correctly. However, when I try to edit a value on the "related" objects, the change doesn't get persisted to the datastore.
When I change b.name on my UI and click "save", I notice only A's persist() call is called. B's persist() is never called. How do I make the editorDriver fire on both ARequest as well as BRequest request contexts? (since what I want is for B's InstanceRequest<AProxy,Void> persist() to be called when my edits are to B objects only.)
Also, AFAICT, if I have an editor on BProxy, any object b that is being shown by the editor (and following the Editor Contract) should automatically be "context.edit(b)"ed by the Driver to make it mutable. However, in my case "context" is an ARequest, not a BRequest.
Do I have to make a ValueAwareEditor like mentioned here: GWT Editor framework
and create a fresh BRequest inside the flush() call and fire it, so that changes to B separately persist in a BRequest before the ARequest is fired?
editorDriver.getPaths() gives me:
"bs"
Also, the driver definitely sees the change to B's property, as editorDriver.isChanged() returns true before I fire() the context.
There are no errors on my client-side or server-side logs, and the Annotation Processor runs with no warnings.
Here's how I setup my driver:
editorDriver = GWT.create(Driver.class);
editorDriver.initialize(rf, view.getAEditor());
final ARequest aRequest = rf.ARequest();
final Request<List<AProxy>> aRequest = aRequest.findAByUser(loginInfo.getUserId());
String[] paths = editorDriver.getPaths();
aRequest.with(paths).fire(new Receiver<List<AProxy>>() {
#Override
public void onSuccess(List<AProxy> response) {
AProxy a = response.get(0);
ARequest aRequest2 = rf.aRequest();
editorDriver.edit(a, aRequest2);
aRequest2.persist().using(a);
}
});
This is how my entities look:
public abstract class PersistentEntity {
public Void persist() {
PersistenceManager pm = getPersistenceManager();
try {
pm.makePersistent(this);
} finally {
pm.close();
}
return null;
}
public Void remove() {
PersistenceManager pm = getPersistenceManager();
try {
pm.deletePersistent(this);
} finally {
pm.close();
}
return null;
}
}
#PersistenceCapable(identityType = IdentityType.APPLICATION)
#Version(strategy=VersionStrategy.VERSION_NUMBER, column="VERSION",
extensions={#Extension(vendorName="datanucleus", key="field-name", value="version")})
public class A extends PersistentEntity {
... (Id, version omitted for brevity)
#Persistent
private String name;
#Persistent
private List<B> bs;
public String getName() {
return name;
}
...
public void setName(String name) {
this.name = name;
}
public List<B> getBs() {
return bs;
}
public void setBs(List<B> bs) {
this.bs = bs;
}
}
... (same annotations as above omitted for brevity)
public class B extends PersistentEntity {
... (Id, version omitted for brevity)
#Persistent
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here are the proxies:
#ProxyFor(A.class)
public interface AProxy extends EntityProxy {
String getName();
List<BProxy> getBs();
void setName(String name);
void setBs(List<BProxy> bs);
}
#ProxyFor(B.class)
public interface BProxy extends EntityProxy {
String getName();
void setName(String name);
}
Here are my service stubs:
#Service(A.class)
public interface ARequest extends RequestContext {
Request<List<A>> findAByUser(String userId);
InstanceRequest<AProxy, Void> persist();
InstanceRequest<AProxy, Void> remove();
}
#Service(B.class)
public interface BRequest extends RequestContext {
Request<List<A>> findB(String key);
InstanceRequest<BProxy, Void> persist();
InstanceRequest<BProxy, Void> remove();
}
Edit:
I've now changed my ARequest interface and service implementation to support a "saveAndReturn" method, so that I can recursively "persist" "a" on the server side:
Request<UserSandboxProxy> saveAndReturn(AProxy aProxy);
I find now that when I "flush" my RequestFactoryEditorDriver, the client-side context object has my new "b.name" value. However, if I call "context.fire()" and inspect my "saveAndReturn" method on the server side, the resulting server-side object "a", just before I "persist" it, doesn't contain the change to "b.name" on any item of the List.
Why could this be happening? How do I debug why this client-information doesn't go across the wire, to the server?
Options I've considered, tried and ruled out:
1) Ensuring the APT has been run, and there are no warnings/errors on Proxy or Service interfaces
2) Ensuring that my proxies does have a valid setter in AProxy for the List
You have to use a session-per-request pattern for RequestFactory to work properly. More details here: https://code.google.com/p/google-web-toolkit/issues/detail?id=7827
I have a whole database shown in a JTable and I added a print button but what it does, is to convert the data in the JTable into a .csv file, commands excel to open it and the user can print it, but it looks pretty ugly. Is there a way to send a JTable component to printer?
There is a method under JTable called print(). Will save you alot. See below :-
package com.tanyasis.librarymanager;
import java.awt.HeadlessException;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import javax.swing.JTable;
/**
* Used to provide printing information and adding information that might be
* important in a page such as page header, contents and footer. This class
* makes sure that all contents of a table fit in the given page.
*
* #author Tanyasis Mwanik
*
*/
public class PrintTable {
private JTable table;
private MessageFormat headerFormat, footerFormat;
/**
* Prints the table and provide post printing information to the user if it
* was succesful
*
* #param table
* <code>JTable</code> to be printed
* #param tableTitle
* <code>String</code> to be used as the table header/title
*/
public PrintTable(JTable table, String tableTitle) {
// TODO Auto-generated constructor stub
this.setTable(table);
// Sets the table header
headerFormat = new MessageFormat(tableTitle);
// Sets the table footer
footerFormat = new MessageFormat("Page {0}");
try {
boolean complete = table.print(JTable.PrintMode.FIT_WIDTH,
headerFormat, footerFormat, true, null, true, null);
if (complete) {
new ConfirmationClass(
"<html><h2>Records Printed Successfully</h2></html>");
}
} catch (HeadlessException | PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* #return the table
*/
public JTable getTable() {
return table;
}
/**
* #param table
* the table to set
*/
public void setTable(JTable table) {
this.table = table;
}
}
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
MessageFormat header=new MessageFormat("Your Invoice");
MessageFormat footer=new MessageFormat("Page");
try
{
table.print(JTable.PrintMode.FIT_WIDTH, header, footer);
}
catch(Exception ae)
{
System.err.println("Error printing: " + ae.getMessage());
}
}
});
It might help :)
I posted a question the other day and didn't get much response. I have tried a few things but still no luck. I am trying to nest a tab presenter in GWTP. I have one frame (North) that is tab group 1 and one frame (West) that is tab group 2. When I interact with group 1 I update the nested tab presenter which controls group 2. I have looked at the example for nested tab presenters but I have been unable to determine my error from it.
The two TabContainerPresenters are ManagementTabsPresenter and SettingsTabsPresenter. The children of these are like the HomePresenter below. The content for these children is displayed in the center of a DockLayoutPanel located in the MainPreseter. I want the ManagementTabsPresenter and the SettingsTabsPresenter to be displayed in the West slot of the same DockLayoutPanel.
When I run this code everything is placed on the screen in the correct locations. You can even click the tabs and have actions occur. The problems start in that the SettingsPresenter is displayed first even though the default page is the HomePresenter. It's like it tries to reveal both at the same time on start up. Then when you selected any of the tabs which are part of the ManagementTabsPresenter or the SettingsTabsPresenter It opens the correct content in the MainPresenter.Center_Slot but the incorrect presenter is displayed in the MainPresenter.West_Slot. I can't figure out what's going wrong here. Any help would be appreciated.
Here is the code:
public class HomePresenter extends Presenter<HomePresenter.MyView, HomePresenter.MyProxy> {
#Inject
AppPlaceManager appPlaceManager;
#NameToken(NameTokens.homePage)
#ProxyStandard
#NoGatekeeper
public interface MyProxy extends TabContentProxyPlace<HomePresenter> {
}
public interface MyView extends View {
}
#TabInfo(container = ManagementTabsPresenter.class)
static TabData getTabLabel(MainAppGinjector injector) {
return new TabDataBasic("home", 0);
}
#Inject
public HomePresenter(final EventBus eventBus, final MyView view, final MyProxy proxy) {
super(eventBus, view, proxy);
}
#Override
protected void revealInParent() {
RevealContentEvent.fire(this, ManagementTabsPresenter.TYPE_MAIN_CONTENT_SLOT, this);
}
}
public class ManagementTabsPresenter extends TabContainerPresenter<ManagementTabsPresenter.MyView, ManagementTabsPresenter.MyProxy> {
/**
* {#link ManagementTabsPresenter}'s proxy.
*/
#ProxyStandard
public interface MyProxy extends NonLeafTabContentProxy<ManagementTabsPresenter> {
}
/**
* {#link ManagementTabsPresenter}'s view.
*/
public interface MyView extends TabView {
}
/**
* Use this in leaf presenters, inside their {#link #revealInParent} method.
*/
#ContentSlot
public static final GwtEvent.Type<RevealContentHandler<?>> TYPE_MAIN_CONTENT_SLOT = MainPresenter.CENTER_SLOT;
/**
* This will be the event sent to our "unknown" child presenters, in order for
* them to register their tabs.
*/
#RequestTabs
public static final Type<RequestTabsHandler> TYPE_Management_RequestTabs = new Type<RequestTabsHandler>();
#TabInfo(container = HeaderTabsPresenter.class, nameToken = NameTokens.homePage)
static TabData getTabLabel(MainAppGinjector injector) {
return new TabDataBasic("Management", 0);
}
#Inject
public ManagementTabsPresenter(final EventBus eventBus, final MyView view,
final MyProxy proxy, AppPlaceManager appPlaceManager) {
super(eventBus, view, proxy,TYPE_MAIN_CONTENT_SLOT, TYPE_Management_RequestTabs);
}
#Override
protected void revealInParent() {
RevealContentEvent.fire(this, HeaderTabsPresenter.TYPE_VERTICAL_TABS_SLOT, this);
}
}
public class SettingsTabsPresenter extends TabContainerPresenter<SettingsTabsPresenter.MyView, SettingsTabsPresenter.MyProxy> {
/**
* {#link SettingsTabsPresenter}'s proxy.
*/
#ProxyStandard
public interface MyProxy extends NonLeafTabContentProxy<SettingsTabsPresenter> {
}
/**
* {#link SettingsTabsPresenter}'s view.
*/
public interface MyView extends TabView {
}
#TabInfo(container = HeaderTabsPresenter.class, nameToken = NameTokens.appUserCollectionPage)
static TabData getTabLabel(MainAppGinjector injector) {
return new TabDataBasic("Settings", 1);
}
/**
* Use this in leaf presenters, inside their {#link #revealInParent} method.
*/
#ContentSlot
public static final GwtEvent.Type<RevealContentHandler<?>> TYPE_MAIN_CONTENT_SLOT = MainPresenter.CENTER_SLOT;
/**
* This will be the event sent to our "unknown" child presenters, in order for
* them to register their tabs.
*/
#RequestTabs
public static final Type<RequestTabsHandler> TYPE_Settings_RequestTabs = new Type<RequestTabsHandler>();
#Inject
public SettingsTabsPresenter(final EventBus eventBus, final MyView view,
final MyProxy proxy, AppPlaceManager appPlaceManager) {
super(eventBus, view, proxy,TYPE_MAIN_CONTENT_SLOT, TYPE_Settings_RequestTabs);
}
#Override
protected void revealInParent() {
RevealContentEvent.fire(this, HeaderTabsPresenter.TYPE_VERTICAL_TABS_SLOT, this);
}
}
public class HeaderTabsPresenter extends TabContainerPresenter<HeaderTabsPresenter.MyView, HeaderTabsPresenter.MyProxy> {
#Inject EventBus eventBus;
/**
* {#link HeaderTabsPresenter}'s proxy.
*/
#ProxyStandard
public interface MyProxy extends Proxy<HeaderTabsPresenter> {
}
/**
* {#link HeaderTabsPresenter}'s view.
*/
public interface MyView extends TabView {
void changeTab(Tab tab, TabData tabData, String historyToken);
}
/**
* This will be the event sent to our "unknown" child presenters, in order for
* them to register their tabs.
*/
#RequestTabs
public static final Type<RequestTabsHandler> TYPE_HEADER_RequestTabs = new Type<RequestTabsHandler>();
/**
* Fired by child proxie's when their tab content is changed.
*/
#ChangeTab
public static final Type<ChangeTabHandler> TYPE_HEADER_ChangeTab = new Type<ChangeTabHandler>();
#ContentSlot
public static final Type<RevealContentHandler<?>> TYPE_VERTICAL_TABS_SLOT = MainPresenter.WEST_SLOT;
#Inject
public HeaderTabsPresenter(final EventBus eventBus, final MyView view,
final MyProxy proxy) {
super(eventBus, view, proxy, TYPE_VERTICAL_TABS_SLOT, TYPE_HEADER_RequestTabs, TYPE_HEADER_ChangeTab);
}
#Override
protected void revealInParent() {
RevealContentEvent.fire(this, MainPresenter.NORTH_SLOT, this);
}
}
public class MainPresenter extends Presenter<MainPresenter.MyView, MainPresenter.MyProxy>{
#Inject VSMRequestFactory requestFactory;
#Inject VSMMessages vsmMessages;
#Inject VSMExceptionMessages vsmExceptionMessages;
#Inject EventBus eventBus;
#ProxyStandard
#NoGatekeeper
public interface MyProxy extends Proxy<MainPresenter> {
}
public interface MyView extends View {
}
#ContentSlot
public static final Type<RevealContentHandler<?>> SOUTH_SLOT = new Type<RevealContentHandler<?>>();
#ContentSlot
public static final Type<RevealContentHandler<?>> WEST_SLOT = new Type<RevealContentHandler<?>>();
#ContentSlot
public static final Type<RevealContentHandler<?>> CENTER_SLOT = new Type<RevealContentHandler<?>>();
#ContentSlot
public static final Type<RevealContentHandler<?>> NORTH_SLOT = new Type<RevealContentHandler<?>>();
#Inject
public MainPresenter(EventBus eventBus, MyView view, MyProxy proxy) {
super(eventBus, view, proxy);
}
#Override
protected void revealInParent() {
RevealRootLayoutContentEvent.fire(this, this);
}
#Override
protected void onReveal() {
super.onReveal();
initializeAppUser();
}
}
I stumbled upon something similar before... I think it is because both of them inherit from TabContainerPresenter and that there is a bug in gwtp that calls onReveal() on the wrong inherited child.
take a look at this gwtp forum post:
https://groups.google.com/forum/?fromgroups#!searchin/gwt-platform/gilad$20egozi/gwt-platform/p0s3BlN-ceE/5EL3nynaiu4J
Is that the case? if so, I don't think anyone filed a bug on the gwtp issue tracker, so let me know.
(of-course, the best check is not to inherit and see if its working)