-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommitInfoComponent.java
213 lines (185 loc) · 7.43 KB
/
CommitInfoComponent.java
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
package ch.hsr.isf.serepo.client.webapp.view.seitems;
import java.util.LinkedList;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.jsclipboard.ClipboardButton;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.Resource;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.ValoTheme;
import ch.hsr.isf.serepo.client.webapp.AppNavigator;
import ch.hsr.isf.serepo.client.webapp.services.SeRepoRestAPI;
import ch.hsr.isf.serepo.client.webapp.view.AppViewType;
import ch.hsr.isf.serepo.data.restinterface.commit.Commit;
import ch.hsr.isf.serepo.data.restinterface.commit.CommitContainer;
import ch.hsr.isf.serepo.data.restinterface.repository.Repository;
import ch.hsr.isf.serepo.data.restinterface.repository.RepositoryContainer;
public class CommitInfoComponent extends CustomComponent {
private static final long serialVersionUID = -9203631624167358267L;
private HorizontalLayout layout = new HorizontalLayout() {
private static final long serialVersionUID = 2725271231172732137L;
@Override
public void addComponent(Component c) {
super.addComponent(c);
super.setComponentAlignment(c, Alignment.MIDDLE_LEFT);
}
};
private ComboBox cmbxRepositories = new ComboBox();
private ComboBox cmbxCommits = new ComboBox();
private boolean cmbxValueChangeListenerEnabled = false;
private TextField tfCommitId = new TextField(null, "");
public CommitInfoComponent() {
createLayout();
loadRepositories();
setCompositionRoot(layout);
}
private void loadRepositories() {
cmbxValueChangeListenerEnabled = false;
try {
cmbxRepositories.removeAllItems();
RepositoryContainer repositoryContainer = SeRepoRestAPI.getRepositories();
for (Repository repo : repositoryContainer.getRepositories()) {
cmbxRepositories.addItem(repo.getName());
}
} finally {
cmbxValueChangeListenerEnabled = true;
}
}
private void createLayout() {
layout.setSpacing(true);
layout.setMargin(new MarginInfo(false, false, true, false));
configRepositoryGroup();
configCommitGroup();
}
private void configRepositoryGroup() {
Button btnRepository = createActionButton("Repository:", FontAwesome.DATABASE, new ClickListener() {
private static final long serialVersionUID = 47623509560655628L;
@Override
public void buttonClick(ClickEvent event) {
AppNavigator.navigateTo(AppViewType.REPOSITORIES);
}
});
btnRepository.setDescription("go to repositories");
layout.addComponent(btnRepository);
configRepositoriesCmbx();
layout.addComponent(cmbxRepositories);
}
private void configCommitGroup() {
Button btnCommit = createActionButton("Commit:", FontAwesome.HISTORY, new ClickListener() {
private static final long serialVersionUID = -5239748636964294986L;
@Override
public void buttonClick(ClickEvent event) {
AppNavigator.navigateTo(AppViewType.COMMITS, cmbxRepositories.getValue().toString());
}
});
btnCommit.addStyleName("left-space-30");
btnCommit.setDescription("go to commits");
layout.addComponent(btnCommit);
configCommitsCmbx();
layout.addComponent(cmbxCommits);
tfCommitId.setId("textfield-commitid");
tfCommitId.setWidth("350px");
tfCommitId.addStyleName(ValoTheme.TEXTFIELD_SMALL);
layout.addComponent(tfCommitId);
ClipboardButton clipboardButton = new ClipboardButton("textfield-commitid");
clipboardButton.addSuccessListener(new ClipboardButton.SuccessListener() {
private static final long serialVersionUID = -71258407509006880L;
@Override
public void onSuccess() {
Notification.show("CommitId was copied to clipboard");
}
});
clipboardButton.setClipboardButtonCaption("Copy");
layout.addComponent(clipboardButton);
}
private void configRepositoriesCmbx() {
cmbxRepositories.setWidth("300px");
cmbxRepositories.setTextInputAllowed(false);
cmbxRepositories.addStyleName(ValoTheme.COMBOBOX_SMALL);
cmbxRepositories.setScrollToSelectedItem(true);
cmbxRepositories.setNullSelectionAllowed(false);
cmbxRepositories.setDescription("Change repository");
cmbxRepositories.addValueChangeListener(new ValueChangeListener() {
private static final long serialVersionUID = -7191898031310316511L;
@Override
public void valueChange(ValueChangeEvent event) {
if (cmbxValueChangeListenerEnabled) {
String repository = event.getProperty().getValue().toString();
String commitId = SeRepoRestAPI.getCommitsForRepository(repository).getCommits().get(0).getCommitId();
AppNavigator.navigateTo(AppViewType.SEITEMS, repository, commitId);
}
}
});
}
private void configCommitsCmbx() {
cmbxCommits.setWidth("700px");
cmbxCommits.setTextInputAllowed(false);
cmbxCommits.addStyleName(ValoTheme.COMBOBOX_SMALL);
cmbxCommits.setScrollToSelectedItem(true);
cmbxCommits.setNullSelectionAllowed(false);
cmbxCommits.setDescription("Change commit");
cmbxCommits.addValueChangeListener(new ValueChangeListener() {
private static final long serialVersionUID = 8134832018266747215L;
@Override
public void valueChange(ValueChangeEvent event) {
if (cmbxValueChangeListenerEnabled) {
AppNavigator.navigateTo(AppViewType.SEITEMS, cmbxRepositories.getValue().toString(), event.getProperty().getValue().toString());
}
}
});
}
private Button createActionButton(String caption, Resource icon, ClickListener action) {
Button btn = new Button(caption, icon);
btn.addClickListener(action);
btn.addStyleName(ValoTheme.BUTTON_BORDERLESS);
btn.addStyleName(ValoTheme.BUTTON_LINK);
btn.addStyleName(ValoTheme.BUTTON_SMALL);
return btn;
}
public void setRepository(String repository) {
cmbxValueChangeListenerEnabled = false;
try {
if (!cmbxRepositories.containsId(repository)) {
cmbxRepositories.addItem(repository);
}
cmbxRepositories.select(repository);
LinkedList<Commit> commits = loadCommits(repository);
addCommitsToComboBox(commits);
} finally {
cmbxValueChangeListenerEnabled = true;
}
}
private void addCommitsToComboBox(LinkedList<Commit> commits) {
cmbxCommits.removeAllItems();
for (Commit commit : commits) {
cmbxCommits.addItem(commit.getCommitId());
cmbxCommits.setItemCaption(commit.getCommitId(), String.format("%s [%s]", commit.getShortMessage(), commit.getCommitId()));
}
}
private LinkedList<Commit> loadCommits(String repository) {
CommitContainer commitsForRepository = SeRepoRestAPI.getCommitsForRepository(repository);
return new LinkedList<>(commitsForRepository.getCommits());
}
public void setCommitId(String commitId) {
if (!cmbxCommits.containsId(commitId)) {
cmbxCommits.addItem(commitId);
}
cmbxValueChangeListenerEnabled = false;
try {
cmbxCommits.select(commitId);
} finally {
cmbxValueChangeListenerEnabled = true;
}
tfCommitId.setValue(commitId);
}
}