summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/gui/ClassSelector.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/gui/ClassSelector.java')
-rw-r--r--src/cuchaz/enigma/gui/ClassSelector.java131
1 files changed, 52 insertions, 79 deletions
diff --git a/src/cuchaz/enigma/gui/ClassSelector.java b/src/cuchaz/enigma/gui/ClassSelector.java
index 8365def..654bfbe 100644
--- a/src/cuchaz/enigma/gui/ClassSelector.java
+++ b/src/cuchaz/enigma/gui/ClassSelector.java
@@ -30,39 +30,32 @@ import com.google.common.collect.Multimap;
30 30
31import cuchaz.enigma.mapping.ClassEntry; 31import cuchaz.enigma.mapping.ClassEntry;
32 32
33public class ClassSelector extends JTree 33public class ClassSelector extends JTree {
34{ 34
35 private static final long serialVersionUID = -7632046902384775977L; 35 private static final long serialVersionUID = -7632046902384775977L;
36 36
37 public interface ClassSelectionListener 37 public interface ClassSelectionListener {
38 { 38 void onSelectClass(ClassEntry classEntry);
39 void onSelectClass( ClassEntry classEntry );
40 } 39 }
41 40
42 public static Comparator<ClassEntry> ObfuscatedClassEntryComparator; 41 public static Comparator<ClassEntry> ObfuscatedClassEntryComparator;
43 public static Comparator<ClassEntry> DeobfuscatedClassEntryComparator; 42 public static Comparator<ClassEntry> DeobfuscatedClassEntryComparator;
44 43
45 static 44 static {
46 { 45 ObfuscatedClassEntryComparator = new Comparator<ClassEntry>() {
47 ObfuscatedClassEntryComparator = new Comparator<ClassEntry>( )
48 {
49 @Override 46 @Override
50 public int compare( ClassEntry a, ClassEntry b ) 47 public int compare(ClassEntry a, ClassEntry b) {
51 { 48 if (a.getName().length() != b.getName().length()) {
52 if( a.getName().length() != b.getName().length() )
53 {
54 return a.getName().length() - b.getName().length(); 49 return a.getName().length() - b.getName().length();
55 } 50 }
56 return a.getName().compareTo( b.getName() ); 51 return a.getName().compareTo(b.getName());
57 } 52 }
58 }; 53 };
59 54
60 DeobfuscatedClassEntryComparator = new Comparator<ClassEntry>( ) 55 DeobfuscatedClassEntryComparator = new Comparator<ClassEntry>() {
61 {
62 @Override 56 @Override
63 public int compare( ClassEntry a, ClassEntry b ) 57 public int compare(ClassEntry a, ClassEntry b) {
64 { 58 return a.getName().compareTo(b.getName());
65 return a.getName().compareTo( b.getName() );
66 } 59 }
67 }; 60 };
68 } 61 }
@@ -70,122 +63,102 @@ public class ClassSelector extends JTree
70 private ClassSelectionListener m_listener; 63 private ClassSelectionListener m_listener;
71 private Comparator<ClassEntry> m_comparator; 64 private Comparator<ClassEntry> m_comparator;
72 65
73 public ClassSelector( Comparator<ClassEntry> comparator ) 66 public ClassSelector(Comparator<ClassEntry> comparator) {
74 {
75 m_comparator = comparator; 67 m_comparator = comparator;
76 68
77 // configure the tree control 69 // configure the tree control
78 setRootVisible( false ); 70 setRootVisible(false);
79 setShowsRootHandles( false ); 71 setShowsRootHandles(false);
80 setModel( null ); 72 setModel(null);
81 73
82 // hook events 74 // hook events
83 addMouseListener( new MouseAdapter() 75 addMouseListener(new MouseAdapter() {
84 {
85 @Override 76 @Override
86 public void mouseClicked( MouseEvent event ) 77 public void mouseClicked(MouseEvent event) {
87 { 78 if (m_listener != null && event.getClickCount() == 2) {
88 if( m_listener != null && event.getClickCount() == 2 )
89 {
90 // get the selected node 79 // get the selected node
91 TreePath path = getSelectionPath(); 80 TreePath path = getSelectionPath();
92 if( path != null && path.getLastPathComponent() instanceof ClassSelectorClassNode ) 81 if (path != null && path.getLastPathComponent() instanceof ClassSelectorClassNode) {
93 {
94 ClassSelectorClassNode node = (ClassSelectorClassNode)path.getLastPathComponent(); 82 ClassSelectorClassNode node = (ClassSelectorClassNode)path.getLastPathComponent();
95 m_listener.onSelectClass( node.getClassEntry() ); 83 m_listener.onSelectClass(node.getClassEntry());
96 } 84 }
97 } 85 }
98 } 86 }
99 } ); 87 });
100 88
101 // init defaults 89 // init defaults
102 m_listener = null; 90 m_listener = null;
103 } 91 }
104 92
105 public void setListener( ClassSelectionListener val ) 93 public void setListener(ClassSelectionListener val) {
106 {
107 m_listener = val; 94 m_listener = val;
108 } 95 }
109 96
110 public void setClasses( Collection<ClassEntry> classEntries ) 97 public void setClasses(Collection<ClassEntry> classEntries) {
111 { 98 if (classEntries == null) {
112 if( classEntries == null ) 99 setModel(null);
113 {
114 setModel( null );
115 return; 100 return;
116 } 101 }
117 102
118 // build the package names 103 // build the package names
119 Map<String,ClassSelectorPackageNode> packages = Maps.newHashMap(); 104 Map<String,ClassSelectorPackageNode> packages = Maps.newHashMap();
120 for( ClassEntry classEntry : classEntries ) 105 for (ClassEntry classEntry : classEntries) {
121 { 106 packages.put(classEntry.getPackageName(), null);
122 packages.put( classEntry.getPackageName(), null );
123 } 107 }
124 108
125 // sort the packages 109 // sort the packages
126 List<String> sortedPackageNames = Lists.newArrayList( packages.keySet() ); 110 List<String> sortedPackageNames = Lists.newArrayList(packages.keySet());
127 Collections.sort( sortedPackageNames, new Comparator<String>( ) 111 Collections.sort(sortedPackageNames, new Comparator<String>() {
128 {
129 @Override 112 @Override
130 public int compare( String a, String b ) 113 public int compare(String a, String b) {
131 {
132 // I can never keep this rule straight when writing these damn things... 114 // I can never keep this rule straight when writing these damn things...
133 // a < b => -1, a == b => 0, a > b => +1 115 // a < b => -1, a == b => 0, a > b => +1
134 116
135 String[] aparts = a.split( "/" ); 117 String[] aparts = a.split("/");
136 String[] bparts = b.split( "/" ); 118 String[] bparts = b.split("/");
137 for( int i=0; true; i++ ) 119 for (int i = 0; true; i++) {
138 { 120 if (i >= aparts.length) {
139 if( i >= aparts.length )
140 {
141 return -1; 121 return -1;
142 } 122 } else if (i >= bparts.length) {
143 else if( i >= bparts.length )
144 {
145 return 1; 123 return 1;
146 } 124 }
147 125
148 int result = aparts[i].compareTo( bparts[i] ); 126 int result = aparts[i].compareTo(bparts[i]);
149 if( result != 0 ) 127 if (result != 0) {
150 {
151 return result; 128 return result;
152 } 129 }
153 } 130 }
154 } 131 }
155 } ); 132 });
156 133
157 // create the root node and the package nodes 134 // create the root node and the package nodes
158 DefaultMutableTreeNode root = new DefaultMutableTreeNode(); 135 DefaultMutableTreeNode root = new DefaultMutableTreeNode();
159 for( String packageName : sortedPackageNames ) 136 for (String packageName : sortedPackageNames) {
160 { 137 ClassSelectorPackageNode node = new ClassSelectorPackageNode(packageName);
161 ClassSelectorPackageNode node = new ClassSelectorPackageNode( packageName ); 138 packages.put(packageName, node);
162 packages.put( packageName, node ); 139 root.add(node);
163 root.add( node );
164 } 140 }
165 141
166 // put the classes into packages 142 // put the classes into packages
167 Multimap<String,ClassEntry> packagedClassEntries = ArrayListMultimap.create(); 143 Multimap<String,ClassEntry> packagedClassEntries = ArrayListMultimap.create();
168 for( ClassEntry classEntry : classEntries ) 144 for (ClassEntry classEntry : classEntries) {
169 { 145 packagedClassEntries.put(classEntry.getPackageName(), classEntry);
170 packagedClassEntries.put( classEntry.getPackageName(), classEntry );
171 } 146 }
172 147
173 // build the class nodes 148 // build the class nodes
174 for( String packageName : packagedClassEntries.keySet() ) 149 for (String packageName : packagedClassEntries.keySet()) {
175 {
176 // sort the class entries 150 // sort the class entries
177 List<ClassEntry> classEntriesInPackage = Lists.newArrayList( packagedClassEntries.get( packageName ) ); 151 List<ClassEntry> classEntriesInPackage = Lists.newArrayList(packagedClassEntries.get(packageName));
178 Collections.sort( classEntriesInPackage, m_comparator ); 152 Collections.sort(classEntriesInPackage, m_comparator);
179 153
180 // create the nodes in order 154 // create the nodes in order
181 for( ClassEntry classEntry : classEntriesInPackage ) 155 for (ClassEntry classEntry : classEntriesInPackage) {
182 { 156 ClassSelectorPackageNode node = packages.get(packageName);
183 ClassSelectorPackageNode node = packages.get( packageName ); 157 node.add(new ClassSelectorClassNode(classEntry));
184 node.add( new ClassSelectorClassNode( classEntry ) );
185 } 158 }
186 } 159 }
187 160
188 // finally, update the tree control 161 // finally, update the tree control
189 setModel( new DefaultTreeModel( root ) ); 162 setModel(new DefaultTreeModel(root));
190 } 163 }
191} 164}