View Javadoc

1   /*
2    * $HeadURL: https://mindtreeinsight.svn.sourceforge.net/svnroot/mindtreeinsight/insight/insight-ui/trunk/src/main/java/com/mindtree/techworks/insight/gui/preferences/EventColumnPreferencePanel.java $
3    * $Date: 2007-12-16 05:20:57 -0700 (Sun, 16 Dec 2007) $
4    * $Revision: 30 $
5    * $Author: bindul $
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.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  
31  import javax.swing.AbstractListModel;
32  import javax.swing.JButton;
33  import javax.swing.JLabel;
34  import javax.swing.JList;
35  import javax.swing.JScrollPane;
36  
37  import com.mindtree.techworks.insight.InsightConstants;
38  import com.mindtree.techworks.insight.ResourceManager;
39  import com.mindtree.techworks.insight.model.LogEventTableModel;
40  import com.mindtree.techworks.insight.preferences.model.PreferenceAttribute;
41  import com.mindtree.techworks.insight.preferences.model.PreferenceAttributeType;
42  import com.mindtree.techworks.insight.preferences.util.PreferenceInterpreter;
43  
44  /**
45  *
46  * The <code>EventColumnPreferencePanel</code> displays the UI for editing 
47  * event list presentation display columns.
48  *
49  * @see com.mindtree.techworks.insight.preferences.model.Preference
50  * @see com.mindtree.techworks.insight.gui.preferences.AbstractPreferencesUIPanel
51  *
52  * @author  Regunath B
53  * @version 1.0, 05/03/30
54  */
55  public class EventColumnPreferencePanel extends AbstractPreferencesUIPanel implements ActionListener {
56  
57  	/**
58  	 * Used for object serialization
59  	 */
60  	private static final long serialVersionUID = 6585162564228536132L;
61  
62  	/**
63  	 * Constant for the column attribute prefix
64  	 */
65  	private static final String ATTRIBUTE_PREFIX = "column";
66  	
67  	/**
68  	 * Display widgets
69  	 */
70  	private JList availableColumns;
71  	private JList selectedColumns;
72  	private JButton leftButton;
73  	private JButton rightButton;
74  	private JButton upButton;
75  	private JButton downButton;
76  	
77  	/**
78  	 * No args constructor
79  	 */
80  	public EventColumnPreferencePanel() {
81  	}
82  
83  	/**
84  	 * Interface method implementation
85  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
86  	 */
87  	public void actionPerformed(ActionEvent event) {
88  		Object source = event.getSource();
89  		if (source == this.leftButton) {
90  			if (this.selectedColumns.getSelectedIndex() > -1) {
91  				Object object = ((ColumnListModel)this.selectedColumns.getModel()).remove(
92  						this.selectedColumns.getSelectedIndex());
93  				((ColumnListModel)this.availableColumns.getModel()).insert(
94  						((Integer)object).intValue(), this.availableColumns.getModel().getSize());
95  			}
96  		} else if (source == this.rightButton) {
97  			if (this.availableColumns.getSelectedIndex() > -1) {
98  				Object object = ((ColumnListModel)this.availableColumns.getModel()).remove(
99  						this.availableColumns.getSelectedIndex());
100 				((ColumnListModel)this.selectedColumns.getModel()).insert(
101 						((Integer)object).intValue(), this.selectedColumns.getModel().getSize());
102 			}			
103 		} else { // it is the 'up' or 'down' buttons
104 			if (this.selectedColumns.getSelectedIndex() > -1) {
105 				int toIndex = (source == this.upButton ? 
106 						this.selectedColumns.getSelectedIndex() - 1 :
107 						this.selectedColumns.getSelectedIndex() + 1 );
108 				if (((ColumnListModel)this.selectedColumns.getModel()).swap(
109 						this.selectedColumns.getSelectedIndex(), toIndex)) {
110 					this.selectedColumns.setSelectedIndex(toIndex);
111 				}
112 			}
113 		}
114 	}
115 	
116 	
117 	/**
118 	 *  Overriden super class method
119 	 * @see com.mindtree.techworks.insight.gui.preferences.AbstractPreferencesUIPanel#initializeDisplay()
120 	 */
121 	protected void initializeDisplay() {
122 		ResourceManager rm = ResourceManager.getInstance();
123 		addComponent(new JLabel(InsightConstants.getLiteral("AVAILABLE_COLUMNS")),0,0,1,0,1,1);		
124 		addComponent(new JLabel(InsightConstants.getLiteral("SELECTED_COLUMNS")),2,0,1,0,1,1);
125 		int[] configuredCols = PreferenceInterpreter.getConfiguredColumnsForDisplay();
126 		this.availableColumns = new JList(new ColumnListModel(configuredCols, true));
127 		this.selectedColumns = new JList(new ColumnListModel(configuredCols, false));
128 		addComponent(new JScrollPane(availableColumns),0,1,1,1,1,3);
129 		addComponent(new JScrollPane(selectedColumns),2,1,1,1,1,3);
130 		this.leftButton = new JButton(rm.loadImageIcon("MOVE_LEFT_ICON"));
131 		this.rightButton = new JButton(rm.loadImageIcon("MOVE_RIGHT_ICON"));
132 		addComponent(leftButton,1,1,0,0,1,1);
133 		addComponent(rightButton,1,2,0,0,1,1);
134 		this.upButton = new JButton(rm.loadImageIcon("MOVE_UP_ICON"));
135 		this.downButton = new JButton(rm.loadImageIcon("MOVE_DOWN_ICON"));
136 		addComponent(upButton,3,1,0,0,1,1);
137 		addComponent(downButton,3,2,0,0,1,1);
138 		// add an empty label to dock components to the top
139 		addComponent(new JLabel(),0,4,1,1,4,1);		
140 		
141 		this.leftButton.addActionListener(this);
142 		this.rightButton.addActionListener(this);
143 		this.upButton.addActionListener(this);
144 		this.downButton.addActionListener(this);
145 	}
146 	
147 	/**
148 	 *  Overriden super class method
149 	 * @see com.mindtree.techworks.insight.gui.preferences.AbstractPreferencesUIPanel#setPreferenceValues()
150 	 */
151 	protected  void setPreferenceValues() {
152 		ArrayList oldValues = new ArrayList();
153 		Iterator iterator = this.preference.iteratePreferenceAttributeIds();
154 		while(iterator.hasNext()) {
155 			String id = (String)iterator.next();
156 			if (id.startsWith(ATTRIBUTE_PREFIX)) {
157 				oldValues.add(id);
158 			}
159 		}
160 		iterator = oldValues.iterator();
161 		while(iterator.hasNext()) {
162 			this.preference.removePreferenceAttribute(
163 					this.preference.getPreferenceAttributeById(
164 							(String)iterator.next()));
165 		}
166 		for (int i = 0; i < this.selectedColumns.getModel().getSize(); i++) {
167 			String identifier = String.valueOf(((ColumnListModel)this.selectedColumns.getModel()).getValue(i));
168 			PreferenceAttribute newAttribute = new PreferenceAttribute(PreferenceAttributeType.TEXT,
169 					ATTRIBUTE_PREFIX + i, identifier, false,
170 					true, true, this.preference);
171 			newAttribute.setName(ATTRIBUTE_PREFIX + i);
172 			this.preference.addPreferenceAttribute(newAttribute);
173 		}
174 	}
175 
176 	/**
177 	 * Helper class to that acts as the model for the JLists 
178 	 */
179 	private class ColumnListModel extends AbstractListModel {
180 		
181 		/**
182 		 * Used for object serialization
183 		 */
184 		private static final long serialVersionUID = 4056148701534021294L;
185 
186 		/**
187 		 * The JList that this ListModel is backing
188 		 */
189 //		private JList jList;
190 
191 		/**
192 		 * List containing the column identifiers
193 		 */
194 		private ArrayList columnList = new ArrayList();
195 		
196 		/**
197 		 * Constructor for this class
198 		 * @param cols array of column ids for this model that might be complementary, if specified
199 		 * @param isComplementary true if the specified list is complementary
200 		 */
201 		public ColumnListModel(int[] cols, boolean isComplementary) {
202 			if (isComplementary) {
203 				for (int i = LogEventTableModel.FIRST_INDEX; i <= LogEventTableModel.LAST_INDEX; i++) {
204 					boolean found = false;
205 					for (int j = 0; j < cols.length; j++) {
206 						if (i == cols[j]) {
207 							found = true;
208 							break;
209 						}
210 					}
211 					if (!found) {
212 						columnList.add(new Integer(i));
213 					}
214 				}
215 			} else {
216 				for (int i = 0; i < cols.length; i++) {
217 					columnList.add(new Integer(cols[i]));
218 				}				
219 			}
220 		}
221 		
222 		/**
223 		 * Adds the specified value at the specified index
224 		 * @param value value to be inserted
225 		 * @param index index at which to insert the specified value
226 		 */
227 		public void insert(int value, int index) {
228 			if (index < 0 || index > columnList.size()) {
229 				return;
230 			}
231 			columnList.add(index, new Integer(value));
232 			super.fireIntervalAdded(this, index, index);			
233 		}
234 		
235 		/**
236 		 * Removes the value at the specified index
237 		 * @param index index of item to be removed
238 		 * @return null or the Object that was removed
239 		 */
240 		public Object remove(int index) {
241 			if (index < 0 || index >= columnList.size()) {
242 				return null;
243 			}
244 			Object objectToBeRemoved = columnList.get(index); 
245 			columnList.remove(index);
246 			super.fireIntervalRemoved(this, index, index);	
247 			return objectToBeRemoved;
248 		}
249 		
250 		/**
251 		 * Swaps objects between the specified indices  
252 		 * @param from the from index for swap
253 		 * @param to the to index of the swap
254 		 * @return true if swap was valid, false otherwise
255 		 */
256 		public boolean swap(int from, int to) {
257 			if (from >= columnList.size() || from < 0 || 
258 				to >= columnList.size() || to < 0) {
259 				return false;
260 			}
261 			Object fromValue = columnList.get(from);
262 			Object toValue = columnList.get(to);
263 			columnList.set(to, fromValue);
264 			columnList.set(from, toValue);
265 			super.fireContentsChanged(this, from, to);
266 			return true;
267 		}
268 		
269 		/**
270 		 * Superclass abstract method implementation
271 		 * @see javax.swing.ListModel#getSize()
272 		 */
273 		public int getSize() {
274 			return columnList.size();
275 		}
276 		
277 		/**
278 		 * Gets the value as a primitive int at the sepcified index
279 		 * @param index index to get the value from
280 		 * @return int value at the specified index
281 		 */
282 		public int getValue(int index) {
283 			return ((Integer)columnList.get(index)).intValue();
284 		}
285 
286 		/**
287 		 * Superclass abstract method implementation
288 		 * @see javax.swing.ListModel#getElementAt(int)
289 		 */
290 		public Object getElementAt(int index) {
291 			return LogEventTableModel.COL_NAMES.get(columnList.get(index));
292 		}
293 		
294 	}
295 
296 }