PageRenderTime 300ms CodeModel.GetById 283ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 1ms

/bundles/plugins-trunk/TextTools/TextToolsSortControl.java

#
Java | 422 lines | 278 code | 53 blank | 91 comment | 19 complexity | 43d2f00e818d6f242a4a7d28c9a091d4 MD5 | raw file
  1/*
  2 * TextToolsSortControl.java - sort options
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2002 Carmine Lucarelli
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23
 24//{{{ imports
 25import javax.swing.*;
 26import javax.swing.table.*;
 27import javax.swing.border.*;
 28import java.awt.*;
 29import java.awt.event.*;
 30import java.util.Iterator;
 31
 32import org.gjt.sp.jedit.*;
 33import org.gjt.sp.util.Log;
 34import org.gjt.sp.jedit.textarea.JEditTextArea;
 35import org.gjt.sp.jedit.gui.EnhancedDialog;
 36//}}}
 37
 38/**
 39 * An option dialog for JSort options.
 40 * @author Carmine Lucarelli
 41 */
 42public class TextToolsSortControl extends EnhancedDialog implements ActionListener
 43{
 44	
 45	//{{{ TextToolsSortControl constructor
 46	/**
 47	  * Constructor.  Sets up and shows the GUI
 48	  */
 49	public TextToolsSortControl(View view, JEditTextArea textArea, java.util.List data, boolean selection)
 50	{
 51		super(view, jEdit.getProperty("text-tools.sortcontrol.label"), false);
 52
 53		this.view = view;
 54		this.textArea = textArea;
 55		this.data = data;
 56		this.selection = selection;
 57	
 58		view.showWaitCursor();
 59
 60		sortTableModel = new SortTableModel();
 61		sortTable = new JTable(sortTableModel);
 62		TableColumnModel cMod = sortTable.getColumnModel();
 63		sortTable.setTableHeader((new SortTableHeader(cMod)));
 64		sortTable.setRowHeight(25);
 65		
 66		sortTable.setPreferredScrollableViewportSize(new Dimension(430, 200));
 67		
 68		JScrollPane scroll = new JScrollPane(sortTable);
 69		
 70		JPanel content = new JPanel(new BorderLayout());
 71		content.setBorder(new EmptyBorder(5,8,8,8));
 72		content.setLayout(new BorderLayout());
 73		setContentPane(content);
 74		content.add(scroll, BorderLayout.CENTER);
 75
 76		JPanel buttons = new JPanel();
 77		buttons.setBorder(new EmptyBorder(12,0,0,0));
 78		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
 79		buttons.add(Box.createGlue());
 80
 81		ok = new JButton(jEdit.getProperty("common.ok"));
 82		ok.addActionListener(this);
 83		buttons.add(ok);
 84		buttons.add(Box.createHorizontalStrut(6));
 85		getRootPane().setDefaultButton(ok);
 86
 87		cancel = new JButton(jEdit.getProperty("common.cancel"));
 88		cancel.addActionListener(this);
 89		buttons.add(cancel);
 90		buttons.add(Box.createHorizontalStrut(6));
 91
 92		clear = new JButton("Clear");
 93		clear.addActionListener(this);
 94		buttons.add(clear);
 95		buttons.add(Box.createHorizontalStrut(6));
 96
 97		help = new JButton("Help");
 98		help.addActionListener(this);
 99		buttons.add(help);
100		buttons.add(Box.createHorizontalStrut(6));
101
102		buttons.add(Box.createGlue());
103
104		content.add(buttons, BorderLayout.SOUTH);
105
106		dups = new JCheckBox("Delete Duplicate Entries: ");
107		content.add(dups, BorderLayout.NORTH);
108		view.hideWaitCursor();
109		pack();
110		GUIUtilities.loadGeometry(this, "texttools-sort-control");
111		setLocationRelativeTo(view);
112		setVisible(true);
113	} //}}}
114	
115	//{{{ EnhancedDialog methods
116	
117	//{{{ ok() method
118	public void ok()
119	{
120		ok(true);
121	} //}}}
122	
123	//{{{ cancel() method
124	public void cancel()
125	{
126		dispose();
127	}//}}}
128
129	//}}}
130	
131	//{{{ ok() method
132	/**
133	  * run the sort
134	  */
135	public void ok(boolean dispose)
136	{
137		jsort = new JSort(dups.isSelected());
138		Log.log(Log.DEBUG, this, " delDups is set to " + dups.isSelected());
139
140		for(int i = 0; i < 10; i++)
141		{
142			int start = ((Integer)sortTableModel.getValueAt(i, 0)).intValue();
143			int end = ((Integer)sortTableModel.getValueAt(i, 1)).intValue();
144			if(end == 0)
145			{
146				break;
147			}
148			boolean ascending = !((Boolean)sortTableModel.getValueAt(i, 2)).booleanValue();
149			boolean ignoreCase = ((Boolean)sortTableModel.getValueAt(i, 3)).booleanValue();
150			boolean textType = ((Boolean)sortTableModel.getValueAt(i, 4)).booleanValue();
151			boolean trim = ((Boolean)sortTableModel.getValueAt(i, 5)).booleanValue();
152			Log.log(Log.DEBUG, this, "Sort constraint: " + start + " " + end + " " + ascending
153				+ " " + ignoreCase + " " + textType + " " + trim);
154			jsort.addSortConstraint(start, end, ascending, ignoreCase, textType, trim, false);
155		}
156
157 		jsort.sort(data);
158		Iterator iter = data.iterator();
159		StringBuilder sb = new StringBuilder();
160		while(iter.hasNext())
161		{
162			sb.append(iter.next()).append('\n');
163		}
164		sb.deleteCharAt(sb.length() - 1);
165		
166		if(selection)
167		{
168			textArea.setSelectedText(sb.toString());
169		}
170		else
171		{
172			textArea.setText(sb.toString());
173		}
174
175		GUIUtilities.saveGeometry(this, "texttools-sort-control");
176
177		// get rid of this dialog if necessary
178		if (dispose)
179			dispose();
180	} //}}}
181	
182	//{{{ actionPerformed() method
183	public void actionPerformed(ActionEvent evt)
184	{
185		Object source = evt.getSource();
186
187		if (source == ok)
188		{
189			ok();
190		}
191		else if (source == cancel)
192		{
193			cancel();
194		}
195		else if (source == clear)
196		{
197			sortTableModel.clear();
198		}
199		else if(source == help)
200		{
201			showHelp();
202		}
203	} //}}}
204	
205	//{{{ showHelp() method
206	/**
207	* Shows JSort help in the jEdit help viewer
208	*/
209	private void showHelp()
210	{
211		java.net.URL helpUrl = TextToolsSortControl.class.getResource("TextTools.html");
212		if (helpUrl == null)
213		{
214			Log.log(Log.NOTICE, this, "Help URL is null, cannot display help");
215		}
216		else 
217		{
218			new org.gjt.sp.jedit.help.HelpViewer(helpUrl.toString());
219		}
220	} //}}}
221	
222	//{{{ SortTableModel class
223	/**
224	  * Our table model
225	  */
226	private class SortTableModel extends AbstractTableModel
227	{
228		//{{{ SortTableModel constructor
229		
230		public SortTableModel()
231		{
232			columnNames = new String[6];
233			columnNames[0] = "Start";
234			columnNames[1] = "End";
235			columnNames[2] = "Desc";
236			columnNames[3] = "ICase";
237			columnNames[4] = "Numeric";
238			columnNames[5] = "Trim";
239
240			clear();
241		} //}}}
242		
243		//{{{ clear() method
244		public void clear()
245		{
246			startColumns = new int[10];
247			endColumns = new int[10];
248			ignoreCases = new boolean[10];
249			directions = new boolean[10];
250			types = new boolean[10];
251			trims = new boolean[10];
252			fireTableDataChanged();
253		}//}}}
254		
255		//{{{ setValueAt() method
256		public void setValueAt(Object value, int row, int col)
257		{
258			switch(col)
259			{
260				case 0:
261					startColumns[row] = ((Integer)value).intValue();
262					break;
263				case 1:
264					endColumns[row] = ((Integer)value).intValue();
265					break;
266				case 2:
267					directions[row] = ((Boolean)value).booleanValue();
268					break;
269				case 3:
270					ignoreCases[row] = ((Boolean)value).booleanValue();
271					break;
272				case 4:
273					types[row] = ((Boolean)value).booleanValue();
274					break;
275				case 5:
276					trims[row] = ((Boolean)value).booleanValue();
277					break;
278			}
279			fireTableCellUpdated(row, col);
280		} //}}}
281		
282		//{{{ getColumnCount() method
283		public int getColumnCount()
284		{
285			return columnNames.length;
286		}//}}}
287		
288		//{{{ getRowCount() method
289		public int getRowCount()
290		{
291			return startColumns.length;
292		} //}}}
293		
294		//{{{ getColumnName() method
295		public String getColumnName(int col)
296		{
297			return columnNames[col];
298		} //}}}
299		
300		//{{{ getValueAt() method
301		public Object getValueAt(int row, int col)
302		{
303			switch(col)
304			{
305				case 0:
306					return new Integer(startColumns[row]);
307				case 1:
308					return new Integer(endColumns[row]);
309				case 2:
310					return new Boolean(directions[row]);
311				case 3:
312					return new Boolean(ignoreCases[row]);
313				case 4:
314					return new Boolean(types[row]);
315				case 5:
316					return new Boolean(trims[row]);
317			}
318			return null;
319		} //}}}
320		
321		//{{{ getColumnClass() method
322		/**
323		 *  JTable uses this method to determine the default renderer/
324		 *  editor for each cell.  If we didn't implement this method,
325		 *  then the last column would contain text ("true"/"false"),
326		 *  rather than a check box.
327		 */
328		public Class getColumnClass(int c)
329		{
330			return getValueAt(0, c).getClass();
331		} //}}}
332		
333		//{{{ isCellEditable() method
334		/*
335		 *  Don't need to implement this method unless your table's
336		 *  editable.
337		 */
338		public boolean isCellEditable(int row, int col)
339		{
340			return true;
341		} //}}}
342		
343		//{{{ Private members
344		private String[] columnNames;
345		private int[] startColumns;
346		private int[] endColumns;
347		private boolean[] ignoreCases;
348		private boolean[] directions;
349		private boolean[] types;
350		private boolean[] trims;
351		//}}}
352	} //}}}
353	
354	//{{{ SortTableHeader class
355	private class SortTableHeader extends JTableHeader
356	{
357		//{{{ SortTableHeader constructor
358		public SortTableHeader(TableColumnModel tcom)
359		{
360			super(tcom);
361		} //}}}
362		
363		//{{{ getToolTipText() method
364		public String getToolTipText(MouseEvent event)
365		{
366			Point p = event.getPoint();
367			switch(this.columnAtPoint(p))
368			{
369				case 0:
370					return "Start column for the sort field";
371				case 1:
372					return "End column for the sort field";
373				case 2:
374					return "Enable for descending sort order";
375				case 3:
376					return "Enable for case insensitive sort";
377				case 4:
378					return "Enable for numeric type sort";
379				case 5:
380					return "Trim whitespace before sorting";
381			}
382			return null;
383		} //}}}
384	} //}}}
385	
386	//{{{ Private members
387	/**
388	  * The class with the sort routines.
389	  */
390	private JSort jsort;
391	
392	/**
393	  * The data to be sorted
394	  */
395	private java.util.List data;
396	
397	/**
398	  * The view containing the buffer that contained the data to be sorted (whew!!)
399	  */
400	private View view;
401	
402	/**
403	  * The text area we'll write the results to
404	  */
405	private JEditTextArea textArea;
406	
407	/**
408	  * Did the data come from a selection within the buffer?
409	  */
410	private boolean selection;
411
412	// gui components
413	private JCheckBox dups;
414	private JButton ok;
415	private JButton cancel;
416	private JButton clear;
417	private JButton help;
418	private SortTableModel sortTableModel;
419	private JTable sortTable;
420
421	//}}}
422}