PageRenderTime 55ms CodeModel.GetById 38ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XInsert/src/XInsertPlugin.java

#
Java | 189 lines | 125 code | 23 blank | 41 comment | 26 complexity | 59923bb8697a7076ea179157a2caebe1 MD5 | raw file
  1/*
  2* 16:57:45 23/10/99
  3*
  4
  5* XInsertPlugin.java - Insert pane based on XML
  6* Original version Copyright (C) 1999 Romain Guy -  powerteam@chez.com
  7* This version Copyright (C) 2000 Dominic Stolerman - dominic@sspd.org.uk
  8* www.chez.com/powerteam
  9* Changes (c) 2005 by Martin Raspe - hertzhaft@biblhertz.it
 10*
 11* This program is free software; you can redistribute it and/or
 12* modify it under the terms of the GNU General Public License
 13* as published by the Free Software Foundation; either version 2
 14* of the License, or any later version.
 15*
 16* This program is distributed in the hope that it will be useful,
 17* but WITHOUT ANY WARRANTY; without even the implied warranty of
 18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19* GNU General Public License for more details.
 20*
 21* You should have received a copy of the GNU General Public License
 22* along with this program; if not, write to the Free Software
 23* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 24*/
 25
 26import org.gjt.sp.jedit.*;
 27import org.gjt.sp.jedit.gui.OptionsDialog;
 28import org.gjt.sp.util.Log;
 29
 30import java.io.*;
 31
 32import java.util.Properties;
 33import java.util.Date;
 34import java.util.Enumeration;
 35import java.text.DateFormat;
 36
 37
 38/**
 39*  Description of the Class
 40*
 41
 42* @author     Dominic Stolerman
 43
 44 * The <code>Plugin</code> implementation for the XInsert window
 45**/
 46
 47public class XInsertPlugin extends EditPlugin {
 48
 49  private static Properties variables;
 50
 51/**
 52  * plugin name
 53  */
 54  public final static String NAME = "XInsert";
 55
 56  public String getName() {
 57    return NAME;
 58  }
 59
 60/**
 61  * Starts the plugin, loads variables.
 62  */
 63  public void start() {
 64    String sep = System.getProperty("file.separator");
 65    if(jEdit.getProperty("xinsert.inserts-directory") == null 
 66    || jEdit.getProperty("xinsert.inserts-directory").equals("")) {
 67      String defDir = jEdit.getSettingsDirectory() + sep + "xinsert" + sep;
 68      jEdit.setProperty("xinsert.inserts-directory", defDir);
 69      Log.log(Log.MESSAGE, this, ("XInsert Inserts Directory set to: " + defDir));
 70      }
 71    variables = new Properties();
 72    try {
 73      InputStream is = jEdit.class.getResourceAsStream("net.sourceforge.jedit.xinsert.variables");
 74      if(is != null) {
 75        loadVariables(is);
 76        }
 77      }
 78    catch(IOException e) {
 79      Log.log(Log.ERROR, XInsertPlugin.class, "Error loading system variables");
 80      Log.log(Log.ERROR, XInsertPlugin.class, e);
 81      }
 82    try {
 83      File glVars = new File(MiscUtilities.constructPath(jEdit.getSettingsDirectory(), "xinsert" + sep + "variables"));
 84      if(glVars.exists() && glVars.isFile()) {
 85        BufferedInputStream buf = new BufferedInputStream(new FileInputStream(glVars));
 86        loadVariables(buf);
 87        }
 88      }
 89    catch(IOException e) {
 90      Log.log(Log.ERROR, XInsertPlugin.class, "Error loading user defined global variables");
 91      Log.log(Log.ERROR, XInsertPlugin.class, e);
 92      }
 93    }
 94
 95/**
 96  * Stops the plugin, saves variables
 97  */
 98  public void stop() {
 99    File glVars;
100    try {
101      File gldir = new File(jEdit.getSettingsDirectory(), "xinsert");
102      gldir.mkdirs();
103      glVars = new File(gldir, "variables");
104      glVars.createNewFile();
105      BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(glVars));
106      saveVariables(buf);
107      }
108    catch(IOException e) {
109      Log.log(Log.ERROR, XInsertPlugin.class, "Error saving user defined global variables");
110      Log.log(Log.ERROR, XInsertPlugin.class, e);
111      }
112    }
113
114  public static void setVariable(String name, String value) {
115    variables.put(name, value);
116    }
117
118  public static String getVariable(String name) {
119    return (String)variables.get(name);
120    }
121
122  public static Enumeration getVariables() {
123    return variables.propertyNames();
124    }
125
126  public static int getVariablesSize() {
127    return variables.size();
128    }
129
130  public static String getViewSpecificVariable(View view, String key) {
131    Buffer buffer = view.getBuffer();
132    if(key.equals("path")) {
133      return Utilities.replace(buffer.getPath(), "\\", "\\\\");
134      }
135    else if(key.equals("name")) {
136      return Utilities.replace(buffer.getName(), "\\", "\\\\");
137      }
138    else if(key.equals("filename")) {
139      String path = buffer.getPath();
140      if(path != null) return (new File(path)).getName();
141      }
142    else if(key.equals("directory")) {
143      File path = new File(buffer.getPath());
144      if(path != null) return Utilities.replace(path.getParent(), "\\", "\\\\");
145      }
146    else if(key.equals("date")) {
147      DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
148      return df.format(new Date(System.currentTimeMillis()));
149      }
150    else if(key.equals("time")) {
151      DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG);
152      return df.format(new Date(System.currentTimeMillis()));
153      }
154    else if(key.equals("datetime")) {
155      DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
156      return df.format(new Date(System.currentTimeMillis()));
157      }
158    else if(key.equals("selected")) {
159      return view.getTextArea().getSelectedText();
160      }
161    return null;
162    }
163
164  public static boolean containsVariable(String name) {
165    return variables.containsKey(name);
166    }
167
168  public static void loadVariables(InputStream in) throws IOException {
169    variables.load(in);
170    }
171
172  public static void saveVariables(OutputStream out) {
173    try {
174      variables.store(out, "XInsert Global Variables File");
175      }
176    catch(IOException e) {
177      Log.log(Log.ERROR, XInsertPlugin.class,
178         "Failure to write XInsert variables to disk.");
179      Log.log(Log.ERROR, XInsertPlugin.class, e);
180      }
181    }
182
183  public static void clearVariables() {
184    variables.clear();
185    }
186
187
188  }
189