From e1736054b2ae1fea54a407e5fd95191dce7793ec Mon Sep 17 00:00:00 2001 From: tjens23 Date: Thu, 25 Dec 2025 20:00:54 +0100 Subject: [PATCH] issue 38 --- .../app/action/file/ExportFileAction.java | 2 +- .../jhotdraw-samples-misc/pom.xml | 10 +++ .../jhotdraw/samples/svg/SVGDrawingPanel.java | 15 ++-- .../org/jhotdraw/samples/svg/SVGView.java | 69 ++++++++++++++++++- .../jhotdraw/samples/svg/Labels.properties | 5 ++ shell.nix | 2 +- 6 files changed, 94 insertions(+), 9 deletions(-) diff --git a/jhotdraw-app/src/main/java/org/jhotdraw/app/action/file/ExportFileAction.java b/jhotdraw-app/src/main/java/org/jhotdraw/app/action/file/ExportFileAction.java index db0423d9f..b87338710 100644 --- a/jhotdraw-app/src/main/java/org/jhotdraw/app/action/file/ExportFileAction.java +++ b/jhotdraw-app/src/main/java/org/jhotdraw/app/action/file/ExportFileAction.java @@ -143,7 +143,7 @@ public void actionPerformed(ActionEvent evt) { proposedURI = file.toURI(); } } catch (IllegalArgumentException e) { - // allowed empty + System.out.println("ExportFileAction: " + e.getMessage()); } } fileChooser.setSelectedURI(proposedURI); diff --git a/jhotdraw-samples/jhotdraw-samples-misc/pom.xml b/jhotdraw-samples/jhotdraw-samples-misc/pom.xml index ca8104ee5..57edc8541 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/pom.xml +++ b/jhotdraw-samples/jhotdraw-samples-misc/pom.xml @@ -23,6 +23,16 @@ net.sourceforge.htmlunit htmlunit 2.37.0 + + + xerces + xercesImpl + + + xml-apis + xml-apis + + org.aspectj diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGDrawingPanel.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGDrawingPanel.java index c5f94204d..193423b92 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGDrawingPanel.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGDrawingPanel.java @@ -37,6 +37,7 @@ import org.jhotdraw.draw.DrawingEditor; import org.jhotdraw.draw.DrawingView; import org.jhotdraw.draw.QuadTreeDrawing; +import org.jhotdraw.draw.io.ImageFormatRegistry; import org.jhotdraw.draw.io.ImageInputFormat; import org.jhotdraw.draw.io.ImageOutputFormat; import org.jhotdraw.draw.io.InputFormat; @@ -196,21 +197,23 @@ public void dispose() { */ public Drawing createDrawing() { Drawing drawing = new QuadTreeDrawing(); + + // Input formats - using modular registry for image formats LinkedList inputFormats = new LinkedList(); inputFormats.add(new SVGZInputFormat()); - inputFormats.add(new ImageInputFormat(new SVGImageFigure(), "PNG", "Portable Network Graphics (PNG)", "png", "image/png")); - inputFormats.add(new ImageInputFormat(new SVGImageFigure(), "JPG", "Joint Photographics Experts Group (JPEG)", "jpg", "image/jpg")); - inputFormats.add(new ImageInputFormat(new SVGImageFigure(), "GIF", "Graphics Interchange Format (GIF)", "gif", "image/gif")); + // Add all registered image formats (PNG, JPEG, GIF, BMP, etc.) + inputFormats.addAll(ImageFormatRegistry.createInputFormats(new SVGImageFigure())); inputFormats.add(new TextInputFormat(new SVGTextFigure())); drawing.setInputFormats(inputFormats); + + // Output formats - using modular registry for image formats LinkedList outputFormats = new LinkedList(); outputFormats.add(new SVGOutputFormat()); outputFormats.add(new SVGZOutputFormat()); - outputFormats.add(new ImageOutputFormat()); - outputFormats.add(new ImageOutputFormat("JPG", "Joint Photographics Experts Group (JPEG)", "jpg", BufferedImage.TYPE_INT_RGB)); - outputFormats.add(new ImageOutputFormat("BMP", "Windows Bitmap (BMP)", "bmp", BufferedImage.TYPE_BYTE_INDEXED)); + outputFormats.addAll(ImageFormatRegistry.createOutputFormats()); outputFormats.add(new ImageMapOutputFormat()); drawing.setOutputFormats(outputFormats); + return drawing; } diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGView.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGView.java index c09b41fef..1272576d7 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGView.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/SVGView.java @@ -15,6 +15,7 @@ import java.net.URI; import java.util.HashMap; import javax.swing.*; +import javax.swing.filechooser.FileFilter; import org.jhotdraw.action.edit.RedoAction; import org.jhotdraw.action.edit.UndoAction; import org.jhotdraw.api.app.View; @@ -23,6 +24,7 @@ import org.jhotdraw.draw.Drawing; import org.jhotdraw.draw.DrawingEditor; import org.jhotdraw.draw.io.InputFormat; +import org.jhotdraw.draw.io.OutputFormat; import org.jhotdraw.draw.print.DrawingPageable; import org.jhotdraw.gui.JFileURIChooser; import org.jhotdraw.net.URIUtil; @@ -50,6 +52,13 @@ public class SVGView extends AbstractView { */ private UndoRedoManager undo; private PropertyChangeListener propertyHandler; + + /** + * The output format to use when saving. This is set based on the file + * extension when opening or exporting a file, ensuring that saves + * preserve the original format. + */ + private OutputFormat currentOutputFormat; /** * Creates a new View. @@ -123,8 +132,62 @@ protected void setHasUnsavedChanges(boolean newValue) { * Writes the view to the specified uri. */ @Override + @SuppressWarnings("unchecked") public void write(URI uri, URIChooser chooser) throws IOException { - new SVGOutputFormat().write(new File(uri), svgPanel.getDrawing()); + OutputFormat outputFormat = null; + + // First, try to get the format from the chooser (for Export operations) + if (chooser instanceof JFileURIChooser) { + JFileURIChooser fc = (JFileURIChooser) chooser; + FileFilter fileFilter = fc.getFileFilter(); + HashMap map = (HashMap) + fc.getClientProperty("ffOutputFormatMap"); + if (map != null) { + outputFormat = map.get(fileFilter); + } + } + + // If no format from chooser, use the stored format (for Save operations) + if (outputFormat == null) { + outputFormat = currentOutputFormat; + } + + // If still no format, determine from file extension + if (outputFormat == null) { + outputFormat = getOutputFormatForURI(uri); + } + + // Last resort: default to SVG + if (outputFormat == null) { + outputFormat = new SVGOutputFormat(); + } + + // Store the format for future saves + currentOutputFormat = outputFormat; + + outputFormat.write(uri, svgPanel.getDrawing()); + } + + /** + * Determines the appropriate output format based on the file extension. + */ + private OutputFormat getOutputFormatForURI(URI uri) { + String path = uri.getPath().toLowerCase(); + Drawing drawing = svgPanel.getDrawing(); + + for (OutputFormat format : drawing.getOutputFormats()) { + javax.swing.filechooser.FileFilter ff = format.getFileFilter(); + if (ff instanceof javax.swing.filechooser.FileNameExtensionFilter) { + javax.swing.filechooser.FileNameExtensionFilter fnef = + (javax.swing.filechooser.FileNameExtensionFilter) ff; + for (String ext : fnef.getExtensions()) { + if (path.endsWith("." + ext.toLowerCase())) { + return format; + } + } + } + } + return null; } /** @@ -173,6 +236,10 @@ public void read(final URI uri, URIChooser chooser) throws IOException { ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.app.Labels"); throw new IOException(labels.getFormatted("file.open.unsupportedFileFormat.message", URIUtil.getName(uri))); } + + // Set the output format based on the file extension so Save preserves format + currentOutputFormat = getOutputFormatForURI(uri); + SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/resources/org/jhotdraw/samples/svg/Labels.properties b/jhotdraw-samples/jhotdraw-samples-misc/src/main/resources/org/jhotdraw/samples/svg/Labels.properties index 191a94841..2257d85b9 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/resources/org/jhotdraw/samples/svg/Labels.properties +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/resources/org/jhotdraw/samples/svg/Labels.properties @@ -398,15 +398,20 @@ align.toolbar=Align figure.toolbar=Figure arrange.toolbar=Arrange canvas.toolbar=Canvas +attribute.fillOpacity.icon=${imageDir}/attributeOpacity.png attribute.fillOpacity.largeIcon=${imageDir}/attributeOpacity.png attribute.fillOpacity.toolTipText=Fill Opacity +attribute.strokeOpacity.icon=${imageDir}/attributeOpacity.png attribute.strokeOpacity.largeIcon=${imageDir}/attributeOpacity.png attribute.strokeOpacity.toolTipText=Stroke Opacity attribute.canvasFillColor.toolTipText=Canvas Color attribute.canvasFillOpacity.toolTipText=Canvas Opacity +attribute.canvasFillOpacity.icon=${imageDir}/attributeOpacity.png attribute.canvasFillOpacity.largeIcon=${imageDir}/attributeOpacity.png +attribute.canvasFillColor.icon=${imageDir}/attributeFillColor.png attribute.canvasFillColor.largeIcon=${imageDir}/attributeFillColor.png attribute.figureOpacity.toolTipText=Figure Opacity +attribute.figureOpacity.icon=${imageDir}/attributeOpacity.png attribute.figureOpacity.largeIcon=${imageDir}/attributeOpacity.png attribute.canvasFillColor.text=Canvas Color attribute.color.noColor.toolTipText=No Color diff --git a/shell.nix b/shell.nix index 6fd5189dd..0bd658ea5 100644 --- a/shell.nix +++ b/shell.nix @@ -39,7 +39,7 @@ pkgs.mkShell { echo "Commands:" echo " mvn clean compile - Build the project" echo " mvn test - Run tests" - echo " mvn exec:java "-Dexec.mainClass=org.jhotdraw.samples.svg.Main" jhotdraw-samples/jhotdraw-samples-misc" + echo " mvn exec:java -Dexec.mainClass=org.jhotdraw.samples.svg.Main -pl jhotdraw-samples/jhotdraw-samples-misc -q" echo " - Run SVG sample app" echo "==========================================" '';