/plugins/XML/tags/release-2-8-0/sidekick/css/Utils.java

# · Java · 98 lines · 57 code · 10 blank · 31 comment · 9 complexity · 52e00877e981b05ee7e2de5d8add76d1 MD5 · raw file

  1. /**
  2. * Utils.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2006 Jakub Roztocil
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package sidekick.css;
  22. //{{{ Imports
  23. import org.gjt.sp.jedit.*;
  24. import org.gjt.sp.jedit.MiscUtilities;
  25. import org.gjt.sp.jedit.browser.VFSBrowser;
  26. import java.io.*;
  27. //}}}
  28. public class Utils {
  29. //{{{ insertRelativePathToBuffer() method
  30. public static void insertRelativePathToBuffer() {
  31. Buffer buffer = jEdit.getActiveView().getBuffer();
  32. buffer.insert(jEdit.getActiveView().getTextArea().getCaretPosition(),
  33. chooseFileAndGetRelativePath());
  34. } //}}}
  35. //{{{ chooseFileAndGetRelativePath() method
  36. public static String chooseFileAndGetRelativePath() {
  37. String relPath = "";
  38. Buffer buffer = (Buffer)jEdit.getActiveView().getBuffer();
  39. String from = MiscUtilities.getParentOfPath(buffer.getPath());
  40. String[] paths = GUIUtilities.showVFSFileDialog(jEdit.getActiveView(),
  41. from,
  42. VFSBrowser.OPEN_DIALOG,
  43. false);
  44. if (paths != null) {
  45. String to = paths[0];
  46. if (buffer.isUntitled()) {
  47. return paths[0];
  48. }
  49. from = from.substring(0, from.length() - 1);
  50. relPath = getRelativePath(from, to);
  51. }
  52. return relPath;
  53. } //}}}
  54. //{{{ getRelativePath() method
  55. public static String getRelativePath(String fromDir, String toFile) {
  56. /* Based on part of macro Browse_and_link.bsh,
  57. thanks to authors Pavel Stetina and Jean-Francois Magni. */
  58. String separator;
  59. // File separator for use in regexp
  60. if (File.separator.equals("\\")) {
  61. separator = "\\\\";
  62. } else {
  63. separator = File.separator;
  64. }
  65. String[] dirsBuffer = fromDir.split(separator);
  66. String[] dirsFile = MiscUtilities.getParentOfPath(toFile).split(separator);
  67. int dirLevels = Math.min(dirsBuffer.length, dirsFile.length);
  68. int sameDirs = 0;
  69. int i;
  70. for (i=0; i < dirLevels; i++) {
  71. if (dirsBuffer[i].equals(dirsFile[i])) {
  72. sameDirs++;
  73. }
  74. }
  75. // From now href contains new relative URL
  76. String href = "";
  77. // Backward in directories tree
  78. for (i=sameDirs; i < dirsBuffer.length; i++) {
  79. href += "../";
  80. }
  81. // Forward in directories tree
  82. for (i=sameDirs; i < dirsFile.length; i++) {
  83. href += dirsFile[i] + "/";
  84. }
  85. href += MiscUtilities.getFileName(toFile);
  86. return href;
  87. } //}}}
  88. }