Source code: com/yaftp/ftp/SwingFtpTable.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.ftp ;
21
22 import java.awt.* ;
23 import java.awt.event.* ;
24 import javax.swing.* ;
25 import javax.swing.border.* ;
26 import javax.swing.table.* ;
27 import javax.swing.tree.* ;
28 import javax.swing.event.* ;
29
30 import java.util.* ;
31 import com.yaftp.utils.* ;
32
33
34 /**
35
36 wrap and customize a JTable for FTP File list
37 purpose
38
39 */
40
41 public class SwingFtpTable extends Observable
42 {
43 private JPanel _panel = new JPanel() ;
44 private CardLayout _cardlayout = new CardLayout() ;
45
46 private final ImageIcon _FILE_ICON_ = new ImageIcon(
47 getClass().getResource("images/file.gif") ,
48 "File"
49 ) ;
50
51 private final ImageIcon _FOLDER_ICON_ = new ImageIcon(
52 getClass().getResource("images/folder.gif"),
53 "Folder"
54 ) ;
55
56 private final static String _FILE_NAMES_ = " Files & Folders " ;
57
58 private JTable _table ;
59 private _DATAMODEL_ _model ;
60
61 private String[] _expandedColNames ;
62 private String[] _colNames ;
63 private boolean _expandedState = false ;
64
65 protected SwingEhnStatusBar _errReport ; // Where any error is reported back
66
67 private Object[][] _displayedData ;
68
69 private Vector _data ; // store lines of FtpOsFile
70
71 public JPanel get_panel()
72 { return _panel ; }
73
74 public ImageIcon get_FolderIcon()
75 { return _FOLDER_ICON_ ; }
76
77 public ImageIcon get_FileIcon()
78 { return _FILE_ICON_ ; }
79
80 // The class bellow drives the BASIC table display
81 class _DATAMODEL_ extends AbstractTableModel {
82
83 public int getColumnCount()
84 {
85 return _colNames.length ;
86 }
87 public int getRowCount()
88 {
89 if ( _displayedData == null )
90 return 0 ;
91 return _displayedData.length ;
92 }
93
94 public Object getValueAt( int row , int col )
95 {
96 if ( _displayedData == null )
97 return null ;
98
99 return _displayedData[row][col] ;
100 }
101
102 public String getColumnName( int column )
103 { return _colNames[column] ;}
104
105 public Class getColumnClasss( int col )
106 { return getValueAt(0,col).getClass() ; }
107
108 public boolean isCellEditable( int row , int col )
109 {
110 return false ;
111 }
112
113 public void setValueAt ( Object aValue , int row , int col )
114 {
115 if ( _displayedData != null )
116 _displayedData[row][col] = (String)aValue ;
117 }
118
119 public void structureChanged()
120 {
121 fireTableStructureChanged() ;
122 }
123 }
124
125 public FtpOsFile[] getCurrentlySelectedFiles()
126 {
127 int selectedRows[] = _table.getSelectedRows() ;
128
129 if ( selectedRows != null )
130 {
131 int totSelected = selectedRows.length ;
132 // remove anyDirectory from selection
133 for ( int ii = 0 ; ii < selectedRows.length ; ii++ )
134 {
135 FtpOsFile cur = (FtpOsFile) _data.elementAt(selectedRows[ii]) ;
136 if ( cur.is_directory() )
137 totSelected-- ;
138 }
139 if ( totSelected == 0 )
140 return null ;
141
142 FtpOsFile[] returned = new FtpOsFile[totSelected] ;
143 for ( int ii = 0 ; ii < selectedRows.length ; ii++ )
144 {
145 FtpOsFile cur = (FtpOsFile) _data.elementAt(selectedRows[ii]) ;
146 if ( ! cur.is_directory() )
147 returned[ii] = cur ;
148 }
149 return returned ;
150 }
151 else
152 return null ;
153 }
154
155 private void notifyTableContainer()
156 {
157 setChanged() ;
158 notifyObservers ( _data.elementAt( _table.getSelectedRow() ) ) ;
159 clearChanged() ;
160 }
161
162 // Capturing DOUBLE CLICK mouse event
163 class _MOUSE_CLICK_ extends MouseAdapter {
164
165 public void mouseReleased( MouseEvent e )
166 {
167 if ( e.getClickCount() == 2 ) // Clicked twice
168 notifyTableContainer() ;
169 }
170 }
171
172 private void buildPanel( String title )
173 {
174 _panel.setLayout ( _cardlayout ) ;
175
176 Border titleMain ;
177
178 // TITLE BORDER IS OPTIONAL
179 if ( title == null )
180 titleMain = Swing.buildRaisedPanelBorder( Color.lightGray , 2 ) ;
181 else
182 titleMain = Swing.buildBorder(
183 title ,
184 TitledBorder.CENTER ,
185 TitledBorder.ABOVE_TOP ,
186 Swing.FTPTITLE ,
187 Swing.BEVELRAISED
188 ) ;
189 _panel.setBorder(titleMain) ;
190
191 }
192
193 private void buildTable()
194 {
195 _model = new _DATAMODEL_() ;
196 _table = new JTable( _model ) ;
197 _table.addMouseListener( new _MOUSE_CLICK_() ) ;
198 _table.setShowVerticalLines(false) ;
199 _table.setShowHorizontalLines(false) ;
200
201 JScrollPane scrollpane = new JScrollPane( (_table) ) ;
202 scrollpane.setColumnHeaderView( _table.getTableHeader() ) ; // MAKE TABLE HEADER VISIBLES
203
204 _panel.add( scrollpane , "table panel" ) ;
205 }
206
207 class _FILE_COLUMNS_RENDERER_ extends DefaultTableCellRenderer {
208
209 public void setValue( Object value )
210 {
211 if ( value != null )
212 {
213 if ( value instanceof FtpOsFile )
214 {
215 FtpOsFile curFile = (FtpOsFile) value ;
216
217 setText( curFile.get_name() ) ;
218 if ( curFile.is_directory() )
219 setIcon( _FOLDER_ICON_ ) ;
220 else
221 setIcon( _FILE_ICON_ ) ;
222 }
223 if ( value instanceof String )
224 {
225 setText( (String) value ) ;
226 }
227 }
228 }
229 }
230
231 /** very simple way of clearing table content */
232 public void clearData()
233 { _data = null ; }
234
235
236 /**
237 Get LIST FILE data and Os dependent titles
238 */
239 public void setData( Vector data ,
240 String expandedColumnNames[]
241 )
242 {
243 _data = data ;
244 _expandedColNames = expandedColumnNames ;
245
246 if ( _expandedState )
247 buildExpanded() ;
248 else
249 buildColapsed() ;
250 }
251
252 /** Make the colapsed window visible */
253 public void set_colapsed()
254 {
255 buildColapsed() ;
256 _expandedState = false ;
257 }
258
259 /** Make the expanded window visible */
260 public void set_expanded()
261 {
262 buildExpanded() ;
263 _expandedState = true ;
264 }
265
266 private void buildColapsed()
267 {
268 _colNames = new String[]{ _FILE_NAMES_ } ;
269
270 if ( _table == null ) // first Time
271 buildTable() ;
272 else
273 _model.structureChanged() ;
274
275
276 _displayedData = new Object[ _data.size()][1] ;
277
278 TableColumn curCol = _table.getColumn(_FILE_NAMES_) ;
279 _table.getColumn(_FILE_NAMES_).setCellRenderer(new _FILE_COLUMNS_RENDERER_());
280
281 for ( int ii = 0 ; ii < _data.size() ; ii++ )
282 _displayedData[ii][0] = _data.elementAt(ii) ;
283 }
284
285 private void buildExpanded()
286 {
287 _colNames = _expandedColNames ;
288
289 _model.structureChanged() ;
290
291 _displayedData = new Object[ _data.size()]
292 [ _expandedColNames.length] ;
293
294 for ( int ii = 0 ; ii < _data.size() ; ii++ )
295 _displayedData[ii] = ((FtpOsFile)(_data.elementAt(ii))).get_detailled();
296 }
297
298 /** FTP Table style constructor */
299 public SwingFtpTable( String title ,
300 SwingEhnStatusBar errReport
301 )
302 {
303 _errReport = errReport ;
304 buildPanel(title) ;
305 }
306
307 public static void main ( String arg[] )
308 {
309 // Exit the debug window frame
310 class WL extends WindowAdapter{
311 public void windowClosing( WindowEvent e )
312 { System.exit(0) ; }
313 }
314
315 SwingEhnStatusBar errors = new SwingEhnStatusBar() ;
316
317 SwingFtpTable w = new SwingFtpTable("Test FTP" , errors ) ;
318
319 Vector data = new Vector() ;
320
321 data.addElement( new FtpOsFile( "FolderName1" , true , 100 , null ) ) ;
322 data.addElement( new FtpOsFile( "FileName1.filetype" , false , 200 , null ) );
323 data.addElement( new FtpOsFile( "FileName2.filetype" , false , 200 , null ) );
324 data.addElement( new FtpOsFile( "FileName3.filetype" , false , 200 , null ) );
325 data.addElement( new FtpOsFile( "FileName4.filetype" , false , 200 , null ) );
326 data.addElement( new FtpOsFile( "FolderName2" , true , 100 , null ) );
327 data.addElement( new FtpOsFile( "FolderName3" , true , 100 , null ) );
328 data.addElement( new FtpOsFile( "FolderName4" , true , 100 , null ) );
329 data.addElement( new FtpOsFile( "FolderName5" , true , 100 , null ) );
330
331 w.setData(data,null) ;
332
333 JFrame f = new JFrame("Swing FTP TABLE testing ") ;
334
335 f.setForeground(SystemColor.text) ;
336 f.setBackground(SystemColor.control.brighter()) ;
337 f.addWindowListener( new WL() ) ;
338
339 f.getContentPane().add("Center", w.get_panel() ) ;
340 f.getContentPane().add("South",errors) ;
341 f.pack() ;
342 f.show() ;
343 }
344
345 }