minor code improvement

This commit is contained in:
Branislav Stojkovic
2016-10-22 12:43:20 +02:00
parent 7c6bd1804b
commit 4738a23fc6
2 changed files with 53 additions and 36 deletions
@@ -1,6 +1,5 @@
package org.diylc.swing.plugins.cloud.presenter;
import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
@@ -9,7 +8,6 @@ import java.net.NetworkInterface;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ComboBoxModel;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
@@ -31,6 +29,7 @@ public class CloudPresenter {
private ServiceAPI service;
private String serviceUrl;
private String machineId;
private String[] categories;
private CloudListener listener;
@@ -115,22 +114,32 @@ public class CloudPresenter {
}
public String[] getCategories() throws CloudException {
Object res;
try {
res = service.getCategories();
if (res != null && res.equals(ERROR))
throw new CloudException(
"Could not fetch categories from the server.");
List<Object> cats = (List<Object>) res;
return cats.toArray(new String[0]);
} catch (Exception e) {
throw new CloudException(e);
if (categories == null) {
Object res;
try {
res = service.getCategories();
if (res != null && res.equals(ERROR))
throw new CloudException(
"Could not fetch categories from the server.");
if (res instanceof List<?>) {
List<String> cats = (List<String>) res;
cats.add(0, "");
categories = cats.toArray(new String[0]);
} else {
throw new CloudException(
"Unexpected server response received for category list: "
+ res.getClass().getName());
}
} catch (Exception e) {
throw new CloudException(e);
}
}
return categories;
}
public void upload(String projectName, String category, String description,
String keywords, String diylcVersion, RenderedImage thumbnail,
File project) throws IOException, CloudException {
String keywords, String diylcVersion, File thumbnail, File project)
throws IOException, CloudException {
String username = ConfigurationManager.getInstance().readString(
USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY,
@@ -139,18 +148,16 @@ public class CloudPresenter {
if (username == null || token == null)
throw new CloudException("Login failed. Please try to login again.");
File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
if (ImageIO.write(thumbnail, "png", thumbnailFile)) {
try {
String res = service.upload(username, token, getMachineId(),
projectName, category, description, diylcVersion,
keywords, thumbnailFile, project);
if (!res.equals(SUCCESS))
throw new CloudException(res);
} catch (Exception e) {
throw new CloudException(e);
}
try {
String res = service.upload(username, token, getMachineId(),
projectName, category, description, diylcVersion, keywords,
thumbnail, project);
if (!res.equals(SUCCESS))
throw new CloudException(res);
} catch (Exception e) {
throw new CloudException(e);
}
}
private String getMachineId() {
@@ -4,6 +4,7 @@ import java.awt.event.ActionEvent;
import java.io.File;
import java.util.EnumSet;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import org.apache.log4j.Logger;
@@ -307,17 +308,26 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
if (ButtonDialog.OK.equals(dialog
.getSelectedButtonCaption())) {
try {
cloudPresenter.upload(dialog.getName(), dialog
.getCategory(),
dialog.getDescription(), dialog
.getKeywords(), plugInPort
.getCurrentVersionNumber()
.toString(), dialog
.getThumbnail(), result);
swingUI.showMessage(
"The project has been uploaded to the cloud successfully. Thank you for your contribution!",
"Upload Success",
IView.INFORMATION_MESSAGE);
File thumbnailFile = File.createTempFile(
"upload-thumbnail", ".png");
if (ImageIO.write(dialog.getThumbnail(), "png",
thumbnailFile)) {
cloudPresenter.upload(dialog.getName(),
dialog.getCategory(), dialog
.getDescription(), dialog
.getKeywords(), plugInPort
.getCurrentVersionNumber()
.toString(), thumbnailFile,
result);
swingUI.showMessage(
"The project has been uploaded to the cloud successfully. Thank you for your contribution!",
"Upload Success",
IView.INFORMATION_MESSAGE);
} else {
swingUI.showMessage(
"Could not prepare temporary files to be uploaded to the cloud.",
"Upload Error", IView.ERROR_MESSAGE);
}
} catch (Exception e) {
swingUI.showMessage(e.getMessage(),
"Upload Error", IView.ERROR_MESSAGE);