| | |
| | | /*
|
| | | * Copyright 2011 gitblit.com.
|
| | | *
|
| | | * Licensed under the Apache License, Version 2.0 (the "License");
|
| | | * you may not use this file except in compliance with the License.
|
| | | * You may obtain a copy of the License at
|
| | | *
|
| | | * http://www.apache.org/licenses/LICENSE-2.0
|
| | | *
|
| | | * Unless required by applicable law or agreed to in writing, software
|
| | | * distributed under the License is distributed on an "AS IS" BASIS,
|
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| | | * See the License for the specific language governing permissions and
|
| | | * limitations under the License.
|
| | | */
|
| | | package com.gitblit.wicket.pages;
|
| | |
|
| | | import java.util.Collections;
|
| | | import java.util.Comparator;
|
| | | import java.util.Iterator;
|
| | | import java.util.List;
|
| | | import java.io.File;
|
| | | import java.io.FileReader;
|
| | | import java.io.InputStream;
|
| | | import java.io.InputStreamReader;
|
| | | import java.text.MessageFormat;
|
| | |
|
| | | import org.apache.wicket.PageParameters;
|
| | | import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
|
| | | import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
|
| | | import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
|
| | | import org.apache.wicket.Component;
|
| | | import org.apache.wicket.markup.html.basic.Label;
|
| | | import org.apache.wicket.markup.repeater.Item;
|
| | | import org.apache.wicket.markup.repeater.data.DataView;
|
| | | import org.apache.wicket.model.IModel;
|
| | | import org.apache.wicket.model.Model;
|
| | | import org.apache.wicket.resource.ContextRelativeResource;
|
| | |
|
| | | import com.gitblit.StoredSettings;
|
| | | import com.gitblit.utils.Utils;
|
| | | import com.gitblit.wicket.BasePage;
|
| | | import com.gitblit.wicket.GitBlitWebApp;
|
| | | import com.gitblit.wicket.LinkPanel;
|
| | | import com.gitblit.GitBlit;
|
| | | import com.gitblit.Keys;
|
| | | import com.gitblit.utils.MarkdownUtils;
|
| | | import com.gitblit.utils.StringUtils;
|
| | | import com.gitblit.wicket.GitBlitWebSession;
|
| | | import com.gitblit.wicket.WicketUtils;
|
| | | import com.gitblit.wicket.models.RepositoryModel;
|
| | | import com.gitblit.wicket.panels.AdminLinksPanel;
|
| | | import com.gitblit.wicket.panels.PageFooter;
|
| | | import com.gitblit.wicket.panels.PageHeader;
|
| | | import com.gitblit.wicket.panels.RepositoriesPanel;
|
| | |
|
| | |
|
| | | public class RepositoriesPage extends BasePage {
|
| | | public class RepositoriesPage extends RootPage {
|
| | |
|
| | | public RepositoriesPage() {
|
| | | add(new PageHeader("pageHeader"));
|
| | | super();
|
| | | setupPage("", "");
|
| | |
|
| | | add(new AdminLinksPanel("adminPanel").setVisible(StoredSettings.getBoolean("allowAdministration", false)));
|
| | | |
| | | add(new Label("repositoriesMessage", StoredSettings.getString("repositoriesMessage", "")).setEscapeModelStrings(false));
|
| | | // check to see if we should display a login message
|
| | | boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, true);
|
| | | if (authenticateView && !GitBlitWebSession.get().isLoggedIn()) {
|
| | | String messageSource = GitBlit.getString(Keys.web.loginMessage, "gitblit");
|
| | | String message = readMarkdown(messageSource, "login.mkd");
|
| | | Component repositoriesMessage = new Label("repositoriesMessage", message);
|
| | | add(repositoriesMessage.setEscapeModelStrings(false));
|
| | | add(new Label("repositoriesPanel"));
|
| | | return;
|
| | | }
|
| | |
|
| | | List<RepositoryModel> rows = GitBlitWebApp.get().getRepositories(getRequest());
|
| | | DataProvider dp = new DataProvider(rows);
|
| | | DataView<RepositoryModel> dataView = new DataView<RepositoryModel>("repository", dp) {
|
| | | private static final long serialVersionUID = 1L;
|
| | | int counter = 0;
|
| | |
|
| | | public void populateItem(final Item<RepositoryModel> item) {
|
| | | final RepositoryModel entry = item.getModelObject();
|
| | | PageParameters pp = new PageParameters("p=" + entry.name);
|
| | | item.add(new LinkPanel("repositoryName", "list", entry.name, SummaryPage.class, pp));
|
| | | item.add(new LinkPanel("repositoryDescription", "list", entry.description, SummaryPage.class, pp));
|
| | | item.add(new Label("repositoryOwner", entry.owner));
|
| | |
|
| | | String lastChange = Utils.timeAgo(entry.lastChange);
|
| | | Label lastChangeLabel = new Label("repositoryLastChange", lastChange);
|
| | | item.add(lastChangeLabel);
|
| | | WicketUtils.setCssClass(lastChangeLabel, Utils.timeAgoCss(entry.lastChange));
|
| | |
|
| | | String clazz = counter % 2 == 0 ? "dark" : "light";
|
| | | WicketUtils.setCssClass(item, clazz);
|
| | | counter++;
|
| | | }
|
| | | };
|
| | | add(dataView);
|
| | |
|
| | | add(newSort("orderByRepository", SortBy.repository, dp, dataView));
|
| | | add(newSort("orderByDescription", SortBy.description, dp, dataView));
|
| | | add(newSort("orderByOwner", SortBy.owner, dp, dataView));
|
| | | add(newSort("orderByDate", SortBy.date, dp, dataView));
|
| | |
|
| | | add(new PageFooter("pageFooter", ""));
|
| | | // Load the markdown welcome message
|
| | | String messageSource = GitBlit.getString(Keys.web.repositoriesMessage, "gitblit");
|
| | | String message = readMarkdown(messageSource, "welcome.mkd");
|
| | | Component repositoriesMessage = new Label("repositoriesMessage", message)
|
| | | .setEscapeModelStrings(false).setVisible(message.length() > 0);
|
| | | add(repositoriesMessage);
|
| | | RepositoriesPanel repositories = new RepositoriesPanel("repositoriesPanel", showAdmin,
|
| | | null, getAccessRestrictions());
|
| | | // push the panel down if we are hiding the admin controls and the
|
| | | // welcome message
|
| | | if (!showAdmin && !repositoriesMessage.isVisible()) {
|
| | | WicketUtils.setCssStyle(repositories, "padding-top:5px;");
|
| | | }
|
| | | add(repositories);
|
| | | }
|
| | |
|
| | | protected enum SortBy {
|
| | | repository, description, owner, date;
|
| | | private String readMarkdown(String messageSource, String resource) {
|
| | | String message = "";
|
| | | if (messageSource.equalsIgnoreCase("gitblit")) {
|
| | | // Read default message
|
| | | message = readDefaultMarkdown(resource);
|
| | | } else {
|
| | | // Read user-supplied message
|
| | | if (!StringUtils.isEmpty(messageSource)) {
|
| | | File file = new File(messageSource);
|
| | | if (file.exists()) {
|
| | | try {
|
| | | FileReader reader = new FileReader(file);
|
| | | message = MarkdownUtils.transformMarkdown(reader);
|
| | | } catch (Throwable t) {
|
| | | message = "Failed to read " + file;
|
| | | warn(message, t);
|
| | | }
|
| | | } else {
|
| | | message = messageSource + " is not a valid file.";
|
| | | }
|
| | | }
|
| | | }
|
| | | return message;
|
| | | }
|
| | |
|
| | | protected OrderByBorder newSort(String wicketId, SortBy field, SortableDataProvider<?> dp, final DataView<?> dataView) {
|
| | | return new OrderByBorder(wicketId, field.name(), dp) {
|
| | | private static final long serialVersionUID = 1L;
|
| | |
|
| | | @Override
|
| | | protected void onSortChanged() {
|
| | | dataView.setCurrentPage(0);
|
| | | }
|
| | | };
|
| | | }
|
| | |
|
| | | private class DataProvider extends SortableDataProvider<RepositoryModel> {
|
| | | private static final long serialVersionUID = 1L;
|
| | | private List<RepositoryModel> list = null;
|
| | |
|
| | | protected DataProvider(List<RepositoryModel> list) {
|
| | | this.list = list;
|
| | | setSort(SortBy.date.name(), false);
|
| | | private String readDefaultMarkdown(String file) {
|
| | | String message;
|
| | | try {
|
| | | ContextRelativeResource res = WicketUtils.getResource(file);
|
| | | InputStream is = res.getResourceStream().getInputStream();
|
| | | InputStreamReader reader = new InputStreamReader(is);
|
| | | message = MarkdownUtils.transformMarkdown(reader);
|
| | | reader.close();
|
| | | } catch (Throwable t) {
|
| | | message = MessageFormat.format("Failed to read default message from {0}!", file);
|
| | | error(message, t, false);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public int size() {
|
| | | if (list == null)
|
| | | return 0;
|
| | | return list.size();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public IModel<RepositoryModel> model(RepositoryModel header) {
|
| | | return new Model<RepositoryModel>(header);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Iterator<RepositoryModel> iterator(int first, int count) {
|
| | | SortParam sp = getSort();
|
| | | String prop = sp.getProperty();
|
| | | final boolean asc = sp.isAscending();
|
| | |
|
| | | if (prop == null || prop.equals(SortBy.date.name())) {
|
| | | Collections.sort(list, new Comparator<RepositoryModel>() {
|
| | | @Override
|
| | | public int compare(RepositoryModel o1, RepositoryModel o2) {
|
| | | if (asc)
|
| | | return o1.lastChange.compareTo(o2.lastChange);
|
| | | return o2.lastChange.compareTo(o1.lastChange);
|
| | | }
|
| | | });
|
| | | } else if (prop.equals(SortBy.repository.name())) {
|
| | | Collections.sort(list, new Comparator<RepositoryModel>() {
|
| | | @Override
|
| | | public int compare(RepositoryModel o1, RepositoryModel o2) {
|
| | | if (asc)
|
| | | return o1.name.compareTo(o2.name);
|
| | | return o2.name.compareTo(o1.name);
|
| | | }
|
| | | });
|
| | | } else if (prop.equals(SortBy.owner.name())) {
|
| | | Collections.sort(list, new Comparator<RepositoryModel>() {
|
| | | @Override
|
| | | public int compare(RepositoryModel o1, RepositoryModel o2) {
|
| | | if (asc)
|
| | | return o1.owner.compareTo(o2.owner);
|
| | | return o2.owner.compareTo(o1.owner);
|
| | | }
|
| | | });
|
| | | } else if (prop.equals(SortBy.description.name())) {
|
| | | Collections.sort(list, new Comparator<RepositoryModel>() {
|
| | | @Override
|
| | | public int compare(RepositoryModel o1, RepositoryModel o2) {
|
| | | if (asc)
|
| | | return o1.description.compareTo(o2.description);
|
| | | return o2.description.compareTo(o1.description);
|
| | | }
|
| | | });
|
| | | }
|
| | | return list.subList(first, first + count).iterator();
|
| | | }
|
| | | return message;
|
| | | }
|
| | | }
|