12e5b6d6dSopenharmony_ci/*
22e5b6d6dSopenharmony_ci *****************************************************************************
32e5b6d6dSopenharmony_ci * Copyright (C) 2000-2004, International Business Machines Corporation and  *
42e5b6d6dSopenharmony_ci * others. All Rights Reserved.                                              *
52e5b6d6dSopenharmony_ci *****************************************************************************
62e5b6d6dSopenharmony_ci */
72e5b6d6dSopenharmony_cipackage com.ibm.rbm.gui;
82e5b6d6dSopenharmony_ci
92e5b6d6dSopenharmony_ciimport java.awt.*;
102e5b6d6dSopenharmony_ciimport java.awt.event.*;
112e5b6d6dSopenharmony_ci
122e5b6d6dSopenharmony_ciimport javax.swing.*;
132e5b6d6dSopenharmony_ciimport javax.swing.table.*;
142e5b6d6dSopenharmony_ci
152e5b6d6dSopenharmony_ciimport com.ibm.rbm.*;
162e5b6d6dSopenharmony_ci
172e5b6d6dSopenharmony_ci/**
182e5b6d6dSopenharmony_ci * The class used to display untranslated items
192e5b6d6dSopenharmony_ci */
202e5b6d6dSopenharmony_ciclass RBUntranslatedPanel extends JPanel {
212e5b6d6dSopenharmony_ci	RBManager rbm;
222e5b6d6dSopenharmony_ci	Bundle bundle;
232e5b6d6dSopenharmony_ci	RBManagerGUI listener;
242e5b6d6dSopenharmony_ci
252e5b6d6dSopenharmony_ci	// Components - Bundle
262e5b6d6dSopenharmony_ci	JLabel                      jLabelUntransTitle;
272e5b6d6dSopenharmony_ci	UntranslatedItemsTableModel untransTableModel;
282e5b6d6dSopenharmony_ci	JTable                      jTableUntrans;
292e5b6d6dSopenharmony_ci	JScrollPane                 jScrollPaneUntransTable;
302e5b6d6dSopenharmony_ci
312e5b6d6dSopenharmony_ci	// Components - Bundle Manager
322e5b6d6dSopenharmony_ci	Box                         mainBox;
332e5b6d6dSopenharmony_ci	JPanel                      mainPanels[];
342e5b6d6dSopenharmony_ci	JLabel                      numUntransLabels[];
352e5b6d6dSopenharmony_ci	JScrollPane                 mainScroll;
362e5b6d6dSopenharmony_ci	JScrollPane                 listScrolls[];
372e5b6d6dSopenharmony_ci	JList                       untransLists[];
382e5b6d6dSopenharmony_ci
392e5b6d6dSopenharmony_ci	public RBUntranslatedPanel(RBManagerGUI gui) {
402e5b6d6dSopenharmony_ci		super();
412e5b6d6dSopenharmony_ci		listener = gui;
422e5b6d6dSopenharmony_ci	}
432e5b6d6dSopenharmony_ci
442e5b6d6dSopenharmony_ci	public void setBundle(Bundle b) {
452e5b6d6dSopenharmony_ci		rbm = null;
462e5b6d6dSopenharmony_ci		if (bundle == null) {
472e5b6d6dSopenharmony_ci			bundle = b;
482e5b6d6dSopenharmony_ci			initComponents();
492e5b6d6dSopenharmony_ci		} else if (bundle != b) {
502e5b6d6dSopenharmony_ci			bundle = b;
512e5b6d6dSopenharmony_ci			updateComponents();
522e5b6d6dSopenharmony_ci		}
532e5b6d6dSopenharmony_ci	}
542e5b6d6dSopenharmony_ci
552e5b6d6dSopenharmony_ci	public void setManager(RBManager m) {
562e5b6d6dSopenharmony_ci		bundle = null;
572e5b6d6dSopenharmony_ci		if (rbm == null) {
582e5b6d6dSopenharmony_ci			rbm = m;
592e5b6d6dSopenharmony_ci			initComponents();
602e5b6d6dSopenharmony_ci		} else if (rbm != m) {
612e5b6d6dSopenharmony_ci			rbm = m;
622e5b6d6dSopenharmony_ci			updateComponents();
632e5b6d6dSopenharmony_ci		}
642e5b6d6dSopenharmony_ci	}
652e5b6d6dSopenharmony_ci
662e5b6d6dSopenharmony_ci	public void removeElements() {
672e5b6d6dSopenharmony_ci		if (rbm != null || bundle != null) {
682e5b6d6dSopenharmony_ci			rbm = null;
692e5b6d6dSopenharmony_ci			bundle = null;
702e5b6d6dSopenharmony_ci			initComponents();
712e5b6d6dSopenharmony_ci		}
722e5b6d6dSopenharmony_ci	}
732e5b6d6dSopenharmony_ci
742e5b6d6dSopenharmony_ci	// Marks the selected resource as translated and removes from this view
752e5b6d6dSopenharmony_ci	private void markSelectedResourceAsTranslated() {
762e5b6d6dSopenharmony_ci		if (bundle == null) return;
772e5b6d6dSopenharmony_ci		if (jTableUntrans.getSelectedRow() < 0) return;
782e5b6d6dSopenharmony_ci		if (jTableUntrans.getModel() instanceof UntranslatedItemsTableModel) {
792e5b6d6dSopenharmony_ci			int row = jTableUntrans.getSelectedRow();
802e5b6d6dSopenharmony_ci			UntranslatedItemsTableModel model = (UntranslatedItemsTableModel)jTableUntrans.getModel();
812e5b6d6dSopenharmony_ci			BundleItem item = model.getBundleItem(row);
822e5b6d6dSopenharmony_ci			item.setTranslated(true);
832e5b6d6dSopenharmony_ci			model.update();
842e5b6d6dSopenharmony_ci		}
852e5b6d6dSopenharmony_ci	}
862e5b6d6dSopenharmony_ci
872e5b6d6dSopenharmony_ci	// Removes the selected resource from the resource file
882e5b6d6dSopenharmony_ci	private void deleteSelectedResource() {
892e5b6d6dSopenharmony_ci		if (bundle == null) return;
902e5b6d6dSopenharmony_ci		if (jTableUntrans.getSelectedRow() < 0) return;
912e5b6d6dSopenharmony_ci		if (jTableUntrans.getModel() instanceof UntranslatedItemsTableModel) {
922e5b6d6dSopenharmony_ci			int row = jTableUntrans.getSelectedRow();
932e5b6d6dSopenharmony_ci			UntranslatedItemsTableModel model = (UntranslatedItemsTableModel)jTableUntrans.getModel();
942e5b6d6dSopenharmony_ci			BundleItem item = model.getBundleItem(row);
952e5b6d6dSopenharmony_ci			if (item.getParentGroup() != null && item.getParentGroup().getParentBundle() != null) {
962e5b6d6dSopenharmony_ci				Bundle bundle = item.getParentGroup().getParentBundle();
972e5b6d6dSopenharmony_ci				bundle.removeItem(item.getKey());
982e5b6d6dSopenharmony_ci			}
992e5b6d6dSopenharmony_ci			model.update();
1002e5b6d6dSopenharmony_ci		}
1012e5b6d6dSopenharmony_ci	}
1022e5b6d6dSopenharmony_ci
1032e5b6d6dSopenharmony_ci	private void printTable() {
1042e5b6d6dSopenharmony_ci		PrintJob pjob = getToolkit().getPrintJob(new Frame(),
1052e5b6d6dSopenharmony_ci                           "Printing Test", null);
1062e5b6d6dSopenharmony_ci
1072e5b6d6dSopenharmony_ci        if (pjob != null) {
1082e5b6d6dSopenharmony_ci            Graphics pg = pjob.getGraphics();
1092e5b6d6dSopenharmony_ci
1102e5b6d6dSopenharmony_ci            if (pg != null) {
1112e5b6d6dSopenharmony_ci                //jTableUntrans.print(pg);
1122e5b6d6dSopenharmony_ci                Dimension page_dim = pjob.getPageDimension();
1132e5b6d6dSopenharmony_ci				pg.setColor(Color.black);
1142e5b6d6dSopenharmony_ci				int y_off = 50;
1152e5b6d6dSopenharmony_ci				int x_off = 30;
1162e5b6d6dSopenharmony_ci				TableModel model = jTableUntrans.getModel();
1172e5b6d6dSopenharmony_ci				pg.setFont(new Font("SansSerif", Font.BOLD, 14));
1182e5b6d6dSopenharmony_ci				pg.drawString("Untranslated Items:       Page 1", x_off, y_off);
1192e5b6d6dSopenharmony_ci				pg.setFont(new Font("SansSerif", Font.PLAIN, 10));
1202e5b6d6dSopenharmony_ci
1212e5b6d6dSopenharmony_ci				for (int i=0 ; i < model.getRowCount(); i++) {
1222e5b6d6dSopenharmony_ci					if (y_off < page_dim.height - 50) {
1232e5b6d6dSopenharmony_ci						y_off += 15;
1242e5b6d6dSopenharmony_ci						String key = model.getValueAt(i, 0).toString();
1252e5b6d6dSopenharmony_ci						String translation = model.getValueAt(i,1).toString();
1262e5b6d6dSopenharmony_ci						pg.drawString(key + " -> " + translation, x_off, y_off);
1272e5b6d6dSopenharmony_ci					}
1282e5b6d6dSopenharmony_ci				}
1292e5b6d6dSopenharmony_ci				pg.dispose(); // flush page
1302e5b6d6dSopenharmony_ci            }
1312e5b6d6dSopenharmony_ci            pjob.end();
1322e5b6d6dSopenharmony_ci
1332e5b6d6dSopenharmony_ci        }
1342e5b6d6dSopenharmony_ci	}
1352e5b6d6dSopenharmony_ci
1362e5b6d6dSopenharmony_ci	public void initComponents() {
1372e5b6d6dSopenharmony_ci		// Initialize components
1382e5b6d6dSopenharmony_ci		if (bundle != null) {
1392e5b6d6dSopenharmony_ci			jLabelUntransTitle        = new JLabel(bundle.name);
1402e5b6d6dSopenharmony_ci			untransTableModel         = new UntranslatedItemsTableModel(bundle);
1412e5b6d6dSopenharmony_ci			jTableUntrans             = new JTable(untransTableModel);
1422e5b6d6dSopenharmony_ci			jScrollPaneUntransTable   = new JScrollPane(jTableUntrans);
1432e5b6d6dSopenharmony_ci
1442e5b6d6dSopenharmony_ci			// Lower panel components
1452e5b6d6dSopenharmony_ci			JPanel  lowerPanel = new JPanel();
1462e5b6d6dSopenharmony_ci			JButton deleteButton = new JButton(Resources.getTranslation("button_delete_resource"));
1472e5b6d6dSopenharmony_ci			JButton translateButton = new JButton(Resources.getTranslation("button_mark_translated"));
1482e5b6d6dSopenharmony_ci			JButton printButton = new JButton(Resources.getTranslation("button_print_table"));
1492e5b6d6dSopenharmony_ci
1502e5b6d6dSopenharmony_ci			deleteButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_delete_resource_trigger")));
1512e5b6d6dSopenharmony_ci			translateButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_mark_translated_trigger")));
1522e5b6d6dSopenharmony_ci			lowerPanel.setBorder(BorderFactory.createTitledBorder(Resources.getTranslation("languageuntrans_selected_resources_options")));
1532e5b6d6dSopenharmony_ci			lowerPanel.setLayout(new GridLayout(1,2));
1542e5b6d6dSopenharmony_ci
1552e5b6d6dSopenharmony_ci			jTableUntrans.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1562e5b6d6dSopenharmony_ci			jTableUntrans.addMouseListener(listener);
1572e5b6d6dSopenharmony_ci
1582e5b6d6dSopenharmony_ci			jLabelUntransTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
1592e5b6d6dSopenharmony_ci
1602e5b6d6dSopenharmony_ci			// Add action listeners
1612e5b6d6dSopenharmony_ci			deleteButton.addActionListener(new ActionListener() {
1622e5b6d6dSopenharmony_ci				public void actionPerformed(ActionEvent ev) {
1632e5b6d6dSopenharmony_ci					deleteSelectedResource();
1642e5b6d6dSopenharmony_ci				}
1652e5b6d6dSopenharmony_ci			});
1662e5b6d6dSopenharmony_ci			translateButton.addActionListener(new ActionListener() {
1672e5b6d6dSopenharmony_ci				public void actionPerformed(ActionEvent ev) {
1682e5b6d6dSopenharmony_ci					markSelectedResourceAsTranslated();
1692e5b6d6dSopenharmony_ci				}
1702e5b6d6dSopenharmony_ci			});
1712e5b6d6dSopenharmony_ci			printButton.addActionListener(new ActionListener() {
1722e5b6d6dSopenharmony_ci				public void actionPerformed(ActionEvent ev) {
1732e5b6d6dSopenharmony_ci					printTable();
1742e5b6d6dSopenharmony_ci				}
1752e5b6d6dSopenharmony_ci			});
1762e5b6d6dSopenharmony_ci
1772e5b6d6dSopenharmony_ci			removeAll();
1782e5b6d6dSopenharmony_ci			setLayout(new BorderLayout());
1792e5b6d6dSopenharmony_ci			lowerPanel.add(deleteButton);
1802e5b6d6dSopenharmony_ci			lowerPanel.add(translateButton);
1812e5b6d6dSopenharmony_ci			//lowerPanel.add(printButton);
1822e5b6d6dSopenharmony_ci			add(jLabelUntransTitle, BorderLayout.NORTH);
1832e5b6d6dSopenharmony_ci			add(jScrollPaneUntransTable, BorderLayout.CENTER);
1842e5b6d6dSopenharmony_ci			add(lowerPanel, BorderLayout.SOUTH);
1852e5b6d6dSopenharmony_ci		} else if (rbm != null) {
1862e5b6d6dSopenharmony_ci
1872e5b6d6dSopenharmony_ci			int langCount = 0;       // The number of languages with untranslated Items
1882e5b6d6dSopenharmony_ci			for (int i=0; i < rbm.getBundles().size(); i++) {
1892e5b6d6dSopenharmony_ci				Bundle bundle = (Bundle)rbm.getBundles().elementAt(i);
1902e5b6d6dSopenharmony_ci				if (bundle.getUntranslatedItemsSize() > 0) langCount++;
1912e5b6d6dSopenharmony_ci			}
1922e5b6d6dSopenharmony_ci
1932e5b6d6dSopenharmony_ci			// Initialize the components
1942e5b6d6dSopenharmony_ci			mainPanels               = new JPanel[langCount];
1952e5b6d6dSopenharmony_ci			numUntransLabels         = new JLabel[langCount];
1962e5b6d6dSopenharmony_ci			listScrolls              = new JScrollPane[langCount];
1972e5b6d6dSopenharmony_ci			untransLists             = new JList[langCount];
1982e5b6d6dSopenharmony_ci
1992e5b6d6dSopenharmony_ci			mainBox                  = new Box(BoxLayout.Y_AXIS);
2002e5b6d6dSopenharmony_ci			mainScroll               = new JScrollPane(mainBox);
2012e5b6d6dSopenharmony_ci			jLabelUntransTitle       = new JLabel(rbm.getBaseClass() + " - " + Resources.getTranslation("untranslated_items"));
2022e5b6d6dSopenharmony_ci
2032e5b6d6dSopenharmony_ci			// Set component properties
2042e5b6d6dSopenharmony_ci			jLabelUntransTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
2052e5b6d6dSopenharmony_ci			mainBox.add(jLabelUntransTitle);
2062e5b6d6dSopenharmony_ci
2072e5b6d6dSopenharmony_ci			int count = 0;
2082e5b6d6dSopenharmony_ci			for (int i=0; i < rbm.getBundles().size(); i++) {
2092e5b6d6dSopenharmony_ci				Bundle bundle = (Bundle)rbm.getBundles().elementAt(i);
2102e5b6d6dSopenharmony_ci				if (bundle.getUntranslatedItemsSize() > 0) {
2112e5b6d6dSopenharmony_ci					mainPanels[count] = new JPanel();
2122e5b6d6dSopenharmony_ci					mainPanels[count].setLayout(new BorderLayout());
2132e5b6d6dSopenharmony_ci					numUntransLabels[count] = new JLabel(Resources.getTranslation("baseuntrans_untrans_count") +
2142e5b6d6dSopenharmony_ci														 bundle.getUntranslatedItemsSize());
2152e5b6d6dSopenharmony_ci					// TODO: Implement a List Model for this list, remove use of vector
2162e5b6d6dSopenharmony_ci					untransLists[count] = new JList(bundle.getUntranslatedItemsAsVector());
2172e5b6d6dSopenharmony_ci					listScrolls[count] = new JScrollPane(untransLists[count]);
2182e5b6d6dSopenharmony_ci
2192e5b6d6dSopenharmony_ci					mainPanels[count].setBorder(BorderFactory.createTitledBorder(
2202e5b6d6dSopenharmony_ci												BorderFactory.createEtchedBorder(),
2212e5b6d6dSopenharmony_ci												Resources.getTranslation("baseuntrans_file") + " " + bundle.toString()));
2222e5b6d6dSopenharmony_ci					mainPanels[count].removeAll();
2232e5b6d6dSopenharmony_ci					mainPanels[count].add(numUntransLabels[count], BorderLayout.NORTH);
2242e5b6d6dSopenharmony_ci					mainPanels[count].add(listScrolls[count], BorderLayout.CENTER);
2252e5b6d6dSopenharmony_ci
2262e5b6d6dSopenharmony_ci					mainBox.add(Box.createVerticalStrut(5));
2272e5b6d6dSopenharmony_ci					mainBox.add(mainPanels[count]);
2282e5b6d6dSopenharmony_ci
2292e5b6d6dSopenharmony_ci					count++;
2302e5b6d6dSopenharmony_ci				}
2312e5b6d6dSopenharmony_ci			}
2322e5b6d6dSopenharmony_ci			mainScroll.setPreferredSize(getSize());
2332e5b6d6dSopenharmony_ci			removeAll();
2342e5b6d6dSopenharmony_ci			add(mainScroll);
2352e5b6d6dSopenharmony_ci		} else {
2362e5b6d6dSopenharmony_ci			removeAll();
2372e5b6d6dSopenharmony_ci		}
2382e5b6d6dSopenharmony_ci	}
2392e5b6d6dSopenharmony_ci
2402e5b6d6dSopenharmony_ci	public void updateComponents() {
2412e5b6d6dSopenharmony_ci		// Update components
2422e5b6d6dSopenharmony_ci		if (bundle != null) {
2432e5b6d6dSopenharmony_ci			jLabelUntransTitle.setText(bundle.name);
2442e5b6d6dSopenharmony_ci			untransTableModel.setBundle(bundle);
2452e5b6d6dSopenharmony_ci		} else if (rbm != null) {
2462e5b6d6dSopenharmony_ci			initComponents();
2472e5b6d6dSopenharmony_ci		} else {
2482e5b6d6dSopenharmony_ci			removeAll();
2492e5b6d6dSopenharmony_ci		}
2502e5b6d6dSopenharmony_ci	}
2512e5b6d6dSopenharmony_ci}
2522e5b6d6dSopenharmony_ci
2532e5b6d6dSopenharmony_ci/**
2542e5b6d6dSopenharmony_ci * The table model for untranslated Items
2552e5b6d6dSopenharmony_ci */
2562e5b6d6dSopenharmony_ci
2572e5b6d6dSopenharmony_ciclass UntranslatedItemsTableModel extends AbstractTableModel {
2582e5b6d6dSopenharmony_ci	Bundle bundle;
2592e5b6d6dSopenharmony_ci
2602e5b6d6dSopenharmony_ci	public UntranslatedItemsTableModel(Bundle bundle) {
2612e5b6d6dSopenharmony_ci		this.bundle = bundle;
2622e5b6d6dSopenharmony_ci	}
2632e5b6d6dSopenharmony_ci
2642e5b6d6dSopenharmony_ci	public void setBundle(Bundle bundle) {
2652e5b6d6dSopenharmony_ci		this.bundle = bundle;
2662e5b6d6dSopenharmony_ci		update();
2672e5b6d6dSopenharmony_ci	}
2682e5b6d6dSopenharmony_ci
2692e5b6d6dSopenharmony_ci	public int getColumnCount() { return 3; }
2702e5b6d6dSopenharmony_ci
2712e5b6d6dSopenharmony_ci	public int getRowCount() {
2722e5b6d6dSopenharmony_ci		return bundle.getUntranslatedItemsSize();
2732e5b6d6dSopenharmony_ci	}
2742e5b6d6dSopenharmony_ci
2752e5b6d6dSopenharmony_ci	public Object getValueAt(int row, int col) {
2762e5b6d6dSopenharmony_ci		BundleItem item = bundle.getUntranslatedItem(row);
2772e5b6d6dSopenharmony_ci		String retStr = null;
2782e5b6d6dSopenharmony_ci
2792e5b6d6dSopenharmony_ci		switch(col) {
2802e5b6d6dSopenharmony_ci		case 0:
2812e5b6d6dSopenharmony_ci			retStr = item.getKey();
2822e5b6d6dSopenharmony_ci			break;
2832e5b6d6dSopenharmony_ci		case 1:
2842e5b6d6dSopenharmony_ci			retStr = item.getTranslation();
2852e5b6d6dSopenharmony_ci			break;
2862e5b6d6dSopenharmony_ci		case 2:
2872e5b6d6dSopenharmony_ci			retStr = (item.getParentGroup() == null ? "" : item.getParentGroup().getName());
2882e5b6d6dSopenharmony_ci			break;
2892e5b6d6dSopenharmony_ci		default:
2902e5b6d6dSopenharmony_ci			retStr = Resources.getTranslation("table_cell_error");
2912e5b6d6dSopenharmony_ci		}
2922e5b6d6dSopenharmony_ci
2932e5b6d6dSopenharmony_ci		return retStr;
2942e5b6d6dSopenharmony_ci	}
2952e5b6d6dSopenharmony_ci
2962e5b6d6dSopenharmony_ci	public String getColumnName(int col) {
2972e5b6d6dSopenharmony_ci		if (col == 0) return Resources.getTranslation("languageuntrans_column_key");
2982e5b6d6dSopenharmony_ci		else if (col == 1) return Resources.getTranslation("languageuntrans_column_translation");
2992e5b6d6dSopenharmony_ci		else if (col == 2) return Resources.getTranslation("languageuntrans_column_group");
3002e5b6d6dSopenharmony_ci		else return Resources.getTranslation("table_column_error");
3012e5b6d6dSopenharmony_ci	}
3022e5b6d6dSopenharmony_ci
3032e5b6d6dSopenharmony_ci	public BundleItem getBundleItem(int row) {
3042e5b6d6dSopenharmony_ci		return bundle.getUntranslatedItem(row);
3052e5b6d6dSopenharmony_ci	}
3062e5b6d6dSopenharmony_ci
3072e5b6d6dSopenharmony_ci	public void update() {
3082e5b6d6dSopenharmony_ci		fireTableDataChanged();
3092e5b6d6dSopenharmony_ci	}
3102e5b6d6dSopenharmony_ci}
3112e5b6d6dSopenharmony_ci
312