0

How to close all overlapped Windows

asked 2009-09-23 16:39:09 +0800

dastultz gravatar image dastultz
797 8

Hello, before opening an overlapped window, I want to close any other overlapped, visible Windows. This doesn't seem to work. Clicking "go" opens window after window. Page.getRoots() doesn't seem to find the existing overlapped windows. How do I find the existing overlapped windows?

<zk>
	<button onClick="click()" label="Go" />
	<zscript><![CDATA[
		
	void click() {
		System.out.println("Click");
		Window window = new Window("Hey", "normal", true);
		window.setPosition("center,center");
		window.setPage(page);
		window.setVisible(false);
		window.appendChild(new Label("Window"));
		detachAllOverlappedWindows(page);
		window.doOverlapped();
	}
	
	void detachAllOverlappedWindows(Page page) {
		Iterator iter = page.getRoots().iterator();
		if (iter.hasNext()) {
			Component comp = (Component) iter.next();
			System.out.println("root: " + comp.getUuid());
			detachAllOverlappedWindows(comp);
		}
	}
		
	void detachAllOverlappedWindows(Component parent) {
		System.out.println("Checking parent " + parent);
		boolean detachedWindow = false;
		if (parent instanceof Window) {
			Window window = (Window) parent;
			System.out.println("Found window " + window.getUuid());
			System.out.println(window.inOverlapped() + ":" + window.isVisible());
			if (window.inOverlapped() && window.isVisible()) {
				window.detach();
				detachedWindow = true;
				System.out.println("Detached");
			}
		} 
		if (! detachedWindow) {
			Iterator iter = parent.getChildren().iterator();
			while (iter.hasNext()) {
				Component child = (Component) iter.next();
				detachAllOverlappedWindows(child);
			}
		}
	}
	]]>
	</zscript>
</zk>
			

Thanks.

/Daryl

delete flag offensive retag edit

14 Replies

Sort by ยป oldest newest

answered 2009-09-23 19:37:12 +0800

dastultz gravatar image dastultz
797 8

I have determined there is a bug in page.getRoots().iterator().
https://sourceforge.net/tracker/?func=detail&aid=2865208&group_id=152762&atid=785191

/Daryl

link publish delete flag offensive edit

answered 2009-09-25 06:36:15 +0800

tomyeh gravatar image tomyeh
610 1 3
http://blog.zkoss.org ZK Team

Hi Daryl,

The first root component is the Go button. That is why you cannot remove the previous overlapped window. Instead of checking only the first root component, you have to iterate thru it as follows.

	void detachAllOverlappedWindows(Page page) {
		Iterator iter = page.getRoots().iterator();
		for (;iter.hasNext();) {
			Component comp = (Component) iter.next();
			if (comp instanceof Window) {
				System.out.println("root: " + comp);
				detachAllOverlappedWindows(comp);
			}
		}
	}

link publish delete flag offensive edit

answered 2009-09-25 12:01:44 +0800

dastultz gravatar image dastultz
797 8

AuGH!!!! I used "if" instead of "while"! Sorry about that.

On a side note, why in the world would you use:

for (;iter.hasNext();) {

instead of
while (iter.hasNext()) {

???

/Daryl

link publish delete flag offensive edit

answered 2009-09-25 13:16:07 +0800

YamilBracho gravatar image YamilBracho
1722 2

Betterr

for (Iterator iter = page.getRoots().iterator();iter.hasNext();) {
   Component comp = (Component) iter.next();
   if (comp instanceof Window) {
     System.out.println("root: " + comp);
     detachAllOverlappedWindows(comp);
   }
}


link publish delete flag offensive edit

answered 2009-09-25 14:51:28 +0800

dastultz gravatar image dastultz
797 8

<code-style-rant>
Eww! No better, no. A for loop has three control parts. If you don't need them all, don't use a for loop. Readability is more important than saving lines of code in some crafy way.
</code-style-rant>

link publish delete flag offensive edit

answered 2009-09-25 18:05:05 +0800

YamilBracho gravatar image YamilBracho
1722 2

Use a "for" with an iterator is so common that Joshua Bloch (Effective Java) discuss this as "common idiom".
Also it could be better idea to use a for-each construction...

link publish delete flag offensive edit

answered 2009-09-28 12:27:16 +0800

dastultz gravatar image dastultz
797 8

Just because and idea is popular doesn't make it the best. I'm not against using an iterator in a for loop, I against using a for loop when you don't need all three parts.

For each is nice, but so many times I write a for each only to change it to a while because I need some other test to exit the loop or the collection I want to loop over doesn't provide a generic type (I guess it depends where you want to put your cast).

/Daryl

link publish delete flag offensive edit

answered 2009-09-28 13:09:36 +0800

YamilBracho gravatar image YamilBracho
1722 2

Totally agree with you("Just because and idea is popular doesn't make it the best") but this book gives some good habits and tips. for example in page 233, you can read:

For example, here is the preferred idiom for iterating over a collection (Item 46):
// Preferred idiom for iterating over a collection
for (Element e : c) {
doSomething(e);
}

Before release 1.5, this was the preferred idiom (and it still has valid uses):
// No for-each loop or generics before release 1.5
for (Iterator i = c.iterator(); i.hasNext(); ) {
doSomething((Element) i.next());
}

I heartly recommed Joshua Bloch' s Effective Java
Sometimes it is a good idea see the picture from another side to discover interesting things...

link publish delete flag offensive edit

answered 2009-09-28 17:53:34 +0800

dastultz gravatar image dastultz
797 8

JB says "this was the preferred idiom". My question is, "preferred by whom?" Sure, preferred by JB, I guess. Ultimately I'm talking about *my* preferred idiom, of course. Many times, coding style is arbitrary (like where to put the opening brace). I'd like to understand why it's better than a while loop. Maybe I'll have to buy the book... The only advantage I see is that you declare the Iterator within the loop which means it's local in scope whereas I have to do this once in while:

Iterator<A> iter = aList.iterator();
while (iter.hasNext()) {
...
}

Iterator<B> iter2 = bList.iterator();
...

I just find the empty component of the for loop "impure" and to a certain degree awkward to read.

/Daryl

link publish delete flag offensive edit

answered 2009-09-28 18:17:07 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

I'm with Daryl on this one. JB seems to make the statement as if it's a matter of fact when it's really a subjective issue.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-09-23 16:39:09 +0800

Seen: 338 times

Last updated: Sep 29 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More