Java tutorial
/* * Zettelkasten - nach Luhmann ** Copyright (C) 2001-2014 by Daniel Ldecke (http://www.danielluedecke.de) * * Homepage: http://zettelkasten.danielluedecke.de * * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; * if not, see <http://www.gnu.org/licenses/>. * * * Dieses Programm ist freie Software. Sie knnen es unter den Bedingungen der GNU * General Public License, wie von der Free Software Foundation verffentlicht, weitergeben * und/oder modifizieren, entweder gem Version 3 der Lizenz oder (wenn Sie mchten) * jeder spteren Version. * * Die Verffentlichung dieses Programms erfolgt in der Hoffnung, da es Ihnen von Nutzen sein * wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder * der VERWENDBARKEIT FR EINEN BESTIMMTEN ZWECK. Details finden Sie in der * GNU General Public License. * * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm * erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>. */ package de.danielluedecke.zettelkasten.tasks.export; import de.danielluedecke.zettelkasten.database.BibTex; import de.danielluedecke.zettelkasten.database.Bookmarks; import de.danielluedecke.zettelkasten.database.Daten; import de.danielluedecke.zettelkasten.database.TasksData; import de.danielluedecke.zettelkasten.util.Constants; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.logging.Level; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.swing.JOptionPane; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.output.XMLOutputter; /** * * @author Luedeke */ public class ExportToZknTask extends org.jdesktop.application.Task<Object, Void> { /** * Reference to the CDaten object, which contains the XML data of the Zettelkasten * will be passed as parameter in the constructor, see below */ private Daten dataObj; /** * */ private Bookmarks bookmarksObj; private BibTex bibtexObj; /** * */ private TasksData taskinfo; /** * file path to export file */ private File filepath; /** * */ private ArrayList<Object> exportentries; /** * */ private boolean exportOk; private boolean exportbibtex; /** * */ private boolean showOkMessage = true; /** * */ private javax.swing.JDialog parentDialog; private javax.swing.JLabel msgLabel; /** * get the strings for file descriptions from the resource map */ private org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application .getInstance(de.danielluedecke.zettelkasten.ZettelkastenApp.class).getContext() .getResourceMap(ExportTask.class); public ExportToZknTask(org.jdesktop.application.Application app, javax.swing.JDialog parent, javax.swing.JLabel label, TasksData td, Daten d, Bookmarks bm, BibTex bib, boolean exportBib, File fp, ArrayList<Object> ee) { super(app); dataObj = d; bookmarksObj = bm; bibtexObj = bib; filepath = fp; exportentries = ee; exportOk = true; taskinfo = td; parentDialog = parent; msgLabel = label; exportbibtex = exportBib; // the variable "exportentries" stores all entry-numbers of those entries that should be exported. // if this array is null, we assume that *all* entries have to be exported. thus, insert // all entry-numbers here if (null == exportentries) { exportentries = new ArrayList<Object>(); // copy all entry-numbers to array. remember that the entrynumbers range from 1 to site of file. for (int cnt = 0; cnt < dataObj.getCount(Daten.ZKNCOUNT); cnt++) { // only add entries that are not empty if (!dataObj.isEmpty(cnt + 1)) { exportentries.add(cnt + 1); } } } // show status text msgLabel.setText(resourceMap.getString("msg1")); } @Override protected Object doInBackground() { // Your Task's code here. This method runs // on a background thread, so don't reference // the Swing GUI from here. // prevent task from processing when the file path is incorrect // if no file exists, exit task if (null == filepath) { showOkMessage = false; return null; } // check whether file already exists if (filepath.exists()) { // file exists, ask user to overwrite it... int optionDocExists = JOptionPane.showConfirmDialog(null, resourceMap.getString("askForOverwriteFileMsg", "", filepath.getName()), resourceMap.getString("askForOverwriteFileTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); // if the user does *not* choose to overwrite, quit... if (optionDocExists != JOptionPane.YES_OPTION) { // don't show "export was OK" message in main frame showOkMessage = false; return null; } } // yet everything is ok... exportOk = true; // create list with all export entries... ArrayList<Integer> entrylist = new ArrayList<Integer>(); // go through all elements of the data file for (int cnt = 0; cnt < exportentries.size(); cnt++) { try { // retrieve zettelnumber int zettelnummer = Integer.parseInt(exportentries.get(cnt).toString()); // and add it to list. entrylist.add(zettelnummer); } catch (NumberFormatException e) { } } // sort array Collections.sort(entrylist); // create document for exporting the entries dataObj.createExportEntries(entrylist); // create export-XML-file for bookmarks Document bookmarks = new Document(new Element("bookmarks")); // iterate all bookmarks for (int cnt = 0; cnt < bookmarksObj.getCount(); cnt++) { // retrieve each bookmarked entry number int bookmarkpos = bookmarksObj.getBookmarkEntry(cnt); // check whether bookmarked entry is in export list if (entrylist.contains(bookmarkpos)) { // create new bookmark element Element bookmark = new Element("bookmark"); // add zettel-id as attribute bookmark.setAttribute("id", dataObj.getZettelID(bookmarkpos)); // add bookmark-category as attribute bookmark.setAttribute("cat", bookmarksObj.getBookmarkCategoryAsString(cnt)); // add comment as text bookmark.setText(bookmarksObj.getComment(cnt)); // add element to XML file bookmarks.getRootElement().addContent(bookmark); } } // TODO suchergebnisse nummern in id's umwandeln und mitexportieren // TODO schreibtisch-Daten: zettel-nummern in id's umwandeln und mitexportieren // export data to zkn3-file try { // open the outputstream ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(filepath)); // I first wanted to use a pretty output format, so advanced users who // extract the data file can better watch the xml-files. but somehow, this // lead to an error within the method "retrieveElement" in the class "CDaten.java", // saying the a org.jdom.text cannot be converted to org.jdom.element?!? // XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); XMLOutputter out = new XMLOutputter(); // show status text msgLabel.setText(resourceMap.getString("msg2")); // save metainformation zip.putNextEntry(new ZipEntry(Constants.metainfFileName)); out.output(dataObj.getMetaInformationData(), zip); // save main data. zip.putNextEntry(new ZipEntry(Constants.zknFileName)); out.output(dataObj.retrieveExportDocument(), zip); // save authors zip.putNextEntry(new ZipEntry(Constants.authorFileName)); out.output(dataObj.getAuthorData(), zip); // save keywords zip.putNextEntry(new ZipEntry(Constants.keywordFileName)); out.output(dataObj.getKeywordData(), zip); // save bookmarks zip.putNextEntry(new ZipEntry(Constants.bookmarksFileName)); out.output(bookmarks, zip); // close zip-stream zip.close(); } catch (IOException e) { // log error-message Constants.zknlogger.log(Level.SEVERE, e.getLocalizedMessage()); // change error-indicator exportOk = false; } catch (SecurityException e) { // log error-message Constants.zknlogger.log(Level.SEVERE, e.getLocalizedMessage()); // change error-indicator exportOk = false; } // if the user requested a bibtex-export, do this now if (exportbibtex) { // show status text msgLabel.setText(resourceMap.getString("msgBibtextExport")); // write bibtex file ExportTools.writeBibTexFile(dataObj, bibtexObj, exportentries, filepath, resourceMap); } return null; // return your result } @Override protected void succeeded(Object result) { // Runs on the EDT. Update the GUI based on // the result computed by doInBackground(). } @Override protected void finished() { super.finished(); taskinfo.setExportOk(exportOk); taskinfo.setShowExportOkMessage(showOkMessage); // Close Window parentDialog.setVisible(false); parentDialog.dispose(); } }