package org.mongodb.meclipse; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.ResourceBundle; import java.util.Set; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.jface.wizard.IWizard; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.eclipse.ui.wizards.IWizardDescriptor; import org.mongodb.meclipse.preferences.MongoInstance; import org.mongodb.meclipse.views.FilterPlacement; import org.mongodb.meclipse.views.MeclipseView; import org.mongodb.meclipse.views.objects.Filter; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import com.csvreader.CsvReader; import com.csvreader.CsvWriter; /** * The activator class controls the plug-in life cycle * * @author Flavio [FlaPer87] Percoco Premoli, walknwind */ public class MeclipsePlugin extends AbstractUIPlugin { // The plug-in ID public static final String PLUGIN_ID = "org.mongodb.meclipse"; private HashMap<String, MongoInstance> mongoInstances = new HashMap<String, MongoInstance>(); private HashMap<FilterPlacement, Set<Filter>> filters = new HashMap<FilterPlacement, Set<Filter>>(); private MeclipseView mongoDbView; private static final ResourceBundle bundle = ResourceBundle .getBundle("messages"); public static String getCaption(String key) { return bundle.getString(key); } public void setMongoDbView(MeclipseView mongoDbView) { this.mongoDbView = mongoDbView; } public MeclipseView getMongoDbView() { return mongoDbView; } // The shared instance private static MeclipsePlugin plugin; /** * The constructor */ public MeclipsePlugin() { } /* * (non-Javadoc) * * @see * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext * ) */ public void start(BundleContext context) throws Exception { super.start(context); plugin = this; MongoInstance[] savedServers = loadSavedServers(); for (MongoInstance savedServer : savedServers) { mongoInstances.put(savedServer.getName(), savedServer); } } /* * (non-Javadoc) * * @see * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext * ) */ public void stop(BundleContext context) throws Exception { plugin = null; saveServers(); super.stop(context); } /** * Returns the shared instance * * @return the shared instance */ public static MeclipsePlugin getDefault() { return plugin; } /** * Taken from EclipseTracPlugin * * Returns an image descriptor for the image file at the given plug-in * relative path * * @param path * the path * @return the image descriptor */ protected static ImageDescriptor getImageDescriptor(String path) { return imageDescriptorFromPlugin(PLUGIN_ID, path); } // IMAGE HANDLING public static final String COLLECTION_IMG_ID = "./icons/table.png"; public static final String CONNECTION_IMG_ID = "./icons/leaf.png"; public static final String CONNECTION_ERROR_IMG_ID = "./icons/leaf_error.png"; public static final String DATABASE_IMG_ID = "./icons/database.png"; public static final String FILTER_IMG_ID = "./icons/table_go.png"; public static final String GET_ALL_IMG_ID = "./icons/getAll.gif"; public static final String GET_NEXT_IMG_ID = "./icons/next.gif"; public static final String FIND_IMG_ID = "./icons/find.gif"; public static final String HELP_IMG_ID = "./icons/help.gif"; final List<String> IMG_ID_LIST = new ArrayList<String>() { /** * Generated by Eclipse */ private static final long serialVersionUID = 666142153396600669L; { add(COLLECTION_IMG_ID); add(CONNECTION_IMG_ID); add(CONNECTION_ERROR_IMG_ID); add(DATABASE_IMG_ID); add(FILTER_IMG_ID); add(GET_NEXT_IMG_ID); add(GET_ALL_IMG_ID); add(FIND_IMG_ID); add(HELP_IMG_ID); } }; @Override protected void initializeImageRegistry(ImageRegistry registry) { super.initializeImageRegistry(registry); Bundle bundle = Platform.getBundle(PLUGIN_ID); for (String img_id : IMG_ID_LIST) { ImageDescriptor myImage = ImageDescriptor.createFromURL(FileLocator .find(bundle, new Path(img_id), null)); registry.put(img_id, myImage); } } public void addMongo(String name, MongoInstance conn) { this.mongoInstances.put(name, conn); } public MongoInstance getMongoInstance(String name) { return mongoInstances.get(name); } public Set<String> getMongoNames() { return mongoInstances.keySet(); } public void addFilter(FilterPlacement placement, Filter filter) { Set<Filter> filtersThere = filters.get(placement); if (filtersThere == null) filtersThere = new HashSet<Filter>(); filtersThere.add(filter); filters.put(placement, filtersThere); getMongoDbView().refreshMe(); } private MongoInstance[] loadSavedServers() { CsvReader reader = null; try { IPath libPath = MeclipsePlugin.getDefault().getStateLocation(); libPath = libPath.append("servers.cfg"); File file = libPath.toFile(); if (!file.exists()) return new MongoInstance[0]; reader = new CsvReader(new BufferedReader(new FileReader(file))); java.util.List<MongoInstance> savedServersList = new ArrayList<MongoInstance>(); while (reader.readRecord()) { MongoInstance server = new MongoInstance(reader.get(0)); server.setHost(reader.get(1)); try { server.setPort(Integer.valueOf(reader.get(2))); } catch (NumberFormatException e) { System.out.println(e); } savedServersList.add(server); } return savedServersList.toArray(new MongoInstance[savedServersList .size()]); } catch (IOException ex) { ex.printStackTrace(); } finally { if (reader != null) { reader.close(); } } return new MongoInstance[0]; } private void saveServers() { // save server preferences here CsvWriter writer = null; try { IPath libPath = getStateLocation(); libPath = libPath.append("servers.cfg"); File file = libPath.toFile(); if (!file.exists()) { file.createNewFile(); } writer = new CsvWriter(new FileWriter(file, false), ','); for (MongoInstance server : mongoInstances.values()) { writer.write(server.getName()); writer.write(server.getHost()); writer.write(String.valueOf(server.getPort())); writer.endRecord(); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (writer != null) { writer.close(); } } } public void removeMongo(String name) { mongoInstances.remove(name); } /** * Stolen from * http://torkildr.blogspot.com/2010/07/invoking-eclipse-wizard.html * * @param id */ public void openWizard(String id) { // First see if this is a "new wizard". IWizardDescriptor descriptor = PlatformUI.getWorkbench() .getNewWizardRegistry().findWizard(id); // If not check if it is an "import wizard". if (descriptor == null) { descriptor = PlatformUI.getWorkbench().getImportWizardRegistry() .findWizard(id); } // Or maybe an export wizard if (descriptor == null) { descriptor = PlatformUI.getWorkbench().getExportWizardRegistry() .findWizard(id); } try { // Then if we have a wizard, open it. if (descriptor != null) { IWizard wizard = descriptor.createWizard(); WizardDialog wd = new WizardDialog(PlatformUI.getWorkbench() .getDisplay().getActiveShell(), wizard); wd.setTitle(wizard.getWindowTitle()); wd.open(); } } catch (CoreException e) { e.printStackTrace(); } } public Set<Filter> getFilters(FilterPlacement filterPlacement) { return filters.get(filterPlacement); } }