// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2011 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.pathvisio.gexplugin;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import org.bridgedb.rdb.construct.DBConnector;
import org.pathvisio.core.debug.Logger;
import org.pathvisio.desktop.PvDesktop;
import org.pathvisio.desktop.data.DBConnectorSwing;
import org.pathvisio.desktop.plugin.Plugin;

/**
 * This plugin enables Gex functionality. Currently only adds menu items
 * to import or load a Gex
 * @author thomas
 */
public class GexPlugin implements Plugin {

	public void init(PvDesktop desktop)
	{
		ImportGexDataAction importAction = new ImportGexDataAction(desktop);
		SelectGexAction selectAction = new SelectGexAction(desktop);

		desktop.registerMenuAction ("Data", importAction);
		desktop.registerMenuAction ("Data", selectAction);
	}

	public void done() {};

	/**
	 * Import gex data and create a new gex database from it
	 */
	public static class ImportGexDataAction extends AbstractAction
	{
		private static final long serialVersionUID = 1L;

		private final PvDesktop sae;

		public ImportGexDataAction(PvDesktop sae)
		{
			super();
			this.sae = sae;
			putValue (NAME, "Import expression data");
			putValue (SHORT_DESCRIPTION, "Import data from a tab delimited text file, for example experimental data from a high-throughput experiment");
		}

		public void actionPerformed (ActionEvent e)
		{
			GexImportWizard wizard = new GexImportWizard(sae);
			wizard.showModalDialog(sae.getSwingEngine().getFrame());
		}
	}

	/**
	 * Let the user open an expression dataset
	 * @author thomas
	 */
	public static class SelectGexAction extends AbstractAction
	{
		private static final long serialVersionUID = 1L;
		private final PvDesktop se;

		public SelectGexAction(PvDesktop standaloneEngine) {
			se = standaloneEngine;
			putValue(NAME, "Select expression dataset");
			putValue(SHORT_DESCRIPTION, "Select expression dataset");
		}

		public void actionPerformed(ActionEvent e) {
			try
			{
				/**
				 * Get the preferred database connector to connect to Gex databases,
				 * and try to cast it to swingDbConnector.
				 * throws an exception if that fails
				 */
				DBConnectorSwing dbcon;
				DBConnector dbc = se.getGexManager().getDBConnector();
				if(dbc instanceof DBConnectorSwing)
				{
					dbcon = (DBConnectorSwing)dbc;
				}
				else
				{
					//TODO: better handling of error
					throw new IllegalArgumentException("Not a Swing database connector");
				}
				String dbName = dbcon.openChooseDbDialog(null);

				if(dbName == null) return;

				se.getGexManager().setCurrentGex(dbName, false);
				se.loadGexCache();
			}
			catch(Exception ex)
			{
				String msg = "Failed to open expression dataset; " + ex.getMessage();
				JOptionPane.showMessageDialog(null,
						"Error: " + msg + "\n\n" + "See the error log for details.",
						"Error",
						JOptionPane.ERROR_MESSAGE);
				Logger.log.error(msg, ex);
			}
		}
	}
}

@@ -1662,60 +1662,8 @@
 %0A%09%7B%0A
-%09%09private static final long serialVersionUID = 1L;%0A%0A
 %09%09pr
@@ -2189,27 +2189,8 @@
 set%0A
-%09 * @author thomas%0A
 %09 */
@@ -2267,59 +2267,8 @@
 ate 
-static final long serialVersionUID = 1L;%0A%09%09private 
 fina
@@ -2276,17 +2276,22 @@
  PvD
+esktop d
 esktop
- se
 ;%0A%0A%09
@@ -2328,52 +2328,41 @@
 top 
-standaloneEngine) %7B%0A%09%09%09se = standaloneEngine
+_desktop) %7B%0A%09%09%09desktop = _desktop
 ;%0A%09%09
@@ -2759,18 +2759,23 @@
 r dbc = 
-se
+desktop
 .getGexM
@@ -3068,20 +3068,34 @@
 bDialog(
-null
+desktop.getFrame()
 );%0A%0A%09%09%09%09
@@ -3126,18 +3126,23 @@
 n;%0A%0A%09%09%09%09
-se
+desktop
 .getGexM
@@ -3184,18 +3184,23 @@
 e);%0A%09%09%09%09
-se
+desktop
 .loadGex
@@ -3349,20 +3349,34 @@
 eDialog(
-null
+desktop.getFrame()
 ,%0A%09%09%09%09%09%09

Set parent for dialogs in SelectGexAction