Use View.isInEditMode() in your custom views with CordovaWebView - eclipse

I am developing application with CordovaWebView and i have added the below code in web_view.xml file.
<org.apache.cordova.CordovaWebView
android:id="#+id/cordovaWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I have implemented methods from CordovaInterface. Now my design page showing the following error message.
Tip: Use View.isInEditMode() in your custom views to skip code when
shown in Eclipse
Could anyone please tell me that how to resolve it?

isInEditMode()should be used inside the Custom View constructor. Try the following code:
public class GraphView extends View implements Grapher
{
public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode())
init(context);
}
public GraphView(Context context) {
super(context);
if(!isInEditMode()){
touchHandler = new TouchHandler(this);
init(context);
}
}
Replace GraphView with your CordovaWebview.
View.isInEditMode()
Indicates whether this View is currently in edit mode. A View is usually in edit mode when displayed within a developer tool. For instance, if this View is being drawn by a visual user interface builder, this method should return true. Subclasses should check the return value of this method to provide different behaviors if their normal behavior might interfere with the host environment. For instance: the class spawns a thread in its constructor, the drawing code relies on device-specific features, etc. This method is usually checked in the drawing code of custom widgets.

Related

GWT Activities: Widget added to view again when using browser back button

The way I understand it, if I want to add widgets dynamically to a view created with UIBinder, I would do that in the start method of the activity that is the presenter for that view.
Here's my code:
#Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
view = clientFactory.getDashboardView();
view.setPresenter(this);
ArrayList<Department> deps = ModelFactory.getDepartments();
view.passData(deps); // Correct?
panel.setWidget(view.asWidget());
}
public void passData(ArrayList<Department> departments) {
TextCell text = new TextCell();
CellList<String> cellList = new CellList<String>(text);
String[] departmentNames = new String[departments.size()];
for (int i = 0; i < departments.size(); i++) {
departmentNames[i] = departments.get(i).getName();
}
cellList.setRowData(Arrays.asList(departmentNames));
departmentsDiv.add(cellList);
}
It works. However, when using the back button to navigate to the previous place and back, the widget is added again.
How do I handle this correctly?
You have two options:
If you don't want to refresh the data on each visit to this view, you need to add a flag to the view to tell if the data has been already populated. Then, when this view is visited again, your activity should call view.passData(deps); only if the flag is set to false. After the data is loaded, set the flag to true.
If you do want to refresh the data on each visit, call departmentsDiv.clear() before adding a new CellList.
NB: A better approach is to create your CellList once, when the view is displayed for the first time, and then only call setRowData when the new data is available.
You are creating View object by using Factory method. You should consider creating views during application load using, for instance: GIN and marking them as Singletons. The proper way is to pass them as start() method parameters and just set presenter reference on them.
General idea is to make Views singletons. Activities should be created while throwing GWT place (stateless) and just using singleton Views, so you can keep your view input data.
Read tutorial here on using MVP / GIN pattern: http://blog.hivedevelopment.co.uk/2009/08/google-web-toolkit-gwt-mvp-example.html

lose content when change orientation in my list view and in forms android

okay guys, i know that have couple topics with the same purpose, but i'm new on android, and the topics that i found just created more mess in my mind, so thats why i'm here;
I got an app with 5 Activitys(fragments) 2 are list views, and 3 are forms, i'm populating my list views with the content from 2 of my forms;
And the problem is when i change my orientantion i loose everything as everybody knows, but i don't know what and where i need to set some configurations that i found in my research for example: i saw a lot examples implementing this:
<activity
android:name="com.abc.src.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
/>
as a solution but many others said that this is not enough, and after this i need to implements one method ex:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
// set visibility visible of Layout in which your list fragment resides
}else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
// set visibility gone of Layout in which your list fragment resides
}
}
And the last one, which one makes more sense to me, but i don't know exaclty how and where to implement is:
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("MyText", edtMyText.getText().toString());
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
if (savedInstanceState != null) {
String myString = savedInstanceState.getString("MyText");
edtMyText.setText(myString);
}
}
#SuppressWarnings("unchecked")
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
???(what and where i need to set) ???
}
This last one makes more sense to me in my forms case, that i can get the values of my sppiners and editTexts and save them into a string, but i can't figure out how can i save the items of my list views with this example,
the question is, i understand the purpose of the methods above, but i don't know where i need to implement,
i'm following the MVC pattern, so i guess i need to insert these methods in my view classes right ??
And all my activites is just calling my fragments so i need to set the StateChange methods, in my fragments class or my Activities classes ?
Any helps or solutions will be appreciate, so if you guys need i can update my post with my class code... thanks
Do not override any of the config changes. Android will handle it for you, especially when it comes to Views. Only under the most specific (not common) edge cases should you be overriding config changes. Remove everything you posted. You'll find that all Views will be recreated accordingly, to include the text in EditText widgets. If not, then your problem lies else where.

GWTP: Correct information flow for popup presenter

i had this task: I have Presenter/View Couple (lets call it ItemListPresenter) showing a Celltable. Now i wanted to edit an item by double-clicking it (or pressing a button, whatever). Then a popup dialog should appear (Lets call it PopupWidget), letting me edit my item properties.
i found a solution how to do it, but i am not sure it is the "right" way. Since i am trying to learn the philosophy behind GWT/GWTP, i would appreciate if you could give me hints what i did right and what i did wrong:
In the onbind method of ItemListPresenter, i wire the CellTable with a DoubleClick handler:
getView().getCellTable().setSelectionModel(selectionModel);
getView().getCellTable().addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(final DoubleClickEvent event) {
DeviceDto selectedDeviceDto = selectionModel.getSelectedObject();
//TODO: Figure out how to best handle editing of DeviceDto
if (selectedDeviceDto != null) {
devicesDialog.setCurrentDeviceDTO(selectedDeviceDto);
addToPopupSlot(devicesDialog);
}
} }, DoubleClickEvent.getType());
What does not feel right is setting the object i want to edit (selectedDeviceDto) in the Dialog Presenter Widget. Is this the "right" way?
My popup presenter which is defined as
public class DeviceEditDialogPresenterWidget extends PresenterWidget<DeviceEditDialogPresenterWidget.MyView> implements
DeviceEditDialogUiHandlers {
is still ugly, since i just set every property into a text box and after editing, i recollect the properties and rebuild the object. This is messy and i guess i should GWT Editors for that. However, when i click the "Save" Button in the dialog, an UiHandler is triggered:
#UiHandler("okButton")
void okButtonClicked(ClickEvent event) {
DeviceDto dev = new DeviceDto(idBox.getText(), deviceIdBox.getText(), typeBox.getText(), firmwareVersionBox.getText(), userBox.getText(), statusBox.getText());
getUiHandlers().updateDevice(dev);
hide();
}
This triggers my DeviceEditDialogPresenterWidget, which itself fires and event:
#Override
public void updateDevice(DeviceDto device) {
eventBus.fireEvent(new DeviceUpdatedEvent(device));
}
This event is caught by a handler in the "mother" presenter with the CellTable, which is wired in the onBind method again:
addRegisteredHandler(DeviceUpdatedEvent.TYPE, new DeviceUpdatedEvent.DeviceUpdatedHandler() {
#Override
public void onDeviceUpdatedEvent(DeviceUpdatedEvent event) {
updateDevice(event.getDevice());
}
});
I would really like to avoid going down a road of messy mistakes, so any hints would be appreciated.
Thanks
Arthur
PresenterWidgets are usually designed to have a setter which is used to set a Model or DTO that the PresenterWidget works on (the same way you did it).
Alternatively you can avoid a PresenterWidget and use an Editor (extends Composite) that you manually add to a PopupPanel or DialogBox in your ListItemEditor.
This way you avoid the complexity of a PresenterWidget. But you have to handle the clicks (i.e. save button) from the ListItemPresenter. I would always try to start small (use a composite) and if you realize that you might need the functionality also in other places, create a PresenterWidget.
Also you don't need the updateDevice method because you pass a reference to your DTO. You only need to fresh the CellTable.
But apart from that your approach looks fine.

View size not match with layoutPanel container in GWT MVP and SmartGWT

I'm new in GWT, and have problem with view implementation... I use MVP, and SmartGWT. I'll expose this by defining how I settle my MVP and what its weird.
In my onModule, I define class builded with UIbinder. I've declared a LayoutPanel and set it like this in the constructor of the class.
layoutPanel = binder.createAndBindUi(this);
I have container in this class:
public void setBodyLayout() {
panel.setWidgetLeftWidth(menuPanel, xx, PCT, xxx, PCT);
panel.setWidgetRightWidth(bodyPanel, xx, PCT, xx, PCT);
}
menuPanel and bodyPanel are both simplePanel declared in the class above(UIfield use with UIbinder). There are in LayoutPanel. For the method display of my ActivityMapper I've got this method (In reality I have two ActivityMappers, two method that like below and two containers, for menu and body)
public AcceptsOneWidget getBodyContainer() {
return new AcceptsOneWidget() {
#Override
public void setWidget(IsWidget w) {
Widget widget = Widget.asWidgetOrNull(w);
bodyPanel.setWidget(widget);
}
};}
return in my onModule, I declared my ActivityMapper like this
BodyActivityMapper bodyContainerActivityMapper = new BodyActivityMapper(clientFactory);
ActivityManager bodyContainerActivityManager = new ActivityManager(bodyContainerActivityMapper, eventBus);
bodyContainerActivityManager.setDisplay(my_class_described_above.getBodyContainer());
the same work was done with MenuActivityMapper...
Finally
RootLayoutPanel.get().add(my_class_described_above.getLayoutPanel());
when getLayoutPanel() return my layoutPanel declared in the class that I have declared above.
So, each region have its own ActivityMapper.ActivityMapper for the menu have only one activity, and "ActivityMapperBody" have sevral activities triggered by menu.
Utility of container are to settle my layout for different "action". I defined zone with it, in order to receive view started with activity.
But this configuration work only with view builded with UIbinder... In each view, I declare a Layout and return it like this
public Widget asWidget() {
return my_layout_declared;
}
When I return my layout, nothing works. I really don't understand why, and I figure that its worse with smartgwt. All I want its just retrieve my layout and put it in my container... Work with smartgwt can save a lot of time...
I've more detailed my issue to make sure that anyone understand. And ask to you Chris Lercher if your post can help me.
Thank for reading
The Smart GWT FAQ mentions a solution:
If you absolutely must place a Smart GWT interface inside a GWT container and you want it to
fill the container, the best approach is to listen for a window-level resize event and run
your own layout calculations that ultimately call resizeTo() on your topmost Smart GWT
widget.
However, it is wrong by saying that
...GWT containers [do not] fire events when they are resized
Layout panels (the "new" panels since GWT 2.0, which was released on Dec 08, 2009) actually do (see the ProvidesResize and RequiresResize interfaces). So if you have such a panel as your MVP body container (and if its parents are also LayoutPanels up to a top RootLayoutPanel), then you can also override the body container's onResize() method instead of listening to the Window resize event.
So how can you solve your concrete problem?
You need to call resizeTo(width, height) on your topmost Smart GWT layout
When you put in a (new) Smart GWT layout, e.g. maybe you put in a new one when the Place changes.
When the window resizes, or when other things happen that change the size of the container (alternatively, if you're using Layout Panels, you can use its onResize())
For the resizeTo(width, height) call, you'll have to determine the width and height by asking the surrounding container, e.g. by using
container.getElement().getClientWidth()

Unity Custom Inspector on Custom Window

I have a custom window that show a list of objects. Each of this objects has a custom inspector editor.
Is possible to show custom inspector inside the custom window?
You can't force Unity3D to draw your custom inspector somewhere else than inspector window.
Btw you can manually instatiate an Editor using Editor.CreateEditor method.
Since you are displaying a custom inspector, than it should be possible to instantiate it manually from inside Window.OnGUI method, and use the public OnInspectorGUI method of the editor to draw the editor inside your window.
For example, if you have attached a script called CustomScript to a GameObject and have a related Editor called CustomScriptEditor, supposing you have selected the GameObject from the hierarchy, this code visualize the custom inspector inside an EditorWindow:
using UnityEditor;
using UnityEngine;
public class TestWindow : EditorWindow
{
[MenuItem ("Window/Editor Window Test")]
static void Init ()
{
// Get existing open window or if none, make a new one:
TestWindow window = (TestWindow)EditorWindow.GetWindow (typeof (TestWindow));
}
void OnGUI () {
GameObject sel = Selection.activeGameObject;
CustomScript targetComp = sel.GetComponent<CustomScript>();
if (targetComp != null)
{
var editor = Editor.CreateEditor(targetComp);
editor.OnInspectorGUI();
}
}
}
As of right now, this question is a top google result for queries such as "Unity inspector inside editor window" so I'm making a necropost to share some very important information I learned through hours of frustration:
I'm building an Editor tool which makes use of custom inspectors inside of an EditorWindow, and I used the exact same method as Heisenbug. Occasionally while working on the tool my console would be bombarded with hundreds of mysterious null reference exceptions. Restarting Unity would make them go away but they would inevitably return. I was finally able to trace the problem to "Editor.CreateEditor", here is the lesson I learned the hard way:
You need to manually destroy any inspectors created from Editor.CreateEditor!
Normally Unity will instantiate an inspector when the relevant object is selected, and automatically destroy that instance as soon as the object is deselected/deleted/etc. But an inspector created via Editor.CreateEditor will never be destroyed and will linger somewhere in memory long after the object it was inspecting has been destroyed.
You can prove this by adding the following code to your custom inspector:
void OnEnable()
{
Debug.Log("on enable " + this.GetInstanceID());
}
void OnDisable()
{
Debug.Log("on disable " + this.GetInstanceID());
}
Using the inspector normally, you will always see these messages in pairs as instances of the inspector get created and then destroyed. But try instantiating a few inspectors from your EditorWindow class and you will only see the "on enable" messages.
I learned that Unity calls OnEnable() whenever scripts are recompiled. Try it and you'll see your console fill up with "on enable" messages from all the inspector instances that were never cleaned up. These instances will linger long after the objects they were inspecting have been destroyed, and any initialization code in OnEnable() will break spectacularly throwing an avalanche of mysterious exceptions.
My solution looks something like this (this code would go in your EditorWindow class):
Editor embeddedInspector;
...
// a helper method that destroys the current inspector instance before creating a new one
// use this in place of "Editor.CreateEditor"
void RecycleInspector(Object target)
{
if (embeddedInspector != null) DestroyImmediate(embeddedInspector);
embeddedInspector = Editor.CreateEditor(target);
}
...
// clean up the inspector instance when the EditorWindow itself is destroyed
void OnDisable()
{
if (embeddedInspector != null) DestroyImmediate(embeddedInspector);
}