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.preferences;
25  
26  import java.awt.Component;
27  import java.awt.GridBagConstraints;
28  import java.awt.GridBagLayout;
29  import java.awt.Insets;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  
36  import javax.swing.AbstractListModel;
37  import javax.swing.BorderFactory;
38  import javax.swing.JButton;
39  import javax.swing.JFileChooser;
40  import javax.swing.JLabel;
41  import javax.swing.JList;
42  import javax.swing.JPanel;
43  import javax.swing.JScrollPane;
44  import javax.swing.JTextField;
45  import javax.swing.ListSelectionModel;
46  
47  import com.mindtree.techworks.insight.InsightConstants;
48  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
49  import com.mindtree.techworks.insight.preferences.PreferenceManager;
50  import com.mindtree.techworks.insight.preferences.model.Preference;
51  import com.mindtree.techworks.insight.preferences.model.PreferenceAttribute;
52  import com.mindtree.techworks.insight.preferences.model.PreferenceAttributeType;
53  import com.mindtree.techworks.insight.preferences.model.PreferenceInfo;
54  
55  
56  /**
57  *
58  * The <code>LocalFilesUIPanel</code> displays the UI for editing 
59  * local file set preferences.
60  *
61  * @see com.mindtree.techworks.insight.preferences.model.Preference
62  * @see com.mindtree.techworks.insight.gui.preferences.AbstractPreferencesUIPanel
63  *
64  * @author  Regunath B
65  * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
66  */
67  public class LocalFilesUIPanel extends AbstractPreferencesUIPanel implements
68  		ActionListener {
69  	
70  	/**
71  	 * SVUID
72  	 */
73  	private static final long serialVersionUID = 7653074890889562083L;
74  	
75  	/**
76  	 * Constants for attribute Ids and prefixes of the remote file 
77  	 * set preference 
78  	 */
79  	private static final String URL_PREFIX = "urlId";
80  //	private static final String URL_ATTRIB_NAME_PREFIX = "urlName";
81  	
82  	/**
83  	 * Constants to identify the file set edit action
84  	 */
85  	private static final int NONE = 0;
86  	private static final int ADD = 1;
87  	private static final int EDIT = 2;
88  	private static final int REMOVE = 3;
89  	
90  	/**
91  	 * The file chooser used for allowing users to pick a file from local and
92  	 * mounted file systems
93  	 * 
94  	 */
95  	private final JFileChooser fileChooser = new JFileChooser(System.getProperty(InsightConstants.INSIGHT_HOME));    {
96      	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
97      	fileChooser.setMultiSelectionEnabled(true);
98      	fileChooser.setDialogTitle(InsightConstants.getLiteral("SELECT_FILES"));
99      	fileChooser.setApproveButtonText(InsightConstants.getLiteral("SELECT_FILES"));
100     }	
101 	
102 	/**
103 	 * State that holds the action type
104 	 */
105 	private int action = NONE;
106 	
107 	/**
108 	 * The display widgets
109 	 */
110 	private JList configuredFilesetsList;
111 	private JButton addButton;
112 	private JButton browseButton;
113 	private JButton editButton;
114 	private JButton removeButton;
115 	private JTextField nameField;
116 	private JList urlList;
117 	private JTextField newURLField;
118 	private JButton addURLButton;
119 	private JButton removeURLButton;
120 	private JButton applyChangesButton;
121 	
122 	/**
123 	 * Layout definition for the fileset details
124 	 */
125 	private GridBagLayout gl;
126 	private GridBagConstraints gc;		
127 	
128 	/**
129 	 * The fileset model used to display the filesets
130 	 */
131 //	private FilesetModel filesetModel;
132 	
133 	/**
134 	 * No args constructor
135 	 */
136 	public LocalFilesUIPanel() {
137 		// No args constructor
138 	}
139 	/**
140 	 * Tells whether the file given by the absolute path already exists in the
141 	 * File Set.
142 	 * 
143 	 * @param urlListModel
144 	 *            The list of URLs in the fileset
145 	 * @param filePath
146 	 *            The path to the file
147 	 * @return <code>true</code> if the file exists in the fileset or
148 	 *         <code>false</code>.
149 	 */
150 	private boolean isFileAlreadyExistsInFileSet(URLListModel urlListModel, String filePath){
151     	for(int j = 0; j < urlListModel.getSize(); j++){
152     		if(((String)(urlListModel.getElementAt(j))).equals(filePath)){
153     			StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
154     			return true;
155     		}
156     	}
157     	return false;
158 	}
159 	
160 	/** 
161 	 * ActionListener interface method implementation
162 	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
163 	 */
164 	public void actionPerformed(ActionEvent event) {
165 		Object source = event.getSource();
166 		if (source == this.addButton) {
167 			this.action = ADD;
168 			this.configuredFilesetsList.removeSelectionInterval(0,this.configuredFilesetsList.getModel().getSize() - 1);
169 			setDetailsWidgetsEnable(true);
170 		} else if (source == this.editButton) {
171 			if (this.configuredFilesetsList.getSelectedIndex() > -1) {
172 				this.action = EDIT;
173 				PreferenceInfo prefInfo = (PreferenceInfo)this.configuredFilesetsList
174 					.getModel().getElementAt(this.configuredFilesetsList
175 							.getSelectedIndex());
176 				displayFilesetInfoForEdit(PreferenceManager.getInstance().getPreference(prefInfo.getCompleteId()));
177 			}
178 		} else if (source == this.removeButton) {
179 			if (this.configuredFilesetsList.getSelectedIndex() > -1) {
180 				this.action = REMOVE;
181 				((FilesetModel)this.configuredFilesetsList.getModel())
182 					.removeFileset(this.configuredFilesetsList.getSelectedIndex());
183 				setDetailsWidgetsEnable(false);
184 			}
185 		} else if (source == this.removeURLButton) {
186 			if (this.urlList.getSelectedIndex() > -1) {
187 				((URLListModel)this.urlList.getModel())
188 				.removeURL(this.urlList.getSelectedIndex());
189 			}
190 		} else if (source == this.addURLButton) {
191 			if (newURLField.getText().trim().length() > 0) {
192 				URLListModel urlListModel = (URLListModel)this.urlList.getModel();
193 					if(!isFileAlreadyExistsInFileSet(urlListModel,newURLField.getText()))
194 						urlListModel.addURL(newURLField.getText());
195 					else
196 						StatusBar.getInstance().setDisplayText(0, InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
197 				newURLField.setText("");
198 			}
199 		} else if (source == this.browseButton) {
200 	        if (fileChooser.showOpenDialog(this.parent) == JFileChooser.APPROVE_OPTION) {
201 	            File[] chosenFiles = fileChooser.getSelectedFiles();
202 	            for (int i = 0; i < chosenFiles.length; i++) {
203 	            	// add files to the URL List
204 	            	URLListModel urlListModel = ((URLListModel)this.urlList.getModel());
205 	            	String filePath = chosenFiles[i].getAbsolutePath();
206 	            	if(!isFileAlreadyExistsInFileSet(urlListModel, filePath))
207 	            		urlListModel.addURL(chosenFiles[i].getAbsolutePath());
208 	            	else
209 	            		StatusBar.getInstance().setDisplayText(0, InsightConstants.getLiteral("FILE_ALREADY_LOADED"), false);
210 	            }
211 	        }			
212 		} else if (source == applyChangesButton) {
213 			if (this.nameField.getText().trim().length() < InsightConstants.PREFERENCE_NAME_MIN_SIZE) {
214 				StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("ERROR_FILESET_NAME") + 
215 						InsightConstants.PREFERENCE_NAME_MIN_SIZE, false);
216 				return;
217 			}
218 			Preference childPreference = null;
219 			switch(this.action) {
220 				case ADD:
221 					childPreference = new Preference(String.valueOf(new Object().hashCode()), true, true, this.nameField.getText());
222 					this.preference.addChildPreference(childPreference);
223 					
224 					// update the GUI
225 					((FilesetModel)this.configuredFilesetsList.getModel()).addFileset(childPreference);
226 					
227 					break;
228 				case EDIT:
229 					childPreference = ((FilesetModel)this.configuredFilesetsList.
230 						getModel()).getPreference(this.configuredFilesetsList.getSelectedIndex());
231 					childPreference.setName(this.nameField.getText());
232 					((FilesetModel)this.configuredFilesetsList.getModel()).listDataUpdated(this.configuredFilesetsList.getSelectedIndex());					
233 					break;
234 			}
235 			// Remove all old URLs and add the new ones
236 			updateURLListToPreference(childPreference);
237 			setDetailsWidgetsEnable(false);
238 			this.action = NONE;
239 		}
240 	}
241 		
242 	
243 	/**
244 	 * Overriden super class method
245 	 * 
246 	 * @see com.mindtree.techworks.insight.gui.preferences.AbstractPreferencesUIPanel#initializeDisplay()
247 	 */
248 	protected void initializeDisplay() {
249 		this.configuredFilesetsList = new JList(new FilesetModel());
250 		this.configuredFilesetsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
251 		addComponent(new JLabel(InsightConstants.getLiteral("CONFIGURED_FILESETS")),0,0,0,0,1,1);		
252 		addComponent(new JScrollPane(configuredFilesetsList),1,0,1,1,1,4);	
253 		this.addButton = new JButton(InsightConstants.getLiteral("ADD"));
254 		this.editButton = new JButton(InsightConstants.getLiteral("EDIT"));
255 		this.removeButton = new JButton(InsightConstants.getLiteral("REMOVE"));
256 		this.addButton.addActionListener(this);
257 		this.editButton.addActionListener(this);
258 		this.removeButton.addActionListener(this);
259 		addComponent(addButton,2,0,0,0,1,1);	
260 		addComponent(editButton,2,1,0,0,1,1);	
261 		addComponent(removeButton,2,2,0,0,1,1);	
262 		
263 		// add the widgets for the fileset details
264 		JPanel filesetDetailsPanel = new JPanel();
265 		this.gl = new GridBagLayout();
266 		this.gc = new GridBagConstraints();
267 		filesetDetailsPanel.setLayout(gl);
268 		filesetDetailsPanel.setBorder(BorderFactory.createTitledBorder(InsightConstants.getLiteral("FILESET_DETAILS")));
269 		addComponent(filesetDetailsPanel,0,4,1,1,6,1);	
270 		
271 		addComponent(filesetDetailsPanel,new JLabel(InsightConstants.getLiteral("NAME")),0,0,0,0,1,1);
272 		this.nameField = new JTextField();
273 		addComponent(filesetDetailsPanel,nameField,1,0,1,0,2,1);
274 		this.urlList = new JList(new URLListModel());
275 		this.urlList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
276 		addComponent(filesetDetailsPanel,new JLabel(InsightConstants.getLiteral("URLS")),0,1,0,0,1,1);		
277 		addComponent(filesetDetailsPanel,new JScrollPane(urlList),1,1,1,1,1,2);			
278 		this.removeURLButton = new JButton(InsightConstants.getLiteral("REMOVE"));
279 		this.removeURLButton.addActionListener(this);
280 		addComponent(filesetDetailsPanel,removeURLButton,3,1,0,0,1,1);	
281 		addComponent(filesetDetailsPanel,new JLabel(InsightConstants.getLiteral("NEW_URL")),0,3,0,0,1,1);
282 		this.newURLField = new JTextField();
283 		addComponent(filesetDetailsPanel,newURLField,1,3,1,0,2,1);
284 		this.addURLButton = new JButton(InsightConstants.getLiteral("ADD"));
285 		this.addURLButton.addActionListener(this);
286 		addComponent(filesetDetailsPanel,addURLButton,3,3,0,0,1,1);
287 		this.browseButton = new JButton(InsightConstants.getLiteral("BROWSE"));
288 		this.browseButton.addActionListener(this);
289 		addComponent(filesetDetailsPanel,browseButton,4,3,0,0,1,1);
290 		this.applyChangesButton = new JButton(InsightConstants.getLiteral("APPLY_FILESET_CHANGES"));
291 		this.applyChangesButton.addActionListener(this);
292 		addComponent(filesetDetailsPanel,applyChangesButton,3,4,0,0,2,1);
293 		// disable the details widgets
294 		setDetailsWidgetsEnable(false);		
295 	}
296 	
297 
298 	/**
299 	 *  Overriden super class method
300 	 * @see com.mindtree.techworks.insight.gui.preferences.AbstractPreferencesUIPanel#setPreferenceValues()
301 	 */
302 	protected  void setPreferenceValues() {
303 		// update the fileset list change
304 		updateFilesetListToPreference();
305 	}
306 	
307 	/**
308 	 * Helper method to update the list of filesets in the Preference for this panel
309 	 */
310 	private final void updateFilesetListToPreference() {
311 		ArrayList oldValues = new ArrayList();
312 		Iterator iterator = this.preference.iterateChildPreferenceIds();
313 		while(iterator.hasNext()) {
314 			oldValues.add(iterator.next());
315 		}
316 		iterator = oldValues.iterator();
317 		while(iterator.hasNext()) {
318 			this.preference.removePreference(
319 					this.preference.getPreferenceById(
320 							(String)iterator.next()));
321 		}
322 		for (int i = 0; i < this.configuredFilesetsList.getModel().getSize(); i++) {
323 			this.preference.addChildPreference(((FilesetModel)this.configuredFilesetsList.getModel()).getPreference(i));
324 		}				
325 	}
326 	
327 	/**
328 	 * Helper method to update the list of URLs in the specified file set Preference
329 	 * @param childPreference the Preference to be updated with URL list
330 	 */
331 	private final void updateURLListToPreference(Preference childPreference) {
332 		ArrayList oldValues = new ArrayList();
333 		Iterator iterator = childPreference.iteratePreferenceAttributeIds();
334 		while(iterator.hasNext()) {
335 			String id = (String)iterator.next();
336 			if (id.startsWith(URL_PREFIX)) {
337 				oldValues.add(id);
338 			}
339 		}
340 		iterator = oldValues.iterator();
341 		while(iterator.hasNext()) {
342 			childPreference.removePreferenceAttribute(
343 					childPreference.getPreferenceAttributeById(
344 							(String)iterator.next()));
345 		}
346 		for (int i = 0; i < this.urlList.getModel().getSize(); i++) {
347 			String identifier = (String)this.urlList.getModel().getElementAt(i);
348 			PreferenceAttribute newAttribute = new PreferenceAttribute(PreferenceAttributeType.TEXT,
349 					URL_PREFIX + i, identifier, false,
350 					true, true, childPreference);
351 			newAttribute.setName(URL_PREFIX + i);
352 			childPreference.addPreferenceAttribute(newAttribute);
353 		}		
354 	}
355 	
356 	/**
357 	 * Helper method that displays the specified file set information for
358 	 * editing
359 	 * @param filesetInfo Presefernce containing the file set information
360 	 */
361 	private final void displayFilesetInfoForEdit(Preference filesetInfo) {
362 		setDetailsWidgetsEnable(true);
363 		this.nameField.setText(filesetInfo.getName());
364 		Iterator iterator = filesetInfo.iteratePreferenceAttributeIds();
365 		while(iterator.hasNext()) {			
366 			String id = (String)iterator.next();
367 			PreferenceAttribute prefAttribute = filesetInfo.getPreferenceAttributeById(id);
368 			if (id.startsWith(URL_PREFIX)) {
369 				((URLListModel)this.urlList.getModel()).addURL(prefAttribute.getValue());
370 			}
371 		}
372 	}
373 	
374 	/**
375 	 * Adds the specified component to this panel using the specified
376 	 * constraints.
377 	 * 
378 	 * @param panel
379 	 *            The panel to add the component to.
380 	 * @param c
381 	 *            The component to layout
382 	 * @param gridx
383 	 *            X location in the Grid
384 	 * @param gridy
385 	 *            Y location in the Grid
386 	 * @param weightx
387 	 *            Weight in X direction
388 	 * @param weighty
389 	 *            Weight in Y direction
390 	 * @param width
391 	 *            Width
392 	 * @param height
393 	 *            Height
394 	 **/
395 	private final void addComponent(JPanel panel, Component c, int gridx, int gridy, int weightx,
396 		int weighty, int width, int height) {
397 		gc.fill = GridBagConstraints.BOTH;
398 		gc.anchor = GridBagConstraints.NORTHWEST;
399 		gc.insets = new Insets(5,5,5,5);		
400 		gc.gridx = gridx;
401 		gc.gridy = gridy;
402 		gc.weightx = weightx;
403 		gc.weighty = weighty;
404 		gc.gridwidth = width;
405 		gc.gridheight = height;		
406 		gl.setConstraints( c, gc);
407 		panel.add(c);
408 	}	
409 
410 	/**
411 	 * Helper method to set the enable status of file set details widgest
412 	 * @param enable true to enable, false to disable
413 	 */
414 	private void setDetailsWidgetsEnable(boolean enable) {
415 		this.nameField.setText("");
416 		this.nameField.setEditable(enable);
417 		this.nameField.setEnabled(enable);		
418 		((URLListModel)this.urlList.getModel()).clearAll();
419 		this.urlList.setEnabled(enable);
420 		this.newURLField.setText("");
421 		this.newURLField.setEditable(enable);
422 		this.newURLField.setEnabled(enable);		
423 		this.removeURLButton.setEnabled(enable);
424 		this.addURLButton.setEnabled(enable);
425 		this.browseButton.setEnabled(enable);
426 		this.applyChangesButton.setEnabled(enable);
427 		if (enable) {
428 			this.nameField.requestFocus();
429 		}
430 	}
431 	
432 	/**
433 	 * Helper class to that acts as the model for the fileset JList 
434 	 */
435 	private class FilesetModel extends AbstractListModel {
436 		
437 		/**
438 		 * SVUID
439 		 */
440 		private static final long serialVersionUID = 1228554705949611510L;
441 		
442 		/**
443 		 * Collection that holds the configured fileset data
444 		 */
445 		private ArrayList configuredFilesets = new ArrayList();
446 
447 		/**
448 		 * No args constructor
449 		 */
450 		public FilesetModel() {
451 			Iterator iterator = preference.iterateChildPreferences();
452 			while(iterator.hasNext()) {
453 				Preference filesetPreference = (Preference)iterator.next();
454 				configuredFilesets.add(filesetPreference);
455 			}			
456 		}
457 
458 		/**
459 		 * Adds the specified fileset information to this model
460 		 * @param filesetInfo Preference containing the file set information to be added
461 		 */
462 		public void addFileset(Preference filesetInfo) {
463 			configuredFilesets.add(filesetInfo);
464 			super.fireIntervalAdded(this, configuredFilesets.size() - 1, configuredFilesets.size() - 1);
465 		}
466 		
467 		/**
468 		 * Informs this model that the data at the specified index has changed
469 		 * @param index index at which the data has changed
470 		 */
471 		public void listDataUpdated(int index) {
472 			super.fireContentsChanged(this, index, index);
473 		}
474 		
475 		/**
476 		 * Removes the specified fileset information to this model
477 		 * @param index from where to remove the file set information
478 		 */
479 		public void removeFileset(int index) {
480 			if (index < 0 || index >= configuredFilesets.size()) {
481 				return;
482 			}
483 			configuredFilesets.remove(index);
484 			super.fireIntervalRemoved(this, index, index);
485 		}
486 		
487 		
488 		/** 
489 		 * Superclass abstract method implementation
490 		 * @see javax.swing.ListModel#getSize()
491 		 */
492 		public int getSize() {
493 			return configuredFilesets.size();
494 		}
495 		
496 		/**
497 		 * Returns the Preference at the sepcified index
498 		 * @param index index of the required Preference
499 		 * @return Preference at the specified index
500 		 */
501 		public Preference getPreference(int index) {
502 			return (Preference)configuredFilesets.get(index);
503 		}
504 
505 		/** 
506 		 * Superclass abstract method implementation
507 		 * @see javax.swing.ListModel#getElementAt(int)
508 		 */
509 		public Object getElementAt(int index) {
510 			return ((Preference)configuredFilesets.get(index)).getPreferenceInfo();
511 		}
512 	}
513 	
514 	/**
515 	 * Helper class to that acts as the model for the URL JList 
516 	 */
517 	private class URLListModel extends AbstractListModel {
518 		
519 		/**
520 		 * SVUID
521 		 */
522 		private static final long serialVersionUID = -1653119024030067594L;
523 		
524 		/**
525 		 * Collection that holds the configured URL data
526 		 */
527 		private ArrayList configuredURLs = new ArrayList();
528 
529 		/**
530 		 * No args constructor
531 		 */
532 		public URLListModel() {
533 			// No args constructor
534 		}
535 
536 		/**
537 		 * Adds the specified URL information to this model
538 		 * @param url the URL string
539 		 */
540 		public void addURL(String url) {
541 			configuredURLs.add(url);
542 			super.fireIntervalAdded(this, configuredURLs.size() - 1, configuredURLs.size() - 1);
543 		}
544 		
545 		/**
546 		 * Removes the specified URL information to this model
547 		 * @param index from where to remove the URL information
548 		 */
549 		public void removeURL(int index) {
550 			if (index < 0 || index >= configuredURLs.size()) {
551 				return;
552 			}			
553 			configuredURLs.remove(index);
554 			super.fireIntervalRemoved(this, index, index);
555 		}
556 		
557 		/**
558 		 * Clears the entire list contents
559 		 */
560 		public void clearAll() {
561 			if (configuredURLs.size() > 0) {
562 				int lastIndex = configuredURLs.size() - 1;
563 				configuredURLs.clear();
564 				super.fireIntervalRemoved(this, 0, lastIndex);
565 			}
566 		}
567 		
568 		/** 
569 		 * Superclass abstract method implementation
570 		 * @see javax.swing.ListModel#getSize()
571 		 */
572 		public int getSize() {
573 			return configuredURLs.size();
574 		}
575 
576 		/** 
577 		 * Superclass abstract method implementation
578 		 * @see javax.swing.ListModel#getElementAt(int)
579 		 */
580 		public Object getElementAt(int index) {
581 			return configuredURLs.get(index);
582 		}
583 	}
584 
585 }