Skip to content

Commit 9bb1de5

Browse files
committed
Improvement for CustomizerExtender #23
1 parent 1f2ab54 commit 9bb1de5

5 files changed

Lines changed: 185 additions & 5 deletions

File tree

nbproject/genfiles.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
build.xml.data.CRC32=f6605689
1+
build.xml.data.CRC32=a6abec14
22
build.xml.script.CRC32=f6cbff90
33
build.xml.stylesheet.CRC32=a56c6a5b@2.62.1
44
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
55
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6-
nbproject/build-impl.xml.data.CRC32=f6605689
6+
nbproject/build-impl.xml.data.CRC32=a6abec14
77
nbproject/build-impl.xml.script.CRC32=bfa38a5f
88
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.62.1

nbproject/project.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
<code-name-base>org.netbeans.modules.php.wordpress</code-name-base>
77
<standalone/>
88
<module-dependencies>
9+
<dependency>
10+
<code-name-base>org.netbeans.api.annotations.common</code-name-base>
11+
<build-prerequisite/>
12+
<compile-dependency/>
13+
<run-dependency>
14+
<release-version>1</release-version>
15+
<specification-version>1.21.1</specification-version>
16+
</run-dependency>
17+
</dependency>
918
<dependency>
1019
<code-name-base>org.netbeans.api.progress</code-name-base>
1120
<build-prerequisite/>

