mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
add support for update/delete project
move some code to background threads
This commit is contained in:
@@ -13,7 +13,6 @@ import org.diylc.appframework.miscutils.ConfigurationManager;
|
||||
import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
import org.diylc.plugins.cloud.model.UserEntity;
|
||||
import org.diylc.plugins.cloud.presenter.CloudPresenter;
|
||||
import org.diylc.swing.gui.components.OverwritePromptFileChooser;
|
||||
import org.diylc.swing.gui.editor.PropertyEditorDialog;
|
||||
import org.diylc.swing.plugins.cloud.view.ChangePasswordDialog;
|
||||
@@ -138,8 +137,8 @@ public class DialogFactory {
|
||||
return dialog;
|
||||
}
|
||||
|
||||
public UploadDialog createUploadDialog(IPlugInPort plugInPort, CloudPresenter cloudPresenter) {
|
||||
UploadDialog dialog = new UploadDialog(mainFrame, plugInPort, cloudPresenter);
|
||||
public UploadDialog createUploadDialog(IPlugInPort plugInPort, String[] categories) {
|
||||
UploadDialog dialog = new UploadDialog(mainFrame, plugInPort, categories);
|
||||
return dialog;
|
||||
}
|
||||
|
||||
|
||||
@@ -390,28 +390,46 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
|
||||
DialogFactory.getInstance().showOpenDialog(FileFilterEnum.DIY.getFilter(), null,
|
||||
FileFilterEnum.DIY.getExtensions()[0], null);
|
||||
if (file != null) {
|
||||
swingUI.executeBackgroundTask(new ITask<File>() {
|
||||
swingUI.executeBackgroundTask(new ITask<String[]>() {
|
||||
|
||||
@Override
|
||||
public File doInBackground() throws Exception {
|
||||
public String[] doInBackground() throws Exception {
|
||||
LOG.debug("Uploading from " + file.getAbsolutePath());
|
||||
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
|
||||
return file;
|
||||
return cloudPresenter.getCategories();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(File result) {
|
||||
UploadDialog dialog = DialogFactory.getInstance().createUploadDialog(thumbnailPresenter, cloudPresenter);
|
||||
public void complete(final String[] result) {
|
||||
final UploadDialog dialog = DialogFactory.getInstance().createUploadDialog(thumbnailPresenter, result);
|
||||
dialog.setVisible(true);
|
||||
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
|
||||
try {
|
||||
File thumbnailFile = File.createTempFile("upload-thumbnail", ".png");
|
||||
final 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);
|
||||
swingUI.executeBackgroundTask(new ITask<Void>() {
|
||||
|
||||
@Override
|
||||
public Void doInBackground() throws Exception {
|
||||
cloudPresenter.uploadProject(dialog.getName(), dialog.getCategory(), dialog.getDescription(),
|
||||
dialog.getKeywords(), plugInPort.getCurrentVersionNumber().toString(), thumbnailFile, file);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Exception e) {
|
||||
swingUI.showMessage(e.getMessage(), "Upload Error", IView.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(Void 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);
|
||||
|
||||
@@ -26,8 +26,6 @@ import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.diylc.common.DrawOption;
|
||||
import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.plugins.cloud.presenter.CloudException;
|
||||
import org.diylc.plugins.cloud.presenter.CloudPresenter;
|
||||
import org.diylc.swingframework.ButtonDialog;
|
||||
import org.diylc.utils.KeywordExtractor;
|
||||
|
||||
@@ -55,12 +53,12 @@ public class UploadDialog extends ButtonDialog {
|
||||
private JTextField keywordsField;
|
||||
|
||||
private IPlugInPort plugInPort;
|
||||
private CloudPresenter cloudPresenter;
|
||||
private String[] categories;
|
||||
|
||||
public UploadDialog(JFrame owner, IPlugInPort plugInPort, CloudPresenter cloudPresenter) {
|
||||
public UploadDialog(JFrame owner, IPlugInPort plugInPort, String[] categories) {
|
||||
super(owner, "Upload A Project", new String[] {OK, CANCEL});
|
||||
this.plugInPort = plugInPort;
|
||||
this.cloudPresenter = cloudPresenter;
|
||||
this.categories = categories;
|
||||
layoutGui();
|
||||
getButton(OK).setEnabled(false);
|
||||
}
|
||||
@@ -208,12 +206,7 @@ public class UploadDialog extends ButtonDialog {
|
||||
|
||||
private JComboBox getCategoryBox() {
|
||||
if (categoryBox == null) {
|
||||
try {
|
||||
categoryBox = new JComboBox(cloudPresenter.getCategories());
|
||||
} catch (CloudException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
categoryBox = new JComboBox(categories);
|
||||
}
|
||||
return categoryBox;
|
||||
}
|
||||
|
||||
+27
-1
@@ -29,6 +29,7 @@ import org.diylc.plugins.cloud.model.ProjectEntity;
|
||||
import org.diylc.plugins.cloud.presenter.CloudPresenter;
|
||||
import org.diylc.plugins.cloud.presenter.PagingProvider;
|
||||
import org.diylc.swing.ISwingUI;
|
||||
import org.diylc.utils.Pair;
|
||||
|
||||
public class CloudBrowserFrame extends JFrame {
|
||||
|
||||
@@ -68,6 +69,31 @@ public class CloudBrowserFrame extends JFrame {
|
||||
rootPane.setDefaultButton(getSearchHeaderPanel().getGoButton());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean b) {
|
||||
super.setVisible(b);
|
||||
if (b) {
|
||||
executeBackgroundTask(new ITask<Pair<String[], String[]>>() {
|
||||
|
||||
@Override
|
||||
public Pair<String[], String[]> doInBackground() throws Exception {
|
||||
return new Pair<String[], String[]>(cloudPresenter.getCategories(), cloudPresenter.getSortings());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Exception e) {
|
||||
LOG.error("Could not fetch categories and sortings from the cloud", e);
|
||||
showMessage("Could not fetch categories and sortings from the cloud", "Cloud Error", IView.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(Pair<String[], String[]> result) {
|
||||
getSearchHeaderPanel().updateLists(result.getFirst(), result.getSecond());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public JPanel getMainPanel() {
|
||||
if (mainPanel == null) {
|
||||
mainPanel = new JPanel(new GridBagLayout());
|
||||
@@ -133,7 +159,7 @@ public class CloudBrowserFrame extends JFrame {
|
||||
|
||||
private SearchHeaderPanel getSearchHeaderPanel() {
|
||||
if (searchHeaderPanel == null) {
|
||||
searchHeaderPanel = new SearchHeaderPanel(cloudPresenter);
|
||||
searchHeaderPanel = new SearchHeaderPanel();
|
||||
searchHeaderPanel.getGoButton().addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
|
||||
+14
-19
@@ -5,6 +5,7 @@ import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JLabel;
|
||||
@@ -12,8 +13,6 @@ import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import org.diylc.images.IconLoader;
|
||||
import org.diylc.plugins.cloud.presenter.CloudException;
|
||||
import org.diylc.plugins.cloud.presenter.CloudPresenter;
|
||||
|
||||
public class SearchHeaderPanel extends JPanel {
|
||||
|
||||
@@ -24,10 +23,7 @@ public class SearchHeaderPanel extends JPanel {
|
||||
private JComboBox sortBox;
|
||||
private JButton goButton;
|
||||
|
||||
private CloudPresenter cloudPresenter;
|
||||
|
||||
public SearchHeaderPanel(CloudPresenter cloudPresenter) {
|
||||
this.cloudPresenter = cloudPresenter;
|
||||
public SearchHeaderPanel() {
|
||||
setLayout(new GridBagLayout());
|
||||
|
||||
this.setBackground(Color.white);
|
||||
@@ -50,7 +46,7 @@ public class SearchHeaderPanel extends JPanel {
|
||||
this.add(new JLabel("Filter By Category:"), gbc);
|
||||
|
||||
gbc.gridx = 3;
|
||||
gbc.weightx = 0.3;
|
||||
gbc.weightx = 0.5;
|
||||
this.add(getCategoryBox(), gbc);
|
||||
|
||||
gbc.gridx = 4;
|
||||
@@ -69,6 +65,15 @@ public class SearchHeaderPanel extends JPanel {
|
||||
getSearchField().requestFocusInWindow();
|
||||
}
|
||||
|
||||
public void updateLists(String[] categories, String[] sortings) {
|
||||
Object selectedCategory = getCategoryBox().getSelectedItem();
|
||||
Object selectedSorting = getSortBox().getSelectedItem();
|
||||
getCategoryBox().setModel(new DefaultComboBoxModel(categories));
|
||||
getSortBox().setModel(new DefaultComboBoxModel(sortings));
|
||||
getCategoryBox().setSelectedItem(selectedCategory);
|
||||
getSortBox().setSelectedItem(selectedSorting);
|
||||
}
|
||||
|
||||
private JTextField getSearchField() {
|
||||
if (searchField == null) {
|
||||
searchField = new JTextField(60);
|
||||
@@ -78,24 +83,14 @@ public class SearchHeaderPanel extends JPanel {
|
||||
|
||||
private JComboBox getCategoryBox() {
|
||||
if (categoryBox == null) {
|
||||
try {
|
||||
categoryBox = new JComboBox(cloudPresenter.getCategories());
|
||||
} catch (CloudException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
categoryBox = new JComboBox();
|
||||
}
|
||||
return categoryBox;
|
||||
}
|
||||
|
||||
private JComboBox getSortBox() {
|
||||
if (sortBox == null) {
|
||||
try {
|
||||
sortBox = new JComboBox(cloudPresenter.getSortings());
|
||||
} catch (CloudException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
sortBox = new JComboBox();
|
||||
}
|
||||
return sortBox;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user