Source Code Cross Referenced for TranslatorsController.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » plugins » i18n » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.plugins.i18n 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.squirrel_sql.plugins.i18n;
002:
003:        import net.sourceforge.squirrel_sql.client.IApplication;
004:        import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
005:        import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
006:        import net.sourceforge.squirrel_sql.fw.util.StringManager;
007:        import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
008:        import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
009:        import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
010:
011:        import javax.swing.*;
012:        import java.awt.event.*;
013:        import java.io.File;
014:        import java.io.IOException;
015:        import java.net.URL;
016:        import java.net.URLClassLoader;
017:        import java.util.*;
018:        import java.util.prefs.Preferences;
019:        import java.util.zip.ZipEntry;
020:        import java.util.zip.ZipFile;
021:
022:        public class TranslatorsController {
023:            private static final StringManager s_stringMgr = StringManagerFactory
024:                    .getStringManager(TranslatorsController.class);
025:
026:            private static ILogger logger = LoggerController
027:                    .createLogger(TranslatorsController.class);
028:
029:            private static final String PREF_KEY_WORK_DIR = "SquirrelSQL.i18n.workDir";
030:            private static final String PREF_KEY_EDITOR_COMMAND = "SquirrelSQL.i18n.editorCommand";
031:            private static final String PREF_KEY_SELECTED_LOCALE = "SquirrelSQL.i18n.selectedLocale";
032:            private static final String PREF_KEY_NATIVE2ASCII_COMMAND = "SquirrelSQL.i18n.native2AsciiCommand";
033:            private static final String PREF_KEY_NATIVE2ASCII_OUT_DIR = "SquirrelSQL.i18n.native2AsciiOutDir";
034:            private static final String PREF_KEY_INCLUDE_TIMESTAMP = "SquirrelSQL.i18n.includeTimestamp";
035:
036:            TranslatorsPanel _panel;
037:            private IApplication _app;
038:            private BundlesTableModel _bundlesTableModel;
039:
040:            private JPopupMenu _popUp = new JPopupMenu();
041:
042:            // i18n[I18n.generateTemplateComments=Generate template comments for missing translations]
043:            private JMenuItem _mnuGenerateTemplateComments = new JMenuItem(
044:                    s_stringMgr.getString("I18n.generateTemplateComments"));
045:
046:            // i18n[I18n.openIOnEditor=Open in Editor]
047:            private JMenuItem _mnuOpenInEditor = new JMenuItem(s_stringMgr
048:                    .getString("I18n.openIOnEditor"));
049:
050:            // i18n[I18n.ExecuteNativeToAscii=Execute nativeToAscii]
051:            private JMenuItem _mnuExecuteNativeToAscii = new JMenuItem(
052:                    s_stringMgr.getString("I18n.ExecuteNativeToAscii"));
053:
054:            TranslatorsController(TranslatorsPanel panel) {
055:                _panel = panel;
056:
057:                _bundlesTableModel = new BundlesTableModel();
058:
059:                _panel.tblBundels.setModel(_bundlesTableModel);
060:
061:                _panel.cbxIncludeTimestamp
062:                        .addActionListener(new ActionListener() {
063:                            public void actionPerformed(ActionEvent e) {
064:                                String includeTimestamp = Boolean
065:                                        .valueOf(
066:                                                _panel.cbxIncludeTimestamp
067:                                                        .isSelected())
068:                                        .toString();
069:                                Preferences.userRoot().put(
070:                                        PREF_KEY_INCLUDE_TIMESTAMP,
071:                                        includeTimestamp);
072:                            }
073:                        });
074:
075:                _panel.btnChooseWorkDir.addActionListener(new ActionListener() {
076:                    public void actionPerformed(ActionEvent e) {
077:                        onChooseWorkDir();
078:                    }
079:                });
080:
081:                _panel.cboLocales.addItemListener(new ItemListener() {
082:                    public void itemStateChanged(ItemEvent e) {
083:                        onLocaleChanged(e);
084:                    }
085:                });
086:
087:                _panel.btnChooseEditorCommand
088:                        .addActionListener(new ActionListener() {
089:                            public void actionPerformed(ActionEvent e) {
090:                                onChooseEditorCommand();
091:                            }
092:                        });
093:
094:                _panel.btnChooseNativeToAsciiCommand
095:                        .addActionListener(new ActionListener() {
096:                            public void actionPerformed(ActionEvent e) {
097:                                onChooseNativeToAsciiCommand();
098:                            }
099:                        });
100:
101:                _panel.btnChooseNativeToAsciiOutDir
102:                        .addActionListener(new ActionListener() {
103:                            public void actionPerformed(ActionEvent e) {
104:                                onChooseNativeToAsciiOutDir();
105:                            }
106:                        });
107:
108:                _panel.tblBundels.addMouseListener(new MouseAdapter() {
109:                    public void mousePressed(MouseEvent e) {
110:                        maybeShowPopup(e);
111:                    }
112:
113:                    public void mouseReleased(MouseEvent e) {
114:                        maybeShowPopup(e);
115:                    }
116:                });
117:
118:                _popUp.add(_mnuGenerateTemplateComments);
119:                _popUp.add(_mnuOpenInEditor);
120:                _popUp.add(_mnuExecuteNativeToAscii);
121:
122:                _mnuGenerateTemplateComments
123:                        .addActionListener(new ActionListener() {
124:                            public void actionPerformed(ActionEvent e) {
125:                                onGenerate();
126:                            }
127:                        });
128:
129:                _mnuOpenInEditor.addActionListener(new ActionListener() {
130:                    public void actionPerformed(ActionEvent e) {
131:                        onOpenInEditor();
132:                    }
133:                });
134:
135:                _mnuExecuteNativeToAscii
136:                        .addActionListener(new ActionListener() {
137:                            public void actionPerformed(ActionEvent e) {
138:                                onExecuteNativeToAscii();
139:                            }
140:                        });
141:
142:                Locale[] availableLocales = Locale.getAvailableLocales();
143:
144:                Arrays.sort(availableLocales, new Comparator<Locale>() {
145:                    public int compare(Locale o1, Locale o2) {
146:                        return o1.toString().compareTo(o2.toString());
147:                    }
148:                });
149:
150:                Locale selectedLocale = Locale.getDefault();
151:                String prefLocale = Preferences.userRoot().get(
152:                        PREF_KEY_SELECTED_LOCALE, null);
153:
154:                for (int i = 0; i < availableLocales.length; i++) {
155:                    _panel.cboLocales.addItem(availableLocales[i]);
156:
157:                    if (availableLocales[i].toString().equals(prefLocale)) {
158:                        selectedLocale = availableLocales[i];
159:                    }
160:                }
161:                _panel.cboLocales.setSelectedItem(selectedLocale);
162:
163:                _panel.btnLoad.addActionListener(new ActionListener() {
164:                    public void actionPerformed(ActionEvent e) {
165:                        onLoadBundels(_app);
166:                    }
167:                });
168:
169:                String workDir = Preferences.userRoot().get(PREF_KEY_WORK_DIR,
170:                        null);
171:                _panel.txtWorkingDir.setText(workDir);
172:
173:                String editorCommand = Preferences.userRoot().get(
174:                        PREF_KEY_EDITOR_COMMAND, null);
175:                _panel.txtEditorCommand.setText(editorCommand);
176:
177:                String nativeToAsciiCommand = Preferences.userRoot().get(
178:                        PREF_KEY_NATIVE2ASCII_COMMAND, null);
179:                _panel.txtNativeToAsciiCommand.setText(nativeToAsciiCommand);
180:
181:                String nativeToAsciiOutDir = Preferences.userRoot().get(
182:                        PREF_KEY_NATIVE2ASCII_OUT_DIR, null);
183:                _panel.txtNativeToAsciiOutDir.setText(nativeToAsciiOutDir);
184:
185:            }
186:
187:            private void onChooseWorkDir() {
188:                JFileChooser chooser = new JFileChooser(System.getProperties()
189:                        .getProperty("user.home"));
190:                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
191:                chooser.showOpenDialog(_app.getMainFrame());
192:
193:                if (null != chooser.getSelectedFile()) {
194:                    _panel.txtWorkingDir.setText(chooser.getSelectedFile()
195:                            .getPath());
196:                }
197:            }
198:
199:            private void onLocaleChanged(ItemEvent e) {
200:                if (ItemEvent.SELECTED == e.getStateChange()) {
201:                    _bundlesTableModel.setBundles(new I18nBundle[0]);
202:                }
203:            }
204:
205:            private void onChooseEditorCommand() {
206:                JFileChooser chooser = new JFileChooser(System.getProperties()
207:                        .getProperty("user.home"));
208:                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
209:                chooser.showOpenDialog(_app.getMainFrame());
210:
211:                if (null != chooser.getSelectedFile()) {
212:                    _panel.txtEditorCommand.setText(chooser.getSelectedFile()
213:                            .getPath());
214:                }
215:            }
216:
217:            private void onChooseNativeToAsciiCommand() {
218:                JFileChooser chooser = new JFileChooser(System.getProperties()
219:                        .getProperty("user.home"));
220:                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
221:                chooser.showOpenDialog(_app.getMainFrame());
222:
223:                if (null != chooser.getSelectedFile()) {
224:                    _panel.txtNativeToAsciiCommand.setText(chooser
225:                            .getSelectedFile().getPath());
226:                }
227:            }
228:
229:            private void onChooseNativeToAsciiOutDir() {
230:                JFileChooser chooser = new JFileChooser(System.getProperties()
231:                        .getProperty("user.home"));
232:                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
233:                chooser.showOpenDialog(_app.getMainFrame());
234:
235:                if (null != chooser.getSelectedFile()) {
236:                    _panel.txtNativeToAsciiOutDir.setText(chooser
237:                            .getSelectedFile().getPath());
238:                }
239:            }
240:
241:            private void onExecuteNativeToAscii() {
242:                String nativeToAsciiCommmand = _panel.txtNativeToAsciiCommand
243:                        .getText();
244:
245:                if (null == nativeToAsciiCommmand
246:                        || 0 == nativeToAsciiCommmand.length()) {
247:                    // i18n[i18n.noNativeToAsciiCommand=Cannot convert files without a native2Ascii command.]
248:                    String msg = s_stringMgr
249:                            .getString("i18n.noNativeToAsciiCommand");
250:                    JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
251:                    return;
252:                }
253:
254:                String nativeToAsciiOutDir = _panel.txtNativeToAsciiOutDir
255:                        .getText();
256:
257:                if (null == nativeToAsciiOutDir
258:                        || 0 == nativeToAsciiOutDir.length()) {
259:                    // i18n[i18n.noNativeToAsciiOutDir=Cannot convert files without a native2Ascii output dir.]
260:                    String msg = s_stringMgr
261:                            .getString("i18n.noNativeToAsciiOutDir");
262:                    JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
263:                    return;
264:                }
265:
266:                File outDir = new File(nativeToAsciiOutDir);
267:                outDir.mkdirs();
268:
269:                if (false == outDir.isDirectory()) {
270:                    // i18n[i18n.noNativeToAsciiOutDirNoDir=native2Ascii output dir is not a directory. native2Ascii will not be executed.]
271:                    String msg = s_stringMgr
272:                            .getString("i18n.noNativeToAsciiOutDirNoDir");
273:                    JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
274:                    return;
275:                }
276:
277:                executeCommand(nativeToAsciiCommmand, true, outDir);
278:            }
279:
280:            private void onOpenInEditor() {
281:
282:                String editorCommmand = _panel.txtEditorCommand.getText();
283:
284:                if (null == editorCommmand || 0 == editorCommmand.length()) {
285:                    String msg = s_stringMgr.getString("i18n.noEditorCommand");
286:                    // i18n[i18n.noEditorCommand=Can not open files withou an editor command.]
287:                    JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
288:                    return;
289:                }
290:
291:                executeCommand(editorCommmand, false, null);
292:            }
293:
294:            private void executeCommand(String command, boolean nativeToAscii,
295:                    File native2AsciiOutDir) {
296:                File workDir = getWorkDir(true);
297:
298:                int[] selRows = _panel.tblBundels.getSelectedRows();
299:                I18nBundle[] selBundles = _bundlesTableModel
300:                        .getBundlesForRows(selRows);
301:
302:                if (0 == selBundles.length) {
303:                    return;
304:                }
305:
306:                boolean filesFound = false;
307:
308:                ArrayList<String> commands = new ArrayList<String>();
309:                for (int i = 0; i < selBundles.length; i++) {
310:                    File f = selBundles[i].getPathRelativeTo(workDir);
311:
312:                    if (f.exists()) {
313:                        if (nativeToAscii) {
314:                            File outFile = selBundles[i]
315:                                    .getPathRelativeTo(native2AsciiOutDir);
316:                            outFile.getParentFile().mkdirs();
317:                            commands.add(command + " " + f.getPath() + " "
318:                                    + outFile.getPath());
319:                        } else {
320:                            if (0 == commands.size()) {
321:                                commands.add(command);
322:                            }
323:                            commands
324:                                    .add(commands.remove(0) + " " + f.getPath());
325:                        }
326:
327:                        filesFound = true;
328:                    } else {
329:                        // i18n[i18n.notGeneratedInWorkDir=File {0} has not been generated. Cannot continue.]
330:                        String msg = s_stringMgr.getString(
331:                                "i18n.notGeneratedInWorkDir", f.getPath());
332:                        _app.getMessageHandler().showMessage(msg);
333:                    }
334:                }
335:
336:                if (false == filesFound) {
337:                    // i18n[i18n.noFilesOpened=No file found.\nSee message panel for details.]
338:                    String msg = s_stringMgr.getString("i18n.noFilesOpened");
339:                    JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
340:                    return;
341:                }
342:
343:                try {
344:                    for (int i = 0; i < commands.size(); i++) {
345:                        // i18n[i18n.executingCommand=Executing command: {0}]
346:                        String msg = s_stringMgr.getString(
347:                                "i18n.executingCommand", commands.get(i));
348:                        _app.getMessageHandler().showMessage(msg);
349:
350:                        Runtime.getRuntime().exec(commands.get(i));
351:                    }
352:                } catch (IOException e) {
353:                    // i18n[i18n.executingCommandFailed=Execution failed with error: {0}]
354:                    String msg = s_stringMgr.getString(
355:                            "i18n.executingCommandFailed", e.getMessage());
356:                    _app.getMessageHandler().showMessage(msg);
357:                    throw new RuntimeException(e);
358:
359:                }
360:            }
361:
362:            public void uninitialize() {
363:                Preferences.userRoot().put(PREF_KEY_SELECTED_LOCALE,
364:                        "" + _panel.cboLocales.getSelectedItem());
365:                Preferences.userRoot().put(PREF_KEY_WORK_DIR,
366:                        _panel.txtWorkingDir.getText());
367:                Preferences.userRoot().put(PREF_KEY_EDITOR_COMMAND,
368:                        _panel.txtEditorCommand.getText());
369:                Preferences.userRoot().put(PREF_KEY_NATIVE2ASCII_COMMAND,
370:                        _panel.txtNativeToAsciiCommand.getText());
371:                Preferences.userRoot().put(PREF_KEY_NATIVE2ASCII_OUT_DIR,
372:                        _panel.txtNativeToAsciiOutDir.getText());
373:            }
374:
375:            private void maybeShowPopup(MouseEvent e) {
376:                if (e.isPopupTrigger()) {
377:                    _popUp.show(e.getComponent(), e.getX(), e.getY());
378:                }
379:            }
380:
381:            private void onGenerate() {
382:                File workDir = getWorkDir(true);
383:
384:                if (null == workDir) {
385:                    return;
386:                }
387:
388:                int[] selRows = _panel.tblBundels.getSelectedRows();
389:                I18nBundle[] selBundles = _bundlesTableModel
390:                        .getBundlesForRows(selRows);
391:
392:                for (int i = 0; i < selBundles.length; i++) {
393:                    selBundles[i].writeMissingProps(_app, workDir);
394:                }
395:            }
396:
397:            /**
398:             * Checks if the workdir is a valid directory name and create the directory
399:             * if it doesn't exist.
400:             *
401:             * @return null if to directory name is not valid
402:             */
403:            private File getWorkDir(boolean withMessages) {
404:                String buf = _panel.txtWorkingDir.getText();
405:                if (null == buf || 0 == buf.trim().length()) {
406:
407:                    if (withMessages) {
408:                        String msg = s_stringMgr.getString("I18n.NoWorkDir");
409:                        // i18n[I18n.NoWorkDir=Please choose a work dir to store your translations.]
410:                        JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
411:                    }
412:                    return null;
413:
414:                }
415:
416:                File workDir = new File(buf);
417:                if (false == workDir.isDirectory()) {
418:                    if (withMessages) {
419:                        String msg = s_stringMgr.getString(
420:                                "I18n.WorkDirIsNotADirectory", workDir
421:                                        .getPath());
422:                        // i18n[I18n.WorkDirIsNotADirectory=Working directory {0} is not a directory]
423:                        JOptionPane.showMessageDialog(_app.getMainFrame(), msg);
424:                    }
425:                    return null;
426:                }
427:
428:                if (false == workDir.exists()) {
429:                    String msg = s_stringMgr.getString(
430:                            "I18n.WorkDirDoesNotExistQuestionCreate", workDir
431:                                    .getPath());
432:                    // i18n[I18n.WorkDirDoesNotExistQuestionCreate=Working directory {0} does not exist.\nDo you want to create it?]
433:
434:                    if (withMessages) {
435:                        if (JOptionPane.YES_OPTION == JOptionPane
436:                                .showConfirmDialog(_app.getMainFrame(), msg)) {
437:                            if (false == workDir.mkdirs()) {
438:                                msg = s_stringMgr.getString(
439:                                        "I18n.CouldNotCreateWorkDir", workDir
440:                                                .getPath());
441:                                // i18n[I18n.CouldNotCreateWorkDir=Could not create Working directory {0}]
442:                                JOptionPane.showMessageDialog(_app
443:                                        .getMainFrame(), msg);
444:                                return null;
445:
446:                            }
447:                        }
448:                    } else {
449:                        return null;
450:                    }
451:                }
452:
453:                return workDir;
454:
455:            }
456:
457:            private void onLoadBundels(IApplication app) {
458:                Locale selLocale = (Locale) _panel.cboLocales.getSelectedItem();
459:
460:                File workDir = null;
461:                if (null != _panel.txtWorkingDir.getText()
462:                        && 0 < _panel.txtWorkingDir.getText().trim().length()) {
463:                    workDir = new File(_panel.txtWorkingDir.getText());
464:                    if (false == workDir.exists()) {
465:                        String msg = s_stringMgr.getString("I18n.noWorkdir");
466:                        // i18n[I18n.noWorkdir=Working directory doesn't exist.\nDo you want to create it?]
467:                        if (JOptionPane.YES_OPTION == JOptionPane
468:                                .showConfirmDialog(app.getMainFrame(), msg)) {
469:                            workDir.mkdirs();
470:                        }
471:                    } else if (false == workDir.isDirectory()) {
472:                        String msg = s_stringMgr.getString(
473:                                "I18n.WorkdirIsNoDir", workDir
474:                                        .getAbsolutePath());
475:                        // i18n[I18n.WorkdirIsNoDir=The working directory is not a directory.\nNo bundles will be loaded from {0}]
476:                        JOptionPane.showMessageDialog(app.getMainFrame(), msg);
477:
478:                    }
479:                } else {
480:                    String msg = s_stringMgr
481:                            .getString("I18n.noWorkdirSpecified");
482:                    // i18n[I18n.noWorkdirSpecified=No working directory specified.]
483:                    JOptionPane.showMessageDialog(app.getMainFrame(), msg);
484:                }
485:
486:                URL[] sourceUrls = getUrlsToLoadI18nPropertiesFrom(workDir);
487:
488:                String pluginDir = new ApplicationFiles().getPluginsDirectory()
489:                        .getPath();
490:
491:                ArrayList<I18nProps> defaultI18nProps = new ArrayList<I18nProps>();
492:                ArrayList<I18nProps> localizedI18nProps = new ArrayList<I18nProps>();
493:
494:                for (int i = 0; i < sourceUrls.length; i++) {
495:                    File file = new File(sourceUrls[i].getFile().replaceAll(
496:                            "%20", " "));
497:
498:                    if (file.isDirectory()) {
499:                        findI18nInDir(selLocale, file, defaultI18nProps,
500:                                localizedI18nProps, sourceUrls);
501:                    } else if (file.getName().equalsIgnoreCase(
502:                            "squirrel-sql.jar")
503:                            || file.getName().equalsIgnoreCase("fw.jar")) {
504:                        findI18nInArchive(selLocale, file, defaultI18nProps,
505:                                localizedI18nProps, sourceUrls);
506:                    } else if (file.getPath().startsWith(pluginDir)) {
507:                        findI18nInArchive(selLocale, file, defaultI18nProps,
508:                                localizedI18nProps, sourceUrls);
509:                    }
510:                }
511:
512:                Hashtable<String, I18nBundle> i18nBundlesByName = new Hashtable<String, I18nBundle>();
513:
514:                for (int i = 0; i < defaultI18nProps.size(); i++) {
515:                    I18nProps i18nProps = defaultI18nProps.get(i);
516:                    I18nBundle pack = new I18nBundle(i18nProps, selLocale,
517:                            getWorkDir(false), sourceUrls);
518:                    i18nBundlesByName.put(i18nProps.getPath(), pack);
519:                }
520:
521:                for (int i = 0; i < localizedI18nProps.size(); i++) {
522:                    I18nProps locI18nProps = localizedI18nProps.get(i);
523:                    String key = locI18nProps.getUnlocalizedPath(selLocale);
524:
525:                    I18nBundle bundle = i18nBundlesByName.get(key);
526:                    if (null != bundle) {
527:                        bundle.setLocalizedProp(locI18nProps);
528:                    }
529:                }
530:
531:                I18nBundle[] bundles = i18nBundlesByName.values().toArray(
532:                        new I18nBundle[0]);
533:
534:                int[] selRows = _panel.tblBundels.getSelectedRows();
535:                _bundlesTableModel.setBundles(bundles);
536:
537:                for (int i = 0; i < selRows.length; i++) {
538:                    _panel.tblBundels.getSelectionModel().addSelectionInterval(
539:                            selRows[i], selRows[i]);
540:                }
541:
542:            }
543:
544:            private URL[] getUrlsToLoadI18nPropertiesFrom(File workDir) {
545:                ApplicationFiles af = new ApplicationFiles();
546:
547:                ArrayList<URL> ret = new ArrayList<URL>();
548:                URL[] urls;
549:
550:                urls = ((URLClassLoader) _app.getClass().getClassLoader())
551:                        .getURLs();
552:
553:                for (int i = 0; i < urls.length; i++) {
554:                    File file = new File(urls[i].getFile().replaceAll("%20",
555:                            " "));
556:                    if (file.getName()
557:                            .equals(af.getSQuirrelJarFile().getName())) {
558:                        ret.add(urls[i]);
559:                    } else if (file.getName().equals(
560:                            af.getFwJarFile().getName())) {
561:                        ret.add(urls[i]);
562:                    }
563:                }
564:
565:                PluginInfo[] pi = _app.getPluginManager()
566:                        .getPluginInformation();
567:
568:                urls = _app.getPluginManager().getPluginURLs();
569:                for (int i = 0; i < urls.length; i++) {
570:                    String jarName = new File(urls[i].getFile()).getName();
571:
572:                    String cleanJarName;
573:                    if (jarName.endsWith(".jar")) {
574:                        cleanJarName = jarName.substring(0, jarName.length()
575:                                - ".jar".length());
576:                    } else {
577:                        continue;
578:                    }
579:
580:                    for (int j = 0; j < pi.length; j++) {
581:                        if (pi[j].getInternalName().equalsIgnoreCase(
582:                                cleanJarName)) {
583:                            ret.add(urls[i]);
584:                        }
585:                    }
586:                }
587:
588:                return ret.toArray(new URL[ret.size()]);
589:            }
590:
591:            private void findI18nInArchive(Locale selLoc, File file,
592:                    ArrayList<I18nProps> defaultI18nProps,
593:                    ArrayList<I18nProps> localizedI18nProps, URL[] sourceUrls) {
594:                try {
595:                    ZipFile zf = new ZipFile(file);
596:
597:                    for (Enumeration e = zf.entries(); e.hasMoreElements();) {
598:                        ZipEntry entry = (ZipEntry) e.nextElement();
599:
600:                        if (entry.getName().endsWith(".properties")) {
601:                            Locale loc = I18nProps
602:                                    .parseLocaleFromPropsFileName(entry
603:                                            .getName());
604:
605:                            if (null == loc) {
606:                                defaultI18nProps.add(new I18nProps(file, entry
607:                                        .getName(), sourceUrls));
608:                            }
609:                            if (selLoc.equals(loc)) {
610:                                localizedI18nProps.add(new I18nProps(file,
611:                                        entry.getName(), sourceUrls));
612:                            }
613:                        }
614:                    }
615:
616:                } catch (IOException e) {
617:                    // i18n[I18n.failedToOpenZip=Failed to open zip/jar {0}]
618:                    String msg = s_stringMgr.getString("I18n.failedToOpenZip",
619:                            file.getAbsolutePath());
620:                    _app.getMessageHandler().showMessage(msg);
621:                    logger.error(msg, e);
622:                }
623:
624:            }
625:
626:            private void findI18nInDir(Locale selLoc, File dir,
627:                    ArrayList<I18nProps> defaultI18nProps,
628:                    ArrayList<I18nProps> localizedI18nProps, URL[] sourceUrls) {
629:                File[] files = dir.listFiles();
630:
631:                for (int i = 0; i < files.length; i++) {
632:                    if (files[i].isDirectory()) {
633:                        findI18nInDir(selLoc, files[i], defaultI18nProps,
634:                                localizedI18nProps, sourceUrls);
635:                    } else if (files[i].getName().endsWith(".properties")) {
636:                        Locale loc = I18nProps
637:                                .parseLocaleFromPropsFileName(files[i]
638:                                        .getName());
639:
640:                        if (null == loc) {
641:                            defaultI18nProps.add(new I18nProps(files[i],
642:                                    sourceUrls));
643:                        }
644:                        if (selLoc.equals(loc)) {
645:                            localizedI18nProps.add(new I18nProps(files[i],
646:                                    sourceUrls));
647:                        }
648:                    }
649:                }
650:
651:            }
652:
653:            public void initialize(IApplication app) {
654:                _app = app;
655:            }
656:
657:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.