View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.gui;
25  
26  import java.awt.BorderLayout;
27  import java.awt.Dimension;
28  import java.awt.Rectangle;
29  import java.awt.event.ActionEvent;
30  import java.awt.event.ActionListener;
31  import java.awt.event.WindowAdapter;
32  import java.awt.event.WindowEvent;
33  
34  import javax.swing.AbstractAction;
35  import javax.swing.ImageIcon;
36  import javax.swing.JFrame;
37  import javax.swing.JMenu;
38  import javax.swing.JMenuBar;
39  import javax.swing.JSplitPane;
40  import javax.swing.JToolBar;
41  import javax.swing.UIManager;
42  
43  import com.mindtree.techworks.insight.Controller;
44  import com.mindtree.techworks.insight.InsightConstants;
45  import com.mindtree.techworks.insight.ResourceManager;
46  import com.mindtree.techworks.insight.ShutdownHookManager;
47  import com.mindtree.techworks.insight.gui.action.ClearDisplayAction;
48  import com.mindtree.techworks.insight.gui.action.FilterAction;
49  import com.mindtree.techworks.insight.gui.action.FindAction;
50  import com.mindtree.techworks.insight.gui.action.IAction;
51  import com.mindtree.techworks.insight.gui.action.LoadFileAction;
52  import com.mindtree.techworks.insight.gui.action.LoadPageAction;
53  import com.mindtree.techworks.insight.gui.action.LocateAction;
54  import com.mindtree.techworks.insight.gui.action.MaintainPreferencesAction;
55  import com.mindtree.techworks.insight.gui.action.RemoteProtocolListenerAction;
56  import com.mindtree.techworks.insight.gui.action.ScrollLockAction;
57  import com.mindtree.techworks.insight.gui.action.SearchAction;
58  import com.mindtree.techworks.insight.gui.action.SearchEventAction;
59  import com.mindtree.techworks.insight.gui.action.StopReceiverAction;
60  import com.mindtree.techworks.insight.gui.util.AboutFrame;
61  import com.mindtree.techworks.insight.gui.util.LoadedNamespacesFrame;
62  import com.mindtree.techworks.insight.gui.widgets.InsightMenuItem;
63  import com.mindtree.techworks.insight.gui.widgets.InsightToolbarButton;
64  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
65  
66  /**
67  *
68  * The <code>Insight</code> class is the GUI application startup window.
69  * Contains all the GUI components that renders the application. Instantiates
70  * the Controller and the Receiver components.
71  *
72  * @see com.mindtree.techworks.insight.Controller
73  * @see com.mindtree.techworks.insight.gui.Presentation
74  * @see com.mindtree.techworks.insight.receiver.AbstractReceiver
75  *
76  * @author  Regunath B
77  * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
78  */
79  
80  public class Insight extends JFrame {
81  	
82  	/**
83  	 * UID for the serialized form
84  	 */
85  	private static final long serialVersionUID = 723734250598646319L;
86  	
87  	/**
88  	 * The instance of this class for anonymous inner classes
89  	 */
90  	protected final Insight instance;
91  
92  	/**
93  	 * Static block that initializes the display attributes of the JProgressBar
94  	 */
95  	static {
96  		UIManager.getDefaults().put("ProgressBar.selectionForeground",InsightConstants.DEFAULT_FOREGROUND);
97  		UIManager.getDefaults().put("ProgressBar.selectionBackground",InsightConstants.DEFAULT_FOREGROUND);
98  		UIManager.getDefaults().put("ProgressBar.font", InsightConstants.DEFAULT_SMALL_FONT);
99  	}	
100 	
101 	/**
102 	 * Controller instance instantiated by this class
103 	 */
104 	private Controller controller;
105 
106 	/**
107 	 * Constructor for this class. 
108 	 *
109 	 */
110 	public Insight() {
111 		this.instance = this;
112 		
113 		this.controller = new Controller(this);
114 		initialize();
115 	}
116 
117 	/**
118 	 * @return Returns the controller.
119 	 */
120 	public Controller getController() {
121 		return controller;
122 	}
123 
124 	/**
125 	 * Initializes this Insight
126 	 */
127 	private void initialize() {
128 		// setup the UI components of this frame
129 		setupThisFrame();
130 		// initialize the Presentation instances
131 		setupPresentations(controller);
132 		// handle closing of this frame
133         addWindowListener(new WindowAdapter() {
134             public void windowClosing(WindowEvent aEvent) {
135                 System.exit(0);
136             }
137         });
138         // Inform the LayoutManager to layout the components inside this frame
139         pack();       
140 		// Center Insight on the screen
141 		Dimension dim = getToolkit().getScreenSize();
142 	    Rectangle abounds = getBounds();
143 	    setLocation((dim.width - abounds.width) / 2,(dim.height - abounds.height) / 2);
144 	    // show this frame
145         setVisible(true);
146         
147         // Adding a shutdown hook for action to be taken before the
148         // application is closed.
149         ShutdownHookManager
150 				.addShutdownAction (ShutdownHookManager.SHUTDOWN_ACTION_CLEAR_PAGES
151 						| ShutdownHookManager.SHUTDOWN_ACTION_SAVE_PREFERENCES);
152 	}
153 
154 	/**
155 	 * Private helper method that sets up the Presentation instances that display
156 	 * the GUI.
157 	 * @param controller the Controller instance that mediates between the Presentation instances
158 	 */
159 	private void setupPresentations(Controller controller) {
160 		EventListPresentation eventListPresentation = new EventListPresentation(controller);
161 		EventDetailsPresentation eventDetailsPresentation = new EventDetailsPresentation(controller);
162         JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, eventListPresentation.getViewComponent(),
163         		eventDetailsPresentation);
164         getContentPane().add(splitPane, BorderLayout.CENTER);
165 	}
166 
167 	/**
168 	 * Private helper method to setup the GUI of this Frame
169 	 */
170 	private void setupThisFrame() {
171 		ResourceManager rm = ResourceManager.getInstance();
172 		this.setTitle(InsightConstants.FRAME_TITLE);
173 		this.setIconImage(rm.loadImageIcon("INSIGHT_ICON").getImage());
174         final JMenuBar menuBar = new JMenuBar();
175         final JToolBar toolBar = new JToolBar();
176         toolBar.setRollover(true);
177         toolBar.setFloatable(false);
178 
179         setJMenuBar(menuBar);
180         this.getContentPane().add(toolBar, BorderLayout.NORTH);
181 
182         setupFileMenu(menuBar, toolBar);
183         setupEditMenu(menuBar, toolBar);
184         setupViewMenu(menuBar, toolBar);
185         setupPreferencesMenu(menuBar, toolBar);
186         setupHelpMenu(menuBar, toolBar);
187         
188         this.getContentPane().add(StatusBar.getInstance(), BorderLayout.SOUTH);
189 	}
190 
191 	/**
192 	 * Helper method that setps up the File menu and tool bars
193 	 * @param menuBar the JMenuBar instance to setup
194 	 * @param toolBar the JToolBar to setup
195 	 */
196 	private void setupFileMenu(JMenuBar menuBar, JToolBar toolBar) {
197 		ResourceManager rm = ResourceManager.getInstance();
198         final JMenu fileMenu = new JMenu(InsightConstants.getLiteral("FILE_MENU"));
199         menuBar.add(fileMenu);
200 
201         LoadFileAction loadFileAction = new LoadFileAction(this);
202         fileMenu.add(getMenuItem(IAction.STARTUP, null, InsightConstants.getLiteral("LOAD_FILE"),loadFileAction));
203         toolBar.add(getToolbarButton(IAction.STARTUP, null, rm.loadImageIcon("OPEN_ICON"),InsightConstants.getLiteral("OPEN_FILE_TOOL_TIP"),loadFileAction));
204 
205         RemoteProtocolListenerAction remoteAction = RemoteProtocolListenerAction.getInstance();
206         remoteAction.initialize(this);
207         fileMenu.add(getMenuItem(IAction.STARTUP, null, InsightConstants.getLiteral("START_RECEIVER"),remoteAction));
208         toolBar.add(getToolbarButton(IAction.STARTUP, null, rm.loadImageIcon("RECEIVER_ICON"),InsightConstants.getLiteral("START_RECEIVER_TOOL_TIP"),remoteAction));
209         
210         // separate the Exit menu item - the last, from other items in the menu
211         fileMenu.insertSeparator(fileMenu.getMenuComponentCount());
212         fileMenu.add(getMenuItem(IAction.ALL, null, InsightConstants.getLiteral("EXIT"),
213         	new AbstractAction(){
214 	        	/**
215 				 * SVUID
216 				 */
217 				private static final long serialVersionUID = 718096549365310016L;
218 
219 				/**
220 				 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
221 				 */
222 				public void actionPerformed(ActionEvent actionEvent) {
223 	        		System.exit(0);
224 	        	}
225         	}
226         ));
227         // add a separator at the end of this tool bar button group
228         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
229 	}
230 
231 	/**
232 	 * Helper method that setps up the Edit menu and tool bars
233 	 * @param menuBar the JMenuBar instance to setup
234 	 * @param toolBar the JToolBar to setup
235 	 */
236 	private void setupEditMenu(JMenuBar menuBar, JToolBar toolBar) {
237 		ResourceManager rm = ResourceManager.getInstance();
238         final JMenu editMenu = new JMenu(InsightConstants.getLiteral("EDIT_MENU"));
239         menuBar.add(editMenu);
240 
241         StopReceiverAction stopReceiverAction = new StopReceiverAction(this);
242         editMenu.add(getMenuItem(IAction.TAIL, null, InsightConstants.getLiteral("STOP_RECEIVER"),stopReceiverAction));
243         toolBar.add(getToolbarButton(IAction.TAIL, null, rm.loadImageIcon("STOP_ICON"), InsightConstants.getLiteral("STOP_RECEIVER_TOOL_TIP"),stopReceiverAction));              
244         
245         ClearDisplayAction clearDisplayAction = new ClearDisplayAction(this);
246         editMenu.add(getMenuItem(IAction.MUTATION, null, InsightConstants.getLiteral("CLEAR_DISPLAY"),clearDisplayAction));
247         toolBar.add(getToolbarButton(IAction.MUTATION, null, rm.loadImageIcon("CLEAR_ICON"), InsightConstants.getLiteral("CLEAR_ENTRIES_TOOL_TIP"),clearDisplayAction));
248 
249         FilterAction filterAction = new FilterAction(this);
250         editMenu.add(getMenuItem(IAction.MUTATION, null, InsightConstants.getLiteral("FILTER_DATA"),filterAction));
251         toolBar.add(getToolbarButton(IAction.MUTATION, null, rm.loadImageIcon("FILTER_ICON"), InsightConstants.getLiteral("FILTER_ENTRIES_TOOL_TIP"),filterAction));
252 
253         // add a separator to separate mutating actions from non-mutating actions that follow
254         // in the tool bar and in the menu
255         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
256         editMenu.insertSeparator(editMenu.getMenuComponentCount());
257         
258         SearchAction searchAction = SearchAction.getInstance(this);
259         editMenu.add(getMenuItem(IAction.VIEW, null, InsightConstants.getLiteral("SEARCH_DATA"),searchAction));
260         toolBar.add(getToolbarButton(IAction.VIEW, null, rm.loadImageIcon("SEARCH_ICON"), InsightConstants.getLiteral("SEARCH_TOOL_TIP"),searchAction));
261 
262         LocateAction locateAction = LocateAction.getInstance(this.controller);
263         editMenu.add(getMenuItem(IAction.VIEW, null, InsightConstants.getLiteral("LOCATE_EVENT"),locateAction));
264         toolBar.add(getToolbarButton(IAction.VIEW, null, rm.loadImageIcon("LOCATE_ICON"), InsightConstants.getLiteral("LOCATE_TOOL_TIP"),locateAction));
265 
266         FindAction findAction = FindAction.getInstance(this);
267         editMenu.add(getMenuItem(IAction.VIEW, null, InsightConstants.getLiteral("FIND_DATA"),findAction));
268         toolBar.add(getToolbarButton(IAction.VIEW, null, rm.loadImageIcon("FIND_ICON"), InsightConstants.getLiteral("FIND_TOOL_TIP"),findAction));
269         
270         // add a separator at the end of this tool bar button group
271         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
272 	}
273 
274 	/**
275 	 * Helper method that setps up the Preferences menu and tool bars
276 	 * @param menuBar the JMenuBar instance to setup
277 	 * @param toolBar the JToolBar to setup
278 	 */
279 	private void setupPreferencesMenu(JMenuBar menuBar, JToolBar toolBar) {
280         final JMenu preferencesMenu = new JMenu(InsightConstants.getLiteral("PREFERENCES_MENU"));
281         menuBar.add(preferencesMenu);
282 
283         MaintainPreferencesAction maintainPreferencesAction = new MaintainPreferencesAction(this);
284         preferencesMenu.add(getMenuItem(IAction.ALL, null, InsightConstants.getLiteral("MAINTAIN_PREFERENCES"),maintainPreferencesAction));
285         toolBar.add(getToolbarButton(IAction.ALL, null, ResourceManager.getInstance().loadImageIcon("PREFERENCES_ICON"), InsightConstants.getLiteral("MAINTAIN_PREFERENCES_TOOL_TIP"),maintainPreferencesAction));
286 
287         // add a separator at the end of this tool bar button group
288         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
289 	}
290 
291 	/**
292 	 * Helper method that setps up the View menu and tool bars
293 	 * @param menuBar the JMenuBar instance to setup
294 	 * @param toolBar the JToolBar to setup
295 	 */
296 	private void setupViewMenu(JMenuBar menuBar, JToolBar toolBar) {
297 		ResourceManager rm = ResourceManager.getInstance();
298 		LoadPageAction loadPageAction = new LoadPageAction(controller);
299 		final JMenu viewMenu = new JMenu(InsightConstants.getLiteral("VIEW_MENU"));
300 		menuBar.add(viewMenu);
301 
302 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("FIRST_PAGE"), 
303 				InsightConstants.getLiteral("FIRST_PAGE_MENU"),loadPageAction));
304 		toolBar.add(getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("FIRST_PAGE"),
305 				rm.loadImageIcon("FIRST_PAGE_ICON"), InsightConstants.getLiteral("FIRST_PAGE_TOOL_TIP"),
306 				loadPageAction));
307 		
308 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("PREV_PAGE"), 
309 				InsightConstants.getLiteral("PREV_PAGE_MENU"),loadPageAction));
310 		toolBar.add(getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("PREV_PAGE"), 
311 				rm.loadImageIcon("PREV_PAGE_ICON"), InsightConstants.getLiteral("PREV_PAGE_TOOL_TIP"),
312 				loadPageAction));
313 
314 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("NEXT_PAGE"), 
315 				InsightConstants.getLiteral("NEXT_PAGE_MENU"),loadPageAction));
316 		toolBar.add(getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("NEXT_PAGE"), 
317 				rm.loadImageIcon("NEXT_PAGE_ICON"), InsightConstants.getLiteral("NEXT_PAGE_TOOL_TIP"),
318 				loadPageAction));
319 
320 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("LAST_PAGE"), 
321 				InsightConstants.getLiteral("LAST_PAGE_MENU"),loadPageAction));
322 		toolBar.add(getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("LAST_PAGE"),
323 				rm.loadImageIcon("LAST_PAGE_ICON"), InsightConstants.getLiteral("LAST_PAGE_TOOL_TIP"),
324 				loadPageAction));
325 		
326         // add a separator to separate pagination actions from event search
327         // navigation actions in the tool bar and in the menu
328         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
329         viewMenu.insertSeparator(viewMenu.getMenuComponentCount());		
330 
331 		SearchEventAction searchEventAction = SearchEventAction.getInstance(controller);
332 		
333 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("PREV_SEARCH_MATCH"), 
334 				InsightConstants.getLiteral("PREV_SEARCH_MATCH"),searchEventAction));
335 		toolBar.add(getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("PREV_SEARCH_MATCH"), 
336 				rm.loadImageIcon("PREV_MATCH_ICON"), InsightConstants.getLiteral("PREV_SEARCH_MATCH_TOOL_TIP"),
337 				searchEventAction));
338 
339 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("NEXT_SEARCH_MATCH"), 
340 				InsightConstants.getLiteral("NEXT_SEARCH_MATCH"),searchEventAction));
341 		toolBar.add(getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("NEXT_SEARCH_MATCH"),
342 				rm.loadImageIcon("NEXT_MATCH_ICON"), InsightConstants.getLiteral("NEXT_SEARCH_MATCH_TOOL_TIP"),
343 				searchEventAction));
344 		
345         // add a separator to separate the scroll lock/unlock items in 
346 		// the toolbar and the menu
347         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
348         viewMenu.insertSeparator(viewMenu.getMenuComponentCount());
349         
350         // Create and add the toggle scroll lock/unlock menu and tool bar button. 
351         ScrollLockAction scrollLockAction = new ScrollLockAction(controller);
352         InsightToolbarButton scrollButton = getToolbarButton(IAction.VIEW, InsightConstants.getLiteral("SCROLL_LOCK_UNLOCK"), 
353         		rm.loadImageIcon("UNLOCK_ICON"), InsightConstants.getLiteral("SCROLL_LOCK_TOOL_TIP"),
354 				scrollLockAction);
355         scrollLockAction.setScrollButton(scrollButton);
356 		viewMenu.add(getMenuItem(IAction.VIEW, InsightConstants.getLiteral("SCROLL_LOCK_UNLOCK"), 
357 				InsightConstants.getLiteral("SCROLL_LOCK_UNLOCK"),scrollLockAction));
358 		toolBar.add(scrollButton);
359         
360         // add a separator at the end of this tool bar button group
361         toolBar.addSeparator(new Dimension(InsightConstants.TOOL_BAR_GROUP_SPACING,toolBar.getHeight()));
362         
363         // Add a menu separator to separate the view loaded namespaces menu item
364         viewMenu.insertSeparator(viewMenu.getMenuComponentCount());		
365 		viewMenu.add(getMenuItem(IAction.MUTATION, null, 
366 				InsightConstants.getLiteral("LOADED_NAMESPACES"),
367 	       		new AbstractAction(){
368 					/**
369 					 * SVUID
370 					 */
371 					private static final long serialVersionUID = -5496879826632520984L;
372 
373 					/**
374 					 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
375 					 */
376 					public void actionPerformed(ActionEvent actionEvent) {
377 						new LoadedNamespacesFrame(instance);
378 					}
379     			}
380 		));				
381 	}
382 
383 	/**
384 	 * Helper method that setps up the Help menu and tool bars
385 	 * @param menuBar the JMenuBar instance to setup
386 	 * @param toolBar the JToolBar to setup
387 	 */
388 	private void setupHelpMenu(JMenuBar menuBar, JToolBar toolBar) {
389         final JMenu helpMenu = new JMenu(InsightConstants.getLiteral("HELP_MENU"));
390         menuBar.add(helpMenu);
391         helpMenu.add(getMenuItem(IAction.ALL, null, InsightConstants.getLiteral("ABOUT_DIALOG"),
392        		new AbstractAction(){
393        			/**
394 				 * SVUID
395 				 */
396 				private static final long serialVersionUID = 177106344651169741L;
397 
398 				/**
399 				 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
400 				 */
401 				public void actionPerformed(ActionEvent actionEvent) {
402        				new AboutFrame(instance);
403        			}
404         	}
405         ));
406 	}
407 	
408 //	/**
409 //	 * Helper method that creates and returns a JButton that may be added
410 //	 * to the application toolbar using the specified parameters
411 //	 * @param type the valid type as defined in IAction
412 //	 * @param name null or the name of the button
413 //	 * @param imageURL relative file path to the image for the JButton
414 //	 * @param toolTip tooltip text for the JButton
415 //	 * @param actionListener ActionListener instance that processes ActionEvent generated on the JButton
416 //	 * @return Jbutton instance created using the specified parameters
417 //	 * @see IAction
418 //	 */
419 //	private InsightToolbarButton getToolbarButton(int type, String name, URL imageURL, String toolTip, ActionListener actionListener) {
420 //		InsightToolbarButton button = new InsightToolbarButton(type, name, imageURL,toolTip, actionListener);
421 //		button.setController(this.controller);
422 //		return button;
423 //	}
424 	
425 	/**
426 	 * Helper method that creates and returns a JButton that may be added
427 	 * to the application toolbar using the specified parameters
428 	 * @param type the valid type as defined in IAction
429 	 * @param name null or the name of the button
430 	 * @param imageIcon relative file path to the image for the JButton
431 	 * @param toolTip tooltip text for the JButton
432 	 * @param actionListener ActionListener instance that processes ActionEvent generated on the JButton
433 	 * @return Jbutton instance created using the specified parameters
434 	 * @see IAction
435 	 */
436 	private InsightToolbarButton getToolbarButton(int type, String name, ImageIcon imageIcon, String toolTip, ActionListener actionListener) {
437 		InsightToolbarButton button = new InsightToolbarButton(type, name, imageIcon, toolTip, actionListener);
438 		button.setController(this.controller);
439 		return button;
440 	}
441 	
442 	/**
443 	 * Helper method that creates and returns a JMenuItem that may be added
444 	 * to the application menus using the specified parameters
445 	 * @param type the valid type of this Insight as defined in IAction
446 	 * @param name null or the name of the menu item
447 	 * @param displayText the text to be diplayed by the JMenuItem
448 	 * @param actionListener ActionListener instance that processes ActionEvent generated on the JMenuItem
449 	 * @return The Insight menu item
450 	 * @see IAction
451 	 */
452 	private InsightMenuItem getMenuItem(int type, String name, String displayText, ActionListener actionListener) {
453 		InsightMenuItem menu = new InsightMenuItem(type, name, displayText, actionListener);
454 		menu.setController(this.controller);
455 		return menu;
456 	}	
457 
458 }