/plugins/ProjectViewer/tags/pv_2_9_1/projectviewer/ProjectPlugin.java

# · Java · 222 lines · 120 code · 21 blank · 81 comment · 27 complexity · dcd094cb4a73f9694142aee49bf761e5 MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer;
  20. //{{{ Imports
  21. import java.io.File;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import javax.swing.SwingUtilities;
  29. import org.gjt.sp.jedit.View;
  30. import org.gjt.sp.jedit.jEdit;
  31. import org.gjt.sp.jedit.EBPlugin;
  32. import org.gjt.sp.jedit.EBMessage;
  33. import org.gjt.sp.jedit.EditBus;
  34. import org.gjt.sp.jedit.EditPlugin;
  35. import org.gjt.sp.jedit.OperatingSystem;
  36. import org.gjt.sp.jedit.msg.PluginUpdate;
  37. import org.gjt.sp.jedit.msg.ViewUpdate;
  38. import org.gjt.sp.util.Log;
  39. import projectviewer.vpt.VPTContextMenu;
  40. import projectviewer.vpt.VPTNode;
  41. import projectviewer.config.ExtensionManager;
  42. import projectviewer.config.ProjectViewerConfig;
  43. import projectviewer.persist.ProjectPersistenceManager;
  44. //}}}
  45. /**
  46. * A Project Viewer plugin for jEdit.
  47. *
  48. * @author <A HREF="mailto:burton@relativity.yi.org">Kevin A. Burton</A>
  49. * @author <A HREF="mailto:cyu77@yahoo.com">Calvin Yu</A>
  50. * @author <A HREF="mailto:ensonic@sonicpulse.de">Stefan Kost</A>
  51. * @author <A HREF="mailto:webmaster@sutternow.com">Matthew Payne</A>
  52. * @author <A HREF="mailto:vanza@users.sourceforge.net">Marcelo Vanzin</A>
  53. * @version 2.0.3
  54. */
  55. public final class ProjectPlugin extends EBPlugin {
  56. //{{{ Static Members
  57. private static File CONFIG_DIR;
  58. private static ProjectViewerConfig config;
  59. //{{{ +_getResourceAsStream(String)_ : InputStream
  60. /**
  61. * Returns an input stream to the specified resource, or <code>null</code>
  62. * if none is found.
  63. *
  64. * @param path The path to the resource to be returned, relative to
  65. * the plugin's resource path.
  66. * @return An input stream for the resource.
  67. */
  68. public static InputStream getResourceAsStream(String path) {
  69. try {
  70. return new FileInputStream(getResourcePath(path));
  71. } catch (IOException e) {
  72. return null;
  73. }
  74. } //}}}
  75. //{{{ +_getResourceAsOutputStream(String)_ : OutputStream
  76. /**
  77. * Returns an output stream to the specified resource, or <code>null</node> if access
  78. * to that resource is denied.
  79. *
  80. * @param path The path to the resource to be returned, relative to
  81. * the plugin's resource path.
  82. * @return An output stream for the resource.
  83. */
  84. public static OutputStream getResourceAsOutputStream(String path) {
  85. try {
  86. return new FileOutputStream(getResourcePath(path));
  87. } catch (IOException e) {
  88. return null;
  89. }
  90. } //}}}
  91. //{{{ +_getResourcePath(String)_ : String
  92. /**
  93. * Returns the full path of the specified plugin resource.
  94. *
  95. * @param path The relative path to the resource from the plugin's
  96. * resource path.
  97. * @return The absolute path to the resource.
  98. */
  99. public static String getResourcePath(String path)
  100. throws FileNotFoundException
  101. {
  102. if (CONFIG_DIR != null) {
  103. File f = new File(CONFIG_DIR, path);
  104. File d = f.getParentFile();
  105. if (! d.exists())
  106. d.mkdirs();
  107. return f.getAbsolutePath();
  108. } else {
  109. throw new FileNotFoundException("No config directory.");
  110. }
  111. } //}}}
  112. //}}}
  113. //{{{ +start() : void
  114. /** Start the plugin. */
  115. public void start() {
  116. /*
  117. * First, try to see if the new config directory exists.
  118. * If it doesn't, try to move the old config directory to
  119. * the new location.
  120. */
  121. File configDir = getPluginHome();
  122. if (configDir == null) {
  123. Log.log(Log.WARNING, this,
  124. "ProjectViewer won't work without a settings directory. " +
  125. "Use this setup at your own risk.");
  126. return;
  127. }
  128. if (!configDir.isDirectory()) {
  129. File oldConfig = new File(jEdit.getSettingsDirectory(),
  130. "projectviewer");
  131. if (oldConfig.isDirectory()) {
  132. File configParentDir = configDir.getParentFile();
  133. if ((!configParentDir.isDirectory()) && (!configParentDir.mkdirs())) {
  134. Log.log(Log.WARNING, this, "Cannot create plugin home dir.");
  135. configDir = oldConfig;
  136. } else if (!oldConfig.renameTo(configDir)) {
  137. Log.log(Log.WARNING, this, "Cannot move config directory.");
  138. configDir = oldConfig;
  139. }
  140. } else if (!configDir.mkdirs()) {
  141. Log.log(Log.ERROR, this, "Cannot create config directory; ProjectViewer will not function properly.");
  142. }
  143. /*
  144. * When moving the settings settings, it most probably means that
  145. * PV is being upgraded from 2.x to 3.0. Take the opportunity to
  146. * clean up the file dialog geometry info in jEdit's config file
  147. * so that the new dialog starts fresh and doesn't have any hidden
  148. * controls because of being shown too small.
  149. */
  150. jEdit.unsetProperty(projectviewer.gui.ImportDialog.class.getName() + ".height");
  151. jEdit.unsetProperty(projectviewer.gui.ImportDialog.class.getName() + ".width");
  152. jEdit.unsetProperty(projectviewer.gui.ImportDialog.class.getName() + ".x");
  153. jEdit.unsetProperty(projectviewer.gui.ImportDialog.class.getName() + ".y");
  154. }
  155. CONFIG_DIR = configDir;
  156. config = ProjectViewerConfig.getInstance();
  157. /*
  158. * set up a task to check any available extensions after
  159. * jEdit is done loading.
  160. */
  161. SwingUtilities.invokeLater(new Runnable() {
  162. public void run() {
  163. ExtensionManager.getInstance().reloadExtensions();
  164. }
  165. });
  166. } //}}}
  167. //{{{ +stop() : void
  168. /** Stop the plugin and save the project resources. */
  169. public void stop() {
  170. config.save();
  171. try {
  172. ProjectManager.getInstance().save();
  173. ProjectManager.getInstance().unload();
  174. } catch (IOException ioe) {
  175. Log.log(Log.ERROR, this, ioe);
  176. }
  177. // clean up edit bus
  178. View[] views = jEdit.getViews();
  179. for (int i = 0; i < views.length; i++) {
  180. ProjectViewer pv = ProjectViewer.getViewer(views[i]);
  181. if (pv != null) {
  182. EditBus.removeFromBus(pv);
  183. }
  184. }
  185. } //}}}
  186. //{{{ +handleMessage(EBMessage) : void
  187. /** Handles plugin load/unload messages in the EditBus. */
  188. public void handleMessage(EBMessage msg) {
  189. if (msg instanceof PluginUpdate) {
  190. ExtensionManager.getInstance().reloadExtensions();
  191. } else if (msg instanceof ViewUpdate) {
  192. ViewUpdate vu = (ViewUpdate) msg;
  193. if (vu.getWhat() == ViewUpdate.CLOSED) {
  194. ProjectViewer.cleanViewEntry(vu.getView());
  195. } else if (vu.getWhat() == ViewUpdate.CREATED
  196. && ProjectViewer.getViewer(vu.getView()) == null) {
  197. ProjectViewer.setActiveNode(vu.getView(), config.getLastNode());
  198. }
  199. }
  200. } //}}}
  201. }