-
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
As you said is matter of taste. However I always collect tips from here or from there or books like JB's (By the way one of java best sellers) and he discuss what construction is better and why, or what problems you would get, etc
The three part in a for-loop are all optional. you can write something like :
for (;;) {} for a endless loop and write too
while (1=1) to get the same result
however (as you say) I like the first one because I am lazy and I do not like to type too much but I am sure pther people prefer the second one because it describe what you try to do (an endless loop)
PS: I am not associate with JB and do not sell books...! :-)
I don't have a problem with some people putting the brace at the beginning of the line vs the end of the previous, that's subjective - I like it my way. In the case of the for loop with missing component, I need a good reason to do it that way, which I'm open to. I don't like the statement "this is the preferred way" with no explanation. I can't say that JB can't defend this, because I haven't read the book...
/Daryl
Yes, you right. JB gives us some tips and you can adopt in yopu code if they makes sense in your coding style.
JB also discuss the code you mention above and tells you what could be wrong with this construction.
I agree with you codigng style is matter of personal taste but looking for another approaches does not hurt you at all...
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.
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
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...
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
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...
<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>
Asked: 2009-09-23 16:39:09 +0800
Seen: 338 times
Last updated: Sep 29 '09