package com.onaro.sanscreen.client.view.task.template.addstoragetohost;
import java.awt.Component;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import org.apache.commons.lang3.StringUtils;
import com.onaro.commons.swing.ListObjectChooser;
import com.onaro.commons.swing.SplitObjectChooser;
import com.onaro.commons.swing.table.OnaroColumn;
import com.onaro.sanscreen.client.view.task.Inventory;
import com.onaro.sanscreen.task.interfaces.data.inventory.Device;
import com.onaro.sanscreen.task.interfaces.data.inventory.Port;
import com.onaro.sanscreen.task.interfaces.data.inventory.Volume;
import com.onaro.util.jfc.OptionPane;
public class InventoryObjectChooser {
private InventoryObjectChooser()
{
//do nothing
}
/**
*
* @return The selected volumes, or null
if cancelled.
*/
public static List showVolumesChooser(Component parent, List unselectedVolumes, List selectedVolumes)
{
SplitObjectChooser chooser = new SplitObjectChooser(Volume.class);
String title = Messages.INSTANCE.getChooseVolumesTitle();
String leftLabel = Messages.INSTANCE.getVolumeLabel();
String rightLabel = Messages.INSTANCE.getSelectedVolumesLable();
chooser.setTitleTexts(leftLabel, rightLabel);
chooser.addPropertyColumn(Volume.class, "name", Messages.INSTANCE.getNameColumnName()); //$NON-NLS-1$
chooser.addPropertyColumn(Volume.class, "volumeMaskingStatus", Messages.INSTANCE.getMaskedColumnName()); //$NON-NLS-1$
chooser.addPropertyColumn(Volume.class, "capacity", Messages.INSTANCE.getCapacityColumnName()); //$NON-NLS-1$
chooser.setObjects(unselectedVolumes, selectedVolumes);
chooser.sortColumns(1, 0);
boolean ok = showDialog(parent, chooser, title);
if (!ok) return null;
return new LinkedList(chooser.getRightObjects());
}
/**
*
* @return The selected applications, or null
if cancelled.
*/
public static List showApplicationsChooser(Component parent, List unselectedApplications, List selectedApplications)
{
SplitObjectChooser chooser = new SplitObjectChooser(String.class);
String title = Messages.INSTANCE.getChooseApplicationsTitle();
String leftLabel = Messages.INSTANCE.getApplicationsLable();
String rightLabel = Messages.INSTANCE.getSelectedApplicationsLabel();
chooser.setTitleTexts(leftLabel, rightLabel);
chooser.addIdentityColumn(Messages.INSTANCE.getNameColumnName());
chooser.setObjects(unselectedApplications, selectedApplications);
chooser.sortColumns(0);
boolean ok = showDialog(parent, chooser, title);
if (!ok) return null;
return new LinkedList(chooser.getRightObjects());
}
/**
*
* @return The selected host, or null
if cancelled.
*/
public static Device showHostChooser(Component parent, Inventory inventory, List hosts, Device selectedHost){
ListObjectChooser chooser = new ListObjectChooser();
chooser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
chooser.enableBorder(true, Messages.INSTANCE.getHostTitleBorderText());
chooser.addPropertyColumn(Device.class, "alias", Messages.INSTANCE.getNameColumnName()); //$NON-NLS-1$
chooser.addPropertyColumn(Device.class, "ip", Messages.INSTANCE.getIPColumnName()); //$NON-NLS-1$
chooser.addColumn(new PortCountColumn(inventory));
chooser.setObjects(hosts);
chooser.setSelectedObject(selectedHost);
chooser.sortColumn(0, true);
final OptionPane pane = new OptionPane(Messages.INSTANCE.getChooseHostTitle(), JOptionPane.OK_CANCEL_OPTION);
pane.prepare(parent, chooser, true);
// By adding a Property Change Listener the "chooser" (table) can send an event to the pane
// This allows the user to select a row by just double click the row, without the need to
// press the OK button.
chooser.addPropertyChangeListener(pane);
pane.show();
// If there is no object selected then a NULL value is returned.
return chooser.getSelectedObject();
}
/**
*
* @return The selected port, or null
if cancelled.
*/
public static Port showPortChooser(Component parent, final Inventory inventory, List ports, Port selectedPort) {
ListObjectChooser chooser = new ListObjectChooser();
chooser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
chooser.addPropertyColumn(Port.class, "wwn", Messages.INSTANCE.getWWNColumnName()); //$NON-NLS-1$
chooser.addPropertyColumn(Port.class, "name", Messages.INSTANCE.getNameColumnName()); //$NON-NLS-1$
OnaroColumn deviceColumn = new OnaroColumn("device", String.class, Messages.INSTANCE.getDeviceColumnName()) //$NON-NLS-1$
{
private static final long serialVersionUID = 1L;
public Object getValue(Port port) {
Long deviceId = port.getRootId();
Device device = Utils.getDevice(inventory, deviceId);
if (device == null) return StringUtils.EMPTY;
return Utils.getDeviceSummary(device);
}
};
chooser.addColumn(deviceColumn);
OnaroColumn connectedToColumn = new OnaroColumn("connectedTo", String.class, Messages.INSTANCE.getConnectedToColumnName()) //$NON-NLS-1$
{
private static final long serialVersionUID = 1L;
public Object getValue(Port port) {
Long connectedPortId = port.getConnectedPortId();
String connectedToText = Utils.getConnectedToSummary(inventory, connectedPortId);
return connectedToText;
}
};
chooser.addColumn(connectedToColumn);
chooser.setObjects(ports);
chooser.setSelectedObject(selectedPort);
chooser.sortColumn(0, true);
String title = Messages.INSTANCE.getChoosePortTitle();
boolean ok = showDialog(parent, chooser, title);
if (!ok) return null;
return chooser.getSelectedObject();
}
/**
*
* @return The selected storage, or null
if cancelled.
*/
public static Device showStorageChooser(Component parent, Inventory inventory, List storages, Device selectedStorage) {
ListObjectChooser chooser = new ListObjectChooser();
chooser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
chooser.enableBorder(true, Messages.INSTANCE.getStorageTitleBorderText());
chooser.addPropertyColumn(Device.class, "alias", Messages.INSTANCE.getNameColumnName()); //$NON-NLS-1$
chooser.addPropertyColumn(Device.class, "ip", Messages.INSTANCE.getIPColumnName()); //$NON-NLS-1$
chooser.addColumn(new PortCountColumn(inventory));
chooser.setObjects(storages);
chooser.setSelectedObject(selectedStorage);
chooser.sortColumn(0, true);
final OptionPane pane = new OptionPane(Messages.INSTANCE.getChooseStorageTite(), JOptionPane.OK_CANCEL_OPTION);
pane.prepare(parent, chooser, true);
// By adding a Property Change Listener the "chooser" (table) can send an event to the pane
// This allows the user to select a row by just double click the row, without the need to
// press the OK button.
chooser.addPropertyChangeListener(pane);
pane.show();
// If there is no object selected then a NULL value is returned.
return chooser.getSelectedObject();
}
private static boolean showDialog(Component parent, JComponent component, String title){
return OptionPane.showDialog(parent, component, title, null, true);
}
}
class PortCountColumn extends OnaroColumn {
private static final long serialVersionUID = 1L;
private final Inventory inventory;
public PortCountColumn(Inventory inventory) {
super("portCount", Integer.class, Messages.INSTANCE.getPortCountColumnName()); //$NON-NLS-1$
if (inventory == null) throw new IllegalArgumentException();
this.inventory = inventory;
}
public Integer getValue(Device device) {
int portCount = Utils.getAllPorts(inventory, device).size();
return portCount;
}
}