/*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
   */
  @Override
  protected void doLoadDefault() {

    // Get all TableItems
    TableItem[] allitems = fTable.getItems();

    for (TableItem tableitem : allitems) {
      // get the IPathManager for the item and set it to the default value
      AVRPathManager path = (AVRPathManager) tableitem.getData();
      path.setToDefault();
      updateTableItem(tableitem);
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.preference.FieldEditor#doStore()
   */
  @Override
  protected void doStore() {

    // Get all TableItems
    TableItem[] allitems = fTable.getItems();

    for (TableItem tableitem : allitems) {
      // Get the IPathManager of the item and store its value to the
      // persistent storage
      AVRPathManager path = (AVRPathManager) tableitem.getData();
      path.store();
    }
  }
    public void widgetSelected(SelectionEvent e) {
      // Enable the Edit button
      fEditButton.setEnabled(true);

      // Enable the rescan button of a system path item has been selected
      TableItem selected = fTable.getSelection()[0];
      AVRPathManager path = (AVRPathManager) selected.getData();
      switch (path.getSourceType()) {
        case System:
          fRescanButton.setEnabled(true);
          break;
        default:
          fRescanButton.setEnabled(false);
      }
    }
  /**
   * Updates the visual of the given TableItem according to its IPathManager.
   *
   * @param item TableItem whose visual needs to be updated
   */
  private void updateTableItem(TableItem item) {

    AVRPathManager path = (AVRPathManager) item.getData();

    // add warn / error icons if path is empty / invalid
    boolean valid = path.isValid();
    boolean optional = path.isOptional();
    boolean empty = (path.getPath().isEmpty());

    if (valid && !empty) {
      // valid path
      item.setImage((Image) null);
    } else if ((valid && empty) || (!valid && optional)) {
      // valid but empty path or invalid and optional path (use for optional paths)
      item.setImage(
          PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK));
    } else {
      // Path is invalid
      item.setImage(
          PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
    }

    item.setText(COLUMN_NAME, path.getName());
    item.setText(COLUMN_TYPE, path.getSourceType().toString());
    item.setText(COLUMN_PATH, path.getPath().toOSString());

    // Adjust color/font according to source type
    switch (path.getSourceType()) {
      case System:
        item.setFont(COLUMN_TYPE, fDialogFont);
        item.setFont(COLUMN_PATH, fDialogFont);
        item.setForeground(COLUMN_PATH, fTable.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
        break;
      case Bundled:
        item.setFont(COLUMN_TYPE, fDialogFont);
        item.setFont(COLUMN_PATH, fDialogFont);
        item.setForeground(COLUMN_PATH, fTable.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
        break;
      case Custom:
        item.setFont(COLUMN_TYPE, fBoldFont);
        item.setFont(COLUMN_PATH, fBoldFont);
        item.setForeground(COLUMN_PATH, fTable.getDisplay().getSystemColor(SWT.COLOR_BLACK));
    }

    // Updates the table layout.
    fTable.getColumn(COLUMN_NAME).pack();
    fTable.getColumn(COLUMN_TYPE).pack();
    fTable.getColumn(COLUMN_PATH).pack();
    fTable.layout();
  }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
   */
  @Override
  protected void refreshValidState() {
    super.refreshValidState();

    // Get all TableItems, extract their IPathManager and check
    // if it is valid and not optional.
    // If any one IPathManager is invalid, the
    // valid boolean is set to false and an error message indicating
    // the culprit is shown.

    TableItem[] allitems = fTable.getItems();
    boolean oldValid = fValid;
    boolean newValid = true;
    String invalidPath = null;

    for (TableItem ti : allitems) {
      AVRPathManager pathitem = (AVRPathManager) ti.getData();
      if (!pathitem.isValid() && !pathitem.isOptional()) {
        newValid = false;
        invalidPath = pathitem.getName();
      }
    }

    // Updates validity and error message.
    fValid = newValid;
    if (fValid == false) {
      showErrorMessage("Path for '" + invalidPath + "' is not valid");
    } else {
      clearErrorMessage();
    }

    // Send some notifications.
    if (newValid != oldValid) {
      fireStateChanged(IS_VALID, oldValid, newValid);
    }
  }