/**
   * Override this method to change the image that is displayed for this node in the database
   * structure outline.
   */
  public final Image getExpandedImage() {

    if (_expandedImageKey == null) {
      return null;
    }
    return ImageUtil.getImage(_expandedImageKey);
  }
  /**
   * Override this method to change the image that is displayed for this node in the database
   * structure outline.
   */
  public Image getImage() {

    if (_image != null) {
      return _image;
    }
    if (_imageKey == null) {
      return _image;
    }
    return ImageUtil.getImage(_imageKey);
  }
 public void dispose() {
   super.dispose();
   ImageUtil.disposeImage("Images.AliasIcon");
   ImageUtil.disposeImage("Images.ConnectedAliasIcon");
   ImageUtil.disposeImage("Images.ConnectionIcon");
 }
/**
 * Label provider for database structure outline.
 *
 * @author Davy Vanherbergen
 */
public class ConnectionTreeLabelProvider extends LabelProvider {

  private Image _inactiveAliasImage = ImageUtil.getImage("Images.AliasIcon");

  private Image _activeAliasImage = ImageUtil.getImage("Images.ConnectedAliasIcon");

  private Image _sessionImage = ImageUtil.getImage("Images.ConnectionIcon");

  public void dispose() {
    super.dispose();
    ImageUtil.disposeImage("Images.AliasIcon");
    ImageUtil.disposeImage("Images.ConnectedAliasIcon");
    ImageUtil.disposeImage("Images.ConnectionIcon");
  }

  /**
   * Return the image used for the given node.
   *
   * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
   */
  public Image getImage(Object element) {
    if (element instanceof Alias) {
      Alias alias = (Alias) element;

      for (User user : alias.getUsers())
        if (!user.getSessions().isEmpty()) return _activeAliasImage;
      return _inactiveAliasImage;
    }
    return _sessionImage;
  }

  /**
   * Return the text to display
   *
   * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
   */
  public String getText(Object element) {
    if (element instanceof Alias) {
      Alias alias = (Alias) element;

      String label = alias.getName();
      int numSessions = 0;

      for (User user : alias.getUsers()) numSessions += user.getConnections().size();

      if (numSessions == 1)
        return label
            + " ("
            + numSessions
            + " "
            + Messages.getString("ConnectionsView.ConnectedAlias.single.Postfix")
            + ")";

      if (numSessions > 1)
        return label
            + " ("
            + numSessions
            + " "
            + Messages.getString("ConnectionsView.ConnectedAlias.multiple.Postfix")
            + ")";

      return label;

    } else if (element instanceof User) {
      User user = (User) element;
      return user.getUserName();

    } else if (element instanceof SQLConnection) {
      SQLConnection connection = (SQLConnection) element;
      String label;

      if (connection.getDescription() == null) {
        SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
        label =
            Messages.getString("ConnectionsView.ConnectedAlias.ConnectedSince")
                + ' '
                + fmt.format(new Date(connection.getCreatedTime()));
      } else
        label =
            Messages.getString("ConnectionsView.ConnectedAlias.Connection")
                + ' '
                + connection.getDescription();

      if (connection.isPooled())
        label += ' ' + Messages.getString("ConnectionsView.ConnectedAlias.Pooled");
      return label;
    }

    return null;
  }
}