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