From 76e18a87471ec487304eb13dc8a5d362585b4f1c Mon Sep 17 00:00:00 2001
From: Paul Martin <paul@paulsputer.com>
Date: Mon, 11 Apr 2016 14:59:46 -0400
Subject: [PATCH] Merge pull request #1046 from gitblit/1028-FilterAndPagingForFilestorePage

---
 src/main/java/com/gitblit/wicket/WicketUtils.java |  187 +++++++++++++++++++++++++++-------------------
 1 files changed, 109 insertions(+), 78 deletions(-)

diff --git a/src/main/java/com/gitblit/wicket/WicketUtils.java b/src/main/java/com/gitblit/wicket/WicketUtils.java
index 1cf465e..99c1e08 100644
--- a/src/main/java/com/gitblit/wicket/WicketUtils.java
+++ b/src/main/java/com/gitblit/wicket/WicketUtils.java
@@ -21,7 +21,6 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.TimeZone;
 
@@ -30,25 +29,26 @@
 import org.apache.wicket.Component;
 import org.apache.wicket.PageParameters;
 import org.apache.wicket.Request;
+import org.apache.wicket.behavior.AttributeAppender;
 import org.apache.wicket.behavior.HeaderContributor;
 import org.apache.wicket.behavior.SimpleAttributeModifier;
 import org.apache.wicket.markup.html.IHeaderContributor;
 import org.apache.wicket.markup.html.IHeaderResponse;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.image.ContextImage;
+import org.apache.wicket.model.Model;
 import org.apache.wicket.protocol.http.WebRequest;
 import org.apache.wicket.resource.ContextRelativeResource;
 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
-import org.wicketstuff.googlecharts.AbstractChartData;
-import org.wicketstuff.googlecharts.IChartData;
 
 import com.gitblit.Constants;
 import com.gitblit.Constants.AccessPermission;
 import com.gitblit.Constants.FederationPullStatus;
-import com.gitblit.GitBlit;
+import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
 import com.gitblit.models.FederationModel;
 import com.gitblit.models.Metric;
+import com.gitblit.utils.DiffUtils.DiffComparator;
 import com.gitblit.utils.HttpUtils;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.utils.TimeUtils;
@@ -57,6 +57,10 @@
 
 	public static void setCssClass(Component container, String value) {
 		container.add(new SimpleAttributeModifier("class", value));
+	}
+
+	public static void addCssClass(Component container, String value) {
+		container.add(new AttributeAppender("class", new Model<String>(value), " "));
 	}
 
 	public static void setCssStyle(Component container, String value) {
@@ -69,8 +73,8 @@
 		container.add(new SimpleAttributeModifier("style", background));
 	}
 
-	public static void setHtmlTooltip(Component container, String value) {
-		container.add(new SimpleAttributeModifier("title", value));
+	public static Component setHtmlTooltip(Component container, String value) {
+		return container.add(new SimpleAttributeModifier("title", value));
 	}
 
 	public static void setInputPlaceholder(Component container, String value) {
@@ -115,7 +119,7 @@
 		default:
 			setCssClass(container, "badge");
 			break;
-		}	
+		}
 	}
 
 	public static void setAlternatingBackground(Component c, int i) {
@@ -190,11 +194,10 @@
 			return newImage(wicketId, "file_settings_16x16.png");
 		}
 
-		List<String> mdExtensions = GitBlit.getStrings(Keys.web.markdownExtensions);
-		for (String ext : mdExtensions) {
-			if (filename.endsWith('.' + ext.toLowerCase())) {
-				return newImage(wicketId, "file_world_16x16.png");
-			}
+		String ext = StringUtils.getFileExtension(filename).toLowerCase();
+		IStoredSettings settings = GitBlitWebApp.get().settings();
+		if (MarkupProcessor.getMarkupExtensions(settings).contains(ext)) {
+			return newImage(wicketId, "file_world_16x16.png");
 		}
 		return newImage(wicketId, "file_16x16.png");
 	}
@@ -232,17 +235,17 @@
 
 	public static Label newIcon(String wicketId, String css) {
 		Label lbl = new Label(wicketId);
-		setCssClass(lbl, css);		
+		setCssClass(lbl, css);
 		return lbl;
 	}
-	
+
 	public static Label newBlankIcon(String wicketId) {
 		Label lbl = new Label(wicketId);
 		setCssClass(lbl, "");
 		lbl.setRenderBodyOnly(true);
 		return lbl;
 	}
-	
+
 	public static ContextRelativeResource getResource(String file) {
 		return new ContextRelativeResource(file);
 	}
