James Moger
2013-03-28 a9e6d5d9207c497c0f2b1e515dc4af05b2316397
commit | author | age
f13c4c 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
5fe7df 16 package com.gitblit.wicket;
JM 17
698678 18 import java.text.DateFormat;
db91a3 19 import java.text.MessageFormat;
698678 20 import java.text.SimpleDateFormat;
f1720c 21 import java.util.Collection;
698678 22 import java.util.Date;
c1c3c6 23 import java.util.List;
698678 24 import java.util.TimeZone;
1e47ab 25
8c9a20 26 import javax.servlet.http.HttpServletRequest;
JM 27
5fe7df 28 import org.apache.wicket.Component;
698678 29 import org.apache.wicket.PageParameters;
8c9a20 30 import org.apache.wicket.Request;
JM 31 import org.apache.wicket.behavior.HeaderContributor;
fc8426 32 import org.apache.wicket.behavior.SimpleAttributeModifier;
8c9a20 33 import org.apache.wicket.markup.html.IHeaderContributor;
JM 34 import org.apache.wicket.markup.html.IHeaderResponse;
698678 35 import org.apache.wicket.markup.html.basic.Label;
1e8390 36 import org.apache.wicket.markup.html.image.ContextImage;
8c9a20 37 import org.apache.wicket.protocol.http.WebRequest;
1e8390 38 import org.apache.wicket.resource.ContextRelativeResource;
9bc17d 39 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
f1720c 40 import org.wicketstuff.googlecharts.AbstractChartData;
JM 41 import org.wicketstuff.googlecharts.IChartData;
698678 42
33d8d8 43 import com.gitblit.Constants;
831469 44 import com.gitblit.Constants.FederationPullStatus;
87cc1e 45 import com.gitblit.GitBlit;
155bf7 46 import com.gitblit.Keys;
831469 47 import com.gitblit.models.FederationModel;
f1720c 48 import com.gitblit.models.Metric;
831469 49 import com.gitblit.utils.HttpUtils;
f1dfc2 50 import com.gitblit.utils.StringUtils;
87cc1e 51 import com.gitblit.utils.TimeUtils;
5fe7df 52
JM 53 public class WicketUtils {
54
55     public static void setCssClass(Component container, String value) {
fc8426 56         container.add(new SimpleAttributeModifier("class", value));
5fe7df 57     }
JM 58
59     public static void setCssStyle(Component container, String value) {
fc8426 60         container.add(new SimpleAttributeModifier("style", value));
db91a3 61     }
JM 62
63     public static void setCssBackground(Component container, String value) {
64         String background = MessageFormat.format("background-color:{0};",
65                 StringUtils.getColor(value));
66         container.add(new SimpleAttributeModifier("style", background));
5fe7df 67     }
JM 68
1e8390 69     public static void setHtmlTooltip(Component container, String value) {
fc8426 70         container.add(new SimpleAttributeModifier("title", value));
9bc17d 71     }
JM 72
4910c1 73     public static void setInputPlaceholder(Component container, String value) {
JM 74         container.add(new SimpleAttributeModifier("placeholder", value));
75     }
76
9bc17d 77     public static void setChangeTypeCssClass(Component container, ChangeType type) {
JM 78         switch (type) {
79         case ADD:
80             setCssClass(container, "addition");
81             break;
82         case COPY:
83         case RENAME:
84             setCssClass(container, "rename");
85             break;
86         case DELETE:
87             setCssClass(container, "deletion");
88             break;
89         case MODIFY:
90             setCssClass(container, "modification");
91             break;
92         }
5fe7df 93     }
155bf7 94
1e47ab 95     public static void setTicketCssClass(Component container, String state) {
JM 96         String css = null;
97         if (state.equals("open")) {
3cc6e2 98             css = "label label-important";
1e47ab 99         } else if (state.equals("hold")) {
3cc6e2 100             css = "label label-warning";
1e47ab 101         } else if (state.equals("resolved")) {
3cc6e2 102             css = "label label-success";
1e47ab 103         } else if (state.equals("invalid")) {
d376ab 104             css = "label";
1e47ab 105         }
JM 106         if (css != null) {
107             setCssClass(container, css);
108         }
109     }
155bf7 110
698678 111     public static void setAlternatingBackground(Component c, int i) {
94750e 112         String clazz = i % 2 == 0 ? "light" : "dark";
698678 113         setCssClass(c, clazz);
JM 114     }
155bf7 115
698678 116     public static Label createAuthorLabel(String wicketId, String author) {
JM 117         Label label = new Label(wicketId, author);
1e8390 118         WicketUtils.setHtmlTooltip(label, author);
698678 119         return label;
1e8390 120     }
JM 121
831469 122     public static ContextImage getPullStatusImage(String wicketId, FederationPullStatus status) {
JM 123         String filename = null;
124         switch (status) {
2548a7 125         case MIRRORED:
831469 126         case PULLED:
JM 127             filename = "bullet_green.png";
128             break;
129         case SKIPPED:
130             filename = "bullet_yellow.png";
131             break;
132         case FAILED:
133             filename = "bullet_red.png";
134             break;
135         case EXCLUDED:
136             filename = "bullet_white.png";
137             break;
138         case PENDING:
db91a3 139         case NOCHANGE:
831469 140         default:
JM 141             filename = "bullet_black.png";
142         }
143         return WicketUtils.newImage(wicketId, filename, status.name());
144     }
145
c1c3c6 146     public static ContextImage getFileImage(String wicketId, String filename) {
JM 147         filename = filename.toLowerCase();
148         if (filename.endsWith(".java")) {
149             return newImage(wicketId, "file_java_16x16.png");
150         } else if (filename.endsWith(".rb")) {
151             return newImage(wicketId, "file_ruby_16x16.png");
152         } else if (filename.endsWith(".php")) {
153             return newImage(wicketId, "file_php_16x16.png");
154         } else if (filename.endsWith(".cs")) {
155             return newImage(wicketId, "file_cs_16x16.png");
156         } else if (filename.endsWith(".cpp")) {
157             return newImage(wicketId, "file_cpp_16x16.png");
158         } else if (filename.endsWith(".c")) {
159             return newImage(wicketId, "file_c_16x16.png");
160         } else if (filename.endsWith(".h")) {
161             return newImage(wicketId, "file_h_16x16.png");
162         } else if (filename.endsWith(".sln")) {
163             return newImage(wicketId, "file_vs_16x16.png");
2a7306 164         } else if (filename.endsWith(".csv") || filename.endsWith(".xls")
JM 165                 || filename.endsWith(".xlsx")) {
c1c3c6 166             return newImage(wicketId, "file_excel_16x16.png");
JM 167         } else if (filename.endsWith(".doc") || filename.endsWith(".docx")) {
a9e6d5 168             return newImage(wicketId, "file_doc_16x16.png");
c1c3c6 169         } else if (filename.endsWith(".ppt")) {
JM 170             return newImage(wicketId, "file_ppt_16x16.png");
171         } else if (filename.endsWith(".zip")) {
172             return newImage(wicketId, "file_zip_16x16.png");
173         } else if (filename.endsWith(".pdf")) {
174             return newImage(wicketId, "file_acrobat_16x16.png");
175         } else if (filename.endsWith(".htm") || filename.endsWith(".html")) {
176             return newImage(wicketId, "file_world_16x16.png");
177         } else if (filename.endsWith(".xml")) {
178             return newImage(wicketId, "file_code_16x16.png");
179         } else if (filename.endsWith(".properties")) {
180             return newImage(wicketId, "file_settings_16x16.png");
181         }
182
2a7306 183         List<String> mdExtensions = GitBlit.getStrings(Keys.web.markdownExtensions);
c1c3c6 184         for (String ext : mdExtensions) {
JM 185             if (filename.endsWith('.' + ext.toLowerCase())) {
186                 return newImage(wicketId, "file_world_16x16.png");
187             }
188         }
189         return newImage(wicketId, "file_16x16.png");
190     }
191
831469 192     public static ContextImage getRegistrationImage(String wicketId, FederationModel registration,
JM 193             Component c) {
194         if (registration.isResultData()) {
195             return WicketUtils.newImage(wicketId, "information_16x16.png",
196                     c.getString("gb.federationResults"));
197         } else {
198             return WicketUtils.newImage(wicketId, "arrow_left.png",
199                     c.getString("gb.federationRegistration"));
200         }
201     }
202
1e8390 203     public static ContextImage newClearPixel(String wicketId) {
JM 204         return newImage(wicketId, "pixel.png");
205     }
c1c3c6 206
1e8390 207     public static ContextImage newBlankImage(String wicketId) {
JM 208         return newImage(wicketId, "blank.png");
209     }
210
211     public static ContextImage newImage(String wicketId, String file) {
212         return newImage(wicketId, file, null);
213     }
214
215     public static ContextImage newImage(String wicketId, String file, String tooltip) {
5450d0 216         ContextImage img = new ContextImage(wicketId, file);
1e8390 217         if (!StringUtils.isEmpty(tooltip)) {
JM 218             setHtmlTooltip(img, tooltip);
219         }
220         return img;
221     }
c1c3c6 222
a45caa 223     public static Label newIcon(String wicketId, String css) {
JM 224         Label lbl = new Label(wicketId);
225         setCssClass(lbl, css);        
226         return lbl;
227     }
228     
229     public static Label newBlankIcon(String wicketId) {
230         Label lbl = new Label(wicketId);
231         setCssClass(lbl, "");
232         lbl.setRenderBodyOnly(true);
233         return lbl;
234     }
235     
1e8390 236     public static ContextRelativeResource getResource(String file) {
5450d0 237         return new ContextRelativeResource(file);
8c9a20 238     }
JM 239
2179fb 240     public static String getGitblitURL(Request request) {
8c9a20 241         HttpServletRequest req = ((WebRequest) request).getHttpServletRequest();
2179fb 242         return HttpUtils.getGitblitURL(req);
8c9a20 243     }
JM 244
245     public static HeaderContributor syndicationDiscoveryLink(final String feedTitle,
246             final String url) {
247         return new HeaderContributor(new IHeaderContributor() {
248             private static final long serialVersionUID = 1L;
249
250             public void renderHead(IHeaderResponse response) {
251                 String contentType = "application/rss+xml";
252
8a53c0 253                 StringBuilder buffer = new StringBuilder();
8c9a20 254                 buffer.append("<link rel=\"alternate\" ");
JM 255                 buffer.append("type=\"").append(contentType).append("\" ");
256                 buffer.append("title=\"").append(feedTitle).append("\" ");
257                 buffer.append("href=\"").append(url).append("\" />");
258                 response.renderString(buffer.toString());
259             }
260         });
698678 261     }
db91a3 262
831469 263     public static PageParameters newTokenParameter(String token) {
JM 264         return new PageParameters("t=" + token);
265     }
db91a3 266
831469 267     public static PageParameters newRegistrationParameter(String url, String name) {
JM 268         return new PageParameters("u=" + url + ",n=" + name);
269     }
155bf7 270
a4d249 271     public static PageParameters newUsernameParameter(String username) {
JM 272         return new PageParameters("user=" + username);
273     }
274
fe24a0 275     public static PageParameters newTeamnameParameter(String teamname) {
JM 276         return new PageParameters("team=" + teamname);
277     }
278
13a3f5 279     public static PageParameters newProjectParameter(String projectName) {
JM 280         return new PageParameters("p=" + projectName);
281     }
282
698678 283     public static PageParameters newRepositoryParameter(String repositoryName) {
ef5c58 284         return new PageParameters("r=" + repositoryName);
831469 285     }
JM 286
287     public static PageParameters newObjectParameter(String objectId) {
288         return new PageParameters("h=" + objectId);
698678 289     }
JM 290
ef5c58 291     public static PageParameters newObjectParameter(String repositoryName, String objectId) {
f1dfc2 292         if (StringUtils.isEmpty(objectId)) {
698678 293             return newRepositoryParameter(repositoryName);
JM 294         }
ef5c58 295         return new PageParameters("r=" + repositoryName + ",h=" + objectId);
698678 296     }
JM 297
2a7306 298     public static PageParameters newPathParameter(String repositoryName, String objectId,
JM 299             String path) {
f1dfc2 300         if (StringUtils.isEmpty(path)) {
ef5c58 301             return newObjectParameter(repositoryName, objectId);
698678 302         }
a2709d 303         if (StringUtils.isEmpty(objectId)) {
JM 304             return new PageParameters("r=" + repositoryName + ",f=" + path);
305         }
ef5c58 306         return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path);
JM 307     }
155bf7 308
2a7306 309     public static PageParameters newLogPageParameter(String repositoryName, String objectId,
JM 310             int pageNumber) {
ef5c58 311         if (pageNumber <= 1) {
JM 312             return newObjectParameter(repositoryName, objectId);
a2709d 313         }
JM 314         if (StringUtils.isEmpty(objectId)) {
e33b91 315             return new PageParameters("r=" + repositoryName + ",pg=" + pageNumber);
ef5c58 316         }
e33b91 317         return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",pg=" + pageNumber);
698678 318     }
98ce17 319
2a7306 320     public static PageParameters newHistoryPageParameter(String repositoryName, String objectId,
JM 321             String path, int pageNumber) {
f602a2 322         if (pageNumber <= 1) {
JM 323             return newObjectParameter(repositoryName, objectId);
324         }
a2709d 325         if (StringUtils.isEmpty(objectId)) {
e33b91 326             return new PageParameters("r=" + repositoryName + ",f=" + path + ",pg=" + pageNumber);
a2709d 327         }
db91a3 328         return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path + ",pg="
JM 329                 + pageNumber);
f602a2 330     }
155bf7 331
2a7306 332     public static PageParameters newBlobDiffParameter(String repositoryName, String baseCommitId,
JM 333             String commitId, String path) {
a2709d 334         if (StringUtils.isEmpty(commitId)) {
JM 335             return new PageParameters("r=" + repositoryName + ",f=" + path + ",hb=" + baseCommitId);
336         }
2a7306 337         return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",f=" + path + ",hb="
JM 338                 + baseCommitId);
f1dfc2 339     }
JM 340
2a7306 341     public static PageParameters newSearchParameter(String repositoryName, String commitId,
33d8d8 342             String search, Constants.SearchType type) {
98ce17 343         if (StringUtils.isEmpty(commitId)) {
9bc17d 344             return new PageParameters("r=" + repositoryName + ",s=" + search + ",st=" + type.name());
98ce17 345         }
2a7306 346         return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search
JM 347                 + ",st=" + type.name());
98ce17 348     }
JM 349
2a7306 350     public static PageParameters newSearchParameter(String repositoryName, String commitId,
33d8d8 351             String search, Constants.SearchType type, int pageNumber) {
a2709d 352         if (StringUtils.isEmpty(commitId)) {
JM 353             return new PageParameters("r=" + repositoryName + ",s=" + search + ",st=" + type.name()
e33b91 354                     + ",pg=" + pageNumber);
a2709d 355         }
2a7306 356         return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",s=" + search
e33b91 357                 + ",st=" + type.name() + ",pg=" + pageNumber);
98ce17 358     }
JM 359
13a3f5 360     public static String getProjectName(PageParameters params) {
JM 361         return params.getString("p", "");
362     }
363
7d35e2 364     public static String getRepositoryName(PageParameters params) {
JM 365         return params.getString("r", "");
366     }
367
368     public static String getObject(PageParameters params) {
a2709d 369         return params.getString("h", null);
7d35e2 370     }
JM 371
372     public static String getPath(PageParameters params) {
373         return params.getString("f", null);
374     }
98ce17 375
f1dfc2 376     public static String getBaseObjectId(PageParameters params) {
JM 377         return params.getString("hb", null);
378     }
7d35e2 379
98ce17 380     public static String getSearchString(PageParameters params) {
JM 381         return params.getString("s", null);
382     }
383
384     public static String getSearchType(PageParameters params) {
385         return params.getString("st", null);
386     }
387
7d35e2 388     public static int getPage(PageParameters params) {
2a7306 389         // index from 1
e33b91 390         return params.getInt("pg", 1);
7d35e2 391     }
2a7306 392
cb57ec 393     public static String getRegEx(PageParameters params) {
JM 394         return params.getString("x", "");
395     }
396
6e6f9f 397     public static String getSet(PageParameters params) {
JM 398         return params.getString("set", "");
399     }
400
cb57ec 401     public static String getTeam(PageParameters params) {
JM 402         return params.getString("team", "");
403     }
404
6e6f9f 405     public static int getDaysBack(PageParameters params) {
JM 406         return params.getInt("db", 14);
407     }
408
dfb889 409     public static String getUsername(PageParameters params) {
JM 410         return params.getString("user", "");
411     }
7d35e2 412
fe24a0 413     public static String getTeamname(PageParameters params) {
JM 414         return params.getString("team", "");
415     }
416
831469 417     public static String getToken(PageParameters params) {
JM 418         return params.getString("t", "");
419     }
db91a3 420
831469 421     public static String getUrlParameter(PageParameters params) {
JM 422         return params.getString("u", "");
423     }
424
425     public static String getNameParameter(PageParameters params) {
426         return params.getString("n", "");
427     }
428
9adf62 429     public static Label createDateLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
2a7306 430         String format = GitBlit.getString(Keys.web.datestampShortFormat, "MM/dd/yy");
JM 431         DateFormat df = new SimpleDateFormat(format);
6c6e7d 432         if (timeZone == null) {
JM 433             timeZone = GitBlit.getTimezone();
698678 434         }
6c6e7d 435         df.setTimeZone(timeZone);
831469 436         String dateString;
JM 437         if (date.getTime() == 0) {
438             dateString = "--";
439         } else {
440             dateString = df.format(date);
441         }
442         String title = null;
443         if (date.getTime() <= System.currentTimeMillis()) {
444             // past
9adf62 445             title = timeUtils.timeAgo(date);
831469 446         }
2a7306 447         if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
698678 448             String tmp = dateString;
JM 449             dateString = title;
450             title = tmp;
451         }
452         Label label = new Label(wicketId, dateString);
9adf62 453         WicketUtils.setCssClass(label, timeUtils.timeAgoCss(date));
831469 454         if (!StringUtils.isEmpty(title)) {
JM 455             WicketUtils.setHtmlTooltip(label, title);
456         }
698678 457         return label;
JM 458     }
db91a3 459
9adf62 460     public static Label createTimeLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
6e6f9f 461         String format = GitBlit.getString(Keys.web.timeFormat, "HH:mm");
JM 462         DateFormat df = new SimpleDateFormat(format);
6c6e7d 463         if (timeZone == null) {
JM 464             timeZone = GitBlit.getTimezone();
6e6f9f 465         }
6c6e7d 466         df.setTimeZone(timeZone);
6e6f9f 467         String timeString;
JM 468         if (date.getTime() == 0) {
469             timeString = "--";
470         } else {
471             timeString = df.format(date);
472         }
9adf62 473         String title = timeUtils.timeAgo(date);
6e6f9f 474         Label label = new Label(wicketId, timeString);
JM 475         if (!StringUtils.isEmpty(title)) {
476             WicketUtils.setHtmlTooltip(label, title);
477         }
478         return label;
479     }
db91a3 480
9adf62 481     public static Label createDatestampLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
6e6f9f 482         String format = GitBlit.getString(Keys.web.datestampLongFormat, "EEEE, MMMM d, yyyy");
JM 483         DateFormat df = new SimpleDateFormat(format);
6c6e7d 484         if (timeZone == null) {
JM 485             timeZone = GitBlit.getTimezone();
6e6f9f 486         }
6c6e7d 487         df.setTimeZone(timeZone);
6e6f9f 488         String dateString;
JM 489         if (date.getTime() == 0) {
490             dateString = "--";
491         } else {
492             dateString = df.format(date);
493         }
494         String title = null;
712210 495         if (TimeUtils.isToday(date)) {
9adf62 496             title = timeUtils.today();
712210 497         } else if (TimeUtils.isYesterday(date)) {
9adf62 498                 title = timeUtils.yesterday();
712210 499         } else if (date.getTime() <= System.currentTimeMillis()) {
6e6f9f 500             // past
9adf62 501             title = timeUtils.timeAgo(date);
6e6f9f 502         }
JM 503         if ((System.currentTimeMillis() - date.getTime()) < 10 * 24 * 60 * 60 * 1000L) {
504             String tmp = dateString;
505             dateString = title;
506             title = tmp;
507         }
508         Label label = new Label(wicketId, dateString);
509         if (!StringUtils.isEmpty(title)) {
510             WicketUtils.setHtmlTooltip(label, title);
511         }
512         return label;
513     }
155bf7 514
9adf62 515     public static Label createTimestampLabel(String wicketId, Date date, TimeZone timeZone, TimeUtils timeUtils) {
2a7306 516         String format = GitBlit.getString(Keys.web.datetimestampLongFormat,
a4ed6d 517                 "EEEE, MMMM d, yyyy HH:mm Z");
2a7306 518         DateFormat df = new SimpleDateFormat(format);
6c6e7d 519         if (timeZone == null) {
JM 520             timeZone = GitBlit.getTimezone();
bc10f9 521         }
6c6e7d 522         df.setTimeZone(timeZone);
a2709d 523         String dateString;
JM 524         if (date.getTime() == 0) {
525             dateString = "--";
526         } else {
527             dateString = df.format(date);
528         }
831469 529         String title = null;
JM 530         if (date.getTime() <= System.currentTimeMillis()) {
531             // past
9adf62 532             title = timeUtils.timeAgo(date);
831469 533         }
bc10f9 534         Label label = new Label(wicketId, dateString);
831469 535         if (!StringUtils.isEmpty(title)) {
JM 536             WicketUtils.setHtmlTooltip(label, title);
537         }
bc10f9 538         return label;
JM 539     }
008322 540
f1720c 541     public static IChartData getChartData(Collection<Metric> metrics) {
JM 542         final double[] commits = new double[metrics.size()];
543         final double[] tags = new double[metrics.size()];
544         int i = 0;
545         double max = 0;
546         for (Metric m : metrics) {
547             commits[i] = m.count;
548             if (m.tag > 0) {
549                 tags[i] = m.count;
550             } else {
551                 tags[i] = -1d;
552             }
553             max = Math.max(max, m.count);
554             i++;
555         }
556         IChartData data = new AbstractChartData(max) {
557             private static final long serialVersionUID = 1L;
558
559             public double[][] getData() {
560                 return new double[][] { commits, tags };
561             }
562         };
563         return data;
564     }
565
566     public static double maxValue(Collection<Metric> metrics) {
567         double max = Double.MIN_VALUE;
568         for (Metric m : metrics) {
569             if (m.count > max) {
570                 max = m.count;
571             }
572         }
573         return max;
574     }
008322 575
f1720c 576     public static IChartData getScatterData(Collection<Metric> metrics) {
JM 577         final double[] y = new double[metrics.size()];
578         final double[] x = new double[metrics.size()];
579         int i = 0;
580         double max = 0;
581         for (Metric m : metrics) {
582             y[i] = m.count;
583             if (m.duration > 0) {
584                 x[i] = m.duration;
585             } else {
586                 x[i] = -1d;
587             }
588             max = Math.max(max, m.count);
589             i++;
590         }
591         IChartData data = new AbstractChartData(max) {
592             private static final long serialVersionUID = 1L;
593
594             public double[][] getData() {
595                 return new double[][] { x, y };
596             }
597         };
598         return data;
599     }
600
5fe7df 601 }