View Javadoc
1   /******************************************************************************
2    * SequentialSets.java - Make a list of random sets sorted by pictures within each set
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2011 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.business.contact.engine;
9   
10  import java.awt.Graphics2D;
11  import java.awt.image.BufferedImage;
12  import java.util.ArrayList;
13  import java.util.Collections;
14  import java.util.Date;
15  import java.util.HashMap;
16  import java.util.LinkedList;
17  import java.util.List;
18  
19  import com.buckosoft.PicMan.domain.Contact;
20  import com.buckosoft.PicMan.domain.Pic;
21  
22  /** Make a contact that contains a list of random sets sorted by pictures within each set
23   * @author Dick Balaska
24   * @since 2011/12/22
25   * @version $Revision: 1.2 $ <br> $Date: 2014/02/13 07:24:02 $
26   * @see <a href="http://cvs.buckosoft.com/Projects/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/contact/engine/SequentialSets.java">SequentialSets.java</a>
27   */
28  public class SequentialSets extends QuadrantSets {
29  	private	boolean	DEBUG = false;
30  	private int	quadrantRows = 1;
31  	private int	quadrantCols = 1;
32  	private int numQuadrants = quadrantRows * quadrantCols;
33  	private String[] workingQuadrantDirs = new String[numQuadrants];
34  	private ArrayList<LinkedList<String>> workingQuadrantFiles;
35  	private LinkedList<String> dirSets;
36  	private	List<String> picsInContact;
37  	private	Graphics2D g;
38  //	private	Font	
39  	private Contact c;
40  	private int[] picInRow;
41  	private	int[] colInRow;
42  	
43  
44  	/** Make a contact sheet based on the setup.
45  	 * @return Success
46  	 */
47  	protected	boolean	_makeContact() {
48  
49  		boolean	success = true;
50  		
51  		logger.info("makeContact '" + cName + "'");
52  
53  //		picsInContact = pmf.getDB().getPicNamesBySet(contactParams.getSetName(), contactParams.getSize());
54  //		picsInContact = setupPicsList(contactParams, picsInContact);
55  		picsInContact = this.contactManager.getPicNames(contactParams);
56  		Collections.sort(picsInContact);
57  		logger.info("select from " + picsInContact.size() + " filters");
58  		if (picsInContact.size() == 0) {
59  			RuntimeException e = new RuntimeException("No pics to make contact " + contactParams.getUuid());
60  			pmf.addError(e);
61  			return(false);
62  		}
63  		HashMap<String, Integer> dirSetsHash = new HashMap<String, Integer>();
64  		Pic			pic;
65  		// figure out all of the dirSets that are available
66  		for (String s : picsInContact) {
67  			pic = pmf.getDB().getPic(s);
68  			Integer x = dirSetsHash.get(pic.getLocation());
69  			if (x == null)
70  				x = 0;
71  			x++;
72  			dirSetsHash.put(pic.getLocation(), x);
73  		}
74  		dirSets = randomize(new LinkedList<String>(dirSetsHash.keySet()));
75  		
76  		if (logger.isDebugEnabled()) {
77  			for (String key : dirSets) {
78  				logger.debug("dirSet " + key);
79  			}
80  		}
81  		// pick the initial sets to work with, one for each quadrant.
82  		workingQuadrantFiles = new ArrayList<LinkedList<String>>();
83  		for (int i=0; i<numQuadrants; i++) {
84  			workingQuadrantFiles.add(null);
85  			restock(i);
86  		}
87  
88  		BufferedImage bi = new BufferedImage(cWidth, cHeight, BufferedImage.TYPE_3BYTE_BGR);
89  		g = bi.createGraphics();
90  		c = new Contact();
91  		c.setName(cUuid);
92  		c.setStartTime(new Date());
93  		c.setCid(chain.getCid());
94  		int	rowCount = cHeight/tHeight;
95  		int quadrantRowCount = rowCount / quadrantRows;
96  		colInRow = new int[rowCount];
97  		picInRow = new int[rowCount];
98  		try {
99  			int curQuadrant = 0;
100 			int	colWidth = cWidth / quadrantCols;
101 			for (int qr=0; qr < quadrantRows; qr++) {
102 				for (int qc=0; qc<quadrantCols; qc++) {
103 					if (DEBUG)
104 						logger.debug("qr=" + qr + " qc=" + qc + " curQuadrant=" + curQuadrant);
105 					fillQuadrant(qr*quadrantRowCount, (qr+1)*quadrantRowCount, (qc+1)*colWidth, curQuadrant);
106 					curQuadrant++;
107 				}
108 			}
109 			drawContactLabel(g);
110 		} catch (Exception e) {
111 			logger.error("Error drawing contact", e);
112 			pmf.addError(e);
113 			return(false);
114 			
115 		}
116 		success = writePic(bi, false);
117 		c.setEndTime(new Date());
118 		pmf.getDB().addContact(c);
119 		logger.info("Done with " + cUuid);
120 		return(success);
121 	}
122 
123 	private void fillQuadrant(int curRow, int lrow, int lcol, int curQuadrant) {
124 		String		tName;
125 		if (DEBUG)
126 			logger.debug("curRow=" + curRow + " lrow=" + lrow + " lcol=" + lcol + " curQuadrant=" + curQuadrant);
127 		while (curRow < lrow) {
128 			while (colInRow[curRow] < lcol) {
129 				if (workingQuadrantFiles.get(curQuadrant).isEmpty())
130 					restock(curQuadrant);
131 				tName = (String)workingQuadrantFiles.get(curQuadrant).pop();
132 				if (DEBUG)
133 					logger.debug(tName + " col/row = " + colInRow[curRow] + "/" + curRow);
134 				int widthDrawn = drawThumb(g, tName, tHeight, colInRow[curRow], curRow*tHeight);
135 				c.setPic(tName, curRow, picInRow[curRow]);
136 				
137 				colInRow[curRow] += widthDrawn;
138 				picInRow[curRow]++;
139 			}
140 			curRow++;
141 		}
142 	}
143 
144 	private	void	restock(int i) {
145 		workingQuadrantDirs[i] = dirSets.pop();
146 		dirSets.add(workingQuadrantDirs[i]);
147 		workingQuadrantFiles.set(i, fillQuadrantFiles(workingQuadrantDirs[i]));		
148 	}
149 
150 	private	LinkedList<String> fillQuadrantFiles(String dir) {
151 		LinkedList<String> list = new LinkedList<String>();
152 		for (String s : picsInContact) {
153 			Pic pic = pmf.getDB().getPic(s);
154 			if (pic.getLocation().equals(dir))
155 				list.add(pic.getName());
156 		}
157 		Collections.sort(list);
158 		return(list);
159 	}
160 }