|
| 1 | +/******************************************************************************* |
| 2 | + * Star Wars Galaxies Stf Searcher by Waverunner |
| 3 | + * Copyright (c) 2013 <Project SWG> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + ******************************************************************************/ |
| 19 | +package com.projectswg.tools.stf; |
| 20 | + |
| 21 | +import java.awt.EventQueue; |
| 22 | + |
| 23 | +import javax.swing.JFileChooser; |
| 24 | +import javax.swing.JFrame; |
| 25 | +import javax.swing.JLabel; |
| 26 | +import javax.swing.JTextField; |
| 27 | +import javax.swing.JButton; |
| 28 | +import javax.swing.UIManager; |
| 29 | +import javax.swing.JPanel; |
| 30 | +import javax.swing.JTextPane; |
| 31 | + |
| 32 | +import engine.clientdata.StfTable; |
| 33 | +import engine.clientdata.StfTable.Pair; |
| 34 | + |
| 35 | +import java.awt.BorderLayout; |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import java.nio.file.Paths; |
| 39 | +import java.util.Date; |
| 40 | +import java.util.prefs.Preferences; |
| 41 | +import java.awt.event.ActionListener; |
| 42 | +import java.awt.event.ActionEvent; |
| 43 | +import java.awt.event.WindowEvent; |
| 44 | +import java.awt.event.WindowListener; |
| 45 | + |
| 46 | +import javax.swing.JScrollPane; |
| 47 | +import javax.swing.JCheckBox; |
| 48 | + |
| 49 | +public class StfSearcher { |
| 50 | + |
| 51 | + private static StfSearcher searcher; |
| 52 | + |
| 53 | + private JFrame frmPswgTools; |
| 54 | + private String searchDirectory = ""; |
| 55 | + private JTextField tfDirectory; |
| 56 | + private JTextField tfValue; |
| 57 | + private JTextPane console; |
| 58 | + |
| 59 | + private Preferences prefs = Preferences.userRoot().node(this.getClass().getName()); |
| 60 | + |
| 61 | + public StfSearcher() { |
| 62 | + searcher = this; |
| 63 | + initialize(); |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + EventQueue.invokeLater(new Runnable() { |
| 68 | + public void run() { |
| 69 | + try { |
| 70 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 71 | + StfSearcher window = new StfSearcher(); |
| 72 | + window.frmPswgTools.setVisible(true); |
| 73 | + } catch (Exception e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Initialize the contents of the frame. |
| 82 | + */ |
| 83 | + private void initialize() { |
| 84 | + frmPswgTools = new JFrame(); |
| 85 | + frmPswgTools.setResizable(false); |
| 86 | + frmPswgTools.setTitle("PSWG Tools - Stf Searcher"); |
| 87 | + frmPswgTools.setBounds(100, 100, 510, 442); |
| 88 | + frmPswgTools.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 89 | + frmPswgTools.getContentPane().setLayout(null); |
| 90 | + |
| 91 | + frmPswgTools.addWindowListener(new WindowListener() { |
| 92 | + @Override |
| 93 | + public void windowClosed(WindowEvent e) {} |
| 94 | + @Override |
| 95 | + public void windowActivated(WindowEvent e) {} |
| 96 | + @Override |
| 97 | + public void windowClosing(WindowEvent e) { prefs.put("searchDirectory", searchDirectory); } |
| 98 | + @Override |
| 99 | + public void windowDeactivated(WindowEvent e) {} |
| 100 | + @Override |
| 101 | + public void windowDeiconified(WindowEvent e) {} |
| 102 | + @Override |
| 103 | + public void windowIconified(WindowEvent e) {} |
| 104 | + @Override |
| 105 | + public void windowOpened(WindowEvent e) {} |
| 106 | + }); |
| 107 | + |
| 108 | + JPanel panelSettings = new JPanel(); |
| 109 | + panelSettings.setBounds(10, 11, 474, 101); |
| 110 | + frmPswgTools.getContentPane().add(panelSettings); |
| 111 | + panelSettings.setLayout(null); |
| 112 | + |
| 113 | + JLabel lblSearchDirectory = new JLabel("Search Directory"); |
| 114 | + lblSearchDirectory.setBounds(10, 11, 89, 14); |
| 115 | + panelSettings.add(lblSearchDirectory); |
| 116 | + |
| 117 | + tfDirectory = new JTextField(); |
| 118 | + tfDirectory.setEditable(false); |
| 119 | + tfDirectory.setBounds(98, 8, 267, 20); |
| 120 | + panelSettings.add(tfDirectory); |
| 121 | + tfDirectory.setColumns(10); |
| 122 | + |
| 123 | + String searchDir = prefs.get("searchDirectory", ""); |
| 124 | + |
| 125 | + tfDirectory.setText(searchDir); |
| 126 | + searchDirectory = searchDir; |
| 127 | + |
| 128 | + JButton btnChangeDir = new JButton("Change"); |
| 129 | + btnChangeDir.addActionListener(new ActionListener() { |
| 130 | + public void actionPerformed(ActionEvent arg0) { |
| 131 | + JFileChooser directoryChooser = new JFileChooser(); |
| 132 | + directoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| 133 | + |
| 134 | + if (searchDirectory != null && !searchDirectory.isEmpty()) |
| 135 | + directoryChooser.setCurrentDirectory(Paths.get(searchDirectory).toFile()); |
| 136 | + int returnValue = directoryChooser.showDialog(frmPswgTools, "Select"); |
| 137 | + |
| 138 | + if (returnValue == JFileChooser.APPROVE_OPTION) { |
| 139 | + File file = directoryChooser.getSelectedFile(); |
| 140 | + |
| 141 | + setSearchDirectory(file.getAbsolutePath()); |
| 142 | + tfDirectory.setText(getSearchDirectory()); |
| 143 | + printConsole("Search directory changed to " + file.getAbsolutePath()); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + btnChangeDir.setBounds(375, 7, 89, 23); |
| 148 | + panelSettings.add(btnChangeDir); |
| 149 | + |
| 150 | + JLabel lblSearchTerm = new JLabel("Find Value"); |
| 151 | + lblSearchTerm.setBounds(10, 39, 89, 14); |
| 152 | + panelSettings.add(lblSearchTerm); |
| 153 | + |
| 154 | + tfValue = new JTextField(); |
| 155 | + tfValue.setColumns(10); |
| 156 | + tfValue.setBounds(66, 36, 398, 20); |
| 157 | + panelSettings.add(tfValue); |
| 158 | + |
| 159 | + JPanel panelButtons = new JPanel(); |
| 160 | + panelButtons.setBounds(10, 64, 454, 33); |
| 161 | + panelSettings.add(panelButtons); |
| 162 | + |
| 163 | + JCheckBox chckbxRecursive = new JCheckBox("Recursive"); |
| 164 | + chckbxRecursive.setEnabled(false); |
| 165 | + panelButtons.add(chckbxRecursive); |
| 166 | + |
| 167 | + JButton btnSearch = new JButton("Search"); |
| 168 | + panelButtons.add(btnSearch); |
| 169 | + |
| 170 | + JButton btnClearConsole = new JButton("Clear Console"); |
| 171 | + btnClearConsole.addActionListener(new ActionListener() { |
| 172 | + public void actionPerformed(ActionEvent arg0) { |
| 173 | + clearConsole(); |
| 174 | + } |
| 175 | + }); |
| 176 | + panelButtons.add(btnClearConsole); |
| 177 | + btnSearch.addActionListener(new ActionListener() { |
| 178 | + public void actionPerformed(ActionEvent arg0) { |
| 179 | + search(tfValue.getText(), false); |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + JPanel panelOutput = new JPanel(); |
| 184 | + panelOutput.setBounds(10, 123, 474, 270); |
| 185 | + frmPswgTools.getContentPane().add(panelOutput); |
| 186 | + panelOutput.setLayout(new BorderLayout(0, 0)); |
| 187 | + |
| 188 | + JScrollPane scrollPane = new JScrollPane(); |
| 189 | + panelOutput.add(scrollPane, BorderLayout.CENTER); |
| 190 | + |
| 191 | + console = new JTextPane(); |
| 192 | + console.setEditable(false); |
| 193 | + console.setText("=== Star Wars Galaxies STF Searcher ==="); |
| 194 | + scrollPane.setViewportView(console); |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + public void search(String term, boolean recursive) { |
| 199 | + printConsole("Looking for value: " + term); |
| 200 | + File directory = Paths.get(searchDirectory).toFile(); |
| 201 | + |
| 202 | + if (directory.isFile()) { |
| 203 | + printConsole("Tried to load a file!"); |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + boolean found = false; |
| 208 | + for (File f : directory.listFiles()) { |
| 209 | + if (found) |
| 210 | + break; |
| 211 | + |
| 212 | + if (f.isFile()) |
| 213 | + found = searchFile(f, term); |
| 214 | + } |
| 215 | + |
| 216 | + if (!found) |
| 217 | + printConsole("No results were found."); |
| 218 | + else |
| 219 | + printConsole("Found a file with the search term."); |
| 220 | + } |
| 221 | + |
| 222 | + public boolean searchFile(File file, String term) { |
| 223 | + try { |
| 224 | + StfTable stfTable = new StfTable(); |
| 225 | + stfTable.readFile(file.getAbsolutePath()); |
| 226 | + |
| 227 | + for (int r = 0; r < stfTable.getRowCount(); r++) { |
| 228 | + Pair<String, String> pair = stfTable.getStringById(r); |
| 229 | + String value = pair.getValue(); |
| 230 | + |
| 231 | + if (value != null && value.contains(term)) { |
| 232 | + printConsole("Found Value in " + file.getName()); |
| 233 | + return true; |
| 234 | + } |
| 235 | + //printConsole(pair.getKey() + " " + pair.getValue()); |
| 236 | + } |
| 237 | + } catch (IOException e) { |
| 238 | + e.printStackTrace(); |
| 239 | + } |
| 240 | + return false; |
| 241 | + } |
| 242 | + |
| 243 | + public void printConsole(String text) { |
| 244 | + console.setText(console.getText() + "\n" + (new Date().toString()) + ": " + text); |
| 245 | + } |
| 246 | + |
| 247 | + public void clearConsole() { |
| 248 | + console.setText("=== Star Wars Galaxies STF Searcher ==="); |
| 249 | + } |
| 250 | + |
| 251 | + public String getSearchDirectory() { |
| 252 | + return searchDirectory; |
| 253 | + } |
| 254 | + |
| 255 | + public void setSearchDirectory(String coreLocation) { |
| 256 | + this.searchDirectory = coreLocation; |
| 257 | + } |
| 258 | + |
| 259 | + public static StfSearcher getInstance() { |
| 260 | + return searcher; |
| 261 | + } |
| 262 | + |
| 263 | + public Preferences getPrefs() { |
| 264 | + return prefs; |
| 265 | + } |
| 266 | +} |
0 commit comments