Skip to content

Commit d7894fc

Browse files
authored
Merge pull request #671 from jMonkeyEngine/copilot/fix-668
Fix drag and drop issues from file tree to scene composer by implementing AssetNameHolder and flavor detection
2 parents 1586f7c + 46f2c7b commit d7894fc

4 files changed

Lines changed: 109 additions & 25 deletions

File tree

jme3-assetbrowser/src/com/jme3/gde/assetbrowser/widgets/AssetPreviewWidget.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2023 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -154,11 +154,6 @@ public String getAssetName() {
154154
return assetNameLabel.getText();
155155
}
156156

157-
@Override
158-
public void setAssetName(String name) {
159-
assetNameLabel.setText(name);
160-
}
161-
162157
public void setEditable(boolean editable) {
163158
this.editable = editable;
164159
}

jme3-core/src/com/jme3/gde/core/assets/AssetDataNode.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2010 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -32,6 +32,7 @@
3232
package com.jme3.gde.core.assets;
3333

3434
import com.jme3.asset.AssetKey;
35+
import com.jme3.gde.core.dnd.AssetNameHolder;
3536
import com.jme3.gde.core.util.PropertyUtils;
3637
import java.beans.PropertyDescriptor;
3738
import java.lang.reflect.Field;
@@ -48,7 +49,7 @@
4849
* @author normenhansen
4950
*/
5051
@SuppressWarnings({"unchecked", "rawtypes"})
51-
public class AssetDataNode extends DataNode {
52+
public class AssetDataNode extends DataNode implements AssetNameHolder {
5253

5354
public AssetDataNode(DataObject obj, Children ch) {
5455
super(obj, ch);
@@ -69,7 +70,7 @@ protected Sheet createSheet() {
6970
if (key == null) {
7071
return sheet;
7172
}
72-
73+
7374
Sheet.Set set = Sheet.createPropertiesSet();
7475
set.setName("AssetKey");
7576
set.setDisplayName("Conversion Settings");
@@ -89,4 +90,14 @@ protected Sheet createSheet() {
8990
sheet.put(set);
9091
return sheet;
9192
}
93+
94+
@Override
95+
public String getAssetName() {
96+
AssetData data = getLookup().lookup(AssetData.class);
97+
if (data != null && data.getAssetKey() != null) {
98+
return data.getAssetKey().getName();
99+
}
100+
return null;
101+
}
102+
92103
}
Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
/*
2-
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3-
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
2+
* Copyright (c) 2009-2025 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
431
*/
532
package com.jme3.gde.core.dnd;
633

@@ -11,6 +38,4 @@
1138
public interface AssetNameHolder {
1239

1340
String getAssetName();
14-
15-
void setAssetName(String name);
1641
}

jme3-core/src/com/jme3/gde/core/dnd/SceneViewerDropTargetListener.java

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
/*
2-
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3-
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
2+
* Copyright (c) 2009-2025 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
431
*/
532
package com.jme3.gde.core.dnd;
633

34+
import com.jme3.gde.core.assets.AssetDataNode;
735
import com.jme3.gde.core.sceneviewer.SceneViewerTopComponent;
836
import com.jme3.math.Vector2f;
937
import java.awt.Cursor;
@@ -19,8 +47,9 @@
1947
import java.util.logging.Logger;
2048

2149
/**
22-
* Handles dropping Materials or Spatial from the AssetBrowser to the
23-
* SceneViewer
50+
* Handles dropping Materials or Spatial from the AssetBrowser and Projects tab
51+
* to the SceneViewer
52+
*
2453
* @author rickard
2554
*/
2655
public class SceneViewerDropTargetListener implements DropTargetListener {
@@ -57,8 +86,7 @@ public void dragExit(final DropTargetEvent dte) {
5786
@Override
5887
public void drop(final DropTargetDropEvent dtde) {
5988
this.rootPanel.setCursor(Cursor.getDefaultCursor());
60-
61-
AssetNameHolder transferableObj = null;
89+
String assetKey = null;
6290
Transferable transferable = null;
6391
DataFlavor flavor = null;
6492

@@ -69,26 +97,51 @@ public void drop(final DropTargetDropEvent dtde) {
6997
flavor = flavors[0];
7098
// What does the Transferable support
7199
if (transferable.isDataFlavorSupported(flavor)) {
72-
transferableObj = (AssetNameHolder) dtde.getTransferable().getTransferData(flavor);
100+
Object o = dtde.getTransferable().getTransferData(flavor);
101+
if (o instanceof AssetNameHolder assetNameHolder) {
102+
assetKey = assetNameHolder.getAssetName();
103+
} else if (o instanceof AssetDataNode assetDataNode) {
104+
assetKey = assetDataNode.getAssetName();
105+
}
73106
}
74107

75108
} catch (UnsupportedFlavorException | IOException ex) {
76109
Logger.getLogger(SceneViewerDropTargetListener.class.getName()).log(Level.WARNING, "Non-supported flavor {0}", transferable);
77110
}
78111

79-
if (transferable == null || transferableObj == null) {
112+
if (transferable == null || assetKey == null) {
80113
return;
81114
}
82115

83116
final int dropYLoc = dtde.getLocation().y;
84117
final int dropXLoc = dtde.getLocation().x;
85118

86-
if (flavor instanceof SpatialDataFlavor) {
87-
rootPanel.addModel(transferableObj.getAssetName(), new Vector2f(dropXLoc, dropYLoc));
88-
} else if (flavor instanceof MaterialDataFlavor) {
89-
rootPanel.applyMaterial(transferableObj.getAssetName(), new Vector2f(dropXLoc, dropYLoc));
119+
if (flavor instanceof SpatialDataFlavor || isModelExtension(assetKey)) {
120+
rootPanel.addModel(assetKey, new Vector2f(dropXLoc, dropYLoc));
121+
} else if (flavor instanceof MaterialDataFlavor || isMaterialExtension(assetKey)) {
122+
rootPanel.applyMaterial(assetKey, new Vector2f(dropXLoc, dropYLoc));
90123
}
91124

92125
}
93126

127+
/**
128+
* Determines if the asset key represents a model/spatial asset by checking its file extension.
129+
*
130+
* @param assetKey The asset key (typically a filename or path); this method checks if it ends with the model file extension.
131+
* @return true if the asset key is for model files
132+
*/
133+
private boolean isModelExtension(String assetKey) {
134+
return assetKey.endsWith("j3o");
135+
}
136+
137+
/**
138+
* Determines if the asset key represents a material asset by checking its file extension.
139+
*
140+
* @param assetKey The asset key (typically a filename or path); this method checks if it ends with the material file extension.
141+
* @return true if the asset key is for material files
142+
*/
143+
private boolean isMaterialExtension(String assetKey) {
144+
return assetKey.endsWith("j3m");
145+
}
146+
94147
}

0 commit comments

Comments
 (0)