Skip to content

Instantly share code, notes, and snippets.

@shannah
Created January 9, 2016 17:15
Show Gist options
  • Save shannah/6e420de673a4d6815a36 to your computer and use it in GitHub Desktop.
Save shannah/6e420de673a4d6815a36 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 com.codename1.test;
import com.codename1.contacts.Contact;
import com.codename1.contacts.ContactsManager;
import com.codename1.ui.Button;
import com.codename1.ui.CheckBox;
import com.codename1.ui.Command;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.TextField;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.table.TableLayout;
import com.codename1.ui.util.Resources;
/**
*
* @author Chen
*/
public class ContactsForm extends Form {
private Resources res;
public ContactsForm(Resources res) {
this.res = res;
initUI();
}
protected void initUI() {
setTitle("ContactsForm.title");
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
setScrollableY(true);
CheckBox cb = new CheckBox("Debug");
cb.addActionListener((evt)->{
if (cb.isSelected()) {
Display.getInstance().setProperty("debug", "1");
} else {
Display.getInstance().setProperty("debug", "0");
}
});
addComponent(cb);
Component compWithLine = new Component() {
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(0x0000ff);
g.drawLine(getX(), getY(), getX()+getWidth(), getY()+getHeight());
}
@Override
protected Dimension calcPreferredSize() {
return new Dimension(100,100);
}
};
addComponent(compWithLine);
setContacts(this, Test.contacts);
}
public void setContacts(Form f, Contact[] contacts) {
Image[] bg = new Image[4];
bg[0] = res.getImage("blue.png");
bg[1] = res.getImage("purple.png");
bg[2] = res.getImage("red.png");
bg[3] = res.getImage("yellow.png");
Image roundMask = Image.createImage(bg[0].getWidth(), bg[0].getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, bg[0].getWidth(), bg[0].getHeight(), 0, 360);
Object mask = roundMask.createMask();
if(contacts.length > 0){
Contact contact1 = contacts[0];
f.add(createContactComponent(f, contact1, bg[0], mask, true));
for (int i = 1; i < contacts.length; i++) {
contact1 = contacts[i-1];
Contact contact = contacts[i];
f.add(createContactComponent(f, contact, bg[i % 4], mask, contact1.getDisplayName().charAt(0) != contact.getDisplayName().charAt(0)));
}
}
}
private static Component createContactComponent(Form f, final Contact contact, Image bg, Object roundMask, boolean displayFirstLetter) {
String fname = contact.getFirstName();
String lname = contact.getFamilyName();
String initialsStr = "";
String display = "";
if (fname != null && fname.length() > 0) {
display += fname;
initialsStr += fname.charAt(0);
}
if (lname != null && lname.length() > 0) {
display += " " + lname;
initialsStr += lname.charAt(0);
}
if (display.length() == 0) {
display = contact.getDisplayName();
}
if (display.length() == 0) {
display = "Unnamed Contact";
}
Container cnt = new Container(new BorderLayout());
cnt.setUIID("ContactCnt");
Container west = new Container(new LayeredLayout());
west.setUIID("ContactIconCnt");
Image photo = contact.getPhoto();
bg.lock();
if (photo != null) {
photo = photo.scaled(bg.getWidth(), bg.getHeight());
photo = photo.applyMask(roundMask);
photo.lock();
Label icon = new Label(photo);
//Label icon = new Label(" ");
west.add(icon);
} else {
Label icon = new Label(bg);
//Label icon = new Label(" ");
Label initials = new Label(initialsStr);
initials.setUIID("ContactInitials");
west.add(icon).add(initials);
}
cnt.add(BorderLayout.WEST, west);
Container center = new Container(new BorderLayout());
center.setUIID("ContactNameCnt");
Label name = new Label(display);
name.setUIID("ContactName");
center.add(BorderLayout.CENTER, name);
cnt.add(BorderLayout.CENTER, center);
Button lead = new Button();
lead.setUIID("Container");
cnt.add(BorderLayout.EAST, lead);
cnt.setLeadComponent(lead);
Container b = new Container(new BorderLayout());
b.setUIID("ContainerWhite");
b.putClientProperty("contact_name", display);
Label letter;
if(displayFirstLetter){
letter = new Label(("" + display.charAt(0)).toUpperCase());
}else{
letter = new Label(" ");
}
letter.setUIID("Letter");
letter.setPreferredW(f.getWidth()*10/100);
b.add(BorderLayout.WEST, letter);
b.add(BorderLayout.CENTER, cnt);
return b;
}
}
package com.codename1.test;
import com.codename1.contacts.Contact;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import java.io.IOException;
import java.util.ArrayList;
public class Test {
private Form current;
private Resources theme;
public static Contact[] contacts;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
UIManager.getInstance().setBundle(theme.getL10N("Localization (L10N) 1", "en"));
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
System.out.println((3 * 3) / 2);
contacts = Display.getInstance().getAllContacts(true, true, true, false, false, false);
ArrayList<Contact> clist = new ArrayList<Contact>();
for (int i=0; i<contacts.length; i++) {
for (int j=0; j<15; j++) {
clist.add(contacts[i]);
}
//clist.add(contacts[i]);
//clist.add(contacts[i]);
//clist.add(contacts[i]);
//clist.add(contacts[i]);
//clist.add(contacts[i]);
}
contacts = clist.toArray(new Contact[clist.size()]);
System.out.println("Contact size "+clist.size());
new ContactsForm(theme).show();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment