examplegui.java

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

JAVA
2,141
字号

      public void mouseDoubleClick(MouseEvent e) {
        onMouseDoubleClick(e);
      }
    });

    addMouseMoveListener(new MouseMoveListener() {
      public void mouseMove(MouseEvent e) {
        onMouseMove(e);
      }
    });

    if (getVerticalBar() != null) {
      getVerticalBar().addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
          m_TopRow = getVerticalBar().getSelection();
          redraw();
        }

      });
    }

    if (getHorizontalBar() != null) {
      getHorizontalBar().addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
          m_LeftColumn = getHorizontalBar().getSelection();
          redraw();
        }
      });
    }
    addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        onKeyDown(e);
      }
    });
  }

  // ////////////////////////////////////////////////////////////////////////////
  // Berechnungen
  // ////////////////////////////////////////////////////////////////////////////

  protected int getFixedWidth() {
    int width = 0;
    for (int i = 0; i < m_Model.getFixedColumnCount(); i++)
      width += m_Model.getColumnWidth(i);
    return width;
  }

  protected int getColumnLeft(int index) {
    if (index < m_Model.getFixedColumnCount()) {
      int x = 0;
      for (int i = 0; i < index; i++) {
        x += m_Model.getColumnWidth(i);
      }
      return x;
    }
    if (index < m_LeftColumn)
      return -1;
    int x = getFixedWidth();
    for (int i = m_LeftColumn; i < index; i++) {
      x += m_Model.getColumnWidth(i);
    }
    return x;
  }

  protected int getColumnRight(int index) {
    if (index < 0)
      return 0;
    return getColumnLeft(index) + m_Model.getColumnWidth(index);
  }

  protected int getLastColumnRight() {
    return getColumnRight(m_Model.getColumnCount() - 1);
  }

  protected void doCalculations() {
    if (m_Model == null) {
      ScrollBar sb = getHorizontalBar();
      if (sb != null) {
        sb.setMinimum(0);
        sb.setMaximum(1);
        sb.setPageIncrement(1);
        sb.setThumb(1);
        sb.setSelection(1);
      }
      sb = getVerticalBar();
      if (sb != null) {
        sb.setMinimum(0);
        sb.setMaximum(1);
        sb.setPageIncrement(1);
        sb.setThumb(1);
        sb.setSelection(1);
      }
      return;
    }

    int m_HeaderHeight = m_Model.getFirstRowHeight();
    int m_RowHeight = m_Model.getRowHeight();

    Rectangle rect = getClientArea();
    if (m_LeftColumn < m_Model.getFixedColumnCount()) {
      m_LeftColumn = m_Model.getFixedColumnCount();
    }

    if (m_TopRow < m_Model.getFixedRowCount()) {
      m_TopRow = m_Model.getFixedRowCount();
    }

    int fixedWidth = getFixedWidth();
    int fixedHeight = m_HeaderHeight + (m_Model.getFixedRowCount() - 1)
        * m_Model.getRowHeight();
    m_ColumnsVisible = 0;
    m_ColumnsFullyVisible = 0;

    if (m_Model.getColumnCount() > m_Model.getFixedColumnCount()) {
      int runningWidth = getColumnLeft(m_LeftColumn);
      for (int col = m_LeftColumn; col < m_Model.getColumnCount(); col++) {
        if (runningWidth < rect.width + rect.x)
          m_ColumnsVisible++;
        runningWidth += m_Model.getColumnWidth(col);
        if (runningWidth < rect.width + rect.x)
          m_ColumnsFullyVisible++;
        else
          break;
      }
    }

    ScrollBar sb = getHorizontalBar();
    if (sb != null) {
      if (m_Model.getColumnCount() <= m_Model.getFixedColumnCount()) {
        sb.setMinimum(0);
        sb.setMaximum(1);
        sb.setPageIncrement(1);
        sb.setThumb(1);
        sb.setSelection(1);
      } else {
        sb.setMinimum(m_Model.getFixedColumnCount());
        sb.setMaximum(m_Model.getColumnCount());
        sb.setIncrement(1);
        sb.setPageIncrement(2);
        sb.setThumb(m_ColumnsFullyVisible);
        sb.setSelection(m_LeftColumn);
      }
    }

    m_RowsFullyVisible = Math.max(0, (rect.height - fixedHeight)
        / m_RowHeight);
    m_RowsFullyVisible = Math.min(m_RowsFullyVisible, m_Model.getRowCount()
        - m_Model.getFixedRowCount());
    m_RowsFullyVisible = Math.max(0, m_RowsFullyVisible);

    m_RowsVisible = m_RowsFullyVisible + 1;

    if (m_TopRow + m_RowsFullyVisible > m_Model.getRowCount()) {
      m_TopRow = Math.max(m_Model.getFixedRowCount(), m_Model
          .getRowCount()
          - m_RowsFullyVisible);
    }

    if (m_TopRow + m_RowsFullyVisible >= m_Model.getRowCount()) {
      m_RowsVisible--;
    }

    sb = getVerticalBar();
    if (sb != null) {
      if (m_Model.getRowCount() <= m_Model.getFixedRowCount()) {
        sb.setMinimum(0);
        sb.setMaximum(1);
        sb.setPageIncrement(1);
        sb.setThumb(1);
        sb.setSelection(1);
      } else {
        sb.setMinimum(m_Model.getFixedRowCount());
        sb.setMaximum(m_Model.getRowCount());
        sb.setPageIncrement(m_RowsVisible);
        sb.setIncrement(1);
        sb.setThumb(m_RowsFullyVisible);
        sb.setSelection(m_TopRow);
      }
    }
  }

  /**
   * Returns the area that is occupied by the given cell
   * 
   * @param col
   * @param row
   * @return Rectangle
   */
  public Rectangle getCellRect(int col, int row) {
    int m_HeaderHeight = m_Model.getFirstRowHeight();
    if ((col < 0) || (col >= m_Model.getColumnCount()))
      return new Rectangle(-1, -1, 0, 0);

    int x = getColumnLeft(col) + 1;
    int y;

    if (row == 0)
      y = 0;
    else if (row < m_Model.getFixedRowCount())
      y = m_HeaderHeight + ((row - 1) * m_Model.getRowHeight());
    else
      y = m_HeaderHeight
          + (m_Model.getFixedRowCount() - 1 + row - m_TopRow)
          * m_Model.getRowHeight();
    int width = m_Model.getColumnWidth(col) - 1;
    int height = m_Model.getRowHeight() - 1;
    if (row == 0)
      height = m_Model.getFirstRowHeight() - 1;

    return new Rectangle(x, y, width, height);
  }

  protected boolean canDrawCell(int col, int row, Rectangle clipRect) {
    Rectangle r = getCellRect(col, row);
    return canDrawCell(r, clipRect);
  }

  protected boolean canDrawCell(Rectangle r, Rectangle clipRect) {
    if (r.y + r.height < clipRect.y)
      return false;
    if (r.y > clipRect.y + clipRect.height)
      return false;
    if (r.x + r.width < clipRect.x)
      return false;
    if (r.x > clipRect.x + clipRect.width)
      return false;
    return true;
  }

  // ////////////////////////////////////////////////////////////////////////////
  // ZEICHNEN
  // ////////////////////////////////////////////////////////////////////////////

  // Paint-Ereignis

  protected void onPaint(PaintEvent event) {
    Rectangle rect = getClientArea();
    GC gc = event.gc;

    doCalculations();

    if (m_Model != null) {

      drawBottomSpace(gc);
      drawCells(gc, gc.getClipping(), 0, m_Model.getFixedColumnCount(),
          0, m_Model.getFixedRowCount());
      drawCells(gc, gc.getClipping(), m_LeftColumn, m_Model
          .getColumnCount(), 0, m_Model.getFixedRowCount());
      drawCells(gc, gc.getClipping(), 0, m_Model.getFixedColumnCount(),
          m_TopRow, m_TopRow + m_RowsVisible);
      drawCells(gc, gc.getClipping(), m_LeftColumn, m_Model
          .getColumnCount(), m_TopRow, m_TopRow + m_RowsVisible);
    } else {
      gc.fillRectangle(rect);
    }
  }

  // Bottom-Space

  protected void drawBottomSpace(GC gc) {
    Rectangle r = getClientArea();
    if (m_Model.getRowCount() > 0) {
      r.y = m_Model.getFirstRowHeight()
          + (m_Model.getFixedRowCount() - 1 + m_RowsVisible)
          * m_Model.getRowHeight() + 1;
    }

    gc.setBackground(getBackground());
    gc.fillRectangle(r);
    gc.fillRectangle(getLastColumnRight() + 2, 0, r.width, r.height);

    if (m_Model.getRowCount() > 0) {
      if (flatStyleSpecified)
        // gc.setForeground(this.getBackground());
        gc.setForeground(m_Display
            .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
      else
        gc.setForeground(m_Display
            .getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
      // Linke Schattenlinie
      gc.drawLine(0, 0, 0, r.y - 1);
    }

    if (!flatStyleSpecified)
      gc.setForeground(this.getBackground());
    else
      gc.setForeground(m_Display.getSystemColor(SWT.COLOR_WHITE));
    // Untere Abschlusslinie
    gc.drawLine(0, r.y - 1, getLastColumnRight() + 1, r.y - 1);

    // Rechte Abschlusslinie
    gc.drawLine(getLastColumnRight() + 1, 0, getLastColumnRight() + 1,
        r.y - 1);
  }

  // Cells

  /**
   * Redraws the the cells only in the given area.
   * 
   * @param cellsToRedraw
   *            Defines the area to redraw. The rectangles elements are not
   *            pixels but cell numbers.
   */
  public void redraw(Rectangle cellsToRedraw) {
    redraw(cellsToRedraw.x, cellsToRedraw.y, cellsToRedraw.width,
        cellsToRedraw.height);
  }

  /**
   * Redraws the the cells only in the given area.
   * 
   * @param firstCol
   * @param firstRow
   * @param numOfCols
   * @param numOfRows
   */
  public void redraw(int firstCol, int firstRow, int numOfCols, int numOfRows) {
    Rectangle clipRect = getClientArea();
    drawCells(new GC(this), clipRect, firstCol, firstCol + numOfCols,
        firstRow, firstRow + numOfRows);
  }

  protected void drawCells(GC gc, Rectangle clipRect, int fromCol, int toCol,
      int fromRow, int toRow) {
    int cnt = 0;
    Rectangle r;

    if (m_CellEditor != null) {
      if (!isCellVisible(m_CellEditor.m_Col, m_CellEditor.m_Row)) {
        Rectangle hide = new Rectangle(-101, -101, 100, 100);
        m_CellEditor.setBounds(hide);
      } else {
        m_CellEditor.setBounds(getCellRect(m_CellEditor.m_Col,
            m_CellEditor.m_Row));
      }
    }

    for (int row = fromRow; row < toRow; row++) {
      r = getCellRect(0, row);
      if (r.y + r.height < clipRect.y) {
        continue;
      }
      if (r.y > clipRect.y + clipRect.height) {
        break;
      }

      for (int col = fromCol; col < toCol; col++) {
        r = getCellRect(col, row);
        if (r.x > clipRect.x + clipRect.width) {
          break;
        }
        if (canDrawCell(col, row, clipRect)) {
          drawCell(gc, col, row);
          cnt++;
        }
      }
    }
  }

  protected void drawCell(GC gc, int col, int row) {
    if ((row < 0) || (row >= m_Model.getRowCount())) {
      return;
    }

    Rectangle rect = getCellRect(col, row);

    m_Model.getCellRenderer(col, row).drawCell(
        gc,
        rect,
        col,
        row,
        m_Model.getContentAt(col, row),
        showAsSelected(col, row),
        col < m_Model.getFixedColumnCount()
            || row < m_Model.getFixedRowCount(),
        col == m_ClickColumnIndex && row == m_ClickRowIndex);

  }

  protected boolean showAsSelected(int col, int row) {
    // A cell with an open editor should be drawn without focus
    if (m_CellEditor != null) {
      if (col == m_CellEditor.m_Col && row == m_CellEditor.m_Row)
        return false;
    }
    return isCellSelected(col, row);
  }

  protected void drawRow(GC gc, int row) {
    Rectangle clipRect = getClientArea();
    drawCells(gc, getClientArea(), 0, m_Model.getFixedColumnCount(), row,
        row + 1);
    drawCells(gc, getClientArea(), m_LeftColumn, m_Model.getColumnCount(),
        row, row + 1);
  }

  protected void drawCol(GC gc, int col) {
    Rectangle clipRect = getClientArea();
    drawCells(gc, clipRect, col, col + 1, 0, m_Model.getFixedRowCount());
    drawCells(gc, clipRect, col, col + 1, m_TopRow, m_TopRow
        + m_RowsVisible);
  }

  // ////////////////////////////////////////////////////////////////////////////
  // REAKTION AUF BENUTZER
  // ////////////////////////////////////////////////////////////////////////////

  /* gibt die Nummer einer Modellspalte zuruck */
  protected int getColumnForResize(int x, int y) {
    if (m_Model == null)
      return -1;
    if ((y <= 0)
        || (y >= m_Model.getFirstRowHeight()
            + (m_Model.getFixedRowCount() - 1)
            * m_Model.getRowHeight()))
      return -1;

    if (x < getFixedWidth() + 3) {
      for (int i = 0; i < m_Model.getFixedColumnCount(); i++)
        if (Math.abs(x - getColumnRight(i)) < 3) {
          if (m_Model.isColumnResizable(i))
            return i;
          return -1;
        }
    }

    for (int i = m_LeftColumn; i < m_Model.getColumnCount(); i++) {

⌨️ 快捷键说明

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