All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.fleet.machine.tree.MachineNode Maven / Gradle / Ivy

/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.fleet.machine.tree;

import java.awt.Image;
import java.awt.datatransfer.Transferable;
import java.io.IOException;
import javax.swing.Action;
import nl.cloudfarming.client.fleet.model.Machine;
import nl.cloudfarming.client.fleet.model.Machine.MachineMetaInfo;
import org.openide.actions.OpenAction;
import org.openide.filesystems.FileObject;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.nodes.NodeTransfer;
import org.openide.util.ImageUtilities;
import org.openide.util.actions.SystemAction;
import org.openide.util.datatransfer.PasteType;

/**
 *
 * @author Wytse Visser && Maciek Dulkiewicz
 */
public abstract class MachineNode extends FilterNode {

    private Machine machine;
    private Node original;

    public MachineNode(Node node) {
        super(node, Children.LEAF, node.getLookup());
        original = node;
        original.setDisplayName(getMachine().getName());
    }

    @Override
    public Image getIcon(int type) {
        return ImageUtilities.loadImage(getMetaInfo().getSmallIconResource());
    }

    @Override
    public String getName() {
        return getMachine().getName();
    }

    @Override
    public String getDisplayName() {
        return getName();
    }

    @Override
    public void setName(String name) {
        String oldName = getName();
        getMachine().setName(name);
        original.setDisplayName(name);
        fireDisplayNameChange(oldName, name);
    }

    @Override
    public void destroy() throws IOException {
        getMachineFile().delete();
    }

    protected FileObject getMachineFile() {
        return (FileObject) getLookup().lookup(FileObject.class);
    }

    @Override
    public boolean canDestroy() {
        return true;
    }

    @Override
    public boolean canRename() {
        return true;
    }

    @Override
    public Action getPreferredAction() {
        return SystemAction.get(OpenAction.class);
    }

    @Override
    public Action[] getActions(boolean context) {
        return super.getActions(context);
    }

    @Override
    public Transferable clipboardCopy() throws IOException {
        return NodeTransfer.transferable(this, NodeTransfer.CLIPBOARD_COPY);
    }

    @Override
    public Transferable clipboardCut() throws IOException {
        return clipboardCopy();
    }

    @Override
    public Transferable drag() throws IOException {
        return clipboardCopy();
    }

    @Override
    public PasteType getDropType(Transferable t, int action, int index) {
        return null;
    }

    protected final Machine getMachine() {
        if (machine == null) {
            machine = getLookup().lookup(Machine.class);
        }
        return machine;
    }

    protected abstract MachineMetaInfo getMetaInfo();

    protected abstract void saveMachine();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy