/*
 * Copyright 2006 Mask@ Project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.maskat.ide.gef.figure;

import java.util.Iterator;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Layer;

public class LayoutFigure extends Layer {

	/**
	 * 
	 * @param name
	 * @param firstOnly
	 *            true:条件に当たるfigure一個があると操作が止める
	 * @return 条件に当たるfigureがあるかどうかフラグ
	 */
	public boolean switchNamedComponentVisibility(String name, boolean firstOnly) {
		return switchVisibility(this, name, firstOnly);
	}

	private boolean switchVisibility(IFigure figure, String name,
			boolean firstOnly) {
		if (name == null) {
			throw new IllegalArgumentException("parameter name is null.");
		}
		boolean found = false;
		if (figure.getChildren() != null) {
			for (Iterator it = figure.getChildren().iterator(); it != null
					&& it.hasNext();) {
				IFigure child = (IFigure) it.next();
				if (child instanceof INamedFigure) {
					if (name.equals(((INamedFigure) child).getName())) {
						child.setVisible(!child.isVisible());
						if (firstOnly)
							return true;
					}
				}
				found = switchVisibility(child, name, firstOnly);
				if (found && firstOnly)
					return true;
			}
		}
		return found;
	}

}