1 /*
2  *  FreeGuide J2
3  *
4  *  Copyright (c) 2001-2004 by Andy Balaam and the FreeGuide contributors
5  *
6  *  freeguide-tv.sourceforge.net
7  *
8  *  Released under the GNU General Public License
9  *  with ABSOLUTELY NO WARRANTY.
10  *
11  *  See the file COPYING for more information.
12  */
13 
14 /*
15  *  parseCommand() method Copyright (c) by Slava Pestov
16  *
17  *  from the Jedit project: www.jedit.org
18  *
19  */
20 package freeguide.common.lib.general;
21 
22 import freeguide.common.lib.fgspecific.Application;
23 
24 import java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.Point;
27 import java.awt.Toolkit;
28 import java.awt.Window;
29 
30 import java.io.StreamTokenizer;
31 import java.io.StringReader;
32 
33 import java.util.Vector;
34 import java.util.logging.Level;
35 
36 /**
37  * Some static global methods used in various parts of FreeGuide.
38  *
39  * @author Andy Balaam
40  * @version 3
41  */
42 public class Utils
43 {
44     // ----------------------------------------------
45     private final static char dosSlash = 127;
46 
47     /**
48      * DOCUMENT_ME!
49      *
50      * @param cmd DOCUMENT_ME!
51      */
execNoWait( String cmd )52     public static void execNoWait( String cmd )
53     {
54         Application.getInstance(  ).getLogger(  )
55                    .info( "Execute system command: " + cmd );
56 
57         try
58         {
59             // Parse the command into arguments and execute
60             Runtime.getRuntime(  ).exec( parseCommand( cmd ) );
61 
62         }
63 
64         catch( java.io.IOException ex )
65         {
66             Application.getInstance(  ).getLogger(  )
67                        .log(
68                 Level.WARNING, "Error execute command : " + cmd, ex );
69         }
70     }
71 
72     /**
73      * Convert a command into an array of arguments. Adapted from a
74      * method written by Slava Pestov for the JEdit project www.jedit.org
75      * Thanks Slava!
76      *
77      * @param command Description of the Parameter
78      *
79      * @return Description of the Return Value
80      */
parseCommand( String command )81     public static String[] parseCommand( String command )
82     {
83         Vector args = new Vector(  );
84 
85         String[] ans;
86 
87         // We replace \ with a non-printable char because
88         // StreamTokenizer handles \ specially, which causes
89         // problems on Windows as \ is the file separator
90         // there.
91         // After parsing is done, the non printable char is
92         // changed to \ once again.
93         // StreamTokenizer needs a way to disable backslash
94         // handling...
95         command = command.replace( '\\', dosSlash );
96 
97         StreamTokenizer st =
98             new StreamTokenizer( new StringReader( command ) );
99 
100         st.resetSyntax(  );
101 
102         st.wordChars( '!', 255 );
103 
104         st.whitespaceChars( 0, ' ' );
105 
106         st.quoteChar( '"' );
107 
108         st.quoteChar( '\'' );
109 
110         try
111         {
112 loop:
113 
114             while( true )
115             {
116                 switch( st.nextToken(  ) )
117                 {
118                 case StreamTokenizer.TT_EOF:
119                     break loop;
120 
121                 case StreamTokenizer.TT_WORD:
122                 case '"':
123 
124                 case '\'':
125                     args.addElement( st.sval.replace( dosSlash, '\\' ) );
126 
127                     break;
128                 }
129             }
130         }
131 
132         catch( java.io.IOException io )
133         {
134             // won't happen
135         }
136 
137         ans = new String[args.size(  )];
138 
139         args.copyInto( ans );
140 
141         return ans;
142 
143     }
144 
145     // --------------------------------------------------------------------
146     /**
147      * In each of the string in str, replace any occurences of oldStr
148      * with newStr.
149      *
150      * @param str Description of the Parameter
151      * @param oldStr Description of the Parameter
152      * @param newStr Description of the Parameter
153      *
154      * @return Description of the Return Value
155      */
substitute( String[] str, String oldStr, String newStr )156     public static String[] substitute(
157         String[] str, String oldStr, String newStr )
158     {
159         String[] ans = new String[str.length];
160 
161         // Go through each string we're processing
162         for( int i = 0; i < str.length; i++ )
163         {
164             // Copy it into the output array
165             ans[i] = str[i];
166 
167             // Find the first occurence of the string to be replaced
168             int j = ans[i].indexOf( oldStr );
169 
170             int k;
171 
172             // Keep replacing until there are no more.
173             while( j != -1 )
174             {
175                 k = j + oldStr.length(  );
176 
177                 if( k < ans[i].length(  ) )
178                 {
179                     ans[i] = ans[i].substring( 0, j ) + newStr
180                         + ans[i].substring( k );
181 
182                 }
183 
184                 else
185                 {
186                     ans[i] = ans[i].substring( 0, j ) + newStr;
187 
188                 }
189 
190                 j = ans[i].indexOf( oldStr );
191 
192             }
193         }
194 
195         return ans;
196 
197     }
198 
199     /**
200      * Move window to centre of parent window.
201      *
202      * @param parent parent window
203      * @param dialog window to move
204      */
centreDialog( final Component parent, final Window dialog )205     public static void centreDialog(
206         final Component parent, final Window dialog )
207     {
208         Dimension thisSize = parent.getSize(  );
209 
210         Dimension dialogSize = dialog.getSize(  );
211 
212         Point thisLocation = parent.getLocation(  );
213 
214         dialog.setLocation(
215             thisLocation.x + ( ( thisSize.width - dialogSize.width ) / 2 ),
216             thisLocation.y + ( ( thisSize.height - dialogSize.height ) / 2 ) );
217     }
218 
219     /**
220      * Move window to centre of screen.
221      *
222      * @param dialog window to move
223      */
centreDialog( final Window dialog )224     public static void centreDialog( final Window dialog )
225     {
226         Dimension screenSize = Toolkit.getDefaultToolkit(  ).getScreenSize(  );
227 
228         Dimension dialogSize = dialog.getSize(  );
229 
230         dialog.setLocation(
231             ( screenSize.width - dialogSize.width ) / 2,
232             ( screenSize.height - dialogSize.height ) / 2 );
233     }
234 }
235