PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label vaadin. Show all posts
Showing posts with label vaadin. Show all posts

Friday, October 28, 2022

[FIXED] What is the best way to check if the Rich Text Editor Content is empty in Vaadin 14?

 October 28, 2022     is-empty, rich-text-editor, vaadin, vaadin14     No comments   

Issue

I want to check if the Rich Text Editor is empty before proceeding a action. But the .isEmpty() method only works if no format button is clicked. So for example if you just press the H1 format button inside the editor without writing anything the .getValue() method returns:

[{"attributes":{"header":1},"insert":"\n"}]

And because of this the .isEmpty() method doesn't work properly in this case. So I want to check if there is any text or a image inside the editor.

Update: So the closest I can get is using this:

private boolean isEditorEmpty(String value) { //value = RichTextEditor.getHtmlValue()
    if (value.replaceAll("<[^>]*>", "").strip().length() < 1)
        return true;
    return false;
}

But I am not able to exclude pictures, because after adding a picture to the RichTextEditor the .getHtmlValue() method always returns an empty string.

Hope someone can help me


Solution

It works using this method to check if the editor is empty:

private boolean isEditorEmpty(String value) { //value = RichTextEditor.getHtmlValue()
   if (value == null || !value.contains("<img") && value.replaceAll("<[^>]*>", "").strip().length() < 1)
        return true;
    return false;
}


Answered By - Lukas
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, July 31, 2022

[FIXED] How to get the File from an Upload component in Vaadin

 July 31, 2022     file-upload, interface, java, receiver, vaadin     No comments   

Issue

I'm new with Vaadin, and I don't know how to get the File from the Upload component, I'm always having a server error. I don't think I understand properly how this works. Any help will be welcome. This is my code.

public MainView() {
    HorizontalLayout horizontalLayout = new HorizontalLayout();
    horizontalLayout.add(
            new H1("Stop Motion Creator")
    );
    add(horizontalLayout);

    MemoryBuffer memoryBuffer = new MemoryBuffer();
    Upload midiFileUpload = new Upload(memoryBuffer);
    midiFileUpload.setMaxFiles(1);
    midiFileUpload.setDropLabel(new Label("Upload a file in .mid format"));
    midiFileUpload.setAcceptedFileTypes("mid");
    midiFileUpload.setMaxFileSize(10);

    midiFileUpload.addSucceededListener(event -> {
        InputStream inputFileData = memoryBuffer.getInputStream();
        String fileName = event.getFileName();
        long contentLength = event.getContentLength();
        String mimeType = event.getMIMEType();

        Receiver receiver = new Receiver() {
            @Override
            public OutputStream receiveUpload(String s, String s1) {
                return null;
            }
        };
        OutputStream outputFileData = receiver.receiveUpload(fileName, mimeType);
        FileData filedata = new FileData(fileName, mimeType,outputFileData);
        File midiFile = filedata.getFile();
}

Solution

Here's a code sample from one of my projects

FileUploader buffer = new FileUploader();    
upload.setReceiver(buffer);
upload.setAcceptedFileTypes("image/jpeg", "image/png");
upload.addSucceededListener(event -> {
            try {
                File file = new File(buffer.getFilename());
                // Do whatever with file object
            } catch (IOException e) {
                e.printStackTrace();
            }
});

FlieUploader.java

public class FileUploader implements Receiver {
    public static String BASE_PATH = "/tmp/";
    private File file;
    private String filename;

    public OutputStream receiveUpload(String filename,
                                      String mimeType) {
        // Create upload stream
        FileOutputStream fos = null; // Stream to write to
        try {
            // Open the file for writing.
            this.filename = FileUploader.BASE_PATH + filename;

            file = new File(this.filename);
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            return null;
        }
        return fos; // Return the output stream to write to
    }

    public String getFilename() {
        return filename;
    }
};


Answered By - Jerry U
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing