import se.datadosen.component.*;
import se.datadosen.util.*;
engine.processBeanShellFile(new File(programDirectory, "includes/printing.bsh"));
public void dbg(String s) {JOptionPane.showMessageDialog(window,s, "", JOptionPane.WARNING_MESSAGE);}
// Read localized strings to use in user interface
SmartResourceBundle texts = SmartResourceBundle.getBundle(new File(skinDirectory, "texts"), "texts");

// Controls that are to be imported into JAlbum as variables
ControlPanel ui = new ControlPanel() {

	JTextField copyright = new JTextField("");
	JComboBox language = new JComboBox(new Object[]
	 {"af", "ca", "cs", "da", "de", "en", "es", "fi", "fr", "hu", "is", "it", "nl", "no", "pl", "pt", "ru", "sk", "sl", "sv", "uk", "zh", "zh-chs", "zh-cht"}
	);
	JComboBox printing = getPrintingComboBox();
	// Variable that defines if the folder view should be used
	JCheckBox useFolderView = new JCheckBox(texts.getString("folders"), false); // Folders
	
	// Plug-ins
	JTextField Plugins = new JTextField("");
	
	// Array of checkboxes
	JCheckBox[] plg;
};

ControlPanel generalPanel = new ControlPanel();

// Layout controls easily similar to how text is added in a word processor
generalPanel.add("p", new JLabel(texts.getString("ui.language"))); // Language
generalPanel.add("tab", ui.language);

// Add folder view checkbox
generalPanel.add("p", ui.useFolderView);

// Plug-ins
String sPlugins = "";

// Fetch information about selected plugins from project variables
window.ui2Engine();
Object txtPlugins = engine.getSkinVariables().get("Plugins");
if (txtPlugins != null)
	sPlugins = txtPlugins.toString();
	
// Open "plugins" folder in the skin directory
File dirPlugins = new File(skinDirectory, "plugins");
if (dirPlugins.exists()) {
	// This filter only returns true if the File is a directory
	FileFilter fileFilter = new FileFilter() {
   		public boolean accept(File file) {
     			return file.isDirectory();
		}
	};
	// List all directories in the plugins directory
	File[] fPlugins = dirPlugins.listFiles(fileFilter);
	// Create array of checkboxs to allow the user to activate the plug-ins
	ui.plg = new JCheckBox[fPlugins.length];
	// Create string array of already activated plug-ins
	String[] arrPlugins = sPlugins.split("#");
	ControlPanel cpPlugins = new ControlPanel("Plugins");
	for (int i = 0; i < fPlugins.length; i++) {
		ui.plg[i] = new JCheckBox(fPlugins[i].getName(), false);
		for (int j = 0; j < arrPlugins.length; j++) {
			if (arrPlugins[j].compareTo(ui.plg[i].getText()) == 0) {
				ui.plg[i].setSelected(true);
			}
		}
		// Add checkbox to user interface
		cpPlugins.add(ui.plg[i]);
		// ...and add event handler to the checkbox
		ui.plg[i].addItemListener(this);
	}
	generalPanel.add("p hfill", cpPlugins);
}

ControlPanel orderPrints = new ControlPanel(texts.getString("ui.printOrderingFrame")); // On-line print ordering
orderPrints.add(new JLabel(texts.getString("ui.printOrderingText1")));
orderPrints.add(ui.printing);
orderPrints.add(new JLabel(texts.getString("ui.printOrderingText2"))); // printing service
orderPrints.add("p", new JLabel(" " + texts.getString("ui.printOrderingDescription1") + " "));  // Each slide...
orderPrints.add("br", new JLabel(" " + texts.getString("ui.printOrderingDescription2") + " ")); // Shortly after...
orderPrints.add("br", new JLabel(" " +texts.getString("ui.printOrderingDescription3") + " ")); // Shutterfly will...

generalPanel.add("p hfill", orderPrints);

// Allow other languages than specified in list
ui.language.setEditable(true);

// Set default language
ui.language.setSelectedItem(System.getProperty("user.language"));

tabs = new JTabbedPane();
tabs.setName("XPTabs");
tabs.add(generalPanel, "General");
ui.add("vtop hfill vfill", tabs);

// Finally install components into JAlbum
window.setSkinUI(ui);

// Eventhandler that updates hidden textbox with information about what plug-ins are selected
public void itemStateChanged(ItemEvent e) {
	JCheckBox source = (JCheckBox)e.getItemSelectable();
	String[] s = sPlugins.split("#");
	if (e.getStateChange() == ItemEvent.DESELECTED) {
		sPlugins = "";
		for (int i = 0; i < s.length; i++) {
			if (s[i].compareTo(source.getText()) != 0) {
				if (sPlugins.length() > 0)
					sPlugins += "#";
				sPlugins += s[i];
			}
		}
	} else {
		boolean bFound = false;
		sPlugins = "";
		for (int i = 0; i < s.length; i++) {
			if (s[i].compareTo(source.getText()) == 0)
				bFound = true;
			if (sPlugins.length() > 0)
				sPlugins += "#";
			sPlugins += s[i];
		}
		if (!bFound) {
			if (sPlugins.length() > 0)
				sPlugins += "#";
			sPlugins += source.getText();
		}
	}
	ui.Plugins.setText(sPlugins);
}


// Add plugin interface tabs
if (dirPlugins.exists()) {
	// This filter only returns true if the File is a directory
	FileFilter fileFilter = new FileFilter() {
   		public boolean accept(File file) {
     			return file.isDirectory();
		}
	};
	// List all directories in the plugins directory
	File[] fPlugins = dirPlugins.listFiles(fileFilter);

	for (int i = 0; i < fPlugins.length; i++) {
		// Add plugin's interface tab
		File plgOnLoad = new File(fPlugins[i], "/onload.bsh");
		if (plgOnLoad.exists()) {
			engine.processBeanShellFile(plgOnLoad);
			window.setSkinUI(ui);
		}
	}
}


