public void destroy() {

		if (!asApplet) {
			undoManager.discardAllEdits();
			getDocument().removeUndoableEditListener(undoHandler);
			getDocument().removeDocumentListener(editResponder);
			editResponder.setPage(null);
		}
		stopSound();
		stopAllRunningModels();
		if (midiPlayer != null)
			midiPlayer.destroy();
		if (sampledAudioPlayer != null)
			sampledAudioPlayer.destroy();

		List<Embeddable> list = getEmbeddedComponents();
		if (list != null) {
			for (Embeddable embed : list) {
				if (!(embed instanceof ModelCanvas))
					embed.destroy();
			}
		}

		// must call to deactive the page's document listeners first, otherwise the destroy code may
		// cause the AbstractDocument.fireChangedUpdate() method to be called.
		removeAllDocumentListeners();

		if (pageLoadingThread != null) {
			pageLoadingThread.interrupt();
			pageLoadingThread = null;
		}

		if (actions != null)
			actions.clear();
		getInputMap().clear();
		getActionMap().clear();
		activityActionMap.clear();
		pageListenerList.clear();
		selectedComponents.clear();
		selectedImages.clear();
		properties.clear();

		if (imageReader != null) {
			imageReader.setParent(null);
			imageReader.getImageImporters().clear();
			imageReader = null;
		}

		if (navigator != null)
			navigator.destroy();
		setNavigator(null);
		setProgressBar(null);
		setURLDisplay(null);
		setComponentPool(null);
		setActionNotifier(null);
		setUploadReportAction(null);

		// getClipboard().setContents(null, null);

		CaretListener[] cl = getCaretListeners();
		if (cl != null) {
			for (CaretListener i : cl)
				removeCaretListener(i);
		}

		KeyListener[] kl = getKeyListeners();
		if (kl != null) {
			for (KeyListener i : kl)
				removeKeyListener(i);
		}

		if (encoder != null) {
			encoder.destroy();
			encoder = null;
		}
		decoder.destroy();
		decoder = null;
		plainTextWriter = null;
		myHighlightPainter = null;

		destroyPopupMenu(popupMenu);
		destroyPopupMenu(imagePopupMenu);
		destroyPopupMenu(colorBarPopupMenu);
		destroyPopupMenu(linkPopupMenu);

		if (fontDialog != null)
			fontDialog.dispose();
		if (paragraphDialog != null)
			paragraphDialog.dispose();
		if (bulletDialog != null)
			bulletDialog.dispose();
		if (hyperlinkDialog != null)
			hyperlinkDialog.dispose();
		if (propertiesDialog != null) {
			propertiesDialog.destroy();
			propertiesDialog = null;
		}

		popupMenu = null;
		imagePopupMenu = null;
		colorBarPopupMenu = null;
		linkPopupMenu = null;
		undoManager = null;
		undoHandler = null;
		editResponder = null;
		navigator = null;
		urlDisplay = null;
		refreshAction = null;
		newAction = null;
		closeAction = null;
		hintAction = null;
		gradeAction = null;
		scriptAction = null;
		insertFileAction = null;
		selectAllAction = null;
		bulletAction = null;
		pastePlainTextAction = null;
		symbolAction = null;
		hyperlinkAction = null;
		printAction = null;
		pageSetupAction = null;
		printPreviewAction = null;
		openAction = null;
		saveAction = null;
		saveAsAction = null;
		insertAtomContainerAction = null;
		insertChemContainerAction = null;
		insertProsynContainerAction = null;
		insertGBContainerAction = null;
		insertComponentAction = null;
		increaseIndentAction = null;
		decreaseIndentAction = null;
		increaseFontSizeAction = null;
		decreaseFontSizeAction = null;
		fontAction = null;
		paragraphAction = null;
		insertBulletAction = null;
		propertiesAction = null;
		saveAsAppletAction = null;
		colorBarAction = null;
		editor = null;
	}