view src/MPipeline/src/mpipeline/MElementSelectable.java @ 49:0bcdf74587d1 database-connection-manager

Cleanup
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:24:36 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

/**
 * Extend the base element class to create elements that are selectable by
 * the user.
 * 
 * @author hewitson
 */
public class MElementSelectable extends MElement {

    /**
     * 
     */
    protected boolean selected = false;
    /**
     *
     */
    protected Border notSelectedBorder = null;
    /**
     *
     */
    protected int selectedBorderThickness = 1;
    /**
     *
     */
    protected Border selectedBorder = BorderFactory.createLineBorder(Color.red, Math.round(scaleFactor*selectedBorderThickness));


    

    /**
     * Set the selected state of this element.
     * @param state
     */
    public void setSelected(boolean state) {

        selected = state;
        if (state) {
            // highlight the border
            setBorder(selectedBorder);
        } else {
            // no border
            setBorder(notSelectedBorder);
        }
        repaint();
    }

    /**
     * Toggle the selected state of the element.
     */
    public void toggleSelected(){
        if (selected){
            setSelected(false);
        } else {
            setSelected(true);
        }
    }

    /**
     * Get the selected state of this element.
     * @return
     */
    public boolean getSelected() {
        return selected;
    }

    /**
     * Return true if the element is selected.
     * @return
     */
    public boolean isSelected(){
        return selected;
    }

    /**
     * Bring the element forward in the stacking order.
     *
     * NOT WORKING !!!
     */
    public void bringForward() {
        MCanvas c = (MCanvas) getParent();
        int zo = c.getComponentZOrder(this);
        if (zo > 0) {
            System.out.println("Setting z-order to " + (zo + 1));
            c.setComponentZOrder(this, zo + 1);
            c.revalidate();
        }
    }

    /**
     * Send the element backwards in the stacking order.
     *
     * NOT WORKING !!!
     */
    public void sendBackward() {
        MCanvas c = (MCanvas) getParent();
        int zo = c.getComponentZOrder(this);
        System.out.println("Setting z-order to " + (zo - 1));
        c.setComponentZOrder(this, zo - 1);
        c.revalidate();
    }

    /**
     * Set the border used when the element is not selected.
     * @param notSelectedBorder
     */
    public void setNotSelectedBorder(Border notSelectedBorder) {
        this.notSelectedBorder = notSelectedBorder;
        if (!selected) {
            setBorder(notSelectedBorder);
        }
    }

    /**
     *
     * @return
     */
    public Border getSelectedBorder() {
        return selectedBorder;
    }

    /**
     *
     * @param selectedBorder
     */
    public void setSelectedBorder(Border selectedBorder) {
        this.selectedBorder = selectedBorder;
    }

    /**
     * Scale the element.
     */
    @Override
    public void scaleElement() {
        super.scaleElement();
        int nbt = Math.round(scaleFactor*selectedBorderThickness);
        if (nbt < 1)
            nbt = 1;
        selectedBorder = BorderFactory.createLineBorder(Color.red, nbt);
        setSelected(selected);
        repaint();
    }

    /**
     *
     * @return
     */
    public int getSelectedBorderThickness() {
        return selectedBorderThickness;
    }

    /**
     *
     * @param selectedBorderThickness
     */
    public void setSelectedBorderThickness(int selectedBorderThickness) {
        this.selectedBorderThickness = selectedBorderThickness;
    }

    

}