com.mansi.client.board.InsertPanelExample.java Source code

Java tutorial

Introduction

Here is the source code for com.mansi.client.board.InsertPanelExample.java

Source

/*
 * Copyright 2009 Fred Sauer
 * 
 * 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 com.mansi.client.board;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.allen_sauer.gwt.dnd.client.DragController;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.HorizontalPanelDropController;
import com.allen_sauer.gwt.dnd.client.drop.VerticalPanelDropController;
import com.mansi.client.board.Example;
import com.mansi.client.text.EditableLabel;
import com.mansi.client.text.EditableLabelListener;

/**
 * Example of columns that can be rearranged, with widget that can be moved within a column or
 * between columns.
 */
public final class InsertPanelExample extends Example {

    private static final int COLUMNS = 5;

    private static final int ROWS = 10;

    private static final int SPACING = 5;

    public InsertPanelExample() {
        //    addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE);
        int count = 0;

        // use the boundary panel as this composite's widget
        AbsolutePanel boundaryPanel = new AbsolutePanel();
        boundaryPanel.setSize("100%", "100%");
        setWidget(boundaryPanel);

        // initialize our column drag controller
        PickupDragController columnDragController = new PickupDragController(boundaryPanel, false);
        columnDragController.setBehaviorMultipleSelection(false);

        // initialize our widget drag controller
        PickupDragController widgetDragController = new PickupDragController(boundaryPanel, true);
        widgetDragController.setBehaviorMultipleSelection(false);

        // initialize horizontal panel to hold our columns
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        // horizontalPanel.setSize("100%", "100%");
        horizontalPanel.setSpacing(SPACING);
        boundaryPanel.add(horizontalPanel);

        // initialize our column drop controller
        HorizontalPanelDropController columnDropController = new HorizontalPanelDropController(horizontalPanel);
        columnDragController.registerDropController(columnDropController);

        for (int col = 1; col <= COLUMNS; col++) {
            // initialize a vertical panel to hold the heading and a second vertical
            // panel
            VerticalPanel columnCompositePanel = new VerticalPanel();

            // initialize inner vertical panel to hold individual widgets
            VerticalPanel verticalPanel = new VerticalPanelWithSpacer();
            verticalPanel.setStyleName("compPanel");
            verticalPanel.setSpacing(SPACING);
            horizontalPanel.add(columnCompositePanel);

            VerticalPanel widgetHolder = new VerticalPanel();
            // initialize a widget drop controller for the current column
            VerticalPanelDropController widgetDropController = new VerticalPanelDropController(verticalPanel);
            widgetDragController.registerDropController(widgetDropController);

            final Label heading = new Label();
            // Put together the column pieces
            final EditableLabel heading_temp = new EditableLabel("");
            if (headings[col - 1] != null) {
                heading_temp.setText(headings[col - 1]);
            } else {
                heading_temp.setText("Column " + col);
            }
            final int column = col;
            EditableLabelListener listener = new EditableLabelListener() {
                @Override
                public void onLabelModified(String text, EditableLabel sender) {
                    // TODO Auto-generated method stub
                    heading.setText("");
                    heading_temp.setText(text);
                    headings[column - 1] = heading_temp.getText();
                    System.out.println(headings[column - 1]);
                }
            };

            heading_temp.addEditableLabelListener(listener);
            heading.addStyleName("heading");
            heading.setSize("200px", "20px");
            columnCompositePanel.add(heading_temp);
            columnCompositePanel.add(heading);
            widgetHolder.add(verticalPanel);
            widgetHolder.setSpacing(SPACING);
            widgetHolder.setStyleName("columns");
            columnCompositePanel.setHeight("370px");
            //      columnCompositePanel.setSpacing(15);
            columnCompositePanel.add(widgetHolder);

            // make the column draggable by its heading
            columnDragController.makeDraggable(columnCompositePanel, heading);

            for (int row = 1; row <= ROWS; row++) {
                if (row < 5) {
                    // initialize a widget
                    HTML widget = new HTML("ID&nbsp;#" + ++count);
                    widget.addStyleName("widget");
                    widget.setHeight("60px");
                    widget.setWidth("100px");
                    verticalPanel.add(widget);

                    // make the widget draggable
                    widgetDragController.makeDraggable(widget);
                }
            }
        }
    }

    @Override
    public String getDescription() {
        return "Allows drop to occur anywhere among the children of a supported <code>InsertPanel</code>.";
    }

    @Override
    public Class<?>[] getInvolvedClasses() {
        return new Class[] { InsertPanelExample.class, VerticalPanelDropController.class,
                VerticalPanelWithSpacer.class, HorizontalPanelDropController.class, PickupDragController.class, };
    }
}