View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.steps;
5   
6   import java.awt.BorderLayout;
7   import java.awt.Color;
8   import java.awt.FlowLayout;
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.awt.event.WindowAdapter;
12  import java.awt.event.WindowEvent;
13  import java.util.Collection;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.Set;
17  import java.util.Map.Entry;
18  
19  import javax.swing.Action;
20  import javax.swing.BorderFactory;
21  import javax.swing.ImageIcon;
22  import javax.swing.JButton;
23  import javax.swing.JDialog;
24  import javax.swing.JLabel;
25  import javax.swing.JPanel;
26  import javax.swing.JScrollPane;
27  import javax.swing.JTabbedPane;
28  import javax.swing.JTextArea;
29  import javax.swing.event.ChangeEvent;
30  import javax.swing.event.ChangeListener;
31  
32  import org.apache.commons.lang.StringUtils;
33  import org.telscenter.pas.common.ui.CommonUI;
34  import org.telscenter.pas.common.ui.tab.PasTabbedPaneUI;
35  import org.telscenter.pas.common.ui.text.CurrentLineHighlighter;
36  import org.telscenter.pas.steps.actions.NoteSaveAction;
37  import org.telscenter.pas.steps.domain.BlockInteraction;
38  import org.telscenter.pas.steps.domain.ExtendedTextInteraction;
39  import org.telscenter.pas.ui.dialog.PasDialogManager;
40  import org.telscenter.pas.ui.dialog.PasMessageDialogUI;
41  import org.telscenter.pas.ui.dialog.PasNoteMessageDialogUI;
42  import org.telscenter.pas.ui.icons.PasCommonIconProvider;
43  import org.telscenter.pas.ui.util.PasColors;
44  
45  /***
46   * @author aperritano
47   * 
48   */
49  public class NoteUI extends AssessmentUI implements INoteUI {
50  
51  	private boolean noteOpen;
52  	
53  	private boolean noteSaveConfirmed;
54  
55  	private boolean noteSavePartsConfirmed;
56  	
57  	/***
58  	 * 
59  	 */
60  	public NoteUI(Assessment note) {
61  		super(note);
62  	}
63  
64  	/***
65  	 * 
66  	 */
67  	protected void initUI() {
68  		assessment.setTabbed(true);
69  		if (assessment.getAssessmentItem() == null
70  				|| assessment.getAssessmentItem().getItemBody()
71  						.getInteractions().size() == 0) {
72  			this.setLayout(new FlowLayout(FlowLayout.CENTER));
73  			this.setOpaque(false);
74  			JLabel noLabel = new JLabel("This Note has no items.");
75  			noLabel.setOpaque(false);
76  			this.add(noLabel, BorderLayout.CENTER);
77  			return;
78  		}
79  
80  		List<BlockInteraction> interactions = assessment.getAssessmentItem()
81  				.getItemBody().getInteractions();
82  
83  		this.setLayout(new BorderLayout(0, 0));
84  		this.setBackground(PasColors.noteBackgroundColor);
85  
86  		tabbedPane = new JTabbedPane();
87  		tabbedPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
88  		tabbedPane.setBackground(PasColors.noteTabBackgrounColor);
89  		tabbedPane.setUI(new PasTabbedPaneUI());
90  		tabbedPane.addChangeListener(new ChangeListener() {
91  
92  			public void stateChanged(ChangeEvent e) {
93  
94  				if (nextTabButton != null || previousTabButton != null)
95  					checkTab();
96  
97  			}
98  		});
99  
100 		if (interactions.size() > 1) {
101 			showTabButtons = true;
102 		} else {
103 			showTabButtons = false;
104 		}// if
105 
106 		int i = 0;
107 		for (BlockInteraction interaction : interactions) {
108 			JPanel itemPanel = createAssessmentItemPanel(interaction);
109 			itemPanel.setOpaque(true);
110 			itemPanel
111 					.setBackground(PasColors.noteTextInteractionFillPanelBackgrounColor);
112 			// itemPanel.setBorder(BorderFactory.createLineBorder(Color.white,
113 			// 2));
114 
115 			// JPanel wpanel = new JPanel(new BorderLayout(0, 0));
116 			JPanel wpanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
117 			wpanel
118 					.setBackground(PasColors.noteTextInteractionFillPanelBackgrounColor);
119 
120 			// adjust the borders if there is more than one tab
121 			if (showTabButtons) {
122 				wpanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 0, 2));
123 
124 			} else {
125 				wpanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
126 			}
127 			wpanel.putClientProperty(ITEM_COMPLETED, new Boolean(false));
128 			wpanel.add(itemPanel, BorderLayout.CENTER);
129 
130 			JScrollPane scrollPane = new JScrollPane();
131 			scrollPane.getViewport().add(wpanel);
132 			scrollPane
133 					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
134 			scrollPane
135 					.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
136 			scrollPane.setBorder(BorderFactory.createEmptyBorder());
137 			scrollPane
138 					.setBackground(PasColors.noteTextInteractionFillPanelBackgrounColor);
139 			scrollPane
140 					.setBorder(BorderFactory.createLineBorder(Color.white, 2));
141 			interactionsToTabindex.put(interaction, new Integer(i));
142 			tabbedPane.addTab("Part " + (i + 1), scrollPane);
143 
144 			i += 1;
145 		}
146 
147 		// install highlighters
148 		Collection<JTextArea> textAreas = textInteractionsToTextareas.values();
149 		for (JTextArea ta : textAreas) {
150 			CurrentLineHighlighter.install(ta);
151 		}
152 
153 		checkTabsAndPlaceHolder();
154 
155 		JPanel comboPanel = new JPanel(new BorderLayout(0, 0));
156 		comboPanel.setOpaque(false);
157 		comboPanel.add(tabbedPane, BorderLayout.CENTER);
158 		comboPanel.setBorder(BorderFactory.createEmptyBorder(0, 2, 4, 2));
159 
160 		JPanel innerButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
161 		innerButtonPanel
162 				.setBackground(PasColors.noteTextInteractionFillPanelBackgrounColor);
163 
164 		previousTabButton = new JButton(
165 				new PreviousTabAction("prev", new ImageIcon(
166 						PasCommonIconProvider.getImage("previousTab.png"))));
167 		previousTabButton.setHorizontalAlignment(JButton.RIGHT);
168 		previousTabButton.setVerticalAlignment(JButton.TOP);
169 		previousTabButton.setVerticalTextPosition(JButton.TOP);
170 
171 		nextTabButton = new JButton(new NextTabAction("next", new ImageIcon(
172 				PasCommonIconProvider.getImage("nextTab.png"))));
173 		nextTabButton.setHorizontalTextPosition(JButton.LEFT);
174 		nextTabButton.setVerticalAlignment(JButton.TOP);
175 		nextTabButton.setVerticalTextPosition(JButton.TOP);
176 		// check the tabs
177 		this.checkTab();
178 
179 		innerButtonPanel.add(CommonUI.makeEnhancedButton(previousTabButton));
180 		innerButtonPanel.add(CommonUI.makeEnhancedButton(nextTabButton));
181 
182 		JPanel shifterButtonsPanel = new JPanel(new BorderLayout(0, 0));
183 		shifterButtonsPanel.setBackground(Color.WHITE);
184 		shifterButtonsPanel.add(innerButtonPanel, BorderLayout.CENTER);
185 		shifterButtonsPanel.setBorder(BorderFactory.createEmptyBorder(0, 2, 2,
186 				2));
187 
188 		if (showTabButtons)
189 			comboPanel.add(shifterButtonsPanel, BorderLayout.SOUTH);
190 
191 		this.add(comboPanel, BorderLayout.CENTER);
192 
193 		// this.add(this,BorderLayout.SOUTH);
194 
195 		final NoteSaveAction saveAction = getSaveAction();
196 		JButton saveButton = new JButton(saveAction.getValue(Action.NAME)
197 				.toString());
198 		saveButton.setToolTipText("Save this note");
199 		saveButton.addActionListener(new ActionListener() {
200 
201 			public void actionPerformed(ActionEvent e) {
202 
203 				if (isCompleted()) {
204 					saveAction.actionPerformed(e);
205 				} else {
206 					saveAction.actionPerformed(e);
207 					// showNotePartsCheckDialog();
208 				}// if
209 
210 			}
211 		});
212 		CommonUI.makeEnhancedButton(saveButton);
213 
214 		JButton hintButton = new JButton();
215 		CommonUI.makeHintbutton(hintButton);
216 		hintButton.setEnabled(assessment.hasHints());
217 		hintButton.addActionListener(new ActionListener() {
218 
219 			public void actionPerformed(ActionEvent e) {
220 				
221 				final JButton b = (JButton) e.getSource();
222 				b.setSelected(true);
223 				b.setEnabled(false);
224 				
225 				JDialog showHintDialog = CommonUI.showHintDialog(assessment.getHintSet(), null);
226 				showHintDialog.addWindowListener(new WindowAdapter() {
227 
228 					public void windowOpened(WindowEvent e) {
229 						
230 					}
231 
232 					public void windowActivated(WindowEvent arg0) {
233 
234 						
235 					};
236 
237 					public void windowClosed(WindowEvent arg0) {
238 						b.setEnabled(true);
239 					}
240 				});
241 			}
242 		});
243 
244 		// JPanel buttonPanel = new JPanel(new EqualsLayout(
245 		// EqualsLayout.HORIZONTAL, EqualsLayout.RIGHT, 0));
246 
247 		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 4, 2));
248 
249 		buttonPanel.add(hintButton);
250 		buttonPanel.add(saveButton);
251 
252 		buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 20));
253 
254 		buttonPanel.setBackground(PasColors.noteButtonPanelBackgroundColor);
255 		this.add(buttonPanel, BorderLayout.SOUTH);
256 		this.validate();
257 
258 	}
259 
260 	/***
261 	 * 
262 	 */
263 	protected void checkTabsAndPlaceHolder() {
264 		if (!assessment.getResponses().isEmpty()) {
265 
266 			Set<Entry<String, Object>> assessmentEntries = assessment
267 					.getResponses().entrySet();
268 			List<BlockInteraction> someInteractions = assessment
269 					.getAssessmentItem().getItemBody().getInteractions();
270 
271 			for (Map.Entry<String, Object> pairs : assessmentEntries) {
272 				String responseKey = pairs.getKey();
273 				String responseValue = (String) pairs.getValue();
274 
275 				responseValue = StringUtils.trimToNull(responseValue);
276 				if (responseValue != null && responseValue != null) {
277 					for (BlockInteraction bi : someInteractions) {
278 						if (bi.getResponseIdentifier().equals(responseKey)) {
279 							showTabChecked(interactionsToTabindex.get(bi)
280 									.intValue());
281 
282 							if (bi instanceof ExtendedTextInteraction)
283 								disablePlaceHolderButton(bi);
284 						}
285 					}
286 				}// if
287 			}// while
288 		}// if
289 	}
290 
291 	/***
292 	 * @param bi
293 	 */
294 	protected void disablePlaceHolderButton(BlockInteraction bi) {
295 		Set<Entry<JButton, ExtendedTextInteraction>> entrySet = placeholderButtonsToTextInteractions
296 				.entrySet();
297 		for (Entry<JButton, ExtendedTextInteraction> entry : entrySet) {
298 			BlockInteraction block = entry.getValue();
299 			if (block.equals(bi)) {
300 				JButton placeholderbutton = entry.getKey();
301 				placeholderbutton.setEnabled(false);
302 			}
303 		}
304 	}
305 
306 	public JDialog showNoteSaveCheckDialog() {
307 		final PasNoteMessageDialogUI ui = new PasNoteMessageDialogUI(
308 				getSaveAction());
309 		// TODO note save action NEEDs to ask the note service for
310 		// its info and have not passed in via constructor
311 		ui.setMessage("Do you want to save this note before closing?");
312 		return PasDialogManager.showPasMessageDialog("Question?", ui
313 				.createDialogPanel(), null, true);
314 	}
315 
316 	public JDialog showNotePartsCheckDialog() {
317 		final PasMessageDialogUI ui = new PasMessageDialogUI(true);
318 		// TODO note save action NEEDs to ask the note service for
319 		// its info and have not passed in via constructor
320 		ui.setMessage("You need to complete all note parts before saving");
321 		return PasDialogManager.showPasMessageDialog("Warning", ui
322 				.createDialogPanel(), null, true);
323 	}
324 
325 	public JDialog showNoteAlreadyOpenDialog() {
326 		final PasMessageDialogUI ui = new PasMessageDialogUI(true);
327 		// TODO note save action NEEDs to ask the note service for
328 		// its info and have not passed in via constructor
329 		ui.setMessage("You can only have one note open at a time.");
330 		return PasDialogManager.showPasMessageDialog("Warning", ui
331 				.createDialogPanel(), null, true);
332 	}
333 
334 	/***
335 	 * @return the noteOpen
336 	 */
337 	public boolean isNoteOpen() {
338 		return noteOpen;
339 	}
340 
341 	/***
342 	 * @param noteOpen
343 	 *            the noteOpen to set
344 	 */
345 	public void setNoteOpen(boolean openedNote) {
346 		this.noteOpen = openedNote;
347 	}
348 
349 	/***
350 	 * TODO maybe get rid of this (made for incremental refactoring)
351 	 * 
352 	 * @return
353 	 */
354 	public NoteSaveAction getSaveAction() {
355 		// mainPanel.requestFocus(true);
356 		return new NoteSaveAction(assessment.getAssessmentItem(), assessment
357 				.getResponses(), assessment.getResponseDeclarationToSocks());
358 	}
359 
360 
361 	
362 	public void setNoteSaveConfirmed(boolean noteSaveConfirmed){
363 		this.noteSaveConfirmed = noteSaveConfirmed;		
364 	}
365 
366 
367 	/***
368 	 * @return
369 	 */
370 	public boolean getNoteSaveConfirmed() {
371 		// TODO Auto-generated method stub
372 		return this.noteSaveConfirmed;
373 	}
374 
375 	/***
376 	 * @return
377 	 */
378 	public boolean getNoteSavePartsConfirmed() {
379 		// TODO Auto-generated method stub
380 		return this.noteSavePartsConfirmed;
381 	}
382 
383 	/***
384 	 * @param b
385 	 */
386 	public void setNoteSavePartsConfirmed(boolean b) {
387 		// TODO Auto-generated method stub
388 		this.noteSavePartsConfirmed = b;
389 	}
390 
391 }