From 62f6d488632ab71af910f04a372da0d183e66d94 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Fri, 22 Nov 2024 19:44:33 +0100 Subject: [PATCH] improve html display --- .../communication/model/EfficientSAM.java | 25 +-- .../model/EfficientViTSAML0.java | 41 ++-- .../model/EfficientViTSAML1.java | 42 ++-- .../model/EfficientViTSAML2.java | 39 ++-- .../model/EfficientViTSAMXL0.java | 38 ++-- .../model/EfficientViTSAMXL1.java | 38 ++-- .../samj/communication/model/SAM2Large.java | 29 +-- .../samj/communication/model/SAM2Small.java | 29 +-- .../samj/communication/model/SAM2Tiny.java | 28 +-- .../samj/communication/model/SAMModel.java | 58 ++++- src/main/java/ai/nets/samj/gui/HTMLPane.java | 199 ++++++++++-------- src/main/java/ai/nets/samj/gui/HTMLPane2.java | 110 ++++++++++ .../samj/gui/components/ModelDrawerPanel.java | 60 +++++- 13 files changed, 452 insertions(+), 284 deletions(-) create mode 100644 src/main/java/ai/nets/samj/gui/HTMLPane2.java diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientSAM.java b/src/main/java/ai/nets/samj/communication/model/EfficientSAM.java index 8017d19..6a84692 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientSAM.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientSAM.java @@ -76,14 +76,6 @@ public void error(String text) { */ public static final String INPUT_IMAGE_AXES = "xyc"; - private static final String HTML_DESCRIPTION = "EfficientSAM: Leveraged Masked Image Pretraining for Efficient Segment Anything
" - + "Weights size: 105.7 MB
" - + "Speed: 6th out of 6
" - + "Performance: 1st out of 6
" - + "GitHub Repository: https://github.com/yformer/EfficientSAM
" - + "Paper: EfficientSAM: Leveraged Masked Image Pretraining for Efficient Segment\n" - + "Anything"; - private static final String CAUTION_STRING = "

CAUTION: This model is computationally heavy. It is not recommended to use it on lower-end computers.

"; @@ -91,6 +83,15 @@ public void error(String text) { * Create an instance of the model that loads the model and encodes an image */ public EfficientSAM() { + this.isHeavy = true; + this.fullName = "EfficientSAM: Leveraged Masked Image Pretraining for Efficient Segment Anything"; + this.githubLink = "https://github.com/yformer/EfficientSAM"; + this.paperLink = "https://arxiv.org/pdf/2312.00863.pdf"; + this.githubName = "https://github.com/yformer/EfficientSAM"; + this.paperName = "EfficientSAM: Leveraged Masked Image Pretraining for Efficient Segment"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 105.7; this.manager = EfficientSamEnvManager.create(); } @@ -102,14 +103,6 @@ public String getName() { return FULL_NAME; } - @Override - /** - * {@inheritDoc} - */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : CAUTION_STRING); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java index ec086fe..3f42806 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java @@ -76,31 +76,31 @@ public void error(String text) { * Axes order required for the input image by the model */ public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "EfficientViT-SAM smallest version (L0)
" - + "Weights size: 139.4 MB
" - + "Speed: 1st out of 6
" - + "Performance: 6th out of 6
" - + "GitHub Repository: " - + "https://github.com/mit-han-lab/efficientvit
" - + "Paper: EfficientViT-SAM: Accelerated " - + "Segment Anything Model Without Performance Loss"; - - @Override + + /** - * {@inheritDoc} + * Create an instance of the model that loads the model and encodes an image */ - public String getName() { - return FULL_NAME; + public EfficientViTSAML0() { + this.isHeavy = false; + this.fullName = "EfficientViT-SAM smallest version (L0)"; + this.githubLink = "https://github.com/mit-han-lab/efficientvit"; + this.githubName = "https://github.com/mit-han-lab/efficientvit"; + this.paperName = "EfficientViT-SAM: Accelerated Segment Anything Model Without Performance Loss"; + this.paperLink = "https://arxiv.org/pdf/2402.05008.pdf"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 139.4; + this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l0"); } - + @Override /** * {@inheritDoc} */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.installed ? SAMModel.HTML_NOT_INSTALLED : ""); + public String getName() { + return FULL_NAME; } @Override @@ -136,13 +136,6 @@ public & NativeType> void setImage(final RandomAccessi } } - /** - * Create an instance of the model that loads the model and encodes an image - */ - public EfficientViTSAML0() { - this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l0"); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java index 10dbc50..5820bb0 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML1.java @@ -74,32 +74,31 @@ public void error(String text) { /** * Axes order required for the input image by the model */ - public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "EfficientViT-SAM, fourth biggest version (L1)
" - + "Weights size: 190.9 MB
" - + "Speed: 2nd out of 6
" - + "Performance: 5th out of 6
" - + "GitHub Repository: " - + "https://github.com/mit-han-lab/efficientvit
" - + "Paper: EfficientViT-SAM: Accelerated " - + "Segment Anything Model Without Performance Loss"; - + public static final String INPUT_IMAGE_AXES = "xyc"; + - @Override /** - * {@inheritDoc} + * Create an instance of the model that loads the model and encodes an image */ - public String getName() { - return FULL_NAME; + public EfficientViTSAML1() { + this.isHeavy = false; + this.fullName = "EfficientViT-SAM smallest version (L1)"; + this.githubLink = "https://github.com/mit-han-lab/efficientvit"; + this.githubName = "https://github.com/mit-han-lab/efficientvit"; + this.paperName = "EfficientViT-SAM: Accelerated Segment Anything Model Without Performance Loss"; + this.paperLink = "https://arxiv.org/pdf/2402.05008.pdf"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 190.9; + this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l1"); } - + @Override /** * {@inheritDoc} */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : ""); + public String getName() { + return FULL_NAME; } @Override @@ -127,13 +126,6 @@ public & NativeType> void setImage(final RandomAccessi } } - /** - * Create an instance of the model that loads the model and encodes an image - */ - public EfficientViTSAML1() { - this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l1"); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java index 2fc16e6..ce0682b 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAML2.java @@ -75,30 +75,30 @@ public void error(String text) { * Axes order required for the input image by the model */ public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "EfficientViT-SAM, third biggest version (L2)
" - + "Weights size: 245.7 MB
" - + "Speed: 3rd out of 6
" - + "Performance: 4th out of 6
" - + "GitHub Repository: " - + "https://github.com/mit-han-lab/efficientvit
" - + "Paper: EfficientViT-SAM: Accelerated " - + "Segment Anything Model Without Performance Loss"; - @Override + /** - * {@inheritDoc} + * Create an instance of the model that loads the model and encodes an image */ - public String getName() { - return FULL_NAME; + public EfficientViTSAML2() { + this.isHeavy = false; + this.fullName = "EfficientViT-SAM smallest version (L2)"; + this.githubLink = "https://github.com/mit-han-lab/efficientvit"; + this.githubName = "https://github.com/mit-han-lab/efficientvit"; + this.paperName = "EfficientViT-SAM: Accelerated Segment Anything Model Without Performance Loss"; + this.paperLink = "https://arxiv.org/pdf/2402.05008.pdf"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 245.7; + this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l2"); } - + @Override /** * {@inheritDoc} */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : ""); + public String getName() { + return FULL_NAME; } @Override @@ -126,13 +126,6 @@ public & NativeType> void setImage(final RandomAccessi } } - /** - * Create an instance of the model that loads the model and encodes an image - */ - public EfficientViTSAML2() { - this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l2"); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java index 58bef26..46752f4 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL0.java @@ -75,30 +75,29 @@ public void error(String text) { * Axes order required for the input image by the model */ public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "EfficientViT-SAM, second biggest version (XL0)
" - + "Weights size: 468.2 MB
" - + "Speed: 4th out of 6
" - + "Performance: 3rd out of 6
" - + "GitHub Repository: " - + "https://github.com/mit-han-lab/efficientvit
" - + "Paper: EfficientViT-SAM: Accelerated " - + "Segment Anything Model Without Performance Loss"; - @Override /** - * {@inheritDoc} + * Create an instance of the model that loads the model and encodes an image */ - public String getName() { - return FULL_NAME; + public EfficientViTSAMXL0() { + this.isHeavy = false; + this.fullName = "EfficientViT-SAM smallest version (XL0)"; + this.githubLink = "https://github.com/mit-han-lab/efficientvit"; + this.githubName = "https://github.com/mit-han-lab/efficientvit"; + this.paperName = "EfficientViT-SAM: Accelerated Segment Anything Model Without Performance Loss"; + this.paperLink = "https://arxiv.org/pdf/2402.05008.pdf"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 468.2; + this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "xl0"); } - + @Override /** * {@inheritDoc} */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : ""); + public String getName() { + return FULL_NAME; } @Override @@ -126,13 +125,6 @@ public & NativeType> void setImage(final RandomAccessi } } - /** - * Create an instance of the model that loads the model and encodes an image - */ - public EfficientViTSAMXL0() { - this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "xl0"); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java index 5021935..c3512b4 100644 --- a/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java +++ b/src/main/java/ai/nets/samj/communication/model/EfficientViTSAMXL1.java @@ -76,31 +76,30 @@ public void error(String text) { * Axes order required for the input image by the model */ public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "EfficientViT-SAM, biggest version (XL1)
" - + "Weights size: 814 MB
" - + "Speed: 5th out of 6
" - + "Performance: 2nd out of 6
" - + "GitHub Repository: " - + "https://github.com/mit-han-lab/efficientvit
" - + "Paper: EfficientViT-SAM: Accelerated " - + "Segment Anything Model Without Performance Loss"; - @Override /** - * {@inheritDoc} + * Create an instance of the model that loads the model and encodes an image */ - public String getName() { - return FULL_NAME; + public EfficientViTSAMXL1() { + this.isHeavy = false; + this.fullName = "EfficientViT-SAM smallest version (XL1)"; + this.githubLink = "https://github.com/mit-han-lab/efficientvit"; + this.githubName = "https://github.com/mit-han-lab/efficientvit"; + this.paperName = "EfficientViT-SAM: Accelerated Segment Anything Model Without Performance Loss"; + this.paperLink = "https://arxiv.org/pdf/2402.05008.pdf"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 814; + this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "xl1"); } - + @Override /** * {@inheritDoc} */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : ""); + public String getName() { + return FULL_NAME; } @Override @@ -128,13 +127,6 @@ public & NativeType> void setImage(final RandomAccessi } } - /** - * Create an instance of the model that loads the model and encodes an image - */ - public EfficientViTSAMXL1() { - this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "xl1"); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Large.java b/src/main/java/ai/nets/samj/communication/model/SAM2Large.java index 9e72e3f..a29e41e 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Large.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Large.java @@ -74,23 +74,22 @@ public void error(String text) { /** * Axes order required for the input image by the model */ - public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "SAM-2: Segment Anything Model 2
" - + "Weights size: 155.9 MB
" - + "Speed: 6th out of 6
" - + "Performance: 1st out of 6
" - + "GitHub Repository: https://github.com/facebookresearch/segment-anything-2
" - + "Paper: SAM 2: Segment Anything in Images and Videos\n" - + "Anything"; - - private static final String CAUTION_STRING = "

CAUTION: This model is computationally heavy. It is not recommended to use it on lower-end computers.

"; + public static final String INPUT_IMAGE_AXES = "xyc"; /** * Create an instance of the model that loads the model and encodes an image */ public SAM2Large() { + this.isHeavy = true; + this.fullName = "SAM-2: Segment Anything Model 2 (Large)"; + this.githubLink = "https://github.com/facebookresearch/segment-anything-2"; + this.paperLink = "https://ai.meta.com/research/publications/sam-2-segment-anything-in-images-and-videos/"; + this.githubName = "https://github.com/facebookresearch/segment-anything-2"; + this.paperName = "SAM 2: Segment Anything in Images and Videos"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 155.9; this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, "large"); } @@ -102,14 +101,6 @@ public String getName() { return FULL_NAME; } - @Override - /** - * {@inheritDoc} - */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : CAUTION_STRING); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Small.java b/src/main/java/ai/nets/samj/communication/model/SAM2Small.java index 41db326..3c74909 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Small.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Small.java @@ -75,22 +75,21 @@ public void error(String text) { * Axes order required for the input image by the model */ public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "SAM-2: Segment Anything Model 2
" - + "Weights size: 184.3 MB
" - + "Speed: 6th out of 6
" - + "Performance: 1st out of 6
" - + "GitHub Repository: https://github.com/facebookresearch/segment-anything-2
" - + "Paper: SAM 2: Segment Anything in Images and Videos\n" - + "Anything"; - - private static final String CAUTION_STRING = "

CAUTION: This model is computationally heavy. It is not recommended to use it on lower-end computers.

"; - + /** * Create an instance of the model that loads the model and encodes an image */ public SAM2Small() { + this.isHeavy = true; + this.fullName = "SAM-2: Segment Anything Model 2 (Small)"; + this.githubLink = "https://github.com/facebookresearch/segment-anything-2"; + this.paperLink = "https://ai.meta.com/research/publications/sam-2-segment-anything-in-images-and-videos/"; + this.githubName = "https://github.com/facebookresearch/segment-anything-2"; + this.paperName = "SAM 2: Segment Anything in Images and Videos"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 184.3; this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, "small"); } @@ -102,14 +101,6 @@ public String getName() { return FULL_NAME; } - @Override - /** - * {@inheritDoc} - */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : CAUTION_STRING); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java b/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java index a9df324..39f7109 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java @@ -74,22 +74,22 @@ public void error(String text) { /** * Axes order required for the input image by the model */ - public static final String INPUT_IMAGE_AXES = "xyc"; - - private static final String HTML_DESCRIPTION = "SAM-2: Segment Anything Model 2
" - + "Weights size: 155.9 MB
" - + "Speed: 6th out of 6
" - + "Performance: 1st out of 6
" - + "GitHub Repository: https://github.com/facebookresearch/segment-anything-2
" - + "Paper: SAM 2: Segment Anything in Images and Videos\n" - + "Anything"; - + public static final String INPUT_IMAGE_AXES = "xyc"; /** * Create an instance of the model that loads the model and encodes an image */ public SAM2Tiny() { + this.isHeavy = false; + this.fullName = "SAM-2: Segment Anything Model 2 (Tiny)"; + this.githubLink = "https://github.com/facebookresearch/segment-anything-2"; + this.paperLink = "https://ai.meta.com/research/publications/sam-2-segment-anything-in-images-and-videos/"; + this.githubName = "https://github.com/facebookresearch/segment-anything-2"; + this.paperName = "SAM 2: Segment Anything in Images and Videos"; + this.speedRank = 3; + this.performanceRank = 3; + this.size = 155.9; this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, "tiny"); } @@ -101,14 +101,6 @@ public String getName() { return FULL_NAME; } - @Override - /** - * {@inheritDoc} - */ - public String getDescription() { - return HTML_DESCRIPTION + (!this.isInstalled() ? SAMModel.HTML_NOT_INSTALLED : ""); - } - @Override /** * {@inheritDoc} diff --git a/src/main/java/ai/nets/samj/communication/model/SAMModel.java b/src/main/java/ai/nets/samj/communication/model/SAMModel.java index ea42c05..d2dc7ea 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAMModel.java +++ b/src/main/java/ai/nets/samj/communication/model/SAMModel.java @@ -43,6 +43,45 @@ * @author Carlos Javier Garcia Lopez de Haro */ public abstract class SAMModel { + + protected String fullName; + protected double size; + protected int performanceRank; + protected int speedRank; + protected String githubName; + protected String paperName; + protected String githubLink; + protected String paperLink; + protected boolean isHeavy; + + private static final String CAUTION_STRING = "

CAUTION: This model is" + + " computationally heavy. It is not recommended to use it on lower-end computers.

"; + + protected static String HTML_MODEL_FORMAT = "" + + "

%s

" + System.lineSeparator() + + "" + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + " " + System.lineSeparator() + + "
Weights size:%s MB
Speed:%sth out of 6
Performance:%sst out of 6
GitHub Repository:%s
Paper:%s
" + System.lineSeparator() + + ""; public static String HTML_NOT_INSTALLED = "

This model is not installed yet.

"; @@ -56,11 +95,6 @@ public abstract class SAMModel { * @return the axes order required for the input image to the model */ public abstract String getInputImageAxes(); - /** - * - * @return a text describing the model. - */ - public abstract String getDescription(); /** * * @return the {@link SamEnvManagerAbstract} used to install this model @@ -158,6 +192,20 @@ void setImage(final RandomAccessibleInterval image, final SAMJLogger useThisL * Notify the User Interface that the model has been closed */ public abstract void notifyUiHasBeenClosed(); + + /** + * + * @return a text describing the model. + */ + public String getDescription() { + String description = String.format(HTML_MODEL_FORMAT, fullName, "" + size, + "" + speedRank, "" + performanceRank, githubLink, githubName, paperLink, paperName); + boolean installed = this.isInstalled(); + description += this.isHeavy & installed ? CAUTION_STRING : ""; + description += installed ? "" : HTML_NOT_INSTALLED; + return description; + } + /** * * @return true or false whether all the things needed to run the model are already installed or not. diff --git a/src/main/java/ai/nets/samj/gui/HTMLPane.java b/src/main/java/ai/nets/samj/gui/HTMLPane.java index 9f76f03..6b26cc1 100644 --- a/src/main/java/ai/nets/samj/gui/HTMLPane.java +++ b/src/main/java/ai/nets/samj/gui/HTMLPane.java @@ -8,103 +8,136 @@ import javax.swing.text.Document; /** - * This class extends the Java JEditorPane to make a easy to use panel to + * This class extends the Java JEditorPane to make an easy-to-use panel to * display HTML information. * - * @author Daniel Sage, Biomedical Imaging Group, EPFL, Lausanne, Switzerland. - * */ public class HTMLPane extends JEditorPane { - private String html = ""; - private String header = ""; - private String footer = ""; - private Dimension dim; - private String font = "verdana"; - private String color = "#222222"; - private String background = "#f8f8f8"; + private String html = ""; + private String header = ""; + private String footer = ""; + private Dimension dim; + private String font = "Segoe UI"; + private String color = "#333333"; + private String background = "#FFFFFF"; - public HTMLPane() { - create(); - } + public HTMLPane() { + create(); + } - public HTMLPane(String font) { - this.font = font; - create(); - } + public HTMLPane(String font) { + this.font = font; + create(); + } - public HTMLPane(int width, int height) { - this.dim = new Dimension(width, height); - create(); - } + public HTMLPane(int width, int height) { + this.dim = new Dimension(width, height); + create(); + } - public HTMLPane(String font, int width, int height) { - this.font = font; - this.dim = new Dimension(width, height); - create(); - } + public HTMLPane(String font, int width, int height) { + this.font = font; + this.dim = new Dimension(width, height); + create(); + } - public HTMLPane(String font, String color, String background, int width, int height) { - this.font = font; - this.dim = new Dimension(width, height); - this.color = color; - this.background = background; - create(); - } + public HTMLPane(String font, String color, String background, int width, int height) { + this.font = font; + this.dim = new Dimension(width, height); + this.color = color; + this.background = background; + create(); + } - @Override - public String getText() { - Document doc = this.getDocument(); - try { - return doc.getText(0, doc.getLength()); - } - catch (BadLocationException e) { - e.printStackTrace(); - return getText(); - } - } + @Override + public String getText() { + Document doc = this.getDocument(); + try { + return doc.getText(0, doc.getLength()); + } catch (BadLocationException e) { + e.printStackTrace(); + return getText(); + } + } - public void clear() { - html = ""; - append(""); - } + public void clear() { + html = ""; + append(""); + } - private void create() { - header += "\n"; - header += "\n"; - header += "\n"; - header += "\n"; - header += "\n"; - header += "\n"; - header += "\n"; - header += "\n"; - header += "\n"; - footer += "\n"; - setEditable(false); - setContentType("text/html; charset=ISO-8859-1"); - } + private void create() { + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + footer += "\n"; + setEditable(false); + setContentType("text/html; charset=UTF-8"); + } - public void append(String content) { - html += content; - setText(header + html + footer); - if (dim != null) { - setPreferredSize(dim); - } - setCaretPosition(0); - } + public void append(String content) { + html += content; + setText(header + html + footer); + if (dim != null) { + setPreferredSize(dim); + } + setCaretPosition(0); + } - public void append(String tag, String content) { - html += "<" + tag + ">" + content + ""; - setText(header + html + footer); - if (dim != null) { - setPreferredSize(dim); - } - setCaretPosition(0); - } + public void append(String tag, String content) { + html += "<" + tag + ">" + content + ""; + setText(header + html + footer); + if (dim != null) { + setPreferredSize(dim); + } + setCaretPosition(0); + } - public JScrollPane getPane() { - JScrollPane scroll = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - scroll.setPreferredSize(dim); - return scroll; - } + public JScrollPane getPane() { + JScrollPane scroll = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + scroll.setPreferredSize(dim); + return scroll; + } } diff --git a/src/main/java/ai/nets/samj/gui/HTMLPane2.java b/src/main/java/ai/nets/samj/gui/HTMLPane2.java new file mode 100644 index 0000000..0006365 --- /dev/null +++ b/src/main/java/ai/nets/samj/gui/HTMLPane2.java @@ -0,0 +1,110 @@ +package ai.nets.samj.gui; + +import java.awt.Dimension; + +import javax.swing.JEditorPane; +import javax.swing.JScrollPane; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; + +/** + * This class extends the Java JEditorPane to make a easy to use panel to + * display HTML information. + * + * @author Daniel Sage, Biomedical Imaging Group, EPFL, Lausanne, Switzerland. + * + */ +public class HTMLPane2 extends JEditorPane { + + private String html = ""; + private String header = ""; + private String footer = ""; + private Dimension dim; + private String font = "verdana"; + private String color = "#222222"; + private String background = "#f8f8f8"; + + public HTMLPane2() { + create(); + } + + public HTMLPane2(String font) { + this.font = font; + create(); + } + + public HTMLPane2(int width, int height) { + this.dim = new Dimension(width, height); + create(); + } + + public HTMLPane2(String font, int width, int height) { + this.font = font; + this.dim = new Dimension(width, height); + create(); + } + + public HTMLPane2(String font, String color, String background, int width, int height) { + this.font = font; + this.dim = new Dimension(width, height); + this.color = color; + this.background = background; + create(); + } + + @Override + public String getText() { + Document doc = this.getDocument(); + try { + return doc.getText(0, doc.getLength()); + } + catch (BadLocationException e) { + e.printStackTrace(); + return getText(); + } + } + + public void clear() { + html = ""; + append(""); + } + + private void create() { + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + header += "\n"; + footer += "\n"; + setEditable(false); + setContentType("text/html; charset=ISO-8859-1"); + } + + public void append(String content) { + html += content; + setText(header + html + footer); + if (dim != null) { + setPreferredSize(dim); + } + setCaretPosition(0); + } + + public void append(String tag, String content) { + html += "<" + tag + ">" + content + ""; + setText(header + html + footer); + if (dim != null) { + setPreferredSize(dim); + } + setCaretPosition(0); + } + + public JScrollPane getPane() { + JScrollPane scroll = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + scroll.setPreferredSize(dim); + return scroll; + } +} diff --git a/src/main/java/ai/nets/samj/gui/components/ModelDrawerPanel.java b/src/main/java/ai/nets/samj/gui/components/ModelDrawerPanel.java index 09fbe03..5c01332 100644 --- a/src/main/java/ai/nets/samj/gui/components/ModelDrawerPanel.java +++ b/src/main/java/ai/nets/samj/gui/components/ModelDrawerPanel.java @@ -3,6 +3,7 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; +import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; @@ -31,7 +32,7 @@ public class ModelDrawerPanel extends JPanel implements ActionListener { private JLabel drawerTitle = new JLabel(); private JButton install = new JButton("Install"); private JButton uninstall = new JButton("Uninstall"); - HTMLPane html = new HTMLPane("Arial", "#000", "#CCCCCC", 200, 200); + HTMLPane html = new HTMLPane("Segoe UI", "#333333", "#FFFFFF", 200, 200); private SAMModel model; private ModelDrawerPanelListener listener; @@ -39,6 +40,8 @@ public class ModelDrawerPanel extends JPanel implements ActionListener { private Thread modelInstallThread; private Thread infoThread; private Thread installedThread; + private Thread loadingAnimationThread; + private volatile boolean isLoading = false; private static final String MODEL_TITLE = "
%s"; @@ -55,8 +58,13 @@ public static ModelDrawerPanel create(int hSize, ModelDrawerPanelListener listen private void createDrawerPanel() { this.setLayout(new BorderLayout()); - this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); + this.setBorder(BorderFactory.createEtchedBorder()); + drawerTitle.setFont(new Font("Segoe UI", Font.BOLD, 20)); + drawerTitle.setForeground(new Color(50, 50, 50)); drawerTitle.setText(String.format(MODEL_TITLE, " ")); + drawerTitle.setHorizontalAlignment(JLabel.CENTER); // Center the title + //drawerTitle.setFont(new Font("Arial", Font.BOLD, 18)); // Set a modern font and size + drawerTitle.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); // Add padding this.add(drawerTitle, BorderLayout.NORTH); this.add(createInstallModelComponent(), BorderLayout.SOUTH); html.append("Model description"); @@ -69,6 +77,7 @@ private void createDrawerPanel() { this.setPreferredSize(new Dimension(hSize, 0)); // Set preferred width } + // Method to create the install model component private JPanel createInstallModelComponent() { JPanel thirdComponent = new JPanel(); @@ -115,9 +124,14 @@ private void setTitle(String title) { private void setInfo() { html.clear(); + startLoadingAnimation("Loading info"); infoThread =new Thread(() -> { String description = model.getDescription(); - SwingUtilities.invokeLater(() -> html.append("p", description)); + stopLoadingAnimation(); + SwingUtilities.invokeLater(() -> { + html.clear(); + html.append("p", description); + }); }); infoThread.start(); } @@ -167,10 +181,39 @@ private void uninstallModel() { }); } - public interface ModelDrawerPanelListener { - - void setGUIEnabled(boolean enabled); + private void startLoadingAnimation(String message) { + stopLoadingAnimation(); // Ensure any previous animation is stopped + isLoading = true; + loadingAnimationThread = new Thread(() -> { + int dotCount = 0; + String[] dots = {".", "..", "...", ""}; + while (isLoading) { + String currentTime = java.time.LocalTime.now() + .format(java.time.format.DateTimeFormatter.ofPattern("HH:mm:ss")); + String displayMessage = String.format("%s -- %s%s", currentTime, message, dots[dotCount % dots.length]); + dotCount++; + SwingUtilities.invokeLater(() -> { + html.clear(); + html.append("p", displayMessage); + }); + try { + Thread.sleep(300); // Update every half second + } catch (InterruptedException e) { + // Thread was interrupted + break; + } + } + }); + loadingAnimationThread.start(); + } + + private void stopLoadingAnimation() { + isLoading = false; + if (loadingAnimationThread != null && loadingAnimationThread.isAlive()) { + loadingAnimationThread.interrupt(); + } } + private void interruptThreads() { if (infoThread != null) @@ -180,5 +223,10 @@ private void interruptThreads() { if (modelInstallThread != null) this.modelInstallThread.interrupt(); } + + public interface ModelDrawerPanelListener { + + void setGUIEnabled(boolean enabled); + } }