001: package org.acm.seguin.completer;
002:
003: /*
004: * Copyright (c) 2002, Beau Tateyama
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: //import anthelper.adapters.Plugin;
022: import org.gjt.sp.jedit.EBComponent; //import anthelper.adapters.SpeedJavaUtils;
023: //import anthelper.ui.ConfigDialog;
024: //import anthelper.utils.JEditLogger;
025: import java.io.*;
026: import java.util.*;
027: import javax.swing.*;
028: import org.gjt.sp.jedit.*;
029: import org.gjt.sp.jedit.textarea.*;
030: import org.acm.seguin.ide.jedit.Navigator;
031:
032: /**
033: * Description of the Class
034: *
035: * @author btateyama@yahoo.com
036: * @created September 9, 2002
037: */
038: public class ContextMgr implements EBComponent {
039: final static Navigator.NavigatorLogger logger = Completer
040: .getLogger(ClassPathSrcMgr.class);
041: final static String FILE_NAME = "Completer.props"; // AntHelperPlugin.PLUGIN_NAME + ".props";
042:
043: final static String CONTEXT_COUNT = "ContextCount";
044: final static String BUILD_FILE = "BuildFile";
045: final static String OUTPUT_DIR = "OutputDir";
046: final static String SOURCEPATH = "SourcePath";
047: final static String SOURCEPATH_ID = "SourcePathRefID";
048: final static String CLASSPATH = "ClassPath";
049: final static String CLASSPATH_ID = "ClassPathRefID";
050: final static String TASKA = "TaskA";
051: final static String TASKB = "TaskB";
052: final static String TASKC = "TaskC";
053: final static String TASKD = "TaskD";
054: final static String SAVE_BEFORE_COMPILE = "SaveBeforeCompile";
055: final static String SAVE_BEFORE_RUN = "SaveBeforeRun";
056: final static String SYNC_TO_SPEEDJAVA = "SyncToSpeedJava";
057: final static String LAST_MOD = "LastModDateMs";
058: final static String RUN_PARAMS = "RunParams";
059: final static String JVM_OPTIONS = "JVMOptions";
060:
061: static ContextMgr _instance = new ContextMgr();
062:
063: /**
064: * Gets the instance attribute of the ContextMgr class
065: *
066: * @return The instance value
067: */
068: public static ContextMgr getInstance() {
069: return _instance;
070: }
071:
072: /**
073: * Gets the context attribute of the ContextMgr class
074: *
075: * @param argView Description of the Parameter
076: * @return The context value
077: */
078: public static Context getContext(View argView) {
079: return getContext(argView, true);
080: }
081:
082: public static Context getContext(View argView, boolean argAutoConfig) {
083: return getInstance().getContext(argView,
084: argView == null ? null : argView.getBuffer(),
085: argAutoConfig);
086: }
087:
088: public static Context getContext(View argView, Buffer argBuffer,
089: boolean argAutoConfig) {
090: return getInstance().getContext(argView,
091: findBuildFile(argBuffer), argAutoConfig);
092: }
093:
094: static File findBuildFile(Buffer argBuffer) {
095: File fileBuild = null;
096: if (argBuffer != null) {
097: File file = new File(argBuffer.getPath());
098: File[] files = null;
099: while ((file = file.getParentFile()) != null
100: && fileBuild == null) {
101: files = file.listFiles();
102: if (files != null) {
103: for (int i = 0; i < files.length; i++) {
104: if (files[i].getName().equals("build.xml")) {
105: fileBuild = files[i];
106: break;
107: }
108: }
109: }
110: }
111: }
112: return fileBuild;
113: }
114:
115: /**
116: * Description of the Method
117: *
118: * @param i Description of the Parameter
119: * @param argVal Description of the Parameter
120: * @return Description of the Return Value
121: */
122: static String makeKey(int i, String argVal) {
123: return i + "." + argVal;
124: }
125:
126: Map _mapContexts = new Hashtable();
127: File _fileConfig = null;
128:
129: /**
130: * Constructor for the ContextMgr object
131: */
132: private ContextMgr() {
133: _fileConfig = new File(jEdit.getSettingsDirectory(), FILE_NAME);
134: if (!_fileConfig.exists()) {
135: // none exists
136: logger.msg("No config exists.");
137: } else {
138: load(_fileConfig);
139: }
140:
141: EditBus.addToBus(this );
142: }
143:
144: public void handleMessage(EBMessage argMsg) {
145: if (argMsg instanceof ContextUpdateMessage) {
146: logger.debug("*****ContextUpdate!");
147: ContextUpdateMessage updateMsg = (ContextUpdateMessage) argMsg;
148: logger.debug("*****new.cp", updateMsg.getClassPath());
149: }
150: }
151:
152: /*
153: public void handleMessage(EBMessage argMsg){
154: if (argMsg instanceof anthelper.ContextUpdateMessage){
155: anthelper.ContextUpdateMessage updateMsg = (anthelper.ContextUpdateMessage) argMsg;
156: Log.log(Log.MESSAGE,this,"*****new.cp=" + updateMsg.getClassPath()););
157: }
158: }
159: */
160:
161: /**
162: * Gets the context attribute of the ContextMgr object
163: *
164: * @param argFilePath Description of the Parameter
165: * @return The context value
166: */
167: Context getContext(View argView, File argBuildFile,
168: boolean argAutoConfig) {
169: Context context = null;
170: if (argBuildFile == null) {
171: if (argAutoConfig) {
172: JOptionPane.showMessageDialog(argView,
173: "No build file found.");
174: } else {
175: logger.warning("No build file found");
176: }
177: } else {
178: context = (Context) _mapContexts
179: .get(argBuildFile.getPath());
180:
181: if (argAutoConfig && context == null) {
182: // no context exists so create one
183: String strMsg = "Create profile for ("
184: + argBuildFile.getPath() + ")?";
185: int iReturn = JOptionPane.showConfirmDialog(argView,
186: strMsg, "Create Profile",
187: JOptionPane.YES_NO_OPTION);
188: if (iReturn == JOptionPane.YES_OPTION) {
189: context = new Context(argBuildFile);
190: context = doConfig(argView, context);
191: }
192: } else if (argAutoConfig
193: && context.getLastMod() < argBuildFile
194: .lastModified()) {
195: // configuration to turn off build file changed nag message.
196: boolean bAutoUpdate = jEdit.getBooleanProperty(
197: "TOGGLE_UPDATE_ALERT", true); //FIXME: AntHelperPlugin.ACTION_TOGGLE_UPDATE_ALERT, true);
198: if (bAutoUpdate) {
199: JOptionPane
200: .showMessageDialog(argView,
201: "Build file updated. Please update configuration.");
202: context = doConfig(argView, context);
203: }
204: } else {
205: // do nothing, context is fine the way it is
206: }
207: }
208:
209: /* should i update other components every time the context is retrieved (i.e. all actions)
210: if (context != null){
211: // things we always update on a context
212:
213: // update antfarm by executing a bogus ("") task
214: AntHelperPlugin.executeTask(argView, context.getBuildFile(), "");
215: }
216: */
217: return context;
218: }
219:
220: static Context doConfig(View argView, Context argContext) {
221: /*
222: Context context = null;
223: ConfigDialog cd = new ConfigDialog(argView, argContext);
224: cd.show();
225: if (cd.cancel()){
226: logger.msg("Cancelled profile edit\n(" + argContext.getBuildFile().getPath() + ")");
227: }else{
228: context = cd.getContext();
229: logger.msg(context.toString());
230: ContextMgr.getInstance().storeContext(context);
231: ContextMgr.getInstance().save();
232: logger.msg("Saved profile\n(" + context.getBuildFile().getPath() + ")");
233: // fire update message
234: ContextUpdateMessage updateMsg = new ContextUpdateMessage(ContextMgr.getInstance(),
235: context);
236: EditBus.send(updateMsg);
237: }
238: return context;
239: */
240: }
241:
242: void storeContext(Context argContext) {
243: if (argContext != null) {
244: _mapContexts.put(argContext.getBuildFile().getPath(),
245: argContext);
246: if (argContext.getSyncToSpeedJava()
247: && argContext.getClassPath() != null) {
248: // set the prop for speedjava and update the classpath mgr
249: jEdit.setProperty("speedjava.classpath", argContext
250: .getClassPath());
251: //FIXME: if (Plugin.SPEEDJAVA.isInstalled()){
252: //FIXME: SpeedJavaUtils.updateClassPath(argContext.getClassPath());
253: //FIXME: }
254: }
255: }
256: }
257:
258: /**
259: * Load the saved contexts from the configuration file
260: *
261: * @param argConfigFile Description of the Parameter
262: */
263: void load(File argConfigFile) {
264:
265: logger.msg("Loading config file:" + argConfigFile.getPath());
266: try {
267: // load from properties
268: Properties p = new Properties();
269: FileInputStream fis = new FileInputStream(argConfigFile);
270: BufferedInputStream bis = new BufferedInputStream(fis);
271: p.load(fis);
272:
273: // now unserialize the contexts within
274: // @TODO: some error checking...
275: String strCount = p.getProperty(CONTEXT_COUNT);
276: if (strCount != null) {
277: int iCount = Integer.parseInt(strCount);
278: Context context = null;
279: String strVal = null;
280: File fileBuild = null;
281: for (int i = 0; i < iCount; i++) {
282: try {
283: // get the build file
284: strVal = p.getProperty(makeKey(i, BUILD_FILE));
285: fileBuild = new File(strVal);
286: // create a context
287: context = new Context(fileBuild
288: .getCanonicalFile());
289:
290: // set other props
291: context.setAntTaskA(p.getProperty(makeKey(i,
292: TASKA)));
293: context.setAntTaskB(p.getProperty(makeKey(i,
294: TASKB)));
295: context.setAntTaskC(p.getProperty(makeKey(i,
296: TASKC)));
297: context.setAntTaskD(p.getProperty(makeKey(i,
298: TASKD)));
299: context.setClassPath(p.getProperty(makeKey(i,
300: CLASSPATH)));
301: context.setClassPathRefID(p
302: .getProperty(makeKey(i, CLASSPATH_ID)));
303: context.setSourcePath(p.getProperty(makeKey(i,
304: SOURCEPATH)));
305: context
306: .setSourcePathRefID(p
307: .getProperty(makeKey(i,
308: SOURCEPATH_ID)));
309:
310: context.setOutputDirectory(new File(p
311: .getProperty(makeKey(i, OUTPUT_DIR))));
312:
313: context.setSaveBeforeCompile(p
314: .getProperty(makeKey(i,
315: SAVE_BEFORE_COMPILE)) != null);
316: context.setSaveBeforeRun(p.getProperty(makeKey(
317: i, SAVE_BEFORE_RUN)) != null);
318: context.setSyncToSpeedJava(p
319: .getProperty(makeKey(i,
320: SYNC_TO_SPEEDJAVA)) != null);
321:
322: context.setLastMod(Long.parseLong(p
323: .getProperty(makeKey(i, LAST_MOD))));
324:
325: context.setJVMOptions(p.getProperty(makeKey(i,
326: JVM_OPTIONS)));
327: context.setRunParams(p.getProperty(makeKey(i,
328: RUN_PARAMS)));
329:
330: storeContext(context);
331: logger.msg("Loading context for build file("
332: + fileBuild.getPath() + ")");
333: } catch (IOException e) {
334: logger
335: .warning("Error loading context. Proceeding to next...");
336: e.printStackTrace();
337: }
338: }
339: }
340: } catch (IOException e) {
341: logger.error("Error loading config file");
342: e.printStackTrace();
343: } catch (Exception e) {
344: logger.error("Misc error parsing config file");
345: e.printStackTrace();
346: }
347:
348: }
349:
350: void save() {
351: save(_fileConfig);
352: }
353:
354: /**
355: * Description of the Method
356: *
357: * @param argFileConfig Description of the Parameter
358: */
359: void save(File argFileConfig) {
360: logger.msg("file.save", argFileConfig.getPath());
361: synchronized (_mapContexts) {
362: Properties p = new Properties();
363:
364: Context c = null;
365: int i = 0;
366: File fileBuild = null, fileOutputDir = null;
367: for (Iterator iter = _mapContexts.values().iterator(); iter
368: .hasNext();) {
369: c = (Context) iter.next();
370: fileBuild = c.getBuildFile();
371: fileOutputDir = c.getOutputDirectory();
372:
373: // null contexts are not saved
374: if (fileBuild != null) {
375: saveProperty(p, makeKey(i, BUILD_FILE), fileBuild
376: .getPath());
377: saveProperty(p, makeKey(i, OUTPUT_DIR),
378: fileOutputDir == null ? null
379: : fileOutputDir.getPath());
380:
381: saveProperty(p, makeKey(i, TASKA), c.getAntTaskA());
382: saveProperty(p, makeKey(i, TASKB), c.getAntTaskB());
383: saveProperty(p, makeKey(i, TASKC), c.getAntTaskC());
384: saveProperty(p, makeKey(i, TASKD), c.getAntTaskD());
385:
386: saveProperty(p, makeKey(i, CLASSPATH), c
387: .getClassPath());
388: saveProperty(p, makeKey(i, CLASSPATH_ID), c
389: .getClassPathRefID());
390: saveProperty(p, makeKey(i, SOURCEPATH), c
391: .getSourcePath());
392: saveProperty(p, makeKey(i, SOURCEPATH_ID), c
393: .getSourcePathRefID());
394:
395: saveProperty(p, makeKey(i, JVM_OPTIONS), c
396: .getJVMOptions());
397: saveProperty(p, makeKey(i, RUN_PARAMS), c
398: .getRunParams());
399:
400: if (c.getSaveBeforeCompile()) {
401: saveProperty(p,
402: makeKey(i, SAVE_BEFORE_COMPILE), "true");
403: }
404: if (c.getSaveBeforeRun()) {
405: saveProperty(p, makeKey(i, SAVE_BEFORE_RUN),
406: "true");
407: }
408: if (c.getSyncToSpeedJava()) {
409: saveProperty(p, makeKey(i, SYNC_TO_SPEEDJAVA),
410: "true");
411: }
412: saveProperty(p, makeKey(i, LAST_MOD), String
413: .valueOf(c.getLastMod()));
414: i++;
415: }
416: }
417: p.setProperty(CONTEXT_COUNT, String.valueOf(i));
418: try {
419: FileOutputStream fos = new FileOutputStream(
420: argFileConfig);
421: BufferedOutputStream bos = new BufferedOutputStream(fos);
422: p.store(bos, "Saved at: " + new Date().toString());
423: } catch (IOException e) {
424: logger.error("Error saving contexts");
425: e.printStackTrace();
426: }
427: }
428: }
429:
430: /**
431: * Description of the Method
432: *
433: * @param argProps Description of the Parameter
434: * @param argKeyName Description of the Parameter
435: * @param argValue Description of the Parameter
436: */
437: static void saveProperty(Properties argProps, String argKeyName,
438: String argValue) {
439: if (argProps != null && argKeyName != null && argValue != null) {
440: argProps.setProperty(argKeyName, argValue);
441: }
442: }
443:
444: public static void main(String[] args) {
445: try {
446:
447: } catch (Throwable t) {
448: t.printStackTrace();
449: }
450: }
451:
452: }
|