work on cloud browser

This commit is contained in:
Branislav Stojkovic
2016-10-23 09:56:45 +02:00
parent 309973e37f
commit 545e483397
5 changed files with 209 additions and 5 deletions
@@ -8,6 +8,7 @@ import java.util.List;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.plugins.cloud.model.ProjectEntity;
import org.diylc.plugins.cloud.model.ServiceAPI;
import org.diylc.plugins.cloud.model.UserEntity;
@@ -226,6 +227,26 @@ public class CloudPresenter {
throw new CloudException(res);
}
public List<ProjectEntity> search(String criteria, String category,
String sortOrder, int pageNumber, int itemsPerPage)
throws CloudException {
try {
Object res = service.search(criteria, category, "json", pageNumber,
itemsPerPage, sortOrder);
if (res == null)
throw new CloudException("Failed to retreive search results.");
if (res instanceof String)
throw new CloudException(res.toString());
if (res instanceof List<?>)
return (List<ProjectEntity>) res;
throw new CloudException(
"Unexpected server response received for search results: "
+ res.getClass().getName());
} catch (Exception e) {
throw new CloudException(e);
}
}
private String getMachineId() {
if (machineId == null) {
try {
@@ -42,7 +42,8 @@ public enum IconLoader {
"rotate_ccw.png"), ElementInto("element_into.png"), Arrow(
"arrow.png"), Cloud("cloud.png"), CloudUp("cloud_up.png"), CloudGear(
"cloud_gear.png"), IdCardEdit("id_card_edit.png"), KeyEdit(
"key_edit.png"), Find("find.png"), Dashboard("dashboard.png");
"key_edit.png"), Find("find.png"), Dashboard("dashboard.png"), DataFind(
"data_find.png");
protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 833 B

@@ -2,7 +2,6 @@ package org.diylc.swing.gui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@@ -4,27 +4,53 @@ import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import org.apache.log4j.Logger;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.core.IView;
import org.diylc.images.IconLoader;
import org.diylc.plugins.cloud.model.ProjectEntity;
import org.diylc.plugins.cloud.presenter.CloudException;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
public class CloudBrowserFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(CloudBrowserFrame.class);
private JPanel mainPanel;
private JTabbedPane tabbedPane;
private JPanel dashboardPanel;
private JPanel browsePanel;
private JPanel searchPanel;
private JTextField searchField;
private JComboBox categoryBox;
private JComboBox sortBox;
private JButton goButton;
private IPlugInPort plugInPort;
private CloudPresenter cloudPresenter;
private int pageNumber;
private int itemsPerPage = 10;
public CloudBrowserFrame(JFrame owner, IPlugInPort plugInPort,
CloudPresenter cloudPresenter) {
super("DIY Cloud Browser");
@@ -58,8 +84,10 @@ public class CloudBrowserFrame extends JFrame {
private JTabbedPane getTabbedPane() {
if (tabbedPane == null) {
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Dashboard", IconLoader.Dashboard.getIcon(), getDashboardPanel());
tabbedPane.addTab("Search For Projects", IconLoader.Find.getIcon(), getBrowsePanel());
tabbedPane.addTab("Dashboard", IconLoader.Dashboard.getIcon(),
getDashboardPanel());
tabbedPane.addTab("Search For Projects", IconLoader.Find.getIcon(),
getBrowsePanel());
}
return tabbedPane;
}
@@ -73,8 +101,163 @@ public class CloudBrowserFrame extends JFrame {
private JPanel getBrowsePanel() {
if (browsePanel == null) {
browsePanel = new JPanel();
browsePanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
browsePanel.add(getSearchPanel(), gbc);
gbc.gridy = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
browsePanel.add(new JPanel(), gbc);
}
return browsePanel;
}
private JPanel getSearchPanel() {
if (searchPanel == null) {
searchPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0;
searchPanel.add(new JLabel("Search For:"), gbc);
gbc.gridx = 1;
gbc.weightx = 1;
searchPanel.add(getSearchField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("Filter By Category:"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(getCategoryBox(), gbc);
gbc.gridx = 4;
gbc.weightx = 0;
searchPanel.add(new JLabel("Sort By:"), gbc);
gbc.gridx = 5;
searchPanel.add(getSortBox(), gbc);
gbc.gridx = 6;
searchPanel.add(getGoButton(), gbc);
}
return searchPanel;
}
private JTextField getSearchField() {
if (searchField == null) {
searchField = new JTextField();
}
return searchField;
}
private JComboBox getCategoryBox() {
if (categoryBox == null) {
try {
categoryBox = new JComboBox(cloudPresenter.getCategories());
} catch (CloudException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return categoryBox;
}
private JComboBox getSortBox() {
if (sortBox == null) {
sortBox = new JComboBox();
}
return sortBox;
}
public JButton getGoButton() {
if (goButton == null) {
goButton = new JButton("Go", IconLoader.DataFind.getIcon());
goButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CloudBrowserFrame.this.pageNumber = 1;
executeBackgroundTask(new ITask<List<ProjectEntity>>() {
@Override
public List<ProjectEntity> doInBackground()
throws Exception {
return cloudPresenter
.search(getSearchField().getText(),
getCategoryBox().getSelectedItem() == null ? ""
: getCategoryBox()
.getSelectedItem()
.toString(),
getSortBox().getSelectedItem() == null ? ""
: getSortBox()
.getSelectedItem()
.toString(),
CloudBrowserFrame.this.pageNumber,
CloudBrowserFrame.this.itemsPerPage);
}
@Override
public void failed(Exception e) {
showMessage(
"Search failed. Error: " + e.getMessage(),
"Search Failed", IView.ERROR_MESSAGE);
}
@Override
public void complete(List<ProjectEntity> result) {
// TODO Auto-generated method stub
}
});
}
});
}
return goButton;
}
public <T extends Object> void executeBackgroundTask(final ITask<T> task) {
getGlassPane().setVisible(true);
SwingWorker<T, Void> worker = new SwingWorker<T, Void>() {
@Override
protected T doInBackground() throws Exception {
return task.doInBackground();
}
@Override
protected void done() {
try {
T result = get();
task.complete(result);
} catch (ExecutionException e) {
LOG.error("Background task execution failed", e);
task.failed(e);
} catch (InterruptedException e) {
LOG.error("Background task execution interrupted", e);
task.failed(e);
}
getGlassPane().setVisible(false);
}
};
worker.execute();
}
public void showMessage(String message, String title, int messageType) {
JOptionPane.showMessageDialog(this, message, title, messageType);
}
}