/plugins/OpenIt/tags/OpenIt-1_05/src/org/etheridge/openit/OpenItPlugin.java

# · Java · 253 lines · 131 code · 41 blank · 81 comment · 13 complexity · db8065d3ee93f8f7172056626a49a156 MD5 · raw file

  1. /*
  2. * OpenIt jEdit Plugin (OpenItPlugin.java)
  3. *
  4. * Copyright (C) 2003 Matt Etheridge (matt@etheridge.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.etheridge.openit;
  21. import java.awt.Dimension;
  22. import java.io.File;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Vector;
  26. import org.etheridge.openit.gui.FileSelectionListener;
  27. import org.etheridge.openit.gui.FindFileWindow;
  28. import org.etheridge.openit.OpenItProperties;
  29. import org.etheridge.openit.options.PopupOptionsPane;
  30. import org.etheridge.openit.options.SourcePathOptionsPane;
  31. import org.etheridge.openit.sourcepath.SourcePath;
  32. import org.etheridge.openit.sourcepath.SourcePathFile;
  33. import org.etheridge.openit.SourcePathManager;
  34. import org.gjt.sp.jedit.EBMessage;
  35. import org.gjt.sp.jedit.EBPlugin;
  36. import org.gjt.sp.jedit.gui.OptionsDialog;
  37. import org.gjt.sp.jedit.GUIUtilities;
  38. import org.gjt.sp.jedit.jEdit;
  39. import org.gjt.sp.jedit.msg.ViewUpdate;
  40. import org.gjt.sp.jedit.OptionGroup;
  41. import org.gjt.sp.jedit.View;
  42. /**
  43. * @author Matt Etheridge
  44. */
  45. public class OpenItPlugin extends EBPlugin
  46. {
  47. // maps from jEdit Views to the single import listener for that view.
  48. private static Map msFileSelectionListenerMap = new HashMap();
  49. // maps from jEdit Views to the single FindFileWindow instance for that
  50. // view.
  51. private static Map msFindFileWindowMap = new HashMap();
  52. //
  53. // jEdit framework/callback methods
  54. //
  55. /**
  56. * Called on jEdit startup
  57. */
  58. public void start()
  59. {
  60. // if the plugin is loaded by "deferred" loading, we should get all views
  61. // and add a listener for each. jEdit 4.1 loaded everything at startup
  62. // so we used to be able to do this inital load when we got a ViewUpdate
  63. // message, however, jEdit4.2 cannot rely on this.
  64. View[] views = jEdit.getViews();
  65. for(int i = 0; i < views.length; i++) {
  66. createListener(views[i]);
  67. }
  68. // get the SourcePathManager singleton to force start of polling thread
  69. SourcePathManager manager = SourcePathManager.getInstance();
  70. }
  71. /**
  72. * Called on jEdit shutdown
  73. */
  74. public void stop()
  75. {
  76. // tell the source path manager to stop its polling
  77. SourcePathManager.getInstance().stopSourcePathPolling();
  78. }
  79. /**
  80. * Description of the Method
  81. *
  82. * @param dialog Description of the Parameter
  83. */
  84. public void createOptionPanes(OptionsDialog dialog)
  85. {
  86. OptionGroup optionGroup = new OptionGroup(jEdit.getProperty("options.OpenIt.label"));
  87. optionGroup.addOptionPane(new SourcePathOptionsPane());
  88. optionGroup.addOptionPane(new PopupOptionsPane());
  89. dialog.addOptionGroup(optionGroup);
  90. }
  91. /**
  92. * Description of the Method
  93. *
  94. * @param menuItems Description of the Parameter
  95. */
  96. public void createMenuItems(Vector menuItems)
  97. {
  98. menuItems.addElement(GUIUtilities.loadMenu("openit.menu"));
  99. }
  100. public void handleMessage(EBMessage message)
  101. {
  102. if (message instanceof ViewUpdate) {
  103. ViewUpdate viewUpdateMessage = (ViewUpdate) message;
  104. if (viewUpdateMessage.getWhat() == ViewUpdate.CREATED) {
  105. // add a import list listener for this view
  106. View createdView = viewUpdateMessage.getView();
  107. FileSelectionListener listener = new SourceFileSelectionListener(createdView);
  108. msFileSelectionListenerMap.put(createdView, listener);
  109. getFindFileWindow(createdView).addFileSelectionListener(listener);
  110. }
  111. else if (viewUpdateMessage.getWhat() == ViewUpdate.CLOSED) {
  112. // remove import listener from import window
  113. View closedView = ((ViewUpdate) message).getView();
  114. FileSelectionListener listenerToRemove = (FileSelectionListener) msFileSelectionListenerMap.get(closedView);
  115. FindFileWindow window = (FindFileWindow) msFindFileWindowMap.get(closedView);
  116. window.removeFileSelectionListener(listenerToRemove);
  117. // remove import listener and import window from maps
  118. msFileSelectionListenerMap.remove(closedView);
  119. msFindFileWindowMap.remove(closedView);
  120. }
  121. }
  122. }
  123. //
  124. // Action Methods - these are "called" from the actions.xml configuration file.
  125. //
  126. /**
  127. * Find a file in the source path
  128. */
  129. public static void findFileInSourcePath(View view)
  130. {
  131. FindFileWindow findClassWindow = getFindFileWindow(view);
  132. // position in center of view
  133. Dimension viewSize = view.getSize();
  134. findClassWindow.setLocation( (int) view.getLocationOnScreen().getX() + ((viewSize.width - findClassWindow.getSize().width) / 2),
  135. (int) view.getLocationOnScreen().getY() + ((viewSize.height - findClassWindow.getSize().height) / 2));
  136. // show window
  137. if (jEdit.getBooleanProperty(OpenItProperties.POP_UP_CLEAN_ON_VISIBLE, true)) {
  138. findClassWindow.clearSourceFiles();
  139. }
  140. findClassWindow.show();
  141. findClassWindow.setVisible(true);
  142. }
  143. /**
  144. * Adds the current buffer's directory to the source path.
  145. */
  146. public static void addCurrentDirectoryToSourcePath(View view)
  147. {
  148. // get the directory of the current buffer
  149. String path = view.getBuffer().getPath();
  150. String name = view.getBuffer().getName();
  151. // remove the name from the path
  152. path = path.substring(0, path.lastIndexOf(name) - 1);
  153. // if the source path has not been loaded yet, put a message on the status
  154. // bar and just return
  155. if (SourcePathManager.staticGetQuickAccessSourcePath() == null) {
  156. view.getStatus().setMessageAndClear(jEdit.getProperty("openit.InitialLoadingNotComplete.StatusBarMessage"));
  157. // force the manager to start loading sourcepath
  158. SourcePathManager.getInstance();
  159. return;
  160. }
  161. // append the new path to the end of the sourcepath property
  162. String stringSourcePath = jEdit.getProperty(OpenItProperties.SOURCE_PATH_STRING, "");
  163. stringSourcePath = (stringSourcePath.trim().endsWith(File.pathSeparator) ?
  164. stringSourcePath + path : stringSourcePath + File.pathSeparator + path);
  165. jEdit.setProperty(OpenItProperties.SOURCE_PATH_STRING, stringSourcePath);
  166. // refresh the sourcepath
  167. SourcePathManager.getInstance().refreshSourcePath();
  168. // set the statusbar with an appropriate message
  169. view.getStatus().setMessageAndClear
  170. (jEdit.getProperty("openit.AddedDirectoryToSourcePath.StatusBarMessage.1") +
  171. " " + path + " " +
  172. jEdit.getProperty("openit.AddedDirectoryToSourcePath.StatusBarMessage.2"));
  173. }
  174. //
  175. // Private Helper Methods
  176. //
  177. private static FindFileWindow getFindFileWindow(View view)
  178. {
  179. FindFileWindow window = (FindFileWindow) msFindFileWindowMap.get(view);
  180. if (window == null) {
  181. window = new FindFileWindow();
  182. msFindFileWindowMap.put(view, window);
  183. }
  184. return window;
  185. }
  186. private void createListener(View view)
  187. {
  188. FileSelectionListener listener = new SourceFileSelectionListener(view);
  189. msFileSelectionListenerMap.put(view, listener);
  190. getFindFileWindow(view).addFileSelectionListener(listener);
  191. }
  192. //
  193. // Inner Classes
  194. //
  195. /**
  196. * Listens for import selection from popup import window.
  197. */
  198. private static class SourceFileSelectionListener implements FileSelectionListener
  199. {
  200. private View mView;
  201. public SourceFileSelectionListener(View view)
  202. {
  203. mView = view;
  204. }
  205. public void fileSelected(SourcePathFile sourcePathFileSelected)
  206. {
  207. if (new File(sourcePathFileSelected.getDirectoryString()).exists()) {
  208. jEdit.openFile(mView, sourcePathFileSelected.getDirectoryString());
  209. }
  210. }
  211. }
  212. }