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.util;
25  
26  import java.awt.Component;
27  import java.awt.Dimension;
28  import java.awt.GridBagConstraints;
29  import java.awt.GridBagLayout;
30  import java.awt.GridLayout;
31  import java.awt.Insets;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.io.File;
35  import java.net.InetAddress;
36  import java.net.UnknownHostException;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.LinkedList;
40  import java.util.List;
41  
42  import javax.swing.AbstractListModel;
43  import javax.swing.BorderFactory;
44  import javax.swing.ButtonGroup;
45  import javax.swing.JButton;
46  import javax.swing.JDialog;
47  import javax.swing.JFileChooser;
48  import javax.swing.JLabel;
49  import javax.swing.JList;
50  import javax.swing.JOptionPane;
51  import javax.swing.JPanel;
52  import javax.swing.JRadioButton;
53  import javax.swing.JScrollPane;
54  import javax.swing.ListSelectionModel;
55  import javax.swing.event.ListSelectionEvent;
56  import javax.swing.event.ListSelectionListener;
57  
58  import com.mindtree.techworks.insight.InsightConstants;
59  import com.mindtree.techworks.insight.download.DownloadProvider;
60  import com.mindtree.techworks.insight.download.FTPFileset;
61  import com.mindtree.techworks.insight.download.Fileset;
62  import com.mindtree.techworks.insight.download.SFTPFileset;
63  import com.mindtree.techworks.insight.download.ftpbrowse.FTPRemoteFileSystemView;
64  import com.mindtree.techworks.insight.download.sftpbrowse.SFTPRemoteFileSystemView;
65  import com.mindtree.techworks.insight.gui.Insight;
66  import com.mindtree.techworks.insight.gui.action.LoadLocalFileAction;
67  import com.mindtree.techworks.insight.gui.widgets.InsightRemoteFileChooser;
68  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
69  import com.mindtree.techworks.insight.model.ReceiverFormat;
70  import com.mindtree.techworks.insight.preferences.util.Log4JPatternInterpeter;
71  import com.mindtree.techworks.insight.preferences.util.PreferenceInterpreter;
72  import com.mindtree.techworks.insight.spi.LogNamespace;
73  
74  /**
75  *
76  * The <code>LoadFilesetFrame</code> class displays details about 
77  * filesets that have been configured and selects the preferred type as
78  * default.
79  *
80  * @author  Regunath B
81  * @version 1.0, 05/04/04
82  */
83  
84  public class LoadFilesetFrame extends JDialog implements ActionListener, ListSelectionListener {
85  	
86  	/**
87  	 * Used for object serialization
88  	 */
89  	private static final long serialVersionUID = 4906063024687614221L;
90  	
91  	/**
92  	 * Useful constants for the preferred size of the text field 
93  	 */
94  	private static final int LIST_WIDTH = 250;
95  	private static final int LIST_HEIGHT = 75;
96  	
97  	/**
98  	 * Layout definition.
99  	 */
100 	private GridBagLayout gl;
101 	private GridBagConstraints gc;
102 	
103 	/**
104 	 * The Insight instance that created this frame
105 	 */
106 	private Insight insight;
107 	
108 	/**
109 	 * Display widgets
110 	 */
111 	private JRadioButton localButton;
112 	private JRadioButton ftpButton;
113 	private JRadioButton sftpButton;
114 	private JRadioButton httpButton;
115 	private JList filesetList;
116 	private JList filesetFilesList;
117 	private JButton browseButton;
118 	private JButton openButton;
119 	private JButton cancelButton;
120 	
121 	/**
122 	 * Constructor for this class
123 	 * @param insight the Insight instance for this class
124 	 */
125 	public LoadFilesetFrame(Insight insight) {
126 		super(JOptionPane.getFrameForComponent(insight),InsightConstants.getLiteral("LOAD_FILE"),true);
127 		this.insight = insight;
128 		gl = new GridBagLayout();
129 		gc = new GridBagConstraints();
130 		getContentPane().setLayout(gl);
131 		
132 		JPanel panel = new JPanel();
133 		panel.setLayout(new GridLayout(3,1));
134 		panel.setBorder(BorderFactory.createTitledBorder(InsightConstants.getLiteral("FILESET_TYPE")));		
135 		
136 		ButtonGroup bg = new ButtonGroup();		
137 		this.localButton = new JRadioButton(InsightConstants.getLiteral("LOCAL_FILESETS"));
138 		localButton.setActionCommand(String.valueOf(Fileset.LOCAL_FILESET));
139 		localButton.addActionListener(this);
140 		bg.add(localButton);
141 		panel.add(localButton);
142 		
143 		this.ftpButton = new JRadioButton(InsightConstants.getLiteral("FTP_FILESETS"));
144 		ftpButton.setActionCommand(String.valueOf(Fileset.FTP_FILESET));
145 		ftpButton.addActionListener(this);
146 		bg.add(ftpButton);
147 		panel.add(ftpButton);
148 
149 		this.sftpButton = new JRadioButton(InsightConstants.getLiteral("SFTP_FILESETS"));
150 		sftpButton.setActionCommand(String.valueOf(Fileset.SFTP_FILESET));
151 		sftpButton.addActionListener(this);
152 		bg.add(sftpButton);
153 		panel.add(sftpButton);
154 		
155 		this.httpButton = new JRadioButton(InsightConstants.getLiteral("HTTP_FILESETS"));
156 		httpButton.setActionCommand(String.valueOf(Fileset.HTTP_FILESET));
157 		httpButton.addActionListener(this);
158 		bg.add(httpButton);
159 		panel.add(httpButton);
160 		
161 		addComponent(panel,0,0,1,0,5,1);
162 		
163 		this.filesetList = new JList(new ListModel());
164 		this.filesetList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
165 		this.filesetList.addListSelectionListener(this);
166 		JScrollPane filesetPane = new JScrollPane(filesetList);
167 		filesetPane.setPreferredSize(new Dimension(LIST_WIDTH, LIST_HEIGHT));
168 		addComponent(new JLabel(InsightConstants.getLiteral("FILESETS")),0,1,1,0,3,1);
169 		addComponent(filesetPane,0,2,1,1,3,2);
170 		
171 		this.filesetFilesList = new JList(new ListModel());
172 		JScrollPane filesPane = new JScrollPane(filesetFilesList);
173 		filesPane.setPreferredSize(new Dimension(LIST_WIDTH, LIST_HEIGHT));
174 		addComponent(new JLabel(InsightConstants.getLiteral("SELECTED_FILES")),0,4,1,0,3,1);
175 		addComponent(filesPane,0,5,1,1,3,2);
176 		
177 		this.browseButton = new JButton(InsightConstants.getLiteral("BROWSE"));
178 		this.browseButton.addActionListener(this);
179 		addComponent(browseButton,4,5,0,0,1,1);	
180 		
181 		this.openButton = new JButton(InsightConstants.getLiteral("OK"));
182 		this.cancelButton = new JButton(InsightConstants.getLiteral("CANCEL"));
183 		openButton.addActionListener(this);
184 		cancelButton.addActionListener(this);
185 		addComponent(openButton,0,7,0,0,1,1);
186 		addComponent(cancelButton,1,7,0,0,1,1);				
187 	}
188 	
189 	/**
190 	 * Displays this frame
191 	 */
192 	public void showFrame() {
193 		populateFilesetDetails(PreferenceInterpreter.getPreferredFilesetType());
194 		this.pack();
195 		// Center this window in Insight
196 		int insightX = insight.getX();
197 		int insightY = insight.getY();
198 		int thisX = insightX + ((this.insight.getWidth() - this.getWidth())/ 2);
199 		int thisY = insightY + ((this.insight.getHeight() - this.getHeight())/ 2);
200 		this.setLocation(thisX, thisY);
201 		this.show();
202 	}
203 	
204 	/**
205 	 * Hides this frame
206 	 */
207 	public void hideFrame() {
208 		this.hide();
209 		this.dispose();
210 	}
211 	
212 	/**
213 	 * ActionListener interface method implementation
214 	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
215 	 */
216 	public void actionPerformed(ActionEvent event) {
217 		Object source = event.getSource();
218 		if (source == this.browseButton) {
219 			Fileset selectedFileset = (Fileset)filesetList.getModel().getElementAt(filesetList.getSelectedIndex());
220 			switch(selectedFileset.getType()) {
221 				case Fileset.LOCAL_FILESET:
222 					LoadLocalFileAction.getInstance().browseAndLoadLocalFiles();
223 					break;
224 				case Fileset.FTP_FILESET:
225 					JFileChooser fileChooser = getRemoteFileChooser(selectedFileset);
226 			        if (fileChooser.showOpenDialog(this.insight) == JFileChooser.APPROVE_OPTION) {
227 			            File[] chosenFiles = fileChooser.getSelectedFiles();
228 			            List selectedPaths = new LinkedList();
229 			            for (int i = 0; i < chosenFiles.length; i++) {
230 			            	selectedPaths.add(chosenFiles[i].getAbsolutePath());
231 			            }
232 			            DownloadProvider downloadProvider = new DownloadProvider(this.insight);
233 						downloadProvider.loadRemoteFiles(selectedFileset, selectedPaths);
234 			        }			
235 					break;
236 				case Fileset.SFTP_FILESET:
237 					/*
238 					 * Initialize the file chooser and invoke it as a separate thread instead
239 					 * of the AWT event queue in order to prevent deadlock and locking issues
240 					 */
241 					Thread fileChooserLauncher = new Thread() {
242 						public void run() {
243 							Fileset selectedFileset = (Fileset)filesetList.getModel().getElementAt(filesetList.getSelectedIndex());
244 							JFileChooser fileChooser = getRemoteFileChooser(selectedFileset);
245 					        if (fileChooser.showOpenDialog(insight) == JFileChooser.APPROVE_OPTION) {
246 					            File[] chosenFiles = fileChooser.getSelectedFiles();
247 					            List selectedPaths = new LinkedList();
248 					            for (int i = 0; i < chosenFiles.length; i++) {
249 					            	selectedPaths.add(chosenFiles[i].getAbsolutePath());
250 					            }
251 					            DownloadProvider downloadProvider = new DownloadProvider(insight);
252 								downloadProvider.loadRemoteFiles(selectedFileset, selectedPaths);
253 					        }			
254 						}
255 					};
256 				  	// explicitly set the priority to normal. Otherwise
257 				  	// this thread will inherit the priority of the creator thread
258 				  	// i.e the AWT event dispatch thread that runs with max priority
259 					fileChooserLauncher.setPriority(Thread.NORM_PRIORITY);						
260 					fileChooserLauncher.start();					
261 					break;
262 			}
263 			hideFrame();			
264 		} else if (source == this.openButton) {
265 			
266 			int[] selectedIndices = filesetFilesList.getSelectedIndices();
267 			int selectedSize = filesetFilesList.getModel().getSize();
268 			boolean isErrorSelectingFiles = false;
269 			if(selectedIndices.length == 0){
270 				StatusBar.getInstance().setDisplayText(0, InsightConstants.getLiteral("ERROR_NO_FILE_SELECTED"), false);
271 				isErrorSelectingFiles = true;
272 			}
273 			if(selectedSize == 0){
274 				isErrorSelectingFiles = true;
275 				StatusBar.getInstance().setDisplayText(0, InsightConstants.getLiteral("ERROR_FILESET_EMPTY"), false);
276 			}
277 			List selectedPaths = new LinkedList();
278 			for (int i = 0; i < selectedIndices.length && selectedSize!=0; i++) {
279 				selectedPaths.add(filesetFilesList.getModel().getElementAt(selectedIndices[i]));
280 			}
281 			Fileset selectedFileset = (Fileset)filesetList.getModel().getElementAt(filesetList.getSelectedIndex());
282 			switch(selectedFileset.getType()) {
283 				case Fileset.LOCAL_FILESET:
284 					String nodeId = null;
285 					ReceiverFormat[] receiverFormat = Log4JPatternInterpeter.getInterpretedRecieverFormat();
286 					try {
287 						nodeId = InetAddress.getLocalHost().getHostName();
288 					} catch(UnknownHostException uhe) {
289 						uhe.printStackTrace();
290 					}									
291 					for (int i = 0; i < selectedPaths.size(); i++) {
292 						LogNamespace namespace = new LogNamespace((String)selectedPaths.get(i), 
293 								InsightConstants.FILE_PROTOCOL_PREFIX + (String)selectedPaths.get(i),
294 								receiverFormat, nodeId);
295 						LoadLocalFileAction.getInstance().addNamespaceForLoad(namespace);
296 					}
297 					LoadLocalFileAction.getInstance().loadNamespaces();
298 					break;
299 				case Fileset.FTP_FILESET:
300 				case Fileset.SFTP_FILESET:
301 				case Fileset.HTTP_FILESET:
302 					DownloadProvider downloadProvider = new DownloadProvider(this.insight);
303 					downloadProvider.loadRemoteFiles(selectedFileset, selectedPaths);
304 					break;
305 			}
306 			if(!isErrorSelectingFiles)
307 				hideFrame();			
308 		} else if (source == this.cancelButton) {
309 			hideFrame();
310 		} else { // one of the radio buttons have been chosen
311 			populateFilesetDetails(Integer.parseInt(((JRadioButton)source).getActionCommand()));
312 		}
313 	}
314 
315 	/**
316 	 * ListSelectionListener interface method implementation
317 	 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
318 	 */
319 	public void valueChanged(ListSelectionEvent event) {
320         if (event.getValueIsAdjusting()) {
321             return;
322         }
323         int index = this.filesetList.getSelectedIndex();
324         if (index < 0 || index >= this.filesetList.getModel().getSize()) {
325         	return;
326         }
327 
328 		Fileset selectedFileset = (Fileset)this.filesetList.getModel().getElementAt(
329 				index);
330 		((ListModel)this.filesetFilesList.getModel()).setDataList(selectedFileset.getFileList());
331 		this.filesetFilesList.setSelectionInterval(0,this.filesetFilesList.getModel().getSize() -1);
332 	}
333 	
334 	/**
335 	 * Helper method that populates the fileset details based
336 	 * on user preferences
337 	 * @param filesetType the fileset type identifier
338 	 */
339 	private void populateFilesetDetails(int filesetType) {	
340 		List filesets = null;
341 		switch(filesetType) {
342 			case Fileset.LOCAL_FILESET:
343 				this.localButton.setSelected(true);
344 				filesets = PreferenceInterpreter.getLocalFilesets();
345 				break;
346 			case Fileset.FTP_FILESET:
347 				this.ftpButton.setSelected(true);
348 				filesets = PreferenceInterpreter.getFTPFilesets();
349 				break;
350 			case Fileset.SFTP_FILESET:
351 				this.sftpButton.setSelected(true);
352 				filesets = PreferenceInterpreter.getSFTPFilesets();
353 				break;
354 			case Fileset.HTTP_FILESET:
355 				this.httpButton.setSelected(true);
356 				filesets = PreferenceInterpreter.getHttpFilesets();
357 				break;
358 		}
359 		this.browseButton.setEnabled(filesetType == Fileset.LOCAL_FILESET 
360 				|| filesetType == Fileset.FTP_FILESET
361 				|| filesetType == Fileset.SFTP_FILESET);
362 		if(filesetType==Fileset.FTP_FILESET 
363 					|| filesetType == Fileset.SFTP_FILESET
364 					|| filesetType ==Fileset.HTTP_FILESET){
365 			this.filesetFilesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
366 		}else if (filesetType==Fileset.LOCAL_FILESET && PreferenceInterpreter.getTailingStatus()){
367 			this.filesetFilesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
368 		}
369 		((ListModel)this.filesetList.getModel()).setDataList(filesets);
370 		if (filesets.size() > 0) {
371 			this.filesetList.setSelectedIndex(0);
372 			// get the first fileset in the list
373 			Fileset selectedFileset = (Fileset)filesets.get(0);
374 			// set the fileset file list to files from a fileset
375 			((ListModel)this.filesetFilesList.getModel()).setDataList(selectedFileset.getFileList());
376 			this.filesetFilesList.setSelectionInterval(0,this.filesetFilesList.getModel().getSize() -1);
377 		}
378 	}
379 	
380 	/**
381 	 * Helper method to return the remote file chooser
382 	 * @return JFileChooser for the remote system
383 	 */
384 	private JFileChooser getRemoteFileChooser(Fileset fileset) {
385 		StatusBar.getInstance().setDisplayText(1, InsightConstants.getLiteral("REMOTE_CONNECTING"), true);
386 		JFileChooser fileChooser = null;
387 		try {
388 			switch(fileset.getType()) {
389 				case Fileset.FTP_FILESET:
390 					fileChooser = new InsightRemoteFileChooser(new FTPRemoteFileSystemView(
391 							((FTPFileset)fileset).getHostURL(), 
392 							((FTPFileset)fileset).getPasswordAuthentication()));
393 					break;
394 				case Fileset.SFTP_FILESET:
395 					SFTPFileset sftpFileset = (SFTPFileset)fileset;
396 					fileChooser = new InsightRemoteFileChooser(new SFTPRemoteFileSystemView(
397 							sftpFileset.getHost(),sftpFileset.getPort(), sftpFileset.getDefaultDirectory(), 
398 							sftpFileset.getPasswordAuthentication(), this.insight));
399 					break;
400 			}
401 		} catch (Exception e) {
402 			e.printStackTrace();
403 			StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("ERROR_BROWSE_REMOTE") + e.getMessage(), false);
404 			return fileChooser;				
405 		} finally {
406 			StatusBar.getInstance().clearDisplay(1);						
407 		}
408     	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
409     	fileChooser.setMultiSelectionEnabled(true);
410     	return fileChooser;
411 	}
412 	
413 	/**
414 	 * Adds the specified component to this panel using the specified constraints.
415 	 **/
416 	private final void addComponent(Component c, int gridx, int gridy, int weightx,
417 		int weighty, int width, int height) {
418 		gc.fill = GridBagConstraints.BOTH;
419 		gc.anchor = GridBagConstraints.NORTHWEST;
420 		gc.insets = new Insets(5,5,5,5);		
421 		gc.gridx = gridx;
422 		gc.gridy = gridy;
423 		gc.weightx = weightx;
424 		gc.weighty = weighty;
425 		gc.gridwidth = width;
426 		gc.gridheight = height;		
427 		gl.setConstraints( c, gc);
428 		this.getContentPane().add(c);
429 	}
430 	
431 	/**
432 	 * Helper class to that acts as the model for the JLists 
433 	 */
434 	private class ListModel extends AbstractListModel {
435 		
436 		/**
437 		 * Used for object serialization
438 		 */
439 		private static final long serialVersionUID = 7744647867290903763L;
440 		
441 		/**
442 		 * Collection that holds the configured fileset or fileset files data
443 		 */
444 		private ArrayList dataList = new ArrayList();
445 
446 		/**
447 		 * No args constructor
448 		 */
449 		public ListModel() {
450 		}
451 		
452 		/**
453 		 * Sets the contents of this model to the elements in the specified Collection
454 		 * @param collection the Collection containing the data
455 		 */
456 		public void setDataList(Collection collection) {
457 			this.dataList = new ArrayList(collection);
458 			if (this.dataList.size() > 0) {
459 				super.fireIntervalAdded(this, dataList.size() - 1, dataList.size() - 1);
460 			} else {
461 				super.fireIntervalAdded(this, 0, 0);				
462 			}
463 		}
464 		
465 		/** 
466 		 * Superclass abstract method implementation
467 		 * @see javax.swing.ListModel#getSize()
468 		 */
469 		public int getSize() {
470 			return dataList.size();
471 		}
472 		
473 		/** 
474 		 * Superclass abstract method implementation
475 		 * @see javax.swing.ListModel#getElementAt(int)
476 		 */
477 		public Object getElementAt(int index) {
478 			return dataList.get(index);
479 		}
480 	}
481 
482 }
483