gt: 			gbc = new GridBagConstraints();
context: /* Copyright (c) 2010, Carl Burch. License information is located in the
   * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
  
  package com.cburch.logisim.gui.main;
  
  import static com.cburch.logisim.util.LocaleString.*;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  import java.awt.image.BufferedImage;
  import java.io.*;
  import java.util.List;
  import javax.imageio.ImageIO;
  import javax.swing.*;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  import javax.swing.filechooser.FileFilter;
  import org.apache.batik.dom.GenericDOMImplementation;
  import org.apache.batik.svggen.SVGGraphics2D;
  import java.io.FileOutputStream;
  import com.itextpdf.awt.DefaultFontMapper;
  import com.itextpdf.awt.DefaultFontMapper.BaseFontParameters;
  import com.itextpdf.awt.PdfGraphics2D;
  import com.itextpdf.text.Rectangle;
  import com.itextpdf.text.pdf.*;
  import org.w3c.dom.DOMImplementation;
  import org.w3c.dom.Document;
  import com.cburch.logisim.circuit.Circuit;
  import com.cburch.logisim.circuit.CircuitState;
  import com.cburch.logisim.comp.ComponentDrawContext;
  import com.cburch.logisim.data.Bounds;
  import com.cburch.logisim.file.Loader;
  import com.cburch.logisim.proj.Project;
  
  class ExportImage {
  	private static final int SLIDER_DIVISIONS = 6;
  
  	private static final int FORMAT_GIF = 0;
  	private static final int FORMAT_PNG = 1;
  	private static final int FORMAT_JPG = 2;
  	private static final int FORMAT_SVG = 3;
  	private static final int FORMAT_PDF = 4;
  	
  	private static final int BORDER_SIZE = 5;
  
  	private ExportImage() { }
  
  	static void doExport(Project proj) {
  		// First display circuit/parameter selection dialog
  		Frame frame = proj.getFrame();
  		CircuitJList list = new CircuitJList(proj, true);
  		if (list.getModel().getSize() == 0) {
  			JOptionPane.showMessageDialog(proj.getFrame(),
  					getFromLocale("exportEmptyCircuitsMessage"),
  					getFromLocale("exportEmptyCircuitsTitle"),
  					JOptionPane.YES_NO_OPTION);
  			return;
  		}
  		OptionsPanel options = new OptionsPanel(list);
  		int action = JOptionPane.showConfirmDialog(frame,
  				options, getFromLocale("exportImageSelect"),
  				JOptionPane.OK_CANCEL_OPTION,
  				JOptionPane.QUESTION_MESSAGE);
  		if (action != JOptionPane.OK_OPTION) return;
  		List<Circuit> circuits = list.getSelectedValuesList();
  		double scale = options.getScale();
  		boolean printerView = options.getPrinterView();
  		if (circuits.isEmpty()) return;
  		
  		ImageFileFilter filter;
  		int fmt = options.getImageFormat();
  		switch (options.getImageFormat()) {
  		case FORMAT_GIF:
  			filter = new ImageFileFilter(fmt, getFromLocale("exportGifFilter"),
  				new String[] { "gif" });
  			break;
  		case FORMAT_PNG:
  			filter = new ImageFileFilter(fmt, getFromLocale("exportPngFilter"),
  				new String[] { "png" });
  			break;
  		case FORMAT_JPG:
  			filter = new ImageFileFilter(fmt, getFromLocale("exportJpgFilter"),
  				new String[] { "jpg", "jpeg", "jpe", "jfi", "jfif", "jfi" });
  			break;
  		case FORMAT_SVG:
  			filter = null;
  			filter = new ImageFileFilter(fmt, getFromLocale("exportSvgFilter"),
  				new String[] { "svg" });
  			break;
          case FORMAT_PDF:
            filter = null;
            filter = new ImageFileFilter(fmt, getFromLocale("exportPdfFilter"),
                new String[] { "pdf" });
            break;
  		default:
  			System.err.println("unexpected format; aborted"); //OK
  			return;
  		}
  		
  		// Then display file chooser
  		Loader loader = proj.getLogisimFile().getLoader();
  		JFileChooser chooser = loader.createChooser();
  		if (circuits.size() > 1) {
  			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  			chooser.setDialogTitle(getFromLocale("exportImageDirectorySelect"));
  		} else {
  			chooser.setFileFilter(filter);
  			chooser.setDialogTitle(getFromLocale("exportImageFileSelect"));
  		}
  		int returnVal = chooser.showDialog(frame, getFromLocale("exportImageButton"));
  		if (returnVal != JFileChooser.APPROVE_OPTION) return;
  
  		// Determine whether destination is valid
  		File dest = chooser.getSelectedFile();
  		chooser.setCurrentDirectory(dest.isDirectory() ? dest : dest.getParentFile());
  		if (dest.exists()) {
  			if (!dest.isDirectory()) {
  				int confirm = JOptionPane.showConfirmDialog(proj.getFrame(),
  					getFromLocale("confirmOverwriteMessage"),
  					getFromLocale("confirmOverwriteTitle"),
  					JOptionPane.YES_NO_OPTION);
  				if (confirm != JOptionPane.YES_OPTION) return;
  			}
  		} else {
  			if (circuits.size() > 1) {
  				boolean created = dest.mkdir();
  				if (!created) {
  					JOptionPane.showMessageDialog(proj.getFrame(),
  							getFromLocale("exportNewDirectoryErrorMessage"),
  							getFromLocale("exportNewDirectoryErrorTitle"),
  							JOptionPane.YES_NO_OPTION);
  					return;
  				}
  			}
  		}
  
  		// Create the progress monitor
  		ProgressMonitor monitor = new ProgressMonitor(frame,
  				getFromLocale("exportImageProgress"),
  				null,
  				0, 10000);
  		monitor.setMillisToDecideToPopup(100);
  		monitor.setMillisToPopup(200);
  		monitor.setProgress(0);
  
  		// And start a thread to actually perform the operation
  		// (This is run in a thread so that Swing will update the
  		// monitor.)
  		new ExportThread(frame, frame.getCanvas(), dest, filter,
  				circuits, scale, printerView, monitor).start();
  
  	}
  
  	private static class OptionsPanel extends JPanel implements ChangeListener {
  		JSlider slider;
  		JLabel curScale;
  		JCheckBox printerView;
  		JRadioButton formatPng;
  		JRadioButton formatGif;
  		JRadioButton formatJpg;
  		JRadioButton formatSvg;
  		JRadioButton formatPdf;
  		GridBagLayout gridbag;
  		GridBagConstraints gbc;
  		Dimension curScaleDim;
  
  		OptionsPanel(JList list) {
  			// set up components
  			formatPng = new JRadioButton("PNG");
  			formatGif = new JRadioButton("GIF");
  			formatJpg = new JRadioButton("JPEG");
  			formatSvg = new JRadioButton("SVG");
  			formatPdf = new JRadioButton("PDF");
  			ButtonGroup bgroup = new ButtonGroup();
  			bgroup.add(formatPng);
  			bgroup.add(formatGif);
  			bgroup.add(formatJpg);
  			bgroup.add(formatSvg);
  			bgroup.add(formatPdf);
  			formatPng.setSelected(true);
  
  			slider = new JSlider(SwingConstants.HORIZONTAL,
  					-3 * SLIDER_DIVISIONS, 3 * SLIDER_DIVISIONS, 0);
  			slider.setMajorTickSpacing(10);
  			slider.addChangeListener(this);
  			curScale = new JLabel("222%");
  			curScale.setHorizontalAlignment(SwingConstants.RIGHT);
  			curScale.setVerticalAlignment(SwingConstants.CENTER);
  			curScaleDim = new Dimension(curScale.getPreferredSize());
  			curScaleDim.height = Math.max(curScaleDim.height,
  					slider.getPreferredSize().height);
  			stateChanged(null);
  
  			printerView = new JCheckBox();
  			printerView.setSelected(true);
  
  			// set up panel
  			gridbag = new GridBagLayout();
  