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

Quick Search    Search Deep

Source code: com/yaftp/utils/OsEditorPanePanel.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.utils;
21  
22  import java.io.* ;
23  import javax.swing.* ;
24  
25  
26  /**
27  
28    we implement here a way to be able to start Operating System
29    dependent text editors like (Emacs,Xemacs,Ultraedit32) to manipulate
30    transfered files
31  
32    @Author Jean-Yves MENGANT
33    CopyRights Jean-Yves MENGANT 1998,1999,2000
34  
35  */
36  
37  
38  public class OsEditorPanePanel
39  implements SwingTextEditors
40  {
41    private final static String _WKFNAME_ = "YaFtpWORK" ;
42    private final static String _DOT_HTML_ = ".html"  ;
43    private final static String _DOT_HTM_ = ".htm"  ;
44  
45    /** name of the Editor program to launch  + parms */
46    private String _browsingEnvironmentString ;
47    private String _editorEnvironmentString ;
48    private String _workDirectory   ;
49    private int    _type       ;
50    private String _providedFName ;
51  
52    /** set the full path name of the os program to be launched when editing */
53    public void set_editorEnvironmentString( String environmentString )
54    { _editorEnvironmentString = environmentString ; }
55  
56    /** set the full path name of the os program to be launched when browsing */
57    public void set_browsingEnvironmentString( String environmentString )
58    { _browsingEnvironmentString = environmentString ; }
59  
60    public void set_workDirectory( String workDirectory )
61    { _workDirectory = workDirectory ; }
62  
63    /** we use it here to build a file suffix to work file */
64    public void setEditorKit( int type )
65    { _type = type ; }
66  
67    /** give a chance to caller to provide a waork file name */
68    public void set_providedFName( String providedFName )
69    { _providedFName = providedFName ; }
70  
71    /** provide a way to setup
72  
73    /**
74      as external text editor launcher, we do not provide a PANEL
75      able to be viewed inside the YaFtp internal frame so we return
76      null here.
77    */
78    public  JPanel get_panel()
79    { return null ; }
80  
81  
82    /**
83      store a temporary file which will contain the text or byte array  to be externally
84      edited
85    */
86    private File buildWkFile( Object object )
87    throws UtilsError
88    {
89      if ( _providedFName == null ) // if not set by user
90        _providedFName = _WKFNAME_ ;
91  
92      // external browser consider non .htm or .html files as
93      // text
94      if ( _type == SwingTextEditors.HTML )
95      {
96        if ( ( ! _providedFName.equalsIgnoreCase(_DOT_HTML_) ) &&
97             ( ! _providedFName.equalsIgnoreCase(_DOT_HTM_) )
98           )
99          _providedFName = _providedFName + _DOT_HTML_ ;
100     }
101 
102     File myWkFile = new File( _workDirectory , _providedFName ) ;
103 
104     try {
105       byte wk[] ;
106       if ( object instanceof String )
107         wk = ((String)object).getBytes() ;
108       else if ( object instanceof byte[] )
109         wk = (byte[]) object  ;
110       else
111         throw new UtilsError("unsupported data type : " + object.getClass().getName()) ;
112 
113       BufferedOutputStream out = new BufferedOutputStream(
114                                  new FileOutputStream( myWkFile )
115                                                          )  ;
116       out.write(wk);
117       out.close() ;
118 
119       return myWkFile ;
120     } catch ( IOException e )
121     {
122       throw new UtilsError( "IOERROR on workFile : " +
123                             myWkFile.getAbsoluteFile() +" : " + e.getMessage()
124                           ) ;
125     }
126   }
127 
128   class WL
129   extends Thread
130   {
131     InputStream _out ;
132     InputStream _err ;
133     public WL( InputStream out , InputStream err )
134     {
135       _out = out ;
136       _err = err ;
137     }
138 
139     public void run()
140     {
141     int c ;
142       try {
143         while ( ( c = _out.read() ) != -1 )
144           System.out.print((char) c) ;
145 
146       System.out.println("end of print" ) ;
147       _out.close() ;
148 
149       while ( ( c = _err.read() ) != -1 )
150         System.out.print((char) c) ;
151       _err.close() ;
152       }  catch( IOException e )
153       { e.printStackTrace() ; }
154     }
155   }
156 
157   /**
158     the load method just launches an instance of the external editor,
159     used to bring object up to screen.
160     This method is able to deal with binary , string and File objects
161 
162     @param object to be manipulated by utility may either be a File , a String or a byte[]
163   */
164   public void load ( Object object )
165   throws UtilsError
166   {
167   File myWkFile ;
168     if ( object instanceof File ) // allready here
169       myWkFile = (File) object ;
170     else // or build an intermediate work
171       myWkFile = buildWkFile ( object ) ;
172 
173     ProcessLauncher launcher = new ProcessLauncher()  ;
174     // assume that editors and browsers accept file to be edited as last argument
175     String argString = _editorEnvironmentString + " "  + myWkFile ;
176 
177     if ( _type == SwingTextEditors.HTML )
178       argString = _browsingEnvironmentString + " "  + myWkFile ;
179 
180     launcher.setCommand(argString);
181     InputStream stdOut = launcher.getStdout() ;
182     InputStream stdErr = launcher.getStderr() ;
183     WL reader = new WL(launcher.getStdout(), launcher.getStderr()) ;
184     reader.start() ;
185   }
186 
187   public OsEditorPanePanel() {}
188 
189 
190   public static void main( String args[] )
191   {
192     System.out.println("testing OsEditorPanePanel in stand alone mode") ;
193     OsEditorPanePanel osEditor = new OsEditorPanePanel() ;
194     osEditor.setEditorKit( SwingTextEditors.HTML ) ;
195     osEditor.set_editorEnvironmentString("f:/ultraedit/uedit32.exe") ;
196     osEditor.set_browsingEnvironmentString("\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\"") ;
197     osEditor.set_workDirectory("d:/tmp") ;
198     try {
199       osEditor.load("<h1>Hello world</h1>") ;
200     }
201     catch ( UtilsError e )
202     { System.out.println(e.getMessage() ) ; }
203 
204 
205   }
206 
207 }