org.openxdata.designer.client.CenterPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.openxdata.designer.client.CenterPanel.java

Source

package org.openxdata.designer.client;

import java.util.ArrayList;
import java.util.List;

import org.openxdata.designer.client.controller.ICenterPanel;
import org.openxdata.designer.client.controller.IFormActionListener;
import org.openxdata.designer.client.controller.IFormChangeListener;
import org.openxdata.designer.client.controller.IFormDesignerListener;
import org.openxdata.designer.client.controller.IFormSelectionListener;
import org.openxdata.designer.client.controller.LayoutChangeListener;
import org.openxdata.designer.client.controller.WidgetPropertyChangeListener;
import org.openxdata.designer.client.controller.WidgetSelectionListener;
import org.openxdata.designer.client.event.CenterPanelTabSelectedEvent;
import org.openxdata.designer.client.event.FormDesignerEventBus;
import org.openxdata.designer.client.util.FormDesignerUtil;
import org.openxdata.designer.client.util.LanguageUtil;
import org.openxdata.designer.client.vew.widget.images.FormDesignerImages;
import org.openxdata.designer.client.view.DesignSurfaceView;
import org.openxdata.designer.client.view.PreviewView;
import org.openxdata.designer.client.view.PropertiesView;
import org.openxdata.sharedlib.client.OpenXdataConstants;
import org.openxdata.sharedlib.client.controller.SubmitListener;
import org.openxdata.sharedlib.client.locale.FormsConstants;
import org.openxdata.sharedlib.client.model.FormDef;
import org.openxdata.sharedlib.client.util.FormUtil;
import org.openxdata.sharedlib.client.widget.RuntimeWidgetWrapper;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventPreview;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

/**
 * Panel containing the contents on the form being designed.
 * 
 *  www.openxdata.org - Licensed as written in license.txt and original sources of this file and its authors are found in sources.txt.
 *
 */
