Search in sources :

Example 1 with IComment

use of de.janrufmonitor.service.comment.api.IComment in project janrufmonitor by tbrandt77.

the class CommentCallerHandler method createComment.

private IComment createComment(File commentFile) {
    IComment comment = new Comment();
    String commentID = commentFile.getName();
    commentID = commentID.substring(0, commentID.indexOf(this.getCommentFilenameExtension()));
    comment.setID(commentID);
    comment.setDate(new Date(commentFile.lastModified()));
    try {
        FileReader commentReader = new FileReader(commentFile);
        BufferedReader bufReader = new BufferedReader(commentReader);
        StringBuffer text = new StringBuffer();
        while (bufReader.ready()) {
            text.append(bufReader.readLine());
            text.append(IJAMConst.CRLF);
        }
        bufReader.close();
        commentReader.close();
        comment.setText(text.substring(0));
    } catch (FileNotFoundException ex) {
        this.m_logger.warning("Cannot find comment file " + commentFile);
    } catch (IOException ex) {
        this.m_logger.severe("IOException on file " + commentFile);
    }
    File commentAttributesFile = new File(commentFile.getAbsolutePath() + ".attributes");
    if (commentAttributesFile.exists() && commentAttributesFile.isFile()) {
        Properties commentAttributes = new Properties();
        try {
            FileInputStream in = new FileInputStream(commentAttributesFile);
            commentAttributes.load(in);
            in.close();
            Iterator it = commentAttributes.keySet().iterator();
            IAttribute a = null;
            String key = null;
            while (it.hasNext()) {
                key = (String) it.next();
                a = PIMRuntime.getInstance().getCallerFactory().createAttribute(key, commentAttributes.getProperty(key));
                comment.addAttribute(a);
            }
        } catch (FileNotFoundException ex) {
            this.m_logger.severe("File not found: " + commentAttributesFile.getAbsolutePath());
        } catch (IOException ex) {
            this.m_logger.severe("IOException on file " + commentAttributesFile.getAbsolutePath());
        }
    }
    return comment;
}
Also used : IComment(de.janrufmonitor.service.comment.api.IComment) Comment(de.janrufmonitor.service.comment.impl.Comment) IComment(de.janrufmonitor.service.comment.api.IComment) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute) FileReader(java.io.FileReader) File(java.io.File)

Example 2 with IComment

use of de.janrufmonitor.service.comment.api.IComment in project janrufmonitor by tbrandt77.

the class CommentCallerHandler method storeComments.

private void storeComments(List comments, String directory) {
    // clean up old comments
    File[] oldComments = new File(directory).listFiles(new FileFilter() {

        public boolean accept(File pathname) {
            if (pathname.getAbsolutePath().endsWith(getCommentFilenameExtension())) {
                return true;
            }
            return false;
        }
    });
    for (int i = 0; i < oldComments.length; i++) oldComments[i].delete();
    for (int i = 0; i < comments.size(); i++) {
        IComment comment = (IComment) comments.get(i);
        File commentFile = new File(directory + comment.getID() + this.getCommentFilenameExtension());
        try {
            FileWriter commentWriter = new FileWriter(commentFile);
            BufferedWriter bufWriter = new BufferedWriter(commentWriter);
            bufWriter.write(comment.getText());
            bufWriter.flush();
            bufWriter.close();
            commentWriter.close();
        } catch (FileNotFoundException ex) {
            this.m_logger.severe("File not found: " + directory + comment.getID());
        } catch (IOException ex) {
            this.m_logger.severe("IOException on file " + directory + comment.getID());
        }
        commentFile.setLastModified(comment.getDate().getTime());
        Properties commentAttributes = new Properties();
        IAttributeMap m = comment.getAttributes();
        Iterator it = m.iterator();
        IAttribute a = null;
        while (it.hasNext()) {
            a = (IAttribute) it.next();
            commentAttributes.setProperty(a.getName(), a.getValue());
        }
        File commentAttributesFile = new File(commentFile.getAbsolutePath() + ".attributes");
        try {
            FileOutputStream fos = new FileOutputStream(commentAttributesFile);
            commentAttributes.store(fos, "");
            fos.flush();
            fos.close();
        } catch (FileNotFoundException ex) {
            this.m_logger.severe("File not found: " + directory + comment.getID());
        } catch (IOException ex) {
            this.m_logger.severe("IOException on file " + directory + comment.getID());
        }
        commentAttributesFile.setLastModified(comment.getDate().getTime());
    // TODO: removed due to performance reasons
    // this.storeAttachments(comment.getAttachments(), directory + comment.getID());
    }
}
Also used : IComment(de.janrufmonitor.service.comment.api.IComment) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) FileFilter(java.io.FileFilter) File(java.io.File)

Example 3 with IComment

use of de.janrufmonitor.service.comment.api.IComment in project janrufmonitor by tbrandt77.

the class DeleteAction method run.

public void run() {
    Viewer v = this.m_app.getApplication().getViewer();
    if (v != null) {
        IStructuredSelection selection = (IStructuredSelection) v.getSelection();
        if (!selection.isEmpty()) {
            Object o = selection.getFirstElement();
            if (o instanceof IComment) {
                List l = new ArrayList();
                l.add(o);
                if (MessageDialog.openConfirm(new Shell(DisplayManager.getDefaultDisplay()), this.getI18nManager().getString(this.getNamespace(), "delete", "label", this.getLanguage()), this.getI18nManager().getString(this.getNamespace(), "delete", "description", this.getLanguage()))) {
                    this.m_app.getController().deleteElements(l);
                    this.m_app.getApplication().initializeController();
                    this.m_app.updateViews(true);
                }
            }
        }
    }
}
Also used : IComment(de.janrufmonitor.service.comment.api.IComment) Shell(org.eclipse.swt.widgets.Shell) ArrayList(java.util.ArrayList) Viewer(org.eclipse.jface.viewers.Viewer) List(java.util.List) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 4 with IComment

use of de.janrufmonitor.service.comment.api.IComment in project janrufmonitor by tbrandt77.

the class ExportAction method run.

public void run() {
    Viewer v = this.m_app.getApplication().getViewer();
    if (v != null) {
        IStructuredSelection selection = (IStructuredSelection) v.getSelection();
        if (!selection.isEmpty()) {
            List l = selection.toList();
            if (l.size() > 0) {
                FileDialog dialog = new FileDialog(new Shell(DisplayManager.getDefaultDisplay()), SWT.SAVE);
                dialog.setFilterExtensions(new String[] { "*.txt" });
                dialog.setFilterNames(new String[] { getI18nManager().getString(getNamespace(), "exportfilter", "label", getLanguage()) });
                dialog.setText(getI18nManager().getString(getNamespace(), "export", "label", getLanguage()));
                String filename = dialog.open();
                if (filename != null && filename.length() > 0) {
                    File f = new File(filename);
                    f.getParentFile().mkdirs();
                    try {
                        FileOutputStream fos = new FileOutputStream(f);
                        IComment c = null;
                        for (int i = 0; i < l.size(); i++) {
                            c = (IComment) l.get(i);
                            fos.write(c.getText().getBytes());
                            fos.write(IJAMConst.CRLF.getBytes());
                        }
                        fos.flush();
                        fos.close();
                    } catch (FileNotFoundException ex) {
                        m_logger.severe(ex.getMessage());
                    } catch (IOException ex) {
                        m_logger.severe(ex.toString() + ": " + ex.getMessage());
                    }
                }
            }
        }
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) IComment(de.janrufmonitor.service.comment.api.IComment) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Viewer(org.eclipse.jface.viewers.Viewer) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

Example 5 with IComment

use of de.janrufmonitor.service.comment.api.IComment in project janrufmonitor by tbrandt77.

the class NewAction method run.

public void run() {
    Object callableObject = null;
    if (this.m_app instanceof Comment) {
        callableObject = ((Comment) this.m_app).getCallableObject();
    }
    CommentDialog cd = new CommentDialog(DisplayManager.getDefaultDisplay().getActiveShell(), null, callableObject);
    int result = cd.open();
    if (result == CommentDialog.OK) {
        IComment c = cd.getResult();
        this.m_app.getController().updateElement(c);
        this.m_app.updateViews(true);
    }
}
Also used : IComment(de.janrufmonitor.service.comment.api.IComment) Comment(de.janrufmonitor.ui.jface.application.comment.Comment) IComment(de.janrufmonitor.service.comment.api.IComment) CommentDialog(de.janrufmonitor.ui.jface.dialogs.CommentDialog)

Aggregations

IComment (de.janrufmonitor.service.comment.api.IComment)9 File (java.io.File)5 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)4 IAttribute (de.janrufmonitor.framework.IAttribute)3 FileOutputStream (java.io.FileOutputStream)3 List (java.util.List)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 Viewer (org.eclipse.jface.viewers.Viewer)3 ICommentCaller (de.janrufmonitor.service.comment.api.ICommentCaller)2 Comment (de.janrufmonitor.ui.jface.application.comment.Comment)2 CommentDialog (de.janrufmonitor.ui.jface.dialogs.CommentDialog)2 FileFilter (java.io.FileFilter)2 FileInputStream (java.io.FileInputStream)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 Shell (org.eclipse.swt.widgets.Shell)2 Chunk (com.lowagie.text.Chunk)1 Document (com.lowagie.text.Document)1