gt: 			LOGGER.error( e.getLocalizedMessage(), e);
context: /*
   * Copyright (C) 2005-2020 Gregory Hedlund & Yvan Rose
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  
   *    http://www.apache.org/licenses/LICENSE-2.0
  
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package ca.phon.app.query.report;
  
  import ca.phon.app.log.BufferPanel;
  import ca.phon.app.log.LogBuffer;
  import ca.phon.app.query.ResultSetSelector;
  import ca.phon.project.Project;
  import ca.phon.query.db.Query;
  import ca.phon.query.db.ResultSet;
  import ca.phon.query.report.ReportBuilder;
  import ca.phon.query.report.ReportBuilderException;
  import ca.phon.query.report.ReportIO;
  import ca.phon.query.report.csv.CSVReportBuilder;
  import ca.phon.query.report.io.ObjectFactory;
  import ca.phon.query.report.io.ReportDesign;
  import ca.phon.ui.CommonModuleFrame;
  import ca.phon.ui.action.PhonUIAction;
  import ca.phon.ui.decorations.DialogHeader;
  import ca.phon.ui.nativedialogs.NativeDialogs;
  import ca.phon.ui.wizard.WizardFrame;
  import ca.phon.ui.wizard.WizardStep;
  import ca.phon.util.PrefHelper;
  import ca.phon.util.icons.IconManager;
  import ca.phon.util.icons.IconSize;
  import ca.phon.worker.PhonTask;
  import ca.phon.worker.PhonTask.TaskStatus;
  import ca.phon.worker.PhonTaskListener;
  import ca.phon.worker.PhonWorker;
  
  import javax.swing.*;
  import java.awt.*;
  import java.io.File;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  
  /**
   * 1-step wizard for creating reports.
   */
  public class ReportWizard extends WizardFrame {
  	
  	private static final long serialVersionUID = -334722251289455999L;
  
  	private final static org.apache.logging.log4j.Logger LOGGER = org.apache.logging.log4j.LogManager.getLogger(ReportWizard.class.getName());
  	
  	private final static String AUTOSAVE_FILENAME = "lastreport.xml";
  	
  	/*
  	 * UI elements
  	 */
  	/*
  	 * Report editor
  	 */
  	private ReportEditor reportEditor;
  	
  	/*
  	 * Console 
  	 */
  	private BufferPanel console;
  	
  	/*
  	 * Session selection
  	 */
  	private ResultSetSelector resultSetSelector;
  	
  	/*
  	 * Query
  	 */
  	private Query query;
  	
  	/*
  	 * Wizard steps 
  	 */
  	private WizardStep editorStep;
  	private WizardStep reportStep;
  	
  	private PhonTask currentTask = null;
  	
  	private Project tempProject = null;
  	
  	public ReportWizard(Project project, Query q) {
  		this(project, null, q);
  	}
  	
  	/**
  	 * Constructor
  	 */
  	public ReportWizard(Project project, Project tempProject, Query q) {
  		super("Phon : " + project.getName() + " : Report");
  		this.tempProject = tempProject;
  		
  		putExtension(Project.class, project);
  
  		super.setWindowName("Report");
  //		super.setProject(project);
  		this.query = q;
  		
  //		super.setResizable(false);
  		
  		init();
  	}
  	
  	private Project getProject() {
  		return getExtension(Project.class);
  	}
  	
  	private void init() {
  		// add steps
  		editorStep = createEditorStep();
  		reportStep = createReportStep();
  		
  		editorStep.setNextStep(1);
  		reportStep.setPrevStep(0);
  		
  		// setup navigation buttons
  		super.btnBack.setVisible(false);
  		
  		super.btnNext.setText("Generate report");
  		super.btnNext.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  		
  		super.getRootPane().setDefaultButton(btnNext);
  		
  		super.btnFinish.setVisible(false);
  		
  		final PhonUIAction saveAct = new PhonUIAction(console, "onSaveBuffer");
  		saveAct.putValue(PhonUIAction.NAME, "Save Report");
  		saveAct.putValue(PhonUIAction.SMALL_ICON, IconManager.getInstance().getIcon("actions/document-save", IconSize.SMALL));
  		super.btnFinish.setAction(saveAct);
  		
  		super.btnCancel.setText("Close");
  	}
  	
  	/**
  	 * Create step one of the wizard
  	 */
  	private WizardStep createEditorStep() {
  		JPanel reportPanel = new JPanel(new BorderLayout());
  		
  		DialogHeader header = new DialogHeader("Report", "Set up report format and select result sets.");
  		reportPanel.add(header, BorderLayout.NORTH);
  		
  		JTabbedPane tabs = new JTabbedPane();
  		
  		ReportDesign design = getReportDesign();
  		
  		final String queryName = query.getName();
  		String reportName = queryName;
  		if(queryName.indexOf('.') > 0) {
  			reportName = queryName.substring(0, queryName.lastIndexOf('.'));
  		}
  		
  		design.setName(reportName);
  		reportEditor = new ReportEditor(design);
  		
  		tabs.addTab("Report Format", reportEditor);
  		
  		resultSetSelector = new ResultSetSelector((tempProject == null ? getProject() : tempProject), query);
  		resultSetSelector.selectAll();
  		tabs.addTab("Result Sets", resultSetSelector);
  		
  		reportPanel.add(tabs, BorderLayout.CENTER);
  		
  		return super.addWizardStep(reportPanel);
  	}
  	
  	/**
  	 * Create report gen step
  	 */
  	private WizardStep createReportStep() {
  		JPanel importPanel = new JPanel(new BorderLayout());
  		
  		DialogHeader importHeader = new DialogHeader("Report", "Generating report, this may take some time.");
  		importPanel.add(importHeader, BorderLayout.NORTH);
  		
  		JPanel consolePanel = new JPanel(new BorderLayout());
  		
  		console = new BufferPanel(getReportDesign().getName());
  		consolePanel.add(console, BorderLayout.CENTER);
  		
  		importPanel.add(consolePanel, BorderLayout.CENTER);
  		
  		return super.addWizardStep(importPanel);
  	}
  
  	/**
  	 * Returns last report design that was open or a new empty report
  	 * design.
  	 * @return
  	 */
  	private ReportDesign getReportDesign() {
  		ObjectFactory factory = new ObjectFactory();
  		ReportDesign retVal = factory.createReportDesign();
  		
  		File lastDesignFile = 
  			new File(PrefHelper.getUserDataFolder(), AUTOSAVE_FILENAME);
  		if(lastDesignFile.exists()) {
  			try {
  				retVal = ReportIO.readDesign(lastDesignFile);
  			} catch (IOException e) {
  				LOGGER.error( e.getLocalizedMessage(), e);
  			}
  		}
  		
  		return retVal;
  	}
  	
  	private void writeAutosaveReport() {
  		File designFile = new File(PrefHelper.getUserDataFolder(), AUTOSAVE_FILENAME);
  		try {
  			ReportIO.writeDesign(reportEditor.getReportDesign(), designFile);
  		} catch (IOException e) {
  