Search in sources :

Example 1 with Attachment

use of org.csstudio.logbook.Attachment in project org.csstudio.display.builder by kasemir.

the class SendToElogAction method createImageAttachment.

/**
 * @return Logbook attachment for the plot's image
 * @throws Exception
 *             on error
 */
private Attachment createImageAttachment() throws Exception {
    // Dump image into buffer
    ImageLoader loader = new ImageLoader();
    loader.data = new ImageData[] { ImageConverter.convertToSWT(graph.getImage()) };
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    loader.save(buf, SWT.IMAGE_PNG);
    buf.close();
    final byte[] image_bits = buf.toByteArray();
    buf = null;
    loader = null;
    // Attachment provides input stream
    return new Attachment() {

        @Override
        public Boolean getThumbnail() {
            return true;
        }

        @Override
        public InputStream getInputStream() {
            try {
                return new ByteArrayInputStream(image_bits);
            } catch (Exception ex) {
                return null;
            }
        }

        @Override
        public Long getFileSize() {
            return (long) image_bits.length;
        }

        @Override
        public String getFileName() {
            return "plot.png";
        }

        @Override
        public String getContentType() {
            return "image/png";
        }
    };
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.csstudio.logbook.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageLoader(org.eclipse.swt.graphics.ImageLoader)

Example 2 with Attachment

use of org.csstudio.logbook.Attachment in project org.csstudio.display.builder by kasemir.

the class SendToElogAction method makeLogEntry.

/**
 * Make a logbook entry.
 */
public void makeLogEntry() {
    try {
        final Attachment image_attachment = createImageAttachment();
        final String text = Messages.LogentryDefaultTitle + "\n" + Messages.LogentryDefaultBody;
        final LogEntryBuilder entry = LogEntryBuilder.withText(text).attach(AttachmentBuilder.attachment(image_attachment));
        // get the command from plugin.xml
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        List<LogEntryBuilder> logList = new ArrayList<LogEntryBuilder>();
        logList.add(entry);
        Event event = new Event();
        event.data = logList;
        // execute the command
        IHandlerService handlerService = window.getService(IHandlerService.class);
        handlerService.executeCommand(OPEN_LOGENTRY_BUILDER_DIALOG_ID, event);
    } catch (Exception e) {
        MessageDialog.openError(null, "Logbook Error", "Failed to make logbook entry: \n" + e.getMessage());
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ArrayList(java.util.ArrayList) Event(org.eclipse.swt.widgets.Event) Attachment(org.csstudio.logbook.Attachment) LogEntryBuilder(org.csstudio.logbook.LogEntryBuilder)

Aggregations

Attachment (org.csstudio.logbook.Attachment)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 LogEntryBuilder (org.csstudio.logbook.LogEntryBuilder)1 ImageLoader (org.eclipse.swt.graphics.ImageLoader)1 Event (org.eclipse.swt.widgets.Event)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 IHandlerService (org.eclipse.ui.handlers.IHandlerService)1