You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by mattux <ma...@hotmail.fr> on 2012/05/17 18:13:37 UTC

Prompt: override option click event

Hi all,

I created a custom Prompt with 2 buttons which is launched at the window
startup (desktop application) to ask for credentials (Login/Exit). I
overrode the listener as below:
prompt.getPromptListeners().add(new PromptListener.Adapter() {
			@Override
			public void selectedOptionChanged(Prompt prompt, int
previousSelectedOption) {
				int selectedOptionIndex = prompt.getSelectedOptionIndex();
				// 1st button
				if (selectedOptionIndex == 0) {
					// Do something
				}
				// 2nd button
				else if (selectedOptionIndex == 1) {
					// Do something
				}
			}
		});

The default option is the 1st button. Therefore, if I click on the 2nd
button, the event will trigger fine. However, if I quit and re-launch the
app and I click on the 1st button, the event won't trigger as the selected
option has not changed.
How can I catch the click on the 1st button?
I would ideally like to keep the prompt opened until I check credentials (I
will include an activity progress control and an error label later on).

Many thanks.
Matt

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: Prompt: override option click event

Posted by mattux <ma...@hotmail.fr>.
Hi Roger,

I'm good for the samples thanks. I got it to work as wanted with the State
listener!
Thanks again!

Regards,
Matt

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4004539.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: Prompt: override option click event

Posted by "Roger L. Whitcomb" <Ro...@actian.com>.
Ah, cool.  You're welcome.  Glad to have you going...

~Roger

-----Original Message-----
From: mattux [mailto:mattux@hotmail.fr] 
Sent: Monday, May 21, 2012 2:03 AM
To: user@pivot.apache.org
Subject: RE: Prompt: override option click event

Thanks Roger for your kind help!

This is the solution to what I wanted to do without going to deep into
customization. Before your answer, I already extended 3 classes, some of
them being skin ones so I'm really pleased to have found a solution that
avoids me extending skin classes!

Regards,
Matt

--
View this message in context:
http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-cl
ick-event-tp3999325p4004550.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: Prompt: override option click event

Posted by mattux <ma...@hotmail.fr>.
Thanks Roger for your kind help!

This is the solution to what I wanted to do without going to deep into
customization. Before your answer, I already extended 3 classes, some of
them being skin ones so I'm really pleased to have found a solution that
avoids me extending skin classes!

Regards,
Matt

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4004550.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: Prompt: override option click event

Posted by "Roger L. Whitcomb" <Ro...@actian.com>.
Alright, here's one that uses a DialogStateListener (exactly the same
concept as SheetStateListener, but different name, basically).  This
allows the close if user clicks 'Cancel' or hits the Escape key, but if
"OK" is clicked or Enter pressed, then it tries to start the operation
and only if successful, allows the dialog to close.  Hopefully this will
give you enough to get going with yours...

public class QueryRecordingDialog
	extends DialogStateListener.Adapter
...
	    dialog.getDialogStateListeners().add(this);
...
	    dialog.open(mainWindow, this);
...
	/**
	 * So, the user wants to close the dialog (either with "Start"
or
	 * "Cancel").  If "Start", then ensure we have a non-empty file
	 * name and that we can start recording to it before allowing
	 * the close.
	 */
	@Override
	public Vote previewDialogClose(Dialog dialog, boolean result) {
	    if (result == false)
		return Vote.APPROVE;
	    // Validate the fields before going on
	    String fileName = fileNameInput.getText().trim();
	    if (fileName.isEmpty())
		return Vote.DENY;
	    QueryRecording qr = new QueryRecording(kindChoice,
legacyModeCheck.isSelected());
	    try {
		qr.startRecording(fileName, appendCheck.isSelected());
		appObj.setRecordButtonState(true);
	    }
	    catch (Exception e) {
		MessageDialog.showException(dialog, e);
		return Vote.DENY;
	    }
	    return Vote.APPROVE;
	}

~Roger

-----Original Message-----
From: mattux [mailto:mattux@hotmail.fr] 
Sent: Monday, May 21, 2012 1:46 AM
To: user@pivot.apache.org
Subject: RE: Prompt: override option click event

Hi Roger,

I think your idea of a SheetStateListener is what I was looking for!!

If you have some samples in hand, I wouldn't mind you sharing them.
Otherwise, don't worry, I will try and find out.

Many thanks for your help, much appreciated! Just started on Pivot so it
has
been a nice help!

Regards,
Matt

--
View this message in context:
http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-cl
ick-event-tp3999325p4004527.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: Prompt: override option click event

Posted by mattux <ma...@hotmail.fr>.
Hi Roger,

I think your idea of a SheetStateListener is what I was looking for!!

If you have some samples in hand, I wouldn't mind you sharing them.
Otherwise, don't worry, I will try and find out.

Many thanks for your help, much appreciated! Just started on Pivot so it has
been a nice help!

Regards,
Matt

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4004527.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: Prompt: override option click event

Posted by "Roger L. Whitcomb" <Ro...@actian.com>.
Hi Matt,
	In that case, I would simply install a SheetStateListener on the
Prompt (DialogStateListener if you're using an Alert) and do whatever
you need to do in the "previewSheetClose" method and return the
appropriate vote.  If you can get done what you need to, then return
Vote.APPROVE, if not and the user needs to try again (maybe the password
is wrong), then return Vote.DENY, and if the action is on its way to
succeeding (but not quite there yet) then return Vote.DEFER.
	Using this technique, you can control whether the action will
really close the Prompt or not.
	If you need some examples, I can dig some up from our code -- I
use this in multiple places and it works quite well.

HTH,
~Roger

-----Original Message-----
From: mattux [mailto:mattux@hotmail.fr] 
Sent: Monday, May 21, 2012 1:13 AM
To: user@pivot.apache.org
Subject: Re: Prompt: override option click event

Hi Roger,

Thanks for your answer.

This is similar to what I was originally doing with prompts after moving
from the selectionindexchanged event:
		prompt.open(this, new SheetCloseListener() {
			public void sheetClosed(Sheet sheet) {

The only problem I have with this is that it will still close the alert
or
prompt. I would like it to remain open until I programmatically decide
to
close it.

I know one could say that I'm not trying to use the prompt in a
conditional
way in that case :)

Thanks,
Matt


--
View this message in context:
http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-cl
ick-event-tp3999325p4004492.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: Prompt: override option click event

Posted by mattux <ma...@hotmail.fr>.
Hi Roger,

Thanks for your answer.

This is similar to what I was originally doing with prompts after moving
from the selectionindexchanged event:
		prompt.open(this, new SheetCloseListener() {
			public void sheetClosed(Sheet sheet) {

The only problem I have with this is that it will still close the alert or
prompt. I would like it to remain open until I programmatically decide to
close it.

I know one could say that I'm not trying to use the prompt in a conditional
way in that case :)

Thanks,
Matt


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4004492.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: Prompt: override option click event

Posted by Roger and Beth Whitcomb <Ro...@rbwhitcomb.com>.
Hi Matt,
     What I have done with Prompts (and Alerts) is attach a close 
listener that gets invoked when one or the other button gets pressed 
which closes the dialog, and then invoke the action in that listener.  
That way you don't have to worry about skins.  Something like this:
         Alert alert = new Alert(MessageType.QUESTION,
         Intl.getString("optionsDialog.warnReset"), null);
         Alert.OptionSequence options = alert.getOptions();
         options.remove(0, 1);
         options.add(Intl.getString("saveFilePrompt.yes"));
         options.add(Intl.getString("saveFilePrompt.no"));
         alert.setSelectedOptionIndex(0);
         alert.open(getWindow(), new DialogCloseListener() {
         @Override
         public void dialogClosed(Dialog dialog, boolean modal) {
             Alert alert = (Alert)dialog;
             int index = alert.getSelectedOptionIndex();
             switch (index) {
             case 0:        // Yes
... do your work here ...
                 break;
             case 1:        // No
                 break;
             }
         }
         });

Though this is an Alert, the same principle applies for Prompt as well.

HTH,
~Roger

On 5/18/12 10:55 AM, mattux wrote:
> Ok so to add a bit to this:
>
> I tried to override the TerraPromptSkin (TerraExtendedPromptSkin.java) class
> so I could have some control on the buttonPressed event.
> Here is an extract of the mock class:
>
> 	private ButtonPressListener optionButtonPressListener = new
> ButtonPressListener() {
> 		public void buttonPressed(Button button) {
> 			int optionIndex = optionButtonBoxPane.indexOf(button);
> 			if (optionIndex>= 0) {
> 				//Prompt prompt = (Prompt)getComponent();
> 				//prompt.setSelectedOptionIndex(optionIndex);
> 				//prompt.close(true);
> 			}
> 		}
> 	};
>
> 	@Override
> 	public void install(Component component) {
> 		ExtendedPrompt prompt = (ExtendedPrompt) component;
> 		// We backup the options
> 		ArrayList options = new ArrayList(prompt.getOptions());
> 		// We reset the options so the subclass doesn't create the buttons that we
> want to override
> 		prompt.getOptions().remove(0, options.getLength());
> 		super.install(component);
> 		BXMLSerializer bxmlSerializer = new BXMLSerializer();
> 		try {
> 			bxmlSerializer.readObject(TerraPromptSkin.class,
> "terra_prompt_skin.bxml");
> 		} catch(Exception exception) {
> 			throw new RuntimeException(exception);
> 		}
> 		optionButtonBoxPane = (BoxPane)
> bxmlSerializer.getNamespace().get("optionButtonBoxPane");
> 		// We set the options back and create buttons with our custom listeners
> 		prompt.setOptions(options);
> 		for (Object option : options) {
> 			PushButton optionButton = new PushButton(option);
> 			optionButton.setStyleName(TerraPromptSkin.class.getPackage().getName() +
> "." + TerraTheme.COMMAND_BUTTON_STYLE);
> 			for (ButtonPressListener listener :
> optionButton.getButtonPressListeners()) {
> 				optionButton.getButtonPressListeners().remove(listener);
> 			}
> 			optionButton.getButtonPressListeners().add(optionButtonPressListener);
> 			optionButtonBoxPane.add(optionButton);
> 		}
> 	}
>
> When putting a breakpoint in both the listener (at int optionIndex =
> optionButtonBoxPane.indexOf(button);) and the install function (at
> ExtendedPrompt prompt = (ExtendedPrompt) component;). The code definitely
> goes through my install function. However, if I click on the prompt button,
> it doesn't stop at my listener breakpoint and the prompt just closes.
>
> Any ideas on what I'm doing wrong here would be much appreciated.
>
> Regards,
> Matt
>
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4001268.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>
>

Re: Prompt: override option click event

Posted by mattux <ma...@hotmail.fr>.
Hi Sandro,

I fully understand, no worries. Hope 2.0.2 goes well.

Just for further info for when you have some free time to look at it, if my
class is an exact copy of the one I was trying to extend, it works (public
class TerraExtendedPromptSkin extends TerraSheetSkin implements
PromptListener {). But ideally, I would prefer to extend TerraPromptSkin and
use the existing code instead of having to do a copy.

Regards,
Matt

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4001525.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: Prompt: override option click event

Posted by Sandro Martini <sa...@gmail.com>.
Hi,
sorry for no mail today but we are in a vote for the 2.0.2 release these
days (and we are doing many tests on it, to ensure it's ok) ... so we have
to update next week.

Bye,
Sandro
 Il giorno 18/mag/2012 19:55, "mattux" <ma...@hotmail.fr> ha scritto:

> Ok so to add a bit to this:
>
> I tried to override the TerraPromptSkin (TerraExtendedPromptSkin.java)
> class
> so I could have some control on the buttonPressed event.
> Here is an extract of the mock class:
>
>        private ButtonPressListener optionButtonPressListener = new
> ButtonPressListener() {
>                public void buttonPressed(Button button) {
>                        int optionIndex =
> optionButtonBoxPane.indexOf(button);
>                        if (optionIndex >= 0) {
>                                //Prompt prompt = (Prompt)getComponent();
>
>  //prompt.setSelectedOptionIndex(optionIndex);
>                                //prompt.close(true);
>                        }
>                }
>        };
>
>        @Override
>        public void install(Component component) {
>                ExtendedPrompt prompt = (ExtendedPrompt) component;
>                // We backup the options
>                ArrayList options = new ArrayList(prompt.getOptions());
>                // We reset the options so the subclass doesn't create the
> buttons that we
> want to override
>                prompt.getOptions().remove(0, options.getLength());
>                super.install(component);
>                BXMLSerializer bxmlSerializer = new BXMLSerializer();
>                try {
>                        bxmlSerializer.readObject(TerraPromptSkin.class,
> "terra_prompt_skin.bxml");
>                } catch(Exception exception) {
>                        throw new RuntimeException(exception);
>                }
>                optionButtonBoxPane = (BoxPane)
> bxmlSerializer.getNamespace().get("optionButtonBoxPane");
>                // We set the options back and create buttons with our
> custom listeners
>                prompt.setOptions(options);
>                for (Object option : options) {
>                        PushButton optionButton = new PushButton(option);
>
>  optionButton.setStyleName(TerraPromptSkin.class.getPackage().getName() +
> "." + TerraTheme.COMMAND_BUTTON_STYLE);
>                        for (ButtonPressListener listener :
> optionButton.getButtonPressListeners()) {
>
>  optionButton.getButtonPressListeners().remove(listener);
>                        }
>
>  optionButton.getButtonPressListeners().add(optionButtonPressListener);
>                        optionButtonBoxPane.add(optionButton);
>                }
>        }
>
> When putting a breakpoint in both the listener (at int optionIndex =
> optionButtonBoxPane.indexOf(button);) and the install function (at
> ExtendedPrompt prompt = (ExtendedPrompt) component;). The code definitely
> goes through my install function. However, if I click on the prompt button,
> it doesn't stop at my listener breakpoint and the prompt just closes.
>
> Any ideas on what I'm doing wrong here would be much appreciated.
>
> Regards,
> Matt
>
> --
> View this message in context:
> http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4001268.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>

Re: Prompt: override option click event

Posted by mattux <ma...@hotmail.fr>.
Ok so to add a bit to this:

I tried to override the TerraPromptSkin (TerraExtendedPromptSkin.java) class
so I could have some control on the buttonPressed event.
Here is an extract of the mock class:

	private ButtonPressListener optionButtonPressListener = new
ButtonPressListener() {
		public void buttonPressed(Button button) {
			int optionIndex = optionButtonBoxPane.indexOf(button);
			if (optionIndex >= 0) {
				//Prompt prompt = (Prompt)getComponent();
				//prompt.setSelectedOptionIndex(optionIndex);
				//prompt.close(true);
			}
		}
	};

	@Override
	public void install(Component component) {
		ExtendedPrompt prompt = (ExtendedPrompt) component;
		// We backup the options
		ArrayList options = new ArrayList(prompt.getOptions());
		// We reset the options so the subclass doesn't create the buttons that we
want to override
		prompt.getOptions().remove(0, options.getLength());
		super.install(component);
		BXMLSerializer bxmlSerializer = new BXMLSerializer();
		try {
			bxmlSerializer.readObject(TerraPromptSkin.class,
"terra_prompt_skin.bxml");
		} catch(Exception exception) {
			throw new RuntimeException(exception);
		}
		optionButtonBoxPane = (BoxPane)
bxmlSerializer.getNamespace().get("optionButtonBoxPane");
		// We set the options back and create buttons with our custom listeners
		prompt.setOptions(options);
		for (Object option : options) {
			PushButton optionButton = new PushButton(option);
			optionButton.setStyleName(TerraPromptSkin.class.getPackage().getName() +
"." + TerraTheme.COMMAND_BUTTON_STYLE);
			for (ButtonPressListener listener :
optionButton.getButtonPressListeners()) {
				optionButton.getButtonPressListeners().remove(listener);
			}
			optionButton.getButtonPressListeners().add(optionButtonPressListener);
			optionButtonBoxPane.add(optionButton);
		}
	}

When putting a breakpoint in both the listener (at int optionIndex =
optionButtonBoxPane.indexOf(button);) and the install function (at
ExtendedPrompt prompt = (ExtendedPrompt) component;). The code definitely
goes through my install function. However, if I click on the prompt button,
it doesn't stop at my listener breakpoint and the prompt just closes.

Any ideas on what I'm doing wrong here would be much appreciated.

Regards,
Matt

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Prompt-override-option-click-event-tp3999325p4001268.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.