1 /*
2  *****************************************************************************
3  * Copyright (C) 2000-2007, 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 import java.util.*;
12 
13 import javax.swing.*;
14 
15 import com.ibm.rbm.*;
16 
17 /**
18  * A dialog which allows the user to create a new Bundle Item
19  */
20 class BundleItemCreationDialog extends JDialog {
21 	RBManager rbm;
22 	String groupName;
23 	BundleItem item;
24 	boolean firstInit = true;
25 
26 	// Helper data
27 	int left_col_width = 125;
28 	int right_col_width = 275;
29 	int row_height = 25;
30 	Dimension leftDim = new Dimension(left_col_width, row_height);
31 	Dimension rightDim = new Dimension(right_col_width, row_height);
32 
33 	// Components
34 	Box mainBox = new Box(BoxLayout.Y_AXIS);
35 	Box box1 = new Box(BoxLayout.X_AXIS);
36 	Box box2 = new Box(BoxLayout.X_AXIS);
37 	Box box3 = new Box(BoxLayout.X_AXIS);
38 	Box box4 = new Box(BoxLayout.X_AXIS);
39 	Box box5 = new Box(BoxLayout.X_AXIS);
40 	Box box6 = new Box(BoxLayout.X_AXIS);
41 
42 	JLabel instructionsLabel = new JLabel("");
43 	JLabel groupLabel = new JLabel(Resources.getTranslation("dialog_group"));
44 	JLabel nameLabel = new JLabel(Resources.getTranslation("dialog_key"));
45 	JLabel transLabel = new JLabel(Resources.getTranslation("dialog_translation"));
46 	JLabel commentLabel = new JLabel(Resources.getTranslation("dialog_comment"));
47 	JLabel lookupLabel = new JLabel(Resources.getTranslation("dialog_lookups"));
48 
49 	JComboBox groupComboBox = new JComboBox();
50 	JTextField nameField = new JTextField("");
51 	JTextField transField = new JTextField("");
52 	JTextField commentField = new JTextField("");
53 	JTextField lookupFields[] = null;
54 	JLabel noLookupLabel = null;
55 	Box lookupBox = null;
56 	Box lookupBoxes[] = null;
57 	JLabel lookupLabels[] = null;
58 
59 	JButton createButton = new JButton(Resources.getTranslation("button_create"));
60 	JButton createMoreButton = new JButton(Resources.getTranslation("button_create_more"));
61 	JButton cancelButton = new JButton(Resources.getTranslation("button_cancel"));
62 
63 	Hashtable lookups = new Hashtable();
64 
BundleItemCreationDialog(RBManager rbm, JFrame frame, String title, boolean modal)65 	public BundleItemCreationDialog(RBManager rbm, JFrame frame, String title, boolean modal) {
66 		super(frame, title, modal);
67 		this.rbm = rbm;
68 		groupName = null;
69 		item = null;
70 		initComponents();
71 	}
72 
BundleItemCreationDialog(String groupName, RBManager rbm, JFrame frame, String title, boolean modal)73 	public BundleItemCreationDialog(String groupName, RBManager rbm, JFrame frame, String title, boolean modal) {
74 		super(frame, title, modal);
75 		this.rbm = rbm;
76 		this.groupName = groupName;
77 		item = null;
78 		initComponents();
79 	}
80 
BundleItemCreationDialog(BundleItem item, RBManager rbm, JFrame frame, String title, boolean modal)81 	public BundleItemCreationDialog(BundleItem item, RBManager rbm, JFrame frame, String title, boolean modal) {
82 		super(frame, title, modal);
83 		this.item = item;
84 		this.rbm = rbm;
85 		groupName = item.getParentGroup().getName();
86 		initComponents();
87 	}
88 
createItem()89 	boolean createItem() {
90 		if (rbm == null) return false;
91 		Hashtable lookupHash = new Hashtable();
92 		if (lookupBoxes != null) {
93 			for (int i=0; i < lookupBoxes.length; i++) {
94 				String nameText = lookupLabels[i].getText().trim();
95 				String name = nameText.substring(nameText.indexOf("{")+1,nameText.indexOf("}"));
96 				String value = lookupFields[i].getText().trim();
97 				lookupHash.put(name,value);
98 			}
99 		}
100 		return rbm.createItem(nameField.getText().trim(), transField.getText().trim(),
101 							  ((BundleGroup)groupComboBox.getSelectedItem()).getName(),
102 							  commentField.getText().trim(), lookupHash);
103 	}
104 
editItem()105 	boolean editItem() {
106 		if (item == null) return false;
107 		Hashtable lookupHash = new Hashtable();
108 		if (lookupBoxes != null) {
109 			for (int i=0; i < lookupBoxes.length; i++) {
110 				String nameText = lookupLabels[i].getText().trim();
111 				String name = nameText.substring(nameText.indexOf("{")+1,nameText.indexOf("}"));
112 				String value = lookupFields[i].getText().trim();
113 				lookupHash.put(name,value);
114 			}
115 		}
116 		return rbm.editItem(item, nameField.getText().trim(),
117 							transField.getText().trim(), ((BundleGroup)groupComboBox.getSelectedItem()).getName(),
118 							commentField.getText().trim(), lookupHash);
119 	}
120 
clearComponents()121 	private void clearComponents() {
122 		nameField.setText("");
123 		transField.setText("");
124 		commentField.setText("");
125 		initComponents();
126 	}
127 
processKeyEvent(KeyEvent ev)128 	protected void processKeyEvent(KeyEvent ev) {
129 		if (ev.getKeyCode() == KeyEvent.VK_ENTER && ev.getID() == KeyEvent.KEY_RELEASED) {
130 			if (transField.hasFocus()) {
131 				// If we are in the translation field, then enter should create a new line character, not exit the dialog
132 				int caretPos = transField.getCaretPosition();
133 				String oldText = transField.getText();
134 				transField.setText(oldText.substring(0,caretPos) + "\n" + oldText.substring(caretPos,oldText.length()));
135 				transField.setCaretPosition(caretPos+1);
136 				validate();
137 				setSize(getPreferredSize());
138 				return;
139 			}
140 
141 			BundleItemCreationDialog dialog = this;
142 			boolean success = false;
143 			if (dialog.item == null) success = dialog.createItem();
144 			else success = dialog.editItem();
145 			if (!success) {
146 				String alert = (item == null ? Resources.getTranslation("error_create_item") :
147 											   Resources.getTranslation("error_modify_item"));
148 				alert += " " + Resources.getTranslation("error_try_again_item");
149 				JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"),
150 											  JOptionPane.ERROR_MESSAGE);
151 			} else {
152 				((RBManagerGUI)dialog.getParent()).updateDisplayPanels();
153 				((RBManagerGUI)dialog.getParent()).invalidate();
154 				//((RBManagerGUI)dialog.getParent()).validateMyTree();
155 				dialog.setVisible(false);
156 				dialog.dispose();
157 			}
158 		} else if (ev.getKeyCode() == KeyEvent.VK_ESCAPE) {
159 			closeWindow();
160 		}
161 	}
162 
initComponents()163 	private void initComponents(){
164 		enableEvents(AWTEvent.KEY_EVENT_MASK);
165 		// Error check
166 		if (rbm == null || rbm.getBundles() == null) {
167 			String alert = Resources.getTranslation("error_no_bundle_for_item");
168 			JOptionPane.showMessageDialog(this, alert, Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
169 			closeWindow();
170 			return;
171 		}
172 
173 		// Initialize values
174 		Bundle mainBundle = (Bundle)rbm.getBundles().firstElement();
175 		if (firstInit) {
176 			groupComboBox = new JComboBox(mainBundle.getGroupsAsVector());
177 			if (groupName != null) {
178 				for (int i = 0; i < groupComboBox.getItemCount(); i++) {
179 					BundleGroup bg = (BundleGroup)groupComboBox.getItemAt(i);
180 					if (bg.getName().equals(groupName)) {
181 						groupComboBox.setSelectedIndex(i);
182 						break;
183 					}
184 				}
185 			}
186 		}
187 
188 		if (firstInit && item != null) {
189 			// We are editing, not creating an item
190 			createButton.setText(Resources.getTranslation("button_edit"));
191 			createMoreButton.setText(Resources.getTranslation("button_edit_more"));
192 			if (item.getKey() != null) nameField.setText(item.getKey());
193 			if (item.getComment() != null) commentField.setText(item.getComment());
194 			if (item.getTranslation() != null) transField.setText(item.getTranslation());
195 			if (item.getLookups() != null) lookups = item.getLookups();
196 		}
197 
198 		String currentTrans = transField.getText();
199 		// ** LOOKUPS **
200 		// Update the lookups if necessary
201 		if (lookupBoxes != null) {
202 			for (int i=0; i < lookupBoxes.length; i++) {
203 				String nameText = lookupLabels[i].getText().trim();
204 				String name = nameText.substring(nameText.indexOf("{")+1,nameText.indexOf("}"));
205 				String value = lookupFields[i].getText().trim();
206 				lookups.put(name,value);
207 			}
208 		}
209 		// Remove old lookups if necessary
210 		Enumeration keys = lookups.keys();
211 		while (keys.hasMoreElements()) {
212 			String name = (String)keys.nextElement();
213 			if (currentTrans.indexOf("{" + name + "}") < 0) {
214 				lookups.remove(name);
215 			}
216 		}
217 		// Add new lookups if necessary
218 		if (currentTrans != null && currentTrans.indexOf("{") >= 0) {
219 			while (currentTrans.indexOf("{") >= 0) {
220 				currentTrans = currentTrans.substring(currentTrans.indexOf("{")+1,currentTrans.length());
221 				String name = currentTrans.substring(0,currentTrans.indexOf("}"));
222 				if (!lookups.containsKey(name)) {
223 					lookups.put(name,"");
224 				}
225 			}
226 		}
227 		// Remove components
228 		box5.removeAll();
229 
230 		// Now create the visual components for the lookups
231 		if (lookups.size() > 0) {
232 			noLookupLabel = null;
233 			lookupBox = new Box(BoxLayout.Y_AXIS);
234 			lookupBoxes = new Box[lookups.size()];
235 			lookupFields = new JTextField[lookups.size()];
236 			lookupLabels = new JLabel[lookups.size()];
237 			int count = 0;
238 			keys = lookups.keys();
239 			while (keys.hasMoreElements()) {
240 				String name = (String)keys.nextElement();
241 				String value = (String)lookups.get(name);
242 				RBManagerGUI.debugMsg("Lookup: " + name + " -> " + value);
243 				RBManagerGUI.debugMsg(lookups.toString());
244 				lookupBoxes[count] = new Box(BoxLayout.X_AXIS);
245 				lookupFields[count] = new JTextField((value == null ? "" : value));
246 				lookupLabels[count] = new JLabel("{" + name + "}");
247 				lookupBoxes[count].add(Box.createHorizontalGlue());
248 				lookupBoxes[count].add(lookupLabels[count]);
249 				lookupBoxes[count].add(Box.createHorizontalStrut(5));
250 				lookupBoxes[count].add(lookupFields[count]);
251 				lookupBox.add(lookupBoxes[count]);
252 				count++;
253 			}
254 		} else {
255 			lookupBox = null;
256 			lookupBoxes = null;
257 			lookupFields = null;
258 			lookupLabels = null;
259 			noLookupLabel = new JLabel(Resources.getTranslation("none"));
260 		}
261 
262 		// Set up the components
263 		if (firstInit) {
264 			groupLabel.setPreferredSize(leftDim);
265 			groupComboBox.setPreferredSize(rightDim);
266 			nameLabel.setPreferredSize(leftDim);
267 			nameField.setColumns(30);
268 			commentLabel.setPreferredSize(leftDim);
269 			commentField.setColumns(30);
270 			transLabel.setPreferredSize(leftDim);
271 			transField.setColumns(30);
272 			lookupLabel.setPreferredSize(leftDim);
273 
274 			box1.add(groupLabel); box1.add(groupComboBox);
275 			box2.add(nameLabel); box2.add(nameField);
276 			box4.add(commentLabel); box4.add(commentField);
277 			box3.add(transLabel); box3.add(transField);
278 
279 			createButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_create_trigger")));
280 			createMoreButton.setMnemonic(RBManagerMenuBar.getKeyEventKey(Resources.getTranslation("button_create_more_trigger")));
281 			getRootPane().setDefaultButton(createButton);
282 		}
283 		box5.add(Box.createHorizontalGlue()); box5.add(lookupLabel); box5.add(Box.createHorizontalStrut(5));
284 		if (noLookupLabel != null) {
285 			noLookupLabel.setPreferredSize(rightDim);
286 			box5.add(noLookupLabel);
287 		}
288 		else
289 			box5.add(lookupBox);
290 		if (firstInit) {
291 			box6.add(createButton);
292 			box6.add(Box.createHorizontalStrut(5));
293 			if (item == null)
294 				box6.add(createMoreButton);
295 			box6.add(Box.createHorizontalStrut(5));
296 			box6.add(cancelButton);
297 		}
298 
299 		instructionsLabel.setBorder(BorderFactory.createEtchedBorder());
300 
301 		// Add the appropriate listeners
302 		if (firstInit) {
303 			cancelButton.addActionListener(new ActionListener() {
304 				public void actionPerformed(ActionEvent ev) {
305 					JDialog dialog = (JDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
306 					dialog.setVisible(false);
307 					dialog.dispose();
308 				}
309 			});
310 
311 			createButton.addActionListener(new ActionListener() {
312 				public void actionPerformed(ActionEvent ev) {
313 					BundleItemCreationDialog dialog =
314 						(BundleItemCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
315 					boolean success = false;
316 					if (dialog.item == null) success = dialog.createItem();
317 					else success = dialog.editItem();
318 					if (!success) {
319 						String alert = (item == null ? Resources.getTranslation("error_create_item") :
320 													   Resources.getTranslation("error_modify_item"));
321 						alert += " " + Resources.getTranslation("error_try_again_item");
322 						JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"),
323 													  JOptionPane.ERROR_MESSAGE);
324 					} else {
325 						((RBManagerGUI)dialog.getParent()).updateDisplayPanels();
326 						((RBManagerGUI)dialog.getParent()).invalidate();
327 						//((RBManagerGUI)dialog.getParent()).validateMyTree();
328 						dialog.setVisible(false);
329 						dialog.dispose();
330 					}
331 				}
332 			});
333 
334 			createMoreButton.addActionListener(new ActionListener() {
335 				public void actionPerformed(ActionEvent ev) {
336 					BundleItemCreationDialog dialog =
337 						(BundleItemCreationDialog)((JButton)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
338 					boolean success = false;
339 					if (dialog.item == null) success = createItem();
340 					else success = dialog.editItem();
341 					if (!success) {
342 						String alert = (item == null ? Resources.getTranslation("error_create_item") :
343 													   Resources.getTranslation("error_modify_item"));
344 						alert += " " + Resources.getTranslation("error_try_again_item");
345 						JOptionPane.showMessageDialog(dialog, alert, Resources.getTranslation("error"),
346 													  JOptionPane.ERROR_MESSAGE);
347 					} else {
348 						((RBManagerGUI)dialog.getParent()).updateDisplayPanels();
349 						((RBManagerGUI)dialog.getParent()).invalidate();
350 						//((RBManagerGUI)dialog.getParent()).validateMyTree();
351 						dialog.clearComponents();
352 					}
353 				}
354 			});
355 
356 			transField.addFocusListener(new FocusListener() {
357 				public void focusGained(FocusEvent ev) {}
358 				public void focusLost(FocusEvent ev) {
359 					BundleItemCreationDialog dialog =
360 						(BundleItemCreationDialog)((JTextField)ev.getSource()).getParent().getParent().getParent().getParent().getParent().getParent();
361 					firstInit = false;
362 					dialog.initComponents();
363 				}
364 			});
365 		}
366 
367 		// Complete the initialization of the frame
368 		if (firstInit)
369 			setLocation(new java.awt.Point(50, 50));
370 		mainBox.removeAll();
371 		//mainBox.add(instructionsLabel);
372 		mainBox.add(Box.createVerticalStrut(5));
373 		mainBox.add(box1);
374 		mainBox.add(Box.createVerticalStrut(5));
375 		mainBox.add(box2);
376 		mainBox.add(Box.createVerticalStrut(5));
377 		mainBox.add(box3);
378 		mainBox.add(Box.createVerticalStrut(5));
379 		mainBox.add(box4);
380 		mainBox.add(Box.createVerticalStrut(5));
381 		if (noLookupLabel == null) {
382 			mainBox.add(box5);
383 			mainBox.add(Box.createVerticalStrut(5));
384 		}
385 		mainBox.add(box6);
386 		getContentPane().add(mainBox, BorderLayout.CENTER);
387 		validateTree();
388 		pack();
389 		setVisible(true);
390 		//setResizable(false);
391 		firstInit = false;
392 	}
393 
closeWindow()394 	void closeWindow() {
395 		setVisible(false);
396 		dispose();
397 	}
398 }
399 
400