Skip to content

Commit 88ef54e

Browse files
authored
Merge pull request #501 from neph1/fix_material_name_parsing
fixes texture name parsing issue in texturepanel
2 parents 6a734ca + 7053b6d commit 88ef54e

2 files changed

Lines changed: 104 additions & 17 deletions

File tree

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* and open the template in the editor.
44
*/
55

6-
/*
6+
/*
77
* SelectionPanel.java
88
*
99
* Created on 14.06.2010, 16:52:22
@@ -25,20 +25,31 @@
2525
import java.util.logging.Logger;
2626

2727
/**
28-
* The TexturePanel is a row in the material editor representing a special texture
28+
* The TexturePanel is a row in the material editor representing a special
29+
* texture
30+
*
2931
* @author normenhansen
3032
*/
3133
public class TexturePanel extends MaterialPropertyWidget {
3234

33-
private final TexturePropertyEditor editor;
34-
private final ProjectAssetManager manager;
35+
private TexturePropertyEditor editor;
36+
private ProjectAssetManager manager;
3537
private boolean flip = false;
3638
private boolean repeat = false;
3739
protected String textureName = null; // always enclosed with ""
3840
private TexturePreview texPreview;
3941
private final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
4042

41-
/** Creates new form SelectionPanel */
43+
/**
44+
* Used by tests
45+
*/
46+
protected TexturePanel() {
47+
48+
}
49+
50+
/**
51+
* Creates new form SelectionPanel
52+
*/
4253
public TexturePanel(ProjectAssetManager manager) {
4354
this.manager = manager;
4455
editor = new TexturePropertyEditor(manager);
@@ -50,25 +61,27 @@ private void displayPreview() {
5061
exec.execute(new Runnable() {
5162
@Override
5263
public void run() {
53-
try{
64+
try {
5465
if (texPreview == null) {
5566
texPreview = new TexturePreview(manager);
5667
}
57-
final String[] textureNameComponents = textureName.split(" ");
58-
texPreview.requestPreview(stripQuotes(textureNameComponents[textureNameComponents.length - 1]), "", 80, 25, texturePreview, null);
68+
texPreview.requestPreview(extractTextureName(textureName), "", 80, 25, texturePreview, null);
5969
} catch (AssetNotFoundException a) {
6070
Logger.getLogger(MaterialEditorTopComponent.class.getName()).log(Level.WARNING, "Could not load texture {0}", textureName);
6171
}
6272
}
6373
});
6474
}
6575
}
66-
67-
private String stripQuotes(String s) {
68-
return s.substring(1, s.length() - 1);
76+
77+
// visible for tests
78+
protected String extractTextureName(String textureName) {
79+
final String[] textureNameComponents = textureName.split("\"");
80+
return textureNameComponents[textureNameComponents.length - 1];
6981
}
7082

71-
private void updateFlipRepeat() {
83+
// visible for tests
84+
protected void updateFlipRepeat() {
7285
String propertyValue = property.getValue();
7386
propertyValue = propertyValue.replaceFirst(textureName, "");
7487
if (flip && !propertyValue.contains("Flip ")) {
@@ -104,10 +117,10 @@ private static BufferedImage resizeImage(BufferedImage originalImage) {
104117
return resizedImage;
105118
}
106119

107-
/** This method is called from within the constructor to
108-
* initialize the form.
109-
* WARNING: Do NOT modify this code. The content of this method is
110-
* always regenerated by the Form Editor.
120+
/**
121+
* This method is called from within the constructor to initialize the form.
122+
* WARNING: Do NOT modify this code. The content of this method is always
123+
* regenerated by the Form Editor.
111124
*/
112125
@SuppressWarnings("unchecked")
113126
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
@@ -263,6 +276,7 @@ private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
263276

264277
private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_texturePreviewMouseClicked
265278
Component view = editor.getCustomEditor();
279+
property.setValue("");
266280
view.setVisible(true);
267281
if (editor.getValue() != null) {
268282
textureName = "\"" + editor.getAsText() + "\"";
@@ -273,7 +287,6 @@ private void texturePreviewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FI
273287
textureName = "\"\"";
274288
texturePreview.setIcon(null);
275289
texturePreview.setToolTipText("");
276-
property.setValue("");
277290
fireChanged();
278291
}
279292
}//GEN-LAST:event_texturePreviewMouseClicked
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2009-2023 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.
31+
*/
32+
package com.jme3.gde.materials.multiview.widgets;
33+
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import org.junit.jupiter.api.Test;
36+
37+
38+
/**
39+
*
40+
* @author rickard
41+
*/
42+
public class TexturePanelTest {
43+
44+
public TexturePanelTest() {
45+
}
46+
47+
@Test
48+
public void testExtractTextureName() {
49+
TexturePanel texturePanel = new TexturePanel();
50+
String textureName = "\"simple_name.jpg\"";
51+
String extractedName = texturePanel.extractTextureName(textureName);
52+
assertEquals("simple_name.jpg", extractedName);
53+
54+
String textureNameWithModifier = "Flip Repeat \"simple_name.jpg\"";
55+
extractedName = texturePanel.extractTextureName(textureNameWithModifier);
56+
assertEquals("simple_name.jpg", extractedName);
57+
58+
String textureNameWithSpaces = "\"texture name with spaces.jpg\"";
59+
extractedName = texturePanel.extractTextureName(textureNameWithSpaces);
60+
assertEquals("texture name with spaces.jpg", extractedName);
61+
62+
String textureNameWithSpaceAndModifier = "Flip Repeat \"texture name with spaces.jpg\"";
63+
extractedName = texturePanel.extractTextureName(textureNameWithSpaceAndModifier);
64+
assertEquals("texture name with spaces.jpg", extractedName);
65+
}
66+
67+
/**
68+
* Test of updateFlipRepeat method, of class TexturePanel.
69+
*/
70+
@org.junit.Test
71+
public void testUpdateFlipRepeat() {
72+
}
73+
74+
}

0 commit comments

Comments
 (0)