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