28
28
import javax .swing .ComboBoxModel ;
29
29
import javax .swing .DefaultListCellRenderer ;
30
30
import javax .swing .Icon ;
31
+ import javax .swing .ImageIcon ;
31
32
import javax .swing .JButton ;
32
33
import javax .swing .JComponent ;
33
34
import javax .swing .JLabel ;
@@ -67,18 +68,32 @@ public class FilestoreChooser extends JPanel {
67
68
private final JLabel logLabel_ ;
68
69
private final PropertyChangeListener connectorWatcher_ ;
69
70
private final Action upAction_ ;
71
+ private final Action prevAction_ ;
70
72
private final Action okAction_ ;
73
+ private final Action [] navActions_ ;
71
74
private final Component [] activeComponents_ ;
72
- private Branch lastBranch_ ;
75
+ private Branch currentBranch_ ;
76
+ private Branch prevBranch_ ;
73
77
private ConnectorAction connectorAction_ ;
74
78
private List connectorActions_ ;
75
79
private static Logger logger_ =
76
80
Logger .getLogger ( "uk.ac.starlink.connect" );
77
81
78
82
/**
79
- * Constructor .
83
+ * Constructs a FilestoreChooser with navigation buttons included .
80
84
*/
81
85
public FilestoreChooser () {
86
+ this ( true );
87
+ }
88
+
89
+ /**
90
+ * Constructs a FilestoreChooser with navigation buttons optionally
91
+ * included.
92
+ *
93
+ * @param includeButtons whether to include navigation buttons in
94
+ * the component
95
+ */
96
+ public FilestoreChooser ( boolean includeButtons ) {
82
97
super ( new BorderLayout () );
83
98
84
99
/* Basic setup. */
@@ -97,35 +112,75 @@ public FilestoreChooser() {
97
112
branchBox .setBorder ( gapBorder );
98
113
add ( branchBox , BorderLayout .NORTH );
99
114
100
- /* Define and add a button for moving up a directory. */
101
- Icon upIcon = UIManager .getIcon ( "FileChooser.upFolderIcon" );
102
- upAction_ = new AbstractAction ( null , upIcon ) {
115
+ /* Action for moving up a directory. */
116
+ Icon upIcon =
117
+ new ImageIcon ( FilestoreChooser .class .getResource ( "Up24.gif" ) );
118
+ upAction_ = new AbstractAction ( "Up" , upIcon ) {
103
119
public void actionPerformed ( ActionEvent evt ) {
104
120
Branch parent = getBranch ().getParent ();
105
121
if ( parent != null ) {
106
122
setBranch ( parent );
107
123
}
108
124
}
109
125
};
110
- JButton upButton = new JButton ( upAction_ );
111
- branchBox .add ( Box .createHorizontalStrut ( 5 ) );
112
- branchBox .add ( upButton );
126
+ upAction_ .putValue ( Action .SHORT_DESCRIPTION ,
127
+ "Move to the parent directory" );
113
128
114
- /* Define and add a button for moving to home directory. */
115
- Icon homeIcon = UIManager .getIcon ( "FileChooser.homeFolderIcon" );
129
+ /* Action for moving to home directory. */
130
+ Icon homeIcon =
131
+ new ImageIcon ( FilestoreChooser .class .getResource ( "Home24.gif" ) );
116
132
final Branch homedir = getHomeBranch ();
117
- Action homeAction = new AbstractAction ( null , homeIcon ) {
133
+ Action homeAction = new AbstractAction ( "Home" , homeIcon ) {
118
134
public void actionPerformed ( ActionEvent evt ) {
119
135
if ( homedir != null ) {
120
136
setBranch ( homedir );
121
137
}
122
138
}
123
139
};
140
+ homeAction .putValue ( Action .SHORT_DESCRIPTION ,
141
+ "Return to home directory" );
124
142
homeAction .setEnabled ( homedir != null );
125
- JButton homeButton = new JButton ( homeAction );
126
- activeList .add ( homeButton );
127
- branchBox .add ( Box .createHorizontalStrut ( 5 ) );
128
- branchBox .add ( homeButton );
143
+
144
+ /* Action for moving to previous directory. */
145
+ Icon prevIcon =
146
+ new ImageIcon ( FilestoreChooser .class .getResource ( "Back24.gif" ) );
147
+ prevAction_ = new AbstractAction ( "Back" , prevIcon ) {
148
+ public void actionPerformed ( ActionEvent evt ) {
149
+ if ( prevBranch_ != null ) {
150
+ setBranch ( prevBranch_ );
151
+ }
152
+ }
153
+ };
154
+ prevAction_ .putValue ( Action .SHORT_DESCRIPTION ,
155
+ "Back to previously selected directory" );
156
+ prevAction_ .setEnabled ( prevBranch_ != null );
157
+
158
+ /* Action for refreshing the file list. */
159
+ Icon refreshIcon =
160
+ new ImageIcon ( FilestoreChooser .class
161
+ .getResource ( "Refresh24.gif" ) );
162
+ Action refreshAction = new AbstractAction ( "Refresh" , refreshIcon ) {
163
+ public void actionPerformed ( ActionEvent evt ) {
164
+ refreshList ();
165
+ }
166
+ };
167
+ refreshAction .putValue ( Action .SHORT_DESCRIPTION ,
168
+ "Refresh list of files in current directory" );
169
+
170
+ /* Add navigation action buttons if required. */
171
+ navActions_ =
172
+ new Action [] { homeAction , upAction_ , prevAction_ , refreshAction , };
173
+ if ( includeButtons ) {
174
+ for ( int ia = 0 ; ia < navActions_ .length ; ia ++ ) {
175
+ JButton button = new JButton ( navActions_ [ ia ] );
176
+ button .setText ( null );
177
+ activeList .add ( button );
178
+ if ( ia > 0 ) {
179
+ branchBox .add ( Box .createHorizontalStrut ( 5 ) );
180
+ branchBox .add ( button );
181
+ }
182
+ }
183
+ }
129
184
130
185
/* Button for login/logout. This will only be visible if the current
131
186
* branch represents a remote filesystem. */
@@ -263,13 +318,23 @@ public Action getOkAction() {
263
318
return okAction_ ;
264
319
}
265
320
321
+ /**
322
+ * Returns the actions which allow the user to do additional navigation.
323
+ *
324
+ * @return navigation actions
325
+ */
326
+ public Action [] getNavigationActions () {
327
+ return navActions_ ;
328
+ }
329
+
266
330
public void setEnabled ( boolean enabled ) {
267
331
if ( enabled != isEnabled () ) {
268
332
okAction_ .setEnabled ( enabled );
269
333
for ( int i = 0 ; i < activeComponents_ .length ; i ++ ) {
270
334
activeComponents_ [ i ].setEnabled ( enabled );
271
335
}
272
- upAction_ .setEnabled ( enabled && lastBranch_ .getParent () != null );
336
+ upAction_ .setEnabled ( enabled &&
337
+ currentBranch_ .getParent () != null );
273
338
}
274
339
super .setEnabled ( enabled );
275
340
}
@@ -336,10 +401,12 @@ public void addDefaultBranches() {
336
401
*/
337
402
public void setBranch ( Branch branch ) {
338
403
if ( branch != branchSelector_ .getSelectedBranch () ) {
404
+ prevBranch_ = branchSelector_ .getSelectedBranch ();
405
+ prevAction_ .setEnabled ( prevBranch_ != null );
339
406
branchSelector_ .setSelectedBranch ( branch );
340
407
}
341
- if ( branch != lastBranch_ ) {
342
- lastBranch_ = branch ;
408
+ if ( branch != currentBranch_ ) {
409
+ currentBranch_ = branch ;
343
410
BoundedRangeModel scrollModel =
344
411
scroller_ .getVerticalScrollBar ().getModel ();
345
412
scrollModel .setValue ( scrollModel .getMinimum () );
@@ -356,11 +423,22 @@ public void setBranch( Branch branch ) {
356
423
* currently selected branch.
357
424
*/
358
425
public void refreshList () {
359
- Branch branch = getBranch ();
360
- Node [] children = branch == null ? new Node [ 0 ]
361
- : branch .getChildren ();
362
- Arrays .sort ( children , NodeComparator .getInstance () );
363
- nodeList_ .setListData ( children );
426
+ final Branch branch = getBranch ();
427
+ if ( branch != null ) {
428
+ new Thread ( "Refresh Directory" ) {
429
+ public void run () {
430
+ final Node [] children = branch .getChildren ();
431
+ Arrays .sort ( children , NodeComparator .getInstance () );
432
+ SwingUtilities .invokeLater ( new Runnable () {
433
+ public void run () {
434
+ if ( getBranch () == branch ) {
435
+ nodeList_ .setListData ( children );
436
+ }
437
+ }
438
+ } );
439
+ }
440
+ }.start ();
441
+ }
364
442
}
365
443
366
444
/**
0 commit comments