alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.windows.view.ui.toolbars;

import org.openide.awt.Toolbar;

/** Listener for toolbar motion.
 * 
 * When somebody works (drag and drop) with any toolbar and this work comes up to this listener.
 * Listener is adherented to ToolbarConfiguration so every toolbar's motion is reflected in
 * this ToolbarConfiguration and it's appropriate ToolbarConstraints.
 * So this is only place, where the ToolbarConfiguration is changed by toolbar's motion.
 *
 * @author Libor Kramolis
 */
public class ToolbarDnDListener extends Object implements Toolbar.DnDListener {
    /** now dragged toolbar */
    private ToolbarConstraints draggedToolbar;
    private ToolbarConfiguration configuration;

    /** Create new Toolbar listener.
     * @param conf specified toolbat configuration.
     */
    public ToolbarDnDListener (ToolbarConfiguration conf) {
        configuration = conf;
    }

    /** Move toolbar and followers horizontaly.
     * @param tc first moved toolbar
     * @param dx horizontaly distance
     */
    protected void moveToolbar2EndHorizontally (ToolbarConstraints tc, int dx) {
        if (dx == 0) // no move
            return;

        if (dx < 0)
            tc.moveLeft2End (-dx);
        if (dx > 0)
            tc.moveRight2End (dx);
    }

    /** Move toolbar horizontaly.
     * @param tc moved toolbar
     * @param dx horizontal distance
     */
    protected void moveToolbarHorizontally (ToolbarConstraints tc, int dx) {
        if (dx == 0) // no move
            return;

        if (dx < 0)
            tc.moveLeft (-dx);
        if (dx > 0)
            tc.moveRight (dx);
    }

    /** Move toolbar verticaly.
     * @param tc moved toolbar
     * @param dy vertical distance
     */
    protected void moveToolbarVertically (ToolbarConstraints tc, int dy) {
        if (dy == 0) // no move
            return;

        if (dy < 0)
            moveUp (tc, -dy);
        if (dy > 0)
            moveDown (tc, dy);
    }

    /** Try move toolbar up.
     * @param tc moved toolbar
     * @param dy vertical distance
     */
    protected void moveUp (ToolbarConstraints tc, int dy) {
        if (dy < ((Toolbar.getBasicHeight() / 2) + 2))
            return;

        int rI = tc.rowIndex();
        if (draggedToolbar.isAlone()) { // is alone on row(s) -> no new rows
            if (rI == 0) // in first row
                return;
        }

        int pos = rI - 1;
        tc.destroy();

        int plus = 0;
        int rowCount = configuration.getRowCount();
        for (int i = pos; i < pos + tc.getRowCount(); i++) {
            configuration.getRow (i + plus).addToolbar (tc, tc.getPosition());
            if (rowCount != configuration.getRowCount()) {
                rowCount = configuration.getRowCount();
                plus++;
            }
        }
        configuration.checkToolbarRows();
    }

    /** Try move toolbar down.
     * @param tc moved toolbar
     * @param dy vertical distance
     */
    public void moveDown (ToolbarConstraints tc, int dy) {
        int rI = tc.rowIndex();

        int step = ((Toolbar.getBasicHeight() / 2) + 2);

        if (draggedToolbar.isAlone()) { // is alone on row(s) -> no new rows
            if (rI == (configuration.getRowCount() - tc.getRowCount())) // in last rows
                return;
            step = ((Toolbar.getBasicHeight() / 4) + 2);
        }

        if (dy < step)
            return;

        int pos = rI + 1;
        tc.destroy();
        
        for (int i = pos; i < pos + tc.getRowCount(); i++)
            configuration.getRow (i).addToolbar (tc, tc.getPosition());

        configuration.checkToolbarRows();
    }
    
    ///////////////////////////
    // from Toolbar.DnDListener

    /** Invoced when toolbar is dragged. */
    public void dragToolbar (Toolbar.DnDEvent e) {
        if (draggedToolbar == null) {
            draggedToolbar = configuration.getToolbarConstraints (e.getName());
        }

        switch (e.getType()) {
        case Toolbar.DnDEvent.DND_LINE:
            // not implemented yet - it's bug [1]
            // not implemented in this version
            return; // only Toolbar.DnDEvent.DND_LINE
        case Toolbar.DnDEvent.DND_END:
            moveToolbar2EndHorizontally (draggedToolbar, e.getDX());
            break;
        case Toolbar.DnDEvent.DND_ONE:
            moveToolbarVertically (draggedToolbar, e.getDY());
            break;
        }
        if (e.getType() == Toolbar.DnDEvent.DND_ONE)
            moveToolbarHorizontally (draggedToolbar, e.getDX());

        draggedToolbar.updatePosition();

        configuration.revalidateWindow();
    }

    /** Invoced when toolbar is dropped. */
    public void dropToolbar (Toolbar.DnDEvent e) {
        dragToolbar (e);

        configuration.reflectChanges();
        draggedToolbar = null;
    }
} // end of class ToolbarDnDListener

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.