001: /*
002: * To change this template, choose Tools | Templates
003: * and open the template in the editor.
004: */
005:
006: package com.xoetrope.carousel.langed;
007:
008: import com.xoetrope.swing.list.XAltListCellRenderer;
009: import java.awt.Color;
010: import java.awt.Component;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.util.ArrayList;
014: import javax.swing.JList;
015: import javax.swing.JScrollPane;
016: import javax.swing.event.ListSelectionEvent;
017: import javax.swing.event.ListSelectionListener;
018: import net.xoetrope.editor.XEditorDefaults;
019: import net.xoetrope.editor.project.XEditorProject;
020: import net.xoetrope.swing.XButton;
021: import net.xoetrope.swing.XLabel;
022: import net.xoetrope.swing.XList;
023: import net.xoetrope.swing.XPanel;
024: import net.xoetrope.swing.XTextArea;
025:
026: /**
027: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
028: * the GNU Public License (GPL), please see license.txt for more details. If
029: * you make commercial use of this software you must purchase a commercial
030: * license from Xoetrope.</p>
031: */
032: public class TranslationPopUp extends XPanel implements ActionListener,
033: ListSelectionListener {
034: private XEditorProject editorProject;
035: private XList stringList;
036: private XTextArea transEdit, hintsEdit;
037: private XLabel simpLabel, nameLabel, textLabel, transLabel,
038: hintLabel;
039: private XButton okBtn, cancelBtn;
040: private TranslationPane transPane;
041: private EdLangMgr langMgr;
042: private EdLanguage currentLang, transLang;
043: private ArrayList storeHints, storeLangs, storeKeys;
044: private String text;
045: private boolean blockListener = false;;
046:
047: /**
048: * Creates a new instance of the translation
049: * @param project <CODE>XEditorProject</CODE> specifying the current project
050: * @param tp the <CODE>TranslationPane</CODE> this coponent will be used within
051: */
052: public TranslationPopUp(XEditorProject project, TranslationPane tp) {
053: editorProject = project;
054: transPane = tp;
055: storeHints = new ArrayList();
056: storeLangs = new ArrayList();
057: storeKeys = new ArrayList();
058: setLayout(null);
059: setOpaque(false);
060: setSize(265, 215);
061:
062: setAttribute("border", "1");
063: setAttribute("arc", "15");
064: setBackground(new Color(246, 246, 246, 200));
065:
066: stringList = new XList();
067: transEdit = new XTextArea();
068: hintsEdit = new XTextArea();
069: simpLabel = new XLabel();
070: nameLabel = new XLabel();
071: textLabel = new XLabel();
072: transLabel = new XLabel();
073: hintLabel = new XLabel();
074: okBtn = new XButton();
075: cancelBtn = new XButton();
076:
077: stringList.setCellRenderer(new StringListRenderer());
078: stringList.addListSelectionListener(this );
079: okBtn.addActionListener(this );
080: cancelBtn.addActionListener(this );
081:
082: stringList.setBackground(Color.white);
083:
084: transEdit.setFont(XEditorDefaults.defaultFont);
085: hintsEdit.setFont(XEditorDefaults.defaultFont);
086: simpLabel.setFont(XEditorDefaults.defaultFont);
087: nameLabel.setFont(XEditorDefaults.defaultFont);
088: textLabel.setFont(XEditorDefaults.defaultFont);
089: transLabel.setFont(XEditorDefaults.defaultFont);
090: hintLabel.setFont(XEditorDefaults.defaultFont);
091: stringList.setFont(XEditorDefaults.defaultFont);
092: okBtn.setFont(XEditorDefaults.defaultFont);
093: cancelBtn.setFont(XEditorDefaults.defaultFont);
094:
095: setAttribute("translucent", "true");
096: simpLabel.setAttribute(project, "antialias", "true");
097: nameLabel.setAttribute(project, "antialias", "true");
098: textLabel.setAttribute(project, "antialias", "true");
099: transLabel.setAttribute(project, "antialias", "true");
100: hintLabel.setAttribute(project, "antialias", "true");
101:
102: transEdit.setEditable(true);
103: hintsEdit.setEditable(false);
104: transEdit.setLineWrap(true);
105: hintsEdit.setLineWrap(true);
106: transEdit.setWrapStyleWord(true);
107: hintsEdit.setWrapStyleWord(true);
108:
109: simpLabel.setBounds(8, 3, 130, 20);
110: nameLabel.setBounds(130, 3, 127, 20);
111: transLabel.setBounds(5, 25, 58, 20);
112: transEdit.setBounds(67, 25, 190, 50);
113: textLabel.setBounds(5, 80, 58, 20);
114: stringList.setBounds(67, 80, 190, 50);
115: hintLabel.setBounds(5, 134, 58, 20);
116: hintsEdit.setBounds(67, 134, 190, 50);
117:
118: okBtn.setBounds(67, 190, 94, 20);
119: cancelBtn.setBounds(163, 190, 94, 20);
120:
121: nameLabel.setAlignment((int) XLabel.RIGHT_ALIGNMENT);
122: textLabel.setAlignment((int) XLabel.RIGHT_ALIGNMENT);
123: transLabel.setAlignment((int) XLabel.RIGHT_ALIGNMENT);
124: hintLabel.setAlignment((int) XLabel.RIGHT_ALIGNMENT);
125:
126: transLabel.setText("Translation:");
127: textLabel.setText("Strings:");
128: hintLabel.setText("Hint:");
129: okBtn.setText("OK");
130: cancelBtn.setText("Cancel");
131:
132: add(simpLabel);
133: add(nameLabel);
134:
135: JScrollPane scrollPane1 = new JScrollPane(transEdit);
136: JScrollPane scrollPane2 = new JScrollPane(stringList);
137: JScrollPane scrollPane3 = new JScrollPane(hintsEdit);
138:
139: scrollPane1.setBounds(67, 25, 190, 50);
140: scrollPane2.setBounds(67, 80, 190, 50);
141: scrollPane3.setBounds(67, 135, 190, 50);
142:
143: add(transLabel);
144: add(scrollPane1);
145: add(hintLabel);
146: add(scrollPane2);
147: add(textLabel);
148: add(scrollPane3);
149: add(okBtn);
150: add(cancelBtn);
151:
152: langMgr = (EdLangMgr) project.getObject("LanguageManager");
153: updateCurrentLang(langMgr.getCurrentLang());
154: updateTransLang(langMgr.getCurrentLang());
155: }
156:
157: /**
158: * Set the displayed information for the selected component
159: * @param simpleName the components simple name
160: * @param name the components name
161: */
162: public void setInfoItems(String simpleName, String name) {
163: simpLabel.setText(simpleName);
164: nameLabel.setText(name);
165: }
166:
167: /**
168: * Erase all contents within the text fields
169: */
170: public void resetTextFields() {
171: transEdit.setText("");
172: hintsEdit.setText("");
173: stringList.requestFocusInWindow();
174: }
175:
176: /**
177: * Update the translation language
178: * @param lang the new translation language
179: */
180: public void updateTransLang(EdLanguage lang) {
181: transLang = lang;
182: }
183:
184: /**
185: * Update the current language and display matches within the current language
186: * @param lang the new current language
187: */
188: public void updateCurrentLang(EdLanguage lang) {
189: currentLang = lang;
190:
191: blockListener = true;
192: stringList.removeAll();
193: storeHints.clear();
194: storeLangs.clear();
195: storeKeys.clear();
196:
197: boolean found = false;
198: EdLanguage hints = getLanguage("ht");
199: int numStrings = lang.getNumStrings();
200:
201: for (int j = 0; j < numStrings; j++) {
202: String s = lang.getStringAt(j);
203: if (s.equals(text)) {
204: currentLang = lang;
205: LangItem item = (LangItem) currentLang.getElementAt(j);
206: String key = item.keyStr;
207: if (!storeKeys.contains(key)) {
208: stringList.addItem(s);
209: storeKeys.add(key);
210:
211: if (hints != null) {
212: String h = hints.getString(hints
213: .findString(key));
214: storeHints.add(h.equals("") ? "<no hint>" : h);
215: } else {
216: storeHints.add("<no hint>");
217: }
218:
219: storeLangs.add(currentLang.getLangName());
220: found = true;
221: }
222: }
223: }
224:
225: if (found == false) {
226: stringList.addItem("<no match>");
227: storeHints.add("<no hint>");
228: storeLangs.add(null);
229: }
230: blockListener = false;
231:
232: stringList.setSelectedIndex(0);
233: }
234:
235: /**
236: * Show matching strings from all languages within the strings list
237: * @param str the components text <CODE>String</CODE>
238: * @return the <CODE>String</CODE> specifying the selected current language
239: */
240: public String showMatchingStrings(String str) {
241: text = str;
242:
243: blockListener = true;
244: stringList.removeAll();
245: storeHints.clear();
246: storeLangs.clear();
247: storeKeys.clear();
248:
249: boolean found = false;
250: int numLangs = langMgr.getNumLangs();
251: EdLanguage hints = getLanguage("ht");
252: int strCount = 0;
253:
254: for (int i = 0; i < numLangs; i++) {
255: EdLanguage lang = langMgr.getLang(i);
256: String code = lang.getLangCode().toLowerCase();
257: int numStrings = lang.getNumStrings();
258: for (int j = 0; j < numStrings; j++) {
259: String s = lang.getStringAt(j);
260: if (s.equals(text)) {
261: currentLang = lang;
262: LangItem item = (LangItem) currentLang
263: .getElementAt(j);
264: String key = item.keyStr;
265: if (!storeKeys.contains(key)) {
266: stringList.addItem(s);
267: storeKeys.add(key);
268:
269: if (hints != null) {
270: String h = hints.getString(hints
271: .findString(key));
272: storeHints.add(h.equals("") ? "<no hint>"
273: : h);
274: } else {
275: storeHints.add("<no hint>");
276: }
277:
278: storeLangs.add(currentLang.getLangName());
279: found = true;
280: }
281: }
282: }
283: }
284:
285: if (found == false) {
286: stringList.addItem("<no match>");
287: storeHints.add("<no hint>");
288: storeLangs.add(null);
289: }
290: blockListener = false;
291:
292: stringList.setSelectedIndex(0);
293:
294: return (String) storeLangs.get(0);
295: }
296:
297: /**
298: * Listens for button pressed events on the 'OK' and 'Cancel' buttons
299: * @param e the fired <CODE>ActionEvent</CODE>
300: */
301: public void actionPerformed(ActionEvent e) {
302: if (e.getSource() == okBtn) {
303: addNewString(transEdit.getText());
304: }
305:
306: setVisible(false);
307: transPane.resetSelectedComp();
308: transPane.repaint();
309: }
310:
311: /**
312: * Add a new string to the translation language
313: * @param str the new <CODE>String</CODE>
314: */
315: public void addNewString(String str) {
316: String sel = (String) stringList.getSelectedObject();
317: EdLanguage refLang = getLanguage("reference");
318:
319: String key = "";
320: for (int i = 0; i < currentLang.getNumStrings(); i++) {
321: LangItem item = (LangItem) currentLang.getElementAt(i);
322: if (item.langStr.equals(sel)) {
323: key = item.keyStr;
324: }
325: }
326:
327: int stringId = refLang.findString(key);
328: if (stringId < 0)
329: stringId = refLang.addString(key, key);
330:
331: transLang.addString(stringId, key, str);
332: }
333:
334: /**
335: * Get a language based on the id string
336: * @param id <CODE>String</CODE> specifying the language
337: * @return the matching <CODE>EdLanguage</CODE> instance
338: */
339: public EdLanguage getLanguage(String id) {
340: int numLangs = langMgr.getNumLangs();
341: for (int i = 0; i < numLangs; i++) {
342: EdLanguage lang = langMgr.getLangFromList(i);
343: if (lang.getLangCode().toLowerCase().equals(
344: id.toLowerCase())) {
345: return lang;
346: }
347: }
348: return null;
349: }
350:
351: /**
352: * Set the focus on the translation text area
353: */
354: public void setTransFocus() {
355: transEdit.requestFocusInWindow();
356: }
357:
358: /**
359: * Listens for list selections
360: * @param e the fired <CODE>ListSelectionEvent</CODE>
361: */
362: public void valueChanged(ListSelectionEvent e) {
363: if (!blockListener && (storeHints.size() > 0)) {
364: int selIndex = stringList.getSelectedIndex();
365: transEdit.setText((String) stringList.getSelectedObject());
366: hintsEdit.setText((String) storeHints.get(selIndex));
367:
368: transEdit.setCaretPosition(0);
369: hintsEdit.setCaretPosition(0);
370:
371: Object o = storeLangs.get(selIndex);
372: if (o != null) {
373: transPane.setSelectedCurrentLang((String) o);
374: }
375: }
376: }
377:
378: /**
379: * Custom list renderer which disables html text rendering
380: */
381: private class StringListRenderer extends XAltListCellRenderer {
382: public StringListRenderer() {
383: }
384:
385: public Component getListCellRendererComponent(JList list,
386: Object value, int index, boolean isSelected,
387: boolean cellHasFocus) {
388: putClientProperty("html.disable", Boolean.TRUE);
389: return super.getListCellRendererComponent(list, value,
390: index, isSelected, cellHasFocus);
391: }
392: }
393: }
|