1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-03 09:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/swing/ImageWindow2.java

444 lines
14 KiB
Java
Raw Normal View History

2011-08-08 17:48:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2011-08-08 17:48:29 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2017-03-15 19:13:31 +00:00
*
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2015-04-07 18:18:37 +00:00
*
2011-08-08 17:48:29 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2011-08-08 17:48:29 +00:00
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
2015-04-07 18:18:37 +00:00
*
2011-08-08 17:48:29 +00:00
*
*/
package net.sourceforge.plantuml.swing;
import java.awt.BorderLayout;
2015-04-07 18:18:37 +00:00
import java.awt.Dimension;
2011-08-08 17:48:29 +00:00
import java.awt.Image;
2015-04-07 18:18:37 +00:00
import java.awt.RenderingHints;
2011-08-08 17:48:29 +00:00
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2015-06-20 10:54:49 +00:00
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
2016-01-09 12:15:40 +00:00
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
2011-08-08 17:48:29 +00:00
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
2015-04-07 18:18:37 +00:00
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
2011-08-08 17:48:29 +00:00
import java.io.File;
2013-12-10 19:36:50 +00:00
import java.io.IOException;
2015-04-07 18:18:37 +00:00
import java.util.Arrays;
import java.util.prefs.Preferences;
2011-08-08 17:48:29 +00:00
import javax.swing.ImageIcon;
import javax.swing.JButton;
2015-04-07 18:18:37 +00:00
import javax.swing.JCheckBox;
2011-08-08 17:48:29 +00:00
import javax.swing.JFrame;
import javax.swing.JPanel;
2015-06-20 10:54:49 +00:00
import javax.swing.JScrollBar;
2011-08-08 17:48:29 +00:00
import javax.swing.JScrollPane;
import javax.swing.ListModel;
2016-01-09 12:15:40 +00:00
import javax.swing.SwingUtilities;
2011-08-08 17:48:29 +00:00
import javax.swing.WindowConstants;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.FileFormat;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.FileFormatOption;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.GeneratedImage;
2018-06-12 20:50:45 +00:00
import net.sourceforge.plantuml.ImageSelection;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.GraphicStrings;
2020-05-30 15:20:23 +00:00
import net.sourceforge.plantuml.security.ImageIO;
import net.sourceforge.plantuml.security.SFile;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.ColorMapperIdentity;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.version.PSystemVersion;
2011-08-08 17:48:29 +00:00
class ImageWindow2 extends JFrame {
2015-06-20 10:54:49 +00:00
private final static Preferences prefs = Preferences.userNodeForPackage(ImageWindow2.class);
private final static String KEY_ZOOM_FIT = "zoomfit";
2017-11-20 16:10:36 +00:00
private final static String KEY_WIDTH_FIT = "widthfit";
2015-04-07 18:18:37 +00:00
2011-08-08 17:48:29 +00:00
private SimpleLine2 simpleLine2;
2015-06-20 10:54:49 +00:00
private final JScrollPane scrollPane;
2011-08-08 17:48:29 +00:00
private final JButton next = new JButton("Next");
2013-12-10 19:36:50 +00:00
private final JButton copy = new JButton("Copy");
2011-08-08 17:48:29 +00:00
private final JButton previous = new JButton("Previous");
2015-04-07 18:18:37 +00:00
private final JCheckBox zoomFitButt = new JCheckBox("Zoom fit");
2017-11-20 16:10:36 +00:00
private final JCheckBox widthFitButt = new JCheckBox("Width fit");
2017-03-12 17:22:02 +00:00
private final JButton zoomMore = new JButton("+");
private final JButton zoomLess = new JButton("-");
2016-01-09 12:15:40 +00:00
private final MainWindow2 main;
2015-04-07 18:18:37 +00:00
2011-08-08 17:48:29 +00:00
private final ListModel listModel;
private int index;
2017-03-12 17:22:02 +00:00
private int zoomFactor = 0;
2011-08-08 17:48:29 +00:00
2015-04-07 18:18:37 +00:00
private enum SizeMode {
2017-11-20 16:10:36 +00:00
FULL_SIZE, ZOOM_FIT, WIDTH_FIT
2015-04-07 18:18:37 +00:00
};
private SizeMode sizeMode = SizeMode.FULL_SIZE;
2016-01-09 12:15:40 +00:00
private int startX, startY;
2011-08-08 17:48:29 +00:00
public ImageWindow2(SimpleLine2 simpleLine, final MainWindow2 main, ListModel listModel, int index) {
super(simpleLine.toString());
2013-12-10 19:36:50 +00:00
setIconImage(PSystemVersion.getPlantumlSmallIcon2());
2011-08-08 17:48:29 +00:00
this.simpleLine2 = simpleLine;
this.listModel = listModel;
this.index = index;
2016-01-09 12:15:40 +00:00
this.main = main;
2011-08-08 17:48:29 +00:00
final JPanel north = new JPanel();
north.add(previous);
2013-12-10 19:36:50 +00:00
north.add(copy);
2015-04-07 18:18:37 +00:00
north.add(next);
north.add(zoomFitButt);
2017-11-20 16:10:36 +00:00
north.add(widthFitButt);
2017-03-12 17:22:02 +00:00
north.add(zoomMore);
north.add(zoomLess);
2015-06-20 10:54:49 +00:00
copy.setFocusable(false);
2013-12-10 19:36:50 +00:00
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
copy();
}
});
2015-06-20 10:54:49 +00:00
next.setFocusable(false);
2011-08-08 17:48:29 +00:00
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
next();
}
});
2015-06-20 10:54:49 +00:00
previous.setFocusable(false);
2011-08-08 17:48:29 +00:00
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
previous();
}
});
2015-06-20 10:54:49 +00:00
zoomFitButt.setFocusable(false);
2015-04-07 18:18:37 +00:00
zoomFitButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
2017-11-20 16:10:36 +00:00
widthFitButt.setSelected(false);
zoomFit();
}
});
widthFitButt.setFocusable(false);
widthFitButt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
zoomFitButt.setSelected(false);
2015-04-07 18:18:37 +00:00
zoomFit();
}
});
2017-03-12 17:22:02 +00:00
zoomMore.setFocusable(false);
zoomMore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
zoomFactor++;
refreshImage(false);
}
});
zoomLess.setFocusable(false);
zoomLess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
zoomFactor--;
refreshImage(false);
}
});
2011-08-08 17:48:29 +00:00
scrollPane = new JScrollPane(buildScrollablePicture());
getContentPane().add(north, BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setSize(640, 400);
2013-12-10 19:36:50 +00:00
this.setLocationRelativeTo(this.getParent());
2011-08-08 17:48:29 +00:00
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
main.closing(ImageWindow2.this);
}
});
2015-04-07 18:18:37 +00:00
this.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent e) {
super.componentResized(e);
2016-01-09 12:15:40 +00:00
refreshImage(false);
2015-04-07 18:18:37 +00:00
}
});
final boolean zoomChecked = prefs.getBoolean(KEY_ZOOM_FIT, false);
zoomFitButt.setSelected(zoomChecked);
if (zoomChecked) {
sizeMode = SizeMode.ZOOM_FIT;
}
2017-11-20 16:10:36 +00:00
final boolean widthZoomChecked = prefs.getBoolean(KEY_WIDTH_FIT, false);
widthFitButt.setSelected(widthZoomChecked);
if (widthZoomChecked) {
sizeMode = SizeMode.WIDTH_FIT;
}
2015-04-07 18:18:37 +00:00
2015-06-20 10:54:49 +00:00
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_RIGHT) {
next();
} else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_LEFT) {
previous();
} else if (evt.isAltDown() && evt.getKeyCode() == KeyEvent.VK_RIGHT) {
next();
} else if (evt.isAltDown() && evt.getKeyCode() == KeyEvent.VK_LEFT) {
previous();
} else if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
imageRight();
} else if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
imageLeft();
} else if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
imageDown();
} else if (evt.getKeyCode() == KeyEvent.VK_UP) {
imageUp();
} else if (evt.getKeyCode() == KeyEvent.VK_C) {
copy();
} else if (evt.getKeyCode() == KeyEvent.VK_Z) {
zoomFitButt.setSelected(!zoomFitButt.isSelected());
zoomFit();
}
}
});
2011-08-08 17:48:29 +00:00
}
private void next() {
index++;
updateSimpleLine();
}
private void previous() {
index--;
updateSimpleLine();
}
2015-06-20 10:54:49 +00:00
private void imageDown() {
final JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue(bar.getValue() + bar.getBlockIncrement());
}
private void imageUp() {
final JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue(bar.getValue() - bar.getBlockIncrement());
}
private void imageLeft() {
final JScrollBar bar = scrollPane.getHorizontalScrollBar();
bar.setValue(bar.getValue() - bar.getBlockIncrement());
}
private void imageRight() {
final JScrollBar bar = scrollPane.getHorizontalScrollBar();
bar.setValue(bar.getValue() + bar.getBlockIncrement());
}
2015-04-07 18:18:37 +00:00
private void zoomFit() {
2017-11-20 16:10:36 +00:00
final boolean selectedZoom = zoomFitButt.isSelected();
final boolean selectedWidth = widthFitButt.isSelected();
prefs.putBoolean(KEY_ZOOM_FIT, selectedZoom);
prefs.putBoolean(KEY_WIDTH_FIT, selectedWidth);
2017-03-12 17:22:02 +00:00
zoomFactor = 0;
2017-11-20 16:10:36 +00:00
if (selectedZoom) {
2015-04-07 18:18:37 +00:00
sizeMode = SizeMode.ZOOM_FIT;
2017-11-20 16:10:36 +00:00
} else if (selectedWidth) {
sizeMode = SizeMode.WIDTH_FIT;
2015-04-07 18:18:37 +00:00
} else {
sizeMode = SizeMode.FULL_SIZE;
}
2016-01-09 12:15:40 +00:00
refreshImage(false);
2015-04-07 18:18:37 +00:00
}
2011-08-08 17:48:29 +00:00
private void updateSimpleLine() {
if (index < 0) {
index = 0;
}
if (index > listModel.getSize() - 1) {
index = listModel.getSize() - 1;
}
simpleLine2 = (SimpleLine2) listModel.getElementAt(index);
setTitle(simpleLine2.toString());
2016-01-09 12:15:40 +00:00
refreshImage(false);
}
private void refreshSimpleLine() {
for (SimpleLine2 line : main.getCurrentDirectoryListing2()) {
if (line.getFile().equals(simpleLine2.getFile())) {
simpleLine2 = line;
setTitle(simpleLine2.toString());
}
}
2011-08-08 17:48:29 +00:00
}
private ScrollablePicture buildScrollablePicture() {
final GeneratedImage generatedImage = simpleLine2.getGeneratedImage();
if (generatedImage == null) {
return null;
}
final File png = generatedImage.getPngFile();
2015-04-07 18:18:37 +00:00
BufferedImage image = null;
try {
2020-05-30 15:20:23 +00:00
image = ImageIO.read(new SFile(png.getAbsolutePath()));
2015-04-07 18:18:37 +00:00
if (sizeMode == SizeMode.ZOOM_FIT) {
final Dimension imageDim = new Dimension(image.getWidth(), image.getHeight());
final Dimension newImgDim = ImageHelper
.getScaledDimension(imageDim, scrollPane.getViewport().getSize());
2017-03-12 17:22:02 +00:00
image = ImageHelper.getScaledInstance(image, newImgDim, getHints(), true);
2017-11-20 16:10:36 +00:00
} else if (sizeMode == SizeMode.WIDTH_FIT) {
final Dimension imageDim = new Dimension(image.getWidth(), image.getHeight());
final Dimension newImgDim = ImageHelper.getScaledDimensionWidthFit(imageDim, scrollPane.getViewport()
.getSize());
image = ImageHelper.getScaledInstance(image, newImgDim, getHints(), false);
2017-03-12 17:22:02 +00:00
} else if (zoomFactor != 0) {
final Dimension imageDim = new Dimension(image.getWidth(), image.getHeight());
final Dimension newImgDim = ImageHelper.getScaledDimension(imageDim, getZoom());
image = ImageHelper.getScaledInstance(image, newImgDim, getHints(), false);
2015-04-07 18:18:37 +00:00
}
} catch (IOException ex) {
final String msg = "Error reading file: " + ex.toString();
2016-11-18 21:12:09 +00:00
final TextBlockBackcolored error = GraphicStrings.createForError(Arrays.asList(msg), false);
final ImageBuilder imageBuilder = ImageBuilder.buildA(new ColorMapperIdentity(),
false, null, null, null, 1.0, error.getBackcolor());
2016-03-06 16:47:34 +00:00
imageBuilder.setUDrawable(error);
2015-04-07 18:18:37 +00:00
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
2017-04-19 18:30:16 +00:00
imageBuilder.writeImageTOBEMOVED(new FileFormatOption(FileFormat.PNG), 42, baos);
2015-04-07 18:18:37 +00:00
baos.close();
image = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
}
2011-08-08 17:48:29 +00:00
final ImageIcon imageIcon = new ImageIcon(image, simpleLine2.toString());
final ScrollablePicture scrollablePicture = new ScrollablePicture(imageIcon, 1);
2017-03-12 17:22:02 +00:00
2016-01-09 12:15:40 +00:00
scrollablePicture.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
super.mousePressed(me);
startX = me.getX();
startY = me.getY();
}
});
scrollablePicture.addMouseMotionListener(new MouseMotionAdapter() {
2017-03-12 17:22:02 +00:00
public void mouseDragged(MouseEvent me) {
super.mouseDragged(me);
final int diffX = me.getX() - startX;
final int diffY = me.getY() - startY;
2016-01-09 12:15:40 +00:00
final JScrollBar hbar = scrollPane.getHorizontalScrollBar();
hbar.setValue(hbar.getValue() - diffX);
final JScrollBar vbar = scrollPane.getVerticalScrollBar();
vbar.setValue(vbar.getValue() - diffY);
2017-03-12 17:22:02 +00:00
}
});
2016-01-09 12:15:40 +00:00
2011-08-08 17:48:29 +00:00
return scrollablePicture;
}
2017-03-12 17:22:02 +00:00
private RenderingHints getHints() {
final RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
return hints;
}
private double getZoom() {
// if (zoomFactor <= -10) {
// return 0.05;
// }
// return 1.0 + zoomFactor / 10.0;
return Math.pow(1.1, zoomFactor);
}
2013-12-10 19:36:50 +00:00
private void copy() {
final GeneratedImage generatedImage = simpleLine2.getGeneratedImage();
if (generatedImage == null) {
return;
}
final File png = generatedImage.getPngFile();
final Image image = Toolkit.getDefaultToolkit().createImage(png.getAbsolutePath());
final ImageSelection imgSel = new ImageSelection(image);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
}
2011-08-08 17:48:29 +00:00
public SimpleLine2 getSimpleLine() {
return simpleLine2;
}
2016-01-09 12:15:40 +00:00
private int v1;
private int v2;
public void refreshImage(boolean external) {
final JScrollBar bar1 = scrollPane.getVerticalScrollBar();
final JScrollBar bar2 = scrollPane.getHorizontalScrollBar();
if (external && isError() == false) {
v1 = bar1.getValue();
v2 = bar2.getValue();
}
2011-08-08 17:48:29 +00:00
scrollPane.setViewportView(buildScrollablePicture());
force();
2016-01-09 12:15:40 +00:00
if (external) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
refreshSimpleLine();
if (isError() == false) {
bar1.setValue(v1);
bar2.setValue(v2);
}
}
});
}
}
private boolean isError() {
return simpleLine2.getGeneratedImage() != null && simpleLine2.getGeneratedImage().lineErrorRaw() != -1;
2011-08-08 17:48:29 +00:00
}
private void force() {
// setVisible(true);
repaint();
// validate();
// getContentPane().validate();
// getContentPane().setVisible(true);
// getContentPane().repaint();
// scrollPane.validate();
// scrollPane.setVisible(true);
// scrollPane.repaint();
}
}