|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * WollMux |
| 4 | + * %% |
| 5 | + * Copyright (C) 2005 - 2020 Landeshauptstadt München |
| 6 | + * %% |
| 7 | + * Licensed under the EUPL, Version 1.1 or – as soon they will be |
| 8 | + * approved by the European Commission - subsequent versions of the |
| 9 | + * EUPL (the "Licence"); |
| 10 | + * |
| 11 | + * You may not use this work except in compliance with the Licence. |
| 12 | + * You may obtain a copy of the Licence at: |
| 13 | + * |
| 14 | + * http://ec.europa.eu/idabc/eupl5 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the Licence is distributed on an "AS IS" basis, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the Licence for the specific language governing permissions and |
| 20 | + * limitations under the Licence. |
| 21 | + * #L% |
| 22 | + */ |
| 23 | +package de.muenchen.allg.itd51.wollmux.event.handlers; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.List; |
| 30 | +import java.util.ListIterator; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Map.Entry; |
| 33 | + |
| 34 | +import de.muenchen.allg.itd51.wollmux.WollMuxFehlerException; |
| 35 | +import de.muenchen.allg.itd51.wollmux.document.DocumentManager; |
| 36 | +import de.muenchen.allg.itd51.wollmux.document.TextDocumentController; |
| 37 | +import de.muenchen.allg.itd51.wollmux.event.WollMuxEventHandler; |
| 38 | +import de.muenchen.allg.itd51.wollmux.util.L; |
| 39 | + |
| 40 | +import com.sun.star.ui.dialogs.FilePicker; |
| 41 | +import com.sun.star.ui.dialogs.TemplateDescription; |
| 42 | +import com.sun.star.ui.dialogs.XFilePicker3; |
| 43 | +import com.sun.star.uno.UnoRuntime; |
| 44 | +import com.sun.star.text.XTextDocument; |
| 45 | +import com.sun.star.document.XEventListener; |
| 46 | +import com.sun.star.lang.EventObject; |
| 47 | + |
| 48 | +import org.slf4j.Logger; |
| 49 | +import org.slf4j.LoggerFactory; |
| 50 | + |
| 51 | +import de.muenchen.allg.itd51.wollmux.dialog.InfoDialog; |
| 52 | +import de.muenchen.allg.afid.UNO; |
| 53 | +import de.muenchen.allg.afid.UnoHelperException; |
| 54 | + |
| 55 | + |
| 56 | +/** |
| 57 | + * Event for importing content of formular. |
| 58 | + */ |
| 59 | +public class OnImportFormularinhalt extends WollMuxEvent |
| 60 | +{ |
| 61 | + |
| 62 | + private static final Logger LOGGER = LoggerFactory.getLogger(OnImportFormularinhalt.class); |
| 63 | + |
| 64 | + private TextDocumentController documentController; |
| 65 | + /** |
| 66 | + * Create this event. |
| 67 | + * |
| 68 | + * @param documentController |
| 69 | + * The document associated with the FormularMax. |
| 70 | + */ |
| 71 | + public OnImportFormularinhalt(TextDocumentController documentController) |
| 72 | + { |
| 73 | + this.documentController = documentController; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void doit() throws WollMuxFehlerException |
| 79 | + { |
| 80 | + XFilePicker3 picker = FilePicker.createWithMode(UNO.defaultContext, |
| 81 | + TemplateDescription.FILEOPEN_SIMPLE); |
| 82 | + String filterName = "Tabellendokument"; |
| 83 | + picker.appendFilter(filterName, "*.odt"); |
| 84 | + picker.appendFilter("Alle Dateien", "*"); |
| 85 | + picker.setCurrentFilter(filterName); |
| 86 | + picker.setMultiSelectionMode(false); |
| 87 | + |
| 88 | + short res = picker.execute(); |
| 89 | + if (res != com.sun.star.ui.dialogs.ExecutableDialogResults.OK) |
| 90 | + return; |
| 91 | + |
| 92 | + String[] files = picker.getFiles(); |
| 93 | + try |
| 94 | + { |
| 95 | + XTextDocument importDoc = UNO.XTextDocument(UNO.loadComponentFromURL(files[0], false, false, true)); |
| 96 | + |
| 97 | + XEventListener listener = new XEventListener() |
| 98 | + { |
| 99 | + @Override |
| 100 | + public void disposing(EventObject arg0) |
| 101 | + { |
| 102 | + // nothing to do |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void notifyEvent(com.sun.star.document.EventObject event) |
| 107 | + { |
| 108 | + if (importDoc!=null |
| 109 | + && UnoRuntime.areSame(importDoc, event.Source) |
| 110 | + && WollMuxEventHandler.ON_WOLLMUX_PROCESSING_FINISHED.equals(event.EventName)) |
| 111 | + { |
| 112 | + new OnRemoveDocumentEventListener(this).emit(); |
| 113 | + try(ByteArrayOutputStream out = new ByteArrayOutputStream()) |
| 114 | + { |
| 115 | + TextDocumentController importDocumentController = |
| 116 | + DocumentManager.getTextDocumentController(importDoc); |
| 117 | + Map<String, String> id2value = importDocumentController.getModel().getFormFieldValuesMap(); |
| 118 | + Map<String, String> id2valueTo = documentController.getModel().getFormFieldValuesMap(); |
| 119 | + List<String> notfound = new ArrayList<String>(); |
| 120 | + for (Entry<String, String> e : id2value.entrySet()) |
| 121 | + { |
| 122 | + if(id2valueTo.containsKey(e.getKey())) |
| 123 | + { |
| 124 | + if (e.getValue() != null) |
| 125 | + { |
| 126 | + new OnSetFormValue(documentController.getModel().doc, e.getKey(), |
| 127 | + e.getValue(), null).emit(); |
| 128 | + } |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + notfound.add(e.getKey()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if(!notfound.isEmpty()) |
| 137 | + { |
| 138 | + String msg = L.m("Die folgenden Schlüssel können nicht importiert werden:\n") + |
| 139 | + String.join("\n", notfound); |
| 140 | + |
| 141 | + InfoDialog.showInfoModal(L.m("WollMux"), msg ); |
| 142 | + } |
| 143 | + } catch(Exception e) |
| 144 | + { |
| 145 | + LOGGER.error("", e); |
| 146 | + InfoDialog.showInfoModal(L.m("WollMux-Fehler"), e.getMessage()); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + new OnAddDocumentEventListener(listener).emit(); |
| 153 | + } catch ( UnoHelperException e ) |
| 154 | + { |
| 155 | + LOGGER.error("", e); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public String toString() |
| 161 | + { |
| 162 | + return this.getClass().getSimpleName() + "(" + documentController.getModel() |
| 163 | + + ")"; |
| 164 | + } |
| 165 | +} |
0 commit comments