Repository navigation
Expand file tree
/
Copy pathFolderPanel.java
More file actions
564 lines (471 loc) · 20.2 KB
/
Copy pathFolderPanel.java
File metadata and controls
564 lines (471 loc) · 20.2 KB
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
/*
* This file is part of muCommander, http://www.mucommander.com
*
* muCommander is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* muCommander is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mucommander.ui.main;
import java.awt.AWTKeyStroke;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mucommander.auth.CredentialsMapping;
import com.mucommander.commons.file.AbstractFile;
import com.mucommander.commons.file.FileFactory;
import com.mucommander.commons.file.FileURL;
import com.mucommander.commons.file.MonitoredFile;
import com.mucommander.core.ChangeFolderThread;
import com.mucommander.core.LocalLocationHistory;
import com.mucommander.core.LocationChanger;
import com.mucommander.desktop.ActionType;
import com.mucommander.preload.PreloadedJFrame;
import com.mucommander.ui.action.ActionKeymap;
import com.mucommander.ui.action.ActionManager;
import com.mucommander.ui.action.impl.FocusNextAction;
import com.mucommander.ui.action.impl.FocusPreviousAction;
import com.mucommander.ui.dnd.FileDragSourceListener;
import com.mucommander.ui.dnd.FileDropTransferHandler;
import com.mucommander.ui.event.LocationManager;
import com.mucommander.ui.main.quicklist.BookmarksQL;
import com.mucommander.ui.main.quicklist.ParentFoldersQL;
import com.mucommander.ui.main.quicklist.RecentExecutedFilesQL;
import com.mucommander.ui.main.quicklist.RecentLocationsQL;
import com.mucommander.ui.main.quicklist.RootFoldersQL;
import com.mucommander.ui.main.quicklist.TabsQL;
import com.mucommander.ui.main.table.FileTable;
import com.mucommander.ui.main.table.FileTableConfiguration;
import com.mucommander.ui.main.tabs.ConfFileTableTab;
import com.mucommander.ui.main.tabs.FileTableTab;
import com.mucommander.ui.main.tabs.FileTableTabs;
import com.mucommander.ui.main.tree.FoldersTreePanel;
import com.mucommander.ui.quicklist.QuickList;
import com.mucommander.ui.quicklist.QuickListContainer;
import com.mucommander.ui.tabs.ActiveTabListener;
/**
* Folder pane that contains the table that displays the contents of the current directory and allows navigation, the
* drive button, and the location field.
*
* @author Maxence Bernard, Arik Hadas
*/
public class FolderPanel implements FocusListener, QuickListContainer, ActiveTabListener {
private static final Logger LOGGER = LoggerFactory.getLogger(FolderPanel.class);
/** The following constants are used to identify the left and right folder panels */
public enum FolderPanelType { LEFT, RIGHT }
private MainFrame mainFrame;
private final JPanel panel;
private LocationManager locationManager = new LocationManager(this);
/* We're NOT using JComboBox anymore because of its strange behavior:
it calls actionPerformed() each time an item is highlighted with the arrow (UP/DOWN) keys,
so there is no way to tell if it's the final selection (ENTER) or not.
*/
private DrivePopupButton driveButton;
private LocationTextField locationTextField;
private FileTable fileTable;
private FileTableTabs tabs;
private FoldersTreePanel foldersTreePanel;
private JSplitPane treeSplitPane;
private FileDragSourceListener fileDragSourceListener;
private LocationChanger locationChanger;
/** Is directory tree visible */
private boolean treeVisible = false;
/** Saved width of a directory tree (when it's not visible) */
private int oldTreeWidth = 150;
/** Array of all the existing pop ups for this panel's FileTable **/
private Future<QuickList[]> fileTablePopups;
/* TODO branch private boolean branchView; */
/**
* Constructor
*
* @param mainFrame - the MainFrame that contains this panel
* @param initialTabs - the initial folders displayed at this panel's tabs
* @param conf - configuration for this panel's file table
*/
FolderPanel(MainFrame mainFrame, ConfFileTableTab[] initialTabs, int indexOfSelectedTab, FileTableConfiguration conf) {
panel = PreloadedJFrame.getJPanel(new BorderLayout());
LOGGER.trace(" initialTabs:");
for (FileTableTab tab:initialTabs)
LOGGER.trace("\t"+(tab.getLocation() != null ? tab.getLocation().toString() : null));
this.mainFrame = mainFrame;
// No decoration for this panel
panel.setBorder(null);
JPanel locationPanel = PreloadedJFrame.getJPanel(new GridBagLayout());
panel.add(locationPanel, BorderLayout.NORTH);
new Thread(() -> {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
// Create and add drive button
this.driveButton = new DrivePopupButton(this);
c.weightx = 0;
c.gridx = 0;
locationPanel.add(driveButton, c);
// Create location text field and wrap it in a LocationBar that can
// alternate between the text field and a breadcrumb view (Ctrl key).
this.locationTextField = new LocationTextField(this);
LocationBar locationBar = new LocationBar(this, locationTextField);
// Give location field all the remaining space until the PoupupsButton
c.weightx = 1;
c.gridx = 1;
// Add some space between drive button and location combo box (none by default)
c.insets = new Insets(0, 4, 0, 0);
locationPanel.add(locationBar, c);
disableCtrlFocusTraversalKeys(locationTextField);
registerCycleThruFolderPanelAction(locationTextField);
// Allow the location field to change the current directory when a file/folder is dropped on it.
// Preserve the field's original (LAF-installed) TransferHandler as a fallback so that
// keyboard-triggered clipboard cut/copy/paste - which also goes through TransferHandler -
// keeps working; otherwise replacing it outright would silently break text paste into the
// location bar.
locationTextField.setTransferHandler(
new FileDropTransferHandler(this, true, locationTextField.getTransferHandler()));
driveButton.setTransferHandler(new FileDropTransferHandler(this, true));
locationTextField.addFocusListener(this);
}).start();
// Initialize quick lists in background
fileTablePopups = CompletableFuture.supplyAsync(() -> {
return new QuickList[] {
new ParentFoldersQL(this),
new RecentLocationsQL(this),
new RecentExecutedFilesQL(this),
new BookmarksQL(this),
new RootFoldersQL(this),
new TabsQL(this)
};
});
// Create the FileTable
fileTable = new FileTable(mainFrame, this, conf);
locationChanger = new LocationChanger(mainFrame, this, locationManager);
// Create the Tabs (Must be called after the fileTable was created and current folder was set)
tabs = new FileTableTabs(mainFrame, this, initialTabs);
// Select the tab that was previously selected on last run
tabs.selectTab(indexOfSelectedTab);
// Select the file-to-select in this tab, if set (note that we already validated
// that the file exists in ConfigFileTableTab#getInitialAbstractPaths)
AbstractFile fileToSelect = initialTabs[indexOfSelectedTab].getFileToSelect();
if (fileToSelect != null)
fileTable.selectFile(fileToSelect);
tabs.addActiveTabListener(this);
// create folders tree on a JSplitPane
foldersTreePanel = new FoldersTreePanel(this);
foldersTreePanel.setVisible(false);
treeSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, foldersTreePanel.getPanel(), tabs);
treeSplitPane.setDividerSize(0);
treeSplitPane.setDividerLocation(0);
// Remove default border
treeSplitPane.setBorder(null);
panel.add(treeSplitPane, BorderLayout.CENTER);
// Disable Ctrl+Tab and Shift+Ctrl+Tab focus traversal keys
disableCtrlFocusTraversalKeys(foldersTreePanel.getTree());
disableCtrlFocusTraversalKeys(fileTable);
disableCtrlFocusTraversalKeys(tabs);
registerCycleThruFolderPanelAction(foldersTreePanel.getTree());
// No need to register cycle actions for FileTable, they already are
// Listen to focus event in order to notify MainFrame of changes of the current active panel/table
fileTable.addFocusListener(this);
tabs.addFocusListener(this);
// Drag and Drop support
// Enable drag support on the FileTable
this.fileDragSourceListener = new FileDragSourceListener(this);
fileDragSourceListener.enableDrag(fileTable);
}
/**
* Removes the Control+Tab and Shift+Control+Tab focus traversal keys from the given component so that those
* shortcuts can be used for other purposes.
*
* @param component the component for which to remove the Control+Tab and Shift+Control+Tab focus traversal keys
*/
private void disableCtrlFocusTraversalKeys(Component component) {
// Remove Ctrl+Tab from forward focus traversal keys
HashSet<AWTKeyStroke> keyStrokeSet = new HashSet<AWTKeyStroke>();
keyStrokeSet.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0));
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keyStrokeSet);
// Remove Shift+Ctrl+Tab from backward focus traversal keys
keyStrokeSet = new HashSet<AWTKeyStroke>();
keyStrokeSet.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, java.awt.event.InputEvent.SHIFT_DOWN_MASK));
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keyStrokeSet);
}
/**
* Registers the {@link FocusNextAction} and {@link FocusPreviousAction} actions onto the given component's
* input map.
*
* @param component the component for which to register the cycle actions
*/
private void registerCycleThruFolderPanelAction(JComponent component) {
ActionKeymap.registerActionAccelerators(
ActionManager.getActionInstance(ActionType.FocusNext, mainFrame),
component,
JComponent.WHEN_FOCUSED);
ActionKeymap.registerActionAccelerators(
ActionManager.getActionInstance(ActionType.FocusPrevious, mainFrame),
component,
JComponent.WHEN_FOCUSED);
}
public FileDragSourceListener getFileDragSourceListener() {
return this.fileDragSourceListener;
}
/**
* Returns the MainFrame that contains this panel.
*
* @return the MainFrame that contains this panel
*/
public MainFrame getMainFrame() {
return this.mainFrame;
}
/**
* Returns the FileTable contained by this panel.
*
* @return the FileTable contained by this panel
*/
public FileTable getFileTable() {
return this.fileTable;
}
/**
* Returns the FileTable tabs contained by this panel.
*
* @return the FileTable tabs contained by this panel
*/
public FileTableTabs getTabs() {
return this.tabs;
}
/**
* Returns the LocationTextField contained by this panel.
*
* @return the LocationTextField contained by this panel
*/
public LocationTextField getLocationTextField() {
return locationTextField;
}
/**
* Returns the DrivePopupButton contained by this panel.
*
* @return the DrivePopupButton contained by this panel
*/
public DrivePopupButton getDriveButton() {
return driveButton;
}
/**
* Returns the visited folders history, wrapped in a FolderHistory object.
*
* @return the visited folders history, wrapped in a FolderHistory object
*/
public LocalLocationHistory getFolderHistory() {
return getTabs().getCurrentTab().getLocationHistory();
}
/**
* Returns the LocationManager instance that notifies registered listeners of location changes
* that occur in this FolderPanel.
*
* @return the LocationManager instance that notifies registered listeners of location changes that occur in
* this FolderPanel
*/
public LocationManager getLocationManager() {
return locationManager;
}
public void setProgressValue(int value) {
locationTextField.setProgressValue(value);
}
public void tryChangeCurrentFolderInternal(FileTableTab tab, Runnable runnable) {
locationChanger.tryChangeCurrentFolderInternal(tab, runnable);
}
public ChangeFolderThread tryChangeCurrentFolder(AbstractFile folder) {
return locationChanger.tryChangeCurrentFolder(folder, false);
}
public ChangeFolderThread tryChangeCurrentFolder(FileURL folderURL, AbstractFile selectThisFileAfter, boolean findWorkableFolder) {
return locationChanger.tryChangeCurrentFolder(true, FileFactory.getFile(folderURL), selectThisFileAfter, findWorkableFolder, false);
}
public ChangeFolderThread tryChangeCurrentFolder(AbstractFile folder, AbstractFile selectThisFileAfter, boolean findWorkableFolder) {
return locationChanger.tryChangeCurrentFolder(true, folder, selectThisFileAfter, findWorkableFolder, false);
}
public ChangeFolderThread tryChangeCurrentFolder(String folderPath) {
return locationChanger.tryChangeCurrentFolder(folderPath);
}
public ChangeFolderThread tryChangeCurrentFolder(FileURL folderURL) {
return locationChanger.tryChangeCurrentFolder(folderURL);
}
public ChangeFolderThread tryChangeCurrentFolder(FileURL folderURL, CredentialsMapping credentialsMapping) {
return locationChanger.tryChangeCurrentFolder(folderURL, credentialsMapping, false);
}
public void tryRefreshCurrentFolder() {
tryRefreshCurrentFolder(true);
}
public void tryRefreshCurrentFolder(boolean internal) {
locationChanger.tryRefreshCurrentFolder(internal);
}
public void tryRefreshCurrentFolder(AbstractFile selectThisFileAfter) {
locationChanger.tryRefreshCurrentFolder(selectThisFileAfter, true);
}
public boolean tryKillChangeFolderThread() {
return locationChanger.tryKillChangeFolderThread();
}
public long getLastFolderChangeTime() {
return locationChanger.getLastFolderChangeTime();
}
/**
* Returns a {@link MonitoredFile} for the folder that is currently being displayed by this panel.
*
* @return a {@link MonitoredFile} for the folder that is currently being displayed by this panel
*/
public MonitoredFile getCurrentFolder() {
return locationManager.getCurrentFolder();
}
/**
* This method updates the UI with the given folder
* If the currently selected tab is locked and the given flag "changeLockedTab" is off,
* then a new tab is opened with the given folder,
* otherwise, the currently presented folder is replaces with the given folder
*
* @param folder - the folder to be set
* @param children - the children of the given folder
* @param fileToSelect - the file that would be selected after changing the folder
* @param changeLockedTab - flag that indicates whether to change the presented folder in
* the currently selected tab although it's locked (used when switching tabs)
*/
public void setCurrentFolder(AbstractFile folder, AbstractFile children[], AbstractFile fileToSelect, boolean changeLockedTab) {
fileTable.setCurrentFolder(folder, children, fileToSelect);
}
/**
* Shows the pop up which is located the given index in fileTablePopups.
*
* @param index - index of the FileTablePopup in fileTablePopups.
*/
public void showQuickList(int index) {
try {
fileTablePopups.get()[index].show();
} catch (Exception e) {
LOGGER.error("Unable to show QuickList", e);
throw new RuntimeException(e);
}
}
/**
* Returns true if a directory tree is visible.
*/
public boolean isTreeVisible() {
return treeVisible;
}
/**
* Returns width of a folders tree.
* @return a width of a folders tree
*/
public int getTreeWidth() {
if (!treeVisible) {
return oldTreeWidth;
} else {
return treeSplitPane.getDividerLocation();
}
}
/**
* Sets a width of a folders tree.
* @param width new width
*/
public void setTreeWidth(int width) {
if (!treeVisible) {
oldTreeWidth = width;
} else {
treeSplitPane.setDividerLocation(width);
treeSplitPane.doLayout();
}
}
/**
* Returns a panel with a folders tree.
* @return a panel with a folders tree
*/
public FoldersTreePanel getFoldersTreePanel() {
return foldersTreePanel;
}
/**
* Enables/disables a directory tree visibility. Invoked by {@link com.mucommander.ui.action.impl.ToggleTreeAction}.
*/
public void setTreeVisible(boolean treeVisible) {
if (this.treeVisible != treeVisible) {
this.treeVisible = treeVisible;
if (!treeVisible) {
// save width of a tree panel
oldTreeWidth = treeSplitPane.getDividerLocation();
fileTable.requestFocusInWindow();
}
foldersTreePanel.setVisible(treeVisible);
// hide completely divider if a tree isn't visible
treeSplitPane.setDividerLocation(treeVisible ? oldTreeWidth : 0);
treeSplitPane.setDividerSize(treeVisible ? 5 : 0);
if (treeVisible) {
foldersTreePanel.requestFocus();
}
}
}
////////////////////////
// Overridden methods //
////////////////////////
/**
* Overridden for debugging purposes.
*/
@Override
public String toString() {
return getClass().getName()+"@"+hashCode() +" currentFolder="+getCurrentFolder()+" hasFocus="+panel.hasFocus();
}
///////////////////////////
// FocusListener methods //
///////////////////////////
public void focusGained(FocusEvent e) {
// Notify MainFrame that we are in control now! (our table/location field is active)
mainFrame.setActiveTable(fileTable);
}
public void focusLost(FocusEvent e) {
fileTable.getQuickSearch().stop();
}
////////////////////////////////
// QuickListContainer methods //
////////////////////////////////
public Point calcQuickListPosition(Dimension dim) {
return new Point(
Math.max((getWidth() - (int)dim.getWidth()) / 2, 0),
getLocationTextField().getHeight() + Math.max((panel.getHeight() - (int)dim.getHeight()) / 3, 0)
);
}
public Component containerComponent() {
return panel;
}
public Component nextFocusableComponent() {
return fileTable;
}
@Override
public int getWidth() {
return panel.getWidth();
}
///////////////////////////////
// ActiveTabListener methods //
///////////////////////////////
public void activeTabChanged() {
boolean isCurrentTabLocked = tabs.getCurrentTab().isLocked();
locationTextField.setEnabled(!isCurrentTabLocked);
driveButton.setEnabled(!isCurrentTabLocked);
}
public JPanel getPanel() {
return panel;
}
}