mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
code improvement
This commit is contained in:
+6
-60
@@ -7,7 +7,6 @@ import java.awt.GridBagLayout;
|
|||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@@ -43,7 +42,6 @@ public class CloudBrowserFrame extends JFrame {
|
|||||||
|
|
||||||
private SearchHeaderPanel searchHeaderPanel;
|
private SearchHeaderPanel searchHeaderPanel;
|
||||||
private ResultsScrollPanel resultsScrollPane;
|
private ResultsScrollPanel resultsScrollPane;
|
||||||
private NavigationPanel navigationPanel;
|
|
||||||
|
|
||||||
private IPlugInPort plugInPort;
|
private IPlugInPort plugInPort;
|
||||||
private CloudPresenter cloudPresenter;
|
private CloudPresenter cloudPresenter;
|
||||||
@@ -135,15 +133,6 @@ public class CloudBrowserFrame extends JFrame {
|
|||||||
gbc.weighty = 1;
|
gbc.weighty = 1;
|
||||||
gbc.fill = GridBagConstraints.BOTH;
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
searchPanel.add(getResultsScrollPane(), gbc);
|
searchPanel.add(getResultsScrollPane(), gbc);
|
||||||
|
|
||||||
// gbc.anchor = GridBagConstraints.NORTHWEST;
|
|
||||||
// gbc.insets = new Insets(2, 2, 2, 2);
|
|
||||||
// gbc.gridx = 0;
|
|
||||||
// gbc.gridy++;
|
|
||||||
// gbc.fill = GridBagConstraints.BOTH;
|
|
||||||
// gbc.weighty = 0;
|
|
||||||
// gbc.gridwidth = 3;
|
|
||||||
// searchPanel.add(getNavigationPanel(), gbc);
|
|
||||||
}
|
}
|
||||||
return searchPanel;
|
return searchPanel;
|
||||||
}
|
}
|
||||||
@@ -155,11 +144,11 @@ public class CloudBrowserFrame extends JFrame {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
setPageNumber(1);
|
CloudBrowserFrame.this.pageNumber = 1;
|
||||||
CloudBrowserFrame.this.searchFor = getSearchHeaderPanel().getSearchText();
|
CloudBrowserFrame.this.searchFor = getSearchHeaderPanel().getSearchText();
|
||||||
CloudBrowserFrame.this.category = getSearchHeaderPanel().getCategory();
|
CloudBrowserFrame.this.category = getSearchHeaderPanel().getCategory();
|
||||||
CloudBrowserFrame.this.sort = getSearchHeaderPanel().getSorting();
|
CloudBrowserFrame.this.sort = getSearchHeaderPanel().getSorting();
|
||||||
getResultsScrollPane().clear();
|
getResultsScrollPane().startOver();
|
||||||
search();
|
search();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -172,13 +161,13 @@ public class CloudBrowserFrame extends JFrame {
|
|||||||
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, new ILazyProvider<ProjectEntity>() {
|
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, new ILazyProvider<ProjectEntity>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void requestMore() {
|
public void requestMoreData() {
|
||||||
setPageNumber(CloudBrowserFrame.this.pageNumber + 1);
|
CloudBrowserFrame.this.pageNumber++;
|
||||||
CloudBrowserFrame.this.search();
|
CloudBrowserFrame.this.search();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasMore() {
|
public boolean hasMoreData() {
|
||||||
return currentResults.size() == itemsPerPage;
|
return currentResults.size() == itemsPerPage;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -203,54 +192,11 @@ public class CloudBrowserFrame extends JFrame {
|
|||||||
@Override
|
@Override
|
||||||
public void complete(List<ProjectEntity> result) {
|
public void complete(List<ProjectEntity> result) {
|
||||||
CloudBrowserFrame.this.currentResults = result;
|
CloudBrowserFrame.this.currentResults = result;
|
||||||
getResultsScrollPane().startBatch();
|
getResultsScrollPane().addData(result);
|
||||||
|
|
||||||
for (Iterator<ProjectEntity> i = result.iterator(); i.hasNext();) {
|
|
||||||
ProjectEntity project = i.next();
|
|
||||||
getResultsScrollPane().addProjectToDisplay(project);
|
|
||||||
}
|
|
||||||
// if (count == 0) {
|
|
||||||
// getResultsScrollPane().showNoMatches();
|
|
||||||
// count++;
|
|
||||||
// } else
|
|
||||||
getResultsScrollPane().finishBatch();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private NavigationPanel getNavigationPanel() {
|
|
||||||
if (navigationPanel == null) {
|
|
||||||
navigationPanel = new NavigationPanel();
|
|
||||||
navigationPanel.getPrevPageButton().addActionListener(new ActionListener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (CloudBrowserFrame.this.pageNumber > 1) {
|
|
||||||
setPageNumber(CloudBrowserFrame.this.pageNumber - 1);
|
|
||||||
CloudBrowserFrame.this.search();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
navigationPanel.getNextPageButton().addActionListener(new ActionListener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (CloudBrowserFrame.this.currentResults != null && !CloudBrowserFrame.this.currentResults.isEmpty()) {
|
|
||||||
setPageNumber(CloudBrowserFrame.this.pageNumber + 1);
|
|
||||||
CloudBrowserFrame.this.search();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return navigationPanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setPageNumber(int pageNumber) {
|
|
||||||
this.pageNumber = pageNumber;
|
|
||||||
getNavigationPanel().getPageLabel().setText("Page " + pageNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T extends Object> void executeBackgroundTask(final ITask<T> task) {
|
public <T extends Object> void executeBackgroundTask(final ITask<T> task) {
|
||||||
getGlassPane().setVisible(true);
|
getGlassPane().setVisible(true);
|
||||||
SwingWorker<T, Void> worker = new SwingWorker<T, Void>() {
|
SwingWorker<T, Void> worker = new SwingWorker<T, Void>() {
|
||||||
|
|||||||
+15
-3
@@ -1,9 +1,21 @@
|
|||||||
package org.diylc.swing.plugins.cloud.view.browser;
|
package org.diylc.swing.plugins.cloud.view.browser;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Generic interface for dynamic data provider.
|
||||||
|
*
|
||||||
|
* @author Branislav Stojkovic
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
public interface ILazyProvider<T> {
|
public interface ILazyProvider<T> {
|
||||||
|
|
||||||
boolean hasMore();
|
/**
|
||||||
|
* @return true if there's potentially more data at the source
|
||||||
|
*/
|
||||||
|
boolean hasMoreData();
|
||||||
|
|
||||||
void requestMore();
|
/**
|
||||||
|
* call to request more data. New data batch should be sent asynchronously to this function call.
|
||||||
|
*/
|
||||||
|
void requestMoreData();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
package org.diylc.swing.plugins.cloud.view.browser;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Cursor;
|
|
||||||
import java.awt.GridBagConstraints;
|
|
||||||
import java.awt.GridBagLayout;
|
|
||||||
import java.awt.Insets;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
|
||||||
import javax.swing.JLabel;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.SwingConstants;
|
|
||||||
|
|
||||||
import org.diylc.images.IconLoader;
|
|
||||||
|
|
||||||
public class NavigationPanel extends JPanel {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
private JButton prevPageButton;
|
|
||||||
private JButton nextPageButton;
|
|
||||||
private JLabel pageLabel;
|
|
||||||
|
|
||||||
public NavigationPanel() {
|
|
||||||
super(new GridBagLayout());
|
|
||||||
|
|
||||||
this.setBackground(Color.white);
|
|
||||||
GridBagConstraints gbc = new GridBagConstraints();
|
|
||||||
gbc.anchor = GridBagConstraints.SOUTHWEST;
|
|
||||||
gbc.insets = new Insets(2, 2, 2, 2);
|
|
||||||
gbc.gridx = 0;
|
|
||||||
gbc.weightx = 0;
|
|
||||||
gbc.fill = GridBagConstraints.NONE;
|
|
||||||
this.add(getPrevPageButton(), gbc);
|
|
||||||
|
|
||||||
gbc.gridx++;
|
|
||||||
gbc.weightx = 1;
|
|
||||||
gbc.anchor = GridBagConstraints.LINE_START;
|
|
||||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
||||||
this.add(getPageLabel(), gbc);
|
|
||||||
|
|
||||||
gbc.gridx++;
|
|
||||||
gbc.weightx = 0;
|
|
||||||
gbc.anchor = GridBagConstraints.SOUTHWEST;
|
|
||||||
gbc.fill = GridBagConstraints.NONE;
|
|
||||||
this.add(getNextPageButton(), gbc);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JButton getPrevPageButton() {
|
|
||||||
if (prevPageButton == null) {
|
|
||||||
prevPageButton = new JButton(IconLoader.NavLeftBlue.getIcon());
|
|
||||||
prevPageButton.setBorderPainted(false);
|
|
||||||
prevPageButton.setContentAreaFilled(false);
|
|
||||||
prevPageButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
||||||
}
|
|
||||||
return prevPageButton;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JButton getNextPageButton() {
|
|
||||||
if (nextPageButton == null) {
|
|
||||||
nextPageButton = new JButton(IconLoader.NavRightBlue.getIcon());
|
|
||||||
nextPageButton.setBorderPainted(false);
|
|
||||||
nextPageButton.setContentAreaFilled(false);
|
|
||||||
nextPageButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
|
||||||
}
|
|
||||||
return nextPageButton;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JLabel getPageLabel() {
|
|
||||||
if (pageLabel == null) {
|
|
||||||
pageLabel = new JLabel();
|
|
||||||
pageLabel.setFont(pageLabel.getFont().deriveFont(12f));
|
|
||||||
pageLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
|
||||||
}
|
|
||||||
return pageLabel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+46
-33
@@ -6,6 +6,7 @@ import java.awt.Graphics;
|
|||||||
import java.awt.GridBagConstraints;
|
import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
|
import java.awt.Point;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
@@ -14,11 +15,13 @@ import java.io.FileOutputStream;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.channels.Channels;
|
import java.nio.channels.Channels;
|
||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.swing.Icon;
|
import javax.swing.Icon;
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JComponent;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
@@ -44,17 +47,24 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
private static final Logger LOG = Logger.getLogger(ResultsScrollPanel.class);
|
private static final Logger LOG = Logger.getLogger(ResultsScrollPanel.class);
|
||||||
|
|
||||||
private JPanel resultsPanel;
|
private JPanel resultsPanel;
|
||||||
|
/**
|
||||||
|
* This label goes at the end of the page. When it gets rendered we know that we need to request
|
||||||
|
* another page.
|
||||||
|
*/
|
||||||
|
private JLabel loadMoreLabel;
|
||||||
|
|
||||||
private ISwingUI mainUI;
|
private ISwingUI mainUI;
|
||||||
private CloudBrowserFrame cloudUI;
|
private CloudBrowserFrame cloudUI;
|
||||||
|
|
||||||
private IPlugInPort plugInPort;
|
private IPlugInPort plugInPort;
|
||||||
|
|
||||||
private boolean running;
|
private int currentLocation;
|
||||||
|
/**
|
||||||
|
* This flag tells us whether the loadMoreLabel should invoke a new data pull or not.
|
||||||
|
*/
|
||||||
|
private boolean armed;
|
||||||
private ILazyProvider<ProjectEntity> provider;
|
private ILazyProvider<ProjectEntity> provider;
|
||||||
|
|
||||||
private int location;
|
|
||||||
|
|
||||||
public ResultsScrollPanel(ISwingUI mainUI, CloudBrowserFrame cloudUI, IPlugInPort plugInPort,
|
public ResultsScrollPanel(ISwingUI mainUI, CloudBrowserFrame cloudUI, IPlugInPort plugInPort,
|
||||||
ILazyProvider<ProjectEntity> provider) {
|
ILazyProvider<ProjectEntity> provider) {
|
||||||
super();
|
super();
|
||||||
@@ -68,9 +78,18 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
setViewportView(getResultsPanel());
|
setViewportView(getResultsPanel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
public void startOver() {
|
||||||
|
currentLocation = 0;
|
||||||
getResultsPanel().removeAll();
|
getResultsPanel().removeAll();
|
||||||
running = false;
|
armed = false;
|
||||||
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
|
gbc.anchor = GridBagConstraints.NORTHWEST;
|
||||||
|
gbc.gridx = 0;
|
||||||
|
gbc.gridy = 10000;
|
||||||
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc.weightx = 0;
|
||||||
|
gbc.weighty = 100;
|
||||||
|
resultsPanel.add(getLoadMoreLabel(), gbc);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showNoMatches() {
|
public void showNoMatches() {
|
||||||
@@ -86,12 +105,19 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
getResultsPanel().add(label, gbc);
|
getResultsPanel().add(label, gbc);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startBatch() {
|
public void addData(List<ProjectEntity> projects) {
|
||||||
this.getResultsPanel().remove(getLastLabel());
|
if (projects == null || projects.isEmpty() && currentLocation == 0) {
|
||||||
this.running = true;
|
showNoMatches();
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < projects.size(); i++) {
|
||||||
|
addProjectToDisplay(projects.get(i));
|
||||||
|
this.currentLocation++;
|
||||||
|
}
|
||||||
|
armed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addProjectToDisplay(final ProjectEntity project) {
|
private JComponent addProjectToDisplay(final ProjectEntity project) {
|
||||||
JLabel thumbnailLabel = new JLabel(loadImage(project.getThumbnailUrl()));
|
JLabel thumbnailLabel = new JLabel(loadImage(project.getThumbnailUrl()));
|
||||||
JLabel nameLabel = new JLabel("<html><b>" + project.getName() + "</b></html>");
|
JLabel nameLabel = new JLabel("<html><b>" + project.getName() + "</b></html>");
|
||||||
nameLabel.setFont(nameLabel.getFont().deriveFont(12f));
|
nameLabel.setFont(nameLabel.getFont().deriveFont(12f));
|
||||||
@@ -100,10 +126,6 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
descriptionArea.setFont(thumbnailLabel.getFont());
|
descriptionArea.setFont(thumbnailLabel.getFont());
|
||||||
descriptionArea.setLineWrap(true);
|
descriptionArea.setLineWrap(true);
|
||||||
descriptionArea.setWrapStyleWord(true);
|
descriptionArea.setWrapStyleWord(true);
|
||||||
// JLabel descriptionLabel = new JLabel("<html>" + project.getDescription() + "</html>");
|
|
||||||
// JScrollPane descriptionScroll = new JScrollPane(descriptionArea);
|
|
||||||
// descriptionScroll.setBorder(null);
|
|
||||||
// descriptionLabel.setMaximumSize(new Dimension(200, 100));
|
|
||||||
JLabel categoryLabel = new JLabel("<html>Category: <b>" + project.getCategory() + "</b></html>");
|
JLabel categoryLabel = new JLabel("<html>Category: <b>" + project.getCategory() + "</b></html>");
|
||||||
JLabel authorLabel = new JLabel("<html>Author: <b>" + project.getOwner() + "</b></html>");
|
JLabel authorLabel = new JLabel("<html>Author: <b>" + project.getOwner() + "</b></html>");
|
||||||
JLabel updatedLabel = new JLabel("<html>Last updated: <b>" + project.getUpdated() + "</b></html>");
|
JLabel updatedLabel = new JLabel("<html>Last updated: <b>" + project.getUpdated() + "</b></html>");
|
||||||
@@ -178,7 +200,7 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
gbc.anchor = GridBagConstraints.NORTHWEST;
|
gbc.anchor = GridBagConstraints.NORTHWEST;
|
||||||
gbc.insets = new Insets(2, 2, 2, 2);
|
gbc.insets = new Insets(2, 2, 2, 2);
|
||||||
gbc.gridx = 0;
|
gbc.gridx = 0;
|
||||||
gbc.gridy = location * 6;
|
gbc.gridy = currentLocation * 6;
|
||||||
gbc.fill = GridBagConstraints.NONE;
|
gbc.fill = GridBagConstraints.NONE;
|
||||||
gbc.gridheight = 5;
|
gbc.gridheight = 5;
|
||||||
gbc.weightx = 0;
|
gbc.weightx = 0;
|
||||||
@@ -231,35 +253,26 @@ public class ResultsScrollPanel extends JScrollPane {
|
|||||||
gbc.insets = new Insets(2, 2, 2, 2);
|
gbc.insets = new Insets(2, 2, 2, 2);
|
||||||
getResultsPanel().add(new JSeparator(), gbc);
|
getResultsPanel().add(new JSeparator(), gbc);
|
||||||
|
|
||||||
location++;
|
return thumbnailLabel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finishBatch() {
|
private JLabel getLoadMoreLabel() {
|
||||||
GridBagConstraints gbc = new GridBagConstraints();
|
if (loadMoreLabel == null) {
|
||||||
gbc.anchor = GridBagConstraints.NORTHWEST;
|
loadMoreLabel = new JLabel("Load more...") {
|
||||||
gbc.gridx = 0;
|
|
||||||
gbc.gridy = location * 6;
|
|
||||||
gbc.fill = GridBagConstraints.BOTH;
|
|
||||||
gbc.weightx = 0;
|
|
||||||
gbc.weighty = 100;
|
|
||||||
getResultsPanel().add(getLastLabel(), gbc);
|
|
||||||
}
|
|
||||||
|
|
||||||
private JLabel lastLabel;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private JLabel getLastLabel() {
|
|
||||||
if (lastLabel == null) {
|
|
||||||
lastLabel = new JLabel("Load more...") {
|
|
||||||
@Override
|
@Override
|
||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
if (running && provider.hasMore()) {
|
if (armed && provider.hasMoreData()) {
|
||||||
running = false;
|
// disarm immediately so we don't trigger successive requests to the provider
|
||||||
provider.requestMore();
|
armed = false;
|
||||||
|
provider.requestMoreData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return lastLabel;
|
return loadMoreLabel;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JPanel getResultsPanel() {
|
private JPanel getResultsPanel() {
|
||||||
|
|||||||
Reference in New Issue
Block a user