View Javadoc
1   /******************************************************************************
2    * ContactEngine.java - Base class for the plugin contact making engines
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2005 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.business.contact;
9   
10  import java.awt.Color;
11  import java.awt.Graphics2D;
12  import java.awt.image.BufferedImage;
13  import java.io.File;
14  import java.text.DecimalFormat;
15  import java.util.Iterator;
16  import java.util.LinkedList;
17  import java.util.List;
18  
19  import javax.imageio.ImageIO;
20  import javax.imageio.ImageWriter;
21  import javax.imageio.stream.ImageOutputStream;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import com.buckosoft.PicMan.business.ContactManager;
27  import com.buckosoft.PicMan.business.PicManFacade;
28  import com.buckosoft.PicMan.business.util.CopyFile;
29  import com.buckosoft.PicMan.db.DatabaseFacade;
30  import com.buckosoft.PicMan.domain.Chain;
31  import com.buckosoft.PicMan.domain.ContactParams;
32  import com.buckosoft.PicMan.domain.Pic;
33  import com.buckosoft.PicMan.domain.Set;
34  import com.buckosoft.PicMan.domain.System;
35  import com.buckosoft.PicMan.domain.Thumbnail;
36  
37  /** Base class for the plugin contact making engines.  Make a single contact sheet. <br>
38   * If we detect that someone else is using the PicReader (like a user editing Filters),
39   * then sleep for a minute so we don't slow down the user.
40   * @author Dick Balaska
41   * @since 2005/08/06
42   * @version $Revision: 1.2 $ <br> $Date: 2014/02/13 07:24:02 $
43   * @see <a href="http://cvs.buckosoft.com/Projects/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/contact/ContactEngine.java">ContactEngine.java</a>
44   */
45  public abstract class ContactEngine {
46  	//private final static boolean DEBUG = true;
47  	protected final Log logger = LogFactory.getLog(getClass());
48  
49  	protected	PicManFacade	pmf;
50  	protected	DatabaseFacade	dbf;
51  	protected	System			system;
52  	protected	ContactParams	contactParams;
53  	protected	Set				set;
54  	protected	int				cWidth;
55  	protected	int				cHeight;
56  	protected	String			cName;
57  	protected	String			cPath;		// contains the path up to the extension i.e. C:\temp\PicMan\poster\FirstPoster_000
58  	protected	int				tHeight;
59  	protected	String			cUuid;
60  	protected	Chain			chain;
61  	protected	ContactManager	contactManager;
62  
63  	/** Set the reference to the PicMan API.
64  	 * @param pmf The PicManFacade
65  	 */
66  	public	void	setPicMan(PicManFacade pmf) {
67  		this.pmf = pmf;
68  		this.dbf = pmf.getDB();
69  	}
70  
71  	public void setContactManager(ContactManager contactManager) {
72  		this.contactManager = contactManager;
73  	}
74  
75  	protected abstract boolean _makeContact();
76  	
77  	/** Make a contact sheet based on these parameters.
78  	 * @param contactParams
79  	 * @return true = success
80  	 */
81  	public boolean makeContact(ContactParams contactParams) {
82  		this.contactParams = contactParams;
83  		system = dbf.getSystem();
84  		chain = dbf.getChain(contactParams.getCid());
85  		cUuid	= contactParams.getUuid();
86  		cWidth  = chain.getContactWidth();
87  		cHeight = chain.getContactHeight();
88  		tHeight = contactParams.getSize();
89  		cName = chain.getContactPrefix() + cUuid;
90  		cPath = contactParams.getOutputDirectory() + "/" + cName;
91  		set = dbf.getSet(contactParams.getSetName());
92  		return(_makeContact());
93  	}
94  
95  
96  	protected LinkedList<String> randomize(List<String> in) {
97  		LinkedList<String> out = new LinkedList<String>();
98  		while (!in.isEmpty()) {
99  			double d = Math.random() * in.size();
100 			out.add(in.remove((int)d));
101 		}
102 		return(out);
103 	}
104 
105 	private final DecimalFormat df = new DecimalFormat("0.##");
106 
107 	protected int drawThumb(Graphics2D g, String tName, int tHeight, int x, int y) {
108 		Pic			pic;
109 		Thumbnail	tn;
110 		pic = pmf.getDB().getPic(tName);
111 		if (pmf.getPicReader().getQueueDepth() > 0) {
112 			logger.info("Queue depth " + pmf.getPicReader().getQueueDepth() + " --- sleeping");
113 			try {
114 				java.lang.Thread.sleep(3000);
115 			} catch (InterruptedException e) {
116 			}
117 		}
118 		String label = null;
119 		if (system.isShowRateOnPics())
120 			label = df.format(dbf.getPicRate(tName, set, contactParams.getSize()));
121 		tn = pmf.getPicReader().getThumbNail(pic, tHeight, label);
122 		if (tn.isXThumb() && system.isSkipXThumbs())
123 			return(0);
124 		g.drawImage(tn.getImage(), null, x, y);		
125 		return(tn.getImage().getWidth());
126 	}
127 	
128 	protected void drawContactLabel(Graphics2D g) {
129 		int xp = 300;
130 		int yp = 12;
131 		String	text = chain.getContactPrefix() + cUuid;
132 		g.setColor(Color.BLACK);
133 		g.drawString(text, xp+1, yp+1);
134 		g.setColor(Color.WHITE);
135 		g.drawString(text, xp,   yp);
136 	}
137 	
138 	protected boolean isAbort() {
139 		return(this.contactManager.isAbort());
140 	}
141 	
142 	protected boolean writePic(BufferedImage bi, boolean makeBackup) {
143 		Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
144 		ImageWriter writer = (ImageWriter)writers.next();
145 		File file = new File(cPath + ".jpg");
146 		logger.info("Writing jpg to '" + file.getPath() + "'");
147 		try {
148 			ImageOutputStream oos = ImageIO.createImageOutputStream(file);
149 			writer.setOutput(oos);
150 			writer.write(bi);
151 			oos.close();
152 		} catch (Exception e) {
153 			bi = null;
154 			pmf.addError(e);
155 			return(false);
156 		}
157 		if (makeBackup)
158 			CopyFile.makeBackup(pmf, file);
159 		logger.info("Done with " + cUuid);
160 		return(true);
161 	}
162 }