| 1 |
package net.oni2.aeinstaller.gui; |
| 2 |
|
| 3 |
import java.awt.Frame; |
| 4 |
import java.io.File; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Comparator; |
| 7 |
import java.util.List; |
| 8 |
import java.util.ResourceBundle; |
| 9 |
import java.util.TreeMap; |
| 10 |
|
| 11 |
import javax.swing.JComboBox; |
| 12 |
import javax.swing.JFrame; |
| 13 |
import javax.swing.JLabel; |
| 14 |
import javax.swing.JSplitPane; |
| 15 |
import javax.swing.JTable; |
| 16 |
import javax.swing.ListSelectionModel; |
| 17 |
import javax.swing.RowSorter; |
| 18 |
import javax.swing.SortOrder; |
| 19 |
import javax.swing.SwingUtilities; |
| 20 |
import javax.swing.event.ListSelectionEvent; |
| 21 |
import javax.swing.event.ListSelectionListener; |
| 22 |
import javax.swing.table.TableRowSorter; |
| 23 |
|
| 24 |
import net.oni2.aeinstaller.backend.Settings; |
| 25 |
import net.oni2.aeinstaller.backend.depot.DepotCacheUpdateProgressListener; |
| 26 |
import net.oni2.aeinstaller.backend.depot.DepotConfig; |
| 27 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
| 28 |
import net.oni2.aeinstaller.backend.depot.model.NodeMod; |
| 29 |
import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm; |
| 30 |
import net.oni2.aeinstaller.backend.depot.model.TaxonomyVocabulary; |
| 31 |
import net.oni2.aeinstaller.gui.modtable.ModTableFilter; |
| 32 |
import net.oni2.aeinstaller.gui.modtable.ModTableModel; |
| 33 |
import net.oni2.aeinstaller.gui.settings.SettingsDialog; |
| 34 |
|
| 35 |
import org.javabuilders.BuildResult; |
| 36 |
import org.javabuilders.annotations.DoInBackground; |
| 37 |
import org.javabuilders.event.BackgroundEvent; |
| 38 |
import org.javabuilders.swing.SwingJavaBuilder; |
| 39 |
|
| 40 |
/** |
| 41 |
* @author Christian Illy |
| 42 |
*/ |
| 43 |
public class MainWin extends JFrame { |
| 44 |
private static final long serialVersionUID = -4027395051382659650L; |
| 45 |
|
| 46 |
private ResourceBundle bundle = ResourceBundle.getBundle(getClass() |
| 47 |
.getName()); |
| 48 |
@SuppressWarnings("unused") |
| 49 |
private BuildResult result = SwingJavaBuilder.build(this, bundle); |
| 50 |
|
| 51 |
private JSplitPane contents; |
| 52 |
|
| 53 |
private JComboBox cmbModTypes; |
| 54 |
private JTable tblMods; |
| 55 |
private ModTableModel model; |
| 56 |
private TableRowSorter<ModTableModel> sorter; |
| 57 |
|
| 58 |
private JLabel lblSubmitterVal; |
| 59 |
private JLabel lblCreatorVal; |
| 60 |
private JLabel lblFilesVal; |
| 61 |
private JLabel lblIdVal; |
| 62 |
private HTMLLinkLabel lblDescriptionVal; |
| 63 |
|
| 64 |
/** |
| 65 |
* Constructor of main window. |
| 66 |
*/ |
| 67 |
public MainWin() { |
| 68 |
this.setTitle(bundle.getString("frame.title") + " - v" |
| 69 |
+ bundle.getString("version")); |
| 70 |
|
| 71 |
contents.setDividerLocation(400); |
| 72 |
initTable(); |
| 73 |
initModTypeBox(); |
| 74 |
} |
| 75 |
|
| 76 |
private void initModTypeBox() { |
| 77 |
cmbModTypes.removeAllItems(); |
| 78 |
|
| 79 |
TaxonomyVocabulary tv = DepotManager.getInstance().getVocabulary( |
| 80 |
DepotConfig.getVocabularyName_ModType()); |
| 81 |
if (tv == null) |
| 82 |
return; |
| 83 |
|
| 84 |
int vid = tv.getVid(); |
| 85 |
TreeMap<String, TaxonomyTerm> terms = new TreeMap<String, TaxonomyTerm>(); |
| 86 |
terms.put(" ", new TaxonomyTerm(-1, vid, "-All-")); |
| 87 |
for (TaxonomyTerm t : DepotManager.getInstance() |
| 88 |
.getTaxonomyTermsByVocabulary(vid)) { |
| 89 |
terms.put(t.getName(), t); |
| 90 |
} |
| 91 |
for (TaxonomyTerm t : terms.values()) { |
| 92 |
cmbModTypes.addItem(t); |
| 93 |
} |
| 94 |
cmbModTypes.setSelectedIndex(0); |
| 95 |
} |
| 96 |
|
| 97 |
private void initTable() { |
| 98 |
tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 99 |
tblMods.getSelectionModel().addListSelectionListener( |
| 100 |
new ListSelectionListener() { |
| 101 |
|
| 102 |
@Override |
| 103 |
public void valueChanged(ListSelectionEvent e) { |
| 104 |
int viewRow = tblMods.getSelectedRow(); |
| 105 |
if (viewRow < 0) { |
| 106 |
modSelection(null); |
| 107 |
} else { |
| 108 |
int modelRow = tblMods |
| 109 |
.convertRowIndexToModel(viewRow); |
| 110 |
NodeMod mod = (NodeMod) model.getValueAt(modelRow, |
| 111 |
-1); |
| 112 |
modSelection(mod); |
| 113 |
} |
| 114 |
} |
| 115 |
}); |
| 116 |
|
| 117 |
model = new ModTableModel(); |
| 118 |
|
| 119 |
tblMods.setModel(model); |
| 120 |
|
| 121 |
sorter = new TableRowSorter<ModTableModel>(model); |
| 122 |
tblMods.setRowSorter(sorter); |
| 123 |
|
| 124 |
sorter.setRowFilter(new ModTableFilter(-1)); |
| 125 |
|
| 126 |
sorter.setSortable(2, false); |
| 127 |
sorter.setComparator(1, new Comparator<String>() { |
| 128 |
|
| 129 |
@Override |
| 130 |
public int compare(String o1, String o2) { |
| 131 |
int i1 = Integer.parseInt(o1); |
| 132 |
int i2 = Integer.parseInt(o2); |
| 133 |
return i1 - i2; |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>(); |
| 138 |
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); |
| 139 |
sorter.setSortKeys(sortKeys); |
| 140 |
|
| 141 |
for (int i = 0; i < model.getColumnCount(); i++) { |
| 142 |
model.setColumnConstraints(i, tblMods.getColumnModel().getColumn(i)); |
| 143 |
} |
| 144 |
|
| 145 |
// for (int i = 3; i > 0; i--) { |
| 146 |
// tblMods.getColumnModel().removeColumn(tblMods.getColumnModel().getColumn(i)); |
| 147 |
// } |
| 148 |
} |
| 149 |
|
| 150 |
@SuppressWarnings("unused") |
| 151 |
private boolean closeFrames() { |
| 152 |
System.gc(); |
| 153 |
for (Frame f : Frame.getFrames()) { |
| 154 |
if (f != this) |
| 155 |
f.dispose(); |
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
@SuppressWarnings("unused") |
| 161 |
private void exit() { |
| 162 |
setVisible(false); |
| 163 |
dispose(); |
| 164 |
} |
| 165 |
|
| 166 |
@SuppressWarnings("unused") |
| 167 |
private void saveLocalData() { |
| 168 |
Settings.getInstance().serializeToFile(); |
| 169 |
DepotManager.getInstance().saveToFile( |
| 170 |
new File(Settings.getDepotCacheFilename())); |
| 171 |
} |
| 172 |
|
| 173 |
@DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false) |
| 174 |
private void execDepotUpdate(final BackgroundEvent evt) { |
| 175 |
try { |
| 176 |
DepotManager.getInstance().updateInformation(false, |
| 177 |
new DepotCacheUpdateProgressListener() { |
| 178 |
|
| 179 |
@Override |
| 180 |
public void cacheUpdateProgress(String stepName, |
| 181 |
int current, int total) { |
| 182 |
evt.setProgressEnd(total); |
| 183 |
evt.setProgressValue(current); |
| 184 |
evt.setProgressMessage(stepName); |
| 185 |
} |
| 186 |
}); |
| 187 |
model.reloadData(); |
| 188 |
initModTypeBox(); |
| 189 |
tblMods.setVisible(true); |
| 190 |
DepotManager.getInstance().printStats(); |
| 191 |
} catch (Exception e) { |
| 192 |
e.printStackTrace(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@SuppressWarnings("unused") |
| 197 |
private void checkUpdates() { |
| 198 |
if (Settings.getInstance().get("notifyupdates", true)) { |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
@SuppressWarnings("unused") |
| 203 |
private void focus() { |
| 204 |
SwingUtilities.invokeLater(new Runnable() { |
| 205 |
|
| 206 |
@Override |
| 207 |
public void run() { |
| 208 |
toFront(); |
| 209 |
repaint(); |
| 210 |
} |
| 211 |
}); |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
@SuppressWarnings("unused") |
| 216 |
private void showSettings() { |
| 217 |
SettingsDialog.openWindow(); |
| 218 |
} |
| 219 |
|
| 220 |
private void modSelection(NodeMod n) { |
| 221 |
lblSubmitterVal.setText(""); |
| 222 |
lblCreatorVal.setText(""); |
| 223 |
lblIdVal.setText(""); |
| 224 |
lblFilesVal.setText(""); |
| 225 |
lblDescriptionVal.setText(""); |
| 226 |
if (n != null) { |
| 227 |
lblSubmitterVal.setText(n.getName()); |
| 228 |
lblCreatorVal.setText(n.getFields().get("creator")); |
| 229 |
lblIdVal.setText(Integer.toString(n.getNid())); |
| 230 |
lblFilesVal.setText(Integer.toString(n.getUploads().size())); |
| 231 |
if (n.getBody() != null) |
| 232 |
lblDescriptionVal.setText(n.getBody().getSafe_value()); |
| 233 |
} |
| 234 |
// TODO |
| 235 |
} |
| 236 |
|
| 237 |
@SuppressWarnings("unused") |
| 238 |
private void modTypeSelection() { |
| 239 |
TaxonomyTerm t = (TaxonomyTerm) cmbModTypes.getSelectedItem(); |
| 240 |
if (t != null) |
| 241 |
sorter.setRowFilter(new ModTableFilter(t.getTid())); |
| 242 |
else |
| 243 |
sorter.setRowFilter(new ModTableFilter(-1)); |
| 244 |
} |
| 245 |
} |