Outer Class Member Variable Null To Inner Class - java

I am working on a game using JME3 and Nifty GUI. I have an outer class that has a Nifty member variable. And the inner class should be able to access that variable regardless of access modifier. In the constructor I assign a new Nifty object to it. However when I access that variable in the inner class I run into problems. I did a little debugging and found out it's because the inner class thinks the Nifty member variable is null and I cant figure out why. Its not null in the outer class. Since this is a JME3 game I tried to have the inner class implement the AppState interface but it still shows the Nifty member variable as null. Here is the code:
public class MenuScreen extends SimpleApplication {
/** Used to configure Nifty GUI. */
private Nifty mNifty;
private NiftyJmeDisplay mNiftyDisplay;
private Element popup;
//*******************
// Overridden medhods
//*******************
/** This method is used to initialize everything needed to display the game screen. */
#Override
public void simpleInitApp() {
guiNode.detachAllChildren();
initNifty();
flyCam.setDragToRotate(true);
}
/**
* The game's main update loop.
*
* #param tpf Time Per Fram, the time it takes each loop to run.
*/
#Override
public void simpleUpdate(float tpf) {
// not used
}
#Override
public void simpleRender(RenderManager rm) {
// not used
}
public static void main(String[] args) {
MenuScreen app = new MenuScreen();
app.start();
}
/**
* Helper method to initialize and configure Nifty GUI.
*/
private void initNifty() {
mNiftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
mNifty = mNiftyDisplay.getNifty();
guiViewPort.addProcessor(mNiftyDisplay);
// If this is being run on a desktop then load the desktop main menu.
if (Strings.OS_NAME.contains("windows") || Strings.OS_NAME.contains("mac") || Strings.OS_NAME.contains("linux")) {
mNifty.fromXml("Interface/XML/DesktopMenuScreenGui.xml", "start", new MenuScreen().new MenuScreenGui());
}
// If its an Android device load the mobile main menu.
else if (Strings.OS_NAME.contains("android")) {
mNifty.fromXml("Interface/XML/MobileMenuScreenGui.xml", "mobile", new MenuScreen().new MenuScreenGui());
}
}
//**************
// Inner Classes
//**************
/**
* © Jason Crosby 2012 <p>
*
* This class handles all the GUI interactions like button clicks.
*
* #author Jason Crosby
*/
public class MenuScreenGui implements ScreenController, EventTopicSubscriber<MenuItemActivatedEvent>,
AppState {
#Override
public void initialize(AppStateManager stateManager, Application app) {
}
#Override
public void cleanup() {
}
#Override
public boolean isEnabled() {
return false;
}
#Override
public boolean isInitialized() {
return false;
}
#Override
public void postRender() {
}
#Override
public void setEnabled(boolean active) {
}
#Override
public void stateAttached(AppStateManager stateManager) {
}
#Override
public void stateDetached(AppStateManager stateManager) {
}
#Override
public void render(RenderManager rm) {
}
#Override
public void update(float tpf) {
}
#Override
public void bind(Nifty nifty, Screen screen) {
// not used
}
#Override
public void onEndScreen() {
// not used
}
#Override
public void onStartScreen() {
// not used
}
#Override
public void onEvent(String string, MenuItemActivatedEvent t) {
}
//**************
// Class methods
//**************
/**
* Called when the play button is clicked.
*/
public void playButton() {
}
/**
* Called when the high scores button is clicked.
*/
public void highScoresButton() {
}
/**
* Called when the settings button is clicked.
*/
public void settingsButton() {
}
public void quitButton() {
showDialog();
}
/**
* Called when the rate button is clicked. Only Available on mobile.
*/
public void rateButton() {
}
/**
* Called when the feedback button is clicked. Only on mobile devices.
*/
public void feedbackButton() {
}
/**
* Called when the help button is clicked.
*/
public void helpButton() {
}
/**
* Called when the dialog needs to be shown.
*/
public void showDialog() {
System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
popup = new Nifty().createPopup("popup");
System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
//Menu myMenu = popup.findNiftyControl("#menu", Menu.class);
//myMenu.setWidth(new SizeValue("100px")); // must be set
//myMenu.addMenuItem("Click me!", new menuItem("menuItemid", "blah blah")); // menuItem is a custom class
//mNifty.subscribe(mNifty.getCurrentScreen(), myMenu.getId(), MenuItemActivatedEvent.class, this);
mNifty.showPopup(mNifty.getCurrentScreen(), popup.getId(), null);
}
public void clsoseDialog() {
}
/**
* Used to return a String to the Nifty xml file.
*
* #param name The name key associated with the String.
* #return The String associated with the key.
*/
public String getString(String name) {
if (name.equals("play")) {
return Strings.PLAY_BUTTON;
}
else if (name.equals("high_score")) {
return Strings.HIGH_SCORES_BUTTON;
}
else if (name.equals("settings")) {
return Strings.SETTINGS_BUTTON;
}
else if (name.equals("quit")) {
return Strings.QUIT_BUTTON;
}
else if (name.equals("rate")) {
return Strings.RATE_BUTTON;
}
else if (name.equals("feedback")) {
return Strings.FEEDBACK_BUTTON;
}
else if (name.equals("rules")) {
return Strings.RULES_BUTTON;
}
return null;
}
}
}
What happens is I click on the quit button which calls the quitButton() method. That works fine. That in turn invokes showDialog() which is where the problem is. In the showDialog() method is this line popup = new Nifty().createPopup("popup"); and it is at that line which mNifty is null when it shouldn't be. Any assistance is appreciated.