src/org/netbeans/modules/php/wordpress/customizer/WordPressCustomizerExtender.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import javax.swing.event.ChangeListener;
4747
import org.netbeans.modules.php.api.phpmodule.PhpModule;
4848
import org.netbeans.modules.php.api.util.StringUtils;
49+
import org.netbeans.modules.php.api.validation.ValidationResult;
4950
import org.netbeans.modules.php.spi.framework.PhpModuleCustomizerExtender;
5051
import org.netbeans.modules.php.wordpress.preferences.WordPressPreferences;
5152
import org.openide.util.HelpCtx;
@@ -60,6 +61,8 @@ public class WordPressCustomizerExtender extends PhpModuleCustomizerExtender {
6061
private WordPressCustomizerExtenderPanel panel;
6162
private final PhpModule phpModule;
6263
private String originalCustomeContentName;
64+
private boolean isValid;
65+
private String errorMessage;
6366

6467
public WordPressCustomizerExtender(PhpModule phpModule) {
6568
this.phpModule = phpModule;
@@ -73,10 +76,12 @@ public String getDisplayName() {
7376

7477
@Override
7578
public void addChangeListener(ChangeListener cl) {
79+
getPanel().addChangeListener(cl);
7680
}
7781

7882
@Override
7983
public void removeChangeListener(ChangeListener cl) {
84+
getPanel().removeChangeListener(cl);
8085
}
8186

8287
@Override
@@ -91,12 +96,38 @@ public HelpCtx getHelp() {
9196

9297
@Override
9398
public boolean isValid() {
94-
return true;
99+
validate();
100+
return isValid;
95101
}
96102

97103
@Override
98104
public String getErrorMessage() {
99-
return null;
105+
validate();
106+
return errorMessage;
107+
}
108+
109+
private void validate() {
110+
String contentName = getPanel().getCustomContentName();
111+
ValidationResult result = new WordPressCustomizerValidator()
112+
.validateWpContent(phpModule, contentName)
113+
.getResult();
114+
// error
115+
if (result.hasErrors()) {
116+
isValid = false;
117+
errorMessage = result.getErrors().get(0).getMessage();
118+
return;
119+
}
120+
121+
// warning
122+
if (result.hasWarnings()) {
123+
isValid = false;
124+
errorMessage = result.getWarnings().get(0).getMessage();
125+
return;
126+
}
127+
128+
// everything ok
129+
errorMessage = null;
130+
isValid = true;
100131
}
101132

102133
@Override

src/org/netbeans/modules/php/wordpress/customizer/WordPressCustomizerExtenderPanel.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,30 @@
4141
*/
4242
package org.netbeans.modules.php.wordpress.customizer;
4343

44+
import javax.swing.JPanel;
45+
import javax.swing.event.ChangeListener;
46+
import javax.swing.event.DocumentEvent;
47+
import javax.swing.event.DocumentListener;
48+
import org.openide.util.ChangeSupport;
49+
4450
/**
4551
*
4652
* @author junichi11
4753
*/
48-
public class WordPressCustomizerExtenderPanel extends javax.swing.JPanel {
54+
public class WordPressCustomizerExtenderPanel extends JPanel {
55+
56+
private final ChangeSupport changeSupport = new ChangeSupport(this);
4957

5058
/**
5159
* Creates new form WordPressCustomizerExtenderPanel
5260
*/
5361
public WordPressCustomizerExtenderPanel() {
5462
initComponents();
63+
init();
64+
}
65+
66+
private void init() {
67+
customContentNameTextField.getDocument().addDocumentListener(new DefaultDocumentListener());
5568
}
5669

5770
public String getCustomContentName() {
@@ -62,6 +75,18 @@ public void setCustomContentName(String name) {
6275
customContentNameTextField.setText(name);
6376
}
6477

78+
public void addChangeListener(ChangeListener cl) {
79+
changeSupport.addChangeListener(cl);
80+
}
81+
82+
public void removeChangeListener(ChangeListener cl) {
83+
changeSupport.removeChangeListener(cl);
84+
}
85+
86+
public void fireChange() {
87+
changeSupport.fireChange();
88+
}
89+
6590
/**
6691
* This method is called from within the constructor to initialize the form.
6792
* WARNING: Do NOT modify this code. The content of this method is always
@@ -104,4 +129,29 @@ private void initComponents() {
104129
private javax.swing.JLabel customContentNameLabel;
105130
private javax.swing.JTextField customContentNameTextField;
106131
// End of variables declaration//GEN-END:variables
132+
133+
private class DefaultDocumentListener implements DocumentListener {
134+
135+
public DefaultDocumentListener() {
136+
}
137+
138+
@Override
139+
public void insertUpdate(DocumentEvent e) {
140+
processUpdate();
141+
}
142+
143+
@Override
144+
public void removeUpdate(DocumentEvent e) {
145+
processUpdate();
146+
}
147+
148+
@Override
149+
public void changedUpdate(DocumentEvent e) {
150+
processUpdate();
151+
}
152+
153+
private void processUpdate() {
154+
fireChange();
155+
}
156+
}
107157
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7+
* Other names may be trademarks of their respective owners.
8+
*
9+
* The contents of this file are subject to the terms of either the GNU
10+
* General Public License Version 2 only ("GPL") or the Common
11+
* Development and Distribution License("CDDL") (collectively, the
12+
* "License"). You may not use this file except in compliance with the
13+
* License. You can obtain a copy of the License at
14+
* http://www.netbeans.org/cddl-gplv2.html
15+
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16+
* specific language governing permissions and limitations under the
17+
* License. When distributing the software, include this License Header
18+
* Notice in each file and include the License file at
19+
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20+
* particular file as subject to the "Classpath" exception as provided
21+
* by Oracle in the GPL Version 2 section of the License file that
22+
* accompanied this code. If applicable, add the following below the
23+
* License Header, with the fields enclosed by brackets [] replaced by
24+
* your own identifying information:
25+
* "Portions Copyrighted [year] [name of copyright owner]"
26+
*
27+
* If you wish your version of this file to be governed by only the CDDL
28+
* or only the GPL Version 2, indicate your decision by adding
29+
* "[Contributor] elects to include this software in this distribution
30+
* under the [CDDL or GPL Version 2] license." If you do not indicate a
31+
* single choice of license, a recipient has the option to distribute
32+
* your version of this file under either the CDDL, the GPL Version 2 or
33+
* to extend the choice of license to its licensees as provided above.
34+
* However, if you add GPL Version 2 code and therefore, elected the GPL
35+
* Version 2 license, then the option applies only if the new code is
36+
* made subject to such option by the copyright holder.
37+
*
38+
* Contributor(s):
39+
*
40+
* Portions Copyrighted 2013 Sun Microsystems, Inc.
41+
*/
42+
package org.netbeans.modules.php.wordpress.customizer;
43+
44+
import org.netbeans.api.annotations.common.NonNull;
45+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
46+
import org.netbeans.modules.php.api.util.StringUtils;
47+
import org.netbeans.modules.php.api.validation.ValidationResult;
48+
import org.openide.filesystems.FileObject;
49+
import org.openide.util.NbBundle;
50+
51+
/**
52+
*
53+
* @author junichi11
54+
*/
55+
public final class WordPressCustomizerValidator {
56+
57+
private final ValidationResult result = new ValidationResult();
58+
59+
@NbBundle.Messages({
60+
"WordPressCustomizerValidator.wordpress.dir.invalid=Project might be broken...",
61+
"WordPressCustomizerValidator.wordpress.content.name.invalid=Existing directory name must be set.",
62+
"WordPressCustomizerValidator.wordpress.content.name.contains.shash=The name must not contain slash."
63+
})
64+
public WordPressCustomizerValidator validateWpContent(@NonNull PhpModule phpModule, String name) {
65+
FileObject sourceDirectory = phpModule.getSourceDirectory();
66+
if (sourceDirectory == null) {
67+
result.addWarning(new ValidationResult.Message("wordpress.dir", Bundle.WordPressCustomizerValidator_wordpress_dir_invalid())); // NOI18N
68+
return this;
69+
}
70+
71+
FileObject wpContent = sourceDirectory.getFileObject(name);
72+
if (wpContent == null
73+
|| !wpContent.isFolder()
74+
|| StringUtils.isEmpty(name)) {
75+
result.addWarning(new ValidationResult.Message("wordpress.content.name", Bundle.WordPressCustomizerValidator_wordpress_content_name_invalid())); // NOI18N
76+
return this;
77+
}
78+
79+
if (name.contains("/")) { // NOI18N
80+
result.addWarning(new ValidationResult.Message("wordpress.content.name.slash", Bundle.WordPressCustomizerValidator_wordpress_content_name_contains_shash())); // NOI18N
81+
return this;
82+
}
83+
84+
return this;
85+
}
86+
87+
public ValidationResult getResult() {
88+
return result;
89+
}
90+
}

0 commit comments

Comments
 (0)