Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions jhotdraw-samples/jhotdraw-samples-misc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.37.0</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -196,21 +197,23 @@ public void dispose() {
*/
public Drawing createDrawing() {
Drawing drawing = new QuadTreeDrawing();

// Input formats - using modular registry for image formats
LinkedList<InputFormat> inputFormats = new LinkedList<InputFormat>();
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<OutputFormat> outputFormats = new LinkedList<OutputFormat>();
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<FileFilter, OutputFormat> map = (HashMap<FileFilter, OutputFormat>)
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;
}

/**
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 "=========================================="
'';
Expand Down
Loading