examplegui.java

来自「java的swt图形程序」· Java 代码 · 共 2,141 行 · 第 1/5 页

JAVA
2,141
字号
      int left = getColumnLeft(i);
      int right = left + m_Model.getColumnWidth(i);
      if (Math.abs(x - right) < 3) {
        if (m_Model.isColumnResizable(i))
          return i;
        return -1;
      }
      if ((x >= left + 3) && (x <= right - 3))
        break;
    }
    return -1;
  }

  /* gibt die Nummer einer Zeile der Ansicht(!) zuruck */
  protected int getRowForResize(int x, int y) {
    if (m_Model == null)
      return -1;
    if ((x <= 0) || (x >= getFixedWidth()))
      return -1;

    if (y < m_Model.getFirstRowHeight())
      return -1;

    int row = 1 + ((y - m_Model.getFirstRowHeight()) / m_Model
        .getRowHeight());
    int rowY = m_Model.getFirstRowHeight() + row * m_Model.getRowHeight();

    if (Math.abs(rowY - y) < 3 && m_Model.isRowResizable())
      return row;

    return -1;
  }

  /**
   * Returns the number of the row that is present at position y or -1, if out
   * of area.
   * 
   * @param y
   * @return int
   */
  public int calcRowNum(int y) {
    if (m_Model == null)
      return -1;
    if (y < m_Model.getFirstRowHeight())
      return (m_Model.getFixedRowCount() == 0 ? m_TopRow : 0);
    y -= m_Model.getFirstRowHeight();
    int row = 1 + (y / m_Model.getRowHeight());
    if ((row < 0) || (row >= m_Model.getRowCount()))
      return -1;
    if (row >= m_Model.getFixedRowCount())
      return m_TopRow + row - m_Model.getFixedRowCount();
    return row;
  }

  /**
   * Returns the number of the column that is present at position x or -1, if
   * out of area.
   * 
   * @param y
   * @return int
   */
  public int calcColumnNum(int x) {
    if (m_Model == null)
      return -1;
    int col = 0;

    int z = 0;
    for (int i = 0; i < m_Model.getFixedColumnCount(); i++) {
      if ((x >= z) && (x <= z + m_Model.getColumnWidth(i))) {
        return i;
      }
      z += m_Model.getColumnWidth(i);
    }

    col = -1;
    z = getFixedWidth();
    for (int i = m_LeftColumn; i < m_Model.getColumnCount(); i++) {
      if ((x >= z) && (x <= z + m_Model.getColumnWidth(i))) {
        col = i;
        break;
      }
      z += m_Model.getColumnWidth(i);
    }
    return col;
  }

  public boolean isCellVisible(int col, int row) {
    if (m_Model == null)
      return false;
    return ((col >= m_LeftColumn && col < m_LeftColumn + m_ColumnsVisible
        && row >= m_TopRow && row < m_TopRow + m_RowsVisible)

    || (col < m_Model.getFixedColumnCount() && row < m_Model
        .getFixedRowCount()));
  }

  public boolean isCellFullyVisible(int col, int row) {
    if (m_Model == null)
      return false;
    return ((col >= m_LeftColumn
        && col < m_LeftColumn + m_ColumnsFullyVisible
        && row >= m_TopRow && row < m_TopRow + m_RowsFullyVisible)

    || (col < m_Model.getFixedColumnCount() && row < m_Model
        .getFixedRowCount()));
  }

  public boolean isRowVisible(int row) {
    if (m_Model == null)
      return false;
    return ((row >= m_TopRow && row < m_TopRow + m_RowsVisible) || row < m_Model
        .getFixedRowCount());

  }

  public boolean isRowFullyVisible(int row) {
    if (m_Model == null)
      return false;
    return ((row >= m_TopRow && row < m_TopRow + m_RowsFullyVisible) || row < m_Model
        .getFixedRowCount());
  }

  /*
   * Focusses the given Cell. Assumes that the given cell is in the viewable
   * area. Does all neccessary redraws.
   */
  protected void focusCell(int col, int row, int stateMask) {
    GC gc = new GC(this);

    // close cell editor if active
    if (m_CellEditor != null)
      m_CellEditor.close(true);

    /*
     * Special rule: in row selection mode the selection if a fixed cell in
     * a non-fixed row is allowed and handled as a selection of a non-fixed
     * cell.
     */

    if (row >= m_Model.getFixedRowCount()
        && (col >= m_Model.getFixedColumnCount() || m_RowSelectionMode)) {

      if ((stateMask & SWT.CTRL) == 0 && (stateMask & SWT.SHIFT) == 0) {
        // case: no modifier key
        boolean redrawAll = (m_Selection.size() > 1);
        int oldFocusRow = m_FocusRow;
        int oldFocusCol = m_FocusCol;
        clearSelectionWithoutRedraw();
        addToSelection(col, row);
        m_FocusRow = row;
        m_FocusCol = col;

        if (redrawAll)
          redraw();
        else if (m_RowSelectionMode) {
          if (isRowVisible(oldFocusRow))
            drawRow(gc, oldFocusRow);
          if (isRowVisible(m_FocusRow))
            drawRow(gc, m_FocusRow);
        } else {
          if (isCellVisible(oldFocusCol, oldFocusRow))
            drawCell(gc, oldFocusCol, oldFocusRow);
          if (isCellVisible(m_FocusCol, m_FocusRow))
            drawCell(gc, m_FocusCol, m_FocusRow);
        }
      }

      else if ((stateMask & SWT.CTRL) != 0) {
        // case: CTRL key pressed
        if (toggleSelection(col, row)) {
          m_FocusCol = col;
          m_FocusRow = row;
        }

        if (m_RowSelectionMode) {
          drawRow(gc, row);
        } else {
          drawCell(gc, col, row);
        }
      }

      else if ((stateMask & SWT.SHIFT) != 0) {
        // case: SHIFT key pressed

        if (m_RowSelectionMode) {
          if (row < m_FocusRow) {
            // backword selection
            while (row != m_FocusRow) {
              addToSelection(0, --m_FocusRow);
            }
          } else {
            // foreward selection
            while (row != m_FocusRow) {
              addToSelection(0, ++m_FocusRow);
            }
          }
        } else // cell selection mode
        {
          if (row < m_FocusRow
              || (row == m_FocusRow && col < m_FocusCol)) {
            // backword selection
            while (row != m_FocusRow || col != m_FocusCol) {
              m_FocusCol--;
              if (m_FocusCol < m_Model.getFixedColumnCount()) {
                m_FocusCol = m_Model.getColumnCount();
                m_FocusRow--;
              }
              addToSelection(m_FocusCol, m_FocusRow);
            }
          } else {
            // foreward selection
            while (row != m_FocusRow || col != m_FocusCol) {
              m_FocusCol++;
              if (m_FocusCol == m_Model.getColumnCount()) {
                m_FocusCol = m_Model.getFixedColumnCount();
                m_FocusRow++;
              }
              addToSelection(m_FocusCol, m_FocusRow);
            }
          }

        }

        redraw();
      }

      // notify non-fixed cell listeners
      fireCellSelection(col, row, stateMask);
    } else {
      // a fixed cell was focused
      drawCell(gc, col, row);
      // notify fixed cell listeners
      fireFixedCellSelection(col, row, stateMask);
    }

    gc.dispose();
  }

  protected void onMouseDown(MouseEvent e) {
    if (e.button == 1) {
      // deactivateEditor(true);
      setCapture(true);
      m_Capture = true;

      // Resize column?
      int columnIndex = getColumnForResize(e.x, e.y);
      if (columnIndex >= 0) {
        m_ResizeColumnIndex = columnIndex;
        m_ResizeColumnLeft = getColumnLeft(columnIndex);
        return;
      }

      // Resize row?
      int rowIndex = getRowForResize(e.x, e.y);
      if (rowIndex >= 0) {
        m_ResizeRowIndex = rowIndex;
        m_ResizeRowTop = m_Model.getFirstRowHeight() + (rowIndex - 1)
            * m_Model.getRowHeight();
        m_NewRowSize = m_Model.getRowHeight();
        return;
      }
    }

    // focus change
    int col = calcColumnNum(e.x);
    int row = calcRowNum(e.y);

    if (col == -1 || row == -1)
      return;

    m_ClickColumnIndex = col;
    m_ClickRowIndex = row;

    focusCell(col, row, e.stateMask);

  }

  protected void onMouseMove(MouseEvent e) {
    if (m_Model == null)
      return;

    // show resize cursor?
    if ((m_ResizeColumnIndex != -1) || (getColumnForResize(e.x, e.y) >= 0))
      setCursor(new Cursor(m_Display, SWT.CURSOR_SIZEWE));
    else if ((m_ResizeRowIndex != -1) || (getRowForResize(e.x, e.y) >= 0))
      setCursor(new Cursor(m_Display, SWT.CURSOR_SIZENS));
    else
      setCursor(null);

    if (e.button == 1) {
      // extend selection?
      if (m_ClickColumnIndex != -1 && m_MultiSelectMode) {
        int row = calcRowNum(e.y);
        int col = calcColumnNum(e.x);

        if (row >= m_Model.getFixedRowCount()
            && col >= m_Model.getFixedColumnCount()) {

          m_ClickColumnIndex = col;
          m_ClickRowIndex = row;

          focusCell(col, row, (e.stateMask | SWT.SHIFT));
        }
      }

    }
    // column resize?
    if (m_ResizeColumnIndex != -1) {
      Rectangle rect = getClientArea();
      int oldSize = m_Model.getColumnWidth(m_ResizeColumnIndex);
      if (e.x > rect.x + rect.width - 1)
        e.x = rect.x + rect.width - 1;
      int newSize = e.x - m_ResizeColumnLeft;
      if (newSize < 5)
        newSize = 5;

      int leftX = getColumnLeft(m_ResizeColumnIndex);
      int rightX = getColumnRight(m_ResizeColumnIndex);

      m_Model.setColumnWidth(m_ResizeColumnIndex, newSize);
      newSize = m_Model.getColumnWidth(m_ResizeColumnIndex);

      GC gc = new GC(this);
      gc.copyArea(rightX, 0, rect.width - rightX, rect.height, leftX
          + newSize, 0);
      drawCol(gc, m_ResizeColumnIndex);
      if (newSize < oldSize) {
        int delta = oldSize - newSize;
        redraw(rect.width - delta, 0, delta, rect.height, false);
      }
      gc.dispose();
    }

    // row resize?
    if (m_ResizeRowIndex != -1) {
      Rectangle rect = getClientArea();
      GC gc = new GC(this);

      // calculate new size
      if (e.y > rect.y + rect.height - 1)
        e.y = rect.y + rect.height - 1;
      m_NewRowSize = e.y - m_ResizeRowTop;
      if (m_NewRowSize < m_Model.getRowHeightMinimum())
        m_NewRowSize = m_Model.getRowHeightMinimum();

      // restore old line area
      if (m_LineRestore != null) {
        gc.drawImage(m_LineRestore, m_LineX, m_LineY);
      }

      // safe old picture and draw line
      gc.setForeground(Display.getCurrent().getSystemColor(
          SWT.COLOR_BLACK));
      int lineEnd = getColumnRight(m_LeftColumn + m_ColumnsVisible - 1);
      m_LineRestore = new Image(m_Display, lineEnd, 1);
      m_LineX = rect.x + 1;
      m_LineY = m_ResizeRowTop + m_NewRowSize - 1;
      gc.copyArea(m_LineRestore, m_LineX, m_LineY);
      gc.drawLine(m_LineX, m_LineY, rect.x + lineEnd, m_LineY);
      gc.dispose();

    }

  }

  protected void onMouseUp(MouseEvent e) {
    // if (e.button == 1)
    {
      if (m_Model == null)
        return;

      setCapture(false);
      m_Capture = false;

      if (m_ResizeColumnIndex != -1) {
        fireColumnResize(m_ResizeColumnIndex, m_Model
            .getColumnWidth(m_ResizeColumnIndex));
        m_ResizeColumnIndex = -1;
        redraw();

      }
      if (m_ResizeRowIndex != -1) {
        m_ResizeRowIndex = -1;
        m_Model.setRowHeight(m_NewRowSize);
        m_LineRestore = null;
        fireRowResize(m_NewRowSize);
        redraw();
      }
      if (m_ClickColumnIndex != -1) {
        int col = m_ClickColumnIndex;
        int row = m_ClickRowIndex;
        m_ClickColumnIndex = -1;
        m_ClickRowIndex = -1;
        if (m_CellEditor == null) {
          drawCell(new GC(this), col, row);
        }
      }

    }
  }

  protected void onKeyDown(KeyEvent e) {
    boolean focusChanged = false;
    int newFocusRow = m_FocusRow;
    int newFocusCol = m_FocusCol;

    if (m_Model == null)
      return;

    if ((e.character == ' ') || (e.character == '\r')) {
      openEditorInFocus();
      return;
    } else if (e.keyCode == SWT.HOME) {
      newFocusCol = m_Model.getFixedColumnCount();
      if (newFocusRow == -1)
        newFocusRow = m_Model.getFixedRowCount();
      focusChanged = true;
    } else if (e.keyCode == SWT.END) {
      newFocusCol = m_Model.getColumnCount() - 1;
      if (newFocusRow == -1)
        newFocusRow = m_Model.getFixedRowCount();
      focusChanged = true;
    } else if (e.keyCode == SWT.ARROW_LEFT) {
      if (!m_RowSelectionMode) {
        if (newFocusCol > m_Model.getFixedColumnCount())
          newFocusCol--;
      }
      focusChanged = true;
    } else if (e.keyCode == SWT.ARROW_RIGHT) {

⌨️ 快捷键说明

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