The line
popup = new Nifty().createPopup("popup");
does not use mNifty. It creates a new instance of Nifty and then calls creatPopup() on this new instance. Since earlier you initialized mNifty by calling what looks like a factory method
mNifty = mNiftyDisplay.getNifty();
it is quite possible that obtaining a Nifty via new does not return a completely initialized instance. Since you haven't posted the code for Nifty it is unclear what is happening.
I would double-check to make sure that creating a Nifty via new will return a fully initialized instance, and that you really wanted a new instance here.

Related

How change an object reference on run time (Design Patterns)

The application is a text based Starbucks simulator.
Image
The problem I am facing is how do I change reference to a screen base on a (x, y) values.
I am looking for a solution where I give the screen the responsibility of changing to the next screen base on x and y values.
Here is the third layer of the application which initial's screen is MyCards:
public class Frame implements IFrame {
private IScreen current = new MyCards();
/* Most code are taken out because not relevant */
public void touch(int x, int y){
if(current!= null){ current.touch(x, y)}
}
}
If I call frame.touch(3,3) it should change the reference to a new screen which I call MyCardsPay.
Here is my interface I call IScreen:
public interface IScreen
{
void touch(int x, int y) ; // send touch events to screen
String display() ; // displays screen components
String name() ; // returns name of screen
void next() ; // navigate to next screen
void prev() ; // navigate to previous screen
void setNext(IScreen s, String n ) ; // set next screen with action name
void setPrev(IScreen s, String n ) ; // set previous screen with action name
}
Here is my base class I call Screen:
public class Screen implements IScreen {
public Screen(){
}
#Override
public void touch(int x, int y) {
}
#Override
public void next() {
// add code here
}
#Override
public void prev() {
// add code here
}
#Override
public void setNext(IScreen s, String n ) {
// add code here
}
#Override
public void setPrev(IScreen s, String n ) {
// add code here
}
#Override
public String display() {
return "";
}
#Override
public String name() {
return (this.getClass().getName()).split("\\.")[1] ;
}
}
Here is my MyCard class:
public class MyCards extends Screen {
private Double price;
public MyCards() {
}
public void setPrice(Double p){
price = p;
}
#Override
public String display(){
return getPriceDescription() + super.display();
}
public String getPriceDescription(){
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return formatter.format(price);
}
}
Here is my MyCardsPay class:
public class MyCardsPay extends Screen
{
String cardID;
public MyCardsPay()
{
}
public void setCardID(String c){
cardID = c;
}
#Override
public String display(){
if(cardID == null){
return super.display();
}
return "[" + cardID + "]" + "\n\n\n" + "Scan Now";
}
}
If i'm not getting wrong, you are storing the current screen by the current filed of Frame class, and you want to change the screen reference by Frame.touch() method?
In this case I would suggest making touch(in IScreen, and in MyCards and MyCardsPay) returning not void, but the "result screen" of touch(). And store the result by Frame.current.
That is to say, Frame like:
public class Frame implements IFrame {
private IScreen current = new MyCards();
/* Most code are taken out because not relevant */
public void touch(int x, int y){
if(current!= null){ current = current.touch(x, y)}
}
}
And the Screens like:
public class MyCards extends Screen {
#Override
public IScreen touch(int x, int y) {
if(isOnThePayButton(x, y)){
return new MyCardsPay();
}
}
}
A good way is to use the Observer pattern. Have Screen tell Frame which screen to update on touch method.
Use an interface to communicate.
public interface IScreenFlowObserver {
void switched(String screen, String n);
}
Have Frame implement it.
public class Frame implements IFrame, IScreenFlowObserver{
/* More code*/
public void switched(String screen, String n){ /* */
}
}
Then change the base class to:
public class Screen implements IScreen{
IScreenFlowObserver observer;
public void setObserver(IScreenFlowObserver o){
observer = o;
}
}
Last, have a ScreenFactory to create Screens.
With that simple design pattern, Screen classes can tell Frame who to switch to. This allows let if statments.

How to create hyperlink in Messagebox.show in ZK in Java?

In my ZK project with Java, I need to show a message box in which there needs to be a hyperlink, from which user can open another web page.
How can I achieve this?
According to the documentation, the customization of the MessageBox is rather limited. There is a possibilty to completely change the UI via MessageBox.setTemplate(), but this affects all MessageBoxes.
In our project, we replaced zk's default MessageBox with our own, which works more like a dialog. With this, we are in full control over the content of the dialog.
public abstract class OurDialog
{
private Window window;
private DialogListener closeListener;
public OurDialog(String title)
{
window = new Window();
window.setTitle(title);
window.setHflex("min");
window.setSizable(false);
window.setPosition("center");
window.setContentStyle("overflow: auto");
window.addEventListener(Events.ON_CLOSE, new EventListener<Event>()
{
#Override
public void onEvent(Event event)
throws Exception
{
if (closeListener != null)
{
if ("confirmed".equals(event.getData()))
{
closeListener.onClose(OurDialog.this);
if (!closeListener.onCloseConfirmation(OurDialog.this))
{
event.stopPropagation();
}
}
else
{
closeListener.onCancel(OurDialog.this);
if (!closeListener.onCancelConfirmation(OurDialog.this))
{
event.stopPropagation();
}
}
}
}
});
}
public final void setWidth(int width)
{
if (width != 0)
{
window.setMinwidth(width);
window.setWidth(width + "px");
}
else
{
window.setHflex("min");
}
}
public final void setHeight(int height)
{
if (height != 0)
{
window.setMinheight(height);
window.setHeight(height + "px");
}
else
{
window.setVflex("min");
}
}
public final void close()
{
Events.sendEvent(Events.ON_CLOSE, window, "confirmed");
}
public final void cancel()
{
Events.sendEvent(Events.ON_CLOSE, window, "cancelled");
}
/**
* #param closeListener called when the dialog is closed.
*/
public final void show(DialogListener closeListener)
{
setCloseListener(closeListener);
window.appendChild(getContent());
GUIHelper.setFocusToFirstInput(window);
OurMeatApplication.getCurrent().showDialog(this);
}
/**
* #return the component to be displayed in the dialog.
*/
protected abstract Component getContent();
}
Our main application class (OurApplication) has a reference to the page and provides the method to show the dialog:
final void showDialog(final OurDialog dialog)
{
Window window = dialog.getWindow();
window.setParent(page);
window.doModal();
}
This is a very generic dialog implementation for pretty much any purpose. For the specific MessageBox case, we have a sub class that provides a prepared UI, error level indicators, several buttons to pick, and a specialised listener to listen to those buttons.

Drop event happening more than one time in SWT

I am implementing Drag and Drop functionality in SWT.
Here is my code snippet
For Drag
private class MyDragSourceAdapter extends DragSourceAdapter {
/** The m tree viewer. */
private final TreeViewer treeViewer;
/**
* Instantiates a new my drag source adapter.
*
* #param treeViewer the tree viewer
*/
public MyDragSourceAdapter(TreeViewer treeViewer) {
this.treeViewer = treeViewer;
}
/**
* {#inheritDoc}
*/
#Override
public void dragFinished(DragSourceEvent event) {
// clean buffer
// k = null;
}
/**
* {#inheritDoc}
*/
#Override
public void dragSetData(DragSourceEvent event) {
event.data = "test";
}
/**
* {#inheritDoc}
*/
#Override
public void dragStart(DragSourceEvent event) {
if (some condition) {
// do something
return;
}
event.doit = false;
}
}
For drop
private class MyDropTargetAdapter extends DropTargetAdapter {
#Override
public void dragEnter(DropTargetEvent event) {
event.detail = DND.DROP_LINK;
}
/**
* {#inheritDoc}
*/
#Override
public void dragOver(DropTargetEvent event) {
// Do some functionality
}
/**
* {#inheritDoc}
*/
#Override
public void drop(DropTargetEvent event) {
event.detail = DND.DROP_NONE;
}
}
Some times "dragOver()" method called more than one time? So dragging is happening as many times as dragOver() method called.
I want drop operation to stop after first time.
Please help
dragOver() happens when the mouse is still hovering over the control, before the actual drop happens. You probably mean to override drop(). But if you want the drop from stop happening altogether, set the field detail on the DropTargetEvent to DND.DROP_NONE.

setVisible in Table in libgdx not working?

