/plugins/QuickOpen/trunk/src/main/java/hohonuuli/jedit/quickopen/QuickOpenPlugin.java

# · Java · 176 lines · 107 code · 25 blank · 44 comment · 23 complexity · 814229a76c1a9f32cb724bee7ab58735 MD5 · raw file

  1. /*
  2. * @(#)QuickOpenPlugin.java 2011.01.21 at 03:18:25 PST
  3. *
  4. * Copyright 2011 Brian Schlining
  5. *
  6. *
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. package hohonuuli.jedit.quickopen;
  14. import org.gjt.sp.jedit.EBMessage;
  15. import org.gjt.sp.jedit.EBPlugin;
  16. import org.gjt.sp.jedit.EditBus.EBHandler;
  17. import org.gjt.sp.jedit.EditPlugin;
  18. import org.gjt.sp.jedit.View;
  19. import org.gjt.sp.jedit.msg.BufferUpdate;
  20. import org.gjt.sp.jedit.msg.PropertiesChanged;
  21. import org.gjt.sp.jedit.msg.VFSPathSelected;
  22. import org.gjt.sp.jedit.msg.ViewUpdate;
  23. import org.gjt.sp.util.Log;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.concurrent.ConcurrentMap;
  28. /**
  29. *
  30. *
  31. * @version Enter version here..., 2011.01.15 at 11:08:15 PST
  32. * @author Brian Schlining [brian@mbari.org]
  33. */
  34. public class QuickOpenPlugin extends EBPlugin {
  35. /** */
  36. public static final String NAME = "quickopen";
  37. /** */
  38. public static final String OPTION_PREFIX = "options.quickopen.";
  39. private static final ConcurrentMap<View, QuickOpenFrame> frameMap = new ConcurrentHashMap<View, QuickOpenFrame>();
  40. protected static final String PROP_EXCLUDE_DIR = "quickopen.exclude-directories";
  41. protected static final String PROP_EXCLUDE_EXT = "quickopen.exclude-extensions";
  42. protected static final String PROP_INCLUDE_HIDDEN = "quickopen.include-hidden";
  43. protected static final String PROP_MAX_FILES = "quickopen.maximum-files";
  44. protected static final String PROP_MATCH_FIRST = "quickopen.match-first";
  45. /**
  46. *
  47. * @param view
  48. * @return
  49. */
  50. public static QuickOpenFrame getQuickOpenFrame(View view) {
  51. QuickOpenFrame frame = null;
  52. frame = frameMap.get(view);
  53. if (frame == null) {
  54. frame = new QuickOpenFrame(view);
  55. frame.updateFileList();
  56. frame.pack();
  57. frameMap.put(view, frame);
  58. frame.setLocationRelativeTo(view);
  59. }
  60. return frame;
  61. }
  62. @Override
  63. public void handleMessage(EBMessage message) {
  64. Log.log(Log.DEBUG, this, "Received " + message);
  65. if (message instanceof VFSPathSelected) {
  66. handleVFSPathSelected((VFSPathSelected) message);
  67. }
  68. // else if (message instanceof ViewUpdate) {
  69. // handleViewUpdate((ViewUpdate) message);
  70. // }
  71. else if (message instanceof PropertiesChanged) {
  72. handlePropertiesChanged((PropertiesChanged) message);
  73. }
  74. else if (message instanceof BufferUpdate) {
  75. handleBufferUpdate((BufferUpdate) message);
  76. }
  77. }
  78. //@EBHandler
  79. public void handleVFSPathSelected(VFSPathSelected vfsPathSelected) {
  80. View view = vfsPathSelected.getView();
  81. QuickOpenFrame frame = frameMap.get(view);
  82. if (frame != null) {
  83. File newDirectory = frame.getController().getDirectory();
  84. File oldDirectory = frame.getFileList().getRootDirectory();
  85. try {
  86. // Only update the file list if the path is actually different
  87. if ((oldDirectory == null) ||
  88. !newDirectory.getCanonicalPath().equals(oldDirectory.getCanonicalPath())) {
  89. getQuickOpenFrame(view).updateFileList();
  90. }
  91. }
  92. catch (IOException e) {
  93. getQuickOpenFrame(view).updateFileList();
  94. }
  95. }
  96. }
  97. //@EBHandler
  98. public void handleViewUpdate(ViewUpdate viewUpdate) {
  99. View view = viewUpdate.getView();
  100. if (viewUpdate.getWhat() == ViewUpdate.CLOSED) {
  101. frameMap.remove(view);
  102. }
  103. else if (viewUpdate.getWhat() == ViewUpdate.ACTIVATED) {
  104. // Refresh list of files when activated
  105. QuickOpenFrame frame = frameMap.get(view);
  106. if (frame != null) {
  107. frame.updateFileList();
  108. }
  109. }
  110. }
  111. //@EBHandler
  112. public void handlePropertiesChanged(PropertiesChanged msg) {
  113. frameMap.clear();
  114. }
  115. //@EBHandler
  116. public void handleBufferUpdate(BufferUpdate bufferUpdate) {
  117. if (bufferUpdate.getWhat() == BufferUpdate.SAVED) {
  118. View view = bufferUpdate.getView();
  119. QuickOpenFrame frame = frameMap.get(view);
  120. if (frame != null) {
  121. String path = bufferUpdate.getBuffer().getPath();
  122. try {
  123. File file = new File(path);
  124. boolean found = frame.getFileList().getFiles().contains(file);
  125. Log.log(Log.DEBUG, this, "path=" + path + ", file" + file + ", found in list = " + found);
  126. if (!found) {
  127. frame.updateFileList();
  128. }
  129. }
  130. catch (Exception e) {
  131. Log.log(Log.ERROR, "Failed to update FileList", e);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Main entry into plugin. Called from Action menu
  138. * @param view
  139. */
  140. public static void quickOpen(View view) {
  141. getQuickOpenFrame(view).setVisible(true);
  142. }
  143. /**
  144. * Called on jEdit startup
  145. */
  146. public void start() {}
  147. /**
  148. * Called on jEdit shutdown
  149. */
  150. public void stop() {}
  151. }