View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.hint;
5   
6   import java.awt.BorderLayout;
7   import java.awt.CardLayout;
8   import java.awt.Color;
9   import java.awt.Dimension;
10  import java.awt.Font;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  import java.util.Iterator;
14  
15  import javax.swing.AbstractAction;
16  import javax.swing.BorderFactory;
17  import javax.swing.BoxLayout;
18  import javax.swing.ImageIcon;
19  import javax.swing.JButton;
20  import javax.swing.JFrame;
21  import javax.swing.JLabel;
22  import javax.swing.JPanel;
23  import javax.swing.JTextArea;
24  
25  import org.telscenter.pas.common.ui.CommonUI;
26  import org.telscenter.pas.common.ui.border.CustomLineBorder;
27  import org.telscenter.pas.common.ui.layouts.EqualsLayout;
28  import org.telscenter.pas.common.ui.panel.SimpleGradientPanel;
29  import org.telscenter.pas.hint.actions.HintNextAction;
30  import org.telscenter.pas.hint.actions.HintPrevAction;
31  import org.telscenter.pas.properties.HintSet;
32  import org.telscenter.pas.service.Messages;
33  import org.telscenter.pas.steps.actions.DialogSaveAction;
34  import org.telscenter.pas.ui.util.PasColors;
35  
36  /***
37   * @author aperritano
38   *
39   */
40  public class HintUI extends JPanel {
41  	
42  	private HintSet hintSet;
43  
44  	public HintUI(HintSet hintSet) {
45  		this.hintSet = hintSet;
46  		init();
47  	}
48  	protected void init() {
49  		this.setLayout(new BorderLayout(0,0));
50  		this.add(createPandaPanel(),BorderLayout.NORTH);
51  		this.add(createMainArea(),BorderLayout.CENTER);
52  	}
53  	
54  	/***
55  	 * @return
56  	 */
57  	protected JPanel createMainArea() {
58  		if( hintSet.isEmpty()) {
59  			JPanel panel = new JPanel(new BorderLayout(0,0));
60  			
61  			JLabel noHintsLabel = new JLabel("This step has no hints");
62  			noHintsLabel.setOpaque(false);
63  			panel.add(noHintsLabel,BorderLayout.CENTER);
64  			panel .setBackground(PasColors.hintBackgroundColor);
65  			return panel;
66  		}// if
67  		
68  		
69  		JPanel buttonPanel = new JPanel(new EqualsLayout(
70  				EqualsLayout.HORIZONTAL, EqualsLayout.RIGHT, 2));
71  
72  		JPanel cards = createMessageCards();
73  
74  		if (hintSet.size() > 1) {
75  			JButton prevButton = createHintButton(new HintPrevAction("Previous", //$NON-NLS-1$
76  					null, cards));
77  			JButton nextButton = createHintButton(new HintNextAction("Next", //$NON-NLS-1$
78  					null, cards));
79  			buttonPanel.add(prevButton);
80  			buttonPanel.add(nextButton);
81  		}
82  		JButton closeButton = createHintButton(new DialogSaveAction("Close")); //$NON-NLS-1$
83  
84  		buttonPanel.add(closeButton);
85  		buttonPanel.setOpaque(false);
86  		buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 15));
87  		SimpleGradientPanel outerPanel = new SimpleGradientPanel(new BorderLayout(0,0));
88  		outerPanel.setDirection(SimpleGradientPanel.VERTICAL);
89  		outerPanel.setStartColor(PasColors.pandaPanelBackgroundColor);
90  		outerPanel.setEndColor(Color.white);
91  		outerPanel.add(cards, BorderLayout.CENTER);
92  		outerPanel.add(buttonPanel, BorderLayout.SOUTH);
93  		outerPanel.setPreferredSize(new Dimension(300, (int) outerPanel
94  				.getPreferredSize().getHeight()));
95  		outerPanel.setBackground(PasColors.hintBackgroundColor);
96  		return outerPanel;
97  	}
98  	
99  	
100 	private JPanel createMessageCards() {
101 		JPanel cards = new JPanel(new CardLayout());
102 		cards.setOpaque(false);
103 		if (hintSet != null) {
104 			JPanel messagePanel;
105 
106 			int hintCount = hintSet.size();
107 
108 			int ordinal = 0;
109 			for (Hint hint : hintSet) {
110 				ordinal += 1;
111 				if (hint != null) {
112 					JLabel hintTitle;
113 					if (hintCount != 1) {
114 						hintTitle = new JLabel(
115 								Messages.getString("HintService.6") + ordinal + Messages.getString("HintService.7") //$NON-NLS-1$ //$NON-NLS-2$
116 										+ hintCount);
117 						
118 					} else {
119 						hintTitle = new JLabel(Messages
120 								.getString("HintService.8")); //$NON-NLS-1$
121 					}
122 
123 //					hintTitle.setBorder(BorderFactory.createEmptyBorder(4, 0,
124 //							4, 0));
125 //					Font font = new Font("Default", Font.BOLD, 24); //$NON-NLS-1$
126 //					hintTitle.setFont(font);
127 					
128 					hintTitle.setFont(hintTitle.getFont().deriveFont(Font.ITALIC,20f));
129 					hintTitle.setForeground(Color.yellow);
130 					hintTitle.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
131 					hintTitle.setOpaque(false);
132 					JTextArea messageArea = new JTextArea(hint.getText());
133 					
134 					messageArea.setEditable(false);
135 					messageArea.setWrapStyleWord(true);
136 					messageArea.setLineWrap(true);
137 					messageArea.setOpaque(false);
138 					
139 					
140 					messageArea.setForeground(Color.black);
141 					messageArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
142 					messagePanel = new JPanel(new BorderLayout(0,0));
143 					messagePanel.add(hintTitle, BorderLayout.NORTH);
144 					messagePanel.add(messageArea, BorderLayout.CENTER);
145 					messagePanel.setOpaque(false);
146 					//messagePanel.setBorder(BorderFactory.createEmptyBorder(8, 4, 8, 4));
147 					cards.add(messagePanel, Integer.toString(ordinal));
148 				}
149 			}
150 
151 		}// if
152 		CardLayout cl = (CardLayout) (cards.getLayout());
153 		cl.show(cards, "1"); //$NON-NLS-1$
154 		return cards;
155 	}
156 
157 	private JButton createHintButton(AbstractAction action) {
158 		JButton button = new JButton(action);
159 		CommonUI.makeEnhancedButton(button);
160 		return button;
161 	}
162 	
163 	protected JPanel createPandaPanel() {
164 		JPanel pandaPanel = new JPanel(new BorderLayout(0,0));
165 		pandaPanel.setOpaque(false);
166 		
167 		JLabel hintLabel = new JLabel("Panda Hints");
168 		hintLabel.setFont(hintLabel.getFont().deriveFont(Font.ITALIC,24f));
169 		hintLabel.setToolTipText("useful stuff");
170 		hintLabel.setOpaque(false);
171 		hintLabel.setForeground(Color.yellow);
172 		hintLabel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
173 		JLabel pandaButton = new JLabel(new ImageIcon(this.getClass().getResource("icons/pandaTalkIcon.png")));
174 		pandaButton.setToolTipText("hi :)");
175 		//this.modButton(pandaButton);
176 		pandaButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
177 		pandaPanel.add(hintLabel,BorderLayout.WEST);
178 		pandaPanel.add(pandaButton,BorderLayout.EAST);
179 		
180 		
181 		JPanel mainPanel = new JPanel();
182 		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
183 		mainPanel.setBackground(PasColors.pandaPanelBackgroundColor);
184 		mainPanel.add(pandaPanel);
185 		
186 		//da stripe
187 		JPanel innerStripedPanel = new JPanel(new BorderLayout(0,0));
188 		innerStripedPanel.setBackground(PasColors.innerStripBackgroundColor);
189 		innerStripedPanel.setBorder(new CustomLineBorder(PasColors.outerStripeBackgroundColor,2,Color.white,0,PasColors.outerStripeBackgroundColor,2,PasColors.outerStripeBackgroundColor,0 ));
190 		
191 		innerStripedPanel.setPreferredSize(new Dimension(innerStripedPanel.getPreferredSize().width,6));
192 		innerStripedPanel.setMaximumSize(new Dimension(innerStripedPanel.getMaximumSize().width,6));
193 		
194 		mainPanel.add(innerStripedPanel);
195 		mainPanel.setMaximumSize(new Dimension(mainPanel.getMaximumSize().width,innerStripedPanel.getPreferredSize().height));
196 		return mainPanel;
197 	}
198 	
199 	  public static void main(String args[]){
200 		  JFrame frame = new JFrame("Hint Test");
201 			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
202 			
203 			
204 			JButton b = new JButton("show hint");
205 			b.addActionListener(new ActionListener() {
206 
207 				public void actionPerformed(ActionEvent e) {
208 					JFrame frame = new JFrame();
209 					frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
210 					
211 					frame.getContentPane().add(new HintUI(null));
212 					frame.pack();
213 					frame.setSize(500,500);
214 					frame.setVisible(true);
215 				}});
216 			
217 		
218 			
219 			frame.getContentPane().add(b);
220 			frame.pack();
221 			frame.setSize(500,500);
222 			frame.setVisible(true);
223 	  }
224 	
225 
226 }