55import com .ss .editor .Editor ;
66import com .ss .editor .annotation .FXThread ;
77import 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 ;
118import com .ss .editor .ui .Icons ;
129import com .ss .editor .ui .css .CSSClasses ;
1310import com .ss .editor .ui .dialog .AbstractSimpleEditorDialog ;
11+ import com .ss .editor .ui .preview .FilePreview ;
12+ import com .ss .editor .ui .preview .FilePreviewFactoryRegistry ;
1413import com .ss .editor .util .EditorUtil ;
1514import com .ss .rlib .ui .util .FXUtils ;
16- import com .ss .rlib .util .FileUtils ;
1715import com .ss .rlib .util .StringUtils ;
18- import com .ss .rlib .util .Utils ;
16+ import com .ss .rlib .util .array . Array ;
1917import javafx .beans .binding .BooleanBinding ;
20- import javafx .beans .property .ObjectProperty ;
2118import javafx .beans .value .ObservableBooleanValue ;
2219import javafx .scene .control .Button ;
2320import javafx .scene .control .Label ;
24- import javafx .scene .control .TextArea ;
2521import javafx .scene .control .TreeItem ;
26- import javafx .scene .image .Image ;
2722import javafx .scene .image .ImageView ;
2823import javafx .scene .layout .HBox ;
2924import javafx .scene .layout .Region ;
3328import org .jetbrains .annotations .Nullable ;
3429
3530import java .awt .*;
36- import java .net .URL ;
3731import java .nio .file .Path ;
3832import java .util .function .Consumer ;
3933import 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
0 commit comments