View Javadoc

1   /***
2    * Created on Feb 8, 2006, Copyright UC Regents
3    */
4   package org.telscenter.pas.steps.quickEditors.hints;
5   
6   import info.clearthought.layout.TableLayout;
7   
8   import java.awt.BorderLayout;
9   import java.awt.Dimension;
10  import java.awt.Font;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.JLabel;
14  import javax.swing.JPanel;
15  import javax.swing.JScrollPane;
16  import javax.swing.JTextArea;
17  import javax.swing.SwingConstants;
18  import javax.swing.event.DocumentEvent;
19  import javax.swing.event.DocumentListener;
20  import javax.swing.text.BadLocationException;
21  import javax.swing.text.Document;
22  import javax.swing.undo.UndoManager;
23  
24  import org.telscenter.pas.hint.Hint;
25  import org.telscenter.pas.steps.quickEditors.qti.assessment.AbstractCardContainerUI;
26  import org.telscenter.pas.steps.quickEditors.qti.assessment.AbstractCardUI;
27  import org.telscenter.pas.ui.util.PasColors;
28  
29  /***
30   * @author aperritano
31   *
32   */
33  public class HintInteractionCardUI extends AbstractCardUI<Hint> {
34  
35  		private JTextArea textArea;
36  
37  		private JPanel formPanel;
38  
39  		private AbstractCardContainerUI<Hint> interactionCardContainerUI;
40  
41  		private UndoManager undoManager;
42  	
43  		
44  		public HintInteractionCardUI() {
45  			super();
46  
47  		}
48  
49  		public HintInteractionCardUI(Hint hint, UndoManager undoManager) {
50  			super();
51  			this.undoManager = undoManager;
52  			this.domainObject = hint;
53  			createMainUI();
54  		}
55  		
56  		protected void createMainUI() {
57  			this.setLayout(new BorderLayout(0, 0));
58  			this.setBackground(PasColors.hintInteractionCardBackgroundColor);
59  			this.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
60  			this.add(createCardUI(), BorderLayout.CENTER);
61  		}
62  
63  		public JPanel createCardUI() {
64  
65  			JPanel cardPanel = new JPanel(new BorderLayout(0, 0));
66  			cardPanel.setOpaque(false);
67  			cardPanel.add(createButtonTab(), BorderLayout.NORTH);
68  			cardPanel.add(createMainArea(), BorderLayout.CENTER);
69  
70  			return cardPanel;
71  		}
72  
73  		/***
74  		 * @return
75  		 */
76  		protected JPanel createMainArea() {
77  
78  			formPanel = new JPanel();
79  
80  			double pref = TableLayout.PREFERRED;
81  			double fill = TableLayout.FILL;
82  			double vSpace = 5;
83  			double hSpace = 6;
84  
85  			// this needs to be refactored
86  			// creates text or choice layout
87  				double size[][] = { { pref, hSpace, fill, // rt
88  				},
89  
90  				{ pref, vSpace, pref, vSpace, pref, vSpace }
91  
92  				};
93  				formPanel.setLayout(new TableLayout(size));
94  				createTextArea(formPanel);
95  			
96  
97  			formPanel.setBackground(PasColors.hintCardFormBackgroundColor);
98  			formPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
99  
100 			return formPanel;
101 		}
102 
103 
104 
105 		protected void createTextArea(JPanel hintTextPanel) {
106 			JLabel textLabel = new JLabel("Hint Text:");
107 			textLabel.setFont(textLabel.getFont().deriveFont(Font.BOLD));
108 			textLabel.setHorizontalAlignment(SwingConstants.LEFT);
109 			textLabel.setVerticalTextPosition(SwingConstants.NORTH);
110 			textLabel.setOpaque(false);
111 
112 			hintTextPanel.add(textLabel, "0,0,l,t");
113 
114 			textArea = new JTextArea(domainObject.getText());
115 			
116 			if( undoManager != null)
117 				textArea.getDocument().addUndoableEditListener(undoManager);
118 			
119 			textArea.getDocument().addDocumentListener(new DocumentListener(){
120 
121 				 public void insertUpdate(DocumentEvent e) {
122 				        updateLog(e, "inserted into");
123 				    }
124 				    public void removeUpdate(DocumentEvent e) {
125 				        updateLog(e, "removed from");
126 				    }
127 				    public void changedUpdate(DocumentEvent e) {
128 				    	updateLog(e, "changed from");
129 				    }
130 
131 				    public void updateLog(DocumentEvent e, String action) {
132 				        Document doc = e.getDocument();
133 				        try {
134 							domainObject.setText(doc.getText(0, doc.getLength()));
135 						} catch (BadLocationException e1) {
136 							// TODO Auto-generated catch block
137 							e1.printStackTrace();
138 						}
139 				    }
140 				    
141 			});
142 //			textArea.addKeyListener(new KeyAdapter() {
143 //
144 //				// FIXME this updates on each keystroke and it loses the last
145 //				// character
146 //				public void keyTyped(KeyEvent e) {
147 //					JTextArea source = (JTextArea) e.getSource();
148 //
149 //					String hintText = source.getText();
150 //					
151 //					domainObject.setText(hintText);
152 //				}
153 //			});
154 			
155 			//textArea.setBackground(PasColors.textEditorPanelTextComponentBackground);
156 			//TextEditorPanel te = new TextEditorPanel(textArea,new Dimension(400,200));
157 
158 			JScrollPane textAreaScroller = new JScrollPane(textArea);
159 			
160 			
161 			textAreaScroller.setPreferredSize(new Dimension(50, 75));
162 
163 			this.modScrollArea(textAreaScroller);
164 			hintTextPanel.add(textAreaScroller, "2,0");
165 			
166 			// promptPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
167 		}
168 
169 	
170 		/***
171 		 * @return Returns the hint.
172 		 */
173 		public Hint getHint() {
174 			return ((Hint) domainObject);
175 		}
176 
177 		/***
178 		 * @param hint The hint to set.
179 		 */
180 		public void setHint(Hint hint) {
181 			this.domainObject = hint;
182 		}
183 
184 }