Ejemplo n.º 1
0
  /**
   * Performs spreadsheet drag-copy operation.
   *
   * @param sx1 source minimum column
   * @param sy1 source minimum row
   * @param sx2 source maximum column
   * @param sy2 source maximum row
   * @param dx1 destination minimum column
   * @param dy1 destination minimum row
   * @param dx2 destination maximum column
   * @param dy2 destination maximum row
   * @return
   */
  public boolean doDragCopy(
      int sx1, int sy1, int sx2, int sy2, int dx1, int dy1, int dx2, int dy2) {
    // -|1|-
    // 2|-|3
    // -|4|-
    app.setWaitCursor();
    Construction cons = kernel.getConstruction();

    try {
      boolean success = false;

      // collect all redefine operations
      cons.startCollectingRedefineCalls();

      boolean patternOK = isPatternSource(new CellRange(app, sx1, sy1, sx2, sy2));

      // ==============================================
      // vertical drag
      // ==============================================
      if ((sx1 == dx1) && (sx2 == dx2)) {

        if (dy2 < sy1) { // 1 ----- drag up
          if (((sy1 + 1) == sy2) && patternOK) {
            // two row source, so drag copy a linear pattern
            for (int x = sx1; x <= sx2; ++x) {
              GeoElement v1 = getValue(app, x, sy1);
              GeoElement v2 = getValue(app, x, sy2);
              if ((v1 == null) || (v2 == null)) {
                continue;
              }
              for (int y = dy2; y >= dy1; --y) {
                GeoElement v3 = getValue(app, x, y + 2);
                GeoElement v4 = getValue(app, x, y + 1);
                String vs1 = v3.isGeoFunction() ? "(x)" : "";
                String vs2 = v4.isGeoFunction() ? "(x)" : "";
                String d0 = GeoElementSpreadsheet.getSpreadsheetCellName(x, y + 2) + vs1;
                String d1 = GeoElementSpreadsheet.getSpreadsheetCellName(x, y + 1) + vs2;
                String text = "=2*" + d1 + "-" + d0;
                doCopyNoStoringUndoInfo1(kernel, app, text, v4, x, y);
              }
            }
          } else { // not two row source, so drag-copy the first row
            // of the source
            doCopyVerticalNoStoringUndoInfo1(sx1, sx2, sy1, dy1, dy2);
          }
          success = true;
        } else if (dy1 > sy2) { // 4 ---- drag down
          if (((sy1 + 1) == sy2) && patternOK) {
            // two row source, so drag copy a linear pattern
            for (int x = sx1; x <= sx2; ++x) {
              GeoElement v1 = getValue(app, x, sy1);
              GeoElement v2 = getValue(app, x, sy2);
              if ((v1 == null) || (v2 == null)) {
                continue;
              }
              for (int y = dy1; y <= dy2; ++y) {
                GeoElement v3 = getValue(app, x, y - 2);
                GeoElement v4 = getValue(app, x, y - 1);
                String vs1 = v3.isGeoFunction() ? "(x)" : "";
                String vs2 = v4.isGeoFunction() ? "(x)" : "";
                String d0 = GeoElementSpreadsheet.getSpreadsheetCellName(x, y - 2) + vs1;
                String d1 = GeoElementSpreadsheet.getSpreadsheetCellName(x, y - 1) + vs2;
                String text = "=2*" + d1 + "-" + d0;
                doCopyNoStoringUndoInfo1(kernel, app, text, v4, x, y);
              }
            }
          } else {
            // not two row source, so drag-copy the last row of the
            // source
            doCopyVerticalNoStoringUndoInfo1(sx1, sx2, sy2, dy1, dy2);
          }
          success = true;
        }
      }

      // ==============================================
      // horizontal drag
      // ==============================================
      else if ((sy1 == dy1) && (sy2 == dy2)) {
        if (dx2 < sx1) { // 2 ---- drag left
          if (((sx1 + 1) == sx2) && patternOK) {
            // two column source, so drag copy a linear pattern
            for (int y = sy1; y <= sy2; ++y) {
              GeoElement v1 = getValue(app, sx1, y);
              GeoElement v2 = getValue(app, sx2, y);
              if ((v1 == null) || (v2 == null)) {
                continue;
              }
              for (int x = dx2; x >= dx1; --x) {
                GeoElement v3 = getValue(app, x + 2, y);
                GeoElement v4 = getValue(app, x + 1, y);
                String vs1 = v3.isGeoFunction() ? "(x)" : "";
                String vs2 = v4.isGeoFunction() ? "(x)" : "";
                String d0 = GeoElementSpreadsheet.getSpreadsheetCellName(x + 2, y) + vs1;
                String d1 = GeoElementSpreadsheet.getSpreadsheetCellName(x + 1, y) + vs2;
                String text = "=2*" + d1 + "-" + d0;
                doCopyNoStoringUndoInfo1(kernel, app, text, v4, x, y);
              }
            }
          } else {
            // not two column source, so drag-copy the first column
            // of the source
            doCopyHorizontalNoStoringUndoInfo1(sy1, sy2, sx1, dx1, dx2);
          }
          success = true;
        } else if (dx1 > sx2) { // 4 --- drag right
          if (((sx1 + 1) == sx2) && patternOK) {
            // two column source, so drag copy a linear pattern
            for (int y = sy1; y <= sy2; ++y) {
              GeoElement v1 = getValue(app, sx1, y);
              GeoElement v2 = getValue(app, sx2, y);
              if ((v1 == null) || (v2 == null)) {
                continue;
              }
              for (int x = dx1; x <= dx2; ++x) {
                GeoElement v3 = getValue(app, x - 2, y);
                GeoElement v4 = getValue(app, x - 1, y);
                String vs1 = v3.isGeoFunction() ? "(x)" : "";
                String vs2 = v4.isGeoFunction() ? "(x)" : "";
                String d0 = GeoElementSpreadsheet.getSpreadsheetCellName(x - 2, y) + vs1;
                String d1 = GeoElementSpreadsheet.getSpreadsheetCellName(x - 1, y) + vs2;
                String text = "=2*" + d1 + "-" + d0;
                doCopyNoStoringUndoInfo1(kernel, app, text, v4, x, y);
              }
            }
          } else {
            // not two column source, so drag-copy the last column
            // of the source
            doCopyHorizontalNoStoringUndoInfo1(sy1, sy2, sx2, dx1, dx2);
          }
          success = true;
        }
      }

      // now do all redefining and build new construction
      cons.processCollectedRedefineCalls();

      if (success) {
        return true;
      }

      String msg =
          "sx1 = " + sx1 + "\r\n" + "sy1 = " + sy1 + "\r\n" + "sx2 = " + sx2 + "\r\n" + "sy2 = "
              + sy2 + "\r\n" + "dx1 = " + dx1 + "\r\n" + "dy1 = " + dy1 + "\r\n" + "dx2 = " + dx2
              + "\r\n" + "dy2 = " + dy2 + "\r\n";
      throw new RuntimeException("Error from RelativeCopy.doCopy:\r\n" + msg);
    } catch (Exception ex) {
      // kernel.getApplication().showError(ex.getMessage());
      ex.printStackTrace();
      return false;
    } finally {
      cons.stopCollectingRedefineCalls();
      app.setDefaultCursor();
    }
  }