Permalink
Browse files

working on "update pages from net" functionality

  • Loading branch information...
1 parent fa304a2 commit 15002112c92131ed694893b38ad4ee52aca402fa Acerbic committed Sep 27, 2012
View
@@ -5,6 +5,7 @@
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
+import java.util.Collection;
import java.util.Deque;
import java.util.Enumeration;
import java.util.LinkedList;
@@ -15,6 +16,7 @@
import dloader.gui.MyWorker;
import dloader.gui.TreeNodePageWrapper;
import dloader.page.AbstractPage;
+import dloader.page.Track;
import javax.swing.tree.*;
@@ -137,6 +139,13 @@ public void mouseClicked(MouseEvent arg0) {
}
});
+ btnFetch.addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent arg0) {
+ initScan();
+ }
+ });
+
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.TRAILING)
@@ -227,21 +236,36 @@ public void init() {
lblStatus.setText("Preparing");
chckbxLog.setSelected( Main.logger != null);
chckbxUseCache.setSelected(Main.allowFromCache);
+
+ updateTree (rootPage, "", 1); //empty message to put root page on display;
}
- public void initPrefetch() {
+ private void initPrefetch() {
lblStatus.setText("Prefetching");
MyWorker newWorker = new MyWorker(rootPage, JobType.READCACHEPAGES);
newWorker.execute();
btnPrefetch.setEnabled(false);
}
- public void finishPrefetch() {
+ private void finishPrefetch() {
if (lblStatus.getText().equals("Prefetching"))
lblStatus.setText("");
btnPrefetch.setEnabled(true);
}
+ private void initScan() {
+ lblStatus.setText("Scanning");
+ MyWorker newWorker = new MyWorker(rootPage, JobType.UPDATEPAGES);
+ newWorker.execute();
+ btnFetch.setEnabled(false);
+ }
+
+ private void finishScan() {
+ if (lblStatus.getText().equals("Scanning"))
+ lblStatus.setText("");
+ btnFetch.setEnabled(true);
+ }
+
/**
* Receiving message from SwingWorker
* @param p - page node to update
@@ -285,29 +309,56 @@ public void updateTree (AbstractPage p, String message, long value) {
indices[0] = parent.getIndex(child);
model.nodesWereInserted(parent, indices); //notification to repaint
- //expand only top node
- Object userObject = parent.getUserObject();
- if (userObject instanceof TreeNodePageWrapper &&
- ((TreeNodePageWrapper)userObject).page.equals(pathToPage.getFirst()))
+ //expand only if not a Track
+ if (!(childsUserObject.page instanceof Track))
tree.expandPath(new TreePath(parent.getPath()));
}
- // pass message to the user object and refresh its visual if needed
- childsUserObject.update(message, value);
- model.nodeChanged(child);
-
parent = child; // advance to search next element in our pathToPage
}
+ // after search is complete, parent points to a DefaultMutableTreeNode containing TreeNodePageWrapper containing original p (but now p is different)
+
+ // Reading cache forces reset of page data, the node branch must be trimmed accordingly
+ if (message.equals("read from cache") || message.equals("read cache failed")
+ || message.equals("cache reading failed, submitting download job")
+ || message.equals("download finished") || message.equals("up to date")
+ || message.equals("download failed")
+ )
+ trimBranch(parent, model);
+ // pass message to the user object and refresh its visual if needed
+ if (((TreeNodePageWrapper)parent.getUserObject()).update(message, value))
+ model.nodeChanged(parent);
+
}
+ /**
+ * Remove branch->children nodes don't correspond to branch->page->children pages
+ * @param branch - node branch to clean up
+ * @param model - reference model
+ */
+ private void trimBranch(DefaultMutableTreeNode branch, DefaultTreeModel model) {
+ AbstractPage branchPage = ((TreeNodePageWrapper)branch.getUserObject()).page;
+ Collection<DefaultMutableTreeNode> removeList = new LinkedList<>();
+ for (@SuppressWarnings("unchecked")
+ Enumeration<DefaultMutableTreeNode> children = branch.children(); children.hasMoreElements();) {
+ DefaultMutableTreeNode childNode = children.nextElement();
+ AbstractPage childPage = ((TreeNodePageWrapper)childNode.getUserObject()).page;
+ if (! branchPage.childPages.contains(childPage)) {
+ // child's page is no more contained within its parent's page children collection
+ removeList.add(childNode); // can't remove on spot, it will fuck up the iteration
+ }
+ }
+ for (DefaultMutableTreeNode element: removeList)
+ model.removeNodeFromParent(element);
+ }
+
// captures SwingWorker finish jobs event
public void myWorkerDone (AbstractPage root, JobMaster.JobType jobType) {
switch (jobType) {
case READCACHEPAGES: finishPrefetch(); break;
- case UPDATEDATA:
- break;
- case UPDATEPAGES:
+ case SAVEDATA:
break;
+ case UPDATEPAGES: finishScan(); break;
default:
break;
}
@@ -34,12 +34,12 @@
* READCACHEPAGES - get pages tree ONLY from cache (prefetch)
* UPDATEPAGES - updated 1st page from the Internet and others too if needed (otherwise tries reading from cache)
* updates cache too
- * UPDATEDATA - download missing/corrupt files.
+ * SAVEDATA - download missing/corrupt files.
*
* It is vital that different kinds of jobs are not ran on the same page concurrently.
* @author Acerbic
*/
- public enum JobType { READCACHEPAGES, UPDATEPAGES, UPDATEDATA};
+ public enum JobType { READCACHEPAGES, UPDATEPAGES, SAVEDATA};
/**
* i'm not sure about this. It is a temporary variable to the rootPage.
@@ -81,8 +81,8 @@ void goGoGo() {
switch (whatToDo) {
case READCACHEPAGES: submit(new ReadCacheJob(rootPage, this)); break;
- case UPDATEPAGES: submit(new DownloadPageJob(rootPage, this, !Main.allowFromCache)); break;
- case UPDATEDATA: submit(new SaveDataJob(rootPage, this)); break;
+ case UPDATEPAGES: submit(new UpdatePageJob(rootPage, this, !Main.allowFromCache)); break;
+ case SAVEDATA: submit(new SaveDataJob(rootPage, this)); break;
}
}
View
@@ -11,14 +11,14 @@
// public static final String nl = System.getProperty ( "line.separator" );
/* Default values */
- public static String baseURL = "http://homestuck.bandcamp.com";
- public static String xmlFileName = "pages_scan_cache.xml";
+ public static String baseURL = "http://homestuck.bandcamp.com"; //-d
+ public static String xmlFileName = "pages_scan_cache.xml";//-t
public static String logFile = "download.log";
- public static boolean forceTagging = false; // if true - always tag, otherwise only new/missing tag fields
- public static boolean allowFromCache = true;
- public static boolean logToCon = true;
- public static boolean logToFile = false;
- public static boolean isInConsoleMode = false;
+ public static boolean forceTagging = false; // if true - always tag, otherwise only new/missing tag fields //-f
+ public static boolean allowFromCache = true; //-r
+ public static boolean logToCon = true; //-s
+ public static boolean logToFile = false; //-l
+ public static boolean isInConsoleMode = false; //-c
// user current directory
public static String saveTo = Paths.get("").toAbsolutePath().toString();
@@ -88,7 +88,7 @@ public void getThingsMoving() {
System.out.println("-------Saving files-------");
//3 save page data
- jm = new STCD_JobMaster(JobMaster.JobType.UPDATEDATA, p);
+ jm = new STCD_JobMaster(JobMaster.JobType.SAVEDATA, p);
jm.goGoGo();
}
}
@@ -18,29 +18,37 @@
// TODO job progress flags and logs
boolean readFromCache = false;
boolean downloadPageQ = false;
+
+ boolean downloading = false;
boolean downloaded = false;
+ boolean downloadPageFailed = false;
+ boolean upToDate = false;
public TreeNodePageWrapper(AbstractPage page) {
this.page = page;
}
- public void update(String type, long report) {
+ /**
+ * update flags and states of this node visual representation
+ * @param type
+ * @param report
+ * @return true if node must be repainted
+ */
+ public boolean update(String type, long report) {
switch (type) {
//messages reported by ReadCacheJob and GetPageJob:
case "checking cache": break;
- case "read from cache": readFromCache = true; break;
+ case "read from cache": readFromCache = true; return true;
//message reported by ReadCacheJob
case "read cache failed": break;
//message reported by GetPageJob:
- case "cache reading failed, submitting download job": downloadPageQ = true; break;
-
- /**
- * summary of the messages reported by DownloadPageJob:
- * "download job started", 1
- * "download finished", 1
- * "up to date", 1
- * "download failed", 1
- */
+ case "cache reading failed, submitting download job": downloadPageQ = true; return true;
+ // messages reported by DownloadPageJob:
+ case "download job started": downloading = true; downloadPageQ = false; return true;
+ case "download finished": downloading = false; downloaded = true; return true;
+ case "up to date": downloading = false; downloaded = true; upToDate = true; return true;
+ case "download failed": downloading = false; downloadPageFailed = true; return true;
+
/**
* summary of the messages reported by SaveDataJob:
@@ -56,20 +64,55 @@ public void update(String type, long report) {
*/
}
+ return false;
}
@Override
public String toString() {
+ // TODO fix representation
String header = "<html>";
String bottom = "</html>";
- // TODO fix representation
+ String styleCompilation = "";
+ String title = page.toString();
+
+ styleCompilation += "span#url {color:gray; font: 0.8em;}";
+
+ // title color
+ String titleColor = "black";
+ if (readFromCache) titleColor = "blue";
+ if (downloaded && !upToDate) titleColor = "green";
+ if (downloadPageQ || downloadPageFailed) titleColor = "red";
+ if (downloading) titleColor = "orange";
+ styleCompilation += "span#title {color:" + titleColor + "}";
+
+ // title formatters;
+ String childrenCount = (page.childPages.size() > 0)? "<span id='children'>["+page.childPages.size()+"]</span>":"";
+
+ if (downloading) {
+ title = "Scanning... " + title;
+// styleCompilation += "span#title {font: bold}";
+ }
+
+ if (downloadPageQ) {
+ title = "In queue for scan... " + title;
+// styleCompilation += "span#title {font: bold}";
+ }
+ // finalize title
+ title = "<span id='title'>"+title+"</span>";
+
+ // finalize style
+ header += "<style type='text/css'> " + styleCompilation + "</style>";
+
+ // output layouts
if (page instanceof Track)
- return header +page.toString()+
+ return header +
+ title +
bottom;
else
- return header+"<b>" +page.toString()+
- "<br></b><u>" +page.url+
+ return header +
+ title + " " + childrenCount +
+ "<br>" + "<span id='url'>" + page.url + "</span>" +
"</u>"+ bottom;
}
Oops, something went wrong.

0 comments on commit 1500211

Please sign in to comment.