• 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 import javax.swing.event.*;
15 
16 import com.ibm.rbm.*;
17 
18 /**
19  * The class used to display groups
20  */
21 class RBGroupPanel extends JPanel {
22 	RBManager rbm;
23 	Bundle bundle;
24 	RBManagerGUI listener;
25 
26 	// Components
27 	JLabel      jLabelGroupTitle;
28 	JLabel      jLabelGroupNameTitle;
29 	JLabel      jLabelGroupCommentTitle;
30 	JLabel      jLabelGroupComment;
31 	JComboBox   jComboBoxGroup;
32 	JTable      jTableGroupTable;
33 	JScrollPane jScrollPaneGroupTable;
34 
35 	// Components - Manager
36 	JList       jListGroup;
37 	JButton     createItemButton;
38 	JButton     createGroupButton;
39 	JButton     editItemButton;
40 	JButton     editGroupButton;
41 	JButton     deleteItemButton;
42 	JButton     deleteGroupButton;
43 	JPanel      itemPanel;
44 	JPanel      groupPanel;
45 
RBGroupPanel(RBManagerGUI gui)46 	public RBGroupPanel(RBManagerGUI gui) {
47 		super();
48 		listener = gui;
49 	}
50 
setBundle(Bundle b)51 	public void setBundle(Bundle b) {
52 		rbm = null;
53 		if (bundle == null) {
54 			bundle = b;
55 			initComponents();
56 		} else if (bundle != b) {
57 			bundle = b;
58 			updateComponents();
59 		}
60 	}
61 
setManager(RBManager m)62 	public void setManager(RBManager m) {
63 		bundle = null;
64 		if (rbm == null) {
65 			rbm = m;
66 			initComponents();
67 		} else if (rbm != m) {
68 			rbm = m;
69 			updateComponents();
70 		}
71 	}
72 
removeElements()73 	public void removeElements() {
74 		if (rbm != null || bundle != null) {
75 			rbm = null;
76 			bundle = null;
77 			initComponents();
78 		}
79 	}
80 
81 	// Marks the selected resource as translated and removes from this view
markSelectedResourceAsTranslated()82 	private void markSelectedResourceAsTranslated() {
83 		if (bundle == null) return;
84 		if (jTableGroupTable.getSelectedRow() < 0) return;
85 		if (jTableGroupTable.getModel() instanceof GroupItemsTableModel) {
86 			int row = jTableGroupTable.getSelectedRow();
87 			GroupItemsTableModel model = (GroupItemsTableModel)jTableGroupTable.getModel();
88 			BundleItem item = model.getBundleItem(row);
89 			item.setTranslated(true);
90 			model.update();
91 		}
92 	}
93 
94 	// Removes the selected resource from the resource file
deleteSelectedResource()95 	private void deleteSelectedResource() {
96 		if (bundle == null) return;
97 		if (jTableGroupTable.getSelectedRow() < 0) return;
98 		if (jTableGroupTable.getModel() instanceof GroupItemsTableModel) {
99 			int row = jTableGroupTable.getSelectedRow();
100 			GroupItemsTableModel model = (GroupItemsTableModel)jTableGroupTable.getModel();
101 			BundleItem item = model.getBundleItem(row);
102 			if (item.getParentGroup() != null && item.getParentGroup().getParentBundle() != null) {
103 				Bundle parentBundle = item.getParentGroup().getParentBundle();
104 				parentBundle.removeItem(item.getKey());
105 			}
106 			model.update();
107 		}
108 	}
109 
initComponents()110 	private void initComponents() {
111 		// Initialize components
112 		if (bundle != null) {
113 			jLabelGroupTitle          = new JLabel(bundle.name);
114 			jComboBoxGroup            = new JComboBox(new GroupComboBoxModel(bundle));
115 
116 			jTableGroupTable          = new JTable(new GroupItemsTableModel((BundleGroup)jComboBoxGroup.getSelectedItem()));
117 			jScrollPaneGroupTable     = new JScrollPane(jTableGroupTable);
118 			jLabelGroupNameTitle      = new JLabel(Resources.getTranslation("basegroup_group_name"));
119 			jLabelGroupCommentTitle   = new JLabel(Resources.getTranslation("basegroup_group_comment"));
120 			jLabelGroupComment        = new JLabel(((BundleGroup)jComboBoxGroup.getSelectedItem()).getComment());
121 
122 			// Lower panel components
123 			JPanel  lowerPanel = new JPanel();
124 			JButton deleteButton = new JButton(Resources.getTranslation("button_delete_resource"));
125 			JButton translateButton = new JButton(Resources.getTranslation("button_mark_translated"));
126 
127 			deleteButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_delete_resource_trigger")));
128 			translateButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_mark_translated_trigger")));
129 			lowerPanel.setBorder(BorderFactory.createTitledBorder(Resources.getTranslation("languageuntrans_selected_resources_options")));
130 			lowerPanel.setLayout(new GridLayout(1,2));
131 
132 			jLabelGroupNameTitle.setHorizontalAlignment(SwingConstants.LEFT);
133 
134 			jTableGroupTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
135 			jTableGroupTable.addMouseListener(listener);
136 
137 			jComboBoxGroup.addActionListener(new GroupComboActionListener(this));
138 
139 			jLabelGroupTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
140 
141 			// Add action listeners
142 			deleteButton.addActionListener(new ActionListener(){
143 				public void actionPerformed(ActionEvent ev) {
144 					deleteSelectedResource();
145 				}
146 			});
147 
148 			translateButton.addActionListener(new ActionListener(){
149 				public void actionPerformed(ActionEvent ev) {
150 					markSelectedResourceAsTranslated();
151 				}
152 			});
153 
154 			// Update the display
155 			setLayout(new GridBagLayout());
156 			GridBagConstraints gbc = new GridBagConstraints();
157 			removeAll();
158 			lowerPanel.add(deleteButton);
159 			lowerPanel.add(translateButton);
160 
161 			gbc.weightx = 1.0;
162 			gbc.weighty = 0.0;
163 			gbc.gridwidth = GridBagConstraints.REMAINDER;
164 			gbc.fill = GridBagConstraints.HORIZONTAL;
165 			add(jLabelGroupTitle, gbc);
166 			gbc.weightx = 0.0;
167 			gbc.gridwidth = 1;
168 			add(jLabelGroupNameTitle, gbc);
169 			gbc.weightx = 1.0;
170 			gbc.gridwidth = GridBagConstraints.REMAINDER;
171 			add(jComboBoxGroup, gbc);
172 			gbc.weightx = 0.0;
173 			gbc.gridwidth = 1;
174 			add(jLabelGroupCommentTitle, gbc);
175 			gbc.weightx = 1.0;
176 			gbc.gridwidth = GridBagConstraints.REMAINDER;
177 			add(jLabelGroupComment, gbc);
178 			gbc.fill = GridBagConstraints.BOTH;
179 			gbc.weighty = 1.0;
180 			add(jScrollPaneGroupTable, gbc);
181 			gbc.weighty = 0.0;
182 			gbc.fill = GridBagConstraints.HORIZONTAL;
183 			add(lowerPanel, gbc);
184 		} else if (rbm != null) {
185 			Bundle mainBundle = (Bundle)rbm.getBundles().firstElement();
186 			jLabelGroupTitle          = new JLabel(rbm.getBaseClass() + " - " + Resources.getTranslation("groups"));
187 			jComboBoxGroup            = new JComboBox(new GroupComboBoxModel(mainBundle));//mainBundle.getGroupsAsVector());
188 
189 			jListGroup                = new JList(new GroupItemsListModel((BundleGroup)jComboBoxGroup.getSelectedItem()));
190 			jScrollPaneGroupTable     = new JScrollPane(jListGroup);
191 			jLabelGroupNameTitle      = new JLabel(Resources.getTranslation("basegroup_group_name"));
192 			jLabelGroupCommentTitle   = new JLabel(Resources.getTranslation("basegroup_group_comment"));
193 			try {
194 				jLabelGroupComment    = new JLabel(((BundleGroup)jComboBoxGroup.getSelectedItem()).getComment());
195 			} catch (NullPointerException npe) {
196 				jLabelGroupComment    = new JLabel("");
197 			}
198 
199 			createItemButton          = new JButton(Resources.getTranslation("button_create_resource"));
200 			createGroupButton         = new JButton(Resources.getTranslation("button_create_group"));
201 			deleteItemButton          = new JButton(Resources.getTranslation("button_delete_resource"));
202 			deleteGroupButton         = new JButton(Resources.getTranslation("button_delete_group"));
203 			editItemButton            = new JButton(Resources.getTranslation("button_edit_resource"));
204 			editGroupButton           = new JButton(Resources.getTranslation("button_edit_group"));
205 
206 			itemPanel                 = new JPanel();
207 			groupPanel                = new JPanel();
208 
209 			itemPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
210 																  Resources.getTranslation("basegroup_item_options")));
211 			groupPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
212 																  Resources.getTranslation("basegroup_group_options")));
213 			itemPanel.setLayout(new GridLayout(1,3));
214 			groupPanel.setLayout(new GridLayout(1,3));
215 			itemPanel.setMaximumSize(new Dimension(20000,50));
216 			groupPanel.setMaximumSize(new Dimension(20000,50));
217 
218 			createItemButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_create_resource_trigger")));
219 			editItemButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_edit_resource_trigger")));
220 			deleteItemButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_delete_resource_trigger")));
221 			createGroupButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_create_group_trigger")));
222 
223 			jListGroup.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
224 
225 			jComboBoxGroup.addActionListener(new GroupComboActionListener(this));
226 
227 			jLabelGroupTitle.setFont(new Font("SansSerif",Font.PLAIN,18));
228 
229 			// Add the listeners
230 			jListGroup.addMouseListener(new MouseAdapter() {
231 				public void mouseClicked(MouseEvent ev) {
232 					if(ev.getClickCount() == 2 && ev.getSource() instanceof JList) {
233 						// A double click means they want to edit a bundle item
234 						if (((JList)ev.getSource()).getSelectedValue() != null)
235 							new BundleItemCreationDialog((BundleItem)((JList)ev.getSource()).getSelectedValue(),
236 								listener.rbm, listener, Resources.getTranslation("dialog_title_edit_item"), true);
237 					}
238 				}
239 			});
240 
241 			createItemButton.addActionListener(new ActionListener(){
242 				public void actionPerformed(ActionEvent ev) {
243 					new BundleItemCreationDialog(((BundleGroup)jComboBoxGroup.getSelectedItem()).getName(),
244 												 listener.rbm, listener,
245 												 Resources.getTranslation("dialog_title_new_item"), true);
246 					updateComponents();
247 				}
248 			});
249 			createGroupButton.addActionListener(listener);
250 			editItemButton.addActionListener(new ActionListener() {
251 				public void actionPerformed(ActionEvent ev) {
252 					if (jListGroup.getSelectedValue() != null)
253 						new BundleItemCreationDialog((BundleItem)jListGroup.getSelectedValue(),
254 							listener.rbm, listener, Resources.getTranslation("dialog_title_edit_item"), true);
255 					updateComponents();
256 				}
257 			});
258 			editGroupButton.addActionListener(new ActionListener() {
259 				public void actionPerformed(ActionEvent ev) {
260 					new BundleGroupEditDialog((BundleGroup)jComboBoxGroup.getSelectedItem(),
261 											  listener, Resources.getTranslation("dialog_title_edit_group"), true);
262 					updateComponents();
263 				}
264 			});
265 			deleteGroupButton.addActionListener(new ActionListener() {
266 				public void actionPerformed(ActionEvent ev) {
267 					int response = JOptionPane.showConfirmDialog(listener,
268 						Resources.getTranslation("dialog_warning_delete_group"),
269 						Resources.getTranslation("dialog_title_delete_group"), JOptionPane.OK_CANCEL_OPTION,
270 						JOptionPane.WARNING_MESSAGE);
271 					if (response == JOptionPane.OK_OPTION) {
272 						// Delete the group
273 						int index = jComboBoxGroup.getSelectedIndex();
274 						BundleGroup group = (BundleGroup)jComboBoxGroup.getSelectedItem();
275 						if (group.getName().equals("Ungrouped Items"))
276 							return;
277 						if (index < jComboBoxGroup.getItemCount()-1)
278 							jComboBoxGroup.setSelectedIndex(index+1);
279 						else
280 							jComboBoxGroup.setSelectedIndex(index-1);
281 						rbm.deleteGroup(group.getName());
282 					}
283 					updateComponents();
284 				}
285 			});
286 
287 			deleteItemButton.addActionListener(new ActionListener() {
288 				public void actionPerformed(ActionEvent ev) {
289 					int response = JOptionPane.showConfirmDialog(listener,
290 						Resources.getTranslation("dialog_warning_delete_item"),
291 						Resources.getTranslation("dialog_title_delete_item"), JOptionPane.OK_CANCEL_OPTION,
292 						JOptionPane.WARNING_MESSAGE);
293 					if (response == JOptionPane.OK_OPTION) {
294 						Object o = jListGroup.getSelectedValue();
295 						if (o != null) {
296 							BundleItem item = (BundleItem) o;
297 							handleDeleteItem(item.getKey());
298 							//panel.rbm.deleteItem(item.getKey());
299 						}
300 					}
301 					updateComponents();
302 				}
303 			});
304 
305 			// Update the display
306 			setLayout(new GridBagLayout());
307 			GridBagConstraints gbc = new GridBagConstraints();
308 			removeAll();
309 			itemPanel.add(createItemButton, BorderLayout.WEST);
310 			itemPanel.add(editItemButton, BorderLayout.CENTER);
311 			itemPanel.add(deleteItemButton, BorderLayout.EAST);
312 			groupPanel.add(createGroupButton, BorderLayout.WEST);
313 			groupPanel.add(editGroupButton, BorderLayout.CENTER);
314 			groupPanel.add(deleteGroupButton, BorderLayout.EAST);
315 
316 
317 			gbc.weightx = 1.0;
318 			gbc.weighty = 0.0;
319 			gbc.gridwidth = GridBagConstraints.REMAINDER;
320 			gbc.fill = GridBagConstraints.HORIZONTAL;
321 			add(jLabelGroupTitle, gbc);
322 			gbc.weightx = 0.0;
323 			gbc.gridwidth = 1;
324 			add(jLabelGroupNameTitle, gbc);
325 			gbc.weightx = 1.0;
326 			gbc.gridwidth = GridBagConstraints.REMAINDER;
327 			add(jComboBoxGroup, gbc);
328 			gbc.weightx = 0.0;
329 			gbc.gridwidth = 1;
330 			add(jLabelGroupCommentTitle, gbc);
331 			gbc.weightx = 1.0;
332 			gbc.gridwidth = GridBagConstraints.REMAINDER;
333 			add(jLabelGroupComment, gbc);
334 			gbc.fill = GridBagConstraints.BOTH;
335 			gbc.weighty = 1.0;
336 			add(jScrollPaneGroupTable, gbc);
337 			gbc.weighty = 0.0;
338 			gbc.fill = GridBagConstraints.HORIZONTAL;
339 			add(groupPanel, gbc);
340 			add(itemPanel, gbc);
341 		} else {
342 			removeAll();
343 		}
344 	}
345 
updateComponents()346 	public void updateComponents() {
347 		// Initialize components
348 		if (bundle != null) {
349 			jLabelGroupTitle.setText(bundle.name);
350 
351 			((GroupItemsTableModel)jTableGroupTable.getModel()).setGroup((BundleGroup)jComboBoxGroup.getSelectedItem());
352 			jLabelGroupComment.setText(((BundleGroup)jComboBoxGroup.getSelectedItem()).getComment());
353 
354 			jTableGroupTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
355 
356 			// Update the group comment
357 			jLabelGroupComment.setText(((BundleGroup)jComboBoxGroup.getSelectedItem()).getComment());
358 			((GroupComboBoxModel)jComboBoxGroup.getModel()).update();
359 		} else if (rbm != null) {
360 
361 			// Update the list of groups
362 //			try {
363 				((GroupComboBoxModel)jComboBoxGroup.getModel()).update();
364 //			}
365 //			catch (Exception e) {}
366 			// Update the group comment
367 			if ((BundleGroup)jComboBoxGroup.getSelectedItem() != null)
368 				jLabelGroupComment.setText(((BundleGroup)jComboBoxGroup.getSelectedItem()).getComment());
369 			else
370 				jLabelGroupComment.setText("");
371 			// Update the list of resources
372 			ListModel lmodel = jListGroup.getModel();
373 			if (lmodel instanceof GroupItemsListModel) {
374 				//((GroupItemsListModel)lmodel).update();
375 				((GroupItemsListModel)lmodel).setGroup((BundleGroup)jComboBoxGroup.getSelectedItem());
376 			}
377 			else {
378 				GroupItemsListModel newModel = new GroupItemsListModel((BundleGroup)jComboBoxGroup.getSelectedItem());
379 				RBManagerGUI.debugMsg("List Model not as anticipated: " + lmodel.getClass().getName());
380 				jListGroup.setModel(newModel);
381 				newModel.update();
382 			}
383 		} else {
384 			RBManagerGUI.debugMsg("Update, but no active components");
385 			removeAll();
386 		}
387 		//validate();
388 	}
389 
handleDeleteItem(String key)390 	private void handleDeleteItem(String key) {
391 		if (rbm != null) rbm.deleteItem(key);
392 	}
393 }
394 
395 /**
396  * The action listener which monitors changes in the group to display
397  */
398 class GroupComboActionListener implements ActionListener {
399 	RBGroupPanel panel;
400 
GroupComboActionListener(RBGroupPanel panel)401 	protected GroupComboActionListener(RBGroupPanel panel) {
402 		this.panel = panel;
403 	}
404 
actionPerformed(ActionEvent ev)405 	public void actionPerformed(ActionEvent ev) {
406 		panel.updateComponents();
407 	}
408 }
409 
410 /**
411  * The list model for groups
412  */
413 class GroupItemsListModel extends AbstractListModel {
414 	BundleGroup group;
415 
setGroup(BundleGroup group)416 	public void setGroup(BundleGroup group) {
417 		this.group = group;
418 		update();
419 	}
420 
GroupItemsListModel(BundleGroup group)421 	public GroupItemsListModel(BundleGroup group) {
422 		this.group = group;
423 	}
424 
getSize()425 	public int getSize() {
426 		if (group == null)
427 			return 0;
428 		int result = group.getItemCount();
429 		return result;
430 	}
431 
getElementAt(int index)432 	public Object getElementAt(int index) {
433 		return group.getBundleItem(index);
434 	}
435 
update()436 	public void update() {
437 		fireContentsChanged(this, 0, getSize()-1);
438 	}
439 }
440 
441 /**
442  * The table model for searched Items
443  */
444 class GroupComboBoxModel extends DefaultComboBoxModel {
445 	Bundle bundle;
446 
GroupComboBoxModel(Bundle bundle)447 	public GroupComboBoxModel (Bundle bundle) {
448 		this.bundle = bundle;
449 		setSelectedItem(bundle.getBundleGroup(0));
450 	}
451 
getSize()452 	public int getSize() {
453 		return bundle.getGroupCount();
454 	}
455 
getElementAt(int index)456 	public Object getElementAt(int index) {
457 		return bundle.getBundleGroup(index);
458 	}
459 
getSelectedItem()460 	public Object getSelectedItem() {
461 		return super.getSelectedItem();
462 		//return getElementAt(0);
463 	}
464 
update()465 	public void update() {
466 		fireContentsChanged(this, 0, getSize()-1);
467 	}
468 }
469 
470 /**
471  * The table model for bundle groups
472  */
473 class GroupItemsTableModel extends AbstractTableModel {
474 	BundleGroup group;
475 
GroupItemsTableModel(BundleGroup group)476 	public GroupItemsTableModel(BundleGroup group) {
477 		this.group = group;
478 	}
479 
getColumnCount()480 	public int getColumnCount() { return 3; }
481 
getRowCount()482 	public int getRowCount() {
483 		return group.getItemCount();
484 	}
485 
setGroup(BundleGroup bg)486 	public void setGroup(BundleGroup bg) {
487 		group = bg;
488 		fireTableChanged(new TableModelEvent(this));
489 	}
490 
getValueAt(int row, int col)491 	public Object getValueAt(int row, int col) {
492 		BundleItem item = group.getBundleItem(row);
493 
494 		String retStr = null;
495 
496 		switch(col) {
497 		case 0:
498 			retStr = item.getKey();
499 			break;
500 		case 1:
501 			retStr = item.getTranslation();
502 			break;
503 		case 2:
504 			retStr = (item.getComment() == null ? "" : item.getComment());
505 			break;
506 		default:
507 			retStr = Resources.getTranslation("table_cell_error");
508 		}
509 
510 		return retStr;
511 	}
512 
getColumnName(int col)513 	public String getColumnName(int col) {
514 		if (col == 0) return Resources.getTranslation("languagegroup_column_key");
515 		else if (col == 1) return Resources.getTranslation("languagegroup_column_translation");
516 		else if (col == 2) return Resources.getTranslation("languagegroup_column_comment");
517 		else return Resources.getTranslation("table_column_error");
518 	}
519 
getBundleItem(int row)520 	public BundleItem getBundleItem(int row) {
521 		if (row >= group.getItemCount())
522 		    return null;
523 		return group.getBundleItem(row);
524 	}
525 
update()526 	public void update() {
527 		fireTableDataChanged();
528 	}
529 }
530 
531