@@ -257,6 +260,7 @@
 		return new HeaderContributor(new IHeaderContributor() {
 			private static final long serialVersionUID = 1L;
 
+			@Override
 			public void renderHead(IHeaderResponse response) {
 				String contentType = "application/rss+xml";
 
@@ -304,7 +308,9 @@
 
 	public static PageParameters newRepositoryParameter(String repositoryName) {
 		Map<String, String> parameterMap = new HashMap<String, String>();
-		parameterMap.put("r", repositoryName);
+		if (!StringUtils.isEmpty(repositoryName)) {
+			parameterMap.put("r", repositoryName);
+		}
 		return new PageParameters(parameterMap);
 	}
 
@@ -322,6 +328,31 @@
 		}
 		parameterMap.put("r", repositoryName);
 		parameterMap.put("h", objectId);
+		return new PageParameters(parameterMap);
+	}
+
+	public static PageParameters newDiffParameter(String repositoryName,
+			String objectId, DiffComparator diffComparator) {
+		Map<String, String> parameterMap = new HashMap<String, String>();
+		if (StringUtils.isEmpty(objectId)) {
+			return newRepositoryParameter(repositoryName);
+		}
+		parameterMap.put("r", repositoryName);
+		parameterMap.put("h", objectId);
+		parameterMap.put("w", "" + diffComparator.ordinal());
+		return new PageParameters(parameterMap);
+	}
+
+	public static PageParameters newDiffParameter(String repositoryName,
+			String objectId, DiffComparator diffComparator, String blobPath) {
+		Map<String, String> parameterMap = new HashMap<String, String>();
+		if (StringUtils.isEmpty(objectId)) {
+			return newRepositoryParameter(repositoryName);
+		}
+		parameterMap.put("r", repositoryName);
+		parameterMap.put("h", objectId);
+		parameterMap.put("w", "" + diffComparator.ordinal());
+		parameterMap.put("f", blobPath);
 		return new PageParameters(parameterMap);
 	}
 
@@ -386,6 +417,19 @@
 		return new PageParameters(parameterMap);
 	}
 
+	public static PageParameters newFilestorePageParameter(int pageNumber, String filter) {
+		Map<String, String> parameterMap = new HashMap<String, String>();
+		
+		if (pageNumber > 1) {
+			parameterMap.put("pg", String.valueOf(pageNumber));
+		}
+		if (filter != null) {
+			parameterMap.put("s", String.valueOf(filter));
+		}
+		
+		return new PageParameters(parameterMap);
+	}
+
 	public static PageParameters newBlobDiffParameter(String repositoryName,
 			String baseCommitId, String commitId, String path) {
 		Map<String, String> parameterMap = new HashMap<String, String>();
@@ -437,6 +481,30 @@
 		return new PageParameters(parameterMap);
 	}
 
+	public static PageParameters newBlameTypeParameter(String repositoryName,
+			String commitId, String path, String blameType) {
+		Map<String, String> parameterMap = new HashMap<String, String>();
+		parameterMap.put("r", repositoryName);
+		parameterMap.put("h", commitId);
+		parameterMap.put("f", path);
+		parameterMap.put("blametype", blameType);
+		return new PageParameters(parameterMap);
+	}
+
+	public static PageParameters newTicketsParameters(String repositoryName, String... states) {
+		PageParameters tParams = newRepositoryParameter(repositoryName);
+		if (states != null) {
+			for (String state : states) {
+				tParams.add("status", state);
+			}
+		}
+		return tParams;
+	}
+
+	public static PageParameters newOpenTicketsParameter(String repositoryName) {
+		return newTicketsParameters(repositoryName, TicketsUI.openStatii);
+	}
+
 	public static String getProjectName(PageParameters params) {
 		return params.getString("p", "");
 	}
@@ -463,6 +531,11 @@
 
 	public static String getSearchType(PageParameters params) {
 		return params.getString("st", null);
+	}
+
+	public static DiffComparator getDiffComparator(PageParameters params) {
+		int ordinal = params.getInt("w", 0);
+		return DiffComparator.values()[ordinal];
 	}
 
 	public static int getPage(PageParameters params) {
@@ -507,10 +580,14 @@
 	}
 
 	public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
-		String format = GitBlit.getString(Keys.web.datestampShortFormat, "MM/dd/yy");
+		return createDateLabel(wicketId, date, timeZone, timeUtils, true);
+	}
+
+	public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils, boolean setCss) {
+		String format = GitBlitWebApp.get().settings().getString(Keys.web.datestampShortFormat, "MM/dd/yy");
 		DateFormat df = new SimpleDateFormat(format);
 		if (timeZone == null) {
-			timeZone = GitBlit.getTimezone();
+			timeZone = GitBlitWebApp.get().getTimezone();
 		}
 		df.setTimeZone(timeZone);
 		String dateString;
@@ -524,13 +601,15 @@
 			// past
 			title = timeUtils.timeAgo(date);
 		}
