package edu.wustl.catissuecore.applet.listener;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JApplet;
import javax.swing.table.TableModel;

import edu.wustl.catissuecore.applet.component.SpecimenArrayTable;
import edu.wustl.catissuecore.applet.model.SpecimenArrayTableModel;
import edu.wustl.catissuecore.applet.ui.SpecimenArrayApplet;
import edu.wustl.catissuecore.applet.util.SpecimenArrayAppletUtil;

/**
 * <p>This class initializes the fields of SpecimenArrayTableMouseHandler.java</p>
 * @author Ashwin Gupta
 * @version 1.1
 */
public class SpecimenArrayTableMouseHandler extends MouseAdapter {

	public void mouseClicked(MouseEvent e) {
		super.mouseClicked(e);
		SpecimenArrayTable arrayTable = (SpecimenArrayTable) e.getSource();
		int column = arrayTable.getSelectedColumn();
		int row = arrayTable.getSelectedRow();
		TableModel model = arrayTable.getModel();
		SpecimenArrayTableModel tableModel = null;
		
		if (model instanceof SpecimenArrayTableModel) {
			tableModel = (SpecimenArrayTableModel) model;
			String concentration = (String) tableModel.getSpecimenArrayModelMap().get(SpecimenArrayAppletUtil.getArrayMapKey(row,column,tableModel.getColumnCount(),3));
			String quantity = (String) tableModel.getSpecimenArrayModelMap().get(SpecimenArrayAppletUtil.getArrayMapKey(row,column,tableModel.getColumnCount(),2));
			JApplet applet = getBaseApplet(arrayTable);
			
			if (applet instanceof SpecimenArrayApplet) {
				SpecimenArrayApplet arrayApplet = (SpecimenArrayApplet) applet;
				arrayApplet.getConcentrationTextField().setText(concentration);
				arrayApplet.getQuantityTextField().setText(quantity);
				arrayApplet.getApplyButton().setEnabled(true);
			}
		}
		System.out.println(e.getSource());
	}
	
	public JApplet getBaseApplet(Component component) {
		while (component != null) {
			if (component instanceof JApplet) {
				return ((JApplet) component);
			}
			component = component.getParent();
		}
		return null;
	}

}

@@ -48,36 +48,8 @@
 %0D%0A%0D%0A
-import java.awt.Component;%0D%0A
 impo
@@ -183,24 +183,79 @@
 leModel;%0D%0A%0D%0A
+import edu.wustl.catissuecore.applet.AppletConstants;%0D%0A
 import edu.w
@@ -437,24 +437,85 @@
 rayApplet;%0D%0A
+import edu.wustl.catissuecore.applet.util.CommonAppletUtil;%0D%0A
 import edu.w
@@ -922,98 +922,8 @@
 );%0D%0A
-%09%09int column = arrayTable.getSelectedColumn();%0D%0A%09%09int row = arrayTable.getSelectedRow();%0D%0A
 %09%09Ta
@@ -1113,24 +1113,294 @@
  model;%0D%0A%09%09%09
+%0D%0A%09%09%09// enable if molecular specimen %0D%0A%09%09%09if ((tableModel.getSpecimenClass() != null) && SpecimenArrayAppletUtil.isMolecularSpecimen(tableModel.getSpecimenClass()))%0D%0A%09%09%09%7B%0D%0A%09%09%09%09int column = arrayTable.getSelectedColumn();%0D%0A%09%09%09%09int row = arrayTable.getSelectedRow();%0D%0A%09%09%09%09
 String conce
@@ -1543,14 +1543,59 @@
 t(),
-3
+AppletConstants.ARRAY_CONTENT_ATTR_CONC_INDEX
 ));%0D%0A
+%09
 %09%09%09S
@@ -1744,14 +1744,63 @@
 t(),
-2
+AppletConstants.ARRAY_CONTENT_ATTR_QUANTITY_INDEX
 ));%0D%0A
+%09
 %09%09%09J
@@ -1815,16 +1815,33 @@
 pplet = 
+CommonAppletUtil.
 getBaseA
@@ -1863,21 +1863,23 @@
 e);%0D%0A%09%09%09
+%09
 %0D%0A%09%09%09
+%09
 if (appl
@@ -1912,24 +1912,25 @@
 yApplet) %7B%0D%0A
+%09
 %09%09%09%09Specimen
@@ -1986,16 +1986,17 @@
 pplet;%0D%0A
+%09
 %09%09%09%09arra
@@ -2052,24 +2052,89 @@
 ntration);%0D%0A
+%09%09%09%09%09arrayApplet.getConcentrationTextField().setEnabled(true);%0D%0A%09
 %09%09%09%09arrayApp
@@ -2180,24 +2180,25 @@
 tity);%0D%0A%09%09%09%09
+%09
 arrayApplet.
@@ -2204,19 +2204,25 @@
 .get
-ApplyButton
+QuantityTextField
 ().s
@@ -2246,292 +2246,120 @@
 %0A%09%09%09
-%7D%0D%0A%09%09%7D%0D%0A%09%09System.out.println(e.getSource());%0D%0A%09%7D%0D%0A%09%0D%0A%09public J
+%09%09array
 Applet
- getBaseApplet(Component component) %7B%0D%0A%09%09while (component != null) %7B%0D%0A%09%09%09if (component instanceof JApplet) %7B%0D%0A%09%09%09%09return ((JApplet) component);
+.getApplyButton().setEnabled(true);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%09
 %0D%0A
-%09
 %09%09%7D%0D%0A%09%09
-%09component = component.getParent
+//System.out.println(e.getSource
 ()
+)
 ;%0D%0A
-%09
 %09%7D%0D%0A
-%09%09return null;%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

update for array model