1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.gui;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import cuchaz.enigma.mapping.ClassEntry;
public class ClassSelector extends JTree
{
private static final long serialVersionUID = -7632046902384775977L;
public interface ClassSelectionListener
{
void onSelectClass( ClassEntry classEntry );
}
public static Comparator<ClassEntry> ObfuscatedClassEntryComparator;
public static Comparator<ClassEntry> DeobfuscatedClassEntryComparator;
static
{
ObfuscatedClassEntryComparator = new Comparator<ClassEntry>( )
{
@Override
public int compare( ClassEntry a, ClassEntry b )
{
if( a.getName().length() != b.getName().length() )
{
return a.getName().length() - b.getName().length();
}
return a.getName().compareTo( b.getName() );
}
};
DeobfuscatedClassEntryComparator = new Comparator<ClassEntry>( )
{
@Override
public int compare( ClassEntry a, ClassEntry b )
{
return a.getName().compareTo( b.getName() );
}
};
}
private ClassSelectionListener m_listener;
private Comparator<ClassEntry> m_comparator;
public ClassSelector( Comparator<ClassEntry> comparator )
{
m_comparator = comparator;
// configure the tree control
setRootVisible( false );
setShowsRootHandles( false );
setModel( null );
// hook events
addMouseListener( new MouseAdapter()
{
@Override
public void mouseClicked( MouseEvent event )
{
if( m_listener != null && event.getClickCount() == 2 )
{
// get the selected node
TreePath path = getSelectionPath();
if( path != null && path.getLastPathComponent() instanceof ClassSelectorClassNode )
{
ClassSelectorClassNode node = (ClassSelectorClassNode)path.getLastPathComponent();
m_listener.onSelectClass( node.getClassEntry() );
}
}
}
} );
// init defaults
m_listener = null;
}
public void setListener( ClassSelectionListener val )
{
m_listener = val;
}
public void setClasses( Collection<ClassEntry> classEntries )
{
if( classEntries == null )
{
setModel( null );
return;
}
// build the package names
Map<String,ClassSelectorPackageNode> packages = Maps.newHashMap();
for( ClassEntry classEntry : classEntries )
{
packages.put( classEntry.getPackageName(), null );
}
// sort the packages
List<String> sortedPackageNames = Lists.newArrayList( packages.keySet() );
Collections.sort( sortedPackageNames, new Comparator<String>( )
{
@Override
public int compare( String a, String b )
{
// I can never keep this rule straight when writing these damn things...
// a < b => -1, a == b => 0, a > b => +1
String[] aparts = a.split( "/" );
String[] bparts = b.split( "/" );
for( int i=0; true; i++ )
{
if( i >= aparts.length )
{
return -1;
}
else if( i >= bparts.length )
{
return 1;
}
int result = aparts[i].compareTo( bparts[i] );
if( result != 0 )
{
return result;
}
}
}
} );
// create the root node and the package nodes
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
for( String packageName : sortedPackageNames )
{
ClassSelectorPackageNode node = new ClassSelectorPackageNode( packageName );
packages.put( packageName, node );
root.add( node );
}
// put the classes into packages
Multimap<String,ClassEntry> packagedClassEntries = ArrayListMultimap.create();
for( ClassEntry classEntry : classEntries )
{
packagedClassEntries.put( classEntry.getPackageName(), classEntry );
}
// build the class nodes
for( String packageName : packagedClassEntries.keySet() )
{
// sort the class entries
List<ClassEntry> classEntriesInPackage = Lists.newArrayList( packagedClassEntries.get( packageName ) );
Collections.sort( classEntriesInPackage, m_comparator );
// create the nodes in order
for( ClassEntry classEntry : classEntriesInPackage )
{
ClassSelectorPackageNode node = packages.get( packageName );
node.add( new ClassSelectorClassNode( classEntry ) );
}
}
// finally, update the tree control
setModel( new DefaultTreeModel( root ) );
}
}
|