Example usage for org.jdom2 Element setText

List of usage examples for org.jdom2 Element setText

Introduction

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

Prototype

public Element setText(final String text) 

Source Link

Document

Sets the content of the element to be the text given.

Usage

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 w  ww.  ja v  a2 s .  c om
 * <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>//from   w  w  w  . jav a  2s .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

/**
 * This method sets an author to a given position in the author datafile
 * could be used for overwriting/changing existing authors
 *
 * @param pos the position of the author. The position {@code pos} is a value from <b>1</b> to
 * {@link #getCount(int) getCount(AUCOUNT)}
 * @param auth the author string itself/*ww w .  j  av a  2  s  .c  o  m*/
 * @param freq the frequency of the author. use <b>-1</b> when the frequency-attribute
 * should be left unchanged.
 */
private void setAuthorValue(int pos, String auth, String bibkey, int freq) {
    // retrieve author
    Element author = retrieveElement(authorFile, pos);
    // if a valid element was found...
    if (author != null) {
        // ...set the new text
        author.setText(auth);
        // and new frequency, but only if it is not -1
        if (freq != -1)
            author.setAttribute(ATTRIBUTE_FREQUENCIES, String.valueOf(freq));
        // change bibkey
        if (bibkey != null) {
            setAuthorBibKey(auth, bibkey.trim());
        }
        // and change the modified state of the file
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method deletes an author by removing the content from the element
 * inside of the author xml datafile. the element itself is kept and left
 * empty. this ensures that the order and numbering of an author never
 * changes. Since the zettelkasten datafile stores the index-numbers of the authors
 * a changing in the position/order/numbering of the author datafile would lead
 * to corrupted author associations in the zettelkasten data file
 * //from   w ww.j  a  v  a2s . c  o m
 * @param pos position of author which should be deleted
 */
public void deleteAuthor(int pos) {
    // check whether author exists...
    if (!getAuthor(pos).isEmpty()) {
        // ...delete its content
        // therefore, get the author's index-number as string (for comparison below)
        String nr = String.valueOf(pos);
        // create new string buffer
        StringBuilder newau = new StringBuilder("");
        // and delete this index-number from all entries
        for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
            // get each element
            Element zettel = retrieveElement(zknFile, cnt);
            // get the author-index-numbers
            String[] aunr = zettel.getChild(ELEMENT_AUTHOR).getText().split(",");
            // reset buffer
            newau.setLength(0);
            for (String aunr1 : aunr) {
                // if deleted value does not equal author-value, add it
                if (!aunr1.equals(nr)) {
                    // append index-number
                    newau.append(aunr1);
                    // and a seperator-comma
                    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);
            // now set the new author-index-numbers to the zettel
            zettel.getChild(ELEMENT_AUTHOR).setText(newau.toString());
        }
        // we don't want to remove the element itself, because this would lead
        // to changing index-numbers/element-position within the document. however,
        // an author should ever keep the same index-number. rather, we could fill
        // this "empty space" with new authors
        Element author = retrieveElement(authorFile, pos);
        // if we have an author, go on...
        if (author != null) {
            // clear text
            author.setText("");
            // and reset attributes
            author.setAttribute(ATTRIBUTE_FREQUENCIES, "0");
            author.setAttribute(ATTRIBUTE_AUTHOR_ID, "");
            author.setAttribute(ATTRIBUTE_AUTHOR_TIMESTAMP, "");
            // and reset bibkey
            author.setAttribute(ATTRIBUTE_AUTHOR_BIBKEY, "");
        }
        // and change modified state
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method returns the links of an entry. since we can have more than just one 
 * link/hyperlink per entry, the return-value is of the type {@code List<Element>}, i.e.
 * we return a list of xml-elements which contain the links of an entry.
 * /*ww  w  .j av a 2s.  c  o  m*/
 * @param pos the entry from which we want to retrieve the hyperlinks
 * @return a List of xml-Elements, or null if no links are available
 */
public List<Element> getAttachments(int pos) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry)
        return null;
    // retrieve list of attachments
    List<Element> dummy = entry.getChild(ELEMENT_ATTACHMENTS).getChildren();
    List<Element> attachments = new LinkedList<Element>();
    // we have to manually copy all elements from one list to the other,
    // so we don't change the original content.
    Iterator<Element> it = dummy.iterator();
    // go through list
    while (it.hasNext()) {
        // retrieve element
        Element att = it.next();
        // change separator chars
        String attstring = Tools.convertSeparatorChars(att.getText(), settings);
        // add element
        if (!attstring.isEmpty()) {
            // create new element
            Element e = new Element(ELEMENT_ATTCHILD);
            // set text
            e.setText(attstring);
            // add element to return-list
            attachments.add(e);
        }
    }
    // else return the child-elements of the links-element
    return attachments;
}

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

License:Open Source License

/**
 * This method sets the links of an entry.
 *
 * @param pos the entry from which we want to set/change the hyperlinks and attachments
 * @param attachments a string-array containing the hyperlinks, attachmentspaths etc.
 *///from w w w .ja v a 2  s .c o  m
public void setAttachments(int pos, String[] attachments) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry || null == attachments || attachments.length < 1)
        return;
    // remove all existing links from that entry
    entry.getChild(ELEMENT_ATTACHMENTS).removeChildren(ELEMENT_ATTCHILD);
    // save modification-stata
    boolean mod = false;
    // add each hyperlink string
    // therefor, iterate the array
    for (String a : attachments) {
        try {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            // add the link-string from the array
            sublink.setText(a);
            // and add sublink-element to entry's child's content
            entry.getChild(ELEMENT_ATTACHMENTS).addContent(sublink);
            // change modification state
            mod = true;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        } catch (IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
    // change modified state
    if (mod)
        setModified(true);
}

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

License:Open Source License

/**
 * This method add the links to an entry.
 *
 * @param pos the entry from which we want to set/change the hyperlinks and attachments
 * @param attachments a string-array containing the hyperlinks, attachmentspaths etc.
 *///from www  . ja v a  2s  .  c o m
public void addAttachments(int pos, String[] attachments) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry || null == attachments || attachments.length < 1)
        return;
    // save modification-stata
    boolean mod = false;
    // add each hyperlink string
    // therefor, iterate the array
    for (String a : attachments) {
        try {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            // add the link-string from the array
            sublink.setText(a);
            // and add sublink-element to entry's child's content
            entry.getChild(ELEMENT_ATTACHMENTS).addContent(sublink);
            // change modification state
            mod = true;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        } catch (IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
    // change modified state
    if (mod)
        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 ww .  j  a  va2 s .c o  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  w w w.  j  a  v a2s .c  o 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;
}

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

License:Open Source License

/**
 * Sets the comment of the selected entry in the jTree in the CDesktop-frame. This method
 * traverses the xml-datafile, looking in each "depth-level" for the element that is 
 * given in the linked list parameter <i>tp</i>. This parameter contains the treepath to
 * the selected element./*from w w  w . j  a v a 2s . co m*/
 * 
 * @param timestamp
 * @param comment a string containing the comment
 * @return {@code true} if comment was successfully set, {@code false} if an error occured.
 */
public boolean setComment(String timestamp, String comment) {
    // retrieve element that matches the timestamp
    Element e = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // check whether an entry was found or not
    if (e != null) {
        try {
            // check whether we have a bullet-point
            if (e.getName().equals(ELEMENT_BULLET)) {
                // if we have a bullet, return the text of it's comment-child.
                e.getChild(ATTR_COMMENT).setText(comment);
            } else {
                // set comment for entry
                e.setText(comment);
            }
            // change modified state
            setModified(true);
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
            return false;
        }
    }
    return true;
}