Revision: 13500
http://jedit.svn.sourceforge.net/jedit/?rev=13500&view=rev
Author: vanza
Date: 2008-09-01 20:51:02 +0000 (Mon, 01 Sep 2008)
Log Message:
-----------
Move some more management stuff into the Importer base class, to make it easier to implement the "auto reimporter" code. Also, fix a few bugs along the way.
Modified Paths:
--------------
plugins/ProjectViewer/trunk/projectviewer/importer/Importer.java
plugins/ProjectViewer/trunk/projectviewer/importer/ReImporter.java
plugins/ProjectViewer/trunk/projectviewer/importer/RootImporter.java
Modified: plugins/ProjectViewer/trunk/projectviewer/importer/Importer.java
===================================================================
--- plugins/ProjectViewer/trunk/projectviewer/importer/Importer.java 2008-09-01 20:34:48 UTC (rev 13499)
+++ plugins/ProjectViewer/trunk/projectviewer/importer/Importer.java 2008-09-01 20:51:02 UTC (rev 13500)
@@ -89,11 +89,11 @@
private Map<VPTNode,VPTNode> addedNodes;
/** The list of added files, if any, for event firing purposes. */
- protected List<VPTFile> added;
+ private List<VPTFile> added;
/** The list of removed files, if any, for event firing purposes. */
- protected List<VPTFile> removed;
- /** Whether this class should automatically fire the project event. */
- protected boolean fireEvent = true;
+ private List<VPTFile> removed;
+ /** The list of directories to be removed. */
+ private List<VPTDirectory> removedDirs;
/**
* An action to be executed after the import occurs. It will be executed
@@ -330,6 +330,10 @@
if (n == null) {
if (isFile && dirs.size() == 0) {
n = new VPTFile(curr);
+ if (contains(removed, (VPTFile) n)) {
+ root = n;
+ break;
+ }
} else {
n = new VPTDirectory(curr);
}
@@ -339,44 +343,42 @@
} else {
root.insert(n, root.findIndexForChild(n));
}
+ } else if (n.isFile()) {
+ contains(removed, (VPTFile) n);
}
root = n;
}
return root;
}
- //{{{ #registerFile(VPTFile) : void
+
/**
- * Registers the file in the project. Also, checks if the file's absolute
- * path is equal to the canonical path, and registers the canonical path
- * in the project in case they differ.
+ * Marks a directory for removal. Directories will only be removed
+ * if, at the end of the import process, they are empty - meaning
+ * that the only children they have are other directories.
*/
- protected void registerFile(VPTFile file) {
- project.registerNodePath(file);
- if (!contains(removed, file)) {
- if (added == null) {
- added = new ArrayList<VPTFile>();
- }
- added.add(file);
+ protected void removeDirectory(VPTDirectory dir)
+ {
+ if (removedDirs == null) {
+ removedDirs = new ArrayList<VPTDirectory>();
}
- } //}}}
+ removedDirs.add(dir);
+ }
- //{{{ #unregisterFile(VPTFile) : void
+
/**
- * Unregisters a file from a project and adds the file to the list of
- * files that have been removed. It automatically checks that "added"
- * list to see if the file is already in there, so that the event does
- * not contain the same file being added and removed at the same time.
+ * Marks a file for removal. At the end of the importing process,
+ * all removed files are unregistered from the project.
*/
- protected void unregisterFile(VPTFile file) {
- project.unregisterNodePath(file);
+ protected void removeFile(VPTFile file)
+ {
if (!contains(added, file)) {
if (removed == null) {
removed = new ArrayList<VPTFile>();
}
removed.add(file);
}
- } //}}}
+ }
/**
@@ -388,13 +390,13 @@
{
if (list != null)
for (int i = 0; i < list.size(); i++) {
- if (list.get(i).compareTo(file) == 0) {
+ if (list.get(i).getURL().equals(file.getURL())) {
list.remove(i);
return true;
}
}
return false;
- } //}}}
+ }
//{{{ #setViewerEnabled(boolean) : void
protected void setViewerEnabled(final boolean flag) {
@@ -512,52 +514,108 @@
/**
+ * Checks whether the given directory is "empty". It's considered
+ * empty if in the tree under the directory there are no openable
+ * nodes.
+ *
+ * @param dir The directory node.
+ */
+ private boolean isDirectoryEmpty(VPTDirectory dir)
+ {
+ for (int i = dir.getChildCount() - 1; i >= 0 ; i--) {
+ VPTNode child = (VPTNode) dir.getChildAt(i);
+ if (child.canOpen()) {
+ return false;
+ } else if (child.isDirectory() &&
+ !isDirectoryEmpty((VPTDirectory)child)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+ /**
* Processes the internal list of added nodes, registering any new
* files and properly inserting the nodes in the trees. This method
* should be called in the AWT thread, since it updates the UI.
*/
private void processAddedNodes()
{
- if (addedNodes == null) {
- return;
- }
- for (VPTNode n : addedNodes.keySet()) {
- VPTNode parent = addedNodes.get(n);
- ProjectViewer.insertNodeInto(n, parent);
- if (n.isFile()) {
- registerFile((VPTFile)n);
- } else if (n.isDirectory()) {
- processDirectory(n);
+ /* Process all the added nodes. */
+ if (addedNodes != null) {
+ for (VPTNode n : addedNodes.keySet()) {
+ VPTNode parent = addedNodes.get(n);
+ ProjectViewer.insertNodeInto(n, parent);
+ if (n.isFile()) {
+ registerFile((VPTFile)n);
+ } else if (n.isDirectory()) {
+ processDirectory(n);
+ }
}
}
- /* Show the count of imported files. */
- String msg = jEdit.getProperty("projectviewer.import.msg_result",
- new Object[] { new Integer(added.size()) });
- if (viewer != null) {
- viewer.setStatus(msg);
- } else {
- jEdit.getActiveView().getStatus().setMessageAndClear(msg);
+ /* Process all the files marked for removal. */
+ if (removed != null) {
+ for (VPTFile file : removed) {
+ project.unregisterNodePath(file);
+ ProjectViewer.removeNodeFromParent(file);
+ }
}
- /* Fire a project event notifying interested parties of any changes. */
- if ((added == null || added.size() == 0)
- && (removed == null || removed.size() == 0)) {
- return;
+ /* Process all the directories marker for removal. */
+ if (removedDirs != null) {
+ for (VPTDirectory dir : removedDirs) {
+ if (isDirectoryEmpty(dir)) {
+ ProjectViewer.removeNodeFromParent(dir);
+ }
+ }
}
if (added != null && added.size() == 0) {
added = null;
}
+ /* Show the count of imported files. */
+ if (added != null) {
+ String msg = jEdit.getProperty("projectviewer.import.msg_result",
+ new Object[] { new Integer(added.size()) });
+ if (viewer != null) {
+ viewer.setStatus(msg);
+ } else {
+ jEdit.getActiveView().getStatus().setMessageAndClear(msg);
+ }
+ }
+
+ /* Fire a project event notifying interested parties of any changes. */
+
if (removed != null && removed.size() == 0) {
removed = null;
}
- project.fireFilesChanged(added, removed);
+ if (added != null || removed != null) {
+ project.fireFilesChanged(added, removed);
+ }
}
+ /**
+ * Registers the file in the project. Also, checks if the file's absolute
+ * path is equal to the canonical path, and registers the canonical path
+ * in the project in case they differ.
+ */
+ private void registerFile(VPTFile file)
+ {
+ if (!contains(removed, file)) {
+ project.registerNodePath(file);
+ if (added == null) {
+ added = new ArrayList<VPTFile>();
+ }
+ added.add(file);
+ }
+ }
+
+
//{{{ #class ShowNodes
/**
* Makes sure all newly imported nodes are visible. Added
Modified: plugins/ProjectViewer/trunk/projectviewer/importer/ReImporter.java
===================================================================
--- plugins/ProjectViewer/trunk/projectviewer/importer/ReImporter.java 2008-09-01 20:34:48 UTC (rev 13499)
+++ plugins/ProjectViewer/trunk/projectviewer/importer/ReImporter.java 2008-09-01 20:51:02 UTC (rev 13500)
@@ -21,8 +21,6 @@
//{{{ Imports
import java.io.IOException;
-import java.util.ArrayList;
-
import javax.swing.SwingUtilities;
import org.gjt.sp.jedit.jEdit;
@@ -61,7 +59,6 @@
*/
public ReImporter(VPTNode node, ProjectViewer viewer) {
super(node, viewer, true);
- fireEvent = false;
} //}}}
@@ -85,8 +82,7 @@
if (node.isFile()) {
VFSFile file = ((VPTFile)node).getFile();
if (file == null || !file.isReadable()) {
- unregisterFile((VPTFile)node);
- project.remove(i--);
+ removeFile((VPTFile)node);
}
} else if (node.isDirectory()) {
reimportDirectory((VPTDirectory)node, false);
@@ -116,19 +112,18 @@
unregisterDir(dir, flatten);
addTree(dir, fnf, flatten);
} else {
- ArrayList toRemove = null;
for (int i = 0; i < dir.getChildCount(); i++) {
VPTNode node = (VPTNode) dir.getChildAt(i);
if (node.isFile()) {
VFSFile file = ((VPTFile)node).getFile();
if (file == null || !file.isReadable()) {
- unregisterFile((VPTFile)node);
- dir.remove(i--);
+ removeFile((VPTFile)node);
}
} else if (node.isDirectory()) {
reimportDirectory((VPTDirectory)node, flatten);
}
}
+ removeDirectory(dir);
}
} catch (IOException ioe) {
Log.log(Log.ERROR, this, "VFS error while importing", ioe);
@@ -150,14 +145,12 @@
if (VFSHelper.pathExists(cdir.getURL()) &&
parent.equals(dir.getNodePath())) {
unregisterFiles((VPTDirectory)n);
- if (cdir.getChildCount() == 0)
- dir.remove(i--);
+ removeDirectory((VPTDirectory)n);
} else {
reimportDirectory(cdir, flatten);
}
} else if (n.isFile()) {
- unregisterFile((VPTFile)n);
- dir.remove(i--);
+ removeFile((VPTFile)n);
}
}
} //}}}
Modified: plugins/ProjectViewer/trunk/projectviewer/importer/RootImporter.java
===================================================================
--- plugins/ProjectViewer/trunk/projectviewer/importer/RootImporter.java 2008-09-01 20:34:48 UTC (rev 13499)
+++ plugins/ProjectViewer/trunk/projectviewer/importer/RootImporter.java 2008-09-01 20:51:02 UTC (rev 13500)
@@ -89,18 +89,24 @@
/** Asks if the user wants to import files from the chosen project root. */
protected void internalDoImport()
{
- String dlgTitle = (project.getChildCount() == 0)
- ? "projectviewer.import.msg_proj_root.title"
- : "projectviewer.import.msg_reimport.title";
- ImportDialog id = getImportDialog();
- id.setTitle(jEdit.getProperty(dlgTitle));
- loadImportFilterStatus(project, id);
- id.setVisible(true);
+ boolean flatten = false;
+ if (fnf == null) {
+ String dlgTitle = (project.getChildCount() == 0)
+ ? "projectviewer.import.msg_proj_root.title"
+ : "projectviewer.import.msg_reimport.title";
+ ImportDialog id = getImportDialog();
+ id.setTitle(jEdit.getProperty(dlgTitle));
+ loadImportFilterStatus(project, id);
+ id.setVisible(true);
- if (!id.isApproved()) {
- return;
+ if (!id.isApproved()) {
+ return;
+ }
+
+ fnf = id.getImportFilter();
+ flatten = id.getFlattenFilePaths();
+ saveImportFilterStatus(project, id);
}
- fnf = id.getImportFilter();
String state = null;
if (viewer != null) {
@@ -113,7 +119,6 @@
}
Enumeration e = project.children();
List<VPTNode> toRemove = new ArrayList<VPTNode>();
- removed = new ArrayList<VPTFile>();
while (e.hasMoreElements()) {
VPTNode n = (VPTNode) e.nextElement();
// need to handle "virtual directories", which mess up the
@@ -129,23 +134,21 @@
for (VPTNode n : toRemove) {
if (n.isDirectory()) {
unregisterFiles((VPTDirectory)n);
+ removeDirectory((VPTDirectory)n);
} else if (n.isFile()) {
- unregisterFile((VPTFile)n);
+ removeFile((VPTFile)n);
}
- if (n.getChildCount() == 0)
- n.removeFromParent();
}
}
}
try {
- addTree(project, id.getImportFilter(), id.getFlattenFilePaths());
+ addTree(project, fnf, flatten);
} catch (IOException ioe) {
Log.log(Log.ERROR, this, "VFS exception while importing", ioe);
}
postAction = new NodeStructureChange(project, state);
- saveImportFilterStatus(project, id);
}
//{{{ #unregisterFiles(VPTDirectory) : void
@@ -155,11 +158,9 @@
VPTNode n = (VPTNode) dir.getChildAt(i);
if (n.isDirectory()) {
unregisterFiles((VPTDirectory)n);
- if (n.getChildCount() == 0)
- dir.remove(i--);
+ removeDirectory((VPTDirectory)n);
} else if (n.isFile()) {
- unregisterFile((VPTFile)n);
- dir.remove(i--);
+ removeFile((VPTFile)n);
}
}
} //}}}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|