1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-12-22 10:59:01 +00:00

Implemented zoom control keyboard shortcuts

- Added a `CTRL + +` to zoom in
- Added a `CTRL + -` to zoom out
- Added a `CTRL + 0` to reset zoom
- Minor refactor in classes names (removed "2" appended to GUI's classes name)
This commit is contained in:
damienwojtowicz 2021-08-02 12:11:47 +02:00
parent 15aaf95dcd
commit b5e9171558
4 changed files with 81 additions and 125 deletions

View File

@ -46,7 +46,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;
@ -70,7 +69,7 @@ import net.sourceforge.plantuml.security.SecurityUtils;
import net.sourceforge.plantuml.sprite.SpriteGrayLevel;
import net.sourceforge.plantuml.sprite.SpriteUtils;
import net.sourceforge.plantuml.stats.StatsUtils;
import net.sourceforge.plantuml.swing.MainWindow2;
import net.sourceforge.plantuml.swing.MainWindow;
import net.sourceforge.plantuml.syntax.LanguageDescriptor;
import net.sourceforge.plantuml.utils.Cypher;
import net.sourceforge.plantuml.version.Version;
@ -171,7 +170,7 @@ public class Run {
}
}
try {
new MainWindow2(option, dir);
new MainWindow(option, dir);
} catch (java.awt.HeadlessException e) {
System.err.println("There is an issue with your server. You will find some tips here:");
System.err.println("https://forum.plantuml.net/3399/problem-with-x11-and-headless-exception");

View File

@ -35,38 +35,6 @@
*/
package net.sourceforge.plantuml.swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.prefs.Preferences;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import net.sourceforge.plantuml.GeneratedImage;
import net.sourceforge.plantuml.ImageSelection;
import net.sourceforge.plantuml.graphic.GraphicStrings;
@ -75,15 +43,25 @@ import net.sourceforge.plantuml.security.SFile;
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
import net.sourceforge.plantuml.version.PSystemVersion;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.prefs.Preferences;
import static net.sourceforge.plantuml.ugraphic.ImageBuilder.plainPngBuilder;
class ImageWindow2 extends JFrame {
class ImageWindow extends JFrame {
private final static Preferences prefs = Preferences.userNodeForPackage(ImageWindow2.class);
private final static Preferences prefs = Preferences.userNodeForPackage(ImageWindow.class);
private final static String KEY_ZOOM_FIT = "zoomfit";
private final static String KEY_WIDTH_FIT = "widthfit";
private SimpleLine2 simpleLine2;
private SimpleLine simpleLine;
private final JScrollPane scrollPane;
private final JButton next = new JButton("Next");
private final JButton copy = new JButton("Copy");
@ -92,7 +70,7 @@ class ImageWindow2 extends JFrame {
private final JCheckBox widthFitButt = new JCheckBox("Width fit");
private final JButton zoomMore = new JButton("+");
private final JButton zoomLess = new JButton("-");
private final MainWindow2 main;
private final MainWindow main;
private final ListModel listModel;
private int index;
@ -106,10 +84,10 @@ class ImageWindow2 extends JFrame {
private int startX, startY;
public ImageWindow2(SimpleLine2 simpleLine, final MainWindow2 main, ListModel listModel, int index) {
public ImageWindow(SimpleLine simpleLine, final MainWindow main, ListModel listModel, int index) {
super(simpleLine.toString());
setIconImage(PSystemVersion.getPlantumlSmallIcon2());
this.simpleLine2 = simpleLine;
this.simpleLine = simpleLine;
this.listModel = listModel;
this.index = index;
this.main = main;
@ -180,7 +158,7 @@ class ImageWindow2 extends JFrame {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
main.closing(ImageWindow2.this);
main.closing(ImageWindow.this);
}
});
@ -205,7 +183,16 @@ class ImageWindow2 extends JFrame {
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_RIGHT) {
if (evt.isControlDown() && (evt.getKeyCode() == KeyEvent.VK_PLUS || evt.getKeyCode() == KeyEvent.VK_ADD)) {
zoomFactor++;
refreshImage(false);
} else if (evt.isControlDown() && (evt.getKeyCode() == KeyEvent.VK_MINUS || evt.getKeyCode() == KeyEvent.VK_SUBTRACT)) {
zoomFactor--;
refreshImage(false);
} else if (evt.isControlDown() && (evt.getKeyCode() == KeyEvent.VK_0 || evt.getKeyCode() == KeyEvent.VK_NUMPAD0)) {
zoomFactor = 0;
refreshImage(false);
} else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_RIGHT) {
next();
} else if (evt.isControlDown() && evt.getKeyCode() == KeyEvent.VK_LEFT) {
previous();
@ -229,7 +216,6 @@ class ImageWindow2 extends JFrame {
}
}
});
}
private void next() {
@ -285,22 +271,22 @@ class ImageWindow2 extends JFrame {
if (index > listModel.getSize() - 1) {
index = listModel.getSize() - 1;
}
simpleLine2 = (SimpleLine2) listModel.getElementAt(index);
setTitle(simpleLine2.toString());
simpleLine = (SimpleLine) listModel.getElementAt(index);
setTitle(simpleLine.toString());
refreshImage(false);
}
private void refreshSimpleLine() {
for (SimpleLine2 line : main.getCurrentDirectoryListing2()) {
if (line.getFile().equals(simpleLine2.getFile())) {
simpleLine2 = line;
setTitle(simpleLine2.toString());
for (SimpleLine line : main.getCurrentDirectoryListing2()) {
if (line.getFile().equals(simpleLine.getFile())) {
simpleLine = line;
setTitle(simpleLine.toString());
}
}
}
private ScrollablePicture buildScrollablePicture() {
final GeneratedImage generatedImage = simpleLine2.getGeneratedImage();
final GeneratedImage generatedImage = simpleLine.getGeneratedImage();
if (generatedImage == null) {
return null;
}
@ -333,7 +319,7 @@ class ImageWindow2 extends JFrame {
e.printStackTrace();
}
}
final ImageIcon imageIcon = new ImageIcon(image, simpleLine2.toString());
final ImageIcon imageIcon = new ImageIcon(image, simpleLine.toString());
final ScrollablePicture scrollablePicture = new ScrollablePicture(imageIcon, 1);
scrollablePicture.addMouseListener(new MouseAdapter() {
@ -376,7 +362,7 @@ class ImageWindow2 extends JFrame {
}
private void copy() {
final GeneratedImage generatedImage = simpleLine2.getGeneratedImage();
final GeneratedImage generatedImage = simpleLine.getGeneratedImage();
if (generatedImage == null) {
return;
}
@ -386,8 +372,8 @@ class ImageWindow2 extends JFrame {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
}
public SimpleLine2 getSimpleLine() {
return simpleLine2;
public SimpleLine getSimpleLine() {
return simpleLine;
}
private int v1;
@ -416,7 +402,7 @@ class ImageWindow2 extends JFrame {
}
private boolean isError() {
return simpleLine2.getGeneratedImage() != null && simpleLine2.getGeneratedImage().lineErrorRaw() != -1;
return simpleLine.getGeneratedImage() != null && simpleLine.getGeneratedImage().lineErrorRaw() != -1;
}

View File

@ -35,59 +35,30 @@
*/
package net.sourceforge.plantuml.swing;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.CompoundBorder;
import net.sourceforge.plantuml.DirWatcher2;
import net.sourceforge.plantuml.GeneratedImage;
import net.sourceforge.plantuml.Log;
import net.sourceforge.plantuml.Option;
import net.sourceforge.plantuml.version.PSystemVersion;
public class MainWindow2 extends JFrame {
import javax.swing.Timer;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final private static Preferences prefs = Preferences.userNodeForPackage(MainWindow2.class);
public class MainWindow extends JFrame {
final private static Preferences prefs = Preferences.userNodeForPackage(MainWindow.class);
final private static String KEY_DIR = "cur";
final private static String KEY_PATTERN = "pat";
@ -97,8 +68,8 @@ public class MainWindow2 extends JFrame {
private final JTextField extensions = new JTextField();
private final int period = 300;
final private List<SimpleLine2> currentDirectoryListing2 = new ArrayList<>();
final private Set<ImageWindow2> openWindows2 = new HashSet<>();
final private List<SimpleLine> currentDirectoryListing2 = new ArrayList<>();
final private Set<ImageWindow> openWindows2 = new HashSet<>();
final private Option option;
private DirWatcher2 dirWatcher;
@ -155,7 +126,7 @@ public class MainWindow2 extends JFrame {
return Option.getPattern();
}
public MainWindow2(Option option, File arg) {
public MainWindow(Option option, File arg) {
super(getDirectory(arg).getAbsolutePath());
System.setProperty("PLANTUML_SECURITY_PROFILE", "UNSECURE");
final File dir = getDirectory(arg);
@ -190,7 +161,7 @@ public class MainWindow2 extends JFrame {
try {
if (e.getClickCount() == 2) {
final int index = jList1.locationToIndex(e.getPoint());
doubleClick((SimpleLine2) jList1.getModel().getElementAt(index), jList1.getModel(), index);
doubleClick((SimpleLine) jList1.getModel().getElementAt(index), jList1.getModel(), index);
}
} catch (Exception ex) {
@ -209,7 +180,7 @@ public class MainWindow2 extends JFrame {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
final int index = jList1.getSelectedIndex();
doubleClick((SimpleLine2) jList1.getModel().getElementAt(index), jList1.getModel(), index);
doubleClick((SimpleLine) jList1.getModel().getElementAt(index), jList1.getModel(), index);
}
}
@ -315,8 +286,8 @@ public class MainWindow2 extends JFrame {
jList1.setVisible(true);
}
private void doubleClick(SimpleLine2 simpleLine, ListModel listModel, int index) {
for (ImageWindow2 win : openWindows2) {
private void doubleClick(SimpleLine simpleLine, ListModel listModel, int index) {
for (ImageWindow win : openWindows2) {
if (win.getSimpleLine().equals(simpleLine)) {
win.setVisible(true);
win.setExtendedState(Frame.NORMAL);
@ -324,7 +295,7 @@ public class MainWindow2 extends JFrame {
}
}
if (simpleLine.getGeneratedImage() != null) {
openWindows2.add(new ImageWindow2(simpleLine, this, listModel, index));
openWindows2.add(new ImageWindow(simpleLine, this, listModel, index));
}
}
@ -356,19 +327,19 @@ public class MainWindow2 extends JFrame {
final File file = ent.getKey();
removeAllThatUseThisFile(file);
final Future<List<GeneratedImage>> future = ent.getValue();
final SimpleLine2 simpleLine = SimpleLine2.fromFuture(file, future);
final SimpleLine simpleLine = SimpleLine.fromFuture(file, future);
currentDirectoryListing2.add(simpleLine);
changed = true;
}
for (SimpleLine2 line : new ArrayList<>(currentDirectoryListing2)) {
for (SimpleLine line : new ArrayList<>(currentDirectoryListing2)) {
if (line.pendingAndFinished()) {
currentDirectoryListing2.remove(line);
changed = true;
final Future<List<GeneratedImage>> future = line.getFuture();
for (GeneratedImage im : future.get()) {
mayRefreshImageWindow(im.getPngFile());
final SimpleLine2 simpleLine = SimpleLine2.fromGeneratedImage(line.getFile(), im);
final SimpleLine simpleLine = SimpleLine.fromGeneratedImage(line.getFile(), im);
currentDirectoryListing2.add(simpleLine);
}
}
@ -378,8 +349,8 @@ public class MainWindow2 extends JFrame {
}
private void removeAllThatUseThisFile(File file) {
for (final Iterator<SimpleLine2> it = currentDirectoryListing2.iterator(); it.hasNext();) {
final SimpleLine2 line = it.next();
for (final Iterator<SimpleLine> it = currentDirectoryListing2.iterator(); it.hasNext();) {
final SimpleLine line = it.next();
if (line.getFile().equals(file)) {
it.remove();
}
@ -387,7 +358,7 @@ public class MainWindow2 extends JFrame {
}
private void mayRefreshImageWindow(File pngFile) {
for (ImageWindow2 win : openWindows2) {
for (ImageWindow win : openWindows2) {
if (win.getSimpleLine().getGeneratedImage() == null) {
continue;
}
@ -398,14 +369,14 @@ public class MainWindow2 extends JFrame {
}
public void closing(ImageWindow2 imageWindow) {
public void closing(ImageWindow imageWindow) {
final boolean ok = openWindows2.remove(imageWindow);
if (ok == false) {
throw new IllegalStateException();
}
}
public List<SimpleLine2> getCurrentDirectoryListing2() {
public List<SimpleLine> getCurrentDirectoryListing2() {
return Collections.unmodifiableList(currentDirectoryListing2);
}

View File

@ -41,21 +41,21 @@ import java.util.concurrent.Future;
import net.sourceforge.plantuml.GeneratedImage;
class SimpleLine2 implements Comparable<SimpleLine2> {
class SimpleLine implements Comparable<SimpleLine> {
private final File file;
private final GeneratedImage generatedImage;
private final Future<List<GeneratedImage>> future;
public static SimpleLine2 fromFuture(File file, Future<List<GeneratedImage>> future) {
return new SimpleLine2(file, null, future);
public static SimpleLine fromFuture(File file, Future<List<GeneratedImage>> future) {
return new SimpleLine(file, null, future);
}
public static SimpleLine2 fromGeneratedImage(File file, GeneratedImage generatedImage) {
return new SimpleLine2(file, generatedImage, null);
public static SimpleLine fromGeneratedImage(File file, GeneratedImage generatedImage) {
return new SimpleLine(file, generatedImage, null);
}
private SimpleLine2(File file, GeneratedImage generatedImage, Future<List<GeneratedImage>> future) {
private SimpleLine(File file, GeneratedImage generatedImage, Future<List<GeneratedImage>> future) {
this.generatedImage = generatedImage;
this.file = file;
this.future = future;
@ -84,7 +84,7 @@ class SimpleLine2 implements Comparable<SimpleLine2> {
return future;
}
public int compareTo(SimpleLine2 other) {
public int compareTo(SimpleLine other) {
return toString().compareTo(other.toString());
}