I have the following problem:
I am trying to implement a menu with submenus in libgdx, using the table layout. When a submenu is clicked, a listener is fired, which should setVisible(false) the previous menu, and setVisible(true) the new one. But, although I successfully display the new one, the previous one is still here! Could someone help me?
Here is my code:
The Menu.java:
public class Menu extends Table {
private Stage stage;
public void attachToStage(Stage s) {
if (s != null) {
stage = s;
s.addActor(this);
}
}
public Menu() {
this(null);
}
public Menu(final Stage s) {
attachToStage(s);
setFillParent(true);
top();
left();
}
private void addButtonWithListener(String label, ClickListener listener) {
add().width(10);
Label l = new Label(label, SkinManager.get());
add(l).width(100);
row();
if (listener != null)
l.addListener(listener);
}
/**
* #param label
* #return The menu, for chaining
*/
public Menu addButton(String label) {
addButtonWithListener(label, null);
return this;
}
/**
*
* #param label
* #param m
* the menu to add
* #return The main menu, for chaining
*/
public Menu addMenu(String label, final Menu m) {
addButtonWithListener(label, new ClickListener() {
#Override public void clicked(InputEvent e, float x, float y) {
System.out.println(getChildren());
setVisible(false);
m.setVisible(true);
}
});
m.attachToStage(stage);
m.setVisible(false);
return this;
}
}
My Application:
public class TestApplication implements ApplicationListener {
private Stage stage;
#Override public void create() {
stage = new Stage();
Menu m = new Menu(stage).addButton("Move").addButton("Stay");
m.addMenu("Attack", new Menu().addButton("Sword").addButton("Bow"));
Gdx.input.setInputProcessor(stage);
}
#Override public void render() {
stage.draw();
}
// Other empty methods
}
Thanks a lot!
EDIT: Problem was unrelated, see my answer
Ok, found the problem: not related at all to the Table, just forgott to clear the screen in the Application.
I just added
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
in the render function.

Clickhandler doesn't fire

I have a problem with ClickHandler in my project using GWT.
In the title of dialog box I want to insert a new button.
I created a new insert method: addToTitle(...).
I added ClickHandler to the button
Problem: click event by button doesn't fire. Why?
Here is my code:
DialogBox dialog = new DialogBox();
Button button = new Button("A new Button");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
Window.alert("yuhuhuhu");
}
});
dialog.addToTitle(button);
code (extracted from the comments section) :
public class PlentyDialogWindow extends DialogBox {
private FlowPanel captionPanel = new FlowPanel();
public Widget closeWidget = null;
private boolean closeOnEscKey = false;
private FlowPanel titleContentWrapper = new FlowPanel();
public PlentyDialogWindow(boolean isModal) {
super( false, isModal);
this.addStyleName("DialogBox");
this.getElement().setId("DialogBoxId");
this.setAnimationEnabled(true);
this.closeWidget = generateCloseButton();
}
public void setCaption( String txt,Widget w) {
captionPanel.setWidth("100%");
this.addCaption(txt);
this.titleContentWrapper.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
captionPanel.add(this.titleContentWrapper);
FlowPanel widgetWrapper = new FlowPanel();
widgetWrapper.add(w);
widgetWrapper.addStyleName("PlentyPopupCloseIconWrapper");
captionPanel.add(widgetWrapper);
captionPanel.addStyleName("Caption");
Element td = getCellElement(0,1);
td.setInnerHTML("");
td.appendChild(captionPanel.getElement());
}
/** * * #param w */ public void addToTitle(Widget w) {
this.titleContentWrapper.add(w);
}
}
If your only problem is ClickHandler not being called try using addDOMHandler instead of addClickHandler
yourWidget.addDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
}
},ClickEvent.getType());
The solution is a bit tricky.
public class PlentyDialogWindow extends DialogBox {
/*
* Create custom inner class extending `FlowPanel`. You need it only
* to make `onAttach` and `onDetach` methods be visible to wrapping
* class (e.g. your `PlentyDialogWindow` class).
*/
static class MyCaptionPanel extends FlowPanel {
#Override
protected void onAttach() {
super.onAttach();
}
#Override
protected void onDetach() {
super.onDetach();
}
}
/*
* `PlentyDialogWindow`'s field `captionPanel` will be an instance of
* this class.
*/
private MyCaptionPanel captionPanel = new MyCaptionPanel();
/*
* ... leave the rest of your class untouched ...
*/
/*
* Finally, overwrite `PlentyDialogWindow`'s `onAttach` and `onDetach`
* methods to invoke `captionPanel`'s corresponding methods:
*/
#Override
protected void onAttach() {
super.onAttach();
captionPanel.onAttach();
}
#Override
protected void onDetach() {
super.onDetach();
captionPanel.onDetach();
}
}
That's all.

Categories

Resources