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

Quick Search    Search Deep

Source code: com/flexstor/common/gui/imprt/FileViewerModel.java


1   /*
2    * FileViewerModel.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.gui.imprt;
12  
13  import com.flexstor.common.exceptions.FileOperationFailedException;
14  import com.flexstor.common.io.FileAccessProtocol;
15  import com.flexstor.common.io.xfile.FlexXFile;
16  
17  public class FileViewerModel extends java.util.Observable
18                      implements ConfigFileModelI
19  {
20    protected String location         = null;
21    protected String currFile         = null; // current file : currently viewed and subject to change
22    protected FlexXFile[] array         = null;
23    public  static String SEPARATOR   = System.getProperty ("file.separator");
24  
25    protected boolean bIsCtlChanged   = false;
26  //  protected boolean bIsCtlDeleted   = false;
27  
28    public FileViewerModel( String location )
29    {
30      this.location = location;
31      parsePath ();
32    }
33    public String getInitialLocation()
34    {
35      return location;
36    }
37    public void setInitialLocation ( String loc )
38    {
39      location = loc;
40      parsePath ();
41    }
42    public void setCurrentFile( String fName )
43    {
44      currFile = fName;
45      // Notify all the observers that the file name is changed
46      // This functions is called when the user select some file name from the
47      // pull down menu in the FileViewer Dialog
48      notifyAllObservers ( new Notification ( Notification.RELOAD, Boolean.TRUE ) );
49    }
50    public String getCurrentFile ()
51    {
52      return currFile;
53    }
54    public boolean isControlFileChanged()
55    {
56      return bIsCtlChanged;
57    }
58    /*
59    public boolean isCtlDeleted()
60    {
61      return bIsCtlDeleted;
62    }
63    */
64  
65    public void setCtlChanged ( boolean bool )
66    {
67      bIsCtlChanged = bool;
68    }
69    /*
70    public void setCtlDeleted ( boolean bool )
71    {
72      bIsCtlDeleted = bool;
73    }
74    */
75  
76    public void saveFile ( String fileName, StringBuffer buffer )
77      throws FileOperationFailedException
78    {
79      if ( ( fileName != null ) &&
80           ( buffer != null ) )
81      {
82        saveFile ( fileName, buffer.toString().getBytes() );
83      }
84    }
85    public void saveFile ( String fileName, String buffer )
86     throws FileOperationFailedException
87    {
88      if ( ( fileName != null ) &&
89           ( buffer != null ) )
90      {
91        saveFile ( fileName, buffer.getBytes() );
92      }
93    }
94  
95    public void saveFile ( String fileName, byte[] buffer )
96     throws FileOperationFailedException
97    {
98        if ( ( fileName != null ) &&
99           ( buffer != null ) )
100       {
101         String   path    = location + fileName;
102         FlexXFile  xFile   = new FlexXFile ( path );
103         if ( xFile.exists() )
104         {
105           if ( !xFile.delete() )
106           {
107             FileOperationFailedException ex =
108               new FileOperationFailedException ( 6070,
109                                                   FileOperationFailedException.EXISTANCE_PERMISSIONS );
110             ex.addParameter ( xFile.getAbsolutePath() );
111             throw ex;
112           }
113         }
114         dumpBufferIntoFile ( buffer, xFile );
115         if ( ( fileName != null ) && ( fileName.endsWith (".ctl") ) )
116         {
117           bIsCtlChanged = true;
118         }
119       }
120   }
121   public void deleteFile ( String fileName )
122     throws FileOperationFailedException
123   {
124       if ( fileName != null )
125       {
126         String   path    = location + fileName;
127         FlexXFile  xFile   = new FlexXFile ( path );
128         deleteFile ( xFile );
129       }
130   }
131 
132   public void deleteFile ( FlexXFile xFile )
133     throws FileOperationFailedException
134   {
135       if ( xFile != null )
136       {
137         if ( ( xFile.exists() ) && ( xFile.isFile() )  )
138         {
139           if ( !xFile.delete() )
140           {
141             FileOperationFailedException ex =
142               new FileOperationFailedException ( 6070,
143                                                   FileOperationFailedException.EXISTANCE_PERMISSIONS );
144             ex.addParameter ( xFile.getAbsolutePath() );
145             throw ex;
146           }
147         }
148         if ( ( xFile != null ) && ( xFile.getName().endsWith (".ctl") ) )
149         {
150           bIsCtlChanged = true;
151         }
152       }
153   }
154 
155 
156   public FlexXFile[] getFileList ()
157     throws FileOperationFailedException
158   {
159 //     if ( ( array != null ) &&
160 //          ( array.length != 0 ) )  // caching later !
161 //    {
162 //       return array;
163 //     }
164 
165      if ( location == null )
166      {
167        FileOperationFailedException ex  = new FileOperationFailedException ( 6582 );
168        ex.addParameter ("<null>");
169      }
170 
171      java.util.Vector vector = new java.util.Vector();
172      FlexXFile  xFile   = new FlexXFile ( location );
173      if ( ( xFile.exists() ) && ( xFile.isDirectory() )  )
174      {
175        String[] fList = xFile.list();
176        if ( fList != null )
177        {
178         FlexXFile xF = null;
179         for ( int i=0;i<fList.length; i++ )
180         {
181           xF = new FlexXFile ( location  + fList[ i ] ) ;
182           if ( ( xF.exists() ) && ( xF.isFile() ) )
183           {
184              vector.addElement ( xF );
185           }
186         }
187        }
188      }
189      if ( vector.size() > 0 )
190      {
191        array = new FlexXFile[ vector.size() ];
192        vector.copyInto ( array );
193      }
194      return array;
195   }
196   public StringBuffer loadBufferFromFile ( String fileName )
197     throws FileOperationFailedException
198   {
199       if ( fileName == null )
200         return null;
201 
202       FlexXFile    file = getFile ( fileName );
203       return loadBufferFromFile ( file );
204   }
205   public StringBuffer loadBufferFromFile ( FlexXFile  file )
206     throws FileOperationFailedException
207 
208   {
209       if ( file == null )
210        return null;
211 
212       StringBuffer buffer = null;
213 
214       if ( file.exists() && file.isFile() )
215       {
216          try
217          {
218                   byte b[] = new byte[512]; // read 1024 byte blocks
219                   int amount;                // bytes left to read in FileInputStream
220                   // create an InputStream on the source file
221                   java.io.BufferedReader inputReader
222                            = file.getBufferedReader(512);
223 
224                   buffer   = new StringBuffer();
225                   String line = null;
226                   while ( ( line = inputReader.readLine() ) != null )
227                   {
228                     buffer.append ( line +"\n");
229                   }
230 
231                   inputReader.close();
232          }
233          catch ( java.io.IOException ioe )
234          {
235             System.out.println("ERROR while reading " + ioe);
236             return buffer;
237          }
238       }
239       return buffer;
240   }    
241 
242   public void enableInput ( boolean mode )
243   {
244     Notification notification = null;
245     if ( mode )
246       notification = new Notification ( Notification.FREE, Boolean.TRUE );
247     else
248       notification = new Notification ( Notification.BUSY, Boolean.TRUE );
249 
250     notifyAllObservers ( notification );  
251   }
252   public void update ( java.util.Observable obs, Object notification ){}
253   private FlexXFile getFile ( String fileName )
254      throws FileOperationFailedException
255   {
256     FlexXFile[] fList = getFileList();
257     if ( fList != null )
258     {
259       for ( int i=0; i<fList.length; i++ )
260       {
261          if ( fList[ i ].getName().equalsIgnoreCase( fileName.trim() ) )
262            return fList[ i ];
263       }
264     }
265     return null;
266   }
267   private void  parsePath ()
268   {
269     if ( location != null )
270     {
271       if ( !isLocalPath( location ) )
272       {
273          SEPARATOR = "/";
274       }
275       else
276       {
277          SEPARATOR = System.getProperty ("file.separator");
278       }
279       if ( !location.endsWith ( SEPARATOR ) )
280       {
281         location.concat ( SEPARATOR );
282       }
283     }
284   }
285   private void dumpBufferIntoFile ( byte[] buffer, FlexXFile dest )
286     throws FileOperationFailedException
287   {
288       if ( ( buffer == null ) || ( dest == null ) )
289         return;
290 
291        java.io.BufferedOutputStream bos;
292 
293        try
294        {
295                if ( checkDestination( dest ) )
296                {
297                   bos = dest.getBufferedOutputStream();
298                   if ( buffer.length > 0)
299                      bos.write( buffer, 0, buffer.length );
300                   bos.close();
301                }
302                else
303                {
304                     FileOperationFailedException ex = new FileOperationFailedException ( 6070, FileOperationFailedException.EXISTANCE_PERMISSIONS );
305                     ex.addParameter ( dest.getAbsolutePath() );
306                     throw ex;
307                }
308         }
309         catch ( java.io.IOException ioe )
310         {
311             System.out.println("ERROR while copying " + ioe);
312             FileOperationFailedException ex = new FileOperationFailedException ( 6070, FileOperationFailedException.EXISTANCE_PERMISSIONS );
313             ex.addParameter ( dest.getAbsolutePath() );
314             throw ex;
315         }
316         System.out.println("Copying completed succesfully" );
317   }
318   private boolean checkDestination( FlexXFile destination )
319   {
320       FlexXFile destDir = new FlexXFile( destination.getParent() );
321 
322       if ( destDir.exists() )
323       {
324          return true;
325       }
326       return false;
327   }
328   private boolean isLocalPath( String loc )
329   {
330      String[] protocols = FileAccessProtocol.getRemoteProtocolsNames ();
331      for ( int i=0; i<protocols.length; i++ )
332      {
333        if ( loc.startsWith ( protocols[ i ] ) )
334          return false;
335      }
336      return true;
337   }
338   private void notifyAllObservers ( Notification notification )
339   {
340     setChanged();
341     notifyObservers( notification );
342     clearChanged();
343   }
344 }
345