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

📄 lwgrid.java

📁 Zaval Light-Weight Visual Components Library (LwVCL) is a pure Java alternative to humble AWT-based
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    * @return data to be painted.    */    protected /*C#virtual*/ Object dataToPaint (int row, int col) {      return data.get(row, col);    }   /**    * Validates the grid metric.    */    protected void vMetric()    {      if (!isMetricValid())      {        if (isUsePsMetric()) rPsMetric();        else                 rCustomMetric();        psSize = rPs();        bits = MathBox.getBits(bits, METRIC_VALID, true);      }    }   /**    * Validates the grid visibility. The method validates the grid metrics    * (by <code>vMetric</code> method) before.    */    protected void vVisibility() {      vMetric();      if (getVisiblePart() != null) rVisibility(0, 0);    }   /**    * Calculates the preferred size of the grid component. The method calls    * <code>colWidth</code> and <code>rowHeight</code> to get actual columns    * widths and rows heights. The method is called by <code>vMetric</code>    * method.    * @return a calculated preferred size;    */    protected Dimension rPs()    {      int cols = getGridCols();      int rows = getGridRows();      int psWidth  = netSize * (cols + 1);      int psHeight = netSize * (rows + 1);      for (int i=0; i<cols; i++) psWidth  += colWidth(i);      for (int i=0; i<rows; i++) psHeight += rowHeight(i);      return new Dimension (psWidth, psHeight);    }   /**    * Paints the grid lines.    * @param <code>g</code> the specified graphics context.    */    protected /*C#virtual*/ void paintNet (Graphics g)    {      int topX = visibility.fc.y - netSize;      int topY = visibility.fr.y - netSize;      int botX = visibility.lc.y + colWidth (visibility.lc.x);      int botY = visibility.lr.y + rowHeight(visibility.lr.x);      g.setColor(netColor);      if (MathBox.checkBit(bits, DRAW_HLINES))      {        int y = topY, i = visibility.fr.x;        for (;i <= visibility.lr.x; i++)        {          g.drawLine(topX, y, botX, y);          y += rowHeight(i) + netSize;        }        g.drawLine(topX, y, botX, y);      }      if (MathBox.checkBit(bits, DRAW_VLINES))      {        int x = topX, i = visibility.fc.x;        for (;i <= visibility.lc.x; i++)        {          g.drawLine(x, topY, x, botY);          x += colWidth(i) + netSize;        }        g.drawLine(x, topY, x, botY);      }    }   /**    * Paints the grid cells.    * @param <code>g</code> the specified graphics context.    */    protected /*C#virtual*/ void paintData (Graphics g)    {      int y    = visibility.fr.y + cellInsets.top;      int addW = cellInsets.left + cellInsets.right;      int addH = cellInsets.top  + cellInsets.bottom;      Rectangle r = g.getClipBounds();      for (int i=visibility.fr.x; i<=visibility.lr.x && y < r.y + r.height; i++)      {        if (y + rowHeight(i) > r.y)        {          int x = visibility.fc.y + cellInsets.left;          for (int j=visibility.fc.x; j<=visibility.lc.x; j++)          {             LwView v = provider.getView(i, j, dataToPaint(i, j));             if (v != null && !(i == editingRow && j == editingCol))             {               Color bg = provider.getCellColor(i, j);               if (bg != null)               {                 g.setColor(bg);                 g.fillRect(x - cellInsets.left, y - cellInsets.top, colWidth(j), rowHeight(i));               }               int w = colWidth (j) - addW;               int h = rowHeight(i) - addH;               g.clipRect(x, y, w, h);               if (isUsePsMetric() || v.getType() == LwView.STRETCH) v.paint(g, x, y, w, h, this);               else               {                 Dimension ps = v.getPreferredSize();                 Point p = Alignment.getLocation (ps,                                                  provider.getXAlignment(i, j),                                                  provider.getYAlignment(i, j),                                                  w, h);                 v.paint(g, x + p.x, y + p.y, ps.width, ps.height, this);               }               g.setClip (r);             }             x += (colWidth(j) + netSize);          }        }        y += (rowHeight(i) + netSize);      }    }   /**    * Paints the grid marker.    * @param <code>g</code> the specified graphics context.    */    protected /*C#virtual*/ void paintMarker (Graphics g)    {      if (controller != null && controller.getOffset() >= 0)      {        //???        g.clipRect(-dx, getTopCaptionHeight() - dy, width, height);        int offset = controller.getOffset();        int y = getRowY(offset), h = rowHeight(offset);        int x = visibility.fc.y;        for (int i=visibility.fc.x; i<=visibility.lc.x; i++)        {          if (i != editingCol || editingRow != offset)          {            Color bg = provider.getCellColor(offset, i);            if (bg == null) bg = getBackground();            LwToolkit.drawMarker(g, x, y, colWidth(i), h, bg, hasFocus()?actSelColor:noneActSelColor);          }          x += colWidth(i) + netSize;        }      }    }    protected /*C#override*/ LwLayout getDefaultLayout() {      return this;    }   /**    * Finds and returns grid cell row and column at the specified location.    * The result is presented with java.awt.Point class where <code>x</code>    * field correspond to row and <code>y</code> field correspond to column.    * @param <code>x</code> the specified x coordinate.    * @param <code>y</code> the specified y coordinate.    * @return a cell at the specified location.    */    protected /*C#virtual*/ Point cellByLocation(int x, int y)    {      vVisibility();      int ry1 = visibility.fr.y + dy;      int ry2 = visibility.lr.y + rowHeight(visibility.lr.x) + dy;      int rx1 = visibility.fc.y + dx;      int rx2 = visibility.lc.y + colWidth(visibility.lc.x) + dx;      int row = -1, col = -1;      if (y > ry1 && y < ry2)      {         for (int i = visibility.fr.x; i<=visibility.lr.x; ry1 += rowHeight(i) + netSize, i++)           if (y > ry1 && y < ry1 + rowHeight(i))           {             row = i;             break;           }      }      if (x > rx1 && x < rx2)      {         for (int i = visibility.fc.x; i<=visibility.lc.x; rx1 += colWidth(i) + netSize, i++)           if (x > rx1 && x < rx1 + colWidth(i))           {             col = i;             break;           }      }      return (col >= 0 && row >=0)?new Point(row, col):null;    }   /**    * Invoked by <code>vMetric</code> method to calculate preferred size metric type.    */    protected /*C#virtual*/ void rPsMetric()    {      colWidths  = new int[getGridCols()];      rowHeights = new int[getGridRows()];      int addW = cellInsets.left + cellInsets.right;      int addH = cellInsets.top  + cellInsets.bottom;      for (int i=0; i<colWidths.length ;i++)      {        for (int j=0; j<rowHeights.length; j++)        {          LwView v = provider.getView(j, i, data.get(j, i));          if (v != null)          {            Dimension ps = v.getPreferredSize();            ps.width  += addW;            ps.height += addH;            if (ps.width  > colWidths[i] ) colWidths [i] = ps.width;            if (ps.height > rowHeights[j]) rowHeights[j] = ps.height;          }          else          {            if (DEF_COLWIDTH  > colWidths [i]) colWidths [i] = DEF_COLWIDTH;            if (DEF_ROWHEIGHT > rowHeights[j]) rowHeights[j] = DEF_ROWHEIGHT;          }        }      }    }   /**    * Invoked by <code>vMetric</code> method to calculate custom metric type.    */    protected /*C#virtual*/ void rCustomMetric()    {      int start = 0;      if (colWidths != null)      {        start = colWidths.length;        if (colWidths.length != getGridCols())        {          int[] na = new int[getGridCols()];          System.arraycopy(colWidths, 0, na, 0, Math.min(colWidths.length, na.length));          colWidths = na;        }      }      else colWidths = new int[getGridCols()];      for (; start<colWidths.length; start++) colWidths[start] = DEF_COLWIDTH;      start = 0;      if (rowHeights != null)      {        start = rowHeights.length;        if (rowHeights.length != getGridRows())        {          int[] na = new int[getGridRows()];          System.arraycopy(rowHeights, 0, na, 0, Math.min(rowHeights.length, na.length));          rowHeights = na;        }      }      else rowHeights = new int[getGridRows()];      for (;start<rowHeights.length; start++) rowHeights[start] = DEF_ROWHEIGHT;    }   /**    * Invalidates grid metric.    */    protected void iMetric()  {      bits = MathBox.getBits(bits, METRIC_VALID, false);      invalidate();    }   /**    * Returns the top caption size.    * @return a top caption size.    */    protected /*C#virtual*/ int getTopCaptionHeight() {      return (topCaption != null && topCaption.isVisible())?topCaption.getPreferredSize().height:0;      //return (topCaption != null && topCaption.isVisible())?topCaption.getHeight():0;    }   /**    * Returns the page size for the specified direction.    * @param <code>d</code> the specified direction. Use <code>-1</code> value to specify bottom-up direction and    * <code>1</code> value to specify up-bottom direction.    * @return a page size.    */    protected /*C#virtual*/ int pageSize(int d)    {      int off = controller.getOffset();      if (off >= 0)      {         Rectangle visibleArea = getVisiblePart();         visibleArea.height -= getTopCaptionHeight();         int sum = 0, poff = off;         for (;off >=0 && off < getGridRows() && sum < visibleArea.height; sum += rowHeight(off) + netSize, off+=d);         return Math.abs(poff - off);      }      return 0;    }   /**    * Invalidates columns visibility properties.    */    protected /*C#virtual*/ void iColVisibility() {      bits = MathBox.getBits(bits, COLVIS_VALID, false);    }   /**    * Invalidates rows visibility properties.    */    protected /*C#virtual*/ void iRowVisibility() {      bits = MathBox.getBits(bits, ROWVIS_VALID, false);    }    private Point colVisibility(Point p, int d, boolean b)    {      Rectangle visibleArea = getVisiblePart();      int       cols        = getGridCols();      if (cols == 0 || p == null) return null;      int x   = p.y;      int col = p.x;      for (;col < cols && col >=0; col+=d)      {        if (x + dx < (visibleArea.x + visibleArea.width) && (x + colWidth(col) + dx) > visibleArea.x)        {          if (b) return new Point (col, x);        }        else if (!b) return colVisibility(new Point(col, x), (d > 0?-1:1), true);        if (d < 0) {          if (col > 0) x -= (colWidth(col - 1) + netSize);        }        else if (col < cols - 1) x += (colWidth(col) + netSize);      }      if (!b)        return (d > 0)?new Point(col - 1, x):                       new Point(0, getInsets().left + netSize);      return null;    }    private Point rowVisibility(Point p, int d, boolean b)    {      Rectangle visibleArea = getVisiblePart();      int       rows        = getGridRows();      if (rows == 0 || p == null) return null;      int y   = p.y;      int row = p.x;      for (;row < rows && row >= 0; row+=d)      {        if (y + dy < (visibleArea.y + visibleArea.height) && (y + rowHeight(row) + dy) > visibleArea.y)        {          if (b) return new Point (row, y);        }        else if (!b) return rowVisibility(new Point(row, y), (d > 0?-1:1), true);        if (d < 0)  {          if (row > 0) y -= (rowHeight(row - 1) + netSize);        }        else if (row < rows - 1) y += (rowHeight(row) + netSize);      }      if (!b)        return (d > 0)?new Point(row - 1, y):                       new Point(0, getInsets().top + getTopCaptionHeight() + netSize);      return null;    }    private void rVisibility (int offx, int offy)    {      boolean b = visibility.hasVisibleCells();      if (!MathBox.checkBit(bits, COLVIS_VALID))      {        if (offx > 0 && b)        {          visibility.lc = colVisibility(visibility.lc, -1, true);          visibility.fc = colVisibility(visibility.lc,-1, false);        }        else        if (offx < 0 && b)        {          visibility.fc = colVisibility(visibility.fc, 1, true);          visibility.lc = colVisibility(visibility.fc, 1, false);        }        else        {          visibility.fc = new Point(0, getInsets().left + netSize);          visibility.fc = colVisibility(visibility.fc, 1, true);          visibility.lc = colVisibility(visibility.fc, 1, false);        }        bits = MathBox.getBits(bits, COLVIS_VALID, true);      }      if (!MathBox.checkBit(bits, ROWVIS_VALID))      {        if (offy > 0 && b)        {          visibility.lr = rowVisibility(visibility.lr,  -1, true);          visibility.fr = rowVisibility(visibility.lr , -1, false);        }        else        if (offy < 0 && b)        {          visibility.fr = rowVisibility(visibility.fr, 1, true);          visibility.lr = rowVisibility(visibility.fr, 1, false);        }        else        {          visibility.fr = new Point(0, getInsets().top + getTopCaptionHeight() + netSize);          visibility.fr = rowVisibility(visibility.fr, 1, true);          visibility.lr = rowVisibility(visibility.fr, 1, false);        }        bits = MathBox.getBits(bits, ROWVIS_VALID, true);      }    }    private Point calcOrigin(int off, int y)    {      Insets i = getInsets();      i.top += getTopCaptionHeight();      Point o = LwToolkit.calcOrigin(getColX(0) - netSize, y - netSize, psSize.width, rowHeight(off) + 2*netSize, dx, dy, this, i);      if (man != null) man.scrollObjMoved(o.x, o.y);      else             setSOLocation(o.x, o.y);      return o;    } }

⌨️ 快捷键说明

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