KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Sort


1 /*
2  * Sort.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: Sort.java,v 1.2 2004/06/06 04:38:27 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import javax.swing.undo.CompoundEdit JavaDoc;
28
29 public final class Sort
30 {
31     public static void sortLines()
32     {
33         final Editor editor = Editor.currentEditor();
34         if (editor.getMark() == null)
35             return;
36         final Region region = new Region(editor);
37         if (region.getEndLineNumber() - region.getBeginLineNumber() < 2)
38             return;
39         if (!editor.checkReadOnly())
40             return;
41         final Buffer buffer = editor.getBuffer();
42         try {
43             buffer.lockWrite();
44         }
45         catch (InterruptedException JavaDoc e) {
46             Log.error(e);
47             return;
48         }
49         try {
50             sortLinesInternal(editor, buffer, region);
51         }
52         finally {
53             buffer.unlockWrite();
54         }
55     }
56
57     private static void sortLinesInternal(Editor editor, Buffer buffer, Region region)
58     {
59         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
60         for (Line line = region.getBeginLine(); line != region.getEndLine(); line = line.next())
61             arrayList.add(line.getText());
62         Collections.sort(arrayList, new SortLinesComparator());
63         CompoundEdit JavaDoc compoundEdit = null;
64         int i = 0;
65         for (Line line = region.getBeginLine(); line != region.getEndLine(); line = line.next(), i++) {
66             String JavaDoc newText = (String JavaDoc) arrayList.get(i);
67             if (!newText.equals(line.getText())) {
68                 if (compoundEdit == null) {
69                     compoundEdit = new CompoundEdit JavaDoc();
70                     compoundEdit.addEdit(new UndoMove(editor));
71                 }
72                 compoundEdit.addEdit(new UndoLineEdit(buffer, line));
73                 line.setText(newText);
74             }
75         }
76         if (compoundEdit != null) {
77             compoundEdit.end();
78             buffer.addEdit(compoundEdit);
79             buffer.modified();
80         }
81         buffer.setNeedsParsing(true);
82         buffer.getFormatter().parseBuffer();
83         buffer.repaint();
84     }
85
86     private static class SortLinesComparator implements Comparator JavaDoc
87     {
88         SortLinesComparator() {}
89
90         public final int compare(Object JavaDoc o1, Object JavaDoc o2)
91         {
92             return ((String JavaDoc)o1).compareTo((String JavaDoc)o2);
93         }
94     }
95 }
96
Popular Tags