001: package com.xoetrope.carousel.langed;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.lang.reflect.Method;
006: import java.net.MalformedURLException;
007: import java.net.URL;
008: import java.util.HashMap;
009: import java.util.Vector;
010: import java.util.prefs.Preferences;
011: import java.util.Iterator;
012: import java.util.Set;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Component;
016: import java.awt.Container;
017: import java.awt.Dimension;
018: import java.awt.Font;
019: import java.awt.Image;
020: import java.awt.Insets;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.KeyEvent;
024: import javax.swing.ImageIcon;
025: import javax.swing.JButton;
026: import javax.swing.JCheckBox;
027: import javax.swing.JComboBox;
028: import javax.swing.JComponent;
029: import javax.swing.JDialog;
030: import javax.swing.JFileChooser;
031: import javax.swing.JMenu;
032: import javax.swing.JMenuBar;
033: import javax.swing.JMenuItem;
034: import javax.swing.JOptionPane;
035: import javax.swing.JPanel;
036: import javax.swing.JSeparator;
037: import javax.swing.JToolBar;
038: import javax.swing.KeyStroke;
039: import javax.swing.SwingConstants;
040: import javax.swing.SwingUtilities;
041: import javax.swing.UIManager;
042: import javax.swing.border.Border;
043: import javax.swing.border.CompoundBorder;
044: import javax.swing.border.EmptyBorder;
045:
046: import com.xoetrope.swing.XCheckList;
047: import java.awt.Toolkit;
048: import net.xoetrope.editor.XEditorUtilities;
049: import net.xoetrope.editor.project.XEditorProject;
050: import net.xoetrope.editor.project.XEditorProjectManager;
051: import net.xoetrope.editor.project.pages.XPageResource;
052: import net.xoetrope.xui.XPage;
053: import net.xoetrope.xui.XTextHolder;
054:
055: /**
056: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
057: * the GNU Public License (GPL), please see license.txt for more details. If
058: * you make commercial use of this software you must purchase a commercial
059: * license from Xoetrope.</p>
060: * <p> $Revision: 1.17 $</p>
061: */
062: public class LangEdDesktop extends JPanel implements ActionListener {
063: protected LangEdPanel langEditor;
064: protected JToolBar toolbar;
065: protected JMenuBar mainMenuBar;
066: protected JMenu mainMenu;
067: private boolean isPlugin = false;
068: protected URL codeBase;
069: private Insets zeroInsets;
070: private JComboBox sourceList, targetList;
071:
072: JDialog aboutBox;
073: JFileChooser fileDialog;
074: AFileFilter langFileFilter;
075: Font defaultFont;
076: private static boolean exportExcel;
077:
078: private static String NEW_STRING = "1";
079: private static String IMPORT_LANG = "2";
080: private static String EXPORT_LANG = "3";
081: private static String PREV_STRING = "4";
082: private static String NEXT_STRING = "5";
083: private static String SORT_A2Z = "6";
084: private static String SORT_Z2A = "7";
085: private static String SORT_029 = "8";
086: private static String SORT_920 = "9";
087: private static String SOURCE_LANG = "A";
088: private static String TARGET_LANG = "B";
089: private static String SWAP_PREVIEW = "C";
090:
091: private XEditorProject currentProject;
092:
093: public LangEdDesktop(URL _codeBase, boolean _isPlugin,
094: Object _mainMenu, JToolBar mainToolbar) {
095: currentProject = (XEditorProject) XEditorProjectManager
096: .getCurrentProject();
097: setBorder(null);
098: codeBase = _codeBase;
099: isPlugin = _isPlugin;
100: if (_mainMenu instanceof JMenuBar)
101: mainMenuBar = (JMenuBar) _mainMenu;
102: else
103: mainMenu = (JMenu) _mainMenu;
104:
105: zeroInsets = new Insets(0, 0, 0, 0);
106: toolbar = mainToolbar;
107: if (!LangFrame.isStandalone) {
108: toolbar = new JToolBar();
109: toolbar.setFloatable(false);
110: toolbar.setRollover(true);
111: Border b = UIManager.getBorder("ToolBar.border"); // NOI18N
112: toolbar.setBorder(new CompoundBorder(b, new EmptyBorder(0,
113: 0, 0, 0)));
114: addTool("new.gif", NEW_STRING, "Add a new language string");
115: addSpacer();
116: addTool("import.gif", IMPORT_LANG, "Import a language file");
117: addTool("export.gif", EXPORT_LANG, "Export a language file");
118: addSpacer();
119: addTool("previous.gif", PREV_STRING, "Previous string (F7)");
120: addTool("next.gif", NEXT_STRING, "Next string (F8)");
121: addSpacer();
122: addTool("a2z.gif", SORT_A2Z,
123: "Sort alphabetically ascending");
124: addTool("z2a.gif", SORT_Z2A,
125: "Sort alphabetically descending");
126: addTool("_0to9.gif", SORT_029, "Sort by ID ascending");
127: addTool("_9to0.gif", SORT_920, "Sort by ID descending");
128: addSpacer();
129: }
130: addSpacer();
131: addTool("swap.gif", SWAP_PREVIEW,
132: "Swap preview of the source or target language");
133:
134: if (!isPlugin) {
135: setOpaque(false);
136:
137: ImageIcon blankImg = loadImageIcon("blank.gif", "help");
138: //-------------------------------------------------------------------
139: // File Menu setup
140: //-------------------------------------------------------------------
141: JMenu fileMenu = new JMenu("File");
142: fileMenu.setMnemonic('F');
143:
144: JMenuItem mi;
145: ImageIcon img = loadImageIcon("new.gif",
146: "Create a new set of language files");
147:
148: addMenuItem("New...", NEW_STRING, img, 'N',
149: "Create a new file", KeyStroke.getKeyStroke(
150: KeyEvent.VK_N,
151: java.awt.event.InputEvent.CTRL_MASK), this ,
152: fileMenu);
153:
154: img = loadImageIcon("open.gif",
155: "Open a set of language files");
156: addMenuItem("Open...", "Open...", img, 'O',
157: "Open a set of language files", null, this ,
158: fileMenu);
159:
160: img = loadImageIcon("import.gif",
161: "Import from ResourceBundle...");
162: addMenuItem("Import...", IMPORT_LANG, img, 'I',
163: "Import from ResourceBundle...", null, this ,
164: fileMenu);
165:
166: img = loadImageIcon("export.gif",
167: "Export ResourceBundle...");
168: addMenuItem("Export...", EXPORT_LANG, img, 'E',
169: "Import ResourceBundle...", null, this , fileMenu);
170:
171: fileMenu.addSeparator();
172:
173: img = loadImageIcon("save.gif",
174: "Save the language resources");
175: addMenuItem("Save", "Save", img, 'S',
176: "Save the language resources",
177: KeyStroke.getKeyStroke(KeyEvent.VK_S,
178: java.awt.event.InputEvent.CTRL_MASK), this ,
179: fileMenu);
180:
181: img = loadImageIcon("exit.gif", "Exit the application");
182: addMenuItem("Exit", "Exit", img, 'x',
183: "Exit the application", KeyStroke.getKeyStroke(
184: KeyEvent.VK_X,
185: java.awt.event.InputEvent.CTRL_MASK), this ,
186: fileMenu);
187: if (mainMenu != null)
188: mainMenu.add(fileMenu);
189: else
190: mainMenuBar.add(fileMenu);
191: //-------------------------------------------------------------------
192:
193: //-------------------------------------------------------------------
194: // Edit Menu setup
195: //-------------------------------------------------------------------
196: JMenu editMenu = new JMenu("Edit");
197: editMenu.setMnemonic('E');
198:
199: img = loadImageIcon("cut.gif",
200: "Cut the selected text and copy to the clipboard");
201: addMenuItem("Cut", "Cut", img, 't',
202: "Cut the selected text and copy to the clipboard",
203: KeyStroke.getKeyStroke(KeyEvent.VK_X,
204: java.awt.event.InputEvent.CTRL_MASK), this ,
205: editMenu);
206:
207: img = loadImageIcon("copy.gif",
208: "Copy the selected text to the clipboard");
209: addMenuItem("Copy", "Copy", img, 'C',
210: "Copy the selected text to the clipboard",
211: KeyStroke.getKeyStroke(KeyEvent.VK_C,
212: java.awt.event.InputEvent.CTRL_MASK), this ,
213: editMenu);
214:
215: img = loadImageIcon("paste.gif", "Paste the clipboard text");
216: addMenuItem("Paste", "Paste", img, 'P',
217: "Paste the clipboard text", KeyStroke.getKeyStroke(
218: KeyEvent.VK_V,
219: java.awt.event.InputEvent.CTRL_MASK), this ,
220: editMenu);
221:
222: editMenu.addSeparator();
223:
224: addMenuItem("Previous", PREV_STRING, blankImg, 'r',
225: "Previous string", KeyStroke.getKeyStroke(
226: KeyEvent.VK_F7,
227: java.awt.event.InputEvent.CTRL_MASK), this ,
228: editMenu);
229:
230: addMenuItem("Next", NEXT_STRING, blankImg, 'N',
231: "Next string", KeyStroke.getKeyStroke(
232: KeyEvent.VK_F8,
233: java.awt.event.InputEvent.CTRL_MASK), this ,
234: editMenu);
235:
236: editMenu.addSeparator();
237:
238: JMenu sortMenu = new JMenu("Sort order");
239: sortMenu.setMnemonic('S');
240: sortMenu.setIcon(blankImg);
241: img = loadImageIcon("a2z.gif", "Sort A..Z");
242: addMenuItem("Sort alphabetically ascending", SORT_A2Z, img,
243: 'a', "Sort alphabetically ascending", null, this ,
244: sortMenu);
245:
246: img = loadImageIcon("z2a.gif", "Sort Z..A");
247: addMenuItem("Sort alphabetically descending", SORT_Z2A,
248: img, 'd', "Sort alphabetically descending", null,
249: this , sortMenu);
250:
251: sortMenu.addSeparator();
252:
253: img = loadImageIcon("_0to9.gif", "Sort by ID ascending");
254: addMenuItem("Sort by ID ascending", SORT_029, img, 's',
255: "Sort by ID ascending", null, this , sortMenu);
256:
257: img = loadImageIcon("_9to0.gif", "Sort by ID descending");
258: addMenuItem("Sort by ID descending", SORT_920, img, 'e',
259: "Sort by ID descending", null, this , sortMenu);
260: editMenu.add(sortMenu);
261:
262: if (mainMenu != null)
263: mainMenu.add(editMenu);
264: else
265: mainMenuBar.add(editMenu);
266:
267: //-------------------------------------------------------------------
268: if (mainMenuBar != null)
269: add(mainMenuBar, BorderLayout.NORTH);
270: }
271:
272: //-------------------------------------------------------------------
273: // Database Editor setup
274: //-------------------------------------------------------------------
275: setLayout(new BorderLayout());
276: addSpacer();
277: URL url = null;
278: // try {
279: if (LangFrame.isStandalone) {
280: url = null;
281: } else {
282: try {
283: url = currentProject.getUrl("LanguageList.properties");
284:
285: if (url == null) {
286: url = currentProject.getUrl("langlist.lst");
287: }
288: } catch (Exception ex) {
289: }
290: }
291:
292: // url = LangFrame.isStandalone ? null : //new URL( "file:/C:\\Xui\\netbeans\\lang\\langlist.lst" ) :
293: // currentProject.getUrl( "langlist.lst" );
294: // }
295: // catch ( MalformedURLException ex ) {
296: // }
297: langEditor = new LangEdPanel(currentProject,
298: LangFrame.isStandalone, isPlugin, url, null,
299: addListTool(SOURCE_LANG, "Reference"), addListTool(
300: TARGET_LANG, "Translation"),
301: LangFrame.isStandalone ? mainMenuBar : null);
302: langEditor.isStandalone(LangFrame.isStandalone);
303: add(toolbar, BorderLayout.NORTH);
304: add(langEditor, BorderLayout.CENTER);
305: }
306:
307: protected static JMenuItem addMenuItem(String name, String command,
308: ImageIcon img, char mnemonic, String description,
309: KeyStroke keystroke, ActionListener listener, JMenu owner) {
310: if (LangFrame.isStandalone) {
311: JMenuItem mi = new JMenuItem(name, img);
312: mi.setMnemonic(mnemonic);
313: mi.setHorizontalTextPosition(SwingConstants.RIGHT);
314: mi.getAccessibleContext().setAccessibleDescription(
315: description);
316: mi.setAccelerator(keystroke);
317: mi.setActionCommand(command);
318: // if ( LanguageEditor.defaultFont != null )
319: // mi.setFont( LanguageEditor.defaultFont );
320: // if ( listener != null )
321: mi.addActionListener(listener);
322: owner.add(mi);
323:
324: return mi;
325: }
326: return null;
327: }
328:
329: public void actionPerformed(ActionEvent e) {
330: String cmd = ((JComponent) e.getSource()).getName();
331: if (cmd == null)
332: cmd = e.getActionCommand();
333:
334: if (cmd.compareTo(NEW_STRING) == 0)
335: newString();
336: else if (cmd.compareTo("Open...") == 0)
337: openFile();
338: else if (cmd.compareTo(IMPORT_LANG) == 0)
339: importFile();
340: else if (cmd.compareTo(EXPORT_LANG) == 0)
341: exportFile();
342: else if (cmd.compareTo(PREV_STRING) == 0)
343: langEditor.previousComment();
344: else if (cmd.compareTo(NEXT_STRING) == 0)
345: langEditor.nextComment();
346: else if (cmd.compareTo("Connect...") == 0)
347: connectToDatabase();
348: else if (cmd.compareTo("Save") == 0)
349: langEditor.saveFile(false);
350: else if (cmd.compareTo("Exit") == 0)
351: System.exit(0);
352: else if (cmd.compareTo("Cut") == 0)
353: langEditor.cut();
354: else if (cmd.compareTo("Copy") == 0)
355: langEditor.copy();
356: else if (cmd.compareTo("Paste") == 0)
357: langEditor.paste();
358: else if (cmd.compareTo(SORT_A2Z) == 0)
359: langEditor.sortA2Z();
360: else if (cmd.compareTo(SORT_Z2A) == 0)
361: langEditor.sortZ2A();
362: else if (cmd.compareTo(SORT_029) == 0)
363: langEditor.sort0to9();
364: else if (cmd.compareTo(SORT_920) == 0)
365: langEditor.sort9to0();
366: else if (cmd.compareTo(SWAP_PREVIEW) == 0)
367: langEditor.swapPreviewLanguage();
368: }
369:
370: public void openFile(String fileName) {
371: langEditor.openFile(new File(fileName));
372: }
373:
374: public String getTargetCode() {
375: return langEditor.getTargetCode();
376: }
377:
378: /**
379: * Add a new 'blank' string
380: * @return the new keyword
381: */
382: public String newString() {
383: return langEditor.newString();
384: }
385:
386: public String newString(String newText) {
387: return langEditor.newString(newText);
388: }
389:
390: public void nextComment() {
391: langEditor.nextComment();
392: }
393:
394: public void previousComment() {
395: langEditor.previousComment();
396: }
397:
398: /**
399: * Find the key that matches this text
400: * @param text the master string
401: * @return the key or null if it is not found
402: */
403: public String findKey(String text) {
404: return langEditor.findKey(text);
405: }
406:
407: /**
408: *
409: * Open the catattrb.lst file and intialize the resources.
410: *
411: */
412: public void openFile() {
413: Preferences prefs = Preferences
414: .userNodeForPackage(LangEdDesktop.class);
415: String fileName = prefs.get("lastFileName", "c:\\");
416: langEditor.saveFile(false);
417: fileDialog = new JFileChooser();
418: // LanguageEditor.setDefaultFont( fileDialog );
419:
420: langFileFilter = new AFileFilter("lst",
421: "XTranslator language lists");
422: langFileFilter.addExtension("properties");
423: langFileFilter.addExtension("xls");
424: fileDialog.addChoosableFileFilter(langFileFilter);
425: fileDialog.setFileHidingEnabled(false);
426: fileDialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
427: fileDialog.setCurrentDirectory(new File(fileName));
428:
429: int retval = fileDialog.showOpenDialog(this );
430: if (retval == JFileChooser.APPROVE_OPTION) {
431: File theFile = fileDialog.getSelectedFile();
432: if (theFile == null)
433: return;
434:
435: if (theFile.exists()) {
436: String fn = theFile.getAbsoluteFile().toString()
437: .toLowerCase();
438: if (fn.indexOf("languagelist.properties") > 0)
439: langEditor.openFile(theFile);
440: else if (fn.indexOf("properties") > 0)
441: langEditor.importFile(theFile, true);
442: else
443: langEditor.openFile(theFile);
444:
445: try {
446: prefs.put("lastFileName", theFile
447: .getCanonicalPath());
448: } catch (IOException ex) {
449: }
450: } else {
451: // should put in status panel
452: System.err.println("No such file: " + theFile);
453: }
454: } else
455: JOptionPane.showMessageDialog(this , "No file was chosen.");
456:
457: doLayout();
458: }
459:
460: public void importFile() {
461: Preferences prefs = Preferences
462: .userNodeForPackage(LangEdDesktop.class);
463: String fileName = prefs.get("lastImportName", "c:\\");
464: langEditor.saveFile(false);
465: fileDialog = new JFileChooser();
466: // LanguageEditor.setDefaultFont( fileDialog );
467:
468: langFileFilter = new AFileFilter("lst",
469: "XTranslator language lists");
470: langFileFilter.addExtension("xls");
471: fileDialog.addChoosableFileFilter(langFileFilter);
472: fileDialog.setFileHidingEnabled(false);
473: fileDialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
474: fileDialog.setCurrentDirectory(new File(fileName));
475:
476: int retval = fileDialog.showOpenDialog(this );
477: if (retval == JFileChooser.APPROVE_OPTION) {
478: File theFile = fileDialog.getSelectedFile();
479: if (theFile == null)
480: return;
481:
482: if (theFile.exists()) {
483: langEditor.importFile(theFile, false);
484: try {
485: prefs.put("lastImportName", theFile
486: .getCanonicalPath());
487: } catch (IOException ex) {
488: }
489: } else {
490: // should put in status panel
491: System.err.println("No such file: " + theFile);
492: }
493: } else
494: JOptionPane.showMessageDialog(this , "No file was chosen.");
495:
496: doLayout();
497: }
498:
499: public static String[] getSelectedLanguages(Component parent,
500: Object[] selectedLangs, boolean export) {
501: XCheckList checkList = new XCheckList(selectedLangs);
502: Object[] message = new Object[export ? 3 : 2];
503: message[0] = "Languages";
504: message[1] = checkList;
505: if (export)
506: message[2] = new JCheckBox("Export Excel Worksheet");
507:
508: String[] options = { "Cancel", "OK" };
509: int rc = 0;
510: rc = JOptionPane.showOptionDialog(parent, message,
511: "Select Languages", JOptionPane.DEFAULT_OPTION,
512: JOptionPane.INFORMATION_MESSAGE, null, options,
513: options[1]);
514: if (rc == 1) {
515: if (export)
516: exportExcel = ((JCheckBox) message[2]).isSelected();
517:
518: // Force export/import of the comments language
519: checkList.selectItem(0, true);
520: return checkList.getSelectedItems();
521: }
522:
523: return null;
524: }
525:
526: public void exportFile() {
527: Vector selectedLangs = new Vector();
528:
529: EdLangMgr elm = langEditor.getLangMgr();
530: int numLangs = elm.getNumLangs();
531: for (int i = 0; i < numLangs; i++)
532: selectedLangs.addElement(elm.getLangName(i));
533:
534: String[] languageList = getSelectedLanguages(this ,
535: selectedLangs.toArray(), true);
536: if ((languageList == null) || (languageList.length == 0))
537: return;
538: Preferences prefs = Preferences
539: .userNodeForPackage(LangEdDesktop.class);
540: String fileName = prefs.get("lastExportName", "c:\\");
541: fileDialog = new JFileChooser();
542: fileDialog
543: .setFileSelectionMode(exportExcel ? JFileChooser.FILES_AND_DIRECTORIES
544: : JFileChooser.DIRECTORIES_ONLY);
545:
546: // langFileFilter = new AFileFilter( "properties", "XTranslator language lists" );
547: // langFileFilter.addExtension( "xls" );
548: // fileDialog.addChoosableFileFilter( langFileFilter );
549: fileDialog.setFileHidingEnabled(false);
550: //fileDialog.setFileSelectionMode( JFileChooser.FILES_ONLY );
551: fileDialog.setCurrentDirectory(new File(fileName));
552:
553: int retval = fileDialog.showSaveDialog(this );
554: if (retval == JFileChooser.APPROVE_OPTION) {
555: File theFile = fileDialog.getSelectedFile();
556: if (theFile == null)
557: return;
558:
559: try {
560: theFile = new File(theFile.getCanonicalPath()
561: + (exportExcel ? "" : System
562: .getProperty("file.separator")
563: + "exportedLang.lst"));
564: langEditor.exportFile(theFile, languageList,
565: exportExcel);
566: prefs.put("lastExportName", theFile.getCanonicalPath());
567: } catch (Exception ex) {
568: }
569: } else
570: JOptionPane.showMessageDialog(this , "No file was chosen.");
571:
572: doLayout();
573: }
574:
575: public void saveFile() {
576: // if ( LanguageEditor.isEvaluation() && ( getNumStrings() > 25 ) )
577: // return;
578: langEditor.saveFile(false);
579: }
580:
581: public void newFile() {
582: fileDialog = new JFileChooser();
583: // LanguageEditor.setDefaultFont( fileDialog );
584:
585: langFileFilter = new AFileFilter("lst",
586: "XTranslator language lists");
587: fileDialog.addChoosableFileFilter(langFileFilter);
588: fileDialog.setFileHidingEnabled(false);
589: fileDialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
590:
591: int retval = fileDialog.showSaveDialog(this );
592: if (retval == JFileChooser.APPROVE_OPTION) {
593: File theFile = fileDialog.getSelectedFile();
594: if (theFile == null)
595: return;
596:
597: langEditor.newFile(theFile.toString());
598: } else
599: JOptionPane.showMessageDialog(this , "No file was chosen.");
600: doLayout();
601: }
602:
603: public void newFile(File srcFile) {
604: langEditor.newFile(srcFile.toString());
605: doLayout();
606: }
607:
608: public void setCurrentLang() {
609: langEditor.setCurrentLang();
610: }
611:
612: public void setDelimiterChar(char c, boolean isStart) {
613: langEditor.setDelimiterChar(c, isStart);
614: }
615:
616: /**
617: * Connect to the database containing the languges.
618: */
619: public void connectToDatabase() {
620: ConnParamsDlg dlg = new ConnParamsDlg();
621: dlg.pack();
622: XEditorUtilities.centreDialog(dlg);
623: dlg.setVisible(true);
624:
625: if (dlg.isOk()) {
626: try {
627: // ConnectionManager.getInstance().reset( dlg.getDriver(), dlg.getDsn(), dlg.getUserName(), dlg.getPassword() );
628: // String languagesTable = dlg.getLanguagesTable();
629: // String translationsTable = dlg.getTranslationsTable();
630: //
631: // LanguageObject.setTableName( languagesTable );
632: // TranslationObject.setTableName( translationsTable );
633: //
634: // langEditor.openFile( LanguageObject.getPrototype().getList( LanguageObject.class.getName() ),
635: // TranslationObject.getPrototype().getList( TranslationObject.class.getName() ) );
636: // dlg.dispose();
637: } catch (Exception ex) {
638: JOptionPane
639: .showMessageDialog(
640: this ,
641: "An error occured while trying to connect to the database.\r\nPlease correct the connection properties and try again",
642: "Database Connection Error",
643: JOptionPane.OK_OPTION);
644: ex.printStackTrace();
645: }
646: }
647: doLayout();
648: }
649:
650: public void isStandalone(boolean isS) {
651: langEditor.isStandalone(isS);
652: }
653:
654: public ImageIcon loadImageIcon(String filename, String description) {
655: try {
656: if (LangFrame.isStandalone) {
657: Image image = Toolkit.getDefaultToolkit().createImage(
658: getClass().getResource(
659: "/com/xoetrope/carousel/langed/"
660: + filename));
661: return new ImageIcon(image, description);
662: } else {
663: URL url;
664: try {
665: url = new URL(codeBase, filename);
666: } catch (MalformedURLException e) {
667: System.err.println("Error trying to load image "
668: + filename);
669: return null;
670: }
671: return new ImageIcon(url, description);
672: }
673: } catch (Exception ex) {
674: System.out.println("load image icon failed ");
675: return null;
676: }
677: }
678:
679: /**
680: * Show the machine language editor
681: */
682: public void runMachineTranslation() {
683: langEditor.machineTranslation();
684: }
685:
686: /**
687: * Run in live translation mode
688: */
689: public void runLiveTranslation() {
690: langEditor.liveTranslation();
691: }
692:
693: /**
694: * Add an toggle button to the toolbar.
695: * @param imageName the name of the image resource
696: * @param compId the ID of the component type
697: * @param tooltipText the text of the tooltip
698: * @return the new image component
699: */
700: private JButton addTool(String imageName, String compId,
701: String tooltipText) {
702: JButton tool = new JButton();
703: try {
704: Image image = Toolkit.getDefaultToolkit().createImage(
705: getClass().getResource(imageName));
706: if (image != null) {
707: ImageIcon icon = new ImageIcon(image);
708: tool.setIcon(icon);
709: }
710: } catch (Exception e) {
711: System.err.println("Error trying to load image "
712: + imageName);
713: }
714: tool.setSize(new Dimension(16, 16));
715: toolbar.add(tool);
716:
717: tool.addActionListener(this );
718: tool.setName(compId);
719: tool.setMargin(zeroInsets);
720: tool.setToolTipText(tooltipText);
721: return tool;
722: }
723:
724: /**
725: * Add an combo to the toolbar.
726: * @param compId the ID of the component type
727: * @param tooltipText the text of the tooltip
728: * @return the new image component
729: */
730: private JComboBox addListTool(String compId, String tooltipText) {
731: JComboBox tool = new JComboBox();
732: tool.setMinimumSize(new Dimension(10, 10));
733: tool.setPreferredSize(new Dimension(70, 16));
734: tool.setMaximumSize(new Dimension(100, 16));
735: if (LangFrame.isStandalone)
736: toolbar.add(tool);
737: else
738: toolbar.add(tool);
739:
740: tool.addActionListener(this );
741: tool.setName(compId);
742: tool.setToolTipText(tooltipText);
743: addSpacer();
744: return tool;
745: }
746:
747: private void addSpacer() {
748: JToolBar.Separator separator = new JToolBar.Separator();
749: separator.setOrientation(JSeparator.VERTICAL);
750: toolbar.add(separator);
751: }
752:
753: public boolean hasSelection() {
754: return langEditor.hasSelection();
755: }
756:
757: public void cut() {
758: langEditor.cut();
759: }
760:
761: public void copy() {
762: langEditor.copy();
763: }
764:
765: public void paste() {
766: langEditor.paste();
767: }
768:
769: public void sortA2Z() {
770: langEditor.sortA2Z();
771: }
772:
773: public void sortZ2A() {
774: langEditor.sortZ2A();
775: }
776:
777: public void sort0to9() {
778: langEditor.sort0to9();
779: }
780:
781: public void sort9to0() {
782: langEditor.sort9to0();
783: }
784:
785: private int getNumStrings() {
786: return langEditor.getNumStrings();
787: }
788:
789: /**
790: * Create a new language
791: */
792: public void newLanguage() {
793: langEditor.newLanguage();
794: }
795:
796: /**
797: * Do a spell check
798: */
799: public void checkSpelling() {
800: langEditor.checkSpelling();
801: }
802:
803: /**
804: * Setup the spell checker
805: */
806: public void setupSpelling() {
807: langEditor.setupSpelling(true);
808: }
809:
810: public void scanCurrentProject() {
811: final HashMap pages = currentProject.getAllPageResources();
812: Set keys = pages.keySet();
813:
814: // Opem the pages
815: Iterator iterator = keys.iterator();
816: while (iterator.hasNext()) {
817: String pageName = (String) iterator.next();
818: XPageResource pageResource = (XPageResource) pages
819: .get(pageName);
820: pageResource.openPage();
821: }
822:
823: // Scan the pages
824: final Iterator pageIterator = keys.iterator();
825: SwingUtilities.invokeLater(new Runnable() {
826: public void run() {
827: while (pageIterator.hasNext()) {
828: String pageName = (String) pageIterator.next();
829: XPageResource pageResource = (XPageResource) pages
830: .get(pageName);
831: XPage page = pageResource.getPage();
832: if (page != null) {
833: if (scanContainer(page))
834: pageResource.setIsModified();
835: }
836: }
837: }
838: });
839: }
840:
841: /**
842: * Scan this container for translatable strings
843: * @return true if the container has been modified
844: */
845: private boolean scanContainer(Container cont) {
846: boolean modified = false;
847: if (cont instanceof XTextHolder) {
848: XTextHolder textHolder = ((XTextHolder) cont);
849: String componentText = textHolder.getText();
850: if ((componentText != null) && (componentText.length() > 0)) {
851: String key = newString(componentText);
852: if ((key != null) && !key.equals(componentText)) {
853: textHolder.setText(key);
854: modified = true;
855: }
856: }
857: }
858:
859: int numChildren = cont.getComponentCount();
860: for (int i = 0; i < numChildren; i++) {
861: Component c = cont.getComponent(i);
862: if (c instanceof Container)
863: modified |= scanContainer((Container) c);
864: else if (c instanceof XTextHolder) {
865: XTextHolder textHolder = ((XTextHolder) c);
866: String componentText = textHolder.getText();
867: if ((componentText != null)
868: && (componentText.length() > 0)) {
869: String key = newString(componentText);
870: // Insert the key into the text component instead of the original text
871: if ((key != null) && !key.equals(componentText)) {
872: textHolder.setText(key);
873: modified = true;
874: }
875: }
876: } else {
877: // Try reflection
878: try {
879: Class[] classes = new Class[0];
880: Object[] args = new Object[0];
881: Method m = c.getClass().getMethod("getText",
882: classes);
883: String componentText = (String) m.invoke(args);
884: if ((componentText != null)
885: && (componentText.length() > 0)) {
886: String key = newString(componentText);
887: if ((key != null) && !key.equals(componentText)) {
888: classes = new Class[1];
889: args = new Object[1];
890: classes[0] = String.class;
891: args[0] = key;
892: m = c.getClass().getMethod("setText",
893: classes);
894: m.invoke(args);
895: modified = true;
896: }
897: }
898: } catch (Exception ex) {
899: ex.getCause().printStackTrace();
900: }
901: }
902: }
903: return modified;
904: }
905: }
|