public void selectionChanged(SelectionChangedEvent event) {
   Object[] sel = getSelection();
   if (sel != null && sel.length != 0) {
     if (sel[0] instanceof Tree<?>) {
       sel[0] = ((Tree<?>) sel[0]).contents;
     }
     if (sel[0] instanceof PersistentObject) {
       ElexisEventDispatcher.fireSelectionEvent((PersistentObject) sel[0]);
     }
   }
   if (selChangeListener != null) selChangeListener.selectionChanged(event);
 }
  /** print a bill into a text container */
  public Result<Rechnung> doPrint(final Rechnung rn) {
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);

    Result<Rechnung> ret = new Result<Rechnung>();
    fall = rn.getFall();
    ElexisEventDispatcher.fireSelectionEvent(fall);
    Kontakt adressat = fall.getGarant(); // .getRequiredContact("Rechnungsempfänger");
    if (!adressat.isValid()) {
      adressat = fall.getPatient();
    }
    tc.createFromTemplateName(
        null,
        PrivaterechnungTextTemplateRequirement.getBillTemplate(),
        Brief.RECHNUNG,
        adressat,
        rn.getNr());
    fillFields();
    List<Konsultation> kons = rn.getKonsultationen();
    Collections.sort(
        kons,
        new Comparator<Konsultation>() {
          TimeTool t0 = new TimeTool();
          TimeTool t1 = new TimeTool();

          public int compare(final Konsultation arg0, final Konsultation arg1) {
            t0.set(arg0.getDatum());
            t1.set(arg1.getDatum());
            return t0.compareTo(t1);
          }
        });
    Object pos = null;
    // Das Wort Leistungen soll jeder selbst in die Vorlage nehmen:
    // pos = tc.getPlugin().insertText("[Leistungen]", "Leistungen\n\n", SWT.LEFT);
    pos = tc.getPlugin().insertText("[Leistungen]", "", SWT.LEFT);
    Money sum = new Money();
    VatRateSum vatSummer = new VatRateSum();
    for (Konsultation k : kons) {
      tc.getPlugin().setStyle(SWT.BOLD);
      // print date
      pos =
          tc.getPlugin()
              .insertText(
                  pos, new TimeTool(k.getDatum()).toString(TimeTool.DATE_GER) + "\n", SWT.LEFT);
      tc.getPlugin().setStyle(SWT.NORMAL);
      // print header for Leistungen
      StringBuilder header = new StringBuilder();
      header
          .append("Anzahl")
          .append("\t")
          .append("MWSt.")
          .append("\t")
          .append("Einzelpreis")
          .append("\t")
          .append("Betrag")
          .append("\n\n");
      pos = tc.getPlugin().insertText(pos, header.toString(), SWT.LEFT);
      // print info for each Leistung
      for (Verrechnet vv : k.getLeistungen()) {
        tc.getPlugin().setStyle(SWT.BOLD);
        pos = tc.getPlugin().insertText(pos, "- " + vv.getText() + "\n", SWT.LEFT);
        tc.getPlugin().setStyle(SWT.NORMAL);

        Money preis = vv.getNettoPreis();
        int zahl = vv.getZahl();
        Money subtotal = new Money(preis);
        subtotal.multiply(zahl);
        StringBuilder sb = new StringBuilder();
        sb.append(zahl)
            .append("\t")
            .append(getVatRate(vv, subtotal, vatSummer))
            .append("\t")
            .append(preis.getAmountAsString())
            .append("\t")
            .append(subtotal.getAmountAsString())
            .append("\n");
        pos = tc.getPlugin().insertText(pos, sb.toString(), SWT.LEFT);
        sum.addMoney(subtotal);
      }
    }
    pos =
        tc.getPlugin()
            .insertText(
                pos,
                "____________________________________________________________________\nTotal:\t\t\t"
                    + sum.getAmountAsString(),
                SWT.LEFT);

    // print vat info of whole bill
    String vatNumber = rn.getMandant().getRechnungssteller().getInfoString(VAT_MANDANTVATNUMBER);
    tc.getPlugin().setStyle(SWT.BOLD);
    pos = tc.getPlugin().insertText(pos, "\n\nMWSt.Nr. \t", SWT.LEFT);
    tc.getPlugin().setStyle(SWT.NORMAL);
    if (vatNumber != null && vatNumber.length() > 0)
      pos = tc.getPlugin().insertText(pos, vatNumber + "\n", SWT.LEFT);
    else pos = tc.getPlugin().insertText(pos, "keine\n", SWT.LEFT);

    tc.getPlugin().setStyle(SWT.BOLD);
    pos = tc.getPlugin().insertText(pos, "\nSatz\tBetrag\tMWSt\n", SWT.LEFT);
    tc.getPlugin().setStyle(SWT.NORMAL);

    VatRateElement[] vatValues = vatSummer.rates.values().toArray(new VatRateElement[0]);
    Arrays.sort(vatValues);
    for (VatRateElement rate : vatValues) {
      StringBuilder sb = new StringBuilder();
      sb.append(nf.format(rate.scale))
          .append("\t")
          .append(nf.format(rate.sumamount))
          .append("\t")
          .append(nf.format(rate.sumvat))
          .append("\n");
      pos = tc.getPlugin().insertText(pos, sb.toString(), SWT.LEFT);
    }

    tc.getPlugin().setStyle(SWT.BOLD);
    pos =
        tc.getPlugin()
            .insertText(
                pos,
                "\nTotal\t" + sum.getAmountAsString() + "\t" + nf.format(vatSummer.sumvat) + "\n",
                SWT.LEFT);
    tc.getPlugin().setStyle(SWT.NORMAL);

    String toPrinter = CoreHub.localCfg.get("Drucker/A4/Name", null);
    tc.getPlugin().print(toPrinter, null, false);
    tc.createFromTemplateName(null, templateESR, Brief.RECHNUNG, adressat, rn.getNr());
    fillFields();
    ESR esr =
        new ESR(
            CoreHub.globalCfg.get(PreferenceConstants.esrIdentity, ""),
            CoreHub.globalCfg.get(PreferenceConstants.esrUser, ""),
            rn.getRnId(),
            27);
    Kontakt bank = Kontakt.load(CoreHub.globalCfg.get(PreferenceConstants.cfgBank, ""));
    if (!bank.isValid()) {
      SWTHelper.showError("Keine Bank", "Bitte geben Sie eine Bank für die Zahlungen ein");
    }
    esr.printBESR(bank, adressat, rn.getMandant(), sum.getCentsAsString(), tc);
    tc.replace("\\[Leistungen\\]", sum.getAmountAsString());
    tc.getPlugin().print(CoreHub.localCfg.get("Drucker/A4ESR/Name", null), null, false);
    tc.getPlugin().setFont(null, SWT.NORMAL, 0.0f);
    return ret;
  }