Skip to content

Commit a292880

Browse files
committed
implemented image preview in asset tree.
1 parent 83b9ce1 commit a292880

10 files changed

Lines changed: 216 additions & 62 deletions

File tree

src/main/java/com/ss/editor/manager/JavaFXImageManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ private Image getImagePreview(@NotNull URL url, @Nullable final FileTime lastMod
228228

229229
final byte[] content = Utils.get(url, first -> IOUtils.toByteArray(first.openStream()));
230230
final BufferedImage awtImage;
231-
232231
try {
233232
awtImage = (BufferedImage) TGAReader.getImage(content);
234233
} catch (final Exception e) {
235234
LOGGER.warning(e);
235+
writeDefaultToCache(cacheFile);
236236
return Icons.IMAGE_512;
237237
}
238238

@@ -249,6 +249,15 @@ private Image getImagePreview(@NotNull URL url, @Nullable final FileTime lastMod
249249
return Icons.IMAGE_512;
250250
}
251251

252+
private void writeDefaultToCache(@NotNull final Path cacheFile) {
253+
final BufferedImage bufferedImage = SwingFXUtils.fromFXImage(Icons.IMAGE_512, null);
254+
try (final OutputStream out = Files.newOutputStream(cacheFile)) {
255+
ImageIO.write(bufferedImage, "png", out);
256+
} catch (final IOException ex) {
257+
LOGGER.warning(ex);
258+
}
259+
}
260+
252261
@NotNull
253262
private Image readIOImage(@NotNull final URL url, final int width, final int height, @NotNull final Path cacheFile) {
254263

src/main/java/com/ss/editor/ui/builder/EditorFXSceneBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,6 @@ private static void build(@NotNull final EditorFXScene scene,
140140
FXUtils.bindFixedWidth(barComponent, container.widthProperty());
141141
FXUtils.addClassTo(leftSplitContainer, bottomSplitContainer, CSSClasses.MAIN_SPLIT_PANEL);
142142

143-
UIUtils.overrideTooltipBehavior(100, 5000, 100);
143+
UIUtils.overrideTooltipBehavior(1000, 5000, 100);
144144
}
145145
}

src/main/java/com/ss/editor/ui/component/asset/tree/ResourceTreeCell.java

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static com.ss.editor.manager.FileIconManager.DEFAULT_FILE_ICON_SIZE;
44
import static java.util.Collections.singletonList;
5-
import com.ss.editor.config.EditorConfig;
65
import com.ss.editor.manager.FileIconManager;
76
import com.ss.editor.ui.component.asset.tree.resource.FolderResourceElement;
87
import com.ss.editor.ui.component.asset.tree.resource.LoadingResourceElement;
@@ -23,7 +22,7 @@
2322
import java.util.function.Consumer;
2423

2524
/**
26-
* The implementation of the cell for {@link TreeView} for showing a resource.
25+
* The implementation of the cell for {@link TreeView} to show resource.
2726
*
2827
* @author JavaSaBr
2928
*/
@@ -33,16 +32,16 @@ public class ResourceTreeCell extends TreeCell<ResourceElement> {
3332
private static final FileIconManager ICON_MANAGER = FileIconManager.getInstance();
3433

3534
/**
36-
* The tooltip of this resource.
35+
* The icon.
3736
*/
3837
@NotNull
39-
private final Tooltip tooltip;
38+
private final ImageView icon;
4039

4140
/**
42-
* The icon.
41+
* The tooltip of this resource.
4342
*/
44-
@NotNull
45-
private final ImageView icon;
43+
@Nullable
44+
private Tooltip tooltip;
4645

4746
/**
4847
* Instantiates a new Resource tree cell.
@@ -100,35 +99,28 @@ private void processClick(@NotNull final MouseEvent event) {
10099
final ResourceTree treeView = (ResourceTree) getTreeView();
101100

102101
if (event.getButton() == MouseButton.SECONDARY) {
103-
104102
final ContextMenu contextMenu = treeView.getContextMenu(item);
105103
if (contextMenu == null) return;
106-
107104
contextMenu.show(this, Side.BOTTOM, 0, 0);
108-
109105
} else if (!isFolder && event.getButton() == MouseButton.PRIMARY && event.getClickCount() > 1) {
110-
111106
final Consumer<ResourceElement> openFunction = treeView.getOpenFunction();
112-
113-
if (openFunction != null) {
114-
openFunction.accept(item);
115-
}
107+
if (openFunction != null) openFunction.accept(item);
116108
}
117109
}
118110

119111
@Override
120112
protected void updateItem(@Nullable final ResourceElement item, boolean empty) {
121113
super.updateItem(item, empty);
122114

115+
removeToolTip();
116+
123117
if (item == null) {
124118
setText(StringUtils.EMPTY);
125119
setGraphic(null);
126-
Tooltip.uninstall(this, tooltip);
127120
return;
128121
} else if (item instanceof LoadingResourceElement) {
129122
setText(StringUtils.EMPTY);
130123
setGraphic(new ProgressIndicator());
131-
Tooltip.uninstall(this, tooltip);
132124
return;
133125
}
134126

@@ -139,22 +131,18 @@ protected void updateItem(@Nullable final ResourceElement item, boolean empty) {
139131

140132
setText(fileName.toString());
141133
setGraphic(icon);
134+
createToolTip();
135+
}
142136

143-
final EditorConfig editorConfig = EditorConfig.getInstance();
144-
final Path currentAsset = editorConfig.getCurrentAsset();
145-
146-
if (file.equals(currentAsset)) {
147-
Tooltip.install(this, tooltip);
148-
updateTooltip(file.toString());
149-
} else {
150-
Tooltip.uninstall(this, tooltip);
151-
}
137+
private void removeToolTip() {
138+
if (tooltip == null) return;
139+
Tooltip.uninstall(this, tooltip);
140+
tooltip = null;
152141
}
153142

154-
/**
155-
* Update the tooltip.
156-
*/
157-
private void updateTooltip(@NotNull final String text) {
158-
tooltip.setText(text);
143+
private void createToolTip() {
144+
tooltip = getItem().createToolTip();
145+
if (tooltip == null) return;
146+
Tooltip.install(this, tooltip);
159147
}
160148
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.ss.editor.ui.component.asset.tree.resource;
2+
3+
import com.ss.editor.ui.tooltip.ImagePreview;
4+
import javafx.scene.control.Tooltip;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
import java.nio.file.Path;
9+
10+
/**
11+
* The presentation of an image.
12+
*
13+
* @author JavaSaBr
14+
*/
15+
public class ImageResourceElement extends FileResourceElement {
16+
17+
/**
18+
* Instantiates a new ImageResourceElement.
19+
*
20+
* @param file the file
21+
*/
22+
ImageResourceElement(@NotNull final Path file) {
23+
super(file);
24+
}
25+
26+
@Override
27+
@Nullable
28+
public Tooltip createToolTip() {
29+
return new ImagePreview(getFile());
30+
}
31+
}

src/main/java/com/ss/editor/ui/component/asset/tree/resource/ResourceElement.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.ss.editor.ui.component.asset.tree.resource;
22

3-
import org.jetbrains.annotations.NotNull;
4-
import org.jetbrains.annotations.Nullable;
53
import com.ss.rlib.logging.Logger;
64
import com.ss.rlib.logging.LoggerManager;
75
import com.ss.rlib.util.array.Array;
6+
import javafx.scene.control.Tooltip;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
89

910
import java.nio.file.Path;
1011

@@ -36,6 +37,16 @@ public ResourceElement(@NotNull final Path file) {
3637
this.file = file;
3738
}
3839

40+
/**
41+
* Create tooltip to preview this element.
42+
*
43+
* @return the tooltip.
44+
*/
45+
@Nullable
46+
public Tooltip createToolTip() {
47+
return null;
48+
}
49+
3950
/**
4051
* Gets file.
4152
*
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.ss.editor.ui.component.asset.tree.resource;
22

3+
import com.ss.editor.manager.JavaFXImageManager;
34
import org.jetbrains.annotations.NotNull;
45

56
import java.nio.file.Files;
67
import java.nio.file.Path;
78

89
/**
9-
* Реализация фабрики элементов ресурсов.
10+
* The factory to create resource elements.
1011
*
11-
* @author Ronn
12+
* @author JavaSaBr
1213
*/
1314
public class ResourceElementFactory {
1415

@@ -18,7 +19,14 @@ public class ResourceElementFactory {
1819
* @param file the file
1920
* @return the resource element
2021
*/
22+
@NotNull
2123
public static ResourceElement createFor(@NotNull final Path file) {
22-
return Files.isDirectory(file) ? new FolderResourceElement(file) : new FileResourceElement(file);
24+
if (Files.isDirectory(file)) {
25+
return new FolderResourceElement(file);
26+
} else if (JavaFXImageManager.isImage(file)) {
27+
return new ImageResourceElement(file);
28+
} else {
29+
return new FileResourceElement(file);
30+
}
2331
}
2432
}

src/main/java/com/ss/editor/ui/css/CSSClasses.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ public interface CSSClasses {
482482
*/
483483
String IMAGE_CHANNEL_PREVIEW = "image-channel-preview";
484484

485+
/**
486+
* The constant IMAGE_PREVIEW.
487+
*/
488+
String IMAGE_PREVIEW = "image-preview";
489+
485490
/**
486491
* The constant PLUGIN_LIST_CELL.
487492
*/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.ss.editor.ui.tooltip;
2+
3+
import static com.ss.rlib.util.ObjectUtils.notNull;
4+
import com.ss.editor.annotation.FXThread;
5+
import com.ss.editor.manager.JavaFXImageManager;
6+
import com.ss.editor.ui.css.CSSClasses;
7+
import com.ss.rlib.ui.util.FXUtils;
8+
import javafx.scene.image.ImageView;
9+
import javafx.scene.layout.BorderPane;
10+
import javafx.stage.Window;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
import java.nio.file.Path;
15+
16+
/**
17+
* THe implementation of tooltip for showing image channels.
18+
*
19+
* @author JavaSaBr
20+
*/
21+
public class ImagePreview extends CustomTooltip<BorderPane> {
22+
23+
@NotNull
24+
private static final JavaFXImageManager IMAGE_MANAGER = JavaFXImageManager.getInstance();
25+
26+
/**
27+
* The file to show.
28+
*/
29+
@NotNull
30+
private final Path path;
31+
32+
/**
33+
* The image view.
34+
*/
35+
@Nullable
36+
private ImageView imageView;
37+
38+
public ImagePreview(@NotNull final Path path) {
39+
this.path = path;
40+
}
41+
42+
@Override
43+
protected void createContent(@NotNull final BorderPane root) {
44+
super.createContent(root);
45+
46+
imageView = new ImageView();
47+
imageView.fitHeightProperty().bind(root.widthProperty());
48+
imageView.fitWidthProperty().bind(root.heightProperty());
49+
50+
root.setCenter(imageView);
51+
}
52+
53+
@Override
54+
public void show(final Window owner) {
55+
super.show(owner);
56+
}
57+
58+
@Override
59+
protected void show() {
60+
61+
final ImageView imageView = getImageView();
62+
if (imageView.getImage() == null) {
63+
showImage(path);
64+
}
65+
66+
super.show();
67+
}
68+
69+
@NotNull
70+
@Override
71+
protected BorderPane createRoot() {
72+
final BorderPane pane = new BorderPane();
73+
FXUtils.addClassesTo(pane, CSSClasses.IMAGE_PREVIEW);
74+
return pane;
75+
}
76+
77+
/**
78+
* @return the image view.
79+
*/
80+
@NotNull
81+
@FXThread
82+
private ImageView getImageView() {
83+
return notNull(imageView);
84+
}
85+
86+
/**
87+
* Show the file.
88+
*
89+
* @param file the file
90+
*/
91+
@FXThread
92+
public void showImage(@Nullable final Path file) {
93+
getImageView().setImage(IMAGE_MANAGER.getImagePreview(file, 200, 200));
94+
}
95+
}

0 commit comments

Comments
 (0)