2
0
Эх сурвалжийг харах

Fix texture cancellation issue in material editor

Co-authored-by: neph1 <[email protected]>
copilot-swe-agent[bot] 2 сар өмнө
parent
commit
ff939b955d

+ 16 - 5
jme3-materialeditor/src/com/jme3/gde/materials/multiview/widgets/TexturePanel.java

@@ -267,6 +267,8 @@ public class TexturePanel extends MaterialPropertyWidget implements TextureDropT
 
     private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_texturePreviewMouseClicked
         Component view = editor.getCustomEditor();
+        String originalValue = property.getValue(); // Store original value before clearing
+        String originalTextureName = textureName; // Store original texture name
         property.setValue(EMPTY);
         view.setVisible(true);
         if (editor.getValue() != null) {
@@ -274,11 +276,20 @@ public class TexturePanel extends MaterialPropertyWidget implements TextureDropT
             displayPreview();
             updateFlipRepeat();
             fireChanged();
-        } else { // "No Texture" has been clicked
-            textureName = "\"\"";
-            texturePreview.setIcon(null);
-            texturePreview.setToolTipText("");
-            fireChanged();
+        } else { // "No Texture" has been clicked or dialog was cancelled
+            String asText = editor.getAsText();
+            if (asText == null) {
+                // "No Texture" was explicitly selected
+                textureName = "\"\"";
+                texturePreview.setIcon(null);
+                texturePreview.setToolTipText("");
+                fireChanged();
+            } else {
+                // Dialog was cancelled, restore original values
+                property.setValue(originalValue);
+                textureName = originalTextureName;
+                displayPreview(); // Restore the preview
+            }
         }
     }//GEN-LAST:event_texturePreviewMouseClicked
 

+ 74 - 0
jme3-materialeditor/test/unit/src/com/jme3/gde/materials/multiview/widgets/TexturePanelTest.java

@@ -171,5 +171,79 @@ public class TexturePanelTest {
         assertTrue(texturePanel.property.getValue().contains("LINEAR"));
         
     }
+    
+    @Test
+    public void testTexturePreviewClickCancel() {
+        // This test simulates the scenario described in the issue:
+        // When a user clicks on a texture preview and then cancels the dialog,
+        // the original texture should be preserved instead of being unset.
+        
+        TexturePanel texturePanel = new TexturePanel();
+        texturePanel.setProperty(new MaterialProperty());
+        
+        // Set up initial texture state
+        String originalTexture = "\"original_texture.jpg\"";
+        texturePanel.property.setValue(originalTexture);
+        texturePanel.textureName = originalTexture;
+        
+        // Simulate the user clicking on texture preview and then canceling
+        // This is a simplified version of what happens in texturePreviewMouseClicked
+        String originalValue = texturePanel.property.getValue();
+        String originalTextureName = texturePanel.textureName;
+        
+        // Clear the property (this happens when the dialog opens)
+        texturePanel.property.setValue("");
+        
+        // Simulate cancel: editor.getValue() returns null and editor.getAsText() returns original value
+        // (not null, because setAsText was never called)
+        // This is the logic from the fixed texturePreviewMouseClicked method
+        String asText = originalTexture; // Would be returned by editor.getAsText() on cancel
+        if (asText != null) {
+            // Dialog was cancelled, restore original values
+            texturePanel.property.setValue(originalValue);
+            texturePanel.textureName = originalTextureName;
+        }
+        
+        // Verify that the original texture is preserved
+        assertEquals(originalTexture, texturePanel.property.getValue());
+        assertEquals(originalTexture, texturePanel.textureName);
+    }
+    
+    @Test
+    public void testTexturePreviewClickNoTexture() {
+        // This test verifies that explicitly selecting "No Texture" still works correctly
+        
+        TexturePanel texturePanel = new TexturePanel();
+        texturePanel.setProperty(new MaterialProperty());
+        
+        // Set up initial texture state
+        String originalTexture = "\"original_texture.jpg\"";
+        texturePanel.property.setValue(originalTexture);
+        texturePanel.textureName = originalTexture;
+        
+        // Simulate the user clicking on texture preview and then selecting "No Texture"
+        String originalValue = texturePanel.property.getValue();
+        String originalTextureName = texturePanel.textureName;
+        
+        // Clear the property (this happens when the dialog opens)
+        texturePanel.property.setValue("");
+        
+        // Simulate "No Texture" selection: editor.getValue() returns null and editor.getAsText() returns null
+        // This is the logic from the fixed texturePreviewMouseClicked method
+        String asText = null; // Would be returned by editor.getAsText() when "No Texture" is selected
+        if (asText == null) {
+            // "No Texture" was explicitly selected
+            texturePanel.textureName = "\"\"";
+            texturePanel.property.setValue("");
+        } else {
+            // Dialog was cancelled, restore original values
+            texturePanel.property.setValue(originalValue);
+            texturePanel.textureName = originalTextureName;
+        }
+        
+        // Verify that the texture is properly cleared
+        assertEquals("", texturePanel.property.getValue());
+        assertEquals("\"\"", texturePanel.textureName);
+    }
 
 }