/
Files.java
763 lines (650 loc) · 25 KB
/
Files.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
* Files.java
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
package org.rstudio.studio.client.workbench.views.files;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.Command;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.rstudio.core.client.StringUtil;
import org.rstudio.core.client.cellview.ColumnSortInfo;
import org.rstudio.core.client.command.CommandBinder;
import org.rstudio.core.client.command.Handler;
import org.rstudio.core.client.files.FileSystemItem;
import org.rstudio.core.client.js.JsObject;
import org.rstudio.core.client.widget.*;
import org.rstudio.studio.client.application.events.EventBus;
import org.rstudio.studio.client.common.ConsoleDispatcher;
import org.rstudio.studio.client.common.GlobalDisplay;
import org.rstudio.studio.client.common.fileexport.FileExport;
import org.rstudio.studio.client.common.filetypes.FileTypeRegistry;
import org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserEvent;
import org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserHandler;
import org.rstudio.studio.client.server.*;
import org.rstudio.studio.client.workbench.WorkbenchContext;
import org.rstudio.studio.client.workbench.WorkbenchView;
import org.rstudio.studio.client.workbench.commands.Commands;
import org.rstudio.studio.client.workbench.model.ClientInitState;
import org.rstudio.studio.client.workbench.model.ClientState;
import org.rstudio.studio.client.workbench.model.RemoteFileSystemContext;
import org.rstudio.studio.client.workbench.model.Session;
import org.rstudio.studio.client.workbench.model.SessionInfo;
import org.rstudio.studio.client.workbench.model.helper.JSObjectStateValue;
import org.rstudio.studio.client.workbench.model.helper.StringStateValue;
import org.rstudio.studio.client.workbench.views.BasePresenter;
import org.rstudio.studio.client.workbench.views.environment.dataimport.DataImportPresenter;
import org.rstudio.studio.client.workbench.views.files.events.*;
import org.rstudio.studio.client.workbench.views.files.model.DirectoryListing;
import org.rstudio.studio.client.workbench.views.files.model.FileChange;
import org.rstudio.studio.client.workbench.views.files.model.FilesServerOperations;
import org.rstudio.studio.client.workbench.views.files.model.PendingFileUpload;
import org.rstudio.studio.client.workbench.views.source.events.SourcePathChangedEvent;
import java.util.ArrayList;
public class Files
extends BasePresenter
implements FileChangeHandler,
OpenFileInBrowserHandler,
DirectoryNavigateHandler
{
interface Binder extends CommandBinder<Commands, Files> {}
public interface Display extends WorkbenchView
{
public interface NavigationObserver
{
void onFileNavigation(FileSystemItem file);
void onSelectAllValueChanged(boolean value);
}
public interface Observer extends NavigationObserver
{
void onFileSelectionChanged();
void onColumnSortOrderChanaged(JsArray<ColumnSortInfo> sortOrder);
}
void setObserver(Observer observer);
void resetColumnWidths();
void setColumnSortOrder(JsArray<ColumnSortInfo> sortOrder);
void listDirectory(FileSystemItem directory,
ServerDataSource<DirectoryListing> filesDS);
void updateDirectoryListing(FileChange action);
void renameFile(FileSystemItem from, FileSystemItem to);
void selectAll();
void selectNone();
ArrayList<FileSystemItem> getSelectedFiles();
void showFolderPicker(
String title,
RemoteFileSystemContext context,
FileSystemItem initialDir,
ProgressOperationWithInput<FileSystemItem> operation);
void showFilePicker(
String title,
RemoteFileSystemContext context,
FileSystemItem initialFile,
ProgressOperationWithInput<FileSystemItem> operation);
void showFileUpload(
String targetURL,
FileSystemItem targetDirectory,
RemoteFileSystemContext fileSystemContext,
OperationWithInput<PendingFileUpload> completedOperation);
void showHtmlFileChoice(FileSystemItem file,
Command onEdit,
Command onBrowse);
void showDataImportFileChoice(FileSystemItem file,
Command onView,
Command onImport);
}
@Inject
public Files(Display view,
EventBus eventBus,
FilesServerOperations server,
RemoteFileSystemContext fileSystemContext,
GlobalDisplay globalDisplay,
Session session,
Commands commands,
Provider<FilesCopy> pFilesCopy,
Provider<FilesUpload> pFilesUpload,
Provider<FileExport> pFileExport,
FileTypeRegistry fileTypeRegistry,
ConsoleDispatcher consoleDispatcher,
WorkbenchContext workbenchContext,
DataImportPresenter dataImportPresenter)
{
super(view);
view_ = view ;
view_.setObserver(new DisplayObserver());
fileTypeRegistry_ = fileTypeRegistry;
consoleDispatcher_ = consoleDispatcher;
workbenchContext_ = workbenchContext;
eventBus_ = eventBus;
server_ = server;
fileSystemContext_ = fileSystemContext;
globalDisplay_ = globalDisplay ;
session_ = session;
pFilesCopy_ = pFilesCopy;
pFilesUpload_ = pFilesUpload;
pFileExport_ = pFileExport;
dataImportPresenter_ = dataImportPresenter;
((Binder)GWT.create(Binder.class)).bind(commands, this);
eventBus_.addHandler(FileChangeEvent.TYPE, this);
initSession();
}
private void initSession()
{
final SessionInfo sessionInfo = session_.getSessionInfo();
ClientInitState state = sessionInfo.getClientState();
// make the column sort order persistent
new JSObjectStateValue(MODULE_FILES, KEY_SORT_ORDER, ClientState.PROJECT_PERSISTENT, state, false)
{
@Override
protected void onInit(JsObject value)
{
if (value != null)
columnSortOrder_ = value.cast();
else
columnSortOrder_ = null;
lastKnownState_ = columnSortOrder_;
view_.setColumnSortOrder(columnSortOrder_);
}
@Override
protected JsObject getValue()
{
if (columnSortOrder_ != null)
return columnSortOrder_.cast();
else
return null;
}
@Override
protected boolean hasChanged()
{
if (lastKnownState_ != columnSortOrder_)
{
lastKnownState_ = columnSortOrder_;
return true;
}
else
{
return false;
}
}
private JsArray<ColumnSortInfo> lastKnownState_ = null;
};
// navigate to previous directory (works for resumed case)
new StringStateValue(MODULE_FILES, KEY_PATH, ClientState.PROJECT_PERSISTENT, state) {
@Override
protected void onInit(final String value)
{
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
public void execute()
{
// if we've already navigated to a directory then
// we don't need to initialize from client state
if (hasNavigatedToDirectory_)
return;
// compute start dir
String path = transformPathStateValue(value);
FileSystemItem start = path != null
? FileSystemItem.createDir(path)
: FileSystemItem.createDir(
sessionInfo.getInitialWorkingDir());
navigateToDirectory(start);
}
});
}
@Override
protected String getValue()
{
return currentPath_.getPath();
}
private String transformPathStateValue(String value)
{
// if the value is null then return null
if (value == null)
return null;
// only respect the value for projects
String projectFile = session_.getSessionInfo().getActiveProjectFile();
if (projectFile == null)
return null;
// ensure that the value is within the project dir (it wouldn't
// be if the project directory has been moved or renamed)
String projectDirPath =
FileSystemItem.createFile(projectFile).getParentPathString();
if (value.startsWith(projectDirPath))
return value;
else
return null;
}
};
}
public Display getDisplay()
{
return view_ ;
}
// observer for display
public class DisplayObserver implements Display.Observer {
public void onFileSelectionChanged()
{
}
public void onFileNavigation(FileSystemItem file)
{
if (file.isDirectory())
{
navigateToDirectory(file);
}
else
{
navigateToFile(file);
}
}
public void onSelectAllValueChanged(boolean value)
{
if (value)
view_.selectAll();
else
view_.selectNone();
}
public void onColumnSortOrderChanaged(
JsArray<ColumnSortInfo> sortOrder)
{
columnSortOrder_ = sortOrder;
}
};
@Handler
void onRefreshFiles()
{
view_.resetColumnWidths();
view_.listDirectory(currentPath_, currentPathFilesDS_);
}
@Handler
void onNewFolder()
{
globalDisplay_.promptForText(
"New Folder",
"Please enter the new folder name",
null,
new ProgressOperationWithInput<String>()
{
public void execute(String input,
final ProgressIndicator progress)
{
progress.onProgress("Creating folder...");
String folderPath = currentPath_.completePath(input);
FileSystemItem folder = FileSystemItem.createDir(folderPath);
server_.createFolder(
folder,
new VoidServerRequestCallback(progress));
}
});
}
void onUploadFile()
{
pFilesUpload_.get().execute(currentPath_, fileSystemContext_);
}
@Handler
void onCopyFile()
{
// copy selected files then clear the selection
pFilesCopy_.get().execute(view_.getSelectedFiles(),
currentPath_,
new Command() {
public void execute()
{
view_.selectNone();
}});
}
@Handler
void onCopyFileTo()
{
final ArrayList<FileSystemItem> selectedFiles = view_.getSelectedFiles();
// validate selection size
if (selectedFiles.size() == 0)
return;
if (selectedFiles.size() > 1)
{
globalDisplay_.showErrorMessage(
"Multiple Items Selected",
"Please select a single file or folder to copy");
return;
}
view_.showFilePicker(
"Choose Destination",
fileSystemContext_,
selectedFiles.get(0),
new ProgressOperationWithInput<FileSystemItem>() {
public void execute(FileSystemItem targetFile,
final ProgressIndicator progress)
{
if (targetFile == null)
return;
if (StringUtil.isNullOrEmpty(targetFile.getExtension()))
{
targetFile = FileSystemItem.createFile(
targetFile.getPath() + selectedFiles.get(0).getExtension());
}
server_.copyFile(selectedFiles.get(0),
targetFile,
true,
new VoidServerRequestCallback(progress) {
@Override
protected void onSuccess()
{
view_.selectNone();
}
});
}
});
}
@Handler
void onMoveFiles()
{
// get currently selected files
final ArrayList<FileSystemItem> selectedFiles = view_.getSelectedFiles();
// validation: some selection exists
if (selectedFiles.size() == 0)
return ;
// validation -- not prohibited move of public folder
if (!validateNotRestrictedFolder(selectedFiles, "moved"))
return ;
view_.showFolderPicker(
"Choose Folder",
fileSystemContext_,
currentPath_,
new ProgressOperationWithInput<FileSystemItem>() {
public void execute(final FileSystemItem targetDir,
final ProgressIndicator progress)
{
if (targetDir == null)
return;
// check to make sure that we aren't moving any folders
// onto themselves or moving any files into the directory
// where they currently reside
for (int i=0; i<selectedFiles.size(); i++)
{
FileSystemItem file = selectedFiles.get(i);
FileSystemItem fileParent = file.getParentPath();
if (file.getPath() == targetDir.getPath() ||
fileParent.getPath() == targetDir.getPath())
{
progress.onError("Invalid target folder");
return ;
}
}
progress.onProgress("Moving files...");
view_.selectNone();
server_.moveFiles(selectedFiles,
targetDir,
new VoidServerRequestCallback(progress));
}
});
}
@Handler
void onExportFiles()
{
pFileExport_.get().export("Export Files",
"selected file(s)",
currentPath_,
view_.getSelectedFiles());
}
@Handler
void onRenameFile()
{
// get currently selected files
ArrayList<FileSystemItem> selectedFiles = view_.getSelectedFiles();
// validation: some selection exists
if (selectedFiles.size() == 0)
return ;
// validation: no more than one file selected
if (selectedFiles.size() > 1)
{
globalDisplay_.showErrorMessage(
"Invalid Selection",
"Please select only one file to rename");
return ;
}
// validation -- not prohibited move of public folder
if (!validateNotRestrictedFolder(selectedFiles, "renamed"))
return ;
// prompt for new file name then execute the rename
final FileSystemItem file = selectedFiles.get(0);
globalDisplay_.promptForText("Rename File",
"Please enter the new file name:",
file.getName(),
0,
file.getStem().length(),
null,
new ProgressOperationWithInput<String>() {
public void execute(String input,
final ProgressIndicator progress)
{
progress.onProgress("Renaming file...");
String path = file.getParentPath().completePath(input);
final FileSystemItem target =
file.isDirectory() ?
FileSystemItem.createDir(path) :
FileSystemItem.createFile(path);
// clear selection
view_.selectNone();
// premptively rename in the UI then fallback to refreshing
// the view if there is an error
view_.renameFile(file, target);
// execute on the server
server_.renameFile(file,
target,
new VoidServerRequestCallback(progress) {
@Override
protected void onSuccess()
{
// if we were successful, let editor know
if (!file.isDirectory())
{
eventBus_.fireEvent(
new SourcePathChangedEvent(
file.getPath(),
target.getPath()));
}
}
@Override
protected void onFailure()
{
onRefreshFiles();
}
});
}
});
}
@Handler
void onDeleteFiles()
{
// get currently selected files
final ArrayList<FileSystemItem> selectedFiles = view_.getSelectedFiles();
// validation: some selection exists
if (selectedFiles.size() == 0)
return ;
// validation -- not prohibited move of public folder
if (!validateNotRestrictedFolder(selectedFiles, "deleted"))
return ;
// confirm delete then execute it
globalDisplay_.showYesNoMessage(
GlobalDisplay.MSG_QUESTION,
"Confirm Delete",
"Are you sure you want to delete the selected files?",
new ProgressOperation() {
public void execute(final ProgressIndicator progress)
{
progress.onProgress("Deleting files...");
view_.selectNone();
server_.deleteFiles(
selectedFiles,
new VoidServerRequestCallback(progress));
}
},
true);
}
private boolean validateNotRestrictedFolder(ArrayList<FileSystemItem> files,
String verb)
{
if (!session_.getSessionInfo().getAllowRemovePublicFolder())
{
for (FileSystemItem file : files)
{
if (file.isPublicFolder())
{
globalDisplay_.showErrorMessage(
"Error",
"The Public folder cannot be " + verb + ".");
return false;
}
}
}
return true;
}
void onGoToWorkingDir()
{
view_.bringToFront();
navigateToDirectory(workbenchContext_.getCurrentWorkingDir());
}
@Handler
void onSetAsWorkingDir()
{
consoleDispatcher_.executeSetWd(currentPath_, true);
}
void onSetWorkingDirToFilesPane()
{
onSetAsWorkingDir();
}
@Handler
void onShowFolder()
{
eventBus_.fireEvent(new ShowFolderEvent(currentPath_));
}
public void onFileChange(FileChangeEvent event)
{
view_.updateDirectoryListing(event.getFileChange());
}
public void onOpenFileInBrowser(OpenFileInBrowserEvent event)
{
showFileInBrowser(event.getFile());
}
public void onDirectoryNavigate(DirectoryNavigateEvent event)
{
navigateToDirectory(event.getDirectory());
if (event.getActivate())
view_.bringToFront();
}
private void navigateToDirectory(FileSystemItem directoryEntry)
{
hasNavigatedToDirectory_ = true;
currentPath_ = directoryEntry;
view_.listDirectory(currentPath_, currentPathFilesDS_);
session_.persistClientState();
}
private void navigateToFile(final FileSystemItem file)
{
final String ext = file.getExtension().toLowerCase();
if (ext.equals(".htm") || ext.equals(".html") || ext.equals(".nb.html"))
{
view_.showHtmlFileChoice(
file,
new Command() {
@Override
public void execute()
{
fileTypeRegistry_.openFile(file);
}
},
new Command() {
@Override
public void execute()
{
showFileInBrowser(file);
}
});
}
else if (ext.equals(".csv") ||
ext.equals(".xls") || ext.equals(".xlsx") ||
ext.equals(".sav") || ext.equals(".dta") || ext.equals("por") ||
ext.equals(".sas") || ext.equals(".stata"))
{
view_.showDataImportFileChoice(
file,
new Command() {
@Override
public void execute()
{
fileTypeRegistry_.openFile(file);
}
},
new Command() {
@Override
public void execute()
{
if (ext.equals(".csv")) {
dataImportPresenter_.openImportDatasetFromCSV(file.getPath());
}
else if (ext.equals(".xls") || ext.equals(".xlsx")) {
dataImportPresenter_.openImportDatasetFromXLS(file.getPath());
}
else {
dataImportPresenter_.openImportDatasetFromSAV(file.getPath());
}
}
});
}
else
{
fileTypeRegistry_.openFile(file);
}
}
private void showFileInBrowser(FileSystemItem file)
{
// show the file in a new window if we can get a file url for it
String fileURL = server_.getFileUrl(file);
if (fileURL != null)
{
globalDisplay_.openWindow(fileURL);
}
}
// data source for listing files on the current path which can
// be passed to the files view
ServerDataSource<DirectoryListing> currentPathFilesDS_ =
new ServerDataSource<DirectoryListing>()
{
public void requestData(
ServerRequestCallback<DirectoryListing> requestCallback)
{
// pass true to enable monitoring for all calls to list_files
server_.listFiles(currentPath_, true, requestCallback);
}
};
private final Display view_ ;
private final FileTypeRegistry fileTypeRegistry_;
private final ConsoleDispatcher consoleDispatcher_;
private final WorkbenchContext workbenchContext_;
private final FilesServerOperations server_;
private final EventBus eventBus_;
private final GlobalDisplay globalDisplay_ ;
private final RemoteFileSystemContext fileSystemContext_;
private final Session session_;
private FileSystemItem currentPath_ = FileSystemItem.home();
private boolean hasNavigatedToDirectory_ = false;
private final Provider<FilesCopy> pFilesCopy_;
private final Provider<FilesUpload> pFilesUpload_;
private final Provider<FileExport> pFileExport_;
private static final String MODULE_FILES = "files-pane";
private static final String KEY_PATH = "path";
private static final String KEY_SORT_ORDER = "sortOrder";
private JsArray<ColumnSortInfo> columnSortOrder_ = null;
private DataImportPresenter dataImportPresenter_;
}