package unoofc.demo;

import java.io.*;
import java.util.*;
import unoofc.*;

/* This class demonstrates Writer document programming using unoofc. The program builds an
 * Writer document by inserting paragraphs, a text file, a text frame, and a table. The program 
 * also creates and applies styles, uses cursors and text fields, and does a text search. The 
 * single input argument is the pathname of the unoofc.demo folder. This folder contains input files
 * required by this program, and the Writer document, unoofc.odt, is written to this folder. 
 * The program also writes logging information to the console.
 */
public class OWriterDocDemo {
	private OWriterDoc iDoc = null;

	public static void main(String[] args) {
		try {
			File folder = new File(args[0]);
			OWriterDocDemo demo = new OWriterDocDemo();
			demo.doParas(folder);
			demo.insertTextFrame();
			demo.insertBitmap(folder);
			demo.insertTable();
			demo.useCursor();
			demo.end(folder);
		} catch (java.lang.Exception xcp) {
			xcp.printStackTrace();
		}
	}
	
	private OWriterDocDemo() throws Exception {
		// create empty document
		iDoc = new OWriterDoc(null);
	}
	
	private void doParas(File folder) throws Exception {
		// using default page style, add para with default style
		iDoc.addText("First sentence of paragraph 1.");
		iDoc.addText(" This paragraph has style 'Default Style'.");
		iDoc.endPara();
		iDoc.addText("First sentence of paragraph 2.");
		iDoc.addText(" This paragraph has style 'Default Style'.");
		iDoc.addText(" Next sentence of paragraph 2.");
				
		// add para with "Heading 1" style
		OStyle.Para heading1 = new OStyle.Para(iDoc, "Heading 1");
		iDoc.startPara(heading1);
		iDoc.addText("First sentence of paragraph 3.");
		iDoc.addText(" This paragraph has style 'Heading 1'.");
		iDoc.addText(" Next sentence of paragraph 3.");
		iDoc.addText(" Next sentence of paragraph 3.");
		
		// create para style "Demo" as a child of style "Heading 1" but with font "Courier New"
		// and add a para
		OStyle.Para paraDemo = new OStyle.Para(iDoc, "Demo");
		paraDemo.setParentStyle(heading1.name());
		paraDemo.setProp(OProps.Char.CharFontName, "Courier New");
		iDoc.startPara(paraDemo);
		iDoc.addText("First sentence of paragraph 4.");
		iDoc.addText(" This paragraph has style 'Demo'.");
		
		// set "Text Body" para style to justified alignment and spacing below paras to 3mm
		OStyle.Para textBody = new OStyle.Para(iDoc, "Text Body");
		textBody.setProp(OProps.Para.ParaAdjust, com.sun.star.style.ParagraphAdjust.BLOCK);
		textBody.setProp(OProps.Para.ParaBottomMargin, OUtil.loUnitsFromMM(3));
		iDoc.startPara(textBody);
		
		// change page style to "Landscape" and add paras from text file
		OStyle.Page landscape = new OStyle.Page(iDoc, "Default Style");
		landscape.setupHeader("Header text - ", com.sun.star.style.ParagraphAdjust.RIGHT, true);
		iDoc.startPage(landscape);
		iDoc.addText("This imported document uses page style 'Landscape'.");
		iDoc.endPara();
		File txtFilePath = new File(folder.getAbsolutePath() + "/writer-text.txt");
		iDoc.insertTextFile(txtFilePath, true);
		
		// replace a word
		iDoc.replace("online", "ONLINE");

		// search for and position to first para containing "classroom Java"
		OWriterDoc.TextSearch search = iDoc.new TextSearch();
		Iterator<OWriterDoc.TextSearch.Result> iter = search.perform("classroom Java").iterator();
		OWriterDoc.TextSearch.Result result = iter.next();
		result.goTo();
		
		// apply "Heading 2" style to para containing "classroom Java" then search for that style
		OStyle.Para title = new OStyle.Para(iDoc, "Heading 2");
		iDoc.applyParaStyle(title);
		search.matchParaStyle(true);
		Collection<OWriterDoc.TextSearch.Result> results = search.perform("Heading 2");
		OEnv.obj().log("Found " + results.size() + " Heading 2 paragraphs", false);
		
		// apply "Emphasis" character style to each occurrence of word "sentence"
		OStyle.Char emphasis = new OStyle.Char(iDoc, "Emphasis");
		iDoc.setWordStyle("sentence", emphasis);
	}
		
	private void insertTextFrame() throws Exception {
		// create frame style "Demo1" as a child of style "Frame" but with color LIGHTGRAY
		OStyle.Frame frameDemo = new OStyle.Frame(iDoc, "Demo1");
		OStyle.Frame frame = new OStyle.Frame(iDoc, "Frame");
		frameDemo.setParentStyle(frame.name());
		frameDemo.setProp(OProps.Frame.BackColor, OColor.LIGHTGRAY);

		// add frame to first "classroom Java" paragraph
		OWriterDoc.TextSearch search = iDoc.new TextSearch();
		Iterator<OWriterDoc.TextSearch.Result> iter = search.perform("classroom Java").iterator();
		OWriterDoc.TextSearch.Result result = iter.next();
		result.goTo();
		// due to style properties, only size width is used
		iDoc.insertTextFrame(frameDemo, new com.sun.star.awt.Size(5000, 0), "frame text", 
				com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH);
	}
	
	private void insertBitmap(File folder) throws Exception {
		// create frame style "Demo2" as a child of style "Frame"
		OStyle.Frame frameDemo = new OStyle.Frame(iDoc, "Demo2");
		OStyle.Frame frame = new OStyle.Frame(iDoc, "Frame");
		frameDemo.setParentStyle(frame.name());
		frameDemo.setProp(OProps.Frame.BackColor, OColor.LIGHTGRAY);

		// add frame to first "The system has" paragraph
		OWriterDoc.TextSearch search = iDoc.new TextSearch();
		Iterator<OWriterDoc.TextSearch.Result> iter = search.perform("The system has").iterator();
		OWriterDoc.TextSearch.Result result = iter.next();
		result.goTo();
		String url = OUtil.urlFromPath(folder.getAbsolutePath()) + "/example.jpg";
		iDoc.insertBitmap(frameDemo, new com.sun.star.awt.Size(5000, 5000), url, 
				com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH);
	}
	
	private void insertTable() throws Exception {
		// add table to "Notepad." paragraph, center cell contents, indent, and set all cells to PALEYELLOW
		OWriterDoc.TextSearch search = iDoc.new TextSearch();
		Iterator<OWriterDoc.TextSearch.Result> iter = search.perform("Notepad.").iterator();
		OWriterDoc.TextSearch.Result result = iter.next();
		result.goToEnd();
		OTable table = iDoc.insertTable(5, 6, com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH);
		table.centerCellContents();
		table.indent(1000);
		table.tableRange().setProp(OProps.TextTable.BackColor, OColor.PALEYELLOW);
		
		// add title row with color LIGHTGRAY and "Title" para style
		OStyle.Para style = new OStyle.Para(iDoc, "Title");
		OTable.CellRange titleRange = table.addTitleRow("This is the Title", style);
		titleRange.setProp(OProps.TextTable.BackColor, OColor.LIGHTGRAY);
		
		// make 2nd row YELLOW and 3rd column CYAN
		OTable.CellRange rowRange = table.rowRange(1);
		rowRange.setProp(OProps.TextTable.BackColor, OColor.GREEN);
		OTable.CellRange colRange = table.columnRange(3);
		colRange.setProp(OProps.TextTable.BackColor, OColor.CYAN);

		// put text in 2nd row and 3rd column
		String[][] rowStrings = {{"A", "B", "C", "D", "E", "F"}};
		table.rowRange(2).setText(rowStrings);
		String[][] colStrings = {{"2"}, {"3"}, {"4"}, {"5"}, {"6"}};
		colRange.setText(colStrings);
	}
	
	private void useCursor() throws Exception {
		// list lengths of all paragraphs to OEnv logger
		String lengths = "Paragraph lengths: ";
		OWriterDoc.ParaCursor pCursor = iDoc.new ParaCursor();
		while (pCursor.hasNext()) {
			String para = pCursor.text();
			lengths += " " + para.length();
		}
		OEnv.obj().log(lengths, false);
	}
	
	private void end(File folder) throws Exception {
		String url = OUtil.urlFromPath(folder.getAbsolutePath()) + "/unoofc";
		iDoc.writeProtected(url, OFileFilter.ODT, null);
		iDoc.logSummary();
	}
}