Skip to content

Commit

Permalink
Attachment editing is not supported...
Browse files Browse the repository at this point in the history
  • Loading branch information
shroffk committed Jul 17, 2023
1 parent bcd4dd0 commit c89bacd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,20 @@ private void showPreview() {
* @param attachment The image {@link Attachment} selected by user.
*/
private void showImagePreview(Attachment attachment) {
try {
BufferedImage bufferedImage = ImageIO.read(attachment.getFile());
// BufferedImage may be null due to lazy loading strategy.
if (bufferedImage == null) {
return;
if (attachment.getFile() != null) {
try {
BufferedImage bufferedImage = ImageIO.read(attachment.getFile());
// BufferedImage may be null due to lazy loading strategy.
if (bufferedImage == null) {
return;
}
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
imagePreview.visibleProperty().setValue(true);
imagePreview.setImage(image);
} catch (IOException ex) {
Logger.getLogger(AttachmentsEditorController.class.getName())
.log(Level.SEVERE, "Unable to load image file " + attachment.getFile().getAbsolutePath(), ex);
}
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
imagePreview.visibleProperty().setValue(true);
imagePreview.setImage(image);
} catch (IOException ex) {
Logger.getLogger(AttachmentsEditorController.class.getName())
.log(Level.SEVERE, "Unable to load image file " + attachment.getFile().getAbsolutePath(), ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javafx.scene.paint.Color;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.framework.selection.SelectionService;
import org.phoebus.logbook.Attachment;
import org.phoebus.logbook.LogClient;
import org.phoebus.logbook.LogEntry;
import org.phoebus.logbook.LogFactory;
Expand All @@ -55,11 +56,14 @@
import org.phoebus.logbook.LogbookException;
import org.phoebus.logbook.LogbookPreferences;
import org.phoebus.logbook.Tag;
import org.phoebus.logbook.olog.ui.AttachmentsViewController;
import org.phoebus.logbook.olog.ui.HelpViewer;
import org.phoebus.logbook.olog.ui.LogbookUIPreferences;
import org.phoebus.logbook.olog.ui.PreviewViewer;
import org.phoebus.logbook.olog.ui.SingleLogEntryDisplayController;
import org.phoebus.logbook.olog.ui.menu.SendToLogBookApp;
import org.phoebus.olog.es.api.OlogProperties;
import org.phoebus.olog.es.api.model.OlogAttachment;
import org.phoebus.olog.es.api.model.OlogLog;
import org.phoebus.security.store.SecureStore;
import org.phoebus.security.tokens.ScopedAuthenticationToken;
Expand All @@ -68,6 +72,10 @@
import org.phoebus.ui.javafx.ImageCache;
import org.phoebus.util.time.TimestampFormats;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -103,7 +111,7 @@ public class LogEntryUpdateController {

@SuppressWarnings("unused")
@FXML
private AttachmentsEditorController attachmentsEditorController;
private AttachmentsViewController attachmentsViewController;
@SuppressWarnings("unused")
@FXML
private LogPropertiesEditorController logPropertiesEditorController;
Expand Down Expand Up @@ -220,7 +228,6 @@ public void initialize() {
completionMessageLabel.textProperty(), submissionInProgress));

cancelButton.disableProperty().bind(submissionInProgress);
attachmentsEditorController.setTextArea(textArea);

userField.textProperty().bindBidirectional(usernameProperty);
userField.textProperty().addListener((changeListener, oldVal, newVal) ->
Expand Down Expand Up @@ -382,6 +389,7 @@ public void initialize() {

// Note: logbooks and tags are retrieved asynchronously from service
setupLogbooksAndTags();
retrieveAttachments();
}

/**
Expand All @@ -406,7 +414,7 @@ public void showHelp() {
*/
@FXML
public void showHtmlPreview() {
new PreviewViewer(getDescription(), attachmentsEditorController.getAttachments()).show();
new PreviewViewer(getDescription(), attachmentsViewController.getAttachments()).show();
}


Expand All @@ -423,7 +431,7 @@ public void submit() {
ologLog.setLevel(selectedLevelProperty.get());
ologLog.setLogbooks(getSelectedLogbooks());
ologLog.setTags(getSelectedTags());
ologLog.setAttachments(attachmentsEditorController.getAttachments());
// ologLog.setAttachments(attachmentsViewController.getAttachments());
ologLog.setProperties(logPropertiesEditorController.getProperties());

LogClient logClient =
Expand All @@ -449,7 +457,6 @@ public void submit() {
logger.log(Level.WARNING, "Secure Store file not found.", ex);
}
}
attachmentsEditorController.deleteTemporaryFiles();
// This will close the editor
Platform.runLater(this::cancel);
}
Expand Down Expand Up @@ -632,6 +639,36 @@ private void setupLogbooksAndTags() {
});
}

private void retrieveAttachments(){
JobManager.schedule("Fetch attachment data", monitor -> {

LogClient logClient =
LogService.getInstance().getLogFactories().get(LogbookPreferences.logbook_factory).getLogClient();
Collection<Attachment> attachments = logEntry.getAttachments().stream()
.filter((attachment) -> attachment.getName() != null && !attachment.getName().isEmpty())
.map((attachment) -> {
OlogAttachment fileAttachment = new OlogAttachment();
fileAttachment.setContentType(attachment.getContentType());
fileAttachment.setThumbnail(false);
fileAttachment.setFileName(attachment.getName());
try {
Path temp = Files.createTempFile("phoebus", attachment.getName());
Files.copy(logClient.getAttachment(logEntry.getId(), attachment.getName()), temp, StandardCopyOption.REPLACE_EXISTING);
fileAttachment.setFile(temp.toFile());
temp.toFile().deleteOnExit();
} catch (LogbookException | IOException e) {
Logger.getLogger(SingleLogEntryDisplayController.class.getName())
.log(Level.WARNING, "Failed to retrieve attachment " + fileAttachment.getFileName(), e);
}
return fileAttachment;
}).collect(Collectors.toList());
// Update UI
Platform.runLater(()->{
attachmentsViewController.setAttachments(attachments);
});
});
}

public void fetchStoredUserCredentials() {
// Perform file IO on background thread.
JobManager.schedule("Access Secure Store", monitor ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<Accordion maxHeight="1.7976931348623157E308">
<panes>
<TitledPane animated="false" maxHeight="1.7976931348623157E308" text="%Attachments">
<fx:include fx:id="attachmentsEditor" maxHeight="1.7976931348623157E308" source="AttachmentsEditor.fxml" />
<fx:include fx:id="attachmentsView" source="../AttachmentsView.fxml" />
</TitledPane>
<TitledPane animated="false" text="%Properties">
<fx:include fx:id="logPropertiesEditor" source="LogPropertiesEditor.fxml" />
Expand Down

0 comments on commit c89bacd

Please sign in to comment.