-		if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
+		if (title != null && (System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
 			String tmp = dateString;
 			dateString = title;
 			title = tmp;
 		}
 		Label label = new Label(wicketId, dateString);
-		WicketUtils.setCssClass(label, timeUtils.timeAgoCss(date));
+		if (setCss) {
+			WicketUtils.setCssClass(label, timeUtils.timeAgoCss(date));
+		}
 		if (!StringUtils.isEmpty(title)) {
 			WicketUtils.setHtmlTooltip(label, title);
 		}
@@ -538,10 +617,10 @@
 	}
 
 	public static Label createTimeLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
-		String format = GitBlit.getString(Keys.web.timeFormat, "HH:mm");
+		String format = GitBlitWebApp.get().settings().getString(Keys.web.timeFormat, "HH:mm");
 		DateFormat df = new SimpleDateFormat(format);
 		if (timeZone == null) {
-			timeZone = GitBlit.getTimezone();
+			timeZone = GitBlitWebApp.get().getTimezone();
 		}
 		df.setTimeZone(timeZone);
 		String timeString;
@@ -559,10 +638,10 @@
 	}
 
 	public static Label createDatestampLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
-		String format = GitBlit.getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
+		String format = GitBlitWebApp.get().settings().getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
 		DateFormat df = new SimpleDateFormat(format);
 		if (timeZone == null) {
-			timeZone = GitBlit.getTimezone();
+			timeZone = GitBlitWebApp.get().getTimezone();
 		}
 		df.setTimeZone(timeZone);
 		String dateString;
@@ -575,10 +654,13 @@
 		if (TimeUtils.isToday(date, timeZone)) {
 			title = timeUtils.today();
 		} else if (TimeUtils.isYesterday(date, timeZone)) {
-				title = timeUtils.yesterday();
+			title = timeUtils.yesterday();
 		} else if (date.getTime() <= System.currentTimeMillis()) {
 			// past
 			title = timeUtils.timeAgo(date);
+		} else {
+			// future
+			title = timeUtils.inFuture(date);
 		}
 		if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
 			String tmp = dateString;
@@ -593,11 +675,11 @@
 	}
 
 	public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
-		String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
+		String format = GitBlitWebApp.get().settings().getString(Keys.web.datetimestampLongFormat,
 				"EEEE, MMMM d, yyyy HH:mm Z");
 		DateFormat df = new SimpleDateFormat(format);
 		if (timeZone == null) {
-			timeZone = GitBlit.getTimezone();
+			timeZone = GitBlitWebApp.get().getTimezone();
 		}
 		df.setTimeZone(timeZone);
 		String dateString;
@@ -618,31 +700,6 @@
 		return label;
 	}
 
-	public static IChartData getChartData(Collection<Metric> metrics) {
-		final double[] commits = new double[metrics.size()];
-		final double[] tags = new double[metrics.size()];
-		int i = 0;
-		double max = 0;
-		for (Metric m : metrics) {
-			commits[i] = m.count;
-			if (m.tag > 0) {
-				tags[i] = m.count;
-			} else {
-				tags[i] = -1d;
-			}
-			max = Math.max(max, m.count);
-			i++;
-		}
-		IChartData data = new AbstractChartData(max) {
-			private static final long serialVersionUID = 1L;
-
-			public double[][] getData() {
-				return new double[][] { commits, tags };
-			}
-		};
-		return data;
-	}
-
 	public static double maxValue(Collection<Metric> metrics) {
 		double max = Double.MIN_VALUE;
 		for (Metric m : metrics) {
@@ -652,30 +709,4 @@
 		}
 		return max;
 	}
-
-	public static IChartData getScatterData(Collection<Metric> metrics) {
-		final double[] y = new double[metrics.size()];
-		final double[] x = new double[metrics.size()];
-		int i = 0;
-		double max = 0;
-		for (Metric m : metrics) {
-			y[i] = m.count;
-			if (m.duration > 0) {
-				x[i] = m.duration;
-			} else {
-				x[i] = -1d;
-			}
-			max = Math.max(max, m.count);
-			i++;
-		}
-		IChartData data = new AbstractChartData(max) {
-			private static final long serialVersionUID = 1L;
-
-			public double[][] getData() {
-				return new double[][] { x, y };
-			}
-		};
-		return data;
-	}
-
 }

--
Gitblit v1.9.1