Skip to content

Commit c747873

Browse files
committed
connect: improve file chooser navigation options and GUI
1 parent e9b67b2 commit c747873

File tree

2 files changed

+115
-24
lines changed

2 files changed

+115
-24
lines changed

connect/src/main/uk/ac/starlink/connect/FilestoreChooser.java

+101-23
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.swing.ComboBoxModel;
2929
import javax.swing.DefaultListCellRenderer;
3030
import javax.swing.Icon;
31+
import javax.swing.ImageIcon;
3132
import javax.swing.JButton;
3233
import javax.swing.JComponent;
3334
import javax.swing.JLabel;
@@ -67,18 +68,32 @@ public class FilestoreChooser extends JPanel {
6768
private final JLabel logLabel_;
6869
private final PropertyChangeListener connectorWatcher_;
6970
private final Action upAction_;
71+
private final Action prevAction_;
7072
private final Action okAction_;
73+
private final Action[] navActions_;
7174
private final Component[] activeComponents_;
72-
private Branch lastBranch_;
75+
private Branch currentBranch_;
76+
private Branch prevBranch_;
7377
private ConnectorAction connectorAction_;
7478
private List connectorActions_;
7579
private static Logger logger_ =
7680
Logger.getLogger( "uk.ac.starlink.connect" );
7781

7882
/**
79-
* Constructor.
83+
* Constructs a FilestoreChooser with navigation buttons included.
8084
*/
8185
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 ) {
8297
super( new BorderLayout() );
8398

8499
/* Basic setup. */
@@ -97,35 +112,75 @@ public FilestoreChooser() {
97112
branchBox.setBorder( gapBorder );
98113
add( branchBox, BorderLayout.NORTH );
99114

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 ) {
103119
public void actionPerformed( ActionEvent evt ) {
104120
Branch parent = getBranch().getParent();
105121
if ( parent != null ) {
106122
setBranch( parent );
107123
}
108124
}
109125
};
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" );
113128

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" ) );
116132
final Branch homedir = getHomeBranch();
117-
Action homeAction = new AbstractAction( null, homeIcon ) {
133+
Action homeAction = new AbstractAction( "Home", homeIcon ) {
118134
public void actionPerformed( ActionEvent evt ) {
119135
if ( homedir != null ) {
120136
setBranch( homedir );
121137
}
122138
}
123139
};
140+
homeAction.putValue( Action.SHORT_DESCRIPTION,
141+
"Return to home directory" );
124142
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+
}
129184

130185
/* Button for login/logout. This will only be visible if the current
131186
* branch represents a remote filesystem. */
@@ -263,13 +318,23 @@ public Action getOkAction() {
263318
return okAction_;
264319
}
265320

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+
266330
public void setEnabled( boolean enabled ) {
267331
if ( enabled != isEnabled() ) {
268332
okAction_.setEnabled( enabled );
269333
for ( int i = 0; i < activeComponents_.length; i++ ) {
270334
activeComponents_[ i ].setEnabled( enabled );
271335
}
272-
upAction_.setEnabled( enabled && lastBranch_.getParent() != null );
336+
upAction_.setEnabled( enabled &&
337+
currentBranch_.getParent() != null );
273338
}
274339
super.setEnabled( enabled );
275340
}
@@ -336,10 +401,12 @@ public void addDefaultBranches() {
336401
*/
337402
public void setBranch( Branch branch ) {
338403
if ( branch != branchSelector_.getSelectedBranch() ) {
404+
prevBranch_ = branchSelector_.getSelectedBranch();
405+
prevAction_.setEnabled( prevBranch_ != null );
339406
branchSelector_.setSelectedBranch( branch );
340407
}
341-
if ( branch != lastBranch_ ) {
342-
lastBranch_ = branch;
408+
if ( branch != currentBranch_ ) {
409+
currentBranch_ = branch;
343410
BoundedRangeModel scrollModel =
344411
scroller_.getVerticalScrollBar().getModel();
345412
scrollModel.setValue( scrollModel.getMinimum() );
@@ -356,11 +423,22 @@ public void setBranch( Branch branch ) {
356423
* currently selected branch.
357424
*/
358425
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+
}
364442
}
365443

366444
/**

table/src/main/uk/ac/starlink/table/gui/FilestoreTableLoadDialog.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import java.awt.BorderLayout;
44
import java.awt.Component;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.KeyEvent;
57
import java.io.IOException;
8+
import javax.swing.AbstractAction;
9+
import javax.swing.Action;
610
import javax.swing.BorderFactory;
711
import javax.swing.Box;
812
import javax.swing.JComponent;
913
import javax.swing.JLabel;
14+
import javax.swing.JMenu;
1015
import javax.swing.JPanel;
1116
import javax.swing.JTextField;
1217
import uk.ac.starlink.connect.Leaf;
@@ -39,12 +44,20 @@ public FilestoreTableLoadDialog() {
3944

4045
protected Component createQueryComponent() {
4146
JComponent panel = new JPanel( new BorderLayout() );
42-
chooser_ = new FilestoreChooser() {
47+
chooser_ = new FilestoreChooser( false ) {
4348
public void leafSelected( Leaf leaf ) {
4449
submit();
4550
}
4651
};
4752
chooser_.addDefaultBranches();
53+
Action[] navActs = chooser_.getNavigationActions();
54+
setToolbarActions( navActs );
55+
JMenu navMenu = new JMenu( "Navigation" );
56+
navMenu.setMnemonic( KeyEvent.VK_N );
57+
for ( int i = 0; i < navActs.length; i++ ) {
58+
navMenu.add( navActs[ i ] );
59+
}
60+
setMenus( new JMenu[] { navMenu } );
4861
panel.add( chooser_, BorderLayout.CENTER );
4962
JLabel posLabel = new JLabel( "Position in file: #" );
5063
posField_ = new JTextField( 6 );

0 commit comments

Comments
 (0)