• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *****************************************************************************
3  * Copyright (C) 2000-2004, International Business Machines Corporation and  *
4  * others. All Rights Reserved.                                              *
5  *****************************************************************************
6  */
7 package com.ibm.rbm.gui;
8 
9 import java.awt.*;
10 import java.awt.event.*;
11 
12 import javax.swing.*;
13 import javax.swing.table.*;
14 
15 import com.ibm.rbm.*;
16 
17 /**
18  * The class used to display untranslated items
19  */
20 class RBUntranslatedPanel extends JPanel {
21 	RBManager rbm;
22 	Bundle bundle;
23 	RBManagerGUI listener;
24 
25 	// Components - Bundle
26 	JLabel                      jLabelUntransTitle;
27 	UntranslatedItemsTableModel untransTableModel;
28 	JTable                      jTableUntrans;
29 	JScrollPane                 jScrollPaneUntransTable;
30 
31 	// Components - Bundle Manager
32 	Box                         mainBox;
33 	JPanel                      mainPanels[];
34 	JLabel                      numUntransLabels[];
35 	JScrollPane                 mainScroll;
36 	JScrollPane                 listScrolls[];
37 	JList                       untransLists[];
38 
RBUntranslatedPanel(RBManagerGUI gui)39 	public RBUntranslatedPanel(RBManagerGUI gui) {
40 		super();
41 		listener = gui;
42 	}
43 
setBundle(Bundle b)44 	public void setBundle(Bundle b) {
45 		rbm = null;
46 		if (bundle == null) {
47 			bundle = b;
48 			initComponents();
49 		} else if (bundle != b) {
50 			bundle = b;
51 			updateComponents();
52 		}
53 	}
54 
setManager(RBManager m)55 	public void setManager(RBManager m) {
56 		bundle = null;
57 		if (rbm == null) {
58 			rbm = m;
59 			initComponents();
60 		} else if (rbm != m) {
61 			rbm = m;
62 			updateComponents();
63 		}
64 	}
65 
removeElements()66 	public void removeElements() {
67 		if (rbm != null || bundle != null) {
68 			rbm = null;
69 			bundle = null;
70 			initComponents();
71 		}
72 	}
73 
74 	// Marks the selected resource as translated and removes from this view
markSelectedResourceAsTranslated()75 	private void markSelectedResourceAsTranslated() {
76 		if (bundle == null) return;
77 		if (jTableUntrans.getSelectedRow() < 0) return;
78 		if (jTableUntrans.getModel() instanceof UntranslatedItemsTableModel) {
79 			int row = jTableUntrans.getSelectedRow();
80 			UntranslatedItemsTableModel model = (UntranslatedItemsTableModel)jTableUntrans.getModel();
81 			BundleItem item = model.getBundleItem(row);
82 			item.setTranslated(true);
83 			model.update();
84 		}
85 	}
86 
87 	// Removes the selected resource from the resource file
deleteSelectedResource()88 	private void deleteSelectedResource() {
89 		if (bundle == null) return;
90 		if (jTableUntrans.getSelectedRow() < 0) return;
91 		if (jTableUntrans.getModel() instanceof UntranslatedItemsTableModel) {
92 			int row = jTableUntrans.getSelectedRow();
93 			UntranslatedItemsTableModel model = (UntranslatedItemsTableModel)jTableUntrans.getModel();
94 			BundleItem item = model.getBundleItem(row);
95 			if (item.getParentGroup() != null && item.getParentGroup().getParentBundle() != null) {
96 				Bundle bundle = item.getParentGroup().getParentBundle();
97 				bundle.removeItem(item.getKey());
98 			}
99 			model.update();
100 		}
101 	}
102 
printTable()103 	private void printTable() {
104 		PrintJob pjob = getToolkit().getPrintJob(new Frame(),
105                            "Printing Test", null);
106 
107         if (pjob != null) {
108             Graphics pg = pjob.getGraphics();
109 
110             if (pg != null) {
111                 //jTableUntrans.print(pg);
112                 Dimension page_dim = pjob.getPageDimension();
113 				pg.setColor(Color.black);
114 				int y_off = 50;
115 				int x_off = 30;
116 				TableModel model = jTableUntrans.getModel();
117 				pg.setFont(new Font("SansSerif", Font.BOLD, 14));
118 				pg.drawString("Untranslated Items:       Page 1", x_off, y_off);
119 				pg.setFont(new Font("SansSerif", Font.PLAIN, 10));
120 
121 				for (int i=0 ; i < model.getRowCount(); i++) {
122 					if (y_off < page_dim.height - 50) {
123 						y_off += 15;
124 						String key = model.getValueAt(i, 0).toString();
125 						String translation = model.getValueAt(i,1).toString();
126 						pg.drawString(key + " -> " + translation, x_off, y_off);
127 					}
128 				}
129 				pg.dispose(); // flush page
130             }
131             pjob.end();
132 
133         }
134 	}
135 
initComponents()136 	public void initComponents() {
137 		// Initialize components
138 		if (bundle != null) {
139 			jLabelUntransTitle        = new JLabel(bundle.name);
140 			untransTableModel         = new UntranslatedItemsTableModel(bundle);
141 			jTableUntrans             = new JTable(untransTableModel);
142 			jScrollPaneUntransTable   = new JScrollPane(jTableUntrans);
143 
144 			// Lower panel components
145 			JPanel  lowerPanel = new JPanel();
146 			JButton deleteButton = new JButton(Resources.getTranslation("button_delete_resource"));
147 			JButton translateButton = new JButton(Resources.getTranslation("button_mark_translated"));
148 			JButton printButton = new JButton(Resources.getTranslation("button_print_table"));
149 
150 			deleteButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_delete_resource_trigger")));
151 			translateButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_mark_translated_trigger")));
152 			lowerPanel.setBorder(BorderFactory.createTitledBorder(Resources.getTranslation("languageuntrans_selected_resources_options")));
153 			lowerPanel.setLayout(new GridLayout(1,2));
154 
155 			jTableUntrans.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
156 			jTableUntrans.addMouseListener(listener);
157 
158 			jLabelUntransTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
159 
160 			// Add action listeners
161 			deleteButton.addActionListener(new ActionListener() {
162 				public void actionPerformed(ActionEvent ev) {
163 					deleteSelectedResource();
164 				}
165 			});
166 			translateButton.addActionListener(new ActionListener() {
167 				public void actionPerformed(ActionEvent ev) {
168 					markSelectedResourceAsTranslated();
169 				}
170 			});
171 			printButton.addActionListener(new ActionListener() {
172 				public void actionPerformed(ActionEvent ev) {
173 					printTable();
174 				}
175 			});
176 
177 			removeAll();
178 			setLayout(new BorderLayout());
179 			lowerPanel.add(deleteButton);
180 			lowerPanel.add(translateButton);
181 			//lowerPanel.add(printButton);
182 			add(jLabelUntransTitle, BorderLayout.NORTH);
183 			add(jScrollPaneUntransTable, BorderLayout.CENTER);
184 			add(lowerPanel, BorderLayout.SOUTH);
185 		} else if (rbm != null) {
186 
187 			int langCount = 0;       // The number of languages with untranslated Items
188 			for (int i=0; i < rbm.getBundles().size(); i++) {
189 				Bundle bundle = (Bundle)rbm.getBundles().elementAt(i);
190 				if (bundle.getUntranslatedItemsSize() > 0) langCount++;
191 			}
192 
193 			// Initialize the components
194 			mainPanels               = new JPanel[langCount];
195 			numUntransLabels         = new JLabel[langCount];
196 			listScrolls              = new JScrollPane[langCount];
197 			untransLists             = new JList[langCount];
198 
199 			mainBox                  = new Box(BoxLayout.Y_AXIS);
200 			mainScroll               = new JScrollPane(mainBox);
201 			jLabelUntransTitle       = new JLabel(rbm.getBaseClass() + " - " + Resources.getTranslation("untranslated_items"));
202 
203 			// Set component properties
204 			jLabelUntransTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
205 			mainBox.add(jLabelUntransTitle);
206 
207 			int count = 0;
208 			for (int i=0; i < rbm.getBundles().size(); i++) {
209 				Bundle bundle = (Bundle)rbm.getBundles().elementAt(i);
210 				if (bundle.getUntranslatedItemsSize() > 0) {
211 					mainPanels[count] = new JPanel();
212 					mainPanels[count].setLayout(new BorderLayout());
213 					numUntransLabels[count] = new JLabel(Resources.getTranslation("baseuntrans_untrans_count") +
214 														 bundle.getUntranslatedItemsSize());
215 					// TODO: Implement a List Model for this list, remove use of vector
216 					untransLists[count] = new JList(bundle.getUntranslatedItemsAsVector());
217 					listScrolls[count] = new JScrollPane(untransLists[count]);
218 
219 					mainPanels[count].setBorder(BorderFactory.createTitledBorder(
220 												BorderFactory.createEtchedBorder(),
221 												Resources.getTranslation("baseuntrans_file") + " " + bundle.toString()));
222 					mainPanels[count].removeAll();
223 					mainPanels[count].add(numUntransLabels[count], BorderLayout.NORTH);
224 					mainPanels[count].add(listScrolls[count], BorderLayout.CENTER);
225 
226 					mainBox.add(Box.createVerticalStrut(5));
227 					mainBox.add(mainPanels[count]);
228 
229 					count++;
230 				}
231 			}
232 			mainScroll.setPreferredSize(getSize());
233 			removeAll();
234 			add(mainScroll);
235 		} else {
236 			removeAll();
237 		}
238 	}
239 
updateComponents()240 	public void updateComponents() {
241 		// Update components
242 		if (bundle != null) {
243 			jLabelUntransTitle.setText(bundle.name);
244 			untransTableModel.setBundle(bundle);
245 		} else if (rbm != null) {
246 			initComponents();
247 		} else {
248 			removeAll();
249 		}
250 	}
251 }
252 
253 /**
254  * The table model for untranslated Items
255  */
256 
257 class UntranslatedItemsTableModel extends AbstractTableModel {
258 	Bundle bundle;
259 
UntranslatedItemsTableModel(Bundle bundle)260 	public UntranslatedItemsTableModel(Bundle bundle) {
261 		this.bundle = bundle;
262 	}
263 
setBundle(Bundle bundle)264 	public void setBundle(Bundle bundle) {
265 		this.bundle = bundle;
266 		update();
267 	}
268 
getColumnCount()269 	public int getColumnCount() { return 3; }
270 
getRowCount()271 	public int getRowCount() {
272 		return bundle.getUntranslatedItemsSize();
273 	}
274 
getValueAt(int row, int col)275 	public Object getValueAt(int row, int col) {
276 		BundleItem item = bundle.getUntranslatedItem(row);
277 		String retStr = null;
278 
279 		switch(col) {
280 		case 0:
281 			retStr = item.getKey();
282 			break;
283 		case 1:
284 			retStr = item.getTranslation();
285 			break;
286 		case 2:
287 			retStr = (item.getParentGroup() == null ? "" : item.getParentGroup().getName());
288 			break;
289 		default:
290 			retStr = Resources.getTranslation("table_cell_error");
291 		}
292 
293 		return retStr;
294 	}
295 
getColumnName(int col)296 	public String getColumnName(int col) {
297 		if (col == 0) return Resources.getTranslation("languageuntrans_column_key");
298 		else if (col == 1) return Resources.getTranslation("languageuntrans_column_translation");
299 		else if (col == 2) return Resources.getTranslation("languageuntrans_column_group");
300 		else return Resources.getTranslation("table_column_error");
301 	}
302 
getBundleItem(int row)303 	public BundleItem getBundleItem(int row) {
304 		return bundle.getUntranslatedItem(row);
305 	}
306 
update()307 	public void update() {
308 		fireTableDataChanged();
309 	}
310 }
311 
312