Skip to content

Commit ab2b462

Browse files
committed
finished working on import action.
1 parent 897b61e commit ab2b462

File tree

11 files changed

+162
-348
lines changed

11 files changed

+162
-348
lines changed

src/main/java/com/ss/editor/Messages.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class Messages {
4848
public static final String ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_OPEN_FILE_BY_EXTERNAL_EDITOR;
4949
public static final String ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_OPEN_FILE_BY_SYSTEM_EXPLORER;
5050
public static final String ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_RENAME_FILE;
51+
public static final String ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_IMPORT_MODEL;
5152

5253
public static final String FILE_EDITOR_ACTION_SAVE;
5354

@@ -532,6 +533,8 @@ public class Messages {
532533
public static final String SIMPLE_DIALOG_BUTTON_ADD;
533534
public static final String SIMPLE_DIALOG_BUTTON_SAVE;
534535
public static final String SIMPLE_DIALOG_BUTTON_GENERATE;
536+
public static final String SIMPLE_DIALOG_BUTTON_CONVERT;
537+
public static final String SIMPLE_DIALOG_BUTTON_IMPORT;
535538
public static final String SIMPLE_DIALOG_BUTTON_CREATE;
536539
public static final String SIMPLE_DIALOG_BUTTON_APPLY;
537540
public static final String SIMPLE_DIALOG_BUTTON_CANCEL;
@@ -622,6 +625,7 @@ public class Messages {
622625
public static final String CREATE_PARTICLE_EMITTER_QUAD_SHAPE_DIALOG_TITLE;
623626
public static final String CREATE_PARTICLE_EMITTER_TORUS_SHAPE_DIALOG_TITLE;
624627
public static final String CREATE_PARTICLE_EMITTER_TRIANGLE_SHAPE_DIALOG_TITLE;
628+
625629
public static final String EDITING_COMPONENT_BRUSH_SIZE;
626630
public static final String EDITING_COMPONENT_BRUSH_POWER;
627631
public static final String EDITING_COMPONENT_SMOOTHLY;
@@ -643,7 +647,11 @@ public class Messages {
643647
public static final String MODEL_CONVERTER_DIALOG_EXPORT_MATERIALS;
644648
public static final String MODEL_CONVERTER_DIALOG_MATERIAL_FOLDER;
645649
public static final String MODEL_CONVERTER_DIALOG_OVERWRITE_MATERIALS;
646-
public static final String MODEL_CONVERTER_DIALOG_BUTTON_OK;
650+
651+
public static final String IMPORT_MODEL_DIALOG_TITLE;
652+
public static final String IMPORT_MODEL_DIALOG_EXTERNAL_FILE;
653+
public static final String IMPORT_MODEL_DIALOG_TEXTURES_FOLDER;
654+
public static final String IMPORT_MODEL_DIALOG_OVERWRITE_TEXTURES;
647655

648656
public static final String FILE_DELETE_HANDLER_DELETE_MATERIALS;
649657

@@ -716,6 +724,7 @@ public class Messages {
716724
ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_OPEN_FILE_BY_EXTERNAL_EDITOR = bundle.getString("AssetComponentResourceTreeContextMenuOpenFileByExternalEditor");
717725
ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_OPEN_FILE_BY_SYSTEM_EXPLORER = bundle.getString("AssetComponentResourceTreeContextMenuOpenFileBySystemExplorer");
718726
ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_RENAME_FILE = bundle.getString("AssetComponentResourceTreeContextMenuRenameFile");
727+
ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_IMPORT_MODEL = bundle.getString("AssetComponentResourceTreeContextMenuImportModel");
719728

720729
FILE_EDITOR_ACTION_SAVE = bundle.getString("FileEditorActionSave");
721730

@@ -1204,6 +1213,8 @@ public class Messages {
12041213
SIMPLE_DIALOG_BUTTON_CLOSE = bundle.getString("SimpleDialogButtonClose");
12051214
SIMPLE_DIALOG_BUTTON_YES = bundle.getString("SimpleDialogButtonYes");
12061215
SIMPLE_DIALOG_BUTTON_NO = bundle.getString("SimpleDialogButtonNo");
1216+
SIMPLE_DIALOG_BUTTON_CONVERT = bundle.getString("SimpleDialogConvert");
1217+
SIMPLE_DIALOG_BUTTON_IMPORT = bundle.getString("SimpleDialogImport");
12071218

12081219
EMPTY_MODEL_CREATOR_DESCRIPTION = bundle.getString("EmptyModelCreatorDescription");
12091220
EMPTY_MODEL_CREATOR_TITLE = bundle.getString("EmptyModelCreatorTitle");
@@ -1310,7 +1321,11 @@ public class Messages {
13101321
MODEL_CONVERTER_DIALOG_EXPORT_MATERIALS = bundle.getString("ModelConverterDialogExportMaterials");
13111322
MODEL_CONVERTER_DIALOG_MATERIAL_FOLDER = bundle.getString("ModelConverterDialogMaterialsFolder");
13121323
MODEL_CONVERTER_DIALOG_OVERWRITE_MATERIALS = bundle.getString("ModelConverterDialogOverwriteMaterials");
1313-
MODEL_CONVERTER_DIALOG_BUTTON_OK = bundle.getString("ModelConverterDialogButtonOk");
1324+
1325+
IMPORT_MODEL_DIALOG_TITLE = bundle.getString("ImportModelDialogTitle");
1326+
IMPORT_MODEL_DIALOG_EXTERNAL_FILE = bundle.getString("ImportModelDialogExternalFile");
1327+
IMPORT_MODEL_DIALOG_TEXTURES_FOLDER = bundle.getString("ImportModelDialogTexturesFolder");
1328+
IMPORT_MODEL_DIALOG_OVERWRITE_TEXTURES = bundle.getString("ImportModelDialogOverwriteTextures");
13141329

13151330
FILE_DELETE_HANDLER_DELETE_MATERIALS = bundle.getString("FileDeleteHandlerDeleteMaterials");
13161331

src/main/java/com/ss/editor/file/converter/impl/AbstractModelFileConverter.java

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.editor.file.converter.impl;
22

3+
import static com.ss.editor.extension.property.EditablePropertyType.*;
34
import static com.ss.editor.util.EditorUtil.getAssetFile;
45
import static com.ss.editor.util.EditorUtil.toAssetPath;
56
import static com.ss.rlib.util.FileUtils.containsExtensions;
@@ -13,20 +14,25 @@
1314
import com.jme3.scene.Geometry;
1415
import com.jme3.scene.Spatial;
1516
import com.ss.editor.FileExtensions;
17+
import com.ss.editor.Messages;
1618
import com.ss.editor.annotation.BackgroundThread;
1719
import com.ss.editor.annotation.FXThread;
1820
import com.ss.editor.model.tool.TangentGenerator;
21+
import com.ss.editor.plugin.api.dialog.GenericFactoryDialog;
22+
import com.ss.editor.plugin.api.property.PropertyDefinition;
1923
import com.ss.editor.serializer.MaterialSerializer;
20-
import com.ss.editor.ui.dialog.converter.ModelConverterDialog;
2124
import com.ss.editor.util.EditorUtil;
2225
import com.ss.editor.util.NodeUtils;
26+
import com.ss.rlib.util.FileUtils;
2327
import com.ss.rlib.util.StringUtils;
28+
import com.ss.rlib.util.VarTable;
2429
import com.ss.rlib.util.array.Array;
2530
import com.ss.rlib.util.array.ArrayFactory;
2631
import com.ss.rlib.util.dictionary.DictionaryFactory;
2732
import com.ss.rlib.util.dictionary.ObjectDictionary;
2833
import org.jetbrains.annotations.NotNull;
2934

35+
import java.awt.*;
3036
import java.io.IOException;
3137
import java.io.OutputStream;
3238
import java.io.PrintWriter;
@@ -40,6 +46,18 @@
4046
*/
4147
public abstract class AbstractModelFileConverter extends AbstractFileConverter {
4248

49+
@NotNull
50+
private static final Point DIALOG_SIZE = new Point(500, -1);
51+
52+
private static final String PROP_RESULT_NAME = "resultName";
53+
private static final String PROP_DESTINATION = "destination";
54+
private static final String PROP_EXPORT_MATERIALS = "exportMaterials";
55+
private static final String PROP_MATERIALS_FOLDER = "materialsFolder";
56+
private static final String PROP_OVERWRITE_MATERIALS = "overwriteMaterials";
57+
58+
@NotNull
59+
private static final Array<String> MATERIAL_DEPS = ArrayFactory.asArray(PROP_EXPORT_MATERIALS);
60+
4361
@Override
4462
public void convert(@NotNull final Path source, @NotNull final Path destination) {
4563

@@ -52,19 +70,57 @@ public void convert(@NotNull final Path source, @NotNull final Path destination)
5270
throw new IllegalArgumentException("incorrect extension of file " + source);
5371
}
5472

55-
final ModelConverterDialog dialog = new ModelConverterDialog(source, destination, settings -> convert(source, settings));
73+
final String resultName = FileUtils.getNameWithoutExtension(source);
74+
final Path assetDestination = EditorUtil.getAssetFile(destination);
75+
76+
final Array<PropertyDefinition> definitions = ArrayFactory.newArray(PropertyDefinition.class);
77+
definitions.add(new PropertyDefinition(STRING, Messages.MODEL_CONVERTER_DIALOG_RESULT_NAME, PROP_RESULT_NAME, resultName));
78+
definitions.add(new PropertyDefinition(FOLDER_FROM_ASSET_FOLDER, Messages.MODEL_CONVERTER_DIALOG_DESTINATION_FOLDER, PROP_DESTINATION, assetDestination));
79+
definitions.add(new PropertyDefinition(BOOLEAN, Messages.MODEL_CONVERTER_DIALOG_EXPORT_MATERIALS, PROP_EXPORT_MATERIALS, false));
80+
definitions.add(new PropertyDefinition(FOLDER_FROM_ASSET_FOLDER, MATERIAL_DEPS, Messages.MODEL_CONVERTER_DIALOG_MATERIAL_FOLDER, PROP_MATERIALS_FOLDER, null));
81+
definitions.add(new PropertyDefinition(BOOLEAN, MATERIAL_DEPS, Messages.MODEL_CONVERTER_DIALOG_OVERWRITE_MATERIALS, PROP_OVERWRITE_MATERIALS, false));
82+
83+
final GenericFactoryDialog dialog = new GenericFactoryDialog(definitions, vars -> convert(source, vars), this::validate);
84+
dialog.setButtonOkText(Messages.SIMPLE_DIALOG_BUTTON_CONVERT);
85+
dialog.setTitle(Messages.MODEL_CONVERTER_DIALOG_TITLE);
86+
dialog.configureSize(DIALOG_SIZE);
5687
dialog.show();
5788
}
5889

90+
/**
91+
* Validate the settings.
92+
*
93+
* @param vars the variables.
94+
* @return true if all are ok.
95+
*/
96+
@FXThread
97+
private boolean validate(@NotNull final VarTable vars) {
98+
99+
if (!vars.has(PROP_RESULT_NAME) || !vars.has(PROP_DESTINATION)) {
100+
return false;
101+
}
102+
103+
final String resultName = vars.getString(PROP_RESULT_NAME);
104+
if (StringUtils.isEmpty(resultName)) return false;
105+
106+
final boolean exportMaterials = vars.getBoolean(PROP_EXPORT_MATERIALS);
107+
108+
if (exportMaterials && !vars.has(PROP_MATERIALS_FOLDER)) {
109+
return false;
110+
}
111+
112+
return true;
113+
}
114+
59115
/**
60116
* Convert a file using settings from the dialog.
61117
*/
62118
@FXThread
63-
private void convert(@NotNull final Path source, @NotNull final ModelConverterDialog dialog) {
119+
private void convert(@NotNull final Path source, @NotNull final VarTable vars) {
64120
EditorUtil.incrementLoading();
65121
EXECUTOR_MANAGER.addBackgroundTask(() -> {
66122
try {
67-
convertImpl(source, dialog);
123+
convertImpl(source, vars);
68124
} catch (final Exception e) {
69125
EditorUtil.handleException(LOGGER, this, e);
70126
EXECUTOR_MANAGER.addFXTask(EditorUtil::decrementLoading);
@@ -76,10 +132,10 @@ private void convert(@NotNull final Path source, @NotNull final ModelConverterDi
76132
* Convert a file using settings from the dialog.
77133
*/
78134
@BackgroundThread
79-
private void convertImpl(@NotNull final Path source, @NotNull final ModelConverterDialog dialog) throws IOException {
135+
private void convertImpl(@NotNull final Path source, @NotNull final VarTable vars) throws IOException {
80136

81-
final String filename = dialog.getFilename();
82-
final Path destinationFolder = dialog.getDestinationFolder();
137+
final String filename = vars.getString(PROP_RESULT_NAME);
138+
final Path destinationFolder = vars.get(PROP_DESTINATION);
83139
final Path destination = destinationFolder.resolve(filename + "." + FileExtensions.JME_OBJECT);
84140
final boolean isOverwrite = Files.exists(destination);
85141

@@ -93,13 +149,13 @@ private void convertImpl(@NotNull final Path source, @NotNull final ModelConvert
93149
TangentGenerator.useMikktspaceGenerator(model);
94150
}
95151

96-
if (dialog.isExportMaterials()) {
152+
if (vars.getBoolean(PROP_EXPORT_MATERIALS)) {
97153

98154
final Array<Geometry> geometries = ArrayFactory.newArray(Geometry.class);
99155
final ObjectDictionary<String, Geometry> mapping = DictionaryFactory.newObjectDictionary();
100156

101-
final Path materialsFolder = dialog.getMaterialsFolder();
102-
final boolean canOverwrite = dialog.isOverwriteMaterials();
157+
final Path materialsFolder = vars.get(PROP_MATERIALS_FOLDER);
158+
final boolean canOverwrite = vars.getBoolean(PROP_OVERWRITE_MATERIALS);
103159

104160
NodeUtils.visitGeometry(model, geometry -> checkAndAdd(geometries, geometry));
105161
geometries.forEach(geometry -> generateNames(mapping, geometry));

src/main/java/com/ss/editor/plugin/api/dialog/GenericFactoryDialog.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.ss.editor.annotation.FXThread;
77
import com.ss.editor.annotation.FromAnyThread;
88
import com.ss.editor.plugin.api.property.PropertyDefinition;
9-
import com.ss.editor.ui.dialog.AbstractSimpleEditorDialog;
109
import com.ss.editor.plugin.api.property.control.PropertyEditorControl;
10+
import com.ss.editor.ui.dialog.AbstractSimpleEditorDialog;
1111
import com.ss.rlib.util.VarTable;
1212
import com.ss.rlib.util.array.Array;
1313
import javafx.collections.ObservableList;
@@ -40,13 +40,13 @@ public class GenericFactoryDialog extends AbstractSimpleEditorDialog {
4040
* The list of all definitions.
4141
*/
4242
@NotNull
43-
private final Array<PropertyDefinition> definitions;
43+
private final Array<@NotNull PropertyDefinition> definitions;
4444

4545
/**
4646
* The handler to handle result properties.
4747
*/
4848
@NotNull
49-
private final Consumer<VarTable> handler;
49+
private final Consumer<@NotNull VarTable> handler;
5050

5151
/**
5252
* THe callback to call re-validating.
@@ -58,22 +58,22 @@ public class GenericFactoryDialog extends AbstractSimpleEditorDialog {
5858
* The validator of all properties.
5959
*/
6060
@NotNull
61-
private Predicate<VarTable> validator;
61+
private Predicate<@NotNull VarTable> validator;
6262

6363
/**
6464
* The root content container.
6565
*/
6666
@Nullable
6767
private VBox root;
6868

69-
public GenericFactoryDialog(@NotNull final Array<PropertyDefinition> definitions,
70-
@NotNull final Consumer<VarTable> handler) {
69+
public GenericFactoryDialog(@NotNull final Array<@NotNull PropertyDefinition> definitions,
70+
@NotNull final Consumer<@NotNull VarTable> handler) {
7171
this(definitions, handler, varTable -> true);
7272
}
7373

74-
public GenericFactoryDialog(@NotNull final Array<PropertyDefinition> definitions,
75-
@NotNull final Consumer<VarTable> handler,
76-
@NotNull final Predicate<VarTable> validator) {
74+
public GenericFactoryDialog(@NotNull final Array<@NotNull PropertyDefinition> definitions,
75+
@NotNull final Consumer<@NotNull VarTable> handler,
76+
@NotNull final Predicate<@NotNull VarTable> validator) {
7777
this.definitions = definitions;
7878
this.handler = handler;
7979
this.validator = validator;
@@ -100,7 +100,7 @@ public void setTitle(@NotNull final String title) {
100100
*/
101101
@FXThread
102102
public void setButtonOkText(@NotNull final String text) {
103-
getOkButton().setText(text);
103+
notNull(getOkButton()).setText(text);
104104
}
105105

106106
/**
@@ -110,7 +110,7 @@ public void setButtonOkText(@NotNull final String text) {
110110
*/
111111
@FXThread
112112
public void setButtonCloseText(@NotNull final String text) {
113-
getCloseButton().setText(text);
113+
notNull(getCloseButton()).setText(text);
114114
}
115115

116116
@Override
@@ -171,7 +171,13 @@ private void createControls() {
171171
*/
172172
@FXThread
173173
protected void validate() {
174-
getOkButton().setDisable(!validator.test(vars));
174+
175+
getRoot().getChildren().stream()
176+
.filter(PropertyEditorControl.class::isInstance)
177+
.map(PropertyEditorControl.class::cast)
178+
.forEach(PropertyEditorControl::checkDependency);
179+
180+
notNull(getOkButton()).setDisable(!validator.test(vars));
175181
}
176182

177183
@Override

src/main/java/com/ss/editor/plugin/api/file/creator/GenericFileCreator.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public class GenericFileCreator extends AbstractFileCreator {
3131
private static final Array<PropertyDefinition> EMPTY_ARRAY = ArrayFactory.asArray();
3232

3333
/**
34-
* The list of created controls.
34+
* The settings container.
3535
*/
3636
@Nullable
37-
private Array<PropertyEditorControl<?>> controls;
37+
private GridPane settingsContainer;
3838

3939
/**
4040
* The result vars of the creator.
@@ -49,8 +49,8 @@ public GenericFileCreator() {
4949
protected void createSettings(@NotNull final GridPane root) {
5050
super.createSettings(root);
5151

52+
this.settingsContainer = root;
5253
this.vars = VarTable.newInstance();
53-
this.controls = ArrayFactory.newArray(PropertyEditorControl.class);
5454

5555
int rowIndex = 1;
5656

@@ -59,7 +59,6 @@ protected void createSettings(@NotNull final GridPane root) {
5959
final PropertyEditorControl<?> control = build(vars, definition, this::validateFileName);
6060
control.prefWidthProperty().bind(widthProperty());
6161
root.add(control, 0, rowIndex++, 2, 1);
62-
controls.add(control);
6362
}
6463
}
6564

@@ -73,29 +72,31 @@ public void show(@NotNull final Window owner) {
7372
/**
7473
* @return the result vars of the creator.
7574
*/
75+
@FXThread
7676
protected @NotNull VarTable getVars() {
7777
return notNull(vars);
7878
}
7979

8080
/**
81-
* Get the list of created controls.
81+
* Get the settings container.
8282
*
83-
* @return the list of created controls.
83+
* @return the settings container.
8484
*/
8585
@FXThread
86-
private @Nullable Array<PropertyEditorControl<?>> getControls() {
87-
return controls;
86+
private @NotNull GridPane getSettingsContainer() {
87+
return notNull(settingsContainer);
8888
}
8989

9090
@Override
9191
@FXThread
9292
protected void validateFileName() {
9393
super.validateFileName();
9494

95-
final Array<PropertyEditorControl<?>> controls = getControls();
96-
if (controls != null) {
97-
controls.forEach(PropertyEditorControl::checkDependency);
98-
}
95+
final GridPane settingsContainer = getSettingsContainer();
96+
settingsContainer.getChildren().stream()
97+
.filter(PropertyEditorControl.class::isInstance)
98+
.map(PropertyEditorControl.class::cast)
99+
.forEach(PropertyEditorControl::checkDependency);
99100

100101
final Button okButton = getOkButton();
101102
if (okButton == null) return;

src/main/java/com/ss/editor/plugin/api/property/control/FolderAssetResourcePropertyControl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public FolderAssetResourcePropertyControl(@NotNull final VarTable vars,
4040
@Override
4141
@FXThread
4242
protected void processSelect() {
43-
super.processSelect();
4443
UIUtils.openFolderAssetDialog(this::processSelect, getActionTester());
4544
}
4645

src/main/java/com/ss/editor/ui/component/asset/tree/context/menu/action/ImportModelFileAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.editor.ui.component.asset.tree.context.menu.action;
22

3+
import com.ss.editor.Messages;
34
import com.ss.editor.annotation.FXThread;
45
import com.ss.editor.ui.Icons;
56
import com.ss.editor.ui.component.asset.tree.resource.FileResourceElement;
@@ -39,7 +40,7 @@ protected void execute(@Nullable final ActionEvent event) {
3940
@Override
4041
@FXThread
4142
protected @NotNull String getName() {
42-
return "Import model";
43+
return Messages.ASSET_COMPONENT_RESOURCE_TREE_CONTEXT_MENU_IMPORT_MODEL;
4344
}
4445

4546
@Override

0 commit comments

Comments
 (0)