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

# · Java · 175 lines · 107 code · 21 blank · 47 comment · 31 complexity · dd4de06f269da42f44fc5dcbb71d47b8 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.util.Iterator;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Enumeration;
  26. import java.awt.Component;
  27. import org.gjt.sp.jedit.jEdit;
  28. import projectviewer.ProjectViewer;
  29. import projectviewer.gui.ImportDialog;
  30. import projectviewer.vpt.VPTFile;
  31. import projectviewer.vpt.VPTNode;
  32. import projectviewer.vpt.VPTDirectory;
  33. //}}}
  34. /**
  35. * Imports files and/or directories from the project root. Optionally, can
  36. * remove all existing files under the root before doing a fresh import.
  37. *
  38. * @author Marcelo Vanzin
  39. * @version $Id: RootImporter.java 6348 2005-08-23 04:56:19Z vanza $
  40. */
  41. public class RootImporter extends FileImporter {
  42. //{{{ Private members
  43. private boolean clean;
  44. protected Component parent;
  45. protected String oldRoot;
  46. //}}}
  47. //{{{ +RootImporter(VPTNode, String, ProjectViewer, Component) : <init>
  48. /**
  49. * Creates an Importer that uses a component other than the ProjectViewer
  50. * as the parent of the dialogs shown to the user. If "oldRoot" is not null,
  51. * files under this directory will be removed from the root node of the
  52. * project.
  53. */
  54. public RootImporter(VPTNode node, String oldRoot, ProjectViewer viewer, Component parent) {
  55. super(node, viewer);
  56. if (parent != null) {
  57. this.parent = parent;
  58. } else if (viewer != null) {
  59. this.parent = viewer;
  60. } else {
  61. this.parent = jEdit.getActiveView();
  62. }
  63. clean = (oldRoot != null);
  64. this.oldRoot = oldRoot;
  65. } //}}}
  66. //{{{ +RootImporter(VPTNode, ProjectViewer, boolean) : <init>
  67. /**
  68. * Imports files from the root of the project. If "clean" is "true", the
  69. * existing nodes that are below the root of the project will be removed
  70. * before the importing.
  71. */
  72. public RootImporter(VPTNode node, ProjectViewer viewer, boolean clean) {
  73. this(node, null, viewer, null);
  74. this.clean = clean;
  75. } //}}}
  76. //{{{ #internalDoImport() : Collection
  77. /** Asks if the user wants to import files from the chosen project root. */
  78. protected Collection internalDoImport() {
  79. fileCount = 0;
  80. String dlgTitle = (project.getChildCount() == 0)
  81. ? "projectviewer.import.msg_proj_root.title"
  82. : "projectviewer.import.msg_reimport.title";
  83. ImportDialog id = getImportDialog();
  84. id.setTitle(jEdit.getProperty(dlgTitle));
  85. loadImportFilterStatus(project, id);
  86. id.show();
  87. if (!id.isApproved()) {
  88. return null;
  89. }
  90. String state = null;
  91. if (viewer != null) {
  92. state = viewer.getFolderTreeState(project);
  93. }
  94. if (clean) {
  95. if (oldRoot == null) {
  96. oldRoot = project.getRootPath();
  97. }
  98. Enumeration e = project.children();
  99. ArrayList toRemove = new ArrayList();
  100. removed = new ArrayList();
  101. while (e.hasMoreElements()) {
  102. VPTNode n = (VPTNode) e.nextElement();
  103. if (n.getNodePath().startsWith(oldRoot)) {
  104. toRemove.add(n);
  105. }
  106. }
  107. if (toRemove.size() > 0) {
  108. for (Iterator i = toRemove.iterator(); i.hasNext(); ) {
  109. VPTNode n = (VPTNode) i.next();
  110. if (n.isDirectory()) {
  111. unregisterFiles((VPTDirectory)n);
  112. } else if (n.isFile()) {
  113. unregisterFile((VPTFile)n);
  114. }
  115. if (n.getChildCount() == 0)
  116. project.remove(n);
  117. }
  118. }
  119. }
  120. addTree(new File(project.getRootPath()),
  121. project,
  122. id.getImportFilter(),
  123. id.getFlattenFilePaths());
  124. if (state != null) {
  125. postAction = new NodeStructureChange(project, state);
  126. }
  127. showFileCount();
  128. saveImportFilterStatus(project, id);
  129. return null;
  130. } //}}}
  131. //{{{ #unregisterFiles(VPTDirectory) : void
  132. /** Unregisters all files in the directory from the project, recursively. */
  133. protected void unregisterFiles(VPTDirectory dir) {
  134. for (int i = 0; i < dir.getChildCount(); i++) {
  135. VPTNode n = (VPTNode) dir.getChildAt(i);
  136. if (n.isDirectory()) {
  137. unregisterFiles((VPTDirectory)n);
  138. if (n.getChildCount() == 0)
  139. dir.remove(i--);
  140. } else if (n.isFile()) {
  141. unregisterFile((VPTFile)n);
  142. dir.remove(i--);
  143. }
  144. }
  145. } //}}}
  146. //{{{ #getImportDialog() : ImportDialog
  147. protected ImportDialog getImportDialog() {
  148. ImportDialog dlg = super.getImportDialog();
  149. dlg.hideFileChooser();
  150. dlg.lockTraverse();
  151. dlg.hideNewNode();
  152. return dlg;
  153. } //}}}
  154. }