package edu.drexel.tm.cs338.textacular;

import java.io.File;

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

/**
 * The abstract class TemplatePanel.
 * 
 * Represents a panel that contains inputs for the creation of
 * a document type
 * 
 * @author Trevor Maglione <tm@cs.drexel.edu>
 */
public abstract class TemplatePanel extends JPanel {
	
	/**
	 * The template file directory.
	 */
	protected static final String TEMPLATE_DIR = "res/templates";
	
	/**
	 * The template name.
	 */
	private String templateName;
	
	/**
	 * The template filename.
	 */
	private String templateFilename;
	
	/**
	 * Instantiate a new TemplatePanel.
	 * 
	 * @param templateName the template name
	 */
	public TemplatePanel(String templateName, String templateFilename) {
		this(templateName, templateFilename, new MigLayout());
	}
	
	/**
	 * Instantiate a new TemplatePanel.
	 * 
	 * @param templateName the template name
	 * @param layout the layout
	 */
	public TemplatePanel(String templateName, String templateFilename, MigLayout layout) {
		super(layout);
		
		this.templateName = templateName;
		this.templateFilename = templateFilename;
		
		System.out.println(checkTemplateFile());
	}
	
	/**
	 * Get the template name.
	 * 
	 * @return the template name
	 */
	protected String getTemplateName() {
		return templateName;
	}
	
	/**
	 * Get the template filename.
	 * 
	 * @return the template filename
	 */
	protected String getTemplateFilename() {
		return templateFilename;
	}
	
	/**
	 * Determine if the template file exists and is readable.
	 */
	private boolean checkTemplateFile() {
		File templateFile = new File(String.format("%s/%s", TEMPLATE_DIR, getTemplateFilename()));
		return templateFile.exists() && !templateFile.isDirectory();
	}
	
	/**
	 * Read the template file.
	 */
	
	/**
	 * Validate the panel inputs.
	 * 
	 * @return if inputs are filled completely and correctly
	 */
	public abstract boolean checkInputs();
	
	/**
	 * Reset the panel inputs.
	 */
	public abstract void resetInputs();
}

@@ -49,20 +49,144 @@
 java.io.
-File
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileReader;%0Aimport java.io.IOException
 ;%0A%0Aimpor
@@ -1270,16 +1270,46 @@
 ame;%0A%09%09%0A
+%09%09if (checkTemplateFile()) %7B%0A%09
 %09%09System
@@ -1321,21 +1321,20 @@
 println(
-check
+read
 Template
@@ -1338,24 +1338,28 @@
 ateFile());%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09/**%0A%09 
@@ -1710,26 +1710,87 @@
 ble.%0A%09 *
-/%0A%09private
+ %0A%09 * @return if the file exists and is not a directory%0A%09 */%0A%09protected
  boolean
@@ -2009,17 +2009,599 @@
 ile.%0A%09 *
-/
+ %0A%09 * @return the contents of the template file, null on error%0A%09 */%0A%09protected String readTemplateFile() %7B%0A%09%09BufferedReader br;%0A%09%09%0A%09%09try %7B%0A%09%09%09br = new BufferedReader(new FileReader(String.format(%22%25s/%25s%22, TEMPLATE_DIR, getTemplateFilename())));%0A%09%09%09StringBuilder sb = new StringBuilder();%0A%09%09%09String line;%0A%09%09%09%0A%09%09%09while ((line = br.readLine()) != null) %7B%0A%09%09%09%09sb.append(String.format(%22%25s%25n%22, line));%0A%09%09%09%7D%0A%0A%09%09%09br.close();%0A%09%09%09%0A%09%09%09return sb.toString();%0A%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09return null;%0A%09%7D
 %0A%09%0A%09/**%0A

Read template file into String