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

nl.cloudfarming.client.fleet.machine.windows.ListOverviewTopComponent 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.windows;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nl.cloudfarming.client.fleet.model.Machine;
import nl.cloudfarming.client.fleet.model.Machine.MachineMetaInfo;
import nl.cloudfarming.client.util.swing.ASBodyPanel;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.nodes.Node;
import org.openide.nodes.NodeEvent;
import org.openide.nodes.NodeListener;
import org.openide.nodes.NodeMemberEvent;
import org.openide.nodes.NodeReorderEvent;
import org.openide.util.ImageUtilities;
import org.openide.windows.TopComponent;

/**
 * This top component shows a list of machines.
 *
 * @author Wytse Visser
 */
@ConvertAsProperties(dtd = "-//nl.cloudfarming.client.fleet.machine.windows//ListOverview//EN",
autostore = false)
@TopComponent.Description(preferredID = "ListOverviewTopComponent",
persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "nl.cloudfarming.client.fleet.machine.windows.ListOverviewTopComponent")
public class ListOverviewTopComponent extends TopComponent {

    private static Map overviewForMachineType = new HashMap<>();
    private ListOverviewPanel panel;
    private ASBodyPanel bodyPanel;

    public ListOverviewTopComponent() {
        setLayout(new BorderLayout());
        panel = new ListOverviewPanel();
        bodyPanel = new ASBodyPanel(panel);
        add(bodyPanel);
    }

    /**
     * Find instance of List overview componennt for provided node.
     *
     * MachineMetaInfo inside the nodes lookup will be used to determine proper
     * instance to return.
     *
     * @param node machine type folder node
     * @return
     */
    public static ListOverviewTopComponent findInstance(final Node node) {
        MachineMetaInfo machineInfo = node.getLookup().lookup(MachineMetaInfo.class);

        if (overviewForMachineType.get(machineInfo) == null) {
            final ListOverviewTopComponent overview = new ListOverviewTopComponent();
            node.addNodeListener(new NodeListener() {
                @Override
                public void childrenAdded(NodeMemberEvent ev) {
                    overview.updateMachines(node);
                }

                @Override
                public void childrenRemoved(NodeMemberEvent ev) {
                    overview.updateMachines(node);
                }

                @Override
                public void childrenReordered(NodeReorderEvent ev) {
                }

                @Override
                public void nodeDestroyed(NodeEvent ev) {
                }

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                }
            });

            overview.initTitleLabel(machineInfo.getDisplayName(), machineInfo.getLargeIconResource());
            overview.setName(machineInfo.getDisplayName());
            overview.setToolTipText(machineInfo.getDisplayName());
            overview.initIcon(machineInfo.getSmallIconResource());
            overview.updateMachines(node);


            overviewForMachineType.put(machineInfo, overview);
        }
        return overviewForMachineType.get(machineInfo);
    }

    private void updateMachines(Node node) {
        List machines = new ArrayList<>();
        for (Node child : node.getChildren().getNodes()) {
            Machine machine = child.getLookup().lookup(Machine.class);
            if (machine != null) {
                machines.add(machine);
            }
        }
        setMachines(machines);
    }

    public void initTitleLabel(String title, String iconResource) {
        bodyPanel.setTitle(title);
        if (iconResource != null) {
            bodyPanel.setIcon(ImageUtilities.loadImageIcon(iconResource, true));
        }
    }

    public void initIcon(String iconResource) {
        if (iconResource != null) {
            setIcon(ImageUtilities.loadImage(iconResource));
        }
    }

    @Override
    public void componentOpened() {
    }

    @Override
    public void componentClosed() {
    }

    void writeProperties(java.util.Properties p) {
    }

    void readProperties(java.util.Properties p) {
    }

    public void setMachines(List machines) {
        panel.setMachines(machines);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy