1 2 17 package com.buchuki.ensmer; 18 19 import java.io.*; 20 import java.util.*; 21 import javax.vecmath.Matrix4f; 22 import com.buchuki.ensmer.builtin.Configurator; 23 import com.buchuki.ensmer.input.InputManager; 24 import com.buchuki.ensmer.input.event.semantic.*; 25 import com.buchuki.ensmer.prevayler.queries.*; 26 27 import org.prevayler.Prevayler; 28 29 37 public class ConfigManager { 38 39 44 public ConfigManager() { 45 this(null); 46 } 47 48 57 public ConfigManager(File settingsDirectory) { 58 if (settingsDirectory == null) { 59 settingsDirectory = new File( 60 System.getProperty("user.home", ".") + File.separator + ".ensmer"); 61 } 62 if (!settingsDirectory.exists()) { 63 String ensmerHome = System.getenv("ENSMER_HOME"); 64 if (ensmerHome == null) { 65 ensmerHome = "."; 66 } 67 File skeleton = new File(ensmerHome, "skeleton"); 68 if (skeleton.exists() && skeleton.isDirectory()) { 69 try { 70 com.buchuki.util.FileUtils.copyDirectory(skeleton, settingsDirectory); 71 } catch (IOException e) { 72 settingsDirectory.mkdirs(); 73 } 74 } 75 else { 76 settingsDirectory.mkdirs(); 77 } 78 } 79 if (!settingsDirectory.isDirectory()) { 80 throw new IllegalArgumentException ("Settings directory " + settingsDirectory.getAbsolutePath() + " points to a regular file"); 81 } 82 if (!settingsDirectory.canRead() || !settingsDirectory.canWrite()) { 83 throw new IllegalArgumentException ("Insufficient privleges to access settings directory"); 84 } 85 86 this.settingsDirectory = settingsDirectory; 87 } 88 89 96 public boolean showConfigurator() { 97 EnsmerManager ensmer = EnsmerManager.instance(); 98 SelectionManager sel = ensmer.getSelectionManager(); 99 Prevayler prevayler = ensmer.getPrevayler(); 100 InputManager inMan = ensmer.getUserManager().getInputManager(); 101 final Area current = ensmer.getAreaManager().getCurrentArea(); 102 final Object [] ids = sel.getSelectedObjects(); 103 if (ids == null) { 104 return false; } 106 Configurator config = getConfigurator((Long ) ids[0]); 107 final Long id = config.getId(); 108 Matrix4f loc = SceneGraphUtils.positionInFrontOfUser(current, 1); 109 try { 110 prevayler.execute(new SetObjectLocationTransaction(id, loc)); 111 } catch (Exception e) { 112 e.printStackTrace(); 113 } 114 115 inMan.getObjectManager().addFocusListener( 116 new FocusListener() { 117 public void focusChanged(FocusEvent e) { 118 if (e.getFocusedObject() == id) { 119 if (!e.gainedFocus()) { 120 current.removeObject(id); 121 } 122 } 123 } 124 }); 125 126 current.addObject(config); 127 inMan.objectMode(id); 128 return true; 129 } 130 131 136 public File getSettingsDirectory() { 137 return settingsDirectory; 138 } 139 140 147 public File getSettingsDirectory(String subfile) { 148 return new File(settingsDirectory, subfile); 149 } 150 151 160 public Configurator getConfigurator(final Long id) { 161 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 162 try { 163 Long configID = (Long ) prevayler.execute(new GetConfiguratorIDQuery(id)); 164 Configurator config; 165 if (configCache.containsKey(configID)) { 166 return configCache.get(configID); 167 } 168 else if (configID == null) { 169 configID = (Long ) prevayler.execute(new GetNextObjectIDQuery()); 171 config = new Configurator(id); 172 prevayler.execute(new AddObjectTransaction( 173 configID, config.getClass())); 174 prevayler.execute(new SetObjectDataTransaction( 175 configID, config.getSerializable())); 176 config.setId(configID); 177 prevayler.execute(new SetConfigPropertiesTransaction(id, configID)); 178 } 179 else { 180 config = (Configurator) 182 EnsmerManager.instance().getBackhoe().getBackend(configID); 183 } 184 config.addChangeListener(new StoreChangesListener()); 185 configCache.put(configID, config); 186 return config; 187 } catch (Exception e) { 188 e.printStackTrace(); 189 return null; 190 } 191 } 192 193 199 void objectDestroyed(Long id) { 200 Prevayler prevayler = EnsmerManager.instance().getPrevayler(); 201 try { 202 Long configID = (Long ) prevayler.execute(new GetConfiguratorIDQuery(id)); 203 if (configID != null) { 204 configCache.remove(configID); 205 prevayler.execute(new RemoveObjectTransaction(id)); 206 } 207 } catch (Exception e) { 208 e.printStackTrace(); 209 } 210 } 211 212 215 private File settingsDirectory; 216 217 220 private Map<Long , Configurator> configCache = new HashMap<Long , Configurator>(); 221 } 222 223 | Popular Tags |