package net.sourceforge.phpdt.sql; import java.io.File; import java.net.URL; import java.util.ResourceBundle; import org.eclipse.core.resources.ISaveContext; import org.eclipse.core.resources.ISaveParticipant; import org.eclipse.core.resources.ISavedState; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPluginDescriptor; import org.eclipse.core.runtime.Path; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.RGB; import org.eclipse.ui.plugin.AbstractUIPlugin; import net.sourceforge.phpdt.sql.sql.MultiSQLServer; import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider; /** * @author */ public class PHPEclipseSQLPlugin extends AbstractUIPlugin implements IConstants { private static final String BUNDLE_NAME = "net.sourceforge.phpdt.sql.PHPEclipseSQLResources"; private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); private static PHPEclipseSQLPlugin plugin; public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.sql"; public PHPEclipseSQLPlugin(IPluginDescriptor descriptor) { super(descriptor); plugin = this; } public static PHPEclipseSQLPlugin getDefault() { return plugin; } protected void readStateFrom(File target) { BookmarkContentProvider.getInstance().load(target); } public void startup() throws CoreException { super.startup(); ISaveParticipant saveParticipant = new QuantumSaveParticipant(); ISavedState lastState = ResourcesPlugin.getWorkspace().addSaveParticipant(this, saveParticipant); if (lastState == null) return; IPath location = lastState.lookup(new Path("save")); if (location == null) return; // the plugin instance should read any important state from the file. File f = getStateLocation().append(location).toFile(); readStateFrom(f); } protected void writeImportantState(File target) { BookmarkContentProvider.getInstance().save(target); } public static ImageDescriptor getImageDescriptor(String name) { ImageDescriptor descriptor = null; try { URL installURL = PHPEclipseSQLPlugin.getDefault().getDescriptor().getInstallURL(); URL url = new URL(installURL, "icons/" + name); descriptor = ImageDescriptor.createFromURL(url); } catch (Exception e) { e.printStackTrace(); } return descriptor; } public static Image getImage(String name) { return getImageDescriptor(name).createImage(); } public void dispose() throws CoreException { MultiSQLServer.getInstance().shutdown(); } protected void initializeDefaultPluginPreferences() { RGB BACKGROUND = new RGB(255, 255, 255); RGB COMMENT = new RGB(88, 148, 64); RGB IDENTIFIER = new RGB(0, 0, 0); RGB KEYWORD = new RGB(126, 0, 75); RGB STRING = new RGB(0, 0, 255); RGB NUMERIC = new RGB(255, 0, 0); RGB DEFAULT = new RGB(0, 0, 0); IPreferenceStore store = getPreferenceStore(); PreferenceConverter.setDefault( store, "quantum.background.color", BACKGROUND); PreferenceConverter.setDefault(store, "quantum.text.color", DEFAULT); PreferenceConverter.setDefault(store, "quantum.keyword.color", KEYWORD); PreferenceConverter.setDefault(store, "quantum.comment.color", COMMENT); PreferenceConverter.setDefault(store, "quantum.string.color", STRING); PreferenceConverter.setDefault(store, "quantum.numeric.color", NUMERIC); getPreferenceStore().setDefault("quantum.text.bold", false); getPreferenceStore().setDefault("quantum.keyword.bold", true); getPreferenceStore().setDefault("quantum.string.bold", false); getPreferenceStore().setDefault("quantum.comment.bold", false); getPreferenceStore().setDefault("quantum.numeric.bold", false); PreferenceConverter.setDefault( getPreferenceStore(), "quantum.font", (FontData) null); } } class QuantumSaveParticipant implements ISaveParticipant, IConstants { /** * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext) */ public void doneSaving(ISaveContext context) { } /** * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext) */ public void prepareToSave(ISaveContext context) throws CoreException { } /** * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext) */ public void rollback(ISaveContext context) { } /** * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext) */ public void saving(ISaveContext context) throws CoreException { switch (context.getKind()) { case ISaveContext.FULL_SAVE : PHPEclipseSQLPlugin quantumPluginInstance = PHPEclipseSQLPlugin.getDefault(); // save the plug in state if (BookmarkContentProvider.getInstance().hasChanged()) { int saveNumber = context.getSaveNumber(); String saveFileName = "save " + Integer.toString(saveNumber); File f = quantumPluginInstance .getStateLocation() .append(saveFileName) .toFile(); // if we fail to write, an exception is thrown and we do not update the path quantumPluginInstance.writeImportantState(f); context.map(new Path("save"), new Path(saveFileName)); context.needSaveNumber(); } else { if (DEBUG) { System.out.println("Not saving unchanged bookmarks"); } } break; case ISaveContext.PROJECT_SAVE : // get the project related to this save operation //IProject project = context.getProject(); // save its information, if necessary break; case ISaveContext.SNAPSHOT : // This operation needs to be really fast because // snapshots can be requested frequently by the // workspace. break; } } }