picture viewer fixes
This commit is contained in:
parent
d223d2a2a1
commit
c99f4b6782
@ -18,6 +18,8 @@ public class FileEditor extends JDialog {
|
||||
private JTextArea textArea;
|
||||
private JScrollPane scrollPane;
|
||||
private File file;
|
||||
private java.util.List<File> imageFiles = new java.util.ArrayList<>();
|
||||
private int currentImageIndex = -1;
|
||||
private AppConfig config;
|
||||
private boolean modified = false;
|
||||
private boolean readOnly;
|
||||
@ -54,6 +56,10 @@ public class FileEditor extends JDialog {
|
||||
this.config = config;
|
||||
this.readOnly = readOnly;
|
||||
|
||||
if (isImageFile(file)) {
|
||||
initImageList();
|
||||
}
|
||||
|
||||
initComponents();
|
||||
loadFile();
|
||||
|
||||
@ -580,6 +586,19 @@ public class FileEditor extends JDialog {
|
||||
if (undoManager.canRedo()) undoManager.redo();
|
||||
}, KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
|
||||
|
||||
// Doprava/Doleva a Mezerník pro listování obrázků
|
||||
rootPane.registerKeyboardAction(e -> {
|
||||
if (isImageFile(file)) nextImage();
|
||||
}, KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
|
||||
|
||||
rootPane.registerKeyboardAction(e -> {
|
||||
if (isImageFile(file)) prevImage();
|
||||
}, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
|
||||
|
||||
rootPane.registerKeyboardAction(e -> {
|
||||
if (isImageFile(file)) nextImage();
|
||||
}, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
|
||||
|
||||
// Ctrl+Shift+Z - Redo (alternative shortcut)
|
||||
rootPane.registerKeyboardAction(e -> {
|
||||
if (undoManager.canRedo()) undoManager.redo();
|
||||
@ -867,16 +886,45 @@ public class FileEditor extends JDialog {
|
||||
try {
|
||||
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
|
||||
if (icon.getImageLoadStatus() == MediaTracker.COMPLETE) {
|
||||
// Get image dimensions
|
||||
int imgW = icon.getIconWidth();
|
||||
int imgH = icon.getIconHeight();
|
||||
|
||||
// Get screen size constraints
|
||||
GraphicsConfiguration gc = getGraphicsConfiguration();
|
||||
Rectangle screenBounds = gc.getBounds();
|
||||
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
|
||||
int maxW = screenBounds.width - screenInsets.left - screenInsets.right - 40;
|
||||
int maxH = screenBounds.height - screenInsets.top - screenInsets.bottom - 100;
|
||||
|
||||
// Scale image if it's too large
|
||||
Image scaledImage = icon.getImage();
|
||||
boolean scaled = false;
|
||||
if (imgW > maxW || imgH > maxH) {
|
||||
double ratio = Math.min((double) maxW / imgW, (double) maxH / imgH);
|
||||
imgW = (int) (imgW * ratio);
|
||||
imgH = (int) (imgH * ratio);
|
||||
scaledImage = scaledImage.getScaledInstance(imgW, imgH, Image.SCALE_SMOOTH);
|
||||
icon = new ImageIcon(scaledImage);
|
||||
scaled = true;
|
||||
}
|
||||
|
||||
JLabel label = new JLabel(icon);
|
||||
label.setHorizontalAlignment(JLabel.CENTER);
|
||||
scrollPane.setViewportView(label);
|
||||
|
||||
statusPosLabel.setText(String.format("Obraz: %d x %d px",
|
||||
icon.getIconWidth(), icon.getIconHeight()));
|
||||
statusSelLabel.setText(String.format("Velikost: %s", formatSize(file.length())));
|
||||
String statusText = String.format("Obraz: %d x %d px", imgW, imgH);
|
||||
if (scaled) statusText += " (zmenšeno)";
|
||||
statusPosLabel.setText(statusText);
|
||||
|
||||
// Adjust window size based on image size
|
||||
adjustWindowSizeForImage(icon.getIconWidth(), icon.getIconHeight());
|
||||
String labelText = String.format("Velikost: %s", formatSize(file.length()));
|
||||
if (imageFiles.size() > 1) {
|
||||
labelText += String.format(" [%d / %d]", currentImageIndex + 1, imageFiles.size());
|
||||
}
|
||||
statusSelLabel.setText(labelText);
|
||||
|
||||
// Adjust window size based on (possibly scaled) image size
|
||||
adjustWindowSizeForImage(imgW, imgH);
|
||||
|
||||
// Disable search and hex for images
|
||||
if (northPanel != null) {
|
||||
@ -929,6 +977,46 @@ public class FileEditor extends JDialog {
|
||||
return String.format("%.1f %sB", size / Math.pow(1024, exp), pre);
|
||||
}
|
||||
|
||||
private void initImageList() {
|
||||
File parent = file.getParentFile();
|
||||
if (parent != null && parent.isDirectory()) {
|
||||
File[] files = parent.listFiles();
|
||||
if (files != null) {
|
||||
java.util.Arrays.sort(files);
|
||||
for (File f : files) {
|
||||
if (f.isFile() && isImageFile(f)) {
|
||||
imageFiles.add(f);
|
||||
if (f.equals(file)) {
|
||||
currentImageIndex = imageFiles.size() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void nextImage() {
|
||||
if (imageFiles.size() > 1) {
|
||||
currentImageIndex = (currentImageIndex + 1) % imageFiles.size();
|
||||
file = imageFiles.get(currentImageIndex);
|
||||
updateTitle();
|
||||
loadImage();
|
||||
}
|
||||
}
|
||||
|
||||
private void prevImage() {
|
||||
if (imageFiles.size() > 1) {
|
||||
currentImageIndex = (currentImageIndex - 1 + imageFiles.size()) % imageFiles.size();
|
||||
file = imageFiles.get(currentImageIndex);
|
||||
updateTitle();
|
||||
loadImage();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTitle() {
|
||||
setTitle((readOnly ? "Prohlížeč - " : "Editor - ") + file.getName() + (modified ? " *" : ""));
|
||||
}
|
||||
|
||||
private void saveFile() {
|
||||
try {
|
||||
String content = textArea.getText();
|
||||
@ -969,14 +1057,6 @@ public class FileEditor extends JDialog {
|
||||
updateTitle();
|
||||
}
|
||||
|
||||
private void updateTitle() {
|
||||
String title = (readOnly ? "Prohlížeč - " : "Editor - ") + file.getName();
|
||||
if (!readOnly && modified) {
|
||||
title += " *";
|
||||
}
|
||||
setTitle(title);
|
||||
}
|
||||
|
||||
private void changeFont() {
|
||||
Font newFont = FontChooserDialog.showDialog(this, textArea.getFont());
|
||||
if (newFont != null) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user