KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > HibernateConsoleSaveParticipant


1 /*
2  * Created on 2004-11-01 by max
3  *
4  */

5 package org.hibernate.eclipse.console;
6
7 import java.io.File JavaDoc;
8
9 import org.eclipse.core.resources.IProject;
10 import org.eclipse.core.resources.ISaveContext;
11 import org.eclipse.core.resources.ISaveParticipant;
12 import org.eclipse.core.resources.ISavedState;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17
18 /**
19  * @author max
20  *
21  */

22 public class HibernateConsoleSaveParticipant implements ISaveParticipant {
23
24     static final String JavaDoc SAVENAME = "hibernate-console.xml";
25     
26     public void prepareToSave(ISaveContext context) throws CoreException {
27
28     }
29
30     public void saving(ISaveContext context) throws CoreException {
31         switch (context.getKind()) {
32         case ISaveContext.FULL_SAVE:
33             HibernateConsolePlugin myPluginInstance = HibernateConsolePlugin
34                     .getDefault();
35             // save the plug-in state
36
int saveNumber = context.getSaveNumber();
37             String JavaDoc saveFileName = SAVENAME + "-" + Integer.toString(saveNumber);
38             File JavaDoc f = myPluginInstance.getStateLocation().append(saveFileName)
39                     .toFile();
40             // if we fail to write, an exception is thrown and we do not update
41
// the path
42
myPluginInstance.writeStateTo(f);
43             context.map(new Path(SAVENAME), new Path(saveFileName));
44             context.needSaveNumber();
45             break;
46         case ISaveContext.PROJECT_SAVE:
47             // get the project related to this save operation
48
IProject project = context.getProject();
49             // save its information, if necessary
50
break;
51         case ISaveContext.SNAPSHOT:
52             // This operation needs to be really fast because
53
// snapshots can be requested frequently by the
54
// workspace.
55
break;
56         }
57
58     }
59
60     public void doneSaving(ISaveContext context) {
61         HibernateConsolePlugin myPluginInstance = HibernateConsolePlugin
62                 .getDefault();
63
64         // delete the old saved state since it is not necessary anymore
65
int previousSaveNumber = context.getPreviousSaveNumber();
66         String JavaDoc oldFileName = "save-" + Integer.toString(previousSaveNumber);
67         File JavaDoc f = myPluginInstance.getStateLocation().append(oldFileName)
68                 .toFile();
69         //System.out.println("delete " + f);
70
f.delete();
71     }
72
73     public void rollback(ISaveContext context) {
74         HibernateConsolePlugin myPluginInstance = HibernateConsolePlugin
75                 .getDefault();
76
77         // since the save operation has failed, delete the saved state we have
78
// just written
79
int saveNumber = context.getSaveNumber();
80         String JavaDoc saveFileName = "save-" + Integer.toString(saveNumber);
81         File JavaDoc f = myPluginInstance.getStateLocation().append(saveFileName)
82                 .toFile();
83         f.delete();
84     }
85
86     public void doStart(HibernateConsolePlugin plugin) throws CoreException {
87         ISavedState lastState = ResourcesPlugin.getWorkspace().addSaveParticipant(plugin, this);
88         if (lastState == null) {
89             return;
90         }
91         IPath location = lastState.lookup(new Path(HibernateConsoleSaveParticipant.SAVENAME));
92         if (location == null) {
93             return;
94         }
95         File JavaDoc f = plugin.getStateLocation().append(location).toFile();
96         plugin.readStateFrom(f);
97     }
98
99 }
100
Popular Tags