public class CenterPanel extends Composite implements SelectionHandler<Integer>, IFormSelectionListener,
        SubmitListener, LayoutChangeListener, ICenterPanel {

    private FormsConstants constants = GWT.create(FormsConstants.class);

    /** Index for the properties tab. */
    public static int SELECTED_INDEX_PROPERTIES = 0;

    /** Index for xforms source xml tab. */
    public static int SELECTED_INDEX_XFORMS_SOURCE = 1;

    /** Index for the design surface tab. */
    public static int SELECTED_INDEX_DESIGN_SURFACE = 1;

    /** Index for the javascript source tab. */
    public static int SELECTED_INDEX_JAVASCRIPT_SOURCE = 3;

    /** Index for the layout xml tab. */
    public static int SELECTED_INDEX_LAYOUT_XML = 4;

    /** Index for the locale or language xml tab. */
    public static int SELECTED_INDEX_LANGUAGE_XML = 5;

    /** Index for the preview tab. */
    public static int SELECTED_INDEX_PREVIEW = 2;

    /** Index for the model xml tab. */
    public static int SELECTED_INDEX_MODEL_XML = 7;

    private boolean showXformsSource = true;
    private boolean showJavaScriptSource = true;
    private boolean showLayoutXml = true;
    private boolean showLanguageXml = true;
    private boolean showModelXml = true;

    /**
     * Tab widget housing the contents.
     */
    private DecoratedTabPanel tabs = new DecoratedTabPanel();

    /**
     * TextArea displaying the XForms xml.
     */
    private TextArea txtXformsSource = new TextArea();

    /**
     * The view displaying form item properties.
     */
    private PropertiesView propertiesView = new PropertiesView();

    /**
     * View onto which user drags and drops ui controls in a WYSIWYG manner.
     */
    private DesignSurfaceView designSurfaceView;

    /** The text area which contains javascript source. */
    private TextArea txtJavaScriptSource = new TextArea();

    /** The text area which contains layout xml. */
    private TextArea txtLayoutXml = new TextArea();

    /** The text area which contains model xml. */
    private TextArea txtModelXml = new TextArea();

    /** The text area which contains locale or language xml. */
    private TextArea txtLanguageXml = new TextArea();

    /**
     * View used to display a form as it will look when the user is entering data in non-design mode.
     */
    private PreviewView previewView;

    /** The form defintion object thats is currently being edited. */
    private FormDef formDef;

    /** The index of the selected tab. */
    private int selectedTabIndex = 0;

    /** Scroll panel for the design surface. */
    private ScrollPanel scrollPanelDesign = new ScrollPanel();

    /** Scroll panel for the preview surface. */
    private ScrollPanel scrollPanelPreview = new ScrollPanel();

    /** Listener to form designer global events. */
    private IFormDesignerListener formDesignerListener;

    private EventBus eventBus = FormDesignerEventBus.getBus();

    /**
     * Constructs a new center panel widget.
     * 
     * @param images
     */
    public CenterPanel(FormDesignerImages images) {
        designSurfaceView = new DesignSurfaceView(images);
        previewView = new PreviewView(images);

        initProperties();
        //initXformsSource();
        initDesignSurface();
        //initJavaScriptSource();
        //initLayoutXml();
        //initLanguageXml();
        initPreview();
        //initModelXml();

        FormUtil.maximizeWidget(tabs);

        tabs.selectTab(0);

        // register initial state with history
        History.newItem("0");

        initWidget(tabs);
        tabs.addSelectionHandler(this);

        Context.setCurrentMode(Context.MODE_QUESTION_PROPERTIES);

        History.addValueChangeHandler(new HistoryHandler());

        previewEvents();
    }

    /**
     * @see com.google.gwt.user.client.DOM#addEventPreview(EventPreview)
     */
    private void previewEvents() {

        DOM.addEventPreview(new EventPreview() {
            public boolean onEventPreview(Event event) {
                if (DOM.eventGetType(event) == Event.ONKEYDOWN) {
                    byte mode = Context.getCurrentMode();

                    if (mode == Context.MODE_DESIGN)
                        return designSurfaceView.handleKeyBoardEvent(event);
                    else if (mode == Context.MODE_PREVIEW)
                        return previewView.handleKeyBoardEvent(event);
                    else if (mode == Context.MODE_QUESTION_PROPERTIES || mode == Context.MODE_XFORMS_SOURCE)
                        return formDesignerListener.handleKeyBoardEvent(event);
                }

                return true;
            }
        });
    }

    /**
     * Sets the listener to form item property change events.
     * 
     * @param formChangeListener the listener.
     */
    public void setFormChangeListener(IFormChangeListener formChangeListener) {
        propertiesView.setFormChangeListener(formChangeListener);
    }

    /**
     * @see com.google.gwt.event.logical.shared.SelectionHandler#onSelection(SelectionEvent)
     */
    public void onSelection(SelectionEvent<Integer> event) {
        // register selection in history
        History.newItem(event.getSelectedItem().toString(), false);

        selectedTabIndex = event.getSelectedItem();

        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            Context.setCurrentMode(Context.MODE_DESIGN);
        else if (selectedTabIndex == SELECTED_INDEX_PREVIEW) {
            if (formDef != null && formDef.getQuestionCount() > 0 && !designSurfaceView.hasWidgets()) {
                tabs.selectTab(SELECTED_INDEX_DESIGN_SURFACE);

                Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                    @Override
                    public void execute() {
                        tabs.selectTab(SELECTED_INDEX_PREVIEW);

                    }
                });
                return;
            }

            Context.setCurrentMode(Context.MODE_PREVIEW);
        } else if (selectedTabIndex == SELECTED_INDEX_PROPERTIES)
            Context.setCurrentMode(Context.MODE_QUESTION_PROPERTIES);
        else if (selectedTabIndex == SELECTED_INDEX_XFORMS_SOURCE)
            Context.setCurrentMode(Context.MODE_XFORMS_SOURCE);
        else
            Context.setCurrentMode(Context.MODE_NONE);

        if (selectedTabIndex == SELECTED_INDEX_PREVIEW) {
            if (formDef != null) {
                if (!previewView.isPreviewing())
                    loadPreview();
                else
                    previewView.moveToFirstWidget();
            }
        } else if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE) {
            if (!designSurfaceView.hasWidgets())
                designSurfaceView.refresh();
        }

        eventBus.fireEvent(new CenterPanelTabSelectedEvent(selectedTabIndex));
    }

    private void loadPreview() {
        FormUtil.dlg.setText(constants.loadingPreview());
        FormUtil.dlg.center();

        Scheduler.get().scheduleDeferred(new ScheduledCommand() {

            @Override
            public void execute() {
                try {
                    commitChanges();

                    List<RuntimeWidgetWrapper> externalSourceWidgets = new ArrayList<RuntimeWidgetWrapper>();

                    previewView.loadForm(formDef, designSurfaceView.getLayoutXml(), getJavaScriptSource(),
                            externalSourceWidgets, true);
                    FormUtil.dlg.hide();
                } catch (Exception ex) {
                    FormUtil.displayException(ex);
                }

            }
        });
    }

    /**
     * Sets up the design surface.
     */
    private void initDesignSurface() {
        tabs.add(scrollPanelDesign, constants.designSurface());

        int height = Window.getClientHeight();
        int width = Window.getClientWidth();
        designSurfaceView.setWidth(width + OpenXdataConstants.UNITS);
        designSurfaceView.setHeight(height + OpenXdataConstants.UNITS);
        designSurfaceView.setLayoutChangeListener(this);

        scrollPanelDesign.setWidget(designSurfaceView);

        tabs.selectTab(SELECTED_INDEX_DESIGN_SURFACE);

        updateScrollPos();
    }

    /**
     * Sets up the xforms source tab.
     */
    private void initXformsSource() {
        tabs.add(txtXformsSource, constants.xformsSource());
        FormUtil.maximizeWidget(txtXformsSource);
    }

    /**
     * Sets up the layout xml tab.
     */
    private void initJavaScriptSource() {
        tabs.add(txtJavaScriptSource, constants.javaScriptSource());
        FormUtil.maximizeWidget(txtJavaScriptSource);
    }

    /**
     * Sets up the layout xml tab.
     */
    private void initLayoutXml() {
        tabs.add(txtLayoutXml, constants.layoutXml());
        FormUtil.maximizeWidget(txtLayoutXml);
    }

    /**
     * Sets up the language xml tab.
     */
    private void initLanguageXml() {
        tabs.add(txtLanguageXml, constants.languageXml());
        FormUtil.maximizeWidget(txtLanguageXml);
    }

    /**
     * Sets up the preview surface tab.
     */
    private void initPreview() {
        tabs.add(scrollPanelPreview, constants.preview());
        previewView.setWidth("100%");
        previewView.setHeight("700" + OpenXdataConstants.UNITS);
        previewView.setSubmitListener(this);
        previewView.setDesignSurface(designSurfaceView);
        previewView.setCenterPanel(this);

        scrollPanelPreview.setWidget(previewView);
    }

    /**
     * Sets up the model xml tab.
     */
    private void initModelXml() {
        tabs.add(txtModelXml, constants.modelXml());
        FormUtil.maximizeWidget(txtModelXml);
    }

    /**
     * Sets up the properties tab.
     */
    private void initProperties() {
        tabs.add(propertiesView, constants.properties());
    }

    /**
     * Sets the height of the text area widgets.
     * 
     * @param height the height in pixels
     */
    public void adjustHeight(String height) {
        txtXformsSource.setHeight(height);
        txtJavaScriptSource.setHeight(height);
        txtLayoutXml.setHeight(height);
        txtModelXml.setHeight(height);
        txtLanguageXml.setHeight(height);
    }

    /**
     * @see org.openxdata.designer.client.controller.IFormSelectionListener#onFormItemSelected(java.lang.Object)
     */
    public void onFormItemSelected(Object formItem) {
        propertiesView.onFormItemSelected(formItem);

        if (selectedTabIndex == SELECTED_INDEX_PROPERTIES)
            propertiesView.setFocus();

        FormDef form = FormDef.getFormDef(formItem);

        if (this.formDef != form) {
            setFormDef(form);

            designSurfaceView.setFormDef(formDef);
            previewView.setFormDef(formDef);

            if (selectedTabIndex == SELECTED_INDEX_PREVIEW && formDef != null)
                previewView.loadForm(formDef, designSurfaceView.getLayoutXml(), getJavaScriptSource(), null, true);

            //This is necessary for those running in a non GWT mode to update the 
            //scroll bars on loading the form.
            updateScrollPos();
        }
    }

    /**
     * @see com.google.gwt.user.client.WindowResizeListener#onWindowResized(int, int)
     */
    public void onWindowResized(int width, int height) {
        propertiesView.onWindowResized(width, height);
        updateScrollPos();
    }

    /**
     * Sets the current scroll height and width of the design and preview surface.
     */
    private void updateScrollPos() {
        onVerticalResize();

        int height = tabs.getOffsetHeight() - 48;
        if (height > 0) {
            scrollPanelDesign.setHeight(height + OpenXdataConstants.UNITS);
            scrollPanelPreview.setHeight(height + OpenXdataConstants.UNITS);
        }
    }

    /**
     * Called every time this widget is resized.
     */
    public void onVerticalResize() {
        int d = Window.getClientWidth() - tabs.getAbsoluteLeft();
        if (d > 0) {
            scrollPanelDesign.setWidth(d - 16 + OpenXdataConstants.UNITS);
            scrollPanelPreview.setWidth(d - 16 + OpenXdataConstants.UNITS);
        }
    }

    /**
     * Loads a form with a given layout xml.
     * 
     * @param formDef the form definition object.
     * @param layoutXml the layout xml. If this is null, the form is loaded
     *                with the default layout which is build automatically.
     */
    public void loadForm(FormDef formDef, String layoutXml) {
        setFormDef(formDef);

        designSurfaceView.setLayoutXml(layoutXml, formDef);

        previewView.clearPreview();
        tabs.selectTab(SELECTED_INDEX_PROPERTIES);
    }

    /**
     * Gets the xforms source xml.
     * 
     * @return the xforms xml.
     */
    public String getXformsSource() {
        if (txtXformsSource.getText().length() == 0 && showXformsSource)
            tabs.selectTab(SELECTED_INDEX_XFORMS_SOURCE);
        return txtXformsSource.getText();
    }

    /**
     * Sets the xforms source xml.
     * 
     * @param xml the xforms xml.
     * @param selectXformsTab set to true to select the xforms source tab, else false.
     */
    public void setXformsSource(String xml, boolean selectXformsTab) {
        txtXformsSource.setText(xml);
        if (selectXformsTab && showXformsSource)
            tabs.selectTab(SELECTED_INDEX_XFORMS_SOURCE);
    }

    /**
     * Gets the widget layout xml.
     * 
     * @return the layout xml.
     */
    public String getLayoutXml() {
        return txtLayoutXml.getText();
    }

    /**
     * Gets the javascript source.
     * 
     * @return the layout xml.
     */
    public String getJavaScriptSource() {
        return txtJavaScriptSource.getText();
    }

    /**
     * Gets the language xml.
     * 
     * @return the language xml.
     */
    public String getLanguageXml() {
        return txtLanguageXml.getText();
    }

    /**
     * Gets the inner html for the selected form page.
     * 
     * @return the html.
     */
    public String getFormInnerHtml() {
        return designSurfaceView.getSelectedPageHtml();
    }

    /** 
     * Sets the widget layout xml.
     * 
     * @param xml the layout xml.
     * @param selectTabs set to true to select the layout xml tab, else set to false.
     */
    public void setLayoutXml(String xml, boolean selectTabs) {
        txtLayoutXml.setText(xml);
        if (selectTabs && showLayoutXml)
            tabs.selectTab(SELECTED_INDEX_LAYOUT_XML);
    }

    /** 
     * Sets the javascript source.
     * 
     * @param src the javascript source.
     */
    public void setJavaScriptSource(String src) {
        txtJavaScriptSource.setText(src);
    }

    /**
     * Sets the language xml.
     * 
     * @param xml the language xml.
     * @param selectTab set to true to select the language xml tab, else set to false.
     */
    public void setLanguageXml(String xml, boolean selectTab) {
        txtLanguageXml.setText(xml);
        if (selectTab)
            selectLanguageTab();
    }

    /**
     * Builds the widget layout xml and puts it in the layout xml tab.
     */
    public void buildLayoutXml() {
        String layout = designSurfaceView.getLayoutXml();

        if (layout != null)
            this.formDef.setLayoutXml(layout);
        else
            layout = formDef.getLayoutXml();

        txtLayoutXml.setText(layout);
    }

    /**
     * Builds the language xml and puts it in the language xml tab.
     */
    public void buildLanguageXml() {
        Document doc = LanguageUtil.createNewLanguageDoc();
        Element rootNode = doc.getDocumentElement();

        Element node = null;
        if (formDef != null) {
            node = formDef.getLanguageNode();
            if (node != null)
                rootNode.appendChild(doc.importNode(node, true));
        }

        node = designSurfaceView.getLanguageNode();
        if (node != null)
            rootNode.appendChild(doc.importNode(node, true));

        txtLanguageXml.setText(FormDesignerUtil.formatXml(doc.toString()));

        if (formDef != null)
            formDef.setLanguageXml(txtLanguageXml.getText());
    }

    /**
     * Loads layout xml and builds the widgets represented on the design surface tab. 
     *
     * @param layoutXml the layout xml. If layoutXml is null, then it uses the one in
     *                the layout xml tab, if any is found there.
     * @param selectTabs set to true to select the layout xml tab, else set to false.
     */
    public void loadLayoutXml(String layoutXml, boolean selectTabs) {
        if (layoutXml != null)
            txtLayoutXml.setText(layoutXml);
        else
            layoutXml = txtLayoutXml.getText();

        if (layoutXml != null && layoutXml.trim().length() > 0) {

            FormDef frmDef = null;
            if (Context.inLocalizationMode())
                frmDef = formDef;
            designSurfaceView.setLayoutXml(layoutXml, frmDef); //TODO This passed null formdef in localization mode

            updateScrollPos();

            if (selectTabs)
                tabs.selectTab(SELECTED_INDEX_DESIGN_SURFACE);
        } else if (selectTabs && showLayoutXml)
            tabs.selectTab(SELECTED_INDEX_LAYOUT_XML);

        if (formDef != null)
            formDef.setLayoutXml(layoutXml);
    }

    /**
     * Loads the form widget layout from the xml in the layout xml tab.
     * 
     * @param selectTabs set to true to select the layout xml tab, else false.
     */
    public void openFormLayout(boolean selectTabs) {
        loadLayoutXml(null, selectTabs);
    }

    /**
     * Loads the current form in the locale whose contents are in the language xml tab.
     */
    public void openLanguageXml() {
        loadLanguageXml(null, false);
    }

    public void loadLanguageXml(String xml, boolean selectTabs) {
        if (xml != null)
            txtLanguageXml.setText(xml);
        else
            xml = txtLanguageXml.getText();

        if (xml != null && xml.trim().length() > 0) {
            if (formDef != null)
                txtXformsSource.setText(
                        FormUtil.formatXml(LanguageUtil.translate(formDef.getDoc(), xml, true).toString()));

            String layoutXml = txtLayoutXml.getText();
            if (layoutXml != null && layoutXml.trim().length() > 0) {
                txtLayoutXml.setText(FormUtil.formatXml(LanguageUtil.translate(layoutXml, xml, false).toString()));
                String s = txtLayoutXml.getText();
                s.trim();
            }

            if (selectTabs)
                selectLanguageTab();

            if (formDef != null)
                formDef.setLanguageXml(xml);
        } else if (selectTabs)
            selectLanguageTab();
    }

    public void saveFormLayout() {
        txtLayoutXml.setText(designSurfaceView.getLayoutXml());

        if (showLayoutXml)
            tabs.selectTab(SELECTED_INDEX_LAYOUT_XML);

        if (formDef != null)
            formDef.setLayoutXml(txtLayoutXml.getText());
    }

    public void saveLanguageText(boolean selectTab) {
        buildLanguageXml();

        if (selectTab)
            selectLanguageTab();

        if (formDef != null)
            formDef.setLanguageXml(txtLanguageXml.getText());
    }

    public void saveJavaScriptSource() {
        if (formDef != null)
            formDef.setJavaScriptSource(txtJavaScriptSource.getText());
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerListener#format()()
     */
    public void format() {
        if (selectedTabIndex == SELECTED_INDEX_XFORMS_SOURCE)
            txtXformsSource.setText(FormDesignerUtil.formatXml(txtXformsSource.getText()));
        else if (selectedTabIndex == SELECTED_INDEX_LAYOUT_XML)
            txtLayoutXml.setText(FormDesignerUtil.formatXml(txtLayoutXml.getText()));
        else if (selectedTabIndex == SELECTED_INDEX_MODEL_XML)
            txtModelXml.setText(FormDesignerUtil.formatXml(txtModelXml.getText()));
        else if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.format();
        else if (selectedTabIndex == SELECTED_INDEX_LANGUAGE_XML)
            txtLanguageXml.setText(FormDesignerUtil.formatXml(txtLanguageXml.getText()));
    }

    public void commitChanges() {
        propertiesView.commitChanges();
    }

    /**
     * Sets the listener to widget selection changes.
     * 
     * @param widgetSelectionListener the listener.
     */
    public void setWidgetSelectionListener(WidgetSelectionListener widgetSelectionListener) {
        designSurfaceView.setWidgetSelectionListener(widgetSelectionListener);
    }

    /**
     * @see org.openxdata.designer.client.controller.IFormActionListener#deleteSelectedItems()
     */
    public void deleteSelectedItem() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.deleteSelectedItem();
    }

    /**
     * @see org.openxdata.designer.client.controller.IFormActionListener#copyItem()
     */
    public void copyItem() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.copyItem();
    }

    /**
     * @see org.openxdata.designer.client.controller.IFormActionListener#cutItem()
     */
    public void cutItem() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.cutItem();
    }

    /**
     * @see org.openxdata.designer.client.controller.IFormActionListener#pasteItem()
     */
    public void pasteItem() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.pasteItem();
    }

    /**
     * @see org.openxdata.formtools.client.controller.SubmitListener#onSubmit(String)()
     */
    public void onSubmit(String xml) {
        this.txtModelXml.setText(xml);

        if (showModelXml)
            tabs.selectTab(SELECTED_INDEX_MODEL_XML);
        else
            Window.alert(constants.formSubmitSuccess());
    }

    /**
     * @see org.openxdata.formtools.client.controller.SubmitListener#onCancel()()
     */
    public void onCancel() {

    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#alignLeft()
     */
    public void alignLeft() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.alignLeft();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#alignRight()
     */
    public void alignRight() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.alignRight();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#alignTop()
     */
    public void alignTop() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.alignTop();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#alignBottom()
     */
    public void alignBottom() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.alignBottom();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#makeSameHeight()
     */
    public void makeSameHeight() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.makeSameHeight();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#makeSameSize()
     */
    public void makeSameSize() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.makeSameSize();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#makeSameWidth()
     */
    public void makeSameWidth() {
        if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.makeSameWidth();
    }

    /**
     * @see org.openxdata.formtools.client.controller.IFormDesignerController#refresh()
     */
    public void refresh() {
        if (selectedTabIndex == SELECTED_INDEX_PREVIEW)
            previewView.refresh();
        else if (selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE)
            designSurfaceView.refresh();
    }

    /**
     * Sets the current form that is being designed.
     * 
     * @param formDef the form definition object.
     */
    public void setFormDef(FormDef formDef) {
        if (this.formDef == null || this.formDef != formDef) {
            if (formDef == null) {
                txtLayoutXml.setText(null);
                txtXformsSource.setText(null);
                txtLanguageXml.setText(null);
                txtJavaScriptSource.setText(null);
            } else {
                txtLayoutXml.setText(formDef.getLayoutXml());
                txtXformsSource.setText(formDef.getXformXml());
                txtLanguageXml.setText(formDef.getLanguageXml());
                txtJavaScriptSource.setText(formDef.getJavaScriptSource());
            }
        }

        this.formDef = formDef;
    }

    /**
     * Gets the current form that is being designed.
     * 
     * @return the form definition object.
     */
    public FormDef getFormDef() {
        return formDef;
    }

    /**
     * Sets the height offset used by this widget when embedded as a widget
     * in a GWT application.
     * 
     * @param offset the offset pixels.
     */
    public void setEmbeddedHeightOffset(int offset) {
        designSurfaceView.setEmbeddedHeightOffset(offset);
        previewView.setEmbeddedHeightOffset(offset);
    }

    /**
     * Sets the listener to form action events.
     * 
     * @param formActionListener the listener.
     */
    public void setFormActionListener(IFormActionListener formActionListener) {
        this.propertiesView.setFormActionListener(formActionListener);
    }

    /**
     * Checks if the layout xml tab is selected.
     * 
     * @return true if yes, else false.
     */
    public boolean isInLayoutMode() {
        return tabs.getTabBar().getSelectedTab() == SELECTED_INDEX_LAYOUT_XML;
    }

    /**
     * @see org.openxdata.designer.client.controller.LayoutChangeListener#onLayoutChanged(String)
     */
    public void onLayoutChanged(String xml) {
        txtLayoutXml.setText(xml);
        if (formDef != null)
            formDef.setLayoutXml(xml);
    }

    /**
     * Selects the language xml tab.
     */
    private void selectLanguageTab() {
        if (showLanguageXml)
            tabs.selectTab(SELECTED_INDEX_LANGUAGE_XML);
    }

    /**
     * Sets listener to form designer global events.
     * 
     * @param formDesignerListener the listener.
     */
    public void setFormDesignerListener(IFormDesignerListener formDesignerListener) {
        this.formDesignerListener = formDesignerListener;
    }

    /**
     * Checks if the current selection mode allows refreshes.
     * 
     * @return true if it allows, else false.
     */
    public boolean allowsRefresh() {
        return selectedTabIndex == SELECTED_INDEX_DESIGN_SURFACE || selectedTabIndex == SELECTED_INDEX_PREVIEW;
    }

    /**
     * Removes the language xml tab.
     */
    public void removeLanguageTab() {
        /*if(showLanguageXml){
           tabs.remove(SELECTED_INDEX_LANGUAGE_XML);
               
           --SELECTED_INDEX_PREVIEW;
           --SELECTED_INDEX_MODEL_XML;
               
           showLanguageXml = false;
        }*/
    }

    public void removeXformSourceTab() {
        /*if(showXformsSource){
           tabs.remove(SELECTED_INDEX_XFORMS_SOURCE);
            
           --SELECTED_INDEX_DESIGN_SURFACE;
           --SELECTED_INDEX_JAVASCRIPT_SOURCE;
           --SELECTED_INDEX_LAYOUT_XML;
           --SELECTED_INDEX_LANGUAGE_XML;
           --SELECTED_INDEX_PREVIEW;
           --SELECTED_INDEX_MODEL_XML;
               
           showXformsSource = false;
        }*/
    }

    public void removeJavaScriptSourceTab() {
        /*if(showJavaScriptSource){
           tabs.remove(SELECTED_INDEX_JAVASCRIPT_SOURCE);
            
           --SELECTED_INDEX_LAYOUT_XML;
           --SELECTED_INDEX_LANGUAGE_XML;
           --SELECTED_INDEX_PREVIEW;
           --SELECTED_INDEX_MODEL_XML;
               
           showJavaScriptSource = false;
        }*/
    }

    public void removeLayoutXmlTab() {
        /*if(showLayoutXml){
           tabs.remove(SELECTED_INDEX_LAYOUT_XML);
            
           --SELECTED_INDEX_LANGUAGE_XML;
           --SELECTED_INDEX_PREVIEW;
           --SELECTED_INDEX_MODEL_XML;
               
           showLayoutXml = false;
        }*/
    }

    public void removeModelXmlTab() {
        /*if(showModelXml){
           tabs.remove(SELECTED_INDEX_MODEL_XML);
           showModelXml = false;
        }*/
    }

    public WidgetPropertyChangeListener getWidgetPropertyChangeListener() {
        return designSurfaceView;
    }

    /**
     * Simple implementation of a History listener
     * History tokens are registered when a tab is selected by the user
     * The tokens themselves are just the tab index
     */
    private class HistoryHandler implements ValueChangeHandler<String> {

        //@Override
        public void onValueChange(ValueChangeEvent<String> event) {
            try {
                int tab = Integer.parseInt(event.getValue());
                tabs.selectTab(tab);
            } catch (NumberFormatException e) {
                // do nothing
            }
        }

    }
}