Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package modmanager; import modmanager.swing.ModificationListRenderer; import java.awt.Desktop; import java.awt.dnd.DropTarget; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import java.util.prefs.Preferences; import javax.imageio.ImageIO; import javax.swing.DefaultListModel; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; import javax.swing.filechooser.FileNameExtensionFilter; import modmanager.backend.Modification; import modmanager.backend.ModificationListener; import modmanager.backend.ModificationOption; import org.apache.commons.io.FileUtils; /** * * @author ruman */ public class MainWindow extends javax.swing.JFrame implements ModificationListener { private ModificationManager modifications; /** * Creates new form MainWindow */ public MainWindow() { modifications = new ModificationManager(); initComponents(); /** * Set icon of this app */ try { this.setIconImage(ImageIO.read(MainWindow.class.getResourceAsStream("/resources/Icon.png"))); } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } registerAsyncActions(); modifications.initialize(); /** * Enforce selection of skyrim directory */ if (modifications.getSkyrimDirectory() == null) { selectSkyrimDirectory(true); } modificationList.setModel(modifications.getModificationModel()); modifications.addModificationListener(this); /** * Make drag and drop */ registerDnD(); /** * make searchable list */ registerSearch(); } private void registerSearch() { searchField.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent ke) { } @Override public void keyPressed(KeyEvent ke) { } @Override public void keyReleased(KeyEvent ke) { String sub = searchField.getText(); for (Modification mod : modifications.getModifications()) { if (mod.getName().toLowerCase().contains(sub.toLowerCase())) { modificationList.setSelectedValue(mod, true); if (ke.getKeyCode() == KeyEvent.VK_ENTER) { modificationList.requestFocus(); searchField.setText(""); } break; } } } }); } private void registerDnD() { ModInstallerDragAndDropListener lis = new ModInstallerDragAndDropListener(modifications); DropTarget trg = new DropTarget(this, lis); } private void registerAsyncActions() { final MainWindow wnd = this; modifications.addAsyncListener(new ModificationManagerAsyncListener() { @Override public void asyncBegin() { wnd.setEnabled(false); progressPanel.setVisible(true); } @Override public void asyncProgress(int progress) { progressProgressBar.setValue(progress); } @Override public void asyncStatus(String status) { progressStatusLabel.setText(status); } @Override public void asyncEnd() { wnd.setEnabled(true); progressPanel.setVisible(false); } }); } private void selectSkyrimDirectory(boolean force) { skyrimDirectoryChooser.setDialogTitle("Select Skyrim folder"); skyrimDirectoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); boolean chosen = false; while (skyrimDirectoryChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { /** * Use a while loop to check for valid skyrim installation ... */ if (!FileUtils.getFile(skyrimDirectoryChooser.getSelectedFile(), "TESV.exe").exists()) { int result = JOptionPane.showConfirmDialog(this, "It seems that this directory does not contain Skyrim!\nContinue anyways?", "Invalid Skyrim directory", JOptionPane.YES_NO_CANCEL_OPTION); if (result == JOptionPane.CANCEL_OPTION) { break; } if (result == JOptionPane.YES_OPTION) { chosen = true; break; } } else { chosen = true; break; } } if (force && !chosen) { System.exit(0); } modifications.setSkyrimDirectory(skyrimDirectoryChooser.getSelectedFile()); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { skyrimDirectoryChooser = new javax.swing.JFileChooser(); openModificationChooser = new javax.swing.JFileChooser(); modificationDirectoryChooser = new javax.swing.JFileChooser(); jToolBar1 = new javax.swing.JToolBar(); filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0)); searchField = new modmanager.swing.SelfExplainingTextField(); jLayeredPane1 = new javax.swing.JLayeredPane(); progressPanel = new javax.swing.JPanel(); progressProgressBar = new javax.swing.JProgressBar(); progressStatusLabel = new javax.swing.JLabel(); mainPanel = new javax.swing.JPanel(); jSplitPane1 = new javax.swing.JSplitPane(); modificationListScrollPane = new javax.swing.JScrollPane(); modificationList = new javax.swing.JList(); modificationPanel = new modmanager.swing.ModificationPanel(modifications); menuBar = new javax.swing.JMenuBar(); modificationMenu = new javax.swing.JMenu(); addModificationMenuItem = new javax.swing.JMenuItem(); reloadModificationsMenuItem = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JPopupMenu.Separator(); changeModDirMenuItem = new javax.swing.JMenuItem(); gameMenu = new javax.swing.JMenu(); setGameDirectoryMenuItem = new javax.swing.JMenuItem(); openGameDirectoryMenuItem = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("JSkyrim Mod Manager " + Version.CURRENT_VERSION); setPreferredSize(new java.awt.Dimension(800, 600)); addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); jToolBar1.setRollover(true); jToolBar1.add(filler1); searchField.setExplanation("Search ..."); searchField .setExplanationColor(javax.swing.UIManager.getDefaults().getColor("TextArea.inactiveForeground")); searchField.setExplanationFont(new java.awt.Font("DejaVu Sans", 2, 13)); // NOI18N searchField.setMinimumSize(new java.awt.Dimension(200, 25)); searchField.setPreferredSize(new java.awt.Dimension(200, 25)); jToolBar1.add(searchField); getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH); progressPanel.setBorder(javax.swing.BorderFactory .createLineBorder(javax.swing.UIManager.getDefaults().getColor("EditorPane.inactiveForeground"))); progressStatusLabel.setText("Working ..."); progressStatusLabel.setToolTipText(""); progressStatusLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP); javax.swing.GroupLayout progressPanelLayout = new javax.swing.GroupLayout(progressPanel); progressPanel.setLayout(progressPanelLayout); progressPanelLayout.setHorizontalGroup( progressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, progressPanelLayout.createSequentialGroup().addContainerGap() .addGroup(progressPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(progressStatusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(progressProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)) .addContainerGap())); progressPanelLayout.setVerticalGroup(progressPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, progressPanelLayout.createSequentialGroup() .addContainerGap() .addComponent(progressStatusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(progressProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); mainPanel.setLayout(new java.awt.BorderLayout()); jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); jSplitPane1.setResizeWeight(0.6); jSplitPane1.setContinuousLayout(true); modificationList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); modificationList.setCellRenderer(new ModificationListRenderer()); modificationList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { modificationListValueChanged(evt); } }); modificationListScrollPane.setViewportView(modificationList); jSplitPane1.setTopComponent(modificationListScrollPane); jSplitPane1.setBottomComponent(modificationPanel); mainPanel.add(jSplitPane1, java.awt.BorderLayout.CENTER); javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1); jLayeredPane1.setLayout(jLayeredPane1Layout); jLayeredPane1Layout.setHorizontalGroup( jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE) .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(progressPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jLayeredPane1Layout .setVerticalGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE) .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jLayeredPane1Layout.createSequentialGroup().addGap(0, 509, Short.MAX_VALUE) .addComponent(progressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))); jLayeredPane1.setLayer(progressPanel, javax.swing.JLayeredPane.MODAL_LAYER); jLayeredPane1.setLayer(mainPanel, javax.swing.JLayeredPane.DEFAULT_LAYER); getContentPane().add(jLayeredPane1, java.awt.BorderLayout.CENTER); modificationMenu.setText("Modification"); addModificationMenuItem.setText("Add modification ..."); addModificationMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addModificationMenuItemActionPerformed(evt); } }); modificationMenu.add(addModificationMenuItem); reloadModificationsMenuItem.setText("Reload modifications"); reloadModificationsMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { reloadModificationsMenuItemActionPerformed(evt); } }); modificationMenu.add(reloadModificationsMenuItem); modificationMenu.add(jSeparator1); changeModDirMenuItem.setText("Set modification directory ..."); changeModDirMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { changeModDirMenuItemActionPerformed(evt); } }); modificationMenu.add(changeModDirMenuItem); menuBar.add(modificationMenu); gameMenu.setText("Game"); setGameDirectoryMenuItem.setText("Set Skyrim directory ..."); setGameDirectoryMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { setGameDirectoryMenuItemActionPerformed(evt); } }); gameMenu.add(setGameDirectoryMenuItem); openGameDirectoryMenuItem.setText("Open Skyrim directory ..."); openGameDirectoryMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { openGameDirectoryMenuItemActionPerformed(evt); } }); gameMenu.add(openGameDirectoryMenuItem); menuBar.add(gameMenu); setJMenuBar(menuBar); pack(); }// </editor-fold>//GEN-END:initComponents private void modificationListValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_modificationListValueChanged {//GEN-HEADEREND:event_modificationListValueChanged if (modificationList.getSelectedIndex() == -1) { modificationPanel.setCurrentModification(null); } else { modificationPanel.setCurrentModification( modifications.getModificationModel().elementAt(modificationList.getSelectedIndex())); } }//GEN-LAST:event_modificationListValueChanged private void setGameDirectoryMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_setGameDirectoryMenuItemActionPerformed {//GEN-HEADEREND:event_setGameDirectoryMenuItemActionPerformed selectSkyrimDirectory(false); }//GEN-LAST:event_setGameDirectoryMenuItemActionPerformed private void openGameDirectoryMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_openGameDirectoryMenuItemActionPerformed {//GEN-HEADEREND:event_openGameDirectoryMenuItemActionPerformed if (Desktop.isDesktopSupported()) { try { Desktop.getDesktop().open(modifications.getSkyrimDirectory()); } catch (IOException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_openGameDirectoryMenuItemActionPerformed private void addModificationMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addModificationMenuItemActionPerformed {//GEN-HEADEREND:event_addModificationMenuItemActionPerformed openModificationChooser.setFileFilter( new FileNameExtensionFilter("Modification archives (*.zip,*.rar,*.7z)", "zip", "rar", "7z")); if (openModificationChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { modifications.addModification(openModificationChooser.getSelectedFile()); } }//GEN-LAST:event_addModificationMenuItemActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing {//GEN-HEADEREND:event_formWindowClosing Preferences node = Preferences.userRoot().node("jskyrimmodmanager/window"); node.putInt("x", this.getX()); node.putInt("y", this.getY()); node.putInt("width", this.getWidth()); node.putInt("height", this.getHeight()); node.putInt("state", this.getExtendedState()); }//GEN-LAST:event_formWindowClosing private void formWindowOpened(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowOpened {//GEN-HEADEREND:event_formWindowOpened Preferences node = Preferences.userRoot().node("jskyrimmodmanager/window"); this.setLocation(node.getInt("x", 0), node.getInt("y", 0)); this.setSize(node.getInt("width", 800), node.getInt("height", 600)); this.setExtendedState(node.getInt("state", JFrame.MAXIMIZED_BOTH)); }//GEN-LAST:event_formWindowOpened private void reloadModificationsMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_reloadModificationsMenuItemActionPerformed {//GEN-HEADEREND:event_reloadModificationsMenuItemActionPerformed modifications.reloadModifications(); }//GEN-LAST:event_reloadModificationsMenuItemActionPerformed private void changeModDirMenuItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_changeModDirMenuItemActionPerformed {//GEN-HEADEREND:event_changeModDirMenuItemActionPerformed modificationDirectoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); modificationDirectoryChooser.setDialogTitle("Choose modification directory"); if (modificationDirectoryChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { modifications.setModificationDirectory(modificationDirectoryChooser.getSelectedFile()); } }//GEN-LAST:event_changeModDirMenuItemActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem addModificationMenuItem; private javax.swing.JMenuItem changeModDirMenuItem; private javax.swing.Box.Filler filler1; private javax.swing.JMenu gameMenu; private javax.swing.JLayeredPane jLayeredPane1; private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JToolBar jToolBar1; private javax.swing.JPanel mainPanel; private javax.swing.JMenuBar menuBar; private javax.swing.JFileChooser modificationDirectoryChooser; private javax.swing.JList modificationList; private javax.swing.JScrollPane modificationListScrollPane; private javax.swing.JMenu modificationMenu; private modmanager.swing.ModificationPanel modificationPanel; private javax.swing.JMenuItem openGameDirectoryMenuItem; private javax.swing.JFileChooser openModificationChooser; private javax.swing.JPanel progressPanel; private javax.swing.JProgressBar progressProgressBar; private javax.swing.JLabel progressStatusLabel; private javax.swing.JMenuItem reloadModificationsMenuItem; private modmanager.swing.SelfExplainingTextField searchField; private javax.swing.JMenuItem setGameDirectoryMenuItem; private javax.swing.JFileChooser skyrimDirectoryChooser; // End of variables declaration//GEN-END:variables @Override public void modificationInstalled(Modification source, ModificationOption[] options) { modificationList.repaint(); modificationPanel.updateModificationData(); } @Override public void modificationUninstalled(Modification source) { modificationList.repaint(); modificationPanel.updateModificationData(); } }