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:
+7
-61
@@ -7,7 +7,6 @@ import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@@ -43,7 +42,6 @@ public class CloudBrowserFrame extends JFrame {
|
||||
|
||||
private SearchHeaderPanel searchHeaderPanel;
|
||||
private ResultsScrollPanel resultsScrollPane;
|
||||
private NavigationPanel navigationPanel;
|
||||
|
||||
private IPlugInPort plugInPort;
|
||||
private CloudPresenter cloudPresenter;
|
||||
@@ -135,15 +133,6 @@ public class CloudBrowserFrame extends JFrame {
|
||||
gbc.weighty = 1;
|
||||
gbc.fill = GridBagConstraints.BOTH;
|
||||
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;
|
||||
}
|
||||
@@ -155,11 +144,11 @@ public class CloudBrowserFrame extends JFrame {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setPageNumber(1);
|
||||
CloudBrowserFrame.this.pageNumber = 1;
|
||||
CloudBrowserFrame.this.searchFor = getSearchHeaderPanel().getSearchText();
|
||||
CloudBrowserFrame.this.category = getSearchHeaderPanel().getCategory();
|
||||
CloudBrowserFrame.this.sort = getSearchHeaderPanel().getSorting();
|
||||
getResultsScrollPane().clear();
|
||||
getResultsScrollPane().startOver();
|
||||
search();
|
||||
}
|
||||
});
|
||||
@@ -170,15 +159,15 @@ public class CloudBrowserFrame extends JFrame {
|
||||
private ResultsScrollPanel getResultsScrollPane() {
|
||||
if (resultsScrollPane == null) {
|
||||
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, new ILazyProvider<ProjectEntity>() {
|
||||
|
||||
|
||||
@Override
|
||||
public void requestMore() {
|
||||
setPageNumber(CloudBrowserFrame.this.pageNumber + 1);
|
||||
public void requestMoreData() {
|
||||
CloudBrowserFrame.this.pageNumber++;
|
||||
CloudBrowserFrame.this.search();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMore() {
|
||||
public boolean hasMoreData() {
|
||||
return currentResults.size() == itemsPerPage;
|
||||
}
|
||||
});
|
||||
@@ -203,54 +192,11 @@ public class CloudBrowserFrame extends JFrame {
|
||||
@Override
|
||||
public void complete(List<ProjectEntity> result) {
|
||||
CloudBrowserFrame.this.currentResults = result;
|
||||
getResultsScrollPane().startBatch();
|
||||
|
||||
for (Iterator<ProjectEntity> i = result.iterator(); i.hasNext();) {
|
||||
ProjectEntity project = i.next();
|
||||
getResultsScrollPane().addProjectToDisplay(project);
|
||||
}
|
||||
// if (count == 0) {
|
||||
// getResultsScrollPane().showNoMatches();
|
||||
// count++;
|
||||
// } else
|
||||
getResultsScrollPane().finishBatch();
|
||||
getResultsScrollPane().addData(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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) {
|
||||
getGlassPane().setVisible(true);
|
||||
SwingWorker<T, Void> worker = new SwingWorker<T, Void>() {
|
||||
|
||||
+15
-3
@@ -1,9 +1,21 @@
|
||||
package org.diylc.swing.plugins.cloud.view.browser;
|
||||
|
||||
|
||||
/***
|
||||
* Generic interface for dynamic data provider.
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*
|
||||
* @param <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.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Point;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -14,11 +15,13 @@ import java.io.FileOutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.List;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
@@ -44,17 +47,24 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
private static final Logger LOG = Logger.getLogger(ResultsScrollPanel.class);
|
||||
|
||||
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 CloudBrowserFrame cloudUI;
|
||||
|
||||
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 int location;
|
||||
|
||||
public ResultsScrollPanel(ISwingUI mainUI, CloudBrowserFrame cloudUI, IPlugInPort plugInPort,
|
||||
ILazyProvider<ProjectEntity> provider) {
|
||||
super();
|
||||
@@ -68,9 +78,18 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
setViewportView(getResultsPanel());
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
public void startOver() {
|
||||
currentLocation = 0;
|
||||
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() {
|
||||
@@ -86,12 +105,19 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
getResultsPanel().add(label, gbc);
|
||||
}
|
||||
|
||||
public void startBatch() {
|
||||
this.getResultsPanel().remove(getLastLabel());
|
||||
this.running = true;
|
||||
public void addData(List<ProjectEntity> projects) {
|
||||
if (projects == null || projects.isEmpty() && currentLocation == 0) {
|
||||
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 nameLabel = new JLabel("<html><b>" + project.getName() + "</b></html>");
|
||||
nameLabel.setFont(nameLabel.getFont().deriveFont(12f));
|
||||
@@ -100,10 +126,6 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
descriptionArea.setFont(thumbnailLabel.getFont());
|
||||
descriptionArea.setLineWrap(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 authorLabel = new JLabel("<html>Author: <b>" + project.getOwner() + "</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.insets = new Insets(2, 2, 2, 2);
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy = location * 6;
|
||||
gbc.gridy = currentLocation * 6;
|
||||
gbc.fill = GridBagConstraints.NONE;
|
||||
gbc.gridheight = 5;
|
||||
gbc.weightx = 0;
|
||||
@@ -231,35 +253,26 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
gbc.insets = new Insets(2, 2, 2, 2);
|
||||
getResultsPanel().add(new JSeparator(), gbc);
|
||||
|
||||
location++;
|
||||
return thumbnailLabel;
|
||||
}
|
||||
|
||||
public void finishBatch() {
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.anchor = GridBagConstraints.NORTHWEST;
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy = location * 6;
|
||||
gbc.fill = GridBagConstraints.BOTH;
|
||||
gbc.weightx = 0;
|
||||
gbc.weighty = 100;
|
||||
getResultsPanel().add(getLastLabel(), gbc);
|
||||
}
|
||||
private JLabel getLoadMoreLabel() {
|
||||
if (loadMoreLabel == null) {
|
||||
loadMoreLabel = new JLabel("Load more...") {
|
||||
|
||||
private JLabel lastLabel;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private JLabel getLastLabel() {
|
||||
if (lastLabel == null) {
|
||||
lastLabel = new JLabel("Load more...") {
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
if (running && provider.hasMore()) {
|
||||
running = false;
|
||||
provider.requestMore();
|
||||
if (armed && provider.hasMoreData()) {
|
||||
// disarm immediately so we don't trigger successive requests to the provider
|
||||
armed = false;
|
||||
provider.requestMoreData();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return lastLabel;
|
||||
return loadMoreLabel;
|
||||
}
|
||||
|
||||
private JPanel getResultsPanel() {
|
||||
|
||||
Reference in New Issue
Block a user