1
1
/*
2
- * Copyright (c) 2020 Tony Ho. Some rights reserved.
2
+ * Copyright (c) 2020 Chuntung Ho. Some rights reserved.
3
3
*/
4
4
5
5
package com .chuntung .plugin .gistsnippet .dto ;
6
6
7
- import com .chuntung .plugin .gistsnippet .dto .api .GistDTO ;
8
- import com .chuntung .plugin .gistsnippet .dto .api .GistFileDTO ;
9
- import com .chuntung .plugin .gistsnippet .dto .api .GistOwnerDTO ;
10
7
import com .intellij .ide .projectView .PresentationData ;
11
8
import com .intellij .openapi .util .IconLoader ;
12
9
import com .intellij .ui .SimpleTextAttributes ;
13
10
import com .intellij .ui .treeStructure .SimpleNode ;
14
11
import org .jetbrains .annotations .NotNull ;
12
+ import org .kohsuke .github .GHGist ;
13
+ import org .kohsuke .github .GHGistFile ;
14
+ import org .kohsuke .github .GHUser ;
15
15
16
16
import javax .swing .*;
17
+ import java .io .IOException ;
17
18
import java .net .MalformedURLException ;
18
19
import java .net .URL ;
19
20
import java .util .*;
@@ -37,9 +38,9 @@ public class SnippetNodeDTO extends SimpleNode {
37
38
private String htmlUrl ;
38
39
private String description ;
39
40
private Integer filesCount ;
40
- private List <GistFileDTO > files ;
41
+ private List <FileNodeDTO > files ;
41
42
private List <String > tags ;
42
- private GistOwnerDTO owner ;
43
+ private GHUser owner ;
43
44
private boolean isPublic ;
44
45
private String createdAt ;
45
46
private String updatedAt ;
@@ -100,11 +101,11 @@ public void setFilesCount(Integer filesCount) {
100
101
this .filesCount = filesCount ;
101
102
}
102
103
103
- public List <GistFileDTO > getFiles () {
104
+ public List <FileNodeDTO > getFiles () {
104
105
return files ;
105
106
}
106
107
107
- public void setFiles (List <GistFileDTO > files ) {
108
+ public void setFiles (List <FileNodeDTO > files ) {
108
109
this .files = files ;
109
110
}
110
111
@@ -116,11 +117,11 @@ public void setTags(List<String> tags) {
116
117
this .tags = tags ;
117
118
}
118
119
119
- public GistOwnerDTO getOwner () {
120
+ public GHUser getOwner () {
120
121
return owner ;
121
122
}
122
123
123
- public void setOwner (GistOwnerDTO owner ) {
124
+ public void setOwner (GHUser owner ) {
124
125
this .owner = owner ;
125
126
}
126
127
@@ -207,30 +208,38 @@ private void render(PresentationData presentation) {
207
208
presentation .setTooltip (tooltip );
208
209
}
209
210
210
- static SnippetNodeDTO of (GistDTO dto , ScopeEnum scope ) {
211
+ static SnippetNodeDTO of (GHGist dto , ScopeEnum scope ) {
211
212
SnippetNodeDTO node = new SnippetNodeDTO ();
212
213
node .setScope (scope );
213
- node .setId (dto .getId ());
214
- node .setHtmlUrl (dto .getHtmlUrl ());
215
- node .setCreatedAt (dto .getCreatedAt ());
216
- node .setUpdatedAt (dto .getUpdatedAt ());
217
- node .setPublic (dto .getPublic ());
218
- node .setOwner (dto .getOwner ());
214
+ node .setId (dto .getGistId ());
215
+ node .setHtmlUrl (dto .getHtmlUrl ().toString ());
216
+ try {
217
+ node .setCreatedAt (dto .getCreatedAt ().toString ());
218
+ node .setUpdatedAt (dto .getUpdatedAt ().toString ());
219
+ node .setOwner (dto .getOwner ());
220
+ } catch (IOException e ) {
221
+ // NOOP
222
+ }
223
+ node .setPublic (dto .isPublic ());
219
224
node .setFilesCount (dto .getFiles ().size ());
220
- node .setFiles (new ArrayList <>(dto .getFiles ().values ()));
225
+ List <FileNodeDTO > files = new ArrayList <>();
226
+ for (GHGistFile gistFile : dto .getFiles ().values ()) {
227
+ files .add (new FileNodeDTO (gistFile ));
228
+ }
229
+ node .setFiles (files );
221
230
222
231
parseDescription (dto , node );
223
232
224
233
return node ;
225
234
}
226
235
227
- private static void parseDescription (GistDTO dto , SnippetNodeDTO node ) {
236
+ private static void parseDescription (GHGist dto , SnippetNodeDTO node ) {
228
237
node .setTitle (null );
229
238
node .setTags (null );
230
239
if (dto .getDescription () == null || dto .getDescription ().isEmpty ()) {
231
240
// set description as first file name if empty
232
- for (GistFileDTO fileDTO : dto .getFiles ().values ()) {
233
- node .setDescription (fileDTO .getFilename ());
241
+ for (GHGistFile fileDTO : dto .getFiles ().values ()) {
242
+ node .setDescription (fileDTO .getFileName ());
234
243
break ;
235
244
}
236
245
} else {
@@ -257,25 +266,28 @@ private static void parseDescription(GistDTO dto, SnippetNodeDTO node) {
257
266
}
258
267
}
259
268
260
- public boolean update (GistDTO dto ) {
269
+ public boolean update (GHGist dto ) {
261
270
boolean updated = false ;
262
- if (! Objects . equals ( createdAt , dto . getCreatedAt ()) || ! Objects . equals ( updatedAt , dto . getUpdatedAt ())) {
263
- parseDescription ( dto , this );
264
-
265
- setPublic (dto .getPublic ());
266
- setCreatedAt (dto .getCreatedAt ());
267
- setUpdatedAt (dto .getUpdatedAt ());
271
+ try {
272
+ if (! Objects . equals ( createdAt , dto . getCreatedAt ()) || ! Objects . equals ( updatedAt , dto . getUpdatedAt ())) {
273
+ parseDescription ( dto , this );
274
+ setPublic (dto .isPublic ());
275
+ setCreatedAt (dto .getCreatedAt (). toString ());
276
+ setUpdatedAt (dto .getUpdatedAt (). toString ());
268
277
269
- updated = true ;
278
+ updated = true ;
279
+ }
280
+ } catch (IOException e ) {
281
+ throw new RuntimeException (e );
270
282
}
271
283
272
284
// merge files
273
285
setFilesCount (dto .getFiles ().size ());
274
286
Set <String > children = new HashSet <>();
275
287
// traverse tree structure to remove non-existing items
276
- Iterator <GistFileDTO > iterator = getFiles ().iterator ();
288
+ Iterator <FileNodeDTO > iterator = getFiles ().iterator ();
277
289
while (iterator .hasNext ()) {
278
- GistFileDTO fileDTO = iterator .next ();
290
+ FileNodeDTO fileDTO = iterator .next ();
279
291
if (dto .getFiles ().containsKey (fileDTO .getFilename ())) {
280
292
fileDTO .setContent (dto .getFiles ().get (fileDTO .getFilename ()).getContent ());
281
293
children .add (fileDTO .getFilename ());
@@ -286,10 +298,10 @@ public boolean update(GistDTO dto) {
286
298
}
287
299
288
300
// traverse latest files to add missing items if gist changed
289
- for (GistFileDTO fileDTO : dto .getFiles ().values ()) {
290
- if (!children .contains (fileDTO . getFilename ())) {
301
+ for (GHGistFile gistFile : dto .getFiles ().values ()) {
302
+ if (!children .contains (gistFile . getFileName ())) {
291
303
updated = true ;
292
- getFiles ().add (fileDTO );
304
+ getFiles ().add (new FileNodeDTO ( gistFile ) );
293
305
}
294
306
}
295
307
0 commit comments