From 7c6bd1804bb27b1344d677f85207bf1eaabdbc61 Mon Sep 17 00:00:00 2001 From: Branislav Stojkovic Date: Sat, 22 Oct 2016 12:31:32 +0200 Subject: [PATCH] project upload is functional! --- .../cloud/presenter/CloudException.java | 4 + .../cloud/presenter/CloudPresenter.java | 56 +++++- .../org/diylc/swing/gui/DialogFactory.java | 4 +- .../swing/plugins/cloud/CloudPlugIn.java | 30 ++- .../plugins/cloud/view/UploadDialog.java | 184 ++++++++++++++---- 5 files changed, 231 insertions(+), 47 deletions(-) diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudException.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudException.java index 038de46b..d05e8ae1 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudException.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudException.java @@ -8,4 +8,8 @@ public class CloudException extends Exception { super(e); } + public CloudException(String string) { + super(string); + } + } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudPresenter.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudPresenter.java index 5effec7c..57d64983 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudPresenter.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/swing/plugins/cloud/presenter/CloudPresenter.java @@ -1,7 +1,15 @@ package org.diylc.swing.plugins.cloud.presenter; +import java.awt.Image; +import java.awt.image.RenderedImage; +import java.io.File; +import java.io.IOException; import java.net.InetAddress; 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; @@ -15,7 +23,10 @@ public class CloudPresenter { private static String USERNAME_KEY = "cloud.Username"; private static String TOKEN_KEY = "cloud.token"; + private static String ERROR = "Error"; + private final static Logger LOG = Logger.getLogger(CloudPresenter.class); + private static final Object SUCCESS = "Success"; private ServiceAPI service; private String serviceUrl; @@ -27,7 +38,7 @@ public class CloudPresenter { public CloudPresenter(CloudListener listener) { serviceUrl = ConfigurationManager.getInstance().readString( - ServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api"); + ServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api/v1"); ProxyFactory factory = new ProxyFactory(new PhpFlatProxy()); service = factory.createProxy(ServiceAPI.class, serviceUrl); if (service == null) @@ -48,7 +59,7 @@ public class CloudPresenter { throw new CloudException(e); } - if (res == null || res.equals("Error")) { + if (res == null || res.equals(ERROR)) { LOG.info("Login failed"); return false; } else { @@ -79,7 +90,7 @@ public class CloudPresenter { } catch (Exception e) { throw new CloudException(e); } - if (res == null || res.equals("Error")) { + if (res == null || res.equals(ERROR)) { LOG.info("Login failed"); return false; } else { @@ -103,6 +114,45 @@ public class CloudPresenter { return loggedIn; } + 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 cats = (List) res; + return cats.toArray(new String[0]); + } catch (Exception e) { + throw new CloudException(e); + } + } + + public void upload(String projectName, String category, String description, + String keywords, String diylcVersion, RenderedImage thumbnail, + File project) throws IOException, CloudException { + String username = ConfigurationManager.getInstance().readString( + USERNAME_KEY, null); + String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, + null); + + 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); + } + } + } + private String getMachineId() { if (machineId == null) { try { diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java index 1f3c1ce6..c14e023b 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java @@ -151,8 +151,8 @@ public class DialogFactory { return dialog; } - public UploadDialog createUploadDialog(IPlugInPort plugInPort) { - UploadDialog dialog = new UploadDialog(mainFrame, plugInPort); + public UploadDialog createUploadDialog(IPlugInPort plugInPort, CloudPresenter cloudPresenter) { + UploadDialog dialog = new UploadDialog(mainFrame, plugInPort, cloudPresenter); return dialog; } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java index 4a5d1f0e..064e3281 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java @@ -288,21 +288,41 @@ public class CloudPlugIn implements IPlugIn, CloudListener { FileFilterEnum.DIY.getFilter(), null, FileFilterEnum.DIY.getExtensions()[0], null); if (file != null) { - swingUI.executeBackgroundTask(new ITask() { + swingUI.executeBackgroundTask(new ITask() { @Override - public Void doInBackground() throws Exception { + public File doInBackground() throws Exception { LOG.debug("Uploading from " + file.getAbsolutePath()); thumbnailPresenter.loadProjectFromFile(file .getAbsolutePath()); - return null; + return file; } @Override - public void complete(Void result) { + public void complete(File result) { UploadDialog dialog = DialogFactory.getInstance() - .createUploadDialog(thumbnailPresenter); + .createUploadDialog(thumbnailPresenter, + cloudPresenter); dialog.setVisible(true); + 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); + } catch (Exception e) { + swingUI.showMessage(e.getMessage(), + "Upload Error", IView.ERROR_MESSAGE); + } + } } @Override diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java index 68fb0f4a..9b911026 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java @@ -8,6 +8,7 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Rectangle; +import java.awt.image.BufferedImage; import java.util.EnumSet; import javax.swing.BorderFactory; @@ -25,6 +26,7 @@ import javax.swing.event.ChangeListener; import org.diylc.common.DrawOption; import org.diylc.common.IPlugInPort; +import org.diylc.swing.plugins.cloud.presenter.CloudException; import org.diylc.swing.plugins.cloud.presenter.CloudPresenter; import org.diylc.swingframework.ButtonDialog; @@ -37,9 +39,13 @@ public class UploadDialog extends ButtonDialog { + BULLET + "All content uploaded to DIY Cloud is shared under Creative Commons 3.0 Licence.
" + BULLET - + "Only finished and resonably verified projects may be uploaded to the library.
" + + "Only finished and resonably verified projects may be uploaded to the DIY Cloud.
" + BULLET - + "Uploaded content may be reviewed and/or removed by the administrators.
" + + "Please provide a meaningful project description to make it possible for others to find the project.
" + + BULLET + + "If you are building on top of someone else's work, please leave credits in the description.
" + + BULLET + + "Uploaded content may be reviewed, edited and removed by the administrators if it is not up to standards.
" + ""; private JPanel mainPanel; @@ -52,12 +58,15 @@ public class UploadDialog extends ButtonDialog { private JTextField keywordsField; private IPlugInPort plugInPort; + private CloudPresenter cloudPresenter; - public UploadDialog(JFrame owner, IPlugInPort plugInPort) { + public UploadDialog(JFrame owner, IPlugInPort plugInPort, + CloudPresenter cloudPresenter) { super(owner, "Upload A Project", new String[] { OK, CANCEL }); this.plugInPort = plugInPort; + this.cloudPresenter = cloudPresenter; layoutGui(); - refreshState(); + getButton(OK).setEnabled(false); } @Override @@ -67,20 +76,65 @@ public class UploadDialog extends ButtonDialog { mainPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); GridBagConstraints gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.LINE_START; + gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = new Insets(2, 2, 2, 2); gbc.gridx = 0; gbc.gridy = 0; + gbc.gridheight = 4; mainPanel.add(getThumbnailPanel(), gbc); + gbc.gridheight = 1; + gbc.gridx = 1; + gbc.weightx = 0; + gbc.fill = GridBagConstraints.HORIZONTAL; + mainPanel.add(new JLabel("Project Name: "), gbc); + + gbc.gridx = 2; + gbc.weightx = 1; + mainPanel.add(getNameField(), gbc); + + gbc.gridx = 1; gbc.gridy = 1; + gbc.weightx = 0; + mainPanel.add(new JLabel("Category:"), gbc); + + gbc.gridx = 2; + gbc.weightx = 1; + mainPanel.add(getCategoryBox(), gbc); + + gbc.gridx = 1; + gbc.gridy = 2; + gbc.weightx = 0; + + mainPanel.add(new JLabel("Description: "), gbc); + + gbc.gridx = 2; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(getDescriptionArea(), gbc); + + gbc.gridx = 1; + gbc.gridy = 3; + gbc.weightx = 0; + gbc.weighty = 0; + gbc.fill = GridBagConstraints.HORIZONTAL; + mainPanel.add(new JLabel("Keywords: "), gbc); + + gbc.gridx = 2; + gbc.weightx = 1; + mainPanel.add(getKeywordsField(), gbc); + + gbc.gridx = 0; + gbc.gridy = 4; + gbc.weightx = 1; + gbc.weighty = 1; + gbc.gridwidth = 3; + gbc.fill = GridBagConstraints.BOTH; mainPanel.add(getTermsLabel(), gbc); - gbc.gridy = 2; + gbc.gridy = 5; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.weightx = 0; @@ -90,10 +144,6 @@ public class UploadDialog extends ButtonDialog { return mainPanel; } - private void refreshState() { - getButton(OK).setEnabled(getAgreeBox().isSelected()); - } - private JCheckBox getAgreeBox() { if (agreeBox == null) { agreeBox = new JCheckBox("I agree to these terms"); @@ -101,7 +151,7 @@ public class UploadDialog extends ButtonDialog { @Override public void stateChanged(ChangeEvent e) { - refreshState(); + getButton(OK).setEnabled(getAgreeBox().isSelected()); } }); } @@ -121,7 +171,7 @@ public class UploadDialog extends ButtonDialog { return termsLabel; } - public JPanel getThumbnailPanel() { + private JPanel getThumbnailPanel() { if (thumbnailPanel == null) { thumbnailPanel = new JPanel(new GridBagLayout()); @@ -141,27 +191,7 @@ public class UploadDialog extends ButtonDialog { public void paint(Graphics g) { super.paint(g); - Graphics2D g2d = (Graphics2D) g; - Dimension d = UploadDialog.this.plugInPort - .getCanvasDimensions(false); - - Rectangle rect = getBounds(); - - g2d.setColor(Color.white); - g2d.fill(rect); - - double projectRatio = d.getWidth() / d.getHeight(); - double actualRatio = rect.getWidth() / rect.getHeight(); - double zoomRatio; - if (projectRatio > actualRatio) { - zoomRatio = rect.getWidth() / d.getWidth(); - } else { - zoomRatio = rect.getHeight() / d.getHeight(); - } - - g2d.scale(zoomRatio, zoomRatio); - UploadDialog.this.plugInPort.draw(g2d, - EnumSet.noneOf(DrawOption.class), null); + paintThumbnail(g, getBounds()); } }, gbc); Dimension d = UploadDialog.this.plugInPort @@ -177,19 +207,99 @@ public class UploadDialog extends ButtonDialog { return thumbnailPanel; } - public JTextField getNameField() { + private JTextField getNameField() { + if (nameField == null) { + nameField = new JTextField(); + + // set default from the project + for (int i = 0; i < plugInPort.getProjectProperties().size(); i++) + if (plugInPort.getProjectProperties().get(i).getName() + .equals("Title")) + nameField.setText(plugInPort.getProjectProperties().get(i) + .getValue().toString()); + } return nameField; } - public JComboBox getCategoryBox() { + private JComboBox getCategoryBox() { + if (categoryBox == null) { + try { + categoryBox = new JComboBox(cloudPresenter.getCategories()); + } catch (CloudException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } return categoryBox; } - public JTextArea getDescriptionArea() { + private JTextArea getDescriptionArea() { + if (descriptionArea == null) { + descriptionArea = new JTextArea(); + + // set default from the project + for (int i = 0; i < plugInPort.getProjectProperties().size(); i++) + if (plugInPort.getProjectProperties().get(i).getName() + .equals("Description")) + descriptionArea.setText(plugInPort.getProjectProperties() + .get(i).getValue().toString()); + descriptionArea.setBorder(getNameField().getBorder()); + descriptionArea.setFont(getNameField().getFont()); + } return descriptionArea; } - public JTextField getKeywordsField() { + private JTextField getKeywordsField() { + if (keywordsField == null) { + keywordsField = new JTextField(); + } return keywordsField; } + + public String getName() { + return getNameField().getText(); + } + + public String getDescription() { + return getDescriptionArea().getText(); + } + + public String getCategory() { + return getCategoryBox().getSelectedItem().toString(); + } + + public BufferedImage getThumbnail() { + BufferedImage thumbnailImage = new BufferedImage(getThumbnailPanel() + .getWidth(), getThumbnailPanel().getHeight(), + BufferedImage.TYPE_INT_RGB); + Graphics2D cg = thumbnailImage.createGraphics(); + paintThumbnail(cg, new Rectangle(thumbnailImage.getWidth(), thumbnailImage.getHeight())); + return thumbnailImage; + } + + private void paintThumbnail(Graphics g, Rectangle rect) { + Graphics2D g2d = (Graphics2D) g; + Dimension d = UploadDialog.this.plugInPort + .getCanvasDimensions(false); + + g2d.setColor(Color.white); + g2d.fill(rect); + + double projectRatio = d.getWidth() / d.getHeight(); + double actualRatio = rect.getWidth() / rect.getHeight(); + double zoomRatio; + if (projectRatio > actualRatio) { + zoomRatio = rect.getWidth() / d.getWidth(); + } else { + zoomRatio = rect.getHeight() / d.getHeight(); + } + + g2d.scale(zoomRatio, zoomRatio); + UploadDialog.this.plugInPort.draw(g2d, + EnumSet.noneOf(DrawOption.class), null); + } + + public String getKeywords() { + return getKeywordsField().getText(); + } }