//package lu.tudor.santec.gecamed.formeditor.gui.view.dialog;
//
//import java.awt.event.ActionEvent;
//import java.awt.event.KeyEvent;
//import java.awt.event.KeyListener;
//import java.awt.event.MouseEvent;
//import java.awt.event.MouseListener;
//import java.util.Collection;
//import java.util.Iterator;
//
//import javax.swing.AbstractAction;
//import javax.swing.DefaultListModel;
//import javax.swing.JButton;
//import javax.swing.JList;
//import javax.swing.JPanel;
//import javax.swing.JScrollPane;
//import javax.swing.JTextArea;
//import javax.swing.JTextField;
//import javax.swing.event.ListSelectionEvent;
//import javax.swing.event.ListSelectionListener;
//
//import lu.tudor.santec.gecamed.core.gui.GECAMedIconNames;
//import lu.tudor.santec.gecamed.core.gui.IconFetcher;
//import lu.tudor.santec.gecamed.core.gui.widgets.NamedComponent;
//import lu.tudor.santec.gecamed.core.utils.ManagerFactory;
//import lu.tudor.santec.gecamed.formeditor.ejb.entity.beans.FormTemplate;
//import lu.tudor.santec.gecamed.formeditor.ejb.session.beans.FormManagerImpl;
//import lu.tudor.santec.gecamed.formeditor.ejb.session.interfaces.FormManager;
//import lu.tudor.santec.gecamed.formeditor.gui.model.ComboBoxElement;
//import lu.tudor.santec.gecamed.formeditor.gui.model.FormEditorModel;
//import lu.tudor.santec.i18n.Translatrix;
//
//import com.jgoodies.forms.layout.CellConstraints;
//import com.jgoodies.forms.layout.FormLayout;
//
//public class SaveTemplateDialog implements ListSelectionListener, KeyListener {
//	
//	private FormEditorModel model;
//	private DefaultListModel listElements;
//	private JList templateList;
//	
//	private ComboBoxElement<FormTemplate> element;
//	private JButton saveBtn;
//	private JButton deleteBtn;
//	private FormEditorDialog dialog;
//	
//	private JTextField templateName;
//	
//	public SaveTemplateDialog (FormEditorModel model) {
//		/* ======================================== */
//		this.model = model;
//		
//		FormManager manager = (FormManager)ManagerFactory.getRemote(FormManagerImpl.class);
//		Collection<FormTemplate> templates = manager.getAllTemplates();
//
//		JPanel mainPanel = new JPanel(new FormLayout("2dlu, min(300px; p):g, 2dlu", 
//				"2dlu, p, " + // description text
//				"0dlu, p, " + // 
//				"5dlu, f:150px, " + // "available templates:" label & JList
//				"5dlu, p," + // the new name
//				"10dlu"));
//		
//		// descriptions on top
//		String text = Translatrix.getTranslationString("formeditor.save_form-template") + ":";
//		JTextArea textArea = FormEditorDialog.getFormatedTextArea(text);
//		
////		text = FormEditorDialog.formatMessage(text, 100, false);
////		JLabel textArea = new JLabel(text);		
//		mainPanel.add(textArea, new CellConstraints(2, 2));
////		mainPanel.add(new JLabel(Translatrix.getTranslationString(
////				"formeditor.available_templates")), new CellConstraints(2, 4));
//		
//		// create JList
//		listElements = new DefaultListModel();//new Vector<ComboBoxElement>();
//		ComboBoxElement<FormTemplate> elem = null;
//		
//		for (Iterator<FormTemplate> iter = templates.iterator(); iter.hasNext(); ) {
//			FormTemplate template = iter.next();
//			elem = new ComboBoxElement<FormTemplate>(
//					template.getName() + " (" + template.getVersion() + ")",
//					template);
//			listElements.addElement(elem);
//		}
//		
//		templateList = new JList(listElements);
//		
//		templateList.addListSelectionListener(this);
//		mainPanel.add(new NamedComponent(
//				Translatrix.getTranslationString("formeditor.available_templates"), 
//				new JScrollPane(templateList)), new CellConstraints(2, 6));
//		templateList.addMouseListener(new MouseListener() {
//			
//			public void mouseReleased(MouseEvent e) {}
//			public void mousePressed(MouseEvent e) {}
//			public void mouseExited(MouseEvent e) {}
//			public void mouseEntered(MouseEvent e) {}
//			
//			public void mouseClicked(MouseEvent e) {
//				/* ---------------------------------------- */
//				if (e.getClickCount() >= 2) {
//					/* ---------------------------------------- */
////					dialog.setVisible(false);
//					saveBtn.doClick();
//					/* ---------------------------------------- */
//				}
//				/* ---------------------------------------- */
//			}
//		});
//
//		templateName = new JTextField(model.getFormModel().getName());
//		templateName.addKeyListener(this);
//		mainPanel.add(new NamedComponent(
//				Translatrix.getTranslationString("formeditor.template_name"), 
//				templateName), new CellConstraints(2, 8));
//		
//		// create dialog
//		dialog = FormEditorDialog.getNewInstance(model.getView(), 
//				Translatrix.getTranslationString("formeditor.save_template"), 
//				mainPanel, FormEditorDialog.OK_CANCEL_BUTTON_MODE);
//		
//		// create buttons
//		
//		// save button
////		saveBtn = new JButton(Translatrix.getTranslationString("formeditor.load"),
////				IconFetcher.getScaledIcon(GECAMedModule.class, GECAMedIconNames.TEMPLATE_OPEN, 24));
//		saveBtn = dialog.getButton(FormEditorDialog.OK_BUTTON);
//		dialog.setAction(FormEditorDialog.OK_BUTTON, new AbstractAction() {
//			private static final long serialVersionUID = 1L;
//			@SuppressWarnings("unchecked")
//			public void actionPerformed(ActionEvent e) {
//				/* ---------------------------------------- */
//				// save the template under the given name
////				element.setValue(null);
//				
//				if (element == null
//						|| element.getValue() == null) {
//					/* ---------------------------------------- */
//					/* 
//					 * if the element's value is null, no template was selected
//					 * -> look if the entered name is the same
//					 * as the name of an existing template
//					 */
//					
//					FormTemplate template;
//					for (Object elem : listElements.toArray()) {
//						/* ---------------------------------------- */
//						template = ((ComboBoxElement<FormTemplate>)elem).getValue();
//						if (template.getName().equals(templateName.getText())) {
//							element = new ComboBoxElement<FormTemplate>(template.getName(), template);
//							break;
//						}
//						/* ---------------------------------------- */
//					}
//					/* ---------------------------------------- */
//				}
//				
//				if (element == null
//						|| element.getValue() == null) {
//					/* ---------------------------------------- */
//					/* 
//					 * if the element's value is still null, an existing template with 
//					 * the entered name wasn't found
//					 * -> create a new template, with the entered name and set it 
//					 * as value of the element 
//					 */
//					FormTemplate template = new FormTemplate();
//					template.setName(templateName.getText());
//					if (element == null) {
//						element = new ComboBoxElement<FormTemplate>("", template);
//					} else {
//						element.setValue(template);
//					}
//					/* ---------------------------------------- */
//				} else {
//					/* ---------------------------------------- */
//					// ask, if the the template shell be overwritten
//					int buttonMode = FormEditorDialog.showMessageDialog(
//							SaveTemplateDialog.this.model.getView(), 
//							Translatrix.getTranslationString("formeditor.override_template_title"), 
//							Translatrix.getTranslationString("formeditor.override_template") + 
//							"\n\n" + Translatrix.getTranslationString("formeditor.template_name") + 
//							": " + element.getValue().getName(), 
//							FormEditorDialog.YES_NO_CANCEL_BUTTON_MODE);
//					
//					if (buttonMode == FormEditorDialog.CANCEL_BUTTON) {
//						/* ---------------------------------------- */
//						return;
//						/* ---------------------------------------- */
////					} else /* if (buttonMode == FormEditorDialog.OK_BUTTON) */ {
////						
//					} else if (buttonMode == FormEditorDialog.NO_BUTTON) {
//						element = null;
//					}
//					/* ---------------------------------------- */
//				}
//				
//				dialog.setVisible(false);
//				/* ---------------------------------------- */
//			}
//		});
////		saveBtn.setActionCommand(MenuPanelController.SAVE_BUTTON);
//		saveBtn.setEnabled(!templateName.getText().equals(""));
////		saveBtn.addActionListener(this);
//		
//		// cancel button
////		JButton cancelBtn = new JButton(Translatrix.getTranslationString("formeditor.cancel"),
////				IconFetcher.getScaledIcon(GECAMedModule.class, GECAMedIconNames.CANCEL, 24));
////		JButton cancelBtn = dialog.getButton(FormEditorDialog.CANCEL_BUTTON);
////		cancelBtn.setActionCommand(MenuPanelController.CANCEL_LOAD_TEMPLATE_BUTTON);
////		cancelBtn.addActionListener(this);
//		dialog.setAction(FormEditorDialog.CANCEL_BUTTON, new AbstractAction() {
//			private static final long serialVersionUID = 1L;
//
//			public void actionPerformed(ActionEvent e) {
//				/* ------------------------------ */
//				element = null;
//				dialog.setVisible(false);
//				/* ------------------------------ */
//			}
//		});
//		
//		// delete button
//		deleteBtn = new JButton(Translatrix.getTranslationString("core.delete"), 
//				IconFetcher.getScaledIcon(GECAMedIconNames.class, GECAMedIconNames.REMOVE, 16));
////		deleteBtn.setActionCommand(MenuPanelController.DELETE_TEMPLATE_BUTTON);
//		deleteBtn.addActionListener(new AbstractAction() {
//			private static final long serialVersionUID = 1L;
//
//			public void actionPerformed(ActionEvent e) {
//				/* ---------------------------------------- */
//				int templateId = element.getValue().getId();
//				if (LoadTemplateDialog.deleteFormTemplate(
//						SaveTemplateDialog.this.model.getView(), templateId)) {
//					/* ---------------------------------------- */
//					listElements.removeElement(element);
//					/* ---------------------------------------- */
//				}
//				/* ---------------------------------------- */
//			}
//		});
//		deleteBtn.setEnabled(false);
//		
////		dialog.setDefaultActions(FormEditorDialog.OK_CANCEL_BUTTON_MODE);
//		
//		dialog.addButton(deleteBtn);
////		dialog.addButton(saveBtn);
////		dialog.addButton(cancelBtn);
//		/* ======================================== */
//	}
//	
//	public FormTemplate show() {
//		/* ======================================== */
//		// format and show dialog
//		dialog.pack();
//		dialog.pack();
//		dialog.pack();
//		
//		// center the window
//		dialog.setLocation(FormEditorDialog.centerWindowOnOwner(dialog, dialog.getOwner()));
////		pushDialog(d);
//		dialog.setResizable(false);
//		dialog.setVisible(true);
//		
//		if (element == null) {
//			return null;
//		}
//		
//		return element.getValue();
//		/* ======================================== */
//	}
//
////	public void actionPerformed(ActionEvent e) {
////		/* ======================================== */
////		if (e.getActionCommand().equals(MenuPanelController.DELETE_TEMPLATE_BUTTON)) {	
////			/* ---------------------------------------- */
////			int templateId = ((FormTemplate)element.getObject()).getId();
////			if (LoadTemplateDialog.deleteFormTemplate(model.getView(), templateId)) {
////				/* ---------------------------------------- */
////				listElements.removeElement(element);
////				/* ---------------------------------------- */
////			}
////			
////			return;
////			/* ---------------------------------------- */
////		} else if (e.getActionCommand()
////				.equals(MenuPanelController.CANCEL_LOAD_TEMPLATE_BUTTON)) {
////			/* ------------------------------ */
////			// cancel button was pressed
////			
////			element = null;
////			/* ------------------------------ */
////		} else if (e.getActionCommand()
////				.equals(MenuPanelController.SAVE_BUTTON)) {
////			/* ---------------------------------------- */
////			// save the template under the given name
//////			element.setValue(null);
////			
////			if (element == null
////					|| element.getObject() == null) {
////				/* ---------------------------------------- */
////				/* 
////				 * if the element's value is null, no template was selected
////				 * -> look if the entered name is the same
////				 * as the name of an existing template
////				 */
////				
////				FormTemplate template;
////				for (Object elem : listElements.toArray()) {
////					/* ---------------------------------------- */
////					template = (FormTemplate) ((ComboBoxElement)elem).getObject();
////					if (template.getName().equals(templateName.getText())) {
////						element = new ComboBoxElement(template, template.getName());
////						break;
////					}
////					/* ---------------------------------------- */
////				}
////				/* ---------------------------------------- */
////			}
////			
////			if (element == null
////					|| element.getObject() == null) {
////				/* ---------------------------------------- */
////				/* 
////				 * if the element's value is still null, an existing template with 
////				 * the entered name wasn't found
////				 * -> create a new template, with the entered name and set it 
////				 * as value of the element 
////				 */
////				FormTemplate template = new FormTemplate();
////				template.setName(templateName.getText());
////				if (element == null) {
////					element = new ComboBoxElement(template, "");
////				} else {
////					element.setValue(template);
////				}
////				/* ---------------------------------------- */
////			} else {
////				/* ---------------------------------------- */
////				// ask, if the the template shell be overwritten
////				int buttonMode = FormEditorDialog.showMessageDialog(model.getView(), 
////						"Overwrite template", 
////						"Do you want to overwrite the existing template:\n\n" +
////						"Template name: " +
////						((FormTemplate)element.getObject()).getName(), 
////						FormEditorDialog.YES_NO_CANCEL_BUTTON_MODE);
////				
////				if (buttonMode == FormEditorDialog.CANCEL_BUTTON) {
////					/* ---------------------------------------- */
////					return;
////					/* ---------------------------------------- */
//////				} else /* if (buttonMode == FormEditorDialog.OK_BUTTON) */ {
//////					
////				} else if (buttonMode == FormEditorDialog.NO_BUTTON) {
////					element = null;
////				}
////				/* ---------------------------------------- */
////			}
////			/* ---------------------------------------- */
////		}
////		dialog.setVisible(false);
////		/* ======================================== */
////	}
//	
//	@SuppressWarnings("unchecked")
//	public void valueChanged(ListSelectionEvent e) {
//		/* ======================================== */
//		element = (ComboBoxElement<FormTemplate>)((JList)e.getSource()).getSelectedValue();
//		if (element != null
//				&& element.getValue() != null
//				&& !templateName.getText().toUpperCase().equals(
//						element.getValue().getName().toUpperCase())) {
//			/* ---------------------------------------- */
//			templateName.setText(element.getValue().getName());
//			/* ---------------------------------------- */
//		}
//		deleteBtn.setEnabled(element != null);
//		
//		saveBtn.setEnabled(!templateName.getText().equals(""));
//		/* ======================================== */
//	}
//
//	public void keyTyped(KeyEvent e) {}
//	public void keyPressed(KeyEvent e) {}
//
//	@SuppressWarnings("unchecked")
//	public void keyReleased(KeyEvent e) {
//		/* ======================================== */
//		saveBtn.setEnabled(!templateName.getText().equals(""));
//		int selectedIndex = templateList.getSelectedIndex();
//		
//		String name = templateName.getText();
//		if (name != null && !name.equals("")) {
//			/* ---------------------------------------- */
//			Object[] elements = ((DefaultListModel)templateList.getModel()).toArray();
//			
//			for (Object elem : elements) {
//				/* ---------------------------------------- */
//				FormTemplate template = ((ComboBoxElement<FormTemplate>)elem).getValue();
//				
//				if (name.toUpperCase().equals(template.getName().toUpperCase())) {
//					/* ---------------------------------------- */
//					templateList.setSelectedValue(elem, true);
//					return;
//					/* ---------------------------------------- */
//				}
//				/* ---------------------------------------- */
//			}
//			/* ---------------------------------------- */
//		}
//		
//		templateList.removeSelectionInterval(selectedIndex, selectedIndex);
//		/* ======================================== */
//	}
//}
