-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteDesktopApp.java
340 lines (290 loc) · 14.5 KB
/
RemoteDesktopApp.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
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
package remoteokdesktop.gui;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import net.miginfocom.swing.MigLayout;
import remoteokdesktop.model.RemoteOkJob;
import remoteokdesktop.util.ComponentUtils;
import remoteokdesktop.util.RemoteOkUtils;
import static java.util.Objects.nonNull;
import static remoteokdesktop.util.ComponentUtils.*;
import static remoteokdesktop.util.StringUtils.formatDate;
import static remoteokdesktop.util.StringUtils.getDescription;
public class ListFrame extends JFrame {
private JPanel allListPanel = new WhitePanel(new MigLayout("fillx"));
private JPanel likedListPanel = new WhitePanel(new MigLayout("fillx"));
private JTextField searchField = new JTextField(25);
private JPanel pagesPanel = new WhitePanel(new MigLayout(""));
private JPanel registryQtyPanel = new WhitePanel(new MigLayout(""));
private JTabbedPane tabbedPane = new JTabbedPane();
private JPanel footerPanel = new WhitePanel(new MigLayout("fillx"));
private List<RemoteOkJob> currentShowingJobs = new ArrayList<>();
private String email = "";
private final Integer jobsPerPage = 50;
public ListFrame() {
this.setTitle("RemoteOK");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(550, 700));
createComponents();
this.setVisible(true);
this.getContentPane().setBackground(Color.WHITE);
this.pack();
this.setLocationRelativeTo(null);
}
public ListFrame(String email) {
this();
this.email = email;
}
public void createComponents() {
try {
this.setLayout(new MigLayout("fillx"));
JPanel searchPanel = new WhitePanel(new MigLayout("fillx"));
searchField.addActionListener((ev) -> {
String searchText = ((JTextField) ev.getSource()).getText();
paintTab(searchText, 0);
});
searchPanel.add(searchField, "align center");
this.add(searchPanel, "align center, wrap");
JScrollPane allListScroller = new JScrollPane(allListPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
allListScroller.getVerticalScrollBar().setUnitIncrement(20);
allListScroller.setBackground(Color.WHITE);
this.paintAllJobs(0);
tabbedPane.add("Todos", allListScroller);
JScrollPane likedListScroller = new JScrollPane(likedListPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
likedListScroller.getVerticalScrollBar().setUnitIncrement(20);
likedListScroller.setBackground(Color.WHITE);
tabbedPane.addChangeListener((e) -> {
paintTab(null, 0);
});
tabbedPane.add("Favoritos", likedListScroller);
tabbedPane.getComponentAt(0).setBackground(Color.BLACK);
this.add(tabbedPane, "align center, growy, wrap");
this.add(footerPanel, "growx");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void paintPageCountAndRegistryQty(Integer selected) {
footerPanel.removeAll();
pagesPanel.removeAll();
registryQtyPanel.removeAll();
for (int i = 0; i < Math.ceil(currentShowingJobs.size() / (double) jobsPerPage); i++) {
JLabel pageLabel = unboldLabel(String.format("%s", i + 1));
if (i == selected) {
bold(pageLabel);
}
pageLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
pageLabel.setForeground(Color.BLUE);
pageLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
if (!isBold(label)) {
Arrays.asList(pagesPanel.getComponents()).forEach(ComponentUtils::unbold);
bold(label);
paintTab(searchField.getText(), Integer.parseInt(label.getText()) - 1);
}
}
});
pagesPanel.add(pageLabel);
}
footerPanel.add(pagesPanel, "align center");
registryQtyPanel.add(new JLabel(String.format("%s jobs encontrados", currentShowingJobs.size())));
footerPanel.add(registryQtyPanel, "dock east");
}
private void paintTab(String searchText, Integer page) {
int selectedIndex = tabbedPane.getSelectedIndex();
switch (selectedIndex) {
case 0:
paintJobs(getJobs(searchText), page, false);
break;
case 1:
paintJobs(getLikedJobs(searchText), page, true);
break;
}
}
private void paintAllJobs(Integer page) {
paintJobs(getJobs(null), page, false);
}
private void paintLikedJobs(Integer page) {
paintJobs(getLikedJobs(null), page, true);
}
private List<RemoteOkJob> getJobs(String filter) {
List<RemoteOkJob> jobs = null;
if (nonNull(filter)) {
currentShowingJobs = jobs = RemoteOkUtils.getAll(filter);
} else {
currentShowingJobs = jobs = RemoteOkUtils.getAll();
}
return jobs;
}
private List<RemoteOkJob> getLikedJobs(String filter) {
List<RemoteOkJob> jobs = null;
if (nonNull(filter) && !filter.isEmpty()) {
currentShowingJobs = jobs = RemoteOkUtils.getLiked(filter);
} else {
currentShowingJobs = jobs = RemoteOkUtils.getLiked();
}
return jobs;
}
private void paintJobs(List<RemoteOkJob> jobsToPaint, Integer page, Boolean isLikedPanel) {
JPanel panelToAppend = (isLikedPanel) ? likedListPanel : allListPanel;
panelToAppend.removeAll();
List<RemoteOkJob> likedJobs = RemoteOkUtils.getLiked();
JLabel loadingLabel = new JLabel("Loading...");
panelToAppend.add(loadingLabel, "growy");
panelToAppend.repaint();
panelToAppend.revalidate();
new Thread(() -> {
try {
Integer startIndex, endIndex;
startIndex = jobsPerPage * page;
endIndex = (startIndex + jobsPerPage < jobsToPaint.size()) ? startIndex + jobsPerPage : jobsToPaint.size();
for (int i = startIndex; i < endIndex; i++) {
RemoteOkJob job = jobsToPaint.get(i);
try {
JPanel remoteJobPanel = new WhitePanel(new MigLayout("fillx"));
JPanel jobPanel = new WhitePanel(new MigLayout("fillx"));
TitledBorder titledBorder = BorderFactory.createTitledBorder(job.getPosition());
titledBorder.setTitleFont(ComponentUtils.unboldFont(titledBorder.getTitleFont()));
titledBorder.setTitleColor(Color.BLUE);
remoteJobPanel.setBorder(titledBorder);
remoteJobPanel.setPreferredSize(new Dimension(100, 100));
remoteJobPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
final Border border = remoteJobPanel.getBorder();
if (border instanceof TitledBorder) {
final Rectangle bounds = getBorderTitleRect((TitledBorder) border, remoteJobPanel);
if (bounds.contains(e.getPoint())) {
try {
Desktop.getDesktop().browse(new URL(job.getUrl()).toURI());
} catch (IOException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
}
}
}
});
remoteJobPanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
final Border border = remoteJobPanel.getBorder();
if (border instanceof TitledBorder) {
final TitledBorder tb = (TitledBorder) border;
final Rectangle bounds = getBorderTitleRect(tb, remoteJobPanel);
if (bounds.contains(e.getPoint())) {
remoteJobPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
tb.setTitleFont(ComponentUtils.boldFont(tb.getTitleFont()));
} else {
remoteJobPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
tb.setTitleFont(ComponentUtils.unboldFont(tb.getTitleFont()));
}
remoteJobPanel.repaint();
remoteJobPanel.revalidate();
}
}
});
JPanel logoPanel = new WhitePanel(new MigLayout());
logoPanel.add(new JLabel(this.getLogo(job)));
jobPanel.add(logoPanel, "aligny top");
JPanel infoPanel = new WhitePanel(new MigLayout());
JLabel companyLabel = new JLabel(job.getCompany());
infoPanel.add(companyLabel, "wrap");
infoPanel.add(unboldLabel(formatDate(job.getDate())), "wrap");
infoPanel.add(unboldLabel(getDescription("<html>" + job.getDescription(), "<br>", 50)));
jobPanel.add(infoPanel, "wrap");
JPanel othersPanel = new WhitePanel(new MigLayout());
JPanel likePanel = new LikePanel(new MigLayout(), job, othersPanel);
Icon icon = getOpenHeartLabel();
JLabel likeLabel = new JLabel(icon);
if (isLikedPanel || RemoteOkUtils.isLiked(likedJobs, job)) {
likeLabel.setIcon(getClosedHeartLabel());
likeLabel.setName("closed");
if (isLikedPanel) {
likePanel.addMouseListener(new MouseAdapter() {
int i;
public MouseAdapter init(int i) {
this.i = i;
return this;
}
@Override
public void mouseReleased(MouseEvent ev) {
panelToAppend.remove(remoteJobPanel);
panelToAppend.repaint();
panelToAppend.revalidate();
JLabel registryQtyLabel = (JLabel) registryQtyPanel.getComponents()[0];
registryQtyLabel.setText(String.format("%s jobs encontrados", Integer.parseInt(registryQtyLabel.getText()
.substring(0, registryQtyLabel.getText().indexOf(' '))) - 1));
registryQtyPanel.repaint();
registryQtyPanel.revalidate();
}
}.init(i));
}
} else {
likeLabel.setName("open");
}
likePanel.add(likeLabel);
othersPanel.add(likePanel);
JPanel sharePanel = new SharePanel(job, this.email);
Icon shareIcon = getShareIcon();
JLabel shareLabel = new JLabel(shareIcon);
sharePanel.add(shareLabel);
othersPanel.add(sharePanel, "gapx 327");
remoteJobPanel.add(jobPanel, "wrap");
remoteJobPanel.add(othersPanel, "wrap");
panelToAppend.add(remoteJobPanel, "wrap");
} catch (Exception e) {
e.printStackTrace();
}
}
paintPageCountAndRegistryQty(page);
panelToAppend.remove(loadingLabel);
footerPanel.repaint();
footerPanel.revalidate();
panelToAppend.repaint();
panelToAppend.revalidate();
} catch (Exception ex) {
ex.printStackTrace();
}
}).start();
}
private Rectangle getBorderTitleRect(TitledBorder border, JPanel jobPanel) {
final TitledBorder tb = border;
final FontMetrics fm = jobPanel.getFontMetrics(jobPanel.getFont());
final int titleWidth = fm.stringWidth(tb.getTitle()) + 20;
return new Rectangle(0, 0, titleWidth, fm.getHeight());
}
private Icon getLogo(RemoteOkJob job) throws IOException, UnirestException {
if (nonNull(job.getLogoUrl())) {
BufferedImage image = ImageIO.read(Unirest.get(job.getLogoUrl()).asBinary().getBody());
if (nonNull(image)) {
image = ComponentUtils.resize(image, 45, 45);
return new ImageIcon(image);
}
}
Icon icon = new EmptyIcon(45, 45);
return icon;
}
}