gt: 		gridData = new GridData(SWT.FILL);
context: /*
  Vault 3
  (C) Copyright 2009, Eric Bergman-Terrell
  
  This file is part of Vault 3.
  
    Vault 3 is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
  
    Vault 3 is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    along with Vault 3.  If not, see <http://www.gnu.org/licenses/>.
  */
  
  package mainPackage;
  
  import org.eclipse.jface.dialogs.IDialogConstants;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.HelpEvent;
  import org.eclipse.swt.events.HelpListener;
  import org.eclipse.swt.events.ModifyEvent;
  import org.eclipse.swt.events.ModifyListener;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.events.SelectionListener;
  import org.eclipse.swt.graphics.Color;
  import org.eclipse.swt.layout.GridData;
  import org.eclipse.swt.layout.GridLayout;
  import org.eclipse.swt.widgets.Button;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Control;
  import org.eclipse.swt.widgets.DirectoryDialog;
  import org.eclipse.swt.widgets.Display;
  import org.eclipse.swt.widgets.Label;
  import org.eclipse.swt.widgets.Shell;
  import org.eclipse.swt.widgets.Text;
  
  /**
   * @author Eric Bergman-Terrell
   *
   */
  public class ExportPhotosToDeviceDialog extends VaultDialog {
  	@Override
  	protected boolean isResizable() {
  		return true;
  	}
  
  	private static String previousDestinationFolder;
  	
  	private Button totalPhotosCheckBox, deleteFolderContentsCheckBox, shuffleCheckBox;
  	
  	private Text totalPhotosText, widthText, heightText, photosPerFolderText;
  
  	private Label statusLabel, destinationFolderLabel;
  	
  	private Color nonErrorBackground, errorBackground;
  	
  	private Button okButton;
  
  	@Override
  	protected void okPressed() {
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosDeleteFolderContents, deleteFolderContentsCheckBox.getSelection());
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosDestFolder, destinationFolderLabel.getText());
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosHeight, heightText.getText());
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosWidth, widthText.getText());
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosPhotosPerFolder, photosPerFolderText.getText());
  		
  		String totalPhotos = totalPhotosCheckBox.getSelection() ? totalPhotosText.getText() : "";
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosTotalPhotos, totalPhotos);
  		
  		Globals.getPreferenceStore().setValue(PreferenceKeys.ExportPhotosShuffle, shuffleCheckBox.getSelection());
  		
  		super.okPressed();
  	}
  
  	@Override
  	protected Control createContents(Composite parent) {
  		Control result = super.createContents(parent);
  		
  	    statusLabel = new Label(parent, SWT.BORDER);
  	
  		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
  		statusLabel.setLayoutData(gridData);
  
  		nonErrorBackground = statusLabel.getBackground();
  		errorBackground = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
  
  		return result;
  	}
  
  	private boolean validInteger(String text) {
  		boolean valid = false;
  		
  		int value = 0;
  		
  		try {
  			value = Integer.parseInt(text);
  			valid = value > 0;
  		}
  		catch (NumberFormatException ignored) {
  		}
  		
  		return valid;
  	}
  	
  	private void enableDisableOKButton() {
  		String errorMessage = null;
  		
  		if (destinationFolderLabel.getText().length() == 0) {
  			errorMessage = "Please specify destination folder";
  		}
  		else if (!validInteger(widthText.getText())) {
  			errorMessage = "Invalid Width";
  		}
  		else if (!validInteger(heightText.getText())) {
  			errorMessage = "Invalid Height";
  		}
  		else if (totalPhotosCheckBox.getSelection() && !validInteger(totalPhotosText.getText())) {
  			errorMessage = "Invalid Total Photos";
  		}
  		else if (!validInteger(photosPerFolderText.getText())) {
  			errorMessage = "Invalid Photos per Folder";
  		}
  		
  		if (errorMessage != null) {
  			statusLabel.setBackground(errorBackground);
  			statusLabel.setText(errorMessage);
  			okButton.setEnabled(false);
  		}
  		else {
  			statusLabel.setBackground(nonErrorBackground);
  			statusLabel.setText("");
  			okButton.setEnabled(true);
  		}
  	}
  	
  	@Override
  	protected void configureShell(Shell newShell) {
  		super.configureShell(newShell);
  
  		newShell.setText("Export Photos to Device");
  	}
  
  	@Override
  	protected void createButtonsForButtonBar(Composite parent) {
  		okButton = createButton(parent, IDialogConstants.OK_ID, "&OK", true);
  		createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", false);
  		
  		okButton.setEnabled(false);
  	}
  
  	@Override
  	protected Control createDialogArea(Composite parent) {
  		Composite composite = (Composite) super.createDialogArea(parent);
  		composite.setLayout(new GridLayout(4, false));
  
  		Composite destinationFolderComposite = new Composite(composite, SWT.NONE);
  		GridLayout gridLayout = new GridLayout(3, false);
  		gridLayout.marginWidth = 0;
  		destinationFolderComposite.setLayout(gridLayout);
  		
  		GridData gridData = new GridData(SWT.FILL);
  		gridData.horizontalSpan = 4;
  		gridData.grabExcessHorizontalSpace = true;
  		gridData.horizontalAlignment = SWT.FILL;
  		destinationFolderComposite.setLayoutData(gridData);
  		
  		Button destinationFolderBrowseButton = new Button(destinationFolderComposite, SWT.PUSH);
  		destinationFolderBrowseButton.setText("&Destination Folder...");
  
  		// Spacer
  		new Label(destinationFolderComposite, SWT.NONE);
  		
  		destinationFolderLabel = new Label(destinationFolderComposite, SWT.NONE);
  
  		gridData = new GridData(SWT.FILL);
  		gridData.grabExcessHorizontalSpace = true;
  		gridData.horizontalAlignment = SWT.FILL;
  		destinationFolderLabel.setLayoutData(gridData);
  		
  		destinationFolderBrowseButton.addSelectionListener(new SelectionListener() {
  			@Override
  			public void widgetDefaultSelected(SelectionEvent e) {
  			}
  
  			@Override
  			public void widgetSelected(SelectionEvent e) {
  				DirectoryDialog directoryDialog = new DirectoryDialog(getShell());
  				directoryDialog.setText("Specify Destination Folder");
  				directoryDialog.setMessage("Folder:");
  				directoryDialog.setFilterPath(previousDestinationFolder);
  				
  				String destinationFolder = directoryDialog.open();
  				
  				if (destinationFolder != null) {
  					destinationFolderLabel.setText(destinationFolder);
  					previousDestinationFolder = destinationFolder;
  					
  					enableDisableOKButton();
  				}
  			}
  		});
  		
  		Composite deleteFolderContentsComposite = new Composite(composite, SWT.NONE);
  		gridLayout = new GridLayout(1, false);
  		gridLayout.marginWidth = 0;
  		deleteFolderContentsComposite.setLayout(gridLayout);
  		
  		gridData = new GridData(SWT.FILL);
  		gridData.horizontalSpan = 4;
  		deleteFolderContentsComposite.setLayoutData(gridData);
  		
  		deleteFolderContentsCheckBox = new Button(deleteFolderContentsComposite, SWT.CHECK);
  		deleteFolderContentsCheckBox.setText("De&lete Folder Contents");
  		
  		deleteFolderContentsCheckBox.setSelection(Globals.getPreferenceStore().getBoolean(PreferenceKeys.ExportPhotosDeleteFolderContents));
  		
  		Label widthLabel = new Label(composite, SWT.NONE);
  		widthLabel.setText("&Width:");
  		
  		widthText = new Text(composite, SWT.BORDER);
  