import java.awt.*;
import java.awt.image.*;
import java.awt.MediaTracker;
import java.io.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
 

public class AIPPhandler implements ItemListener,ActionListener
{
	int width;
	int height;
	int pix[];
	byte info[];
	int image_num;

	String temp;
	AIPP myframe;
	Image pic;
	Image picold;

	static int masksize;
	static float variance;
	static float edge_thresh;
	static double hsi1,hsi2,hsi3,hsi4,hsi5;
	static boolean ok;
	
	boolean custom;
	
	
	AIPPhandler(AIPP win)
	{
		myframe=win;
		masksize=3;
		image_num = 1;
		picold = myframe.image;
		pic = myframe.image;
		ok = false;
	}
	
	public void eventStart()
	{
		myframe.mainCanvas.setCursor(AIPP.waitCursor);
		myframe.open1m.setEnabled(false);
		myframe.open2m.setEnabled(false);
		myframe.open3m.setEnabled(false);
		myframe.savem.setEnabled(false);
		myframe.editM.setEnabled(false);
		myframe.filtM.setEnabled(false);
		myframe.analM.setEnabled(false);
		myframe.noiseM.setEnabled(false);
		AIPP.statusBar.setText("AIPP Working ...");
	}
	
	public void eventEnd(int type)
	{
		MediaTracker mt=new MediaTracker(myframe);
		mt.addImage(pic,0);
		try
		{
			mt.waitForAll();
		}
		catch (Exception ex)
		{
		}

		myframe.mainCanvas.setCursor(AIPP.defaultCursor);
		myframe.open1m.setEnabled(true);
		myframe.open2m.setEnabled(true);
		myframe.open3m.setEnabled(true);
		myframe.savem.setEnabled(true);
		myframe.editM.setEnabled(true);
		myframe.filtM.setEnabled(true);
		myframe.analM.setEnabled(true);
		myframe.noiseM.setEnabled(true);
		if (type == 1)
		{
			// event was successful
			// set old image and current image 
			// rebuild current image array ro reflect changes
			picold=myframe.image;
			myframe.image=pic;
			myframe.getArray(pic);
		}
		System.gc();
	}

	public void actionPerformed(ActionEvent e)
	{
		pic=myframe.image;
		
		if (e.getSource() instanceof MenuItem)
		{
			String label=((MenuItem)e.getSource()).getLabel();
			if (label.equals("Open Image 1")
			|| label.equals("Open Image 2")
			|| label.equals("Open Image 3")
			|| label.equals("Revert to Original"))
			{
				eventStart();
				if (myframe.infom.getState() == true)
				{
					myframe.infoBox = false;
					myframe.infoPopup.dispose();
					myframe.infom.setState(false);
				}
				
				if (label.equals("Open Image 1"))
				{
					image_num = 1;
				}
				else if (label.equals("Open Image 2"))
				{
					image_num = 2;
				}
				if (label.equals("Open Image 3"))
				{
					image_num = 3;
				}

				if (image_num == 1)
					pic=AIPPapplet.image1;
				else if (image_num == 2)
					pic=AIPPapplet.image2;
				else if (image_num == 3)
					pic=AIPPapplet.image3;

				eventEnd(1);
				AIPP.statusBar.setText("Opened image "+image_num);
			}
			else if (label.equals("Save"))
			{
				AIPP.statusBar.setText("Save function not supported in AIPP Applet");
			}
			else if (label.equals("Undo"))
			{
				pic=myframe.image;
				myframe.image=picold;
				picold=pic;
				AIPP.statusBar.setText("Undo Performed");
			}
			else if (label.equals("Exit"))
			{
				myframe.dispose();
			}
			else if (label.equals("Grey"))
			{
				eventStart();
				greyfilt filter=new greyfilt();
				pic=myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Image converted to Grey-Scale");
			}
			else if (label.equals("Negative"))
			{
				eventStart();
				negfilt filter=new negfilt();
				pic=myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Negative Filter Applied");
			}
			else if (label.equals("Emboss"))
			{
				eventStart();
				int mask[] = {-2,-1,0,-1,0,1,0,1,2};
				embfilt filter=new embfilt(3,mask);
				pic=myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Emboss Filter Applied");
			}
			else if (label.equals("Blue"))
			{
				eventStart();
				bluefilt filter=new bluefilt();
				pic=myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Blue Channel");
			}
			else if (label.equals("Green"))
			{
				eventStart();
				greenfilt filter=new greenfilt();
				pic=myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Green Channel");
			}
			else if (label.equals("Red"))
			{
				eventStart();
				redfilt filter=new redfilt();
				pic=myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Red Channel");
			}
			else if (label.equals("Blur"))
			{
				eventStart();
				if (custom)
				{
					int mask[] = new int[masksize*masksize];
					genDialog gd = new genDialog(myframe,mask);
					gd.setVisible(true);
					AIPP.statusBar.setText("AIPP Working ...");

					if (ok)
					{
						blurfilt filter=new blurfilt(masksize,mask);
						pic=myframe.createImage(new FilteredImageSource
							(myframe.image.getSource(),filter));
						eventEnd(1);
						AIPP.statusBar.setText("Blur Filter Applied");
					}
					else
					{
						eventEnd(0);
						AIPP.statusBar.setText("Operation Canceled");
					}
				}
				else
				{
					int mask[] = new int[masksize*masksize];
				
					for (int x=0;x<masksize*masksize;x++)
						mask[x]=1;

					blurfilt filter=new blurfilt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Blur Filter Applied");
				}
			}
			else if (label.equals("Sharpen"))
			{
				eventStart();
				if (custom)
				{
					int mask[] = new int[masksize*masksize];
					genDialog gd = new genDialog(myframe,mask);
					gd.setVisible(true);
					AIPP.statusBar.setText("AIPP Working ...");

					if (ok)
					{
						sharpfilt filter=new sharpfilt(masksize,mask);
						pic=myframe.createImage(new FilteredImageSource
							(myframe.image.getSource(),filter));
						eventEnd(1);
						AIPP.statusBar.setText("Sharpen Filter Applied");
					}
					else
					{
						eventEnd(0);
						AIPP.statusBar.setText("Operation Canceled");
					}
				}
				else
				{
					int mask[] =new int[masksize*masksize];
				
					for (int x=0;x<masksize*masksize;x++)
						mask[x]=1;

					sharpfilt filter=new sharpfilt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Sharpen Filter Applied");
				}
			}
			else if (label.equals("D_Plus"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	0,1,0,
							1,1,1,
							0,1,0};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	0,0,1,0,0,
							0,0,1,0,0,
							1,1,1,1,1,
							0,0,1,0,0,
							0,0,1,0,0};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	0,0,0,1,0,0,0,
							0,0,0,1,0,0,0,
							0,0,0,1,0,0,0,
							1,1,1,1,1,1,1,
							0,0,0,1,0,0,0,
							0,0,0,1,0,0,0,
							0,0,0,1,0,0,0};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Dilate Filter(D-Plus) Applied");
			}
			else if (label.equals("D_Square"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	1,1,1,
							1,0,1,
							1,1,1};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	1,1,1,1,1,
							1,0,0,0,1,
							1,0,0,0,1,
							1,0,0,0,1,
							1,1,1,1,1};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	1,1,1,1,1,1,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,1,1,1,1,1,1};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Dilate Filter(D-Square) Applied");
			}
			else if (label.equals("D_X"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	1,0,1,
							0,1,0,
							1,0,1};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	1,0,0,0,1,
							0,1,0,1,0,
							0,0,1,0,0,
							0,1,0,1,0,
							1,0,0,0,1};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	1,0,0,0,0,0,1,
							0,1,0,0,0,1,0,
							0,0,1,0,1,0,0,
							0,0,0,1,0,0,0,
							0,0,1,0,1,0,0,
							0,1,0,0,0,1,0,
							1,0,0,0,0,0,1};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Dilate Filter(D-X) Applied");
			}
			else if (label.equals("D_Circle"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	0,1,0,
							1,0,1,
							0,1,0};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	0,0,1,0,0,
							0,1,0,1,0,
							1,0,0,0,1,
							0,1,0,1,0,
							0,0,1,0,0};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	0,0,1,1,1,0,0,
							0,1,0,0,0,1,0,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							0,1,0,0,0,1,0,
							0,0,1,1,1,0,0};
					dilate_filt filter=new dilate_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Dilate Filter(D-Circle) Applied");
			}
			else if (label.equals("E_Plus"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	0,1,0,
							1,1,1,
							0,1,0};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	0,0,1,0,0,
							0,0,1,0,0,
							1,1,1,1,1,
							0,0,1,0,0,
							0,0,1,0,0};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	0,0,0,1,0,0,0,
							0,0,0,1,0,0,0,
							0,0,0,1,0,0,0,
							1,1,1,1,1,1,1,
							0,0,0,1,0,0,0,
							0,0,0,1,0,0,0,
							0,0,0,1,0,0,0};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Erode Filter(E-Plus) Applied");
			}
			else if (label.equals("E_Square"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	1,1,1,
							1,0,1,
							1,1,1};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	1,1,1,1,1,
							1,0,0,0,1,
							1,0,0,0,1,
							1,0,0,0,1,
							1,1,1,1,1};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	1,1,1,1,1,1,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,1,1,1,1,1,1};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Erode Filter(E-Square) Applied");
			}
			else if (label.equals("E_X"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	1,0,1,
							0,1,0,
							1,0,1};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	1,0,0,0,1,
							0,1,0,1,0,
							0,0,1,0,0,
							0,1,0,1,0,
							1,0,0,0,1};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	1,0,0,0,0,0,1,
							0,1,0,0,0,1,0,
							0,0,1,0,1,0,0,
							0,0,0,1,0,0,0,
							0,0,1,0,1,0,0,
							0,1,0,0,0,1,0,
							1,0,0,0,0,0,1};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Erode Filter(E-X) Applied");
			}
			else if (label.equals("E_Circle"))
			{
				eventStart();

				if (masksize == 3)
				{
					int mask[] = {	0,1,0,
							1,0,1,
							0,1,0};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 5)
				{
					int mask[] = {	0,0,1,0,0,
							0,1,0,1,0,
							1,0,0,0,1,
							0,1,0,1,0,
							0,0,1,0,0};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				else if (masksize == 7)
				{
					int mask[] = {	0,0,1,1,1,0,0,
							0,1,0,0,0,1,0,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							1,0,0,0,0,0,1,
							0,1,0,0,0,1,0,
							0,0,1,1,1,0,0};
					erode_filt filter=new erode_filt(masksize,mask);
					pic=myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
				}
				eventEnd(1);
				AIPP.statusBar.setText("Erode Filter(E-Circle) Applied");
			}
			else if (label.equals("Fuzzy_Vector_Directional"))
			{
				eventStart();
				FVDF filter = new FVDF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Fuzzy_Vector_Directional Applied");
			}
			else if (label.equals("Vector_Median"))
			{
				eventStart();
				VMF filter = new VMF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Vector_Median Applied");
			}
			else if (label.equals("Arithmatic_Mean"))
			{
				eventStart();
				AMF filter = new AMF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Arithmatic_Mean Applied");
			}
			else if (label.equals("Basic_Vector_Directional"))
			{
				eventStart();
				BVDF filter = new BVDF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Basic_Vector_Directional Applied");
			}
			else if (label.equals("Generalized_Vector_Directional"))
			{
				eventStart();
				GVDF filter = new GVDF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Generalized_Vector_Directional Applied");
			}
			else if (label.equals("Directional_Distance"))
			{
				eventStart();
				DDF filter = new DDF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Directional_Distance Applied");
			}
			else if (label.equals("Hybrid_Directional"))
			{
				eventStart();
				HF filter = new HF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Hybrid_Directional Applied");
			}
			else if (label.equals("Adaptive_Hybrid_Directional"))
			{
				eventStart();
				AHF filter = new AHF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_Hybrid_Directional Applied");
			}
			else if (label.equals("Adaptive_Nearest_Neighbor"))
			{
				eventStart();
				ANNF filter = new ANNF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_Nearest_Neighbor Applied");
			}
			else if (label.equals("Adaptive_Nearest_Neighbor(Canberra)"))
			{
				eventStart();
				CANNF filter = new CANNF(3);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_Nearest_Neighbor(Canberra) Applied");
			}
			else if (label.equals("Adaptive_Nearest_Neighbor(S)"))
			{
				eventStart();
				CBANNF filter = new CBANNF(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_Nearest_Neighbor Applied");
			}
			else if (label.equals("Adaptive_NonParametric(Exponential)"))
			{
				eventStart();
				AMNFE filter = new AMNFE(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_NonParametric Applied");
			}
			else if (label.equals("Adaptive_NonParametric(Gaussian)"))
			{
				eventStart();
				AMNFG filter = new AMNFG(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_NonParametric Applied");
			}
			else if (label.equals("Adaptive_NonParametric(Directional)"))
			{
				eventStart();
				AMNFD filter = new AMNFD(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Adaptive_NonParametric Applied");
			}			
			else if (label.equals("Bayesian Adaptive(Median/Mean)"))
			{
				eventStart();
				BFMA filter = new BFMA(masksize);
				pic = myframe.createImage(new FilteredImageSource
					(myframe.image.getSource(),filter));
				eventEnd(1);
				AIPP.statusBar.setText("Bayesian Adaptive(Median/Mean) Applied");
			}			
			else if (label.equals("HSI Segmentation"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,3);
				gd.setVisible(true);
				if (ok)
				{
					hsi filter = new hsi(hsi1,hsi2,hsi3,hsi4,hsi5);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Mean"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,2);
				gd.setVisible(true);
				if (ok)
				{
					mean_edge filter = new mean_edge(edge_thresh,masksize);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Edge Detection (Mean) Completed");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Median"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,2);
				gd.setVisible(true);
				if (ok)
				{
					median_edge filter = new median_edge(edge_thresh,masksize);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Edge Detection (Median) Completed");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("VR (Euclidean Distance)"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,2);
				gd.setVisible(true);
				if (ok)
				{
					vr0_edge filter = new vr0_edge(edge_thresh,masksize);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Edge Detection (VR-Euclidean) Completed");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("VR (Angle Distance)"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,2);
				gd.setVisible(true);
				if (ok)
				{
					vr3_edge filter = new vr3_edge(edge_thresh,masksize);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Edge Detection (VR-Angle) Completed");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Adaptive"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,2);
				gd.setVisible(true);
				if (ok)
				{
					adapt_edge filter = new adapt_edge(edge_thresh,masksize);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Edge Detection (Adaptive) Completed");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Gaussian"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,1);
				gd.setVisible(true);
				if (ok)
				{
					gaus_noise filter = new gaus_noise(variance);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Gaussian Noise Added");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Impulsive"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,1);
				gd.setVisible(true);
				if (ok)
				{
					imp_noise filter = new imp_noise(variance);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Impulsive Noise Added");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Poisson"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,1);
				gd.setVisible(true);
				if (ok)
				{
					poiss_noise filter = new poiss_noise(variance);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Poisson Noise Added");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Uniform"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,1);
				gd.setVisible(true);
				if (ok)
				{
					un_noise filter = new un_noise(variance);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Uniform Noise Added");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("Exponential"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,1);
				gd.setVisible(true);
				if (ok)
				{
					ex_noise filter = new ex_noise(variance);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("Exponential Noise Added");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			else if (label.equals("White Impulsive"))
			{
				eventStart();
				genDialog gd = new genDialog(myframe,1);
				gd.setVisible(true);
				if (ok)
				{
					w_noise filter = new w_noise(variance);
					pic = myframe.createImage(new FilteredImageSource
						(myframe.image.getSource(),filter));
					eventEnd(1);
					AIPP.statusBar.setText("White Impulsive Noise Added");
				}
				else
				{
					eventEnd(0);
					AIPP.statusBar.setText("Operation Canceled");
				}
			}
			myframe.repaint();		
		}
	}

	public void itemStateChanged(ItemEvent e)
	{
		if(e.getSource() instanceof CheckboxMenuItem)
		{
			String label=((CheckboxMenuItem)e.getSource()).getLabel();
			if (label.equals("3x3"))
			{
				myframe.mask5m.setState(false);
				myframe.mask7m.setState(false);
				myframe.mask3m.setState(true);
				masksize=3;
				AIPP.statusBar.setText("Mask Size set to 3x3");
			}
			else if (label.equals("5x5"))
			{
				myframe.mask3m.setState(false);
				myframe.mask7m.setState(false);
				myframe.mask5m.setState(true);
				masksize=5;
				AIPP.statusBar.setText("Mask Size set to 5x5");
			}
			else if (label.equals("7x7"))
			{
				myframe.mask3m.setState(false);
				myframe.mask5m.setState(false);
				myframe.mask7m.setState(true);
				masksize=7;
				AIPP.statusBar.setText("Mask Size set to 7x7");
			}
			else if (label.equals("Custom"))
			{
				custom = myframe.custm.getState();
				AIPP.statusBar.setText("Custom Mask Selected");
			}
			else if(label.equals("Show Image Info"))
			{
				if (((CheckboxMenuItem)e.getSource()).getState() == true)
				{
					myframe.infoPopup = new InfoPopup(myframe);
					myframe.infoPopup.setVisible(true);
					myframe.infoBox = true;
				}
				else
				{
					myframe.infoBox = false;
					myframe.infoPopup.dispose();
				}
			}
		}
		
	}

}
