/* @(#)PicturePages.java 1.0 20 September 2004 */
/* Useful imports */
import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
// import java.text.*;
public class PicturePages extends JPF
{
public static void main(String[] args) {
// To optionally adjust the look and feel,
// remove the comments from one of the two statements below.
// LookAndFeelTools.showSelectLookAndFeelDialog();
LookAndFeelTools.adjustAllDefaultFontSizes(10);
new PicturePages();
}
// modify these definitions for new picture pages
/** The image directory. */
private String directory = "C:\\Images\\Oregon2004";
/** The web site title. */
private String pagetitle = "Sue and Richard in Oregon 2004";
/** The list of image file names in the image directory. */
private String[] files = new String[0];
/** The length of the files list. */
private int length = 0;
// constants
/** The doctype string. */
private final String doctype =
"\n";
/** The css link string. */
private final String csslink =
"";
/** The html string. */
private final String html = "\n";
/** The end html string. */
private final String endhtml = "\n";
/** The head string. */
private final String head = "
\n";
/** The end head string. */
private final String endhead = "\n";
/** The title string. */
private final String title = "\n";
/** The end title string. */
private final String endtitle = "\n\n";
/** The body string. */
private final String body = "\n";
/** The end body string. */
private final String endbody = "\n";
/** The paragraph string. */
private final String paragraph = "
\n";
/** The end paragraph string. */
private final String endparagraph = "
\n";
/** The open link string. */
private final String openlink = "";
/** The end link string. */
private final String endlink = "\n";
/** The open image string. */
private final String openimage = "";
/** The original web site maker that uses a fixed directory. */
public void makePictureWebSite() {
readImageFileNames(directory);
createPictureWebSite();
}
/** The method that reads the file names of the image files. */
private void readImageFileNames(final String directory) {
// define filter to pick out .jpg and .gif files
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name == null)
return false;
return name.startsWith(".jpg", name.length() - 4)
|| name.startsWith(".gif", name.length() - 4);
}
};
// open File object to access the directory with images
File imagesDirectory = new File(directory);
// obtain file names in directory that are .jpg or .gif
files = imagesDirectory.list(filter);
// save count of file names
length = files.length;
}
/** Extracts a picture label from an image file name. */
private String refineFileName(String name) {
// eliminate .jpg or .gif from name
name = name.substring(0, name.length() - 4);
// use tokenizer to eliminate digits and underscores
StringTokenizer tokenizer =
new StringTokenizer(name, "0123456789_");
// the text we want will be what is left in the name
name = tokenizer.nextToken();
// add blanks before any uppercase character in name
// use StringBuffer to collect characters
StringBuffer buffer = new StringBuffer(128);
int length = name.length();
if (length == 0)
return name;
buffer.append(name.charAt(0));
for (int i = 1; i < length; i++) {
char c = name.charAt(i);
if (Character.isUpperCase(c))
buffer.append(' ');
buffer.append(c);
}
// convert the buffer back to a string and return it
name = buffer.toString();
return name;
}
/** Create the web site by creating each page. */
private void createPictureWebSite() {
for (int i = 0; i < length; i++)
createPictureWebPage(i);
console.out.println("Web Site Done: " + length + " Files");
}
/** Create the i-th page of the web site. */
private void createPictureWebPage(int i) {
// use StringBuffer to collect characters
StringBuffer buffer = new StringBuffer(1024);
// standard web page opening
buffer.append(doctype);
buffer.append(html);
// web page head
buffer.append(head);
buffer.append(title);
buffer.append(pagetitle);
buffer.append(endtitle);
buffer.append(csslink);
buffer.append(endhead);
// web page body
buffer.append(body);
appendSideLinks(buffer, i);
makePictureLink(buffer, files[i]);
buffer.append(endbody);
// standard web page closing
buffer.append(endhtml);
// convert the buffer back to a string to get page text
String page = buffer.toString();
// send the i-th page text to the i-th web site file
try {
// use pattern: open file; write page; close file
FileWriter writer = new FileWriter(createWebPagePathName(i));
writer.write(page);
writer.close();
}
catch (IOException ex) {
// print messages if IO errors happen
console.out.println("IO failure for web page: " + i);
console.out.println("Message: " + ex.getMessage());
// exit since things are not OK
throw new RuntimeException("Exiting due to IO Failure");
}
}
/**
* Append to the buffer the html code that links one picture page to
* the first page, the back page, the next page, and the last page.
*/
private void appendSideLinks(StringBuffer buffer, int i) {
// start paragraph
buffer.append(paragraph);
// make link to first page unless we are the first page
if (i > 0)
linkTo(buffer, "First", 0);
else
buffer.append("First");
buffer.append("\n");
// make link to back page unless we are the first page
if (i > 0)
linkTo(buffer, "Back", (i-1));
else
buffer.append("Back");
buffer.append("\n");
// make link to next page unless we are the last page
if (i < (length - 1))
linkTo(buffer, "Next", (i+1));
else
buffer.append("Next");
buffer.append("\n");
// make link to last page unless we are the last page
if (i < (length - 1))
linkTo(buffer, "Last", (length - 1));
else
buffer.append("Last");
buffer.append("\n");
// finish paragraph
buffer.append(endparagraph);
}
/** Create html for a link to the k-th page with given link text. */
private void linkTo(StringBuffer buffer, String text, int k) {
if ((k < 0) || (k >= length))
return;
buffer.append(openlink);
buffer.append(createWebPageName(k));
buffer.append(shutlink);
buffer.append(text);
buffer.append(endlink);
}
/**
* Create the file name for the i-th web page.
*
* If i equals zero use "index.htm".
*
* If i is positive use "pictureXXX.htm" where XXX is the three
* digit represention of i in digit characters.
*/
private String createWebPageName(int i) {
// for i equal to zero return "index.htm"
if (i == 0)
return "index.htm";
// otherwise build "pictureXXX.htm"
StringBuffer buffer = new StringBuffer(128);
buffer.append("picture");
buffer.append(threeChars(i));
buffer.append(".htm");
return buffer.toString();
}
/** Create the full path name for the i-th web page. */
private String createWebPagePathName(int i) {
StringBuffer buffer = new StringBuffer(128);
buffer.append(directory);
buffer.append("\\");
buffer.append(createWebPageName(i));
return buffer.toString();
}
/** Returns the integer i represented as XXX. */
private String threeChars(int i) {
if (i < 10)
return "00" + i;
if (i < 100)
return "0" + i;
return "" + i;
}
/** Create html for the link to the image and for its caption. */
private void makePictureLink(StringBuffer buffer, String name) {
String caption = refineFileName(name);
buffer.append(paragraph);
buffer.append(openimage);
buffer.append(name);
buffer.append(shutimage);
buffer.append(endparagraph);
buffer.append(paragraph);
buffer.append(caption);
buffer.append(endparagraph);
}
/** Test the reading of file names. */
private void printRawFileNames() {
console.out.println(directory + "\n");
console.out.println(length + "\n");
for (int i = 0; i < length; i++)
console.out.println(files[i]);
}
/** Test the conversion of file names to picture labels. */
private void printRefinedFileNames() {
console.out.println(directory + "\n");
console.out.println(length + "\n");
for (int i = 0; i < length; i++)
console.out.println(refineFileName(files[i]));
}
/** Build a GUI to input directory and page title. */
public void makePictureWebSiteGUI() {
final TextFieldView directoryView = new TextFieldView(600);
final TextFieldView pagetitleView = new TextFieldView(600);
TablePanel viewPanel =
new TablePanel(
new Object[][] {
{ "Images Directory" , directoryView },
{ "Web Page Title", pagetitleView } },
10, 10, WEST);
SimpleAction makeSite = new SimpleAction("Make Web Site") {
public void perform() {
directory = directoryView.getViewState();
pagetitle = pagetitleView.getViewState();
makePictureWebSite();
}
};
TablePanel mainPanel =
new TablePanel(
new Object[] { viewPanel, makeSite },
VERTICAL, 10, 10, CENTER);
JPTFrame.createQuickJPTFrame("Make Images Web Site", mainPanel);
}
}