xref: /AOO42X/main/l10ntools/java/receditor/java/transex3/controller/EditorController.java (revision b0efeae40e43e6d4ccd561d22ec612d42773857b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package transex3.controller;
23 
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.*;
27 import java.awt.event.WindowAdapter;
28 import java.awt.event.WindowEvent;
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.lang.Runtime;
35 import java.util.*;
36 
37 import javax.swing.ListSelectionModel;
38 import javax.swing.WindowConstants;
39 import javax.swing.event.*;
40 
41 import transex3.model.*;
42 
43 import java.io.*;
44 import javax.swing.*;
45 //import transex3.model.*;
46 public class EditorController {
47     public final String[] RESTYPES = { ".src",".hrc",".xcu",".xrm",".xhp" };
48     public final String   RECFILE  =   ".recommand";
49     // Editor View
50     static transex3.view.Editor aEditor                 = null;
51     // Editor Model
52     static Vector   sdfstrings                  = new Vector();
53     static HashMap hashedsdfstrings         = new HashMap();
54     int oldindex = 0;
55     //HashMap hashedfilenames               = new HashMap();
56     // Search for source Strings
fetchSourceStrings( String rootdir )57     public String fetchSourceStrings( String rootdir ){
58 
59         //String outputfile = "h:\\workspace\\recommandEditor\\null2";
60         File tempfile = null;
61 
62         try {
63             tempfile = File.createTempFile( "receditor" , "tmp" );
64         } catch (IOException e1) {
65             // TODO Auto-generated catch block
66             System.err.println("Can not create temp file\n");
67             e1.printStackTrace();
68         }
69 
70         String outputfile = tempfile.getAbsolutePath();
71         try
72         {
73             //System.out.println("localize_sl -QQ -skip_links -e -l en-US -f "+outputfile+" -d "+rootdir);
74             System.out.println("localize_sl -QQ -skip_links -e -l en-US -f "+outputfile );
75             java.lang.Process aProc = Runtime.getRuntime().exec("localize_sl -QQ -skip_links -e -l en-US -f "+outputfile);
76 
77             //java.lang.Process aProc = Runtime.getRuntime().exec("localize_sl -QQ -e -l en-US -f "+outputfile+" -d "+rootdir);
78             BufferedReader aBR = new BufferedReader( new InputStreamReader( aProc.getInputStream() ) );
79             String line = aBR.readLine();
80             while( line != null && line.length() > 0 ){
81                 //System.out.print( line );
82                 line = aBR.readLine();
83             }
84 
85         } catch (IOException e) {
86         // TODO Auto-generated catch block
87         e.printStackTrace();
88         }
89         return outputfile;
90     }
91     // Search for recommand files
findRecommandFiles( File rootdir , Vector list )92     public void findRecommandFiles( File rootdir , Vector list ){
93         System.out.print(".");
94         System.out.flush();
95         File[] aFileArray = rootdir.listFiles();
96         if( aFileArray != null ){
97             for( int cnt = 0; aFileArray.length > cnt ; cnt++ ){
98                 if( aFileArray[ cnt ].isDirectory() && !aFileArray[ cnt ].getAbsolutePath().endsWith(".lnk") )
99                     findRecommandFiles( aFileArray[ cnt ] , list);
100                 else if( aFileArray[ cnt ].isFile() && isRecommandFile( aFileArray[ cnt ] ) )
101                     list.add( aFileArray[ cnt ]);
102             }
103         }
104     }
isResourceType( File aFile )105     private boolean isResourceType( File aFile ){
106         String filename     = aFile.getName();
107         boolean isResType   = false;
108         for(int cnt = 0; cnt < RESTYPES.length ; cnt++){
109             if( filename.endsWith( RESTYPES[ cnt ] ) )
110                     isResType = true;
111         }
112         return isResType;
113     }
isRecommandFile( File aFile )114     private boolean isRecommandFile( File aFile ){
115         return aFile.getName().endsWith( RECFILE );
116     }
clearAllRows( JTable aTable )117     public void clearAllRows( JTable aTable ){
118         for ( int n = 0; n < aTable.getRowCount() ; n++ ){
119             aTable.setValueAt( null , n , 0 );
120             aTable.setValueAt( null , n , 1 );
121             aTable.setValueAt( null , n , 2 );
122             aTable.setValueAt( null , n , 3 );
123             aTable.setValueAt( null , n , 4 );
124         }
125     }
126     // Add all data to view
updateData()127     void updateData(){
128         JTable recTable =transex3.controller.EditorController.aEditor.getRectable();
129 
130         SdfString aSdfString = (SdfString) sdfstrings.get( oldindex );
131         Vector newStrings = new Vector();
132         for ( int n = 1; n < recTable.getRowCount() ; n++ ){
133             String lang     = (String) recTable.getValueAt(n , 0 );
134             String text     = (String) recTable.getValueAt(n , 1 );
135             String htext    = (String) recTable.getValueAt(n , 2 );
136             String qhtext   = (String) recTable.getValueAt(n , 3 );
137             String ttext    = (String) recTable.getValueAt(n , 4 );
138             if( lang != null && text != null ){
139                 //System.out.println("Data "+ lang + " " + text );
140                 SdfEntity aSdfEntity = new SdfEntity();
141                 aSdfEntity.setLangid( lang );
142                 aSdfEntity.setText(  text );
143                 aSdfEntity.setHelptext( htext );
144                 aSdfEntity.setQuickhelptext( qhtext );
145                 aSdfEntity.setTitle( ttext );
146                 newStrings.add( aSdfEntity );
147                 aSdfString.setLanguageStrings( newStrings );
148             }
149         }
150     }
151 
initView()152     public void initView(){
153         Object[][]  sourceStringData = new Object[ sdfstrings.size() ][ 4 ];
154         Object[][] firstData = new Object[100][5];
155         // Set the files
156         Iterator aIter = sdfstrings.iterator();
157         int counter = 0;
158         while( aIter.hasNext() ){
159             SdfString aSdfString = (SdfString) aIter.next();
160             sourceStringData[ counter ][ 0 ] = aSdfString.getSourceString().getProject()+"\\"+aSdfString.getSourceString().getSource_file();
161             sourceStringData[ counter ][ 1 ] = aSdfString.getSourceString().getGid();
162             sourceStringData[ counter ][ 2 ] = aSdfString.getSourceString().getLid();
163             sourceStringData[ counter ][ 3 ] = aSdfString.getSourceString().getText();
164             if( counter == 0 ){
165                 firstData[ 0 ][ 0 ] = "en-US";
166                 firstData[ 0 ][ 1 ] = aSdfString.getSourceString().getText();
167                 firstData[ 0 ][ 2 ] = aSdfString.getSourceString().getHelptext();
168                 firstData[ 0 ][ 3 ] = aSdfString.getSourceString().getQuickhelptext();
169                 firstData[ 0 ][ 4 ] = aSdfString.getSourceString().getTitle();
170                 aSdfString = (SdfString) sdfstrings.get( 0 );
171                 Vector values = aSdfString.getLanguageStrings();
172                 for( int n = 0; n < values.size() ; n++ )
173                 {
174                     SdfEntity aEntity = (SdfEntity) values.get( n );
175                     firstData[ n+1 ][ 0 ] = aEntity.getLangid();
176                     firstData[ n+1 ][ 1 ] = aEntity.getText();
177                     firstData[ n+1 ][ 2 ] = aEntity.getHelptext();
178                     firstData[ n+1 ][ 3 ] = aEntity.getQuickhelptext();
179                     firstData[ n+1 ][ 4 ] = aEntity.getTitle();
180                 }
181             }
182             counter++;
183         }
184         // Set the source srtings
185 
186 
187         //aEditor = new transex3.view.Editor( sourceStringData , filedata.toArray() );
188         aEditor = new transex3.view.Editor( sourceStringData , firstData );
189 
190         aEditor.setBounds(100,100,800,900);
191         aEditor.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
192         aEditor.setVisible(true);
193         aEditor.repaint();
194         aEditor.addWindowListener( new WindowAdapter(){
195             public void windowClosed(WindowEvent e ){
196                 System.exit( 0 );
197             }
198         });
199 
200         aEditor.getMiExit().addActionListener( new ActionListener(){
201             public void actionPerformed( ActionEvent e ){
202                 System.exit( 0 );
203             }
204         });
205 
206         aEditor.getMiSave().addActionListener( new ActionListener(){
207             public void actionPerformed( ActionEvent e ){
208                 Iterator aIter = sdfstrings.iterator();
209                 String lastFile="";
210                 while( aIter.hasNext() )
211                 {
212                     SdfString aSdfString = (SdfString )aIter.next();
213                     if( aSdfString.getFileId().compareTo( lastFile ) !=  0 ){
214                         //aSdfString.removeFile();
215                     }
216                     aSdfString.writeString();
217                     lastFile = aSdfString.getFileId();
218                 }
219             }
220         });
221 
222         //aEditor.getRectable().putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
223         aEditor.getRectable().addFocusListener( new FocusListener(){
224             public void focusLost(FocusEvent e){
225                 //super.focusLost( e );
226                 //System.out.println("focus lost");
227                 JTable aTable = aEditor.getRectable();
228                 if(  aTable.getSelectedRow() != -1 && aTable.getSelectedColumn() != -1 )
229                     aTable.getCellEditor( aTable.getSelectedRow(), aTable.getSelectedColumn() ).stopCellEditing();
230                 updateData();
231             }
232             public void focusGained( FocusEvent e){
233                 //super.focusGained( e );
234                 //System.out.println("focus gained");
235             }
236         });
237         //setDefaultEditor(Object.class, new transex3.view.FocusCellEditor(new JTextField()));
238 
239         aEditor.getRectable().getModel().addTableModelListener( new TableModelListener() {
240             public void tableChanged( TableModelEvent e ){
241                 //System.out.println( e );
242             }});
243 
244 
245         aEditor.getRectable().getSelectionModel().addListSelectionListener( new  ListSelectionListener(){
246             public void valueChanged( ListSelectionEvent e ){
247                 JTable aTable = aEditor.getRectable();
248                 //if(  aTable.getSelectedRow() != -1 && aTable.getSelectedColumn() != -1 )
249                     //aTable.getCellEditor( aTable.getSelectedRow(), aTable.getSelectedColumn() ).stopCellEditing();
250 
251                 updateData();
252             }
253         });
254 
255         aEditor.getTable().setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
256         aEditor.getTable().getSelectionModel().addListSelectionListener( new  ListSelectionListener(){
257             public void valueChanged( ListSelectionEvent e ){
258                 //System.out.println("Selected = " +e.getFirstIndex()+"\n");
259                 JTable table =transex3.controller.EditorController.aEditor.getTable();
260                 JTable recTable =transex3.controller.EditorController.aEditor.getRectable();
261                 SdfString aSdfString;
262                 JTable aTable = aEditor.getRectable();
263                 if(  aTable.getSelectedRow() != -1 && aTable.getSelectedColumn() != -1 )
264                     aTable.getCellEditor( aTable.getSelectedRow(), aTable.getSelectedColumn() ).stopCellEditing();
265 
266                 updateData();
267                 clearAllRows( recTable );
268 
269                 aSdfString = (SdfString) sdfstrings.get( table.getSelectedRow() );
270                 recTable.setValueAt( "en-US" , 0, 0 );
271                 recTable.setValueAt( aSdfString.getSourceString().getText()          , 0, 1 );
272                 recTable.setValueAt( aSdfString.getSourceString().getHelptext()      , 0, 2 );
273                 recTable.setValueAt( aSdfString.getSourceString().getQuickhelptext() , 0, 3 );
274                 recTable.setValueAt( aSdfString.getSourceString().getTitle()         , 0, 4 );
275                 Vector values = aSdfString.getLanguageStrings();
276                 for( int n = 0; n < values.size() ; n++ )
277                 {
278                     SdfEntity aEntity = (SdfEntity) values.get( n );
279                     recTable.setValueAt( aEntity.getLangid()        , n+1 , 0 );
280                     recTable.setValueAt( aEntity.getText()          , n+1 , 1 );
281                     recTable.setValueAt( aEntity.getHelptext()      , n+1 , 2 );
282                     recTable.setValueAt( aEntity.getQuickhelptext() , n+1 , 3 );
283                     recTable.setValueAt( aEntity.getTitle()         , n+1 , 4 );
284                 }
285                 oldindex = table.getSelectedRow();
286             }
287         });
288         //System.out.println("initView successfully");
289     }
initInitialStrings()290     public void initInitialStrings(){
291         String rootdir = java.lang.System.getProperty("SOLARSRC");
292         String sourcestringsfile = null;
293         Vector recList = new Vector();
294         sourcestringsfile = fetchSourceStrings( rootdir );
295         //findRecommandFiles( new File( rootdir )  , recList );
296         readStrings( sourcestringsfile , recList );
297         File sfile = new File ( sourcestringsfile );
298         sfile.delete();
299         initView();
300         aEditor.repaint();
301     }
302     // Connect recommand strings with source strings
readStrings( String sourcefiles , Vector recfiles )303     public void readStrings( String sourcefiles , Vector recfiles ) {
304         BufferedReader aBR              = null;
305         try {
306             //System.out.println("DBG: sourcefiles = " +sourcefiles);
307             aBR = new BufferedReader( new FileReader( sourcefiles ) );
308             String current = aBR.readLine();
309             SdfString aSdfString            = null;
310             SdfEntity aSdfEntity            = null;
311             while( current != null ){
312                 aSdfEntity = new SdfEntity();
313                 aSdfEntity.setProperties( current );
314                 aSdfString = new SdfString();
315                 aSdfString.addSourceString( aSdfEntity );
316                 hashedsdfstrings.put( aSdfString.getId() , aSdfString );
317                 //System.out.println("Put ID '"+aSdfString.getId()+"'");
318                 sdfstrings.add( aSdfString );
319                 current = aBR.readLine();
320 
321             }
322             Iterator aIter=recfiles.iterator();
323             File aFile;
324             BufferedReader aBR2             = null;
325             //System.out.println("Connecting strings");
326             while( aIter.hasNext() ){
327                 aFile = (File) aIter.next();
328                 aBR2 = new BufferedReader( new FileReader( aFile ) ) ;
329                 String current2 = aBR2.readLine();
330 
331                 while ( current2 != null ){
332                     SdfEntity aEntity = new SdfEntity();
333                     aEntity.setProperties( current2 );
334 
335                     if( hashedsdfstrings.containsKey( aEntity.getId() ) )
336                     {
337                         aSdfString = (SdfString) hashedsdfstrings.get( aEntity.getId() );
338                         aSdfString.addLanguageString( aEntity );
339                     }
340                     else
341                     {
342                         System.out.println("DBG: Can't find source string '"+aEntity.getId()+"'" );
343                     }
344                     current2 = aBR2.readLine();
345                 }
346             }
347         } catch (FileNotFoundException e) {
348             // TODO Auto-generated catch block
349             e.printStackTrace();
350         } catch ( IOException e){
351             e.printStackTrace();
352         }
353 
354     }
355 }
356