/*******************************************************************************
* Copyright 2011 Google Inc. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* 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 com.google.gdt.eclipse.designer.gwtext.model.widgets;
import org.eclipse.wb.core.model.JavaInfo;
import org.eclipse.wb.core.model.ObjectInfo;
import org.eclipse.wb.core.model.association.AssociationObject;
import org.eclipse.wb.core.model.association.AssociationObjects;
import org.eclipse.wb.core.model.broadcast.JavaEventListener;
import org.eclipse.wb.core.model.broadcast.ObjectInfoDelete;
import org.eclipse.wb.internal.core.model.JavaInfoUtils;
import org.eclipse.wb.internal.core.model.creation.ConstructorCreationSupport;
import org.eclipse.wb.internal.core.model.creation.CreationSupport;
import org.eclipse.wb.internal.core.model.description.ComponentDescription;
import org.eclipse.wb.internal.core.utils.GenericsUtils;
import org.eclipse.wb.internal.core.utils.ast.AstEditor;
import java.util.List;
/**
* Model for <code>TreePanel</code>.
*
* @author scheglov_ke
* @coverage GWTExt.model
*/
public class TreePanelInfo extends PanelInfo {
private final TreePanelInfo m_this = this;
////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
////////////////////////////////////////////////////////////////////////////
public TreePanelInfo(AstEditor editor,
ComponentDescription description,
CreationSupport creationSupport) throws Exception {
super(editor, description, creationSupport);
ensureRootNode();
}
////////////////////////////////////////////////////////////////////////////
//
// Access
//
////////////////////////////////////////////////////////////////////////////
public TreeNodeInfo getRootNode() {
List<TreeNodeInfo> nodes = getChildren(TreeNodeInfo.class);
return GenericsUtils.getFirstOrNull(nodes);
}
////////////////////////////////////////////////////////////////////////////
//
// Listeners
//
////////////////////////////////////////////////////////////////////////////
private void ensureRootNode() {
addBroadcastListener(new ObjectInfoDelete() {
@Override
public void after(ObjectInfo parent, ObjectInfo child) throws Exception {
if (parent == m_this && !m_this.isDeleting() && getRootNode() == null) {
addRootNode();
}
}
});
addBroadcastListener(new JavaEventListener() {
@Override
public void addAfter(JavaInfo parent, JavaInfo child) throws Exception {
if (child == m_this) {
addRootNode();
}
}
});
}
private void addRootNode() throws Exception {
TreeNodeInfo rootNode =
(TreeNodeInfo) JavaInfoUtils.createJavaInfo(
getEditor(),
"com.gwtext.client.widgets.tree.TreeNode",
new ConstructorCreationSupport());
AssociationObject association =
AssociationObjects.invocationChild("%parent%.setRootNode(%child%)", true);
JavaInfoUtils.add(rootNode, association, m_this, null);
rootNode.getPropertyByTitle("text").setValue("(Root)");
}
}