示例#1
0
  /** Call to explicitly go to the next page from within a single draw(). */
  public void nextPage() {
    PStyle savedStyle = getStyle();
    g2.dispose();

    try {
      //    writer.setPageEmpty(false);  // maybe useful later
      document.newPage(); // is this bad if no addl pages are made?
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (textMode == SHAPE) {
      g2 = content.createGraphicsShapes(width, height);
    } else if (textMode == MODEL) {
      g2 = content.createGraphics(width, height, mapper);
    }
    style(savedStyle);

    // should there be a beginDraw/endDraw in here?
  }
  /**
   * @param shiftsToAccount
   * @param month
   * @param year
   * @return
   */
  public static String createAccounting(ShiftInstance[] shiftsToAccount, int month, int year) {
    // check if personal Data exists
    if (!PersonalData.getInstance().isDataSet()) {
      UtilityBox.getInstance()
          .displayInfoPopup(
              "Fehlende Daten",
              "Um die " + "Abrechnung zu erstellen müssen\npersönliche Daten gespeichert sein.");
      return null;
    }
    boolean success = false;
    String filePath = "Abrechnungen/" + year;
    // create directory if nessessary
    UtilityBox.createDirectory(filePath);
    // change filePath from directory to file
    filePath = filePath + "/Abrechnung" + UtilityBox.getMonthString(month) + year + ".pdf";
    Document accounting = new Document();

    ArrayList<ShiftInstance> rd = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> ktp = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> baby = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> breisach = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> kiza = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> event = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> sc = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> concert_hall = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> kvs = new ArrayList<ShiftInstance>();
    ArrayList<ShiftInstance> elw = new ArrayList<ShiftInstance>();

    // add shifts to seperate shift lists
    for (int i = 0; i < shiftsToAccount.length; i++) {
      switch (shiftsToAccount[i].getType()) {
        case KTW:
          ktp.add(shiftsToAccount[i]);
          break;
        case RTW:
          rd.add(shiftsToAccount[i]);
          break;
        case HINTERGRUND:
          rd.add(shiftsToAccount[i]);
          break;
        case ELW:
          elw.add(shiftsToAccount[i]);
          break;
        case EVENT:
          event.add(shiftsToAccount[i]);
          break;
        case SC:
          sc.add(shiftsToAccount[i]);
          break;
        case CONCERT_HALL:
          concert_hall.add(shiftsToAccount[i]);
          break;
        case KVS:
          kvs.add(shiftsToAccount[i]);
          break;
        case BREISACH:
          breisach.add(shiftsToAccount[i]);
          break;
        case KIZA:
          kiza.add(shiftsToAccount[i]);
          break;
        default:
          break;
      }
    }
    ArrayList<ShiftInstance>[] allShifts =
        (ArrayList<ShiftInstance>[])
            (new ArrayList[] {rd, elw, ktp, baby, breisach, kiza, event, sc, concert_hall, kvs});
    try {
      accounting.setPageSize(PageSize.A4);
      PdfWriter pdfWriter = PdfWriter.getInstance(accounting, new FileOutputStream(filePath));
      accounting.open();
      for (int i = 0; i < allShifts.length; i++) {
        if (!allShifts[i].isEmpty()) {
          int numberOfPages = ((int) (allShifts[i].size() / 13)) + 1;
          int counter = allShifts[i].size() - 1;
          for (int j = 1; j <= numberOfPages; j++) {
            ArrayList<ShiftInstance> tempShiftInstances = new ArrayList<ShiftInstance>();
            for (int k = 0; k < 13; k++) {
              if (counter < 0) {
                break;
              }
              tempShiftInstances.add(allShifts[i].get(counter));
              counter--;
            }
            accounting.newPage();
            success = createSingleAccounting(accounting, pdfWriter, tempShiftInstances, i * 10 + j);
          }
        }
      }
      // TODO: for JDK7 use Multicatch
    } catch (Exception e) { // DocumentException | IOException e) {
      UtilityBox.getInstance()
          .displayErrorPopup(
              "Abrechnung", "Fehler beim " + "Erstellen der Abrechnung:\n" + e.getMessage());
      filePath = null;
    } finally {
      try {
        accounting.close();
      } catch (Exception e) {
        UtilityBox.getInstance()
            .displayErrorPopup(
                "Abrechnung",
                "Fehler beim "
                    + "Erstellen der Abrechnung:\nDokument nicht geschlossen:\n"
                    + ""
                    + e.getMessage());
        filePath = null;
      }
    }
    return filePath;
  }