Skip to content

Commit b11d241

Browse files
committed
implemented file preview system.
1 parent d60b542 commit b11d241

File tree

9 files changed

+597
-133
lines changed

9 files changed

+597
-133
lines changed

src/main/java/com/ss/editor/ui/dialog/asset/BaseAssetEditorDialog.java

Lines changed: 43 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@
55
import com.ss.editor.Editor;
66
import com.ss.editor.annotation.FXThread;
77
import com.ss.editor.annotation.FromAnyThread;
8-
import com.ss.editor.manager.JMEFilePreviewManager;
9-
import com.ss.editor.manager.JavaFXImageManager;
10-
import com.ss.editor.manager.ResourceManager;
118
import com.ss.editor.ui.Icons;
129
import com.ss.editor.ui.css.CSSClasses;
1310
import com.ss.editor.ui.dialog.AbstractSimpleEditorDialog;
11+
import com.ss.editor.ui.preview.FilePreview;
12+
import com.ss.editor.ui.preview.FilePreviewFactoryRegistry;
1413
import com.ss.editor.util.EditorUtil;
1514
import com.ss.rlib.ui.util.FXUtils;
16-
import com.ss.rlib.util.FileUtils;
1715
import com.ss.rlib.util.StringUtils;
18-
import com.ss.rlib.util.Utils;
16+
import com.ss.rlib.util.array.Array;
1917
import javafx.beans.binding.BooleanBinding;
20-
import javafx.beans.property.ObjectProperty;
2118
import javafx.beans.value.ObservableBooleanValue;
2219
import javafx.scene.control.Button;
2320
import javafx.scene.control.Label;
24-
import javafx.scene.control.TextArea;
2521
import javafx.scene.control.TreeItem;
26-
import javafx.scene.image.Image;
2722
import javafx.scene.image.ImageView;
2823
import javafx.scene.layout.HBox;
2924
import javafx.scene.layout.Region;
@@ -33,7 +28,6 @@
3328
import org.jetbrains.annotations.Nullable;
3429

3530
import java.awt.*;
36-
import java.net.URL;
3731
import java.nio.file.Path;
3832
import java.util.function.Consumer;
3933
import java.util.function.Function;
@@ -52,17 +46,18 @@ public class BaseAssetEditorDialog<T, C> extends AbstractSimpleEditorDialog {
5246
protected static final Point DIALOG_SIZE = new Point(-1, -1);
5347

5448
/**
55-
* The image manager.
49+
* The registry of available file previews.
5650
*/
5751
@NotNull
58-
protected static final JavaFXImageManager JAVA_FX_IMAGE_MANAGER = JavaFXImageManager.getInstance();
52+
protected static final FilePreviewFactoryRegistry FILE_PREVIEW_FACTORY_REGISTRY = FilePreviewFactoryRegistry.getInstance();
5953

6054
/**
6155
* The editor.
6256
*/
6357
@NotNull
6458
protected static final Editor EDITOR = Editor.getInstance();
6559

60+
6661
/**
6762
* The function to handle the choose.
6863
*/
@@ -76,16 +71,10 @@ public class BaseAssetEditorDialog<T, C> extends AbstractSimpleEditorDialog {
7671
private final Function<@NotNull C, @Nullable String> validator;
7772

7873
/**
79-
* The image preview.
74+
* The list of available previews.
8075
*/
8176
@Nullable
82-
private ImageView imageView;
83-
84-
/**
85-
* The preview of text files.
86-
*/
87-
@Nullable
88-
private TextArea textView;
77+
protected Array<FilePreview> previews;
8978

9079
/**
9180
* The label with any warning.
@@ -215,44 +204,16 @@ protected void processSelected(@Nullable final TreeItem<T> newValue) {
215204
@FXThread
216205
private void updatePreview(@NotNull final Path file) {
217206

218-
final ImageView imageView = getImageView();
219-
imageView.setVisible(false);
220-
221-
final TextArea textView = getTextView();
222-
textView.setVisible(false);
223-
224-
final int width = (int) imageView.getFitWidth();
225-
final int height = (int) imageView.getFitHeight();
226-
227-
if (JMEFilePreviewManager.isJmeFile(file)) {
228-
229-
final JMEFilePreviewManager previewManager = JMEFilePreviewManager.getInstance();
230-
previewManager.show(file, width, height);
231-
232-
final ImageView sourceView = previewManager.getImageView();
233-
final ObjectProperty<Image> imageProperty = imageView.imageProperty();
234-
imageProperty.bind(sourceView.imageProperty());
235-
236-
imageView.setVisible(true);
237-
238-
} else if (JavaFXImageManager.isImage(file)) {
239-
240-
final Image preview = JAVA_FX_IMAGE_MANAGER.getImagePreview(file, width, height);
241-
imageView.setImage(preview);
242-
imageView.setVisible(true);
243-
244-
} else if (!StringUtils.isEmpty(FileUtils.getExtension(file))) {
207+
final Array<FilePreview> previews = getPreviews();
208+
final FilePreview preview = previews.search(file, FilePreview::isSupport);
209+
previews.forEach(preview, (filePreview, toCheck) -> filePreview != toCheck,
210+
(filePreview, tooCheck) -> filePreview.hide());
245211

246-
imageView.imageProperty().unbind();
247-
imageView.setImage(null);
248-
249-
textView.setText(FileUtils.read(file));
250-
textView.setVisible(true);
251-
252-
} else {
253-
imageView.imageProperty().unbind();
254-
imageView.setImage(null);
212+
if (preview == null) {
213+
return;
255214
}
215+
216+
preview.show(file);
256217
}
257218

258219
/**
@@ -263,56 +224,22 @@ private void updatePreview(@NotNull final Path file) {
263224
@FXThread
264225
private void updatePreview(@Nullable final String assetPath) {
265226

266-
final ImageView imageView = getImageView();
267-
imageView.setVisible(false);
268-
269-
final TextArea textView = getTextView();
270-
textView.setVisible(false);
271-
272-
final int width = (int) imageView.getFitWidth();
273-
final int height = (int) imageView.getFitHeight();
274-
275-
if (JMEFilePreviewManager.isJmeFile(assetPath)) {
276-
277-
final JMEFilePreviewManager previewManager = JMEFilePreviewManager.getInstance();
278-
previewManager.show(assetPath, width, height);
279-
280-
final ImageView sourceView = previewManager.getImageView();
281-
final ObjectProperty<Image> imageProperty = imageView.imageProperty();
282-
imageProperty.bind(sourceView.imageProperty());
283-
284-
imageView.setVisible(true);
285-
286-
} else if (JavaFXImageManager.isImage(assetPath)) {
287-
288-
final Image preview = JAVA_FX_IMAGE_MANAGER.getImagePreview(assetPath, width, height);
289-
imageView.setImage(preview);
290-
imageView.setVisible(true);
291-
292-
} else if (assetPath != null && !StringUtils.isEmpty(FileUtils.getExtension(assetPath))) {
293-
294-
final ResourceManager resourceManager = ResourceManager.getInstance();
295-
final URL url = resourceManager.tryToFindResource(assetPath);
227+
final Array<FilePreview> previews = getPreviews();
296228

297-
String content;
298-
299-
if (url != null) {
300-
content = Utils.get(url, first -> FileUtils.read(first.openStream()));
301-
} else {
302-
final Path realFile = EditorUtil.getRealFile(assetPath);
303-
content = realFile == null ? "" : FileUtils.read(realFile);
304-
}
305-
306-
imageView.imageProperty().unbind();
307-
imageView.setImage(null);
229+
if (assetPath == null) {
230+
previews.forEach(FilePreview::hide);
231+
return;
232+
}
308233

309-
textView.setText(content);
310-
textView.setVisible(true);
234+
final FilePreview preview = previews.search(assetPath, FilePreview::isSupport);
235+
previews.forEach(preview, (filePreview, toCheck) -> filePreview != toCheck,
236+
(filePreview, tooCheck) -> filePreview.hide());
311237

312-
} else {
313-
imageView.imageProperty().unbind();
314-
imageView.setImage(null);
238+
if (preview == null) {
239+
return;
315240
}
241+
242+
preview.show(assetPath);
316243
}
317244

318245
/**
@@ -350,38 +277,14 @@ protected void validate(@NotNull final Label warningLabel, @Nullable final T ele
350277

351278
final StackPane previewContainer = new StackPane();
352279

353-
imageView = new ImageView();
354-
imageView.fitHeightProperty().bind(previewContainer.heightProperty().subtract(2));
355-
imageView.fitWidthProperty().bind(previewContainer.widthProperty().subtract(2));
356-
357-
textView = new TextArea();
358-
textView.setEditable(false);
359-
textView.prefWidthProperty().bind(previewContainer.widthProperty().subtract(2));
360-
textView.prefHeightProperty().bind(previewContainer.heightProperty().subtract(2));
361-
362-
FXUtils.addToPane(imageView, previewContainer);
363-
FXUtils.addToPane(textView, previewContainer);
280+
final Array<FilePreview> availablePreviews = FILE_PREVIEW_FACTORY_REGISTRY.createAvailablePreviews();
281+
availablePreviews.forEach(previewContainer, FilePreview::initialize);
364282

365283
FXUtils.addClassTo(previewContainer, CSSClasses.ASSET_EDITOR_DIALOG_PREVIEW_CONTAINER);
366-
FXUtils.addClassTo(textView, CSSClasses.TRANSPARENT_TEXT_AREA);
367284

368-
return previewContainer;
369-
}
370-
371-
/**
372-
* @return the image preview.
373-
*/
374-
@FXThread
375-
protected @NotNull ImageView getImageView() {
376-
return notNull(imageView);
377-
}
285+
this.previews = availablePreviews;
378286

379-
/**
380-
* @return the text preview.
381-
*/
382-
@FXThread
383-
protected @NotNull TextArea getTextView() {
384-
return notNull(textView);
287+
return previewContainer;
385288
}
386289

387290
/**
@@ -402,13 +305,20 @@ protected void validate(@NotNull final Label warningLabel, @Nullable final T ele
402305
return notNull(warningLabel);
403306
}
404307

308+
/**
309+
* Get the list of available file previews.
310+
*
311+
* @return the list of available file previews.
312+
*/
313+
@FXThread
314+
protected @NotNull Array<FilePreview> getPreviews() {
315+
return notNull(previews);
316+
}
317+
405318
@Override
406319
@FXThread
407320
public void hide() {
408-
409-
final JMEFilePreviewManager previewManager = JMEFilePreviewManager.getInstance();
410-
previewManager.clear();
411-
321+
getPreviews().forEach(FilePreview::release);
412322
super.hide();
413323
}
414324

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.ss.editor.ui.preview;
2+
3+
import com.ss.editor.annotation.FXThread;
4+
import com.ss.rlib.util.FileUtils;
5+
import com.ss.rlib.util.StringUtils;
6+
import com.ss.rlib.util.pools.Reusable;
7+
import javafx.scene.layout.StackPane;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
import java.nio.file.Path;
11+
12+
/**
13+
* The interface to implement a file preview.
14+
*
15+
* @author JavaSaBr
16+
*/
17+
public interface FilePreview extends Reusable {
18+
19+
/**
20+
* Initialize this preview to work in the pane.
21+
*
22+
* @param pane the pane.
23+
*/
24+
@FXThread
25+
void initialize(@NotNull StackPane pane);
26+
27+
/**
28+
* Hide this preview.
29+
*/
30+
@FXThread
31+
void hide();
32+
33+
/**
34+
* Check of supporting of the resource.
35+
*
36+
* @param resource the resource.
37+
* @return true if this preview supports the resource.
38+
*/
39+
@FXThread
40+
default boolean isSupport(@NotNull String resource) {
41+
return !StringUtils.isEmpty(FileUtils.getExtension(resource));
42+
}
43+
44+
/**
45+
* Check of supporting of the file.
46+
*
47+
* @param file the file.
48+
* @return true if this preview supports the file.
49+
*/
50+
@FXThread
51+
default boolean isSupport(@NotNull Path file) {
52+
return !StringUtils.isEmpty(FileUtils.getExtension(file));
53+
}
54+
55+
/**
56+
* Show the preview of the resource.
57+
*
58+
* @param resource the resource.
59+
*/
60+
@FXThread
61+
void show(@NotNull String resource);
62+
63+
/**
64+
* Show the preview of the file.
65+
*
66+
* @param file the file.
67+
*/
68+
@FXThread
69+
void show(@NotNull Path file);
70+
71+
/**
72+
* Get the order of this preview component.
73+
*
74+
* @return the order.
75+
*/
76+
@FXThread
77+
default int getOrder() {
78+
return 0;
79+
}
80+
81+
@Override
82+
@FXThread
83+
default void release() {
84+
}
85+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ss.editor.ui.preview;
2+
3+
import com.ss.editor.annotation.FXThread;
4+
import com.ss.rlib.util.array.Array;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* The factory of file previews.
9+
*
10+
* @author JavaSaBr
11+
*/
12+
public interface FilePreviewFactory {
13+
14+
/**
15+
* Add to the result list new file previews implementations.
16+
*
17+
* @param result the list to store new implementations.
18+
*/
19+
@FXThread
20+
void createFilePreviews(@NotNull Array<FilePreview> result);
21+
}

0 commit comments

Comments
 (0)