⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rowstatus.java

📁 你个snmp的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            subRequest.getStatus().setErrorStatus(PDU.undoFailed);
          }
          else {
            RowStatusEvent rowStatusEvent =
                new RowStatusEvent(this, getTable(), oldRow, row,
                                   oldValue.getValue(), RowStatus.destroy);
            fireRowStatusChanged(rowStatusEvent);
          }
          break;
        }
        case createAndGo:
        case createAndWait: {
          MOTableRow deleted = getTable().removeRow(row.getIndex());
          if (deleted == null) {
            subRequest.getStatus().setErrorStatus(PDU.undoFailed);
          }
          else {
            RowStatusEvent rowStatusEvent =
                new RowStatusEvent(this, getTable(), row, deleted,
                                   RowStatus.notExistant, newStatus);
            fireRowStatusChanged(rowStatusEvent);
          }
          break;
        }
      }
    }
  }

  public void beforePrepareMOChange(MOChangeEvent changeEvent) {
    if (changeEvent.getOID().startsWith(oid)) {
      int currentValue = notExistant;
      if (changeEvent.getOldValue() instanceof Integer32) {
        currentValue = ((Integer32) changeEvent.getOldValue()).getValue();
      }
      int newValue = ((Integer32) changeEvent.getNewValue()).getValue();
      boolean ok = false;
      switch (currentValue) {
        case notExistant:
          ok = ((newValue == createAndGo) ||
                (newValue == createAndWait) || (newValue == destroy));
          break;
        case notReady:
          ok = ((newValue == destroy) || (newValue == active) ||
                (newValue == notInService));
          break;
        case active:
          ok = ((newValue == active) ||
                (newValue == notInService) || (newValue == destroy));
          break;
        case notInService:
          ok = ((newValue == notInService) ||
                (newValue == active) || (newValue == destroy));
          break;
        // for row creation
        case createAndWait:
          ok = (newValue == createAndWait);
          break;
        case createAndGo:
          ok = (newValue == createAndGo);
          break;
        case destroy:
          ok = (newValue == destroy);
          break;
      }
      if (!ok) {
        changeEvent.setDenyReason(PDU.badValue);
      }
    }
  }

  public void beforeMOChange(MOChangeEvent changeEvent) {
  }

  public void afterMOChange(MOChangeEvent changeEvent) {
  }

  public void afterPrepareMOChange(MOChangeEvent changeEvent) {
  }

  public synchronized void addRowStatusListener(RowStatusListener l) {
    if (rowStatusListeners == null) {
      rowStatusListeners = new Vector(2);
    }
    rowStatusListeners.add(l);
  }

  public synchronized void removeRowStatusListener(RowStatusListener l) {
    if (rowStatusListeners != null) {
      rowStatusListeners.remove(l);
    }
  }

  protected void fireRowStatusChanged(RowStatusEvent event) {
    if (rowStatusListeners != null) {
      Vector listeners = rowStatusListeners;
      int count = listeners.size();
      for (int i = 0; i < count; i++) {
        ((RowStatusListener) listeners.elementAt(i)).rowStatusChanged(event);
      }
    }
  }

  /**
   * Tests if the specified row is active.
   * @param row
   *    a row with a RowStatus column.
   * @param rowStatusColumnIndex
   *    the column index of the RowStatus column in <code>row</code>.
   * @return
   *    <code>true</code> if <code>row</code> is active.
   */
  public static boolean isRowActive(MOTableRow row, int rowStatusColumnIndex) {
    Integer32 rowStatus = (Integer32) row.getValue(rowStatusColumnIndex);
    if (rowStatus != null) {
      return rowStatus.getValue() == RowStatus.active;
    }
    return false;
  }

  /**
   * The <code>ActiveRowsFilter</code> is a {@link MOTableRowFilter} that
   * returns only the active rows of a table with a RowStatus column.
   *
   * @author Frank Fock
   * @version 1.0
   */
  public static class ActiveRowsFilter implements MOTableRowFilter {

    private int rowStatusColumnIndex;

    /**
     * Creates an active row filter by specifying the RowStatus column's index
     * in the target table.
     * @param rowStatusColumnIndex
     *    the column index (zero-based) of the RowStatus column on behalf the
     *    filtering is done.
     */
    public ActiveRowsFilter(int rowStatusColumnIndex) {
      this.rowStatusColumnIndex = rowStatusColumnIndex;
    }

    public boolean passesFilter(MOTableRow row) {
      return (((Integer32)
               row.getValue(rowStatusColumnIndex)).getValue() == active);
    }
  }

  static class RowStatusValidator implements MOValueValidationListener {

    public void validate(MOValueValidationEvent event) {
      if (!(event.getNewValue()  instanceof Integer32)) {
        event.setValidationStatus(PDU.wrongType);
      }
      int v = ((Integer32)event.getNewValue()).getValue();
      if ((v < 1) || (v > 6) || (v == 3)) {
        event.setValidationStatus(PDU.badValue);
      }
    }
  }

  public void rowChanged(MOTableRowEvent event) {
    switch (event.getType()) {
      case MOTableRowEvent.CREATE: {
        // by default do not allow row creation if RowStatus is not set!
        MOTableRow row = event.getRow();
        int myIndex = getTable().getColumnIndex(getColumnID());
        if (row.getValue(myIndex) == null) {
          event.setVetoStatus(PDU.inconsistentName);
        }
        break;
      }
      case MOTableRowEvent.CHANGE: {
        // check whether the changed column can be changed if row is active
        int rowStatus = getCurrentRowStatus(event);
        switch (rowStatus) {
          case active: {
            for (int i=0; i<getTable().getColumnCount(); i++) {
              if (event.getPreparedChanges().getValue(i) == null) {
                continue;
              }
              MOColumn col = getTable().getColumn(i);
              if (col instanceof MOMutableColumn) {
                if (!((MOMutableColumn) col).isMutableInService()) {
                  event.setVetoStatus(PDU.inconsistentValue);
                  event.setVetoColumn(i);
                }
              }
            }
            break;
          }
        }
      }
      case MOTableRowEvent.UPDATED: {
        // check whether changed row is ready to be set active
        int rowStatus = getCurrentRowStatus(event);
        switch (rowStatus) {
          case notReady: {
            if ((event.getRow() instanceof MOMutableTableRow) &&
                (isReady(event.getRow(), columnIndex))) {
              ((MOMutableTableRow) event.getRow()).setValue(columnIndex,
                  new Integer32(notInService));
            }
            break;
          }
        }
      }
    }
  }

  private int getCurrentRowStatus(MOTableRowEvent event) {
    Integer32 rowStatusVariable =
        (Integer32)event.getRow().getValue(columnIndex);
    int rowStatus = RowStatus.notExistant;
    if (rowStatusVariable != null) {
      rowStatus = rowStatusVariable.getValue();
    }
    return rowStatus;
  }

  public boolean isVolatile(MOTableRow row, int column) {
    Integer32 value = (Integer32) row.getValue(column);
    if (value != null) {
      int rowStatus = value.getValue();
      if ((rowStatus != active) && (rowStatus != notInService)) {
        return true;
      }
    }
    return false;
  }

  public void get(SubRequest subRequest, MOTableRow row, int column) {
    Variable rowStatus = getValue(row, column);
    if ((rowStatus != null) &&
        (((Integer32)rowStatus).getValue() == notReady)) {
      if (isReady(row, column)) {
        ((Integer32)rowStatus).setValue(notInService);
      }
    }
    super.get(subRequest, row, column);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -