Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

In this page you can find the example usage for org.jdom2 Element addContent.

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method duplicates an entry and inserts it at the end or the next empty place in the
 * data file/*from  w  w w.java 2 s  . c om*/
 * 
 * @param nr the number of the entry that should be duplicated
 * @return 
 */
public boolean duplicateEntry(int nr) {
    // first of all, we duplicate all authors and keywords frequencies from the existing entry.
    // therefore, we first retrieve all author-index-numbers from that entry
    changeFrequencies(nr, 1);
    // retrieve entry that should be duplicated
    Element oldzettel = retrieveElement(zknFile, nr);
    // create new zettel
    Element zettel = new Element(ELEMENT_ZETTEL);
    // check whether we have any empty elements in between where we can insert the new entry
    int emptypos = retrieveFirstEmptyEntry();
    // if we have any empty elements...
    if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) {
        // retrieve empty element
        zettel = retrieveElement(zknFile, emptypos);
        // and remove former content, so we can add new content
        zettel.removeContent();
    }
    try {
        setZettelID(zettel);
        //
        // add title
        //
        // create child element with title information
        Element t = new Element(ELEMENT_TITLE);
        // and add it to the zettel-element
        zettel.addContent(t);
        // set value of the child element
        t.setText(oldzettel.getChild(ELEMENT_TITLE).getText());
        //
        // add content
        //
        // create child element with content information
        Element c = new Element(ELEMENT_CONTENT);
        // and add it to the zettel-element
        zettel.addContent(c);
        // set value of the content element
        c.setText(oldzettel.getChild(ELEMENT_CONTENT).getText());
        //
        // add author
        //
        // create child element with author information
        Element a = new Element(ELEMENT_AUTHOR);
        // and add it to the zettel-element
        zettel.addContent(a);
        // set value of author element
        a.setText(oldzettel.getChild(ELEMENT_AUTHOR).getText());
        //
        // add keywords
        //
        // create child element with keyword information
        Element k = new Element(ELEMENT_KEYWORD);
        // and add it to the zettel-element
        zettel.addContent(k);
        // store keyword index numbers
        k.setText(oldzettel.getChild(ELEMENT_KEYWORD).getText());
        //
        // now comes the manual links to other entries
        //
        Element m = new Element(ELEMENT_MANLINKS);
        zettel.addContent(m);
        m.setText("");
        //
        // add hyperlinks
        //
        // create child element with link information
        Element h = new Element(ELEMENT_ATTACHMENTS);
        // and add it to the zettel-element
        zettel.addContent(h);
        // add each hyperlink. therefor, iterate the array
        List<Element> links = oldzettel.getChild(ELEMENT_ATTACHMENTS).getChildren();
        Iterator<Element> i = links.iterator();
        while (i.hasNext()) {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            Element le = i.next();
            // and add the link-string from the array
            sublink.setText(le.getText());
            h.addContent(sublink);
        }
        //
        // add remarks
        //
        // create child element with content information
        Element r = new Element(ELEMENT_REMARKS);
        // and add it to the zettel-element
        zettel.addContent(r);
        // set value of the content element
        r.setText(oldzettel.getChild(ELEMENT_REMARKS).getText());
        //
        // add timestamp
        //
        // set creation timestamp, but set no text for edit timestamp
        // since the entry is not edited
        setTimestamp(zettel, Tools.getTimeStamp(), "");
        //
        // now comes the luhmann number
        //
        Element l = new Element(ELEMENT_TRAILS);
        zettel.addContent(l);
        l.setText(oldzettel.getChild(ELEMENT_TRAILS).getText());
        //
        // complete datafile
        //
        // if we have any empty elements, go on here
        if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) {
            // return the empty-position, which is now filled with the new author-value
            zettelPos = emptypos;
        } else {
            // finally, add the whole element to the data file
            zknFile.getRootElement().addContent(zettel);
            // set the zettel-position to the new entry
            zettelPos = getCount(ZKNCOUNT);
        }
        // duplicate this entry into the correct entry order
        // by changing the prev/nex references (or pointers) of the entries.
        changeZettelPointer(zettelPos, nr);
        // titles have to be updated.
        setTitlelistUpToDate(false);
        // set modified state
        setModified(true);
    } catch (IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method adds a new keyword item to the keyword xml datafile
 * /*from  www . java 2 s. c  om*/
 * @param kw the keyword which should be added
 * @param freq the new frequency of the keyword, or - if keyword already exists, e.g. in case
 * of merging entries or adding existing keywords to an entry - the increasement-step of the
 * frequency-occurences of existing keywords. use "1" if a keyword is simply added to an entry, so
 * in case the keyword already exists, its frequency is increased by 1.
 * @return position of the recently added keyword, or -1 if keyword could not be added
 */
public int addKeyword(String kw, int freq) {
    // check for valid value
    if (null == kw || kw.isEmpty()) {
        return -1;
    }
    // trim leading and trailing spaces
    kw = kw.trim();
    // if keyeord is empty, return
    if (kw.isEmpty()) {
        return -1;
    }
    // check whether author already exists
    int pos = getKeywordPosition(kw, false);
    // if keyword already exists, just increase counter
    if (pos != -1) {
        try {
            // retrieve existing author
            Element keyw = retrieveElement(keywordFile, pos);
            // get the count-value, which indicates the frequency of occurences of this
            // keywords in the whole data file
            int f = Integer.parseInt(keyw.getAttributeValue(ATTRIBUTE_FREQUENCIES));
            // increase frequency by 1
            // change timestamp attribute
            updateKeywordTimestampAndID(keyw, f + freq, Tools.getTimeStampWithMilliseconds(), null);
            // change modified state
            setModified(true);
            // and return keyword index-number
            return pos;
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        }
    }
    // check whether we have any empty elements in between where we can insert the keyword
    int emptypos = retrieveFirstEmptyElement(keywordFile);
    // if we have any empty elements, go on here
    if (emptypos != -1) {
        try {
            // retrieve empty element
            Element k = retrieveElement(keywordFile, emptypos);
            // set keyword string as new value
            k.setText(kw);
            // set frequency of occurences to 1
            // set timestamp attribute
            // set ID attribute
            // but first, check the length of "kw", because we want max. 5 first chars of kw
            // in keyword id
            String kwid;
            try {
                kwid = kw.substring(0, 5);
            } catch (IndexOutOfBoundsException ex) {
                kwid = kw;
            }
            updateKeywordTimestampAndID(k, freq, Tools.getTimeStampWithMilliseconds(),
                    String.valueOf(emptypos) + kwid + Tools.getTimeStampWithMilliseconds());
            // change list-up-to-date-state
            setKeywordlistUpToDate(false);
            // change modified state
            setModified(true);
            // return the empty-position, which is now filled with the new keyword-value
            return emptypos;
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        }
    }
    // get the root element of the keyword xml datafile
    else {
        try {
            Element kwFile = keywordFile.getRootElement();
            // create a new keyword element
            Element newKeyword = new Element(ELEMENT_ENTRY);
            // add the new keyword element to the keyword datafile
            try {
                kwFile.addContent(newKeyword);
                // and finally add the parameter (new keyword string) to the recently created
                // keyword element
                newKeyword.addContent(kw);
                // set frequency of occurences to 1
                // set timestamp attribute
                // set ID attribute
                // but first, check the length of "kw", because we want max. 5 first chars of kw
                // in keyword id
                String kwid;
                try {
                    kwid = kw.substring(0, 5);
                } catch (IndexOutOfBoundsException ex) {
                    kwid = kw;
                }
                updateKeywordTimestampAndID(newKeyword, freq, Tools.getTimeStampWithMilliseconds(),
                        String.valueOf(keywordFile.getRootElement().getContent().size()) + kwid
                                + Tools.getTimeStampWithMilliseconds());
                // change list-up-to-date-state
                setKeywordlistUpToDate(false);
                // change modified state
                setModified(true);
            } catch (IllegalAddException e) {
                // do nothing here
                Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            } catch (IllegalNameException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            } catch (IllegalDataException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            }
            // return the new size of the keyword file, i.e. the keyword position of 
            // the recently added keyword entry
            //
            // get a list with all entry-elements of the keyword data
            List<?> keywordList = keywordFile.getRootElement().getContent();
            // and return the size of this list
            return keywordList.size();
        } catch (IllegalStateException e) {
            Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            return -1;
        }
    }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method adds several keywords to the keyword xml datafile, without assigning them
 * to a certain entry//from ww w .  j  av  a2 s .  c om
 * 
 * @param kws the keywords which should be added
 */
public void addKeywordsToDatabase(String[] kws) {
    // if keyeord is empty, return
    if (null == kws || 0 == kws.length) {
        return;
    }
    // iterate all keywords
    for (String kw : kws) {
        // check whether keyword already exists
        int pos = getKeywordPosition(kw, false);
        // no, we have a new keyword. so add it...
        if (-1 == pos) {
            // check whether we have any empty elements in between where we can insert the keyword
            int emptypos = retrieveFirstEmptyElement(keywordFile);
            // if we have any empty elements, go on here
            if (emptypos != -1) {
                try {
                    // retrieve empty element
                    Element k = retrieveElement(keywordFile, emptypos);
                    // set keyword string as new value
                    k.setText(kw);
                    // set frequency of occurences to 0
                    // set timestamp attribute
                    // set ID attribute
                    // but first, check the length of "kw", because we want max. 5 first chars of kw
                    // in keyword id
                    String kwid;
                    try {
                        kwid = kw.substring(0, 5);
                    } catch (IndexOutOfBoundsException ex) {
                        kwid = kw;
                    }
                    updateKeywordTimestampAndID(k, 0, Tools.getTimeStampWithMilliseconds(),
                            String.valueOf(emptypos) + kwid + Tools.getTimeStampWithMilliseconds());
                    // change list-up-to-date-state
                    setKeywordlistUpToDate(false);
                    // change modified state
                    setModified(true);
                } catch (IllegalNameException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalDataException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                }
            }
            // get the root element of the keyword xml datafile
            else {
                Element kwFile = keywordFile.getRootElement();
                // create a new keyword element
                Element newKeyword = new Element(ELEMENT_ENTRY);
                // add the new keyword element to the keyword datafile
                try {
                    kwFile.addContent(newKeyword);
                    // and finally add the parameter (new keyword string) to the recently created
                    // keyword element
                    newKeyword.addContent(kw);
                    // set frequency of occurences to 0
                    // set timestamp attribute
                    // set ID attribute
                    // but first, check the length of "kw", because we want max. 5 first chars of kw
                    // in keyword id
                    String kwid;
                    try {
                        kwid = kw.substring(0, 5);
                    } catch (IndexOutOfBoundsException ex) {
                        kwid = kw;
                    }
                    updateKeywordTimestampAndID(newKeyword, 0, Tools.getTimeStampWithMilliseconds(),
                            String.valueOf(keywordFile.getRootElement().getContent().size()) + kwid
                                    + Tools.getTimeStampWithMilliseconds());
                    // change list-up-to-date-state
                    setKeywordlistUpToDate(false);
                    // change modified state
                    setModified(true);
                } catch (IllegalAddException e) {
                    // do nothing here
                    Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
                } catch (IllegalNameException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalDataException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                }
            }
        }
    }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method adds a new author item to the author xml datafile
 * @param auth the author which should be added
 * @param freq the new frequency of the author, or - if author already exists, e.g. in case
 * of merging entries or adding existing authors to an entry - the increasement-step of the
 * frequency-occurences of existing authors. use "1" if an author is simply added to an entry, so
 * in case the author already exists, its frequency is increased by 1.
 * @return position of the recently added author, or -1 if author could not be added
 *///from w ww .j  a  va  2  s.  c  o  m
public int addAuthor(String auth, int freq) {
    // trim leading and trailing spaces
    auth = auth.trim();
    // if author is empty, return
    if (auth.isEmpty())
        return -1;
    // check whether author already exists
    int pos = getAuthorPosition(auth);
    // if author already exists, just increase counter
    if (pos != -1) {
        try {
            // retrieve existing author
            Element au = retrieveElement(authorFile, pos);
            // get the count-value, which indicates the frequency of occurences of this
            // author in the whole data file
            int f = Integer.parseInt(au.getAttributeValue(ATTRIBUTE_FREQUENCIES));
            // increase frequency of occurences
            // change timestamp attribute
            updateAuthorTimestampAndID(au, f + freq, Tools.getTimeStampWithMilliseconds(), null);
            // change modified state
            setModified(true);
            // and return author index-number
            return pos;
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        }
    }
    // check whether we have any empty elements in between where we can insert the author
    int emptypos = retrieveFirstEmptyElement(authorFile);
    // if we have any empty elements, go on here
    if (emptypos != -1) {
        try {
            // retrieve empty element
            Element au = retrieveElement(authorFile, emptypos);
            // set author string as new value
            au.setText(auth);
            // set frequency of occurences to 1
            // set timestamp attribute
            // set ID attribute
            // but first, check the length of "auth", because we want max. 5 first chars of auth
            // in author id
            String auid;
            try {
                auid = auth.substring(0, 5);
            } catch (IndexOutOfBoundsException ex) {
                auid = auth;
            }
            updateAuthorTimestampAndID(au, freq, Tools.getTimeStampWithMilliseconds(),
                    String.valueOf(emptypos) + auid + Tools.getTimeStampWithMilliseconds());
            // change list-up-to-date-state
            setAuthorlistUpToDate(false);
            // change modified state
            setModified(true);
            // return the empty-position, which is now filled with the new author-value
            return emptypos;
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return -1;
        }
    }
    // get the root element of the author xml datafile
    else
        try {
            // get the root element of the author xml datafile
            Element authFile = authorFile.getRootElement();
            // create a new author element
            Element newAuthor = new Element(ELEMENT_ENTRY);
            // add the new author element to the author datafile
            try {
                // add the new author element to the author datafile
                authFile.addContent(newAuthor);
                // and finally add the parameter (new author string) to the recently created
                // author element
                newAuthor.addContent(auth);
                // set frequency of occurences to 1
                // set timestamp attribute
                // set ID attribute
                // but first, check the length of "auth", because we want max. 5 first chars of auth
                // in author id
                String auid;
                try {
                    auid = auth.substring(0, 5);
                } catch (IndexOutOfBoundsException ex) {
                    auid = auth;
                }
                updateAuthorTimestampAndID(newAuthor, freq, Tools.getTimeStampWithMilliseconds(),
                        String.valueOf(authorFile.getRootElement().getContent().size()) + auid
                                + Tools.getTimeStampWithMilliseconds());
                // change list-up-to-date-state
                setAuthorlistUpToDate(false);
                // change modified state
                setModified(true);
            } catch (IllegalAddException ex) {
                Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
            } catch (IllegalNameException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            } catch (IllegalDataException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            }
            // return the new size of the author file, i.e. the author position of 
            // the recently added author entry
            // get a list with all entry-elements of the author data
            List<?> authorList = authorFile.getRootElement().getContent();
            // and return the size of this list
            return authorList.size();
        } catch (IllegalStateException e) {
            Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            return -1;
        }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method adds a new entry to the datafile. The needed parameters come from the JDialog
 * "CNewEntry.java". This dialog opens an edit-mask so the user can input the necessary information.
 * If everything is done, the JDialog retrieves all the information as string(-array)-variables
 * and simply passes these as paramaters to this method.
 * <br>//from  www  .ja v a2 s  .c o  m
 * <br>
 * What we have to do here is to check whether the keywords or links e.g. partly exist, and if so,
 * find out the related index number. Keywords which until now do not already exist in the keyword
 * file have to be added to the keyword file and the new index number has to be addes to the
 * keyword-element of the entry. and so on...
 *
 * @param title the entry's title as string
 * @param content the entry's content as string
 * @param authors the entry's author as string, retrieve index number and add it to the entry's author-element.
 * use {@code null} if no authors should be added
 * @param keywords the entry's keywords as string-array. retrieve index numbers and add those to the entry's keyword-element
 * use {@code null} if no authors should be added
 * @param remarks the remarks as string
 * @param links the entry's links as string
 * use {@code null} if no authors should be added
 * @param timestamp the current date. in this case, add it as creation date to the timestamp
 * @param luhmann the number of the currently display entry, before the user clicked "new" or "insert entry".
 * if we have to insert an entry, we need to know this number, because that entry retrieves this new entry's
 * index-number and adds it to its luhmann-tag (which indicates follower- and sub-entries).
 * use {@code -1} if no luhmann-number is needed (i.e. no follower-entry is added).
 * @param editDeletedEntry use {@code true} if user edits an deleted entry, which is the same as inserting a
 * new entry at the deleted entry's position. use {@code false} if a entry is added normally.
 * @param editDeletedEntryPosition the position of the currently displayed entry that is deleted and should be
 * overwritten with a new entry ({@code editDeletedEntry} is set to true).
 * @param insertAfterEntry indicates the position, after which existing entry the new added entry should be inserted.
 * Use {@code -1} to add the new entry to the end of the database.
 * @return one of the following constants:<br>
 * {@link #ADD_ENTRY_OK ADD_ENTRY_OK} if a normal entry was successfully added<br>
 * {@link #ADD_LUHMANNENTRY_OK ADD_LUHMANNENTRY_OK} if a follower-entry (trailing entry) was successfully added<br>
 * {@link #ADD_ENTRY_ERR ADD_ENTRY_ERR} if an error occured when adding a normal entry<br>
 * {@link #ADD_LUHMANNENTRY_ERR ADD_LUHMANNENTRY_ERR} if an error occured when adding a follower-entry (trailing entry)
 */
public int addEntry(String title, String content, String[] authors, String[] keywords, String remarks,
        String[] links, String timestamp, int luhmann, boolean editDeletedEntry, int editDeletedEntryPosition,
        int insertAfterEntry) {
    // init return value
    int retval = ADD_ENTRY_OK;
    List<Integer> manlinks;
    // check for valid content. if we have any content,
    // replace Unicode-chars with UBB-tags
    if (content != null && !content.isEmpty()) {
        content = Tools.replaceUnicodeToUbb(content);
    }
    // create a new zettel-element
    Element zettel = new Element(ELEMENT_ZETTEL);
    // check whether we have any empty elements in between where we can insert the new entry
    int emptypos = (editDeletedEntry) ? editDeletedEntryPosition : retrieveFirstEmptyEntry();
    // check whether user wants to edit an already deleted entry and insert a new one at
    // that position
    if (editDeletedEntry || (emptypos != -1 && settings.getInsertNewEntryAtEmpty())) {
        // retrieve empty element
        zettel = retrieveElement(zknFile, emptypos);
        // and remove former content, so we can add new content
        zettel.removeContent();
    }
    try {
        // add unique ID
        setZettelID(zettel);
        //
        // add title
        //
        // create child element with title information
        Element t = new Element(ELEMENT_TITLE);
        // and add it to the zettel-element
        zettel.addContent(t);
        // set value of the child element
        t.setText(title);
        //
        // add content
        //
        // create child element with content information
        Element c = new Element(ELEMENT_CONTENT);
        // and add it to the zettel-element
        zettel.addContent(c);
        // set value of the content element
        c.setText(content);
        // then, create form-images
        createFormImagesFromContent(content);
        //
        // add author
        //
        // create child element with author information
        Element a = new Element(ELEMENT_AUTHOR);
        // and add it to the zettel-element
        zettel.addContent(a);
        // create empty string buffer which stores the index numbers
        // of the converted authors
        StringBuilder newau = new StringBuilder("");
        // check whether we have authors at all
        if ((authors != null) && (authors.length > 0)) {
            // iterate the array and get the index number of each author string
            // if a author does not already exist, add it to the authorfile
            for (String aut : authors) {
                // trim leading and trailing spaces
                aut = aut.trim();
                // only proceed for this entry, if it contains a value
                if (!aut.isEmpty()) {
                    // add author
                    int authorPos = addAuthor(aut, 1);
                    // append the index number in the string buffer
                    newau.append(String.valueOf(authorPos));
                    // separator for the the index numbers, since more authors
                    // and thus more index numbers might be stored in the author element
                    newau.append(",");
                }
            }
            // check whether we have any author-value at all...
            if (newau.length() > 0) {
                // shorten the stringbuffer by one char, since we have a
                // superfluous comma char (see for-loop above)
                newau.setLength(newau.length() - 1);
                // and say that author list is out of date
                setAuthorlistUpToDate(false);
            }
        }
        a.setText(newau.toString());
        //
        // add keywords
        //
        // create child element with keyword information
        Element k = new Element(ELEMENT_KEYWORD);
        // and add it to the zettel-element
        zettel.addContent(k);
        // create empty string buffer which stores the index numbers
        // of the converted keywords
        StringBuilder newkw = new StringBuilder("");
        // check whether we have keywords at all
        if ((keywords != null) && (keywords.length > 0)) {
            // iterate the array and get the index number of each keyword string
            // if a keyword does not already exist, add it to the keywordfile
            for (String keyw : keywords) {
                // trim leading and trailing spaces
                keyw = keyw.trim();
                // only proceed for this entry, if it contains a value
                if (!keyw.isEmpty()) {
                    // add it to the data file
                    // and store the position of the new added keyword in the
                    // variable keywordPos
                    int keywordPos = addKeyword(keyw, 1);
                    // append the index number in the string buffer
                    newkw.append(String.valueOf(keywordPos));
                    // separator for the the index numbers, since more keywords
                    // and thus more index numbers might be stored in the keyword element
                    newkw.append(",");
                }
            }
            // check whether we have any keyword-values at all...
            if (newkw.length() > 0) {
                // shorten the stringbuffer by one char, since we have a
                // superfluous comma char (see for-loop above)
                newkw.setLength(newkw.length() - 1);
                // and say that author list is out of date
                setKeywordlistUpToDate(false);
            }
        }
        // store keyword index numbers
        k.setText(newkw.toString());
        //
        // now comes the manual links to other entries
        //
        Element m = new Element(ELEMENT_MANLINKS);
        zettel.addContent(m);
        // check for manual links in content
        // and add them
        manlinks = extractManualLinksFromContent(content);
        m.setText(retrievePreparedManualLinksFromContent(manlinks));
        //
        // add hyperlinks
        //
        // create child element with link information
        Element h = new Element(ELEMENT_ATTACHMENTS);
        // and add it to the zettel-element
        zettel.addContent(h);
        // add each hyperlink string
        if (links != null && links.length > 0) {
            // therefor, iterate the array
            for (String l : links) {
                // create a new subchuld-element
                Element sublink = new Element(ELEMENT_ATTCHILD);
                // and add the link-string from the array
                sublink.setText(l);
                h.addContent(sublink);
            }
        }
        //
        // add remarks
        //
        // create child element with content information
        Element r = new Element(ELEMENT_REMARKS);
        // and add it to the zettel-element
        zettel.addContent(r);
        // set value of the content element
        r.setText(remarks);
        //
        // add remarks
        //
        // set creation timestamp, but set no text for edit timestamp
        // since the entry is not edited
        setTimestamp(zettel, Tools.getTimeStamp(), "");
        //
        // now comes the luhmann number
        //
        Element l = new Element(ELEMENT_TRAILS);
        zettel.addContent(l);
        l.setText("");
        //
        // complete datafile
        //
        // if we have any empty elements, go on here
        if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) {
            // return the empty-position, which is now filled with the new author-value
            zettelPos = emptypos;
        } else {
            // finally, add the whole element to the data file
            zknFile.getRootElement().addContent(zettel);
            // set the zettel-position to the new entry
            zettelPos = getCount(ZKNCOUNT);
        }
        // and add the new position to the history...
        addToHistory();
        // set modified state
        setModified(true);
    } catch (IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return ADD_ENTRY_ERR;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return ADD_ENTRY_ERR;
    }
    // if we have a follower-number (insert-entry), we have to change the luhmann-tag
    // of the related entry (which number is passed in the luhmann-variable)
    if (luhmann != -1) {
        // try to add luhmann number
        if (addLuhmannNumber(luhmann, zettelPos)) {
            // if it was successfull, we can insert this entry
            // after the "parent" entry
            retval = ADD_LUHMANNENTRY_OK;
            // to do this, we need to change the "insertAfter" value
            insertAfterEntry = luhmann;
        } else {
            retval = ADD_LUHMANNENTRY_ERR;
        }
    }
    // check whether inserted entry position is already the last position in
    // the entry order
    // in this case, we can set the variable to -1, so it will automatically be
    // added to the end
    changeZettelPointer(zettelPos, insertAfterEntry);
    // set this entry as first entry if we do not have any
    // first entry yet...
    if (-1 == getFirstZettel())
        setFirstZettel(zettelPos);
    // save ID of last added entry
    setLastAddedZettelID(zettel);
    // create back-references for manual links
    // we can do this here first, because we need
    // "zettelPos" as reference, which is not available earlier
    addManualLink(manlinks, zettelPos);
    // entry successfully added
    return retval;
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method changed an existing entry in the datafile. The needed parameters come from the JDialog
 * "CNewEntry.java". This dialog opens an edit-mask so the user can input the necessary information.
 * If everything is done, the JDialog retrieves all the information as string(-array)-variables
 * and simply passes these as paramaters to this method.
 * <br>/*w w w. java  2  s  .  c  o m*/
 * <br>
 * What we have to do here is to check whether the keywords or links e.g. partly exist, and if so,
 * find out the related index number. Keywords which until now do not already exist in the keyword
 * file have to be added to the keyword file and the new index number has to be addes to the
 * keyword-element of the entry. and so on...
 * 
 * @param title the entry's title as string
 * @param content the entry's content as string
 * @param authors the entry's authors as string-array, retrieve index number and add it to the entry's author-element
 * @param keywords the entry's keywords as string-array. retrieve index numbers and add those to the entry's keyword-element
 * @param remarks the remarks as string
 * @param links the entry's links as string
 * @param timestamp the current date. in this case, add it as edit date to the timestamp
 * @param entrynumber the number of the entry that should be changed.
 * @return 
 */
public boolean changeEntry(String title, String content, String[] authors, String[] keywords, String remarks,
        String[] links, String timestamp, int entrynumber) {
    // create a new zettel-element
    Element zettel = retrieveElement(zknFile, entrynumber);
    // create dummy element
    Element child;
    // if no entry exists, quit
    if (null == zettel)
        return false;
    // first of all, we remove all authors and keywords from the existing entry
    // we do this to update the frequency of the authors and keywords, so when adding
    // authors/keywords to the data-file, which already belonged to the entry, we would
    // increase the frequency although those authors/keywords are not new
    changeFrequencies(entrynumber, -1);
    // then, create form-images
    createFormImagesFromContent(content);
    try {
        //
        // change title
        //
        // retrieve the element
        child = zettel.getChild(ELEMENT_TITLE);
        // if child-element doesn't exist, add it to the zettel
        if (null == child) {
            // create new child element
            child = new Element(ELEMENT_TITLE);
            // and add it
            zettel.addContent(child);
        }
        // set value of the child element
        child.setText(title);
        //
        // change content
        //
        // retrieve the element
        child = zettel.getChild(ELEMENT_CONTENT);
        // if child-element doesn't exist, add it to the zettel
        if (null == child) {
            // create new child element
            child = new Element(ELEMENT_CONTENT);
            // and add it
            zettel.addContent(child);
        }
        // set value of the child element
        child.setText(content);
        //
        // change author
        //
        // retrieve the element
        child = zettel.getChild(ELEMENT_AUTHOR);
        // if child-element doesn't exist, add it to the zettel
        if (null == child) {
            // create new child element
            child = new Element(ELEMENT_AUTHOR);
            // and add it
            zettel.addContent(child);
        }
        // create empty string buffer which stores the index numbers
        // of the converted authors
        StringBuilder newau = new StringBuilder("");
        // check whether we have authors at all
        if ((authors != null) && (authors.length > 0)) {
            // iterate the array and get the index number of each author string
            // if a keauthoryword does not already exist, add it to the authorfile
            for (String aut : authors) {
                // trim leading and trailing spaces
                aut = aut.trim();
                // only proceed for this entry, if it contains a value
                if (!aut.isEmpty()) {
                    // add it to the data file
                    // and store the position of the new added author in the
                    // variable authorPos
                    int authorPos = addAuthor(aut, 1);
                    // append the index number in the string buffer
                    newau.append(String.valueOf(authorPos));
                    // separator for the the index numbers, since more authors
                    // and thus more index numbers might be stored in the author element
                    newau.append(",");
                }
            }
            // shorten the stringbuffer by one char, since we have a
            // superfluous comma char (see for-loop above)
            if (newau.length() > 0)
                newau.setLength(newau.length() - 1);
        }
        // store author index numbers
        child.setText(newau.toString());
        //
        // change keywords
        //
        // retrieve the element
        child = zettel.getChild(ELEMENT_KEYWORD);
        // if child-element doesn't exist, add it to the zettel
        if (null == child) {
            // create new child element
            child = new Element(ELEMENT_KEYWORD);
            // and add it
            zettel.addContent(child);
        }
        // create empty string buffer which stores the index numbers
        // of the converted keywords
        StringBuilder newkw = new StringBuilder("");
        // check whether we have keywords at all
        if ((keywords != null) && (keywords.length > 0)) {
            // iterate the array and get the index number of each keyword string
            // if a keyword does not already exist, add it to the keywordfile
            for (String keyw : keywords) {
                // trim leading and trailing spaces
                keyw = keyw.trim();
                // only proceed for this entry, if it contains a value
                if (!keyw.isEmpty()) {
                    // add it to the data file
                    // and store the position of the new added keyword in the
                    // variable keywordPos
                    int keywordPos = addKeyword(keyw, 1);
                    // append the index number in the string buffer
                    newkw.append(String.valueOf(keywordPos));
                    // separator for the the index numbers, since more keywords
                    // and thus more index numbers might be stored in the keyword element
                    newkw.append(",");
                }
            }
            // shorten the stringbuffer by one char, since we have a
            // superfluous comma char (see for-loop above)
            if (newkw.length() > 0)
                newkw.setLength(newkw.length() - 1);
        }
        // store keyword index numbers
        child.setText(newkw.toString());
        //
        // change manual links
        //
        addManualLink(entrynumber, extractManualLinksFromContent(content));
        //
        // change hyperlinks
        //
        // retrieve the element
        child = zettel.getChild(ELEMENT_ATTACHMENTS);
        // if child-element doesn't exist, add it to the zettel
        if (null == child) {
            // create new child element
            child = new Element(ELEMENT_ATTACHMENTS);
            // and add it
            zettel.addContent(child);
        }
        // first, remove all existing links, since they are completely
        // set again
        child.removeChildren(ELEMENT_ATTCHILD);
        // add each hyperlink string
        // therefor, iterate the array
        for (String l : links) {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            // and add the link-string from the array
            sublink.setText(l);
            child.addContent(sublink);
        }
        //
        // change remarks
        //
        // retrieve the element
        child = zettel.getChild(ELEMENT_REMARKS);
        // if child-element doesn't exist, add it to the zettel
        if (null == child) {
            // create new child element
            child = new Element(ELEMENT_REMARKS);
            // and add it
            zettel.addContent(child);
        }
        // set value of the content element
        child.setText(remarks);
        //
        // change timestamp
        //
        setTimestampEdited(zettel, timestamp);
        //
        // we don't need any changes on the luhmann number or for
        // manual links here...
        //
        // update the current zettel-position
        zettelPos = entrynumber;
        // and add the new position to the history...
        addToHistory();
        // set modified state
        setModified(true);
    } catch (IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * Sets the manual links for an entry. these links appear in the main-window's tabbed pane
 * on the "links"-page, in the jTableManLinks.
 * /*from ww  w . ja  v  a2  s  . c  o  m*/
 * @param pos the entry-number that should get new manlinks
 * @param manlinks the entry-numbers where the entry "pos" refers to, stored in an integer-array
 */
public void setManualLinks(int pos, int[] manlinks) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if we found an entry-element, go on
    if (zettel != null) {
        // if no child-element ELEMENT_MANLINKS exists, create it...
        if (null == zettel.getChild(ELEMENT_MANLINKS))
            zettel.addContent(new Element(ELEMENT_MANLINKS));
        // create stringbuilder
        StringBuilder sb = new StringBuilder("");
        // iterate int-array
        for (int ml : manlinks) {
            // and copy all int-values to array
            sb.append(String.valueOf(ml));
            sb.append(",");
        }
        // delete last comma
        if (sb.length() > 1)
            sb.setLength(sb.length() - 1);
        // and set value to element...
        zettel.getChild(ELEMENT_MANLINKS).setText(sb.toString());
        setModified(true);
    }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * Sets the manual links for an entry. these links appear in the main-window's tabbed pane
 * on the "links"-page, in the jTableManLinks.
 * //from w  ww . j a v a2s.  c o  m
 * @param pos the entry-number that should get new manlinks
 * @param manlinks the entry-numbers where the entry "pos" refers to, stored in a string-value
 * (comma-separated)
 */
public void setManualLinks(int pos, String manlinks) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if we found an entry-element, go on
    if (zettel != null) {
        // if no child-element ELEMENT_MANLINKS exists, create it...
        if (null == zettel.getChild(ELEMENT_MANLINKS))
            zettel.addContent(new Element(ELEMENT_MANLINKS));
        // and set value to element...
        zettel.getChild(ELEMENT_MANLINKS).setText(manlinks);
        setModified(true);
    }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method removes wrong placed edit-tags in the xml-file. this error occured in the
 * {@link #changeEditTimeStamp(int) changeEditTimeStamp()} method, where the edit-element,
 * child-element of the timestamp-element, was set as child of the zettel-element (and not its
 * child "timestamp"). This method tries to fix this error...
 */// w w  w . j a  va2  s . co m
public void fixWrongEditTags() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
        // retrieve element
        Element zettel = retrieveZettel(cnt);
        // check for valid value
        if (zettel != null) {
            // check whether element has a child named "edited". if so, it either
            // has to be moved as sub-child to the child-element timestamp, or removed
            // if "timestamp" already has an edit-element
            Element edited = zettel.getChild("edited");
            // only proceed, if wrong placed edited element exists
            if (edited != null) {
                // retrieve timestamp-element
                Element timestamp = zettel.getChild("timestamp");
                // check for valid value
                if (timestamp != null) {
                    // retrieve edited-timestamp
                    Element timestampedit = timestamp.getChild("edited");
                    // check whether edited-element exists
                    if (null == timestampedit) {
                        // if timestampedit is null, the element has no edited-element
                        // so we add the content of the wrong placed element as new edited-element
                        // create new edited element
                        Element ed = new Element("edited");
                        // add to timestamp
                        timestamp.addContent(ed);
                        // set content
                        ed.setText(edited.getText());
                    } else {
                        // now we know that an edited-element already exists
                        // we now want to check whether the existing editing-timestamp
                        // is older than the value in the wrong placed edited-element,
                        // and if so, update the timestamp
                        if (timestamp.getText().compareTo(edited.getText()) < 0)
                            timestampedit.setText(edited.getText());
                    }
                }
                // and remove wrong edited element
                zettel.removeChild("edited");
            }
        }
    }
}

From source file:de.danielluedecke.zettelkasten.database.DesktopData.java

License:Open Source License

/**
 * This method adds a new desktop-element to the document. Furthermore, currentDesktop-number
 * is set to the latest added desktop-index-number.
 *
 * @param name the name of the desktop, which appears in the desktopDialog's combobox
 * @param notes the initial desktop-notes that should be associated with this desktop. use this
 * e.g. when importing an archived desktop (see {@link #importArchivedDesktop(org.jdom.Document) importArchivedDesktop()}.
 * use {@code null} when no notes-content should be added (i.e. the elements are being created, but
 * they have no text)./*from  ww  w  .  ja  v  a2 s.  co  m*/
 * @return {@code true} if the desktop was successfully added, {@code false} otherwise (e.g. because the
 * desktop-name already existed)
 */
public boolean addNewDesktop(String name, String[] notes) {
    // first of all, go through all desktops and check whether the name
    // already exist, to avoid double naming...
    // when such a desktopname as "name" already exists, return false
    for (int cnt = 0; cnt < getCount(); cnt++)
        if (name.equalsIgnoreCase(getDesktopName(cnt)))
            return false;
    // create new element
    Element d = new Element("desktop");
    try {
        // set the desktop's name as attribute
        d.setAttribute("name", name);
        // add the element to the desktop
        desktop.getRootElement().addContent(d);
        // set currentDesktop index to the new desktop-element
        currentDesktop = desktop.getRootElement().getContentSize() - 1;
        // also add new desktop-notes-element
        Element desk = new Element("desktop");
        // set name attribute
        desk.setAttribute("name", name);
        // create notes elements
        Element n1 = new Element("notes1");
        Element n2 = new Element("notes2");
        Element n3 = new Element("notes3");
        // set initial notes text, if we have any...
        if (notes != null && notes.length > 0) {
            // check for array-length before setting text
            if (notes.length > 0)
                n1.setText(notes[0]);
            // check for array-length before setting text
            if (notes.length > 1)
                n2.setText(notes[1]);
            // check for array-length before setting text
            if (notes.length > 2)
                n3.setText(notes[2]);
        }
        // add notes-sub-elements
        desk.addContent(n1);
        desk.addContent(n2);
        desk.addContent(n3);
        // add element to desktop-notes
        desktopNotes.getRootElement().addContent(desk);
        // change modified state
        setModified(true);
    } catch (IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalNameException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}