mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
lazy loading
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package org.diylc.plugins.cloud.model;
|
||||
|
||||
public class CountryDetails {
|
||||
|
||||
private String country;
|
||||
private String details;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.diylc.plugins.cloud.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DashboardEntity {
|
||||
|
||||
private List<ProjectEntity> latestProjects;
|
||||
private List<ProjectEntity> mostDownloadedProjects;
|
||||
private List<UserEntity> topContributors;
|
||||
private List<CountryDetails> contributorsByCountry;
|
||||
private List<CountryDetails> visitorsByCountry;
|
||||
}
|
||||
@@ -371,7 +371,7 @@ public class MainFrame extends JFrame implements ISwingUI {
|
||||
fileName = "Untitled";
|
||||
}
|
||||
String modified = (Boolean) params[1] ? " (modified)" : "";
|
||||
setTitle(String.format("DIYLC G3 release %s.%s - %s %s", plugInPort.getCurrentVersionNumber().getMinor(), plugInPort.getCurrentVersionNumber().getBuild(), fileName, modified));
|
||||
setTitle(String.format("DIYLC G3 version %s.%s - %s %s", plugInPort.getCurrentVersionNumber().getMinor(), plugInPort.getCurrentVersionNumber().getBuild(), fileName, modified));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
|
||||
|
||||
public LibraryAction() {
|
||||
super();
|
||||
putValue(AbstractAction.NAME, "Browse The Cloud");
|
||||
putValue(AbstractAction.NAME, "Search The Cloud");
|
||||
putValue(AbstractAction.SMALL_ICON, IconLoader.Cloud.getIcon());
|
||||
}
|
||||
|
||||
|
||||
+31
-19
@@ -60,7 +60,7 @@ public class CloudBrowserFrame extends JFrame {
|
||||
private ISwingUI swingUI;
|
||||
|
||||
public CloudBrowserFrame(ISwingUI swingUI, IPlugInPort plugInPort, CloudPresenter cloudPresenter) {
|
||||
super("Cloud Surfer");
|
||||
super("Search The Cloud");
|
||||
this.swingUI = swingUI;
|
||||
this.setIconImage(IconLoader.Cloud.getImage());
|
||||
this.setPreferredSize(new Dimension(700, 640));
|
||||
@@ -136,14 +136,14 @@ public class CloudBrowserFrame extends JFrame {
|
||||
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);
|
||||
// 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;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public class CloudBrowserFrame extends JFrame {
|
||||
CloudBrowserFrame.this.searchFor = getSearchHeaderPanel().getSearchText();
|
||||
CloudBrowserFrame.this.category = getSearchHeaderPanel().getCategory();
|
||||
CloudBrowserFrame.this.sort = getSearchHeaderPanel().getSorting();
|
||||
|
||||
getResultsScrollPane().clear();
|
||||
search();
|
||||
}
|
||||
});
|
||||
@@ -169,7 +169,19 @@ public class CloudBrowserFrame extends JFrame {
|
||||
|
||||
private ResultsScrollPanel getResultsScrollPane() {
|
||||
if (resultsScrollPane == null) {
|
||||
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort);
|
||||
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, new ILazyProvider<ProjectEntity>() {
|
||||
|
||||
@Override
|
||||
public void requestMore() {
|
||||
setPageNumber(CloudBrowserFrame.this.pageNumber + 1);
|
||||
CloudBrowserFrame.this.search();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMore() {
|
||||
return currentResults.size() == itemsPerPage;
|
||||
}
|
||||
});
|
||||
}
|
||||
return resultsScrollPane;
|
||||
}
|
||||
@@ -191,17 +203,17 @@ public class CloudBrowserFrame extends JFrame {
|
||||
@Override
|
||||
public void complete(List<ProjectEntity> result) {
|
||||
CloudBrowserFrame.this.currentResults = result;
|
||||
getResultsScrollPane().clear();
|
||||
int count = 0;
|
||||
getResultsScrollPane().startBatch();
|
||||
|
||||
for (Iterator<ProjectEntity> i = result.iterator(); i.hasNext();) {
|
||||
ProjectEntity project = i.next();
|
||||
getResultsScrollPane().addProjectToDisplay(project, count++);
|
||||
getResultsScrollPane().addProjectToDisplay(project);
|
||||
}
|
||||
if (count == 0) {
|
||||
getResultsScrollPane().showNoMatches();
|
||||
count++;
|
||||
} else
|
||||
getResultsScrollPane().finish();
|
||||
// if (count == 0) {
|
||||
// getResultsScrollPane().showNoMatches();
|
||||
// count++;
|
||||
// } else
|
||||
getResultsScrollPane().finishBatch();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.diylc.swing.plugins.cloud.view.browser;
|
||||
|
||||
|
||||
public interface ILazyProvider<T> {
|
||||
|
||||
boolean hasMore();
|
||||
|
||||
void requestMore();
|
||||
}
|
||||
+38
-10
@@ -2,7 +2,7 @@ package org.diylc.swing.plugins.cloud.view.browser;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
@@ -50,11 +50,18 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
|
||||
private IPlugInPort plugInPort;
|
||||
|
||||
public ResultsScrollPanel(ISwingUI mainUI, CloudBrowserFrame cloudUI, IPlugInPort plugInPort) {
|
||||
private boolean running;
|
||||
private ILazyProvider<ProjectEntity> provider;
|
||||
|
||||
private int location;
|
||||
|
||||
public ResultsScrollPanel(ISwingUI mainUI, CloudBrowserFrame cloudUI, IPlugInPort plugInPort,
|
||||
ILazyProvider<ProjectEntity> provider) {
|
||||
super();
|
||||
this.mainUI = mainUI;
|
||||
this.cloudUI = cloudUI;
|
||||
this.plugInPort = plugInPort;
|
||||
this.provider = provider;
|
||||
|
||||
this.getVerticalScrollBar().setUnitIncrement(16);
|
||||
this.setBorder(null);
|
||||
@@ -63,6 +70,7 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
|
||||
public void clear() {
|
||||
getResultsPanel().removeAll();
|
||||
running = false;
|
||||
}
|
||||
|
||||
public void showNoMatches() {
|
||||
@@ -78,7 +86,12 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
getResultsPanel().add(label, gbc);
|
||||
}
|
||||
|
||||
public void addProjectToDisplay(final ProjectEntity project, int location) {
|
||||
public void startBatch() {
|
||||
this.getResultsPanel().remove(getLastLabel());
|
||||
this.running = true;
|
||||
}
|
||||
|
||||
public void 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));
|
||||
@@ -111,7 +124,7 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
|
||||
@Override
|
||||
public Void doInBackground() throws Exception {
|
||||
LOG.debug("Downloading project to to " + file.getAbsolutePath());
|
||||
LOG.debug("Downloading project to " + file.getAbsolutePath());
|
||||
URL website = new URL(project.getDownloadUrl());
|
||||
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
|
||||
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
|
||||
@@ -218,20 +231,35 @@ public class ResultsScrollPanel extends JScrollPane {
|
||||
gbc.insets = new Insets(2, 2, 2, 2);
|
||||
getResultsPanel().add(new JSeparator(), gbc);
|
||||
|
||||
this.lastGridY = gbc.gridy;
|
||||
location++;
|
||||
}
|
||||
|
||||
private int lastGridY;
|
||||
|
||||
public void finish() {
|
||||
public void finishBatch() {
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.anchor = GridBagConstraints.NORTHWEST;
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy = lastGridY;
|
||||
gbc.gridy = location * 6;
|
||||
gbc.fill = GridBagConstraints.BOTH;
|
||||
gbc.weightx = 0;
|
||||
gbc.weighty = 100;
|
||||
getResultsPanel().add(new JLabel(), gbc);
|
||||
getResultsPanel().add(getLastLabel(), gbc);
|
||||
}
|
||||
|
||||
private JLabel lastLabel;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return lastLabel;
|
||||
}
|
||||
|
||||
private JPanel getResultsPanel() {
|
||||
|
||||
Reference in New Issue
Block a user