Skip to content

Commit f5901bd

Browse files
committed
Minor refactoring
1 parent a845fa2 commit f5901bd

2 files changed

Lines changed: 53 additions & 42 deletions

File tree

src/org/netbeans/modules/php/wordpress/WordPressPhpModuleExtender.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
*/
4242
package org.netbeans.modules.php.wordpress;
4343

44-
import java.awt.Component;
4544
import java.io.BufferedReader;
4645
import java.io.IOException;
4746
import java.io.InputStreamReader;
@@ -83,10 +82,12 @@
8382
import org.netbeans.modules.php.wordpress.util.NetUtils;
8483
import org.netbeans.modules.php.wordpress.util.WPFileUtils;
8584
import org.netbeans.modules.php.wordpress.util.WPZipEntryFilter;
85+
import org.netbeans.modules.php.wordpress.wpapis.WordPressVersionCheckApi;
8686
import org.openide.filesystems.FileObject;
8787
import org.openide.filesystems.FileUtil;
8888
import org.openide.util.Exceptions;
8989
import org.openide.util.HelpCtx;
90+
import org.openide.util.NbBundle;
9091

9192
/**
9293
*
@@ -112,6 +113,7 @@ public class WordPressPhpModuleExtender extends PhpModuleExtender {
112113
private static final Set<String> CONFIG_KEYS = new HashSet<String>();
113114
private static final Map<String, String> CONFIG_MAP = new HashMap<String, String>();
114115
private boolean isInternetReachable = true;
116+
private String errorMessage;
115117
private static final Logger LOGGER = Logger.getLogger(WordPressPhpModuleExtender.class.getName());
116118

117119
static {
@@ -150,31 +152,32 @@ public HelpCtx getHelp() {
150152
return null;
151153
}
152154

155+
@NbBundle.Messages("WordPressPhpModuleExtender.no.installation=There is no available installation method.")
153156
@Override
154157
public boolean isValid() {
155158
String url = panel.getUrlLabel();
156159
String localFile = panel.getLocalFileLabel();
157160
if (!isInternetReachable || url.isEmpty()) {
158161
if (localFile.isEmpty()) {
159-
Component[] components = panel.getComponents();
160-
for (Component component : components) {
161-
component.setEnabled(false);
162-
}
162+
// disable all field
163+
panel.setAllEnabled(panel, false);
164+
panel.setAllEnabled(panel.getWpConfigPanel(), false);
165+
errorMessage = Bundle.WordPressPhpModuleExtender_no_installation();
163166
return false;
164-
165167
}
166168
}
169+
errorMessage = null;
167170
return true;
168171
}
169172

170173
@Override
171174
public String getErrorMessage() {
172-
return null;
175+
return errorMessage;
173176
}
174177

175178
@Override
176179
public String getWarningMessage() {
177-
return null;
180+
return errorMessage;
178181
}
179182

180183
/**
@@ -282,27 +285,31 @@ public Set<FileObject> extend(PhpModule pm) throws ExtendingException {
282285
*/
283286
private String getDownloadUrl() {
284287
String downloadUrl = WordPressOptions.getInstance().getDownloadUrl();
285-
if (downloadUrl != null && !downloadUrl.isEmpty()) {
288+
if (!StringUtils.isEmpty(downloadUrl)) {
286289
return downloadUrl;
287290
}
288-
Locale locale = Locale.getDefault();
289-
String language = locale.getLanguage();
290-
String urlPath = WP_DL_URL_DEFAULT;
291-
if (language != null && !language.isEmpty()) {
292-
if (!language.equals("en")) { // NOI18N
293-
urlPath = String.format(WP_DL_URL_FORMAT, language, language);
291+
292+
// get url from version check api
293+
String wpLocale = WordPressOptions.getInstance().getWpLocale();
294+
if (StringUtils.isEmpty(wpLocale)) {
295+
Locale locale = Locale.getDefault();
296+
wpLocale = locale.getLanguage();
297+
}
298+
if (!StringUtils.isEmpty(wpLocale)) {
299+
WordPressVersionCheckApi versionCheckApi = new WordPressVersionCheckApi(wpLocale);
300+
try {
301+
versionCheckApi.parse();
302+
downloadUrl = versionCheckApi.getDownload();
303+
} catch (IOException ex) {
304+
downloadUrl = WP_DL_URL_DEFAULT;
294305
}
295306
}
296-
try {
297-
URL check = new URL(urlPath);
298-
URLConnection connection = check.openConnection();
299-
connection.connect();
300-
} catch (MalformedURLException ex) {
301-
Exceptions.printStackTrace(ex);
302-
} catch (IOException ex) {
303-
urlPath = WP_DL_URL_DEFAULT;
307+
if (!StringUtils.isEmpty(downloadUrl)) {
308+
return downloadUrl;
304309
}
305-
return urlPath;
310+
311+
// default url
312+
return WP_DL_URL_DEFAULT;
306313
}
307314

308315
private void createWpConfig(FileObject sample) {
@@ -379,7 +386,7 @@ private List<String> getSecretKey() {
379386
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
380387
BufferedReader reader = new BufferedReader(new InputStreamReader(httpsConnection.getInputStream(), Charset.UTF8)); // NOI18N
381388
try {
382-
String line = null;
389+
String line;
383390
while ((line = reader.readLine()) != null) {
384391
keys.add(line);
385392
}

src/org/netbeans/modules/php/wordpress/wpapis/WordPressVersionCheckApi.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,28 @@ public String getUrl() {
8686
public void parse() throws IOException {
8787
// get json
8888
InputStream inputStream = openStream();
89-
JSONObject jsonObject = (JSONObject) JSONValue.parse(new InputStreamReader(inputStream, Charset.UTF8));
90-
JSONArray offers = (JSONArray) jsonObject.get("offers"); // NOI18N
91-
92-
// get version and locale
93-
String upgradeLocale;
94-
for (Object offer : offers) {
95-
JSONObject object = (JSONObject) offer;
96-
upgradeLocale = object.get("locale").toString(); // NOI18N
97-
version = object.get("version").toString(); // NOI18N
98-
download = object.get("download").toString(); // NOI18N
99-
phpVersion = object.get("php_version").toString(); // NOI18N
100-
mysqlVersion = object.get("mysql_version").toString(); // NOI18N
101-
if (StringUtils.isEmpty(this.locale)) {
102-
return;
103-
}
104-
if (upgradeLocale.equals(this.locale)) {
105-
return;
89+
try {
90+
JSONObject jsonObject = (JSONObject) JSONValue.parse(new InputStreamReader(inputStream, Charset.UTF8));
91+
JSONArray offers = (JSONArray) jsonObject.get("offers"); // NOI18N
92+
93+
// get version and locale
94+
String upgradeLocale;
95+
for (Object offer : offers) {
96+
JSONObject object = (JSONObject) offer;
97+
upgradeLocale = object.get("locale").toString(); // NOI18N
98+
version = object.get("version").toString(); // NOI18N
99+
download = object.get("download").toString(); // NOI18N
100+
phpVersion = object.get("php_version").toString(); // NOI18N
101+
mysqlVersion = object.get("mysql_version").toString(); // NOI18N
102+
if (StringUtils.isEmpty(this.locale)) {
103+
return;
104+
}
105+
if (upgradeLocale.equals(this.locale)) {
106+
return;
107+
}
106108
}
109+
} finally {
110+
inputStream.close();
107111
}
108112

109113
// not found locale

0 commit comments

Comments
 (0)