Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/yaftp/ftp/gui/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  package com.yaftp.ftp.gui ;
20  
21  import java.awt.*    ;
22  import java.awt.event.* ;
23  import javax.swing.* ;
24  import javax.swing.border.* ;
25  import javax.swing.table.* ;
26  import javax.swing.tree.* ;
27  import javax.swing.event.* ;
28  
29  import java.util.* ;
30  import com.yaftp.utils.* ;
31  import com.yaftp.ftp.*   ;
32  import com.yaftp.ftp.FtpOsFile   ;
33  
34  
35  /**
36  
37    wrap and customize a JTable for FTP File list
38    purpose
39  
40    @Author Jean-Yves MENGANT
41    CopyRights Jean-Yves MENGANT 1999,2000
42  
43    @version : 0.0.1
44  
45  
46  */
47  
48  public class SwingFtpTable
49  extends Observable
50  {
51    private JPanel     _panel      = new JPanel()     ;
52    private CardLayout _cardlayout = new CardLayout() ;
53  
54    private final static int _COLUMN_FILLER_ = 5 ;
55    private final ImageIcon _FILE_ICON_  =  new ImageIcon(
56                                   getClass().getResource("images/file.gif") ,
57                                                          "File"
58                                                          ) ;
59  
60    private final ImageIcon _FOLDER_ICON_  =  new ImageIcon(
61                                  getClass().getResource("images/folder.gif"),
62                                                          "Folder"
63                                                          ) ;
64  
65    private final static String _FILE_NAMES_ = " Files & Folders " ;
66  
67    private JTable      _table     ;
68    private SwingTableColumnSizer _sizer ;
69    private int         _containerWidth ;
70    private _DATAMODEL_ _model     ;
71    private SwingComponentPopup _popup  ;
72  
73  
74    private String[]    _expandedColNames  ;
75    private String[]    _colNames          ;
76    private boolean     _expandedState     = false ;
77  
78    protected SwingEhnStatusBar _errReport ; // Where any error is reported back
79  
80    private Object[][]  _displayedData ;
81  
82    private Vector      _data      ; // store lines of FtpOsFile
83  
84    public JPanel get_panel()
85    { return _panel ; }
86  
87    public ImageIcon get_FolderIcon()
88    { return _FOLDER_ICON_ ; }
89  
90    public ImageIcon get_FileIcon()
91    { return _FILE_ICON_ ; }
92  
93    public void set_containerWidth( int width )
94    { _containerWidth = width ; }
95  
96    // The class bellow drives the BASIC table display
97    class _DATAMODEL_ extends AbstractTableModel {
98  
99      public int getColumnCount()
100     {
101       return _colNames.length ;
102     }
103     public int getRowCount()
104     {
105       if ( _displayedData == null )
106         return 0   ;
107       return _displayedData.length   ;
108     }
109 
110     public Object getValueAt( int row , int col )
111     {
112       if ( _displayedData == null )
113         return null ;
114 
115       return _displayedData[row][col] ;
116     }
117 
118     public String getColumnName( int column )
119     { return _colNames[column] ;}
120 
121     public Class getColumnClasss( int col )
122     { return getValueAt(0,col).getClass() ; }
123 
124     public boolean isCellEditable( int row , int col )
125     {
126       return false ;
127     }
128 
129     public void setValueAt ( Object aValue , int row , int col )
130     {
131       if ( _displayedData != null )
132         _displayedData[row][col] = (String)aValue  ;
133     }
134 
135     public void structureChanged()
136     {
137       fireTableStructureChanged() ;
138     }
139   }
140 
141   public FtpSwingSelectedFiles getCurrentlySelectedFiles( int doubleClicked )
142   {
143   int selectedRows[] = _table.getSelectedRows() ;
144   FtpSwingSelectedFiles returned = new FtpSwingSelectedFiles( _data , selectedRows ) ;
145     returned.set_doubleClicked(doubleClicked) ;
146     return returned ;
147   }
148 
149   private void notifyTableContainer( int doubleClicked )
150   {
151     setChanged() ;
152     notifyObservers ( getCurrentlySelectedFiles(doubleClicked) ) ;
153     clearChanged() ;
154   }
155 
156   // Capturing DOUBLE CLICK mouse event
157   class _MOUSE_CLICK_ extends MouseAdapter {
158 
159     public void mouseReleased( MouseEvent e )
160     {
161       if ( e.getClickCount() == 2 )  // Clicked twice
162         notifyTableContainer(_table.getSelectedRow()) ;
163     }
164   }
165 
166   private void buildPanel( String title )
167   {
168     _panel.setLayout ( _cardlayout ) ;
169 
170     Border titleMain ;
171 
172     // TITLE BORDER IS OPTIONAL
173     if ( title == null )
174       titleMain =  Swing.buildRaisedPanelBorder( Color.lightGray , 2 ) ;
175     else
176       titleMain = Swing.buildBorder(
177                                       title ,
178                                       TitledBorder.CENTER ,
179                                       TitledBorder.ABOVE_TOP ,
180                                       Swing.FTPTITLE ,
181                                       Swing.BEVELRAISED
182                                    ) ;
183     _panel.setBorder(titleMain) ;
184 
185   }
186 
187   class _SELECTIONS_
188   implements ListSelectionListener
189   {
190     public void valueChanged ( ListSelectionEvent e )
191     {
192       if ( ! e.getValueIsAdjusting()  )
193         notifyTableContainer(FtpSwingSelectedFiles.NON_APPLICABLE) ;
194     }
195   }
196 
197   class _COMPONENT_RESIZED_
198   extends ComponentAdapter {
199     void ComponentResized( ComponentEvent e )
200     {
201     Component c = e.getComponent() ;
202       System.out.println( "component resized : " + c.getSize() ) ;
203     }
204   }
205 
206   private void buildTable()
207   {
208     _model    = new _DATAMODEL_() ;
209     _table    = new JTable( _model ) ;
210     _sizer    = new SwingTableColumnSizer( _table ) ;
211     _table.addMouseListener( new _MOUSE_CLICK_() ) ;
212     _table.getSelectionModel().addListSelectionListener(new _SELECTIONS_() );
213     _table.setShowVerticalLines(false) ;
214     _table.setShowHorizontalLines(false) ;
215     // autoresize last column  is set
216     _table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN ) ;
217 
218     JScrollPane scrollpane = new JScrollPane( (_table) ) ;
219     _table.addComponentListener( new _COMPONENT_RESIZED_() ) ;
220 
221     scrollpane.setColumnHeaderView( _table.getTableHeader() ) ; // MAKE TABLE HEADER VISIBLES
222 
223     _panel.add( scrollpane , "table panel" ) ;
224   }
225 
226   class _FILE_COLUMNS_RENDERER_ extends DefaultTableCellRenderer {
227 
228     public void setValue( Object value )
229     {
230       if ( value != null )
231       {
232         if ( value instanceof FtpOsFile )
233         {
234         FtpOsFile curFile = (FtpOsFile) value   ;
235 
236           setText( curFile.get_name()  ) ;
237           if ( curFile.is_directory() )
238             setIcon( _FOLDER_ICON_ ) ;
239           else
240             setIcon( _FILE_ICON_ ) ;
241         }
242         if ( value instanceof String )
243         {
244           setText( (String) value ) ;
245         }
246       }
247     }
248   }
249 
250   /** very simple way of clearing table content */
251   public void clearData()
252   { _data = null  ; }
253 
254 
255   /**
256     Get LIST FILE data and Os dependent titles
257   */
258   public void setData( Vector data ,
259                        String expandedColumnNames[]
260                      )
261   {
262     _data = data  ;
263     _expandedColNames = expandedColumnNames ;
264 
265     if ( _expandedState )
266       buildExpanded() ;
267     else
268       buildColapsed() ;
269   }
270 
271   /** Make the colapsed window visible */
272   public void set_colapsed()
273   {
274     buildColapsed() ;
275     _expandedState = false ;
276   }
277 
278   /** Make the expanded window visible */
279   public void set_expanded()
280   {
281     buildExpanded() ;
282     _expandedState = true  ;
283   }
284 
285   private void resizeColumn( TableColumn column , int nbcols  )
286   {
287   int size  = _containerWidth / nbcols  ;
288     column.setMaxWidth( size );
289     column.setMinWidth( size );
290     // if sizeColumnsToFit is not called repaint fails ...
291     // this is due to a Swing bug in JDK1.2.2
292     _table.sizeColumnsToFit(0) ;
293   }
294 
295   private void buildColapsed()
296   {
297     _colNames = new String[]{ _FILE_NAMES_ } ;
298 
299     if ( _table == null ) // first Time
300       buildTable() ;
301     else
302       _model.structureChanged() ;
303 
304 
305     _displayedData = new Object[ _data.size()][1] ;
306 
307     TableColumn curCol = _table.getColumn(_FILE_NAMES_) ;
308     _table.getColumn(_FILE_NAMES_).setCellRenderer(new _FILE_COLUMNS_RENDERER_());
309 
310     // resizeColumn( curCol , 1 ) ;
311 
312     for ( int ii = 0 ; ii < _data.size() ; ii++ )
313       _displayedData[ii][0] = _data.elementAt(ii) ;
314 
315     _sizer.resizeColumn( _colNames[0] , _COLUMN_FILLER_ );
316   }
317 
318   private void buildExpanded()
319   {
320     _colNames = _expandedColNames ;
321 
322     _model.structureChanged() ;
323 
324     _displayedData = new Object[ _data.size()]
325                                [ _expandedColNames.length] ;
326 
327     for ( int ii = 0 ; ii < _data.size() ; ii++ )
328       _displayedData[ii] = ((FtpOsFile)(_data.elementAt(ii))).get_detailled();
329 
330     for ( int ii = 0 ; ii < _expandedColNames.length ; ii++ )
331     {
332     TableColumn curCol = _table.getColumn( _expandedColNames[ii] ) ;
333       _sizer.resizeColumn( _colNames[ii] , _COLUMN_FILLER_ ) ;
334       //resizeColumn( curCol , _expandedColNames.length ) ;
335     }
336 
337   }
338 
339   /** FTP Table style constructor  */
340   public SwingFtpTable( String title        ,
341                         SwingEhnStatusBar errReport
342                       )
343   {
344     _errReport = errReport ;
345     buildPanel(title) ;
346   }
347 
348   public JTable get_table()
349   { return _table ; }
350 
351   public static void main ( String arg[] )
352   {
353 
354     SwingEhnStatusBar errors = new SwingEhnStatusBar()  ;
355 
356     SwingFtpTable w = new SwingFtpTable("Test FTP"  , errors ) ;
357 
358     Vector data = new Vector() ;
359 
360     data.addElement( new FtpOsFile(  "FolderName1" , true , 100 , null )  ) ;
361     data.addElement( new FtpOsFile(  "FileName1.filetype" , false , 200 , null )  );
362     data.addElement( new FtpOsFile(  "FileName2.filetype" , false , 200 , null )  );
363     data.addElement( new FtpOsFile(  "FileName3.filetype" , false , 200 , null )  );
364     data.addElement( new FtpOsFile(  "FileName4.filetype" , false , 200 , null )  );
365     data.addElement( new FtpOsFile(  "FolderName2" , true , 100 , null )  );
366     data.addElement( new FtpOsFile(  "FolderName3" , true , 100 , null )  );
367     data.addElement( new FtpOsFile(  "FolderName4" , true , 100 , null )  );
368     data.addElement( new FtpOsFile(  "FolderName5" , true , 100 , null )  );
369 
370     w.setData(data,null) ;
371 
372     JFrame f = new JFrame("Swing FTP TABLE testing ")  ;
373 
374     f.setForeground(SystemColor.text) ;
375     f.setBackground(SystemColor.control.brighter()) ;
376     f.addWindowListener( new WindowAdapter ()
377        {
378          public void windowClosing( WindowEvent e )
379          { System.exit(0)  ;  }
380        }
381                        ) ;
382 
383     f.getContentPane().add("Center", w.get_panel() ) ;
384     f.getContentPane().add("South",errors) ;
385     f.pack() ;
386     f.show() ;
387   }
388 
389 }