/*******************************************************************************
* 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.model.widgets.panels;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gdt.eclipse.designer.model.widgets.WidgetInfo;
import com.google.gdt.eclipse.designer.model.widgets.support.GwtState;
import org.eclipse.wb.core.model.broadcast.JavaInfoChildBeforeAssociation;
import org.eclipse.wb.draw2d.geometry.Rectangle;
import org.eclipse.wb.internal.core.model.TopBoundsSupport;
import org.eclipse.wb.internal.core.model.clipboard.ClipboardCommand;
import org.eclipse.wb.internal.core.model.creation.CreationSupport;
import org.eclipse.wb.internal.core.model.description.ComponentDescription;
import org.eclipse.wb.internal.core.model.util.PropertyUtils;
import org.eclipse.wb.internal.core.model.util.StackContainerSupport;
import org.eclipse.wb.internal.core.utils.ast.AstEditor;
import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils;
import java.util.List;
import java.util.Map;
/**
* Model for <code>com.google.gwt.user.client.ui.TabLayoutPanel</code>.
*
* @author scheglov_ke
* @coverage gwt.model
*/
public final class TabLayoutPanelInfo extends ComplexPanelInfo {
private final StackContainerSupport<WidgetInfo> m_stackContainer =
new StackContainerSupport<WidgetInfo>(this) {
@Override
protected List<WidgetInfo> getChildren() {
return getChildrenWidgets();
}
};
////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
////////////////////////////////////////////////////////////////////////////
public TabLayoutPanelInfo(AstEditor editor,
ComponentDescription description,
CreationSupport creationSupport) throws Exception {
super(editor, description, creationSupport);
addBroadcastListener(new JavaInfoChildBeforeAssociation(this));
}
////////////////////////////////////////////////////////////////////////////
//
// Hierarchy
//
////////////////////////////////////////////////////////////////////////////
@Override
protected TopBoundsSupport createTopBoundsSupport() {
return new RootLayoutPanelTopBoundsSupport(this);
}
////////////////////////////////////////////////////////////////////////////
//
// Access
//
////////////////////////////////////////////////////////////////////////////
/**
* @return the active (visible) {@link WidgetInfo}.
*/
public WidgetInfo getActiveWidget() {
return m_stackContainer.getActive();
}
////////////////////////////////////////////////////////////////////////////
//
// Refresh
//
////////////////////////////////////////////////////////////////////////////
@Override
protected void refresh_afterCreate() throws Exception {
super.refresh_afterCreate();
showActiveWidget();
}
private void showActiveWidget() throws Exception {
WidgetInfo active = getActiveWidget();
if (active != null) {
ReflectionUtils.invokeMethod(
getObject(),
"selectTab(com.google.gwt.user.client.ui.Widget)",
active.getObject());
}
}
@Override
protected void refresh_fetch() throws Exception {
super.refresh_fetch();
prepareWidgetHandles();
}
////////////////////////////////////////////////////////////////////////////
//
// Clipboard
//
////////////////////////////////////////////////////////////////////////////
@Override
protected void clipboardCopy_addWidgetCommands(WidgetInfo widget, List<ClipboardCommand> commands)
throws Exception {
final String tabText = (String) PropertyUtils.getByPath(widget, "TabText").getValue();
commands.add(new PanelClipboardCommand<TabLayoutPanelInfo>(widget) {
private static final long serialVersionUID = 0L;
@Override
protected void add(TabLayoutPanelInfo panel, WidgetInfo widget) throws Exception {
panel.command_CREATE2(widget, null);
PropertyUtils.getByPath(widget, "TabText").setValue(tabText);
}
});
}
////////////////////////////////////////////////////////////////////////////
//
// Header
//
////////////////////////////////////////////////////////////////////////////
/**
* Object that describes header for widgets on {@link StackPanelInfo}.
*/
public class WidgetHandle extends AbstractWidgetHandle<WidgetInfo> {
////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
////////////////////////////////////////////////////////////////////////////
public WidgetHandle(WidgetInfo widget) {
super(widget);
}
////////////////////////////////////////////////////////////////////////////
//
// Access
//
////////////////////////////////////////////////////////////////////////////
@Override
public Rectangle getBounds() {
return m_widgetToHandleBounds.get(m_widget);
}
@Override
public void show() {
m_stackContainer.setActive(m_widget);
}
}
////////////////////////////////////////////////////////////////////////////
//
// WidgetHandle objects
//
////////////////////////////////////////////////////////////////////////////
private final List<WidgetHandle> m_widgetHandles = Lists.newArrayList();
private final Map<WidgetInfo, Rectangle> m_widgetToHandleBounds = Maps.newHashMap();
/**
* Return list of all {@link WidgetHandle}'s.
*/
public List<WidgetHandle> getWidgetHandles() {
return m_widgetHandles;
}
/**
* Fills {@link #m_widgetHandles}.
*/
private void prepareWidgetHandles() throws Exception {
GwtState state = getState();
m_widgetHandles.clear();
//
List<?> tabs = (List<?>) ReflectionUtils.getFieldObject(getObject(), "tabs");
for (WidgetInfo widget : getChildrenWidgets()) {
int index =
(Integer) ReflectionUtils.invokeMethod(
getObject(),
"getWidgetIndex(com.google.gwt.user.client.ui.Widget)",
widget.getObject());
Object tabWidget = tabs.get(index);
Object tabElement = state.getUIObjectUtils().getElement(tabWidget);
// prepare bounds
Rectangle tabBounds = state.getAbsoluteBounds(tabElement);
absoluteToRelative(tabBounds);
// add handle object
m_widgetHandles.add(new WidgetHandle(widget));
m_widgetToHandleBounds.put(widget, tabBounds);
}
}
}