GWT: execution stops when adding a Composite widget to RootPanel - java

I have an abstract class extending Composite (AbstractWhiteBoard). Then I have a concrete class extending AbstractWhiteBoard. When I instantiate the concrete class and try to add it to the RootPanel, the program simply stops executing. There is no error or any output to direct me to a log file. I have no idea what is going wrong.
Here is my abstract class:
public abstract class AbstractWhiteBoard extends Composite {
/*
* FIELDS
*/
protected HorizontalPanel WhiteBoardWrapperPanel;
public AbstractWhiteBoard( ) {
WhiteBoardWrapperPanel = new HorizontalPanel();
WhiteBoardWrapperPanel.setStyleName("WhiteBoard-Wrapper");
initWidget(WhiteBoardWrapperPanel);
}
/*
* ABSTRACT PUBLIC METHODS
*/
abstract public void addNotecard( Notecard nc );
abstract public void addPostit( Postit postit );
/*
* ABSTRACT PROTECTED HELPER METHODS
*/
abstract protected void registerDragDropControllers();
}
And here is my concrete implementation class:
public class ConcreteWhiteBoard extends AbstractWhiteBoard {
/*
* CONTSTRUCTORS
*/
public ConcreteWhiteBoard() {
super();
}
/*
* PUBLIC METHOD OVERRIDES
*/
#Override
public void addNotecard(Notecard nc) {
// TODO Auto-generated method stub
}
#Override
public void addPostit(Postit postit) {
// TODO Auto-generated method stub
}
/*
* PRIVATE HELPER METHOD OVERRIDES
*/
#Override
protected void registerDragDropControllers() {
// TODO Auto-generated method stub
}
}
So, what is happening, is I have this code:
AbstractWhiteBoard wb = new ConcreteWhiteBoard();
RootPanel.get().add(wb);
Window.alert("wb added!");
But after I add wb to the RootPanel, execution stops. The alert statement never even gets called. There is no error and I don't see anything in the log.
Is there something wrong with having an abstract class that extends Composite? Or is it something entirely different that I am just not seeing? any help is greatly appreciated!

take a look at the uncaught exception handler in gwt. if a runtime exception occurs it is called. Think of it as a global try catch around your code.
But if your code is inside your entrypoint on module load make sure to set the uncaught exception handler and call the next function within a timer (so that the uncaught exception handler is active.
For a quick example take a look here:
http://code.google.com/p/mgwt/source/browse/src/main/java/com/googlecode/mgwt/examples/showcase/client/ShowCaseEntryPoint.java?repo=showcase
In web mode you can turn on emulated stack (and get meaningful stacktraces). YOu need to add this to your gwt.xml file (only for debug purposes because it is quite slow):
<set-property name="compiler.emulatedStack" value="true" />
<set-configuration-property name="compiler.emulatedStack.recordLineNumbers" value="true" />
<set-configuration-property name="compiler.emulatedStack.recordFileNames" value="true" />

So, this is one of those times that you feel like the most retarded developers of all time. What happened is that I was running several async calls all at the same time and I tried to refer to an object that was returned by one of those calls before it was actually created. Dunce cap on me, I got confused with async threads.
Major thanks to Daniel. Your input lead me straight to the problem!

Related

How to instantiate, configure and use a lib/framework in a oo-application?

I decided to split the last part of that question here into a new question here: https://softwareengineering.stackexchange.com/questions/411738/extension-of-classes-where-to-put-behaviour-how-much-direct-access-is-allowe
If i have a lib and i want to use it, i wrote mostly a own class. This class has one method. In that there is the code how to instantiate the lib/framework. Sometimes there are a few more methods, with them i not only instantiate the class but use it. For example if i want to start a http-server i have there a start-method.
class Container
{
TheLib theLib;
public void init() //or a constructor
{
//some init of the theLib
}
public void start() //
{
theLib.doSomething(...)
theLib.doSomethingmore(...);
theLib.start(...);
}
//important!
public TheLib getTheLib()
{
return this.theLib; //after i started configured it and so on, i want of course use all methods,
which the lib have in some other parts in my application
}
}
But it seems not to be the best solution.
Are there any better solutions, that OO is?
Often i also use only one method, a own class for this seems to be here a big overhead?
Exposing the lib breaks encapsulation? Tell-Dont-Ask is also violated?
Everything depend on what you actually need or how you have access to your 'the lib' instance.
public class Container {
private TheLib theLib;
/* #1: Do you already created the instance before? */
public Container(TheLib theLib) {
this.theLib = theLib;
}
/* #2: Do you need to created the instance each time? */
public Container() {
this.theLib = new TheLib();
}
public void start() {
theLib.doSomething(...)
theLib.doSomethingmore(...);
theLib.start(...);
}
public TheLib getTheLib() {
return this.theLib;
}
public static void main(String[] args) {
/* #1 */
TheLib theLib = ...;
Container container = new Container(theLib);
/* #2 */
Container container = new Container();
/* Continue the flow of your program */
container.start();
container.getTheLib().doSomethingEvenMore();
}
}
Or maybe you actually need only one instance of your 'Container' class. In this case, you should look on how to make a singleton: Java Singleton and Synchronization
Anwser: Often i also use only one method, a own class for this seems to be here a big overhead?
Well, in Java, you cannot do formal programming like in C, so everything line of code that you write, or will be using, has to be in a class of some sort.
If your piece of code is small and don't really need an object, static function might do the work.

How to Access a Property of the Outer Class Instance from an Inner Class

In a method of a custom class View:
public class View {
private Timer timer;
...
private double[][] allLevels;
...
I have a method with an abstract call that needs to point to the variable allLevels. The variable is produced by another class GameLogic, but in the Main of the application. In the Main, the return argument from a public method is then passed to the View:
public class Game extends ApplicationAdapter {
View view;
GameLogic gameLogic;
#Override
public void create () {
System.out.println("Creating");
this.gameLogic = new GameLogic();
this.gameLogic.prepareStimulus();
}
#Override
public void render () {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.view = new View();
this.view.presentStimulus(this.gameLogic.allLevels);
}
}
Because of a very complex game/business logic I thought I would try to separate and encapsulate as much as possible in a MVC-ish pattern. The Main uses libgdx, which requires create and render to be separate.
My main problem is that I am unable to reach the variable in the class View from the abstract call to the class scope.
public void presentStimulus(double[][] allLevels){
...
timer = new Timer();
...
timer.scheduleTask(new Task(){
#Override
public void run(){
DO SOMETHING WITH that.allLevels[0][0]
}
}, .....);
I have looked at a similar issue, but I guess my question is more basic.
The IDE is unable to autocomplete the reference to the properties using the keyword "this". How can I make the Run() method access the property of the instance of the outer class?
The problem ( I rather doubt it) is here -
public void presentStimulus(double[][] allLevels){
...
timer = new Timer();
...
timer.scheduleTask(new Task(){
#Override
public void run(){
DO SOMETHING WITH that.allLevels[0][0]
}
}, .....);
You see, in this code time.scheduleTask, you are just only creating a new Task and not executing it. It is executed after a while with another thread I supposed, thus it is run in a different context, which does not have allLevels value at the time of execution. So there is no way you can access that allLevels in that run method unless you use closure. I am not sure whether java supports closure or not, but here is a similar answer that might help you - Closure in Java 7
You can use some other solutions, like save the hash and allLevels in a separate static dictionary that is accessible globally and then pick the value from there.

How to find out whether Method has called for given instance. Like "Object obj" check whether obj called "equals" method or not

I want to find out whether method for some object is being called for that instance or not.
Is it possible in java ?
Like ...
class Button {
public void focus(){}
public void setName(){}
}
class MyTest {
public static void main(String[] args){
Button button = new Button();
button.focus();
// I want to find out on button instance whether focus() or setName() is called or not.
whetherMethodCalled(button);
// OR
whetherMethodCalled(button, 'focus');
whetherMethodCalled(button, 'setName');
}
}
EDIT : Forgot to add Button class is third party class which I cannot modify... Also I want to check in my code whether method has called for given object instance or not on basis of that I have to write some code.
In order to reduce extra work, perhaps profiling your application with JConsole or another tool is good enough to show if certain methods have run. Another option is using a code coverage tool like EMMA which detects dead code. There is a list of open-source profilers for Java at http://java-source.net/open-source/profilers and EMMA is at http://emma.sourceforge.net/.
With some extra work AspectJ could be use to intercept method calls without changing existing code. For example, the following would intercept calls to Button.focus()
#Aspect
public class InterceptButtonMethods {
#Before("execution(* Button.focus())")
public void beforeInvoke() {
System.out.println("Button.focus invoked");
incrementFocusCount();
}
}
If more extra work is ok, there is a way to wrap all calls to the Button's focus() and setName() methods so that they update separate counters in addition to their normal functions. This can be done by extending Button in YourButton class which is identical to Button except for a couple of int counters with getters, setters and increment methods; and countingFocus() and countingSetName() methods which update their counters and call focus() and setName() respectively, such as in outline:
Class YourButton extends Button {
int focusCount;
int setNameCount
int getFocusCount() {return this.focusCount;}
void setFocusCount(int counter) {this.focusCount = counter} // optional to reset counter
void incrementFocusCount() {this.focusCount = getFocusCount() + 1;)
...
void countingFocus() {
incrementFocusCount();
focus()
}
...
}
If it is required in many places and involves complex things, I recommend to use Mockito to test your code. Using that you can verify if the method was invoked (also how many times if invoked)
You can mock the button and verify in your MyTest how many times the method must be called. Using Mockito you can mock and stub your methods(Stubbing voids requires different approach from when(Object) because the compiler does not like void methods inside brackets) and then verify it using verify statement.
verify(mockButton, times(1)).focus();
verify(mockButton, times(1)).setName();
You can write a wrapper class over the 3rd party Button class through which all calls to Button class will be made.
This wrapper class can keep track of whether each method has been called or not
class ButtonCaller {
private Button button = null;
private boolean focusCalled;
private boolean setNameCalled;
public ButtonCaller() {
button = new Button();
focusCalled = false;
setNameCalled = false;
}
public void focus() {
button.focus();
focusCalled = true;
}
public void setName() {
button.setName();
setNameCalled = true;
}
public void whetherMethodCalled(ButtonMethod method) {
switch (method) {
case FOCUS:
return focusCalled;
case SET_NAME:
return setNameCalled;
}
throw new RuntimeException("Unknown ButtonMethod !!!");
}
public static Enum ButtonMethod {
FOCUS,
SET_NAME;
}
}

Implement postWindowClose() in RCP application

Hi RCP developers,
I want to iplement postWindowClose() in my ECLIPSE RCP application.
Before coding this method, I just did a small test to see if when I close my application, the method is called, so I did that :
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class MainWindowControl extends WorkbenchWindowAdvisor{
public MainWindowControl(IWorkbenchWindowConfigurer configurer) {
super(configurer);
// TODO Auto-generated constructor stub
}
#Override
public void postWindowClose() {
// TODO Auto-generated method stub
super.postWindowClose();
System.out.println("close");
}
}
I am expecting to see : close in ECLIPSE console, but it's still blank after closing the application.
All the required plugins are added , and I have no error while launching or closing the application.
So, AM I missing something ?
The reasons why to implemets this method are :
Msg box : Are you sure you want to close the application
Kill all the running threads, my application upload files and even when I close the application running uploads continues. I want to abort them when closing the application.
Edit :
My life cycle class :
package upload.center.util;
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
public class WindowLifeCycle {
#PostContextCreate
public void postContextCreate()
{
// TODO start up code here
System.out.println("open");
}
#PreSave
public void preSave()
{
// TODO add shutdown code here
System.out.println("close");
}
}
My plugin.xml :
<product ....
<property
name="windowLifeCycle"
value="bundleclass://UploadCenter.Source/upload.center.util.WindowLifeCycle">
</property>
...</product>
I hope that I am clear enough.
Ismail
For a pure Eclipse 4 (e4) application the workbench window advisor (and the other advisors) are not used. You use the #PreSave method of a life cycle class to run code during shutdown.
public class LifeCycle
{
#PostContextCreate
public void postContextCreate()
{
// TODO start up code here
}
#PreSave
public void preSave()
{
// TODO add shutdown code here
}
}
declare the life cycle class in the product definition in the plugin.xml:
<extension
id="product"
point="org.eclipse.core.runtime.products">
<product
name="%product.name"
application="org.eclipse.e4.ui.workbench.swt.E4Application">
<property
name="lifeCycleURI"
value="bundleclass://plugin-id/package.LifeCycle">
</property>
.... more properties ...
For more details see here

JPanel.addComponentListener does not work when the listener is a class variable

I have a public class which has the following method and instance variable:
public void setImagePanel(JPanel value) {
imagePanel = value;
if (imagePanel != null) {
//method 1 : works
imagePanel.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent evt) {
System.out.println("Here 1");
}
});
//method 2 : does not work
panelResizeListener = new ResizeListener();
imagePanel.addComponentListener(panelResizeListener);
//method 3 : works
//ResizeListener listener = new ResizeListener();
//imagePanel.addComponentListener(listener);
//method 4 : works
//imagePanel.addComponentListener(new ResizeListener());
//method 5 : does not work -- THIS IS THE DESIRED CODE I WANT TO USE
imagePanel.addComponentListener(panelResizeListener);
}
}
public class ResizeListener extends ComponentAdapter {
#Override
public void componentResized(ComponentEvent evt) {
System.out.println("RESIZE 3");
}
}
private ResizeListener panelResizeListener = new ResizeListener();
private static JPanel imagePanel;
Each of the methods above correspond the to code immediately below until the next //method comment. What i don't understand is why i can't use the class instance variable and add that to the JPanel as a component listener.
What happens in the cases above where i say that the method does not work is that i don't get the "RESIZE 3" log messages. In all cases where i list that it works, then i get the "RESIZE 3" messages.
The outer class is public with no other modification except that it implements an interface that i created (which has no methods or variables in common with the methods and variables listed above).
If anyone can help me i would greatly appreciate it. This problem makes no sense to me, the code should be identical.
Man camickr, you were right. Man this was a weird one to solve. There was something else wrong with my code. The order of the methods calls into my class resulted in me adding the listener then another method would end up removing the listener referenced by that variable so of course i would never get events. Thanks a lot for all the help ppl.
I think your problem is that you're declaring panelResizeListener after you're using it. That normally kills just about anything.

Categories