Skip to content

Instantly share code, notes, and snippets.

@shannah
Created June 24, 2014 21:08
Show Gist options
  • Save shannah/c825895c009d500a0e53 to your computer and use it in GitHub Desktop.
Save shannah/c825895c009d500a0e53 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ca.weblite.comiccl.view;
import ca.weblite.comiccl.model.Collection;
import ca.weblite.comiccl.model.Issue;
import com.codename1.ui.CheckBox;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.List;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.list.ListCellRenderer;
import com.codename1.ui.util.UITimer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
* @author shannah
*/
public class IssuesListRenderer extends Container implements ListCellRenderer {
EncodedImage coverArt;
Label coverArtIcon = new Label("");
Label titleLabel = new Label("");
CheckBox cb;
Set<Image> pendingImages = new HashSet<Image>();
UITimer timer = null;
private int hdips(int pixels){
return Display.getInstance().convertToPixels(pixels, true);
}
private int vdips(int pixels){
return Display.getInstance().convertToPixels(pixels, false);
}
public IssuesListRenderer(){
this.setLayout(new BorderLayout());
this.addComponent(BorderLayout.WEST, coverArtIcon);
this.addComponent(BorderLayout.CENTER, titleLabel);
this.setUIID("IssuesListCell");
cb = new CheckBox();
this.addComponent(BorderLayout.EAST, cb);
int prefWidth = Issue.getPreferredCoverArtWidth();
if ( prefWidth >= 400 ){
this.setPreferredH(4*160);
} else if ( prefWidth >= 200){
this.setPreferredH(2*160);
} else {
this.setPreferredH(160);
}
//this.getStyle().setPadding(3, 3, 100/hdips(1), 5);
cb.setHeight(150);
cb.setWidth(hdips(15));
coverArtIcon.setIcon(Issue.getPlaceholderImage());
//this.calcPreferredSize();
}
public Component getListCellRendererComponent(final List list, Object value, int index, boolean isSelected) {
Map m = (Map)value;
Issue issue = (Issue)m.get("issue");
Collection coll = (Collection)m.get("collection");
titleLabel.setText(issue.toString());
coverArt = (EncodedImage)issue.getCoverImage();
coverArtIcon.setIcon(coverArt);
cb.setSelected(coll.hasIssue(issue));
if ( coverArt != null && coverArt.getImageData() == null){
//Log.p("Need to refresh cover art ");
pendingImages.add(coverArt);
if ( timer == null ){
timer = new UITimer(new Runnable(){
public void run() {
if ( !pendingImages.isEmpty()){
ArrayList<Image> imgs = new ArrayList<Image>(pendingImages);
boolean needsUpdate = false;
for ( Image img : imgs ){
if ( img.animate() ){
pendingImages.remove(img);
needsUpdate = true;
}
}
if ( needsUpdate ){
list.repaint();
}
if ( pendingImages.isEmpty()){
if ( timer != null ){
timer.cancel();
timer = null;
}
}
}
}
});
timer.schedule(200, true, list.getComponentForm());
}
}
return this;
}
public Component getListFocusComponent(List list) {
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment