List of usage examples for org.jdom2 Element setAttribute
public Element setAttribute(final String name, final String value)
This sets an attribute value for this element.
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 ww . j a va2 s .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
/** * Adds a new bullet-point to the xml-document. * //from w ww. j av a2 s. co m * @param timestamp the timestamp of the currently selected bullet-point after which the new bullet should * be inserted. * @param name the name of the bullet-point * @return the timestamp of the added bullet-element as {@code String} or {@code null} if an error * occured. */ public String addBullet(String timestamp, String name) { // find the bulletgroup that is described in the treepath Element parentb = (timestamp != null) ? findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp) : getCurrentDesktopElement(); String newts; // if we have a valid bullet-group, add the new bullet to the xml-file if (parentb != null) { // create a new element from the given name Element b = new Element(ELEMENT_BULLET); try { b.setAttribute("name", name); // set attribute for collapsed/expanded state of bullet b.setAttribute("treefold", TREE_FOLDING_EXPANDED); // create timestamp newts = Tools.getTimeStampWithMilliseconds(); // check whether timestamp already exists. this is particulary the // case when a user adds several entries at once. while (timeStampExists(newts)) newts = Tools.getTimeStampWithMilliseconds(); // set timestamp as attribute b.setAttribute(ATTR_TIMESTAMP, newts); // add a "comment" to that bullet. remember, that each bullet // automatically gets a child-element called "comment" b.addContent(new Element(ATTR_COMMENT)); // add new bullet to the bulletgroup parentb.addContent(b); // change modified state setModified(true); // return timestamp of addes bullet return newts; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return null; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return null; } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return null; } } return null; }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method renames the bullet which is given through the path {@code tp}. * // w ww.ja v a 2s.co m * @param timestamp the timestamp of the bullet that should be renamed * @param newName the new name of the bullet * @return {@code true} is renaming was successful, {@code false} if an error occured. */ public boolean renameBullet(String timestamp, String newName) { // retrieve selected bullet element Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp); if (b != null) { try { // change name b.setAttribute("name", newName); // change modified state setModified(true); } 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
/** * Pastes the bullet-point {@code clipbullet} into the xml-document. The Element {@code clipbullet} * must be retrieved by {@link #copyBulletToClip(java.util.LinkedList) copyBulletToClip(java.util.LinkedList)}. * * @param timestamp the timestamp of the bullet that should be inserted * @param isRoot {@code true} when the drop-source is the root-element of the jTree, {@code false} when * the drop-source is any other parent-element *///from w w w. j a va2 s.c o m public void pasteBulletFromClip(String timestamp, boolean isRoot) { // find the bulletgroup that is described in the treepath Element parentb = (isRoot) ? getCurrentDesktopElement() : findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp); // if we have a valid bullet-group, add the new bullet to the xml-file if (parentb != null && clipbullet != null) { // create a new element Element b = new Element(ELEMENT_BULLET); try { // set the name of the copied bullet-value b.setAttribute("name", clipbullet.getAttributeValue("name")); // and use copied timestamp b.setAttribute(ATTR_TIMESTAMP, clipbullet.getAttributeValue(ATTR_TIMESTAMP)); // clone the content from the clipboard-bullet to the new element. we // have to clone the content, since the element "clipbullet" still might // be attached to the document, when the bullet is just copied, not cut. b.addContent(clipbullet.cloneContent()); // add new bullet to the bulletgroup parentb.addContent(b); // change modified state setModified(true); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method adds a new entry (child-node) to the xml-document. * /* w ww. j av a2 s .co m*/ * @param timestamp the timestamp of the element, where the entry "nr" should be inserted as new child * @param nr the entry-number of the entry that should be added * @param insertpos the position where the new entry should be inserted. necessary when we have already * more children and the entry should be inserted in between, at the beginning or end of the children-list. * @return the timestamp of the added entry-element as {@code String} or {@code null} if an error * occured. */ public String addEntry(String timestamp, String nr, int insertpos) { // find the bullet that is described in the treepath Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp); // if we have a valid bullet, add the new enry to the xml-file if (b != null) { // check whether element is a bullet, if not, retrieve its parent element if (!b.getName().equals(ELEMENT_BULLET)) b = b.getParentElement(); // create a new element Element e = new Element(ELEMENT_ENTRY); try { e.setAttribute("id", nr); // create timestamp String ts = Tools.getTimeStampWithMilliseconds(); // check whether timestamp already exists. this is particulary the // case when a user adds several entries at once. while (timeStampExists(ts)) ts = Tools.getTimeStampWithMilliseconds(); // add timestamp to entry element e.setAttribute(ATTR_TIMESTAMP, ts); // add new enry to the bullet at insert-position+1 (because at first // position in the bullet is always the comment) b.addContent(insertpos, e); // change modified state setModified(true); // return timestamp return ts; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } return null; }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method changes the treefold-state of a bullet-point. The <i>treefold</i>-attribute of the * bullet-point is either set to {@link #TREE_FOLDING_EXPANDED TREE_FOLDING_EXPANDED} or * {@link #TREE_FOLDING_COLLAPSED TREE_FOLDING_COLLAPSED}.<br><br> * With this attribute, we can check whether a bullet-point should be expanded or not, * when creating the TreeView in the CDesktop-Window. * * @param timestamp the timestamp of the bullet point which treefold-state was changed * @param expanded {@code true} if the bullet point was expanded, {@code false} if it was * collapsed.//from w w w .j a v a2s.c o m */ public void setBulletTreefold(String timestamp, boolean expanded) { // retrieve bullet of which fold-state should be switched Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp); // check for valid value if (b != null) { try { // set new treefold-attribute b.setAttribute("treefold", (expanded) ? TREE_FOLDING_EXPANDED : TREE_FOLDING_COLLAPSED); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method adds a modified entry to the {@link #modifiedEntries modifiedEntries}-Document, * in case the user modifies an entry on the desktop, while the original content of that entry * in the main database should be left unchanged. * * @param timestamp a unique timestamp which is associated to the entry that is modified, so we * can later easily find the related modified content. * @param content the modified content itself. * @return {@code true} if entry was successfully saved, {@code false} if an error occured. *//*from www .ja v a 2s . com*/ public boolean addModifiedEntry(String timestamp, String content) { // check for valid parameters if (null == timestamp || timestamp.isEmpty()) return false; if (null == content) content = ""; // first check, whether modified entry already exists. this may // occur, when importing archived desktop-files if (null == retrieveModifiedEntryElementFromTimestamp(timestamp)) { // create a new element Element entry = new Element(ELEMENT_ENTRY); try { // set timestamp-attribute entry.setAttribute(ATTR_TIMESTAMP, timestamp); // add content-string entry.setText(content); // add element to XML-document modifiedEntries.getRootElement().addContent(entry); // change modified-flag setModified(true); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); return false; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); return false; } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); return false; } } return true; }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method initialises the desktopNotes-xml-file during an update-process.<br><br> * This method is called when updating a file with version number 3.0 or 3.1 to 3.2 and higher. *//* w w w . j ava 2 s .com*/ public void initDesktopNotesUpdate() { // iterate all desktops for (int cnt = 0; cnt < getCount(); cnt++) { // retrieve desktopname String dname = getDesktopName(cnt); // create new desktop-element Element desk = new Element("desktop"); try { // set name attribute desk.setAttribute("name", dname); // add notes-sub-elements desk.addContent(new Element("notes1")); desk.addContent(new Element("notes2")); desk.addContent(new Element("notes3")); // add element to desktop-notes desktopNotes.getRootElement().addContent(desk); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } } }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method updates the timestamp-attributes when the datafile is being updated due to * a newer file-version.<br><br> * This method is called when updating a file with version number 3.0 or 3.1 to 3.2 and higher. * * @param e the initial element, where the updating should start. usually, use something * like {@link #getDesktopElement(int) getDesktopElement(int)}. *///from w ww . j a v a 2s. c om private void updateTimestamps(Element e) { // get a list with all children of the element List<Element> children = e.getChildren(); // create an iterator Iterator<Element> it = children.iterator(); DecimalFormat df = new DecimalFormat("00000"); // go through all children while (it.hasNext()) { // get the child e = it.next(); try { if (e.getName().equals(ELEMENT_ENTRY)) { String timestamp = e.getAttributeValue(ATTR_TIMESTAMP); if (timestamp != null) e.setAttribute(ATTR_TIMESTAMP, timestamp.concat(df.format(timestampid++))); } if (e.getName().equals(ELEMENT_BULLET)) { e.setAttribute(ATTR_TIMESTAMP, Tools.getTimeStamp() + df.format(timestampid++)); e.setAttribute("treefold", TREE_FOLDING_EXPANDED); } } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } // when the new element also has children, call this method again, // so we go through the strucuture recursively... if (hasChildren(e)) { updateTimestamps(e); } } }
From source file:de.danielluedecke.zettelkasten.database.DesktopData.java
License:Open Source License
/** * This method archives the desktop-data of the desktop with the name {@code name} to a * zipped xml-file. The file contains the desktop-data, the modifed-entries-data for those entries * that appear on the desktop and the saved desktop-notes. * * @param name the name of the desktop that should be archived. * @return the archived document as XML-focument, or {@code null} if an error occured. *///from w ww .j av a 2 s .c o m public Document archiveDesktop(String name) { // create new document Document archive = new Document(new Element("archivedDesktop")); // add desktop-element of desktop that should be archived Element deskel = getDesktopElement(name); // if we found a valid value, go on if (deskel != null) { try { // set name attribute archive.getRootElement().setAttribute("name", name); // create desktop-element Element content_desktop = new Element("desktop"); // clone content from current desktop content_desktop.addContent(deskel.cloneContent()); // add element to archive-file archive.getRootElement().addContent(content_desktop); // now retrieve desktop-notes Element noteel = getDesktopNotes(name); // if we found a valid value, go on if (noteel != null) { // create notes-element Element content_notes = new Element("desktopNotes"); // clone content from current desktop-notes content_notes.addContent(noteel.cloneContent()); // add content archive.getRootElement().addContent(content_notes); } // now retrieve all timestamps from the archived desktop // and look for modified entries... // create new list that will contain the timestamps timestampList = new ArrayList<String>(); // fill list with all entry-numbers. since we have sub-bullets/levels, we // recursevly go through the desktop-data retrieveDesktopEntriesTimestamps(deskel); // if we have any results, go on... if (timestampList.size() > 0) { // create base element Element modifiedel = new Element("modifiedEntries"); // add all modified entries that appear on the archived desktop String[] timestamps = timestampList.toArray(new String[timestampList.size()]); for (String ts : timestamps) { // retrieve modifed entry Element me_dummy = retrieveModifiedEntryElementFromTimestamp(ts); // check for valid value if (me_dummy != null) { // crate new modified-entry-element Element me = new Element(ELEMENT_ENTRY); // set timestamp-attribute me.setAttribute(ATTR_TIMESTAMP, ts); // and add modified text me.setText(me_dummy.getText()); // and add content modifiedel.addContent(me); } } archive.getRootElement().addContent(modifiedel); } } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return null; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return null; } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return null; } } return archive; }