MergeDatabase.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.viewer.actions;
00002
00003 import java.io.File;
00004
00005 import org.eclipse.core.commands.AbstractHandler;
00006 import org.eclipse.core.commands.ExecutionEvent;
00007 import org.eclipse.core.commands.ExecutionException;
00008 import org.eclipse.core.runtime.IPath;
00009 import org.eclipse.jface.dialogs.MessageDialog;
00010 import org.eclipse.jface.viewers.ArrayContentProvider;
00011 import org.eclipse.jface.viewers.LabelProvider;
00012 import org.eclipse.swt.widgets.Display;
00013 import org.eclipse.ui.IWorkbenchWindow;
00014 import org.eclipse.ui.dialogs.ListSelectionDialog;
00015 import org.eclipse.ui.handlers.HandlerUtil;
00016
00017 import edu.rice.cs.hpc.data.experiment.Experiment;
00018 import edu.rice.cs.hpc.data.experiment.merge.ExperimentMerger;
00019 import edu.rice.cs.hpc.viewer.experiment.ExperimentView;
00020 import edu.rice.cs.hpc.viewer.window.ViewerWindow;
00021 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 public abstract class MergeDatabase extends AbstractHandler
00033 {
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 public Object execute(ExecutionEvent event, final ExperimentMerger.MergeType type)
00045 throws ExecutionException {
00046
00047 final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
00048 final ViewerWindow vWin = ViewerWindowManager.getViewerWindow(window);
00049 final Experiment[] dbArray = vWin.getExperiments();
00050
00051
00052 if (dbArray.length > 1)
00053 {
00054 final Experiment db1;
00055 final Experiment db2;
00056
00057
00058
00059 if (dbArray.length == 2)
00060 {
00061 db1 = (Experiment) dbArray[0];
00062 db2 = (Experiment) dbArray[1];
00063
00064 } else
00065 {
00066
00067 ListSelectionDialog dlg = new ListSelectionDialog(window.getShell(), dbArray,
00068 new ArrayContentProvider(), new ExperimentLabelProvider(), "Select two databases to merge:");
00069 dlg.setTitle("Merging database");
00070 dlg.open();
00071 Object[] selectedDatabases = dlg.getResult();
00072
00073 if ((selectedDatabases != null) && (selectedDatabases.length == 2)) {
00074
00075 db1 = (Experiment) selectedDatabases[0];
00076 db2 = (Experiment) selectedDatabases[1];
00077 } else
00078 {
00079
00080 return null;
00081 }
00082 }
00083
00084 Display display = HandlerUtil.getActiveShell(event).getDisplay();
00085 display.asyncExec(new Runnable(){
00086
00088 public void run() {
00089 boolean need_to_find_name = true;
00090 int i = 0;
00091 String path = dbArray[0].getXMLExperimentFile().getParentFile().getAbsolutePath() +
00092 IPath.SEPARATOR + "merged" ;
00093
00094 do {
00095 path = path + (i==0? "" : "-"+String.valueOf(i));
00096 for (Experiment exp: dbArray) {
00097 File file2 = exp.getXMLExperimentFile().getParentFile();
00098 need_to_find_name = path.equals( file2.getAbsolutePath() );
00099 if (need_to_find_name) {
00100 i++;
00101 break;
00102 }
00103 }
00104 } while (need_to_find_name);
00105
00106 final Experiment expMerged = ExperimentMerger.merge(db1, db2, type, path + IPath.SEPARATOR, false);
00107
00108 ExperimentView ev = new ExperimentView(window.getActivePage());
00109 ev.generateView(expMerged);
00110 }
00111 });
00112 }
00113 else
00114 {
00115 MessageDialog.openError( window.getShell(), "Error merging database",
00116 "The number of open database has to be at least 2 to enable to merge");
00117 }
00118
00119 return null;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 private class ExperimentLabelProvider extends LabelProvider {
00129
00130 public String getText(Object element)
00131 {
00132 final Experiment exp = (Experiment) element;
00133 final File file = exp.getXMLExperimentFile();
00134
00135 final String path = file.getAbsolutePath();
00136 return path;
00137 }
00138 }
00139 }