001: package com.vividsolutions.jump.workbench.ui.plugin;
002:
003: import java.awt.event.ComponentAdapter;
004: import java.awt.event.ComponentEvent;
005: import java.io.BufferedReader;
006: import java.io.BufferedWriter;
007: import java.io.File;
008: import java.io.FileReader;
009: import java.io.FileWriter;
010:
011: import com.vividsolutions.jump.util.Blackboard;
012: import com.vividsolutions.jump.util.java2xml.Java2XML;
013: import com.vividsolutions.jump.util.java2xml.XML2Java;
014: import com.vividsolutions.jump.workbench.WorkbenchContext;
015: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
016: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
017:
018: public class PersistentBlackboardPlugIn extends AbstractPlugIn {
019: private static String persistenceDirectory = ".";
020:
021: private static String fileName = "workbench-state.xml";
022:
023: private static final String BLACKBOARD_KEY = PersistentBlackboardPlugIn.class
024: .getName()
025: + " - BLACKBOARD";
026:
027: public static Blackboard get(WorkbenchContext context) {
028: Blackboard blackboard = context.getBlackboard();
029: return get(blackboard);
030: }
031:
032: public static Blackboard get(Blackboard blackboard) {
033: if (blackboard.get(BLACKBOARD_KEY) == null) {
034: blackboard.put(BLACKBOARD_KEY, new Blackboard());
035: }
036: return (Blackboard) blackboard.get(BLACKBOARD_KEY);
037: }
038:
039: public static void setPersistenceDirectory(String value) {
040: persistenceDirectory = value;
041: }
042:
043: public static void setFileName(String value) {
044: fileName = value;
045: }
046:
047: public String getFilePath() {
048: return persistenceDirectory + "/" + fileName;
049: }
050:
051: public void initialize(final PlugInContext context)
052: throws Exception {
053: restoreState(context.getWorkbenchContext());
054: context.getWorkbenchFrame().addComponentListener(
055: new ComponentAdapter() {
056: public void componentHidden(ComponentEvent e) {
057: saveState(context.getWorkbenchContext());
058: }
059: });
060: }
061:
062: private void restoreState(WorkbenchContext workbenchContext) {
063: if (!new File(getFilePath()).exists()) {
064: return;
065: }
066: try {
067: FileReader fileReader = new FileReader(getFilePath());
068:
069: try {
070: BufferedReader bufferedReader = new BufferedReader(
071: fileReader);
072:
073: try {
074: get(workbenchContext).putAll(
075: ((Blackboard) new XML2Java(workbenchContext
076: .getWorkbench().getPlugInManager()
077: .getClassLoader()).read(
078: bufferedReader, Blackboard.class))
079: .getProperties());
080: } finally {
081: bufferedReader.close();
082: }
083: } finally {
084: fileReader.close();
085: }
086: } catch (Exception e) {
087: // Before we just ate exceptions. But this is confusing when
088: // there is a problem and we don't know that the cause is an
089: // exception [Jon Aquino 2005-03-11]
090: e.printStackTrace(System.err);
091: }
092: }
093:
094: private void saveState(WorkbenchContext workbenchContext) {
095: try {
096: FileWriter fileWriter = new FileWriter(getFilePath(), false);
097:
098: try {
099: BufferedWriter bufferedWriter = new BufferedWriter(
100: fileWriter);
101:
102: try {
103: new Java2XML().write(get(workbenchContext),
104: "workbench-state", bufferedWriter);
105: bufferedWriter.flush();
106: fileWriter.flush();
107: } finally {
108: bufferedWriter.close();
109: }
110: } finally {
111: fileWriter.close();
112: }
113: } catch (Exception e) {
114: e.printStackTrace(System.err);
115: //Eat it. Persistence isn't critical. [Jon Aquino]
116: }
117: }
118:
119: }
|