You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by ccp999 <cc...@hotmail.com> on 2012/03/24 11:10:09 UTC

How to use windowStateListener?

Hi,

I want to do a check before a frame is closed, so that for unsaved content,
I can veto the closing.

and here is my code:

......

      private Vote vote = Vote.APPROVE;

	@Override
	public void initialize(Map<String, Object> namespace, URL location,
			Resources resources) {

		this.getWindowStateListeners().add(new WindowStateListener.Adapter() {

			@Override
			public Vote previewWindowClose(Window window) {

				if (Compose.this.isEdited()) {
					closeConfirmPrompt.open(window, new SheetCloseListener() {

						@Override
						public void sheetClosed(Sheet sheet) {
							if (closeConfirmPrompt.getResult()
									&& closeConfirmPrompt
											.getSelectedOptionIndex() == 1) {
								vote = Vote.APPROVE;
							} else
								vote = Vote.DENY;
						}
					});
					return vote;
				} else
					return Vote.APPROVE;

			}

		});

However, the result is the window is closed before the prompt show up.
why prompt is bypassed ?

Brendan


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/How-to-use-windowStateListener-tp3853550p3853550.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: How to use windowStateListener?

Posted by Noel Grandin <no...@gmail.com>.
Yes, that's a good idea.

On 2012-03-30 13:22, Sandro Martini wrote:
> Hi all,
>
>> Sorry, Prompt#open() is not a synchronous call, it schedules an event to open the window.
> Noel, do you think could be useful to write it in Javadoc for that
> method (could be useful even fore others in future) ?
> And maybe even in other places ... if someone has suggestions, tell me.
>
> Bye,
> Sandro

Re: How to use windowStateListener?

Posted by Sandro Martini <sa...@gmail.com>.
Hi all,

> Sorry, Prompt#open() is not a synchronous call, it schedules an event to open the window.
Noel, do you think could be useful to write it in Javadoc for that
method (could be useful even fore others in future) ?
And maybe even in other places ... if someone has suggestions, tell me.

Bye,
Sandro

Re: How to use windowStateListener?

Posted by Noel Grandin <no...@gmail.com>.
Sorry, Prompt#open() is not a synchronous call, it schedules an event to 
open the window.

On 2012-03-29 04:06, ccp999 wrote:
> Thanks Chris,
>
> I implemented your rework into my program and it works.
>
> I just wonder if this behavior is expected. I meant the behavior of carry on
> closing the window without waiting the opening of prompt.
>
> Brendan
>
>
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/How-to-use-windowStateListener-tp3853550p3866537.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: How to use windowStateListener?

Posted by ccp999 <cc...@hotmail.com>.
Thanks Chris,

I implemented your rework into my program and it works.

I just wonder if this behavior is expected. I meant the behavior of carry on
closing the window without waiting the opening of prompt.

Brendan


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/How-to-use-windowStateListener-tp3853550p3866537.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: How to use windowStateListener?

Posted by Chris Bartlett <cb...@gmail.com>.
Brendan,

Sorry for the slow reply.  I have only just been able to download your
sample (which was exactly what was needed).

The problem is that your WindowStateListener is opening a Prompt, but
the program execution doesn't wait for the user to click on one of the
buttons.  It just carries on and returns Vote.APPROVE, which means
that the Frame gets closed.

I have attached a slightly reworked TestFrame class that opens the
Prompt and returns a Vote.DENY to stop the Frame from closing.  Any
time later, if the 'Yes' button is pressed, it will explicitly ask for
the Frame to be closed after setting the return value to Vote.APPROVE.

public class TestFrame extends Frame implements Bindable {

	@BXML
	private Prompt closeConfirmPrompt;

	private Vote vote = Vote.DENY;

	@Override
	public void initialize(Map<String, Object> namespace, URL location,
			Resources resources) {

		this.getWindowStateListeners().add(new WindowStateListener.Adapter() {

			@Override
			public Vote previewWindowClose(final Window window) {
				if (vote == Vote.DENY) {
					closeConfirmPrompt.open(window, new SheetCloseListener() {
						@Override
						public void sheetClosed(Sheet sheet) {
							if (closeConfirmPrompt.getResult() &&
closeConfirmPrompt.getSelectedOptionIndex() == 1) {
								TestFrame.this.vote = Vote.APPROVE;
								window.close();
							}
						}
					});
				}
				return vote;
			}

		});
	}
}


I can't help thinking there is a tidier way of doing this, but my
brain is a bit rusty because I have been away from Pivot for a couple
of months.  Anyway, I hope that helps.

Chris

On 24 March 2012 11:41, Brendan cheng <cc...@hotmail.com> wrote:
> Hi Chris,
>
> I just made a small completed program for you.
> Please try to close the frames and I added listener to prompt for
> confirmation before it close but didn't work.
>
> Brendan
>
>
>
>> Date: Sat, 24 Mar 2012 10:28:12 +0000
>> Subject: Re: How to use windowStateListener?
>> From: cbartlett.x@gmail.com
>> To: user@pivot.apache.org
>
>>
>> Could you please post a complete, small example of this?
>>
>> How is the Frame closed? Are you talking about closing the
>> org.apache.pivot.wtk.Frame or the operating system window (which is a
>> java.awt.Frame I think) in a desktop application?
>>
>> Chris
>>
>> On 24 March 2012 10:10, ccp999 <cc...@hotmail.com> wrote:
>> > Hi,
>> >
>> > I want to do a check before a frame is closed, so that for unsaved
>> > content,
>> > I c an veto the closing.
>
>> >
>> > and here is my code:
>> >
>> > ......
>> >
>> > │ │ │private Vote vote = Vote.APPROVE;
>> >
>> > │ │ │ │@Override
>> > │ │ │ │public void initialize(Map<String, Object> namespace, URL
>> > location,
>> > │ │ │ │ │ │ │ │ │ │ │ │Resources resources) {
>> >
>> > │ │ │ │ │ │ │ │this.getWindowStateListeners().add(new
>> > WindowStateListener.Adapter() {
>> >
>> > │ │ │ │ │ │ │ │ │ │ │ │@Override
>> > │ │ │ │ │ │ │ │ │ │ │ │public Vote previewWindowClose(Window window) {
>> >
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │if (Compose.this.isEdited()) {
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │closeConfirmPrompt.open(window,
>> > new SheetCloseListener() {
>> >
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ � �│ │@Override
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │public void
>> > sheetClosed(Sheet sheet) {
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │if
>> > (closeConfirmPrompt.getResult()
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
>> > │&& closeConfirmPrompt
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
>> > │ │ │ │ │ │ │ │.getSelectedOptionIndex() == 1) {
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │vote =
>> > Vote.APPROVE;
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │} else
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │vote =
>> > Vote.DENY;
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │}
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │});
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │return vote;
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │} else
>> > │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │return Vote.APPROVE;
>> >
>> > │ │ │ │ │ │ │ │ │ │ │ │}
>> >
>> > │ │ │ │ │ │ │ │});
>> >
>> > However, the result is the window is closed before the prompt show up.
>> > why prompt is bypassed ?
>> >
>> > Brendan
>> >
>> >
>> > --
>> > View this message in context:
>> > http://apache-pivot-users.399431.n3.nabble.com/How-to-use-windowStateListener-tp3853550p3853550.html
>> > Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

RE: How to use windowStateListener?

Posted by Brendan cheng <cc...@hotmail.com>.
Hi Chris,
I just made a small completed program for you.Please try to close the frames and I added listener to prompt for confirmation before it close but didn't work.
Brendan


> Date: Sat, 24 Mar 2012 10:28:12 +0000
> Subject: Re: How to use windowStateListener?
> From: cbartlett.x@gmail.com
> To: user@pivot.apache.org
> 
> Could you please post a complete, small example of this?
> 
> How is the Frame closed?  Are you talking about closing the
> org.apache.pivot.wtk.Frame or the operating system window (which is a
> java.awt.Frame I think) in a desktop application?
> 
> Chris
> 
> On 24 March 2012 10:10, ccp999 <cc...@hotmail.com> wrote:
> > Hi,
> >
> > I want to do a check before a frame is closed, so that for unsaved content,
> > I can veto the closing.
> >
> > and here is my code:
> >
> > ......
> >
> > á á áprivate Vote vote = Vote.APPROVE;
> >
> > á á á á@Override
> > á á á ápublic void initialize(Map<String, Object> namespace, URL location,
> > á á á á á á á á á á á áResources resources) {
> >
> > á á á á á á á áthis.getWindowStateListeners().add(new WindowStateListener.Adapter() {
> >
> > á á á á á á á á á á á á@Override
> > á á á á á á á á á á á ápublic Vote previewWindowClose(Window window) {
> >
> > á á á á á á á á á á á á á á á áif (Compose.this.isEdited()) {
> > á á á á á á á á á á á á á á á á á á á ácloseConfirmPrompt.open(window, new SheetCloseListener() {
> >
> > á á á á á á á á á á á á á á á á á á á á á á á á@Override
> > á á á á á á á á á á á á á á á á á á á á á á á ápublic void sheetClosed(Sheet sheet) {
> > á á á á á á á á á á á á á á á á á á á á á á á á á á á áif (closeConfirmPrompt.getResult()
> > á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á&& closeConfirmPrompt
> > á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á.getSelectedOptionIndex() == 1) {
> > á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á ávote = Vote.APPROVE;
> > á á á á á á á á á á á á á á á á á á á á á á á á á á á á} else
> > á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á á ávote = Vote.DENY;
> > á á á á á á á á á á á á á á á á á á á á á á á á}
> > á á á á á á á á á á á á á á á á á á á á});
> > á á á á á á á á á á á á á á á á á á á áreturn vote;
> > á á á á á á á á á á á á á á á á} else
> > á á á á á á á á á á á á á á á á á á á áreturn Vote.APPROVE;
> >
> > á á á á á á á á á á á á}
> >
> > á á á á á á á á});
> >
> > However, the result is the window is closed before the prompt show up.
> > why prompt is bypassed ?
> >
> > Brendan
> >
> >
> > --
> > View this message in context: http://apache-pivot-users.399431.n3.nabble.com/How-to-use-windowStateListener-tp3853550p3853550.html
> > Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
 		 	   		  

Re: How to use windowStateListener?

Posted by Chris Bartlett <cb...@gmail.com>.
Could you please post a complete, small example of this?

How is the Frame closed?  Are you talking about closing the
org.apache.pivot.wtk.Frame or the operating system window (which is a
java.awt.Frame I think) in a desktop application?

Chris

On 24 March 2012 10:10, ccp999 <cc...@hotmail.com> wrote:
> Hi,
>
> I want to do a check before a frame is closed, so that for unsaved content,
> I can veto the closing.
>
> and here is my code:
>
> ......
>
>      private Vote vote = Vote.APPROVE;
>
>        @Override
>        public void initialize(Map<String, Object> namespace, URL location,
>                        Resources resources) {
>
>                this.getWindowStateListeners().add(new WindowStateListener.Adapter() {
>
>                        @Override
>                        public Vote previewWindowClose(Window window) {
>
>                                if (Compose.this.isEdited()) {
>                                        closeConfirmPrompt.open(window, new SheetCloseListener() {
>
>                                                @Override
>                                                public void sheetClosed(Sheet sheet) {
>                                                        if (closeConfirmPrompt.getResult()
>                                                                        && closeConfirmPrompt
>                                                                                        .getSelectedOptionIndex() == 1) {
>                                                                vote = Vote.APPROVE;
>                                                        } else
>                                                                vote = Vote.DENY;
>                                                }
>                                        });
>                                        return vote;
>                                } else
>                                        return Vote.APPROVE;
>
>                        }
>
>                });
>
> However, the result is the window is closed before the prompt show up.
> why prompt is bypassed ?
>
> Brendan
>
>
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/How-to-use-windowStateListener-tp3853550p3853550.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.