-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
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); } } }
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); } }
<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>
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...
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
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...
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
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.
Asked: 2009-09-23 16:39:09 +0800
Seen: 338 times
Last updated: Sep 29 '09