/plugins/ProjectViewer/tags/pv_2_0_1/projectviewer/importer/RootImporter.java

# · Java · 170 lines · 103 code · 21 blank · 46 comment · 25 complexity · ba6b31d29e6e97a6a2b15a1478a16267 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.importer;
  20. //{{{ Imports
  21. import java.io.File;
  22. import java.io.FilenameFilter;
  23. import java.util.Iterator;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Enumeration;
  27. import java.awt.Component;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.JFileChooser;
  30. import javax.swing.SwingUtilities;
  31. import javax.swing.filechooser.FileFilter;
  32. import org.gjt.sp.jedit.jEdit;
  33. import projectviewer.ProjectViewer;
  34. import projectviewer.vpt.VPTFile;
  35. import projectviewer.vpt.VPTNode;
  36. import projectviewer.vpt.VPTProject;
  37. import projectviewer.vpt.VPTDirectory;
  38. //}}}
  39. /**
  40. * Imports files and/or directories from the project root. Optionally, can
  41. * remove all existing files under the root before doing a fresh import.
  42. *
  43. * @author Marcelo Vanzin
  44. * @version $Id: RootImporter.java 6122 2003-03-04 22:26:22Z vanza $
  45. */
  46. public class RootImporter extends FileImporter {
  47. //{{{ Private members
  48. private Component parent;
  49. private boolean clean;
  50. //}}}
  51. //{{{ Constructor
  52. /**
  53. * Creates an Importer that uses a component other than the ProjectViewer
  54. * as the parent of the dialogs shown to the user.
  55. */
  56. public RootImporter(VPTNode node, ProjectViewer viewer, Component parent) {
  57. super(node, viewer);
  58. if (parent != null) {
  59. this.parent = parent;
  60. } else {
  61. this.parent = viewer;
  62. }
  63. clean = false;
  64. }
  65. public RootImporter(VPTNode node, ProjectViewer viewer) {
  66. this(node, viewer, null);
  67. }
  68. /**
  69. * Imports files from the root of the project. If "clean" is "true", the
  70. * existing nodes that are below the root of the project will be removed
  71. * before the importing.
  72. */
  73. public RootImporter(VPTNode node, ProjectViewer viewer, boolean clean) {
  74. this(node, viewer, null);
  75. this.clean = clean;
  76. }
  77. //}}}
  78. //{{{ internalDoImport() method
  79. /** Asks if the user wants to import files from the chosen project root. */
  80. protected Collection internalDoImport() {
  81. fileCount = 0;
  82. Object[] options = {
  83. jEdit.getProperty("projectviewer.import.yes-settings"),
  84. jEdit.getProperty("projectviewer.import.yes-all"),
  85. jEdit.getProperty("projectviewer.import.yes-cvs"),
  86. jEdit.getProperty("projectviewer.import.no")
  87. };
  88. Object sel = JOptionPane.showInputDialog(parent,
  89. jEdit.getProperty("projectviewer.import.msg_proj_root"),
  90. jEdit.getProperty("projectviewer.import.msg_proj_root.title"),
  91. JOptionPane.QUESTION_MESSAGE,
  92. null, options, options[0]);
  93. FilenameFilter fnf = null;
  94. if (sel == null || sel == options[3]) {
  95. return null;
  96. } else if (sel == options[0]) {
  97. fnf = new ImportSettingsFilter();
  98. } else if (sel == options[2]) {
  99. fnf = new CVSEntriesFilter();
  100. }
  101. if (clean) {
  102. Enumeration e = project.children();
  103. ArrayList toRemove = new ArrayList();
  104. while (e.hasMoreElements()) {
  105. VPTNode n = (VPTNode) e.nextElement();
  106. if (n.getNodePath().startsWith(project.getRootPath())) {
  107. toRemove.add(n);
  108. }
  109. }
  110. if (toRemove.size() > 0) {
  111. for (Iterator i = toRemove.iterator(); i.hasNext(); ) {
  112. VPTNode n = (VPTNode) i.next();
  113. if (n.isDirectory()) {
  114. unregisterFiles((VPTDirectory)n, project);
  115. } else if (n.isFile()) {
  116. project.unregisterFile((VPTFile)n);
  117. }
  118. project.remove(n);
  119. }
  120. }
  121. }
  122. addTree(new File(project.getRootPath()), project, fnf);
  123. try {
  124. SwingUtilities.invokeAndWait(new Runnable() {
  125. public void run() {
  126. ProjectViewer.nodeStructureChanged(project);
  127. }
  128. });
  129. } catch (InterruptedException ie) {
  130. // not gonna happen
  131. } catch (java.lang.reflect.InvocationTargetException ite) {
  132. // not gonna happen
  133. }
  134. showFileCount();
  135. return null;
  136. } //}}}
  137. //{{{ unregisterFiles(VPTDirectory, VPTProject) method
  138. /** Unregisters all files in the directory from the project, recursively. */
  139. private void unregisterFiles(VPTDirectory dir, VPTProject p) {
  140. for (Enumeration e = dir.children(); e.hasMoreElements(); ) {
  141. VPTNode n = (VPTNode) e.nextElement();
  142. if (n.isDirectory()) {
  143. unregisterFiles((VPTDirectory)n, p);
  144. } else if (n.isFile()) {
  145. p.unregisterFile((VPTFile)n);
  146. }
  147. }
  148. } //}}}
  149. }