summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/client/SearchDialog.java
blob: 829bc52a26e23e6ba517eb05a794298ac712fc8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * 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.client;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.gitblit.Constants;
import com.gitblit.models.FeedEntryModel;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.StringUtils;

/**
 * The search dialog allows searching of a repository branch. This matches the
 * search implementation of the site.
 * 
 * @author James Moger
 * 
 */
public class SearchDialog extends JFrame {

	private static final long serialVersionUID = 1L;

	private final boolean isSearch;

	private final GitblitClient gitblit;

	private FeedEntryTableModel tableModel;

	private HeaderPanel header;

	private JTable table;

	private JComboBox repositorySelector;

	private DefaultComboBoxModel branchChoices;

	private JComboBox branchSelector;

	private JComboBox searchTypeSelector;

	private JTextField searchFragment;

	private JComboBox maxHitsSelector;

	private int page;

	private JButton prev;

	private JButton next;

	public SearchDialog(GitblitClient gitblit, boolean isSearch) {
		super();
		this.gitblit = gitblit;
		this.isSearch = isSearch;
		setTitle(Translation.get(isSearch ? "gb.search" : "gb.log"));
		setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
		initialize();
		setSize(900, 550);
	}

	private void initialize() {

		prev = new JButton("<");
		prev.setToolTipText(Translation.get("gb.pagePrevious"));
		prev.setEnabled(false);
		prev.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				search(--page);
			}
		});

		next = new JButton(">");
		next.setToolTipText(Translation.get("gb.pageNext"));
		next.setEnabled(false);
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				search(++page);
			}
		});

		final JButton search = new JButton(Translation.get(isSearch ? "gb.search" : "gb.refresh"));
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				search(0);
			}
		});

		final JButton viewCommit = new JButton(Translation.get("gb.view"));
		viewCommit.setEnabled(false);
		viewCommit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewCommit();
			}
		});

		final JButton viewCommitDiff = new JButton(Translation.get("gb.commitdiff"));
		viewCommitDiff.setEnabled(false);
		viewCommitDiff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewCommitDiff();
			}
		});

		final JButton viewTree = new JButton(Translation.get("gb.tree"));
		viewTree.setEnabled(false);
		viewTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewTree();
			}
		});

		JPanel controls = new JPanel(new FlowLayout(FlowLayout.CENTER, Utils.MARGIN, 0));
		controls.add(viewCommit);
		controls.add(viewCommitDiff);
		controls.add(viewTree);

		NameRenderer nameRenderer = new NameRenderer();
		tableModel = new FeedEntryTableModel();
		header = new HeaderPanel(Translation.get(isSearch ? "gb.search" : "gb.log"),
				isSearch ? "search-icon.png" : "commit_changes_16x16.png");
		table = Utils.newTable(tableModel, Utils.DATE_FORMAT);

		String name = table.getColumnName(FeedEntryTableModel.Columns.Author.ordinal());
		table.getColumn(name).setCellRenderer(nameRenderer);
		name = table.getColumnName(FeedEntryTableModel.Columns.Repository.ordinal());
		table.getColumn(name).setCellRenderer(nameRenderer);

		name = table.getColumnName(FeedEntryTableModel.Columns.Branch.ordinal());
		table.getColumn(name).setCellRenderer(new BranchRenderer());

		name = table.getColumnName(FeedEntryTableModel.Columns.Message.ordinal());
		table.getColumn(name).setCellRenderer(new MessageRenderer());

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					if (e.isControlDown()) {
						viewCommitDiff();
					} else {
						viewCommit();
					}
				}
			}
		});

		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting()) {
					return;
				}
				boolean singleSelection = table.getSelectedRowCount() == 1;
				viewCommit.setEnabled(singleSelection);
				viewCommitDiff.setEnabled(singleSelection);
				viewTree.setEnabled(singleSelection);
			}
		});

		repositorySelector = new JComboBox(gitblit.getRepositories().toArray());
		repositorySelector.setRenderer(nameRenderer);
		repositorySelector.setForeground(nameRenderer.getForeground());
		repositorySelector.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				// repopulate the branch list based on repository selection
				// preserve branch selection, if possible
				String selectedBranch = null;
				if (branchSelector.getSelectedIndex() > -1) {
					selectedBranch = branchSelector.getSelectedItem().toString();
				}
				updateBranches();
				if (StringUtils.isEmpty(selectedBranch)) {
					// do not select branch
					branchSelector.setSelectedIndex(-1);
				} else {
					if (branchChoices.getIndexOf(selectedBranch) > -1) {
						// select branch
						branchChoices.setSelectedItem(selectedBranch);
					} else {
						// branch does not exist, do not select branch
						branchSelector.setSelectedIndex(-1);
					}
				}
			}
		});

		branchChoices = new DefaultComboBoxModel();
		branchSelector = new JComboBox(branchChoices);
		branchSelector.setRenderer(new BranchRenderer());

		searchTypeSelector = new JComboBox(Constants.SearchType.values());
		searchTypeSelector.setSelectedItem(Constants.SearchType.COMMIT);

		maxHitsSelector = new JComboBox(new Integer[] { 25, 50, 75, 100 });
		maxHitsSelector.setSelectedIndex(0);

		searchFragment = new JTextField(25);
		searchFragment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				search(0);
			}
		});

		JPanel queryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, Utils.MARGIN, 0));
		queryPanel.add(new JLabel(Translation.get("gb.repository")));
		queryPanel.add(repositorySelector);
		queryPanel.add(new JLabel(Translation.get("gb.branch")));
		queryPanel.add(branchSelector);
		if (isSearch) {
			queryPanel.add(new JLabel(Translation.get("gb.type")));
			queryPanel.add(searchTypeSelector);
		}
		queryPanel.add(new JLabel(Translation.get("gb.maxHits")));
		queryPanel.add(maxHitsSelector);

		JPanel actionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, Utils.MARGIN, 0));
		actionsPanel.add(search);
		actionsPanel.add(prev);
		actionsPanel.add(next);

		JPanel northControls = new JPanel(new BorderLayout(Utils.MARGIN, Utils.MARGIN));
		northControls.add(queryPanel, BorderLayout.WEST);
		if (isSearch) {
			northControls.add(searchFragment, BorderLayout.CENTER);
		}
		northControls.add(actionsPanel, BorderLayout.EAST);

		JPanel northPanel = new JPanel(new BorderLayout(0, Utils.MARGIN));
		northPanel.add(header, BorderLayout.NORTH);
		northPanel.add(northControls, BorderLayout.CENTER);

		JPanel contentPanel = new JPanel() {

			private static final long serialVersionUID = 1L;

			@Override
			public Insets getInsets() {
				return Utils.INSETS;
			}
		};
		contentPanel.setLayout(new BorderLayout(Utils.MARGIN, Utils.MARGIN));
		contentPanel.add(northPanel, BorderLayout.NORTH);
		contentPanel.add(new JScrollPane(table), BorderLayout.CENTER);
		contentPanel.add(controls, BorderLayout.SOUTH);
		setLayout(new BorderLayout());
		add(contentPanel, BorderLayout.CENTER);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent event) {
				if (isSearch) {
					searchFragment.requestFocus();
				} else {
					search(0);
				}
			}

			@Override
			public void windowActivated(WindowEvent event) {
				if (isSearch) {
					searchFragment.requestFocus();
				}
			}
		});
	}

	public void selectRepository(RepositoryModel repository) {
		repositorySelector.setSelectedItem(repository);
	}

	private void updateBranches() {
		String repository = null;
		if (repositorySelector.getSelectedIndex() > -1) {
			repository = repositorySelector.getSelectedItem().toString();
		}
		List<String> branches = gitblit.getBranches(repository);
		branchChoices.removeAllElements();
		for (String branch : branches) {
			branchChoices.addElement(branch);
		}
	}

	protected void search(final int page) {
		this.page = page;
		final String repository = repositorySelector.getSelectedItem().toString();
		final String branch = branchSelector.getSelectedIndex() > -1 ? branchSelector
				.getSelectedItem().toString() : null;
		final Constants.SearchType searchType = (Constants.SearchType) searchTypeSelector
				.getSelectedItem();
		final String fragment = isSearch ? searchFragment.getText() : null;
		final int maxEntryCount = maxHitsSelector.getSelectedIndex() > -1 ? ((Integer) maxHitsSelector
				.getSelectedItem()) : -1;

		if (isSearch && StringUtils.isEmpty(fragment)) {
			return;
		}
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		SwingWorker<List<FeedEntryModel>, Void> worker = new SwingWorker<List<FeedEntryModel>, Void>() {
			@Override
			protected List<FeedEntryModel> doInBackground() throws IOException {
				if (isSearch) {
					return gitblit.search(repository, branch, fragment, searchType, maxEntryCount,
							page);
				} else {
					return gitblit.log(repository, branch, maxEntryCount, page);
				}
			}

			@Override
			protected void done() {
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
				try {
					List<FeedEntryModel> results = get();
					if (isSearch) {
						updateTable(true, fragment, results);
					} else {
						updateTable(true, branch == null ? "" : branch, results);
					}
				} catch (Throwable t) {
					Utils.showException(SearchDialog.this, t);
				}
			}
		};
		worker.execute();
	}

	protected void updateTable(boolean pack, String text, List<FeedEntryModel> entries) {
		tableModel.entries.clear();
		tableModel.entries.addAll(entries);
		tableModel.fireTableDataChanged();
		setTitle(Translation.get(isSearch ? "gb.search" : "gb.log")
				+ (StringUtils.isEmpty(text) ? "" : (": " + text)) + " (" + entries.size()
				+ (page > 0 ? (", pg " + (page + 1)) : "") + ")");
		header.setText(getTitle());
		if (pack) {
			Utils.packColumns(table, Utils.MARGIN);
		}
		table.scrollRectToVisible(new Rectangle(table.getCellRect(0, 0, true)));

		// update pagination buttons
		int maxHits = (Integer) maxHitsSelector.getSelectedItem();
		next.setEnabled(entries.size() == maxHits);
		prev.setEnabled(page > 0);
	}

	protected FeedEntryModel getSelectedSyndicatedEntry() {
		int viewRow = table.getSelectedRow();
		int modelRow = table.convertRowIndexToModel(viewRow);
		FeedEntryModel entry = tableModel.get(modelRow);
		return entry;
	}

	protected void viewCommit() {
		FeedEntryModel entry = getSelectedSyndicatedEntry();
		Utils.browse(entry.link);
	}

	protected void viewCommitDiff() {
		FeedEntryModel entry = getSelectedSyndicatedEntry();
		Utils.browse(entry.link.replace("/commit/", "/commitdiff/"));
	}

	protected void viewTree() {
		FeedEntryModel entry = getSelectedSyndicatedEntry();
		Utils.browse(entry.link.replace("/commit/", "/tree/"));
	}
}