1 21 22 25 package net.sf.launch4j.formimpl; 26 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.util.jar.Attributes ; 32 import java.util.jar.JarFile ; 33 34 import javax.swing.DefaultListModel ; 35 import javax.swing.JFileChooser ; 36 import javax.swing.JTextField ; 37 import javax.swing.ListSelectionModel ; 38 import javax.swing.event.ChangeEvent ; 39 import javax.swing.event.ChangeListener ; 40 import javax.swing.event.ListSelectionEvent ; 41 import javax.swing.event.ListSelectionListener ; 42 43 import net.sf.launch4j.FileChooserFilter; 44 import net.sf.launch4j.binding.Bindings; 45 import net.sf.launch4j.binding.Validator; 46 import net.sf.launch4j.config.ClassPath; 47 import net.sf.launch4j.form.ClassPathForm; 48 49 52 public class ClassPathFormImpl extends ClassPathForm { 53 private final JFileChooser _fileChooser; 54 private final FileChooserFilter _filter 55 = new FileChooserFilter("Executable jar", ".jar"); 56 57 public ClassPathFormImpl(Bindings bindings, JFileChooser fc) { 58 bindings.addOptComponent("classPath", ClassPath.class, _classpathCheck) 59 .add("classPath.mainClass", _mainclassField) 60 .add("classPath.paths", _classpathList); 61 _fileChooser = fc; 62 63 ClasspathCheckListener cpl = new ClasspathCheckListener(); 64 _classpathCheck.addChangeListener(cpl); 65 cpl.stateChanged(null); 66 67 _classpathList.setModel(new DefaultListModel ()); 68 _classpathList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 69 _classpathList.addListSelectionListener(new ClasspathSelectionListener()); 70 71 _newClasspathButton.addActionListener(new NewClasspathListener()); 72 _acceptClasspathButton.addActionListener( 73 new AcceptClasspathListener(_classpathField)); 74 _removeClasspathButton.addActionListener(new RemoveClasspathListener()); 75 _importClasspathButton.addActionListener(new ImportClasspathListener()); 76 _classpathUpButton.addActionListener(new MoveUpListener()); 77 _classpathDownButton.addActionListener(new MoveDownListener()); 78 } 79 80 private class ClasspathCheckListener implements ChangeListener { 81 public void stateChanged(ChangeEvent e) { 82 boolean on = _classpathCheck.isSelected(); 83 _importClasspathButton.setEnabled(on); 84 _classpathUpButton.setEnabled(on); 85 _classpathDownButton.setEnabled(on); 86 _classpathField.setEnabled(on); 87 _newClasspathButton.setEnabled(on); 88 _acceptClasspathButton.setEnabled(on); 89 _removeClasspathButton.setEnabled(on); 90 } 91 } 92 93 private class NewClasspathListener implements ActionListener { 94 public void actionPerformed(ActionEvent e) { 95 _classpathList.clearSelection(); 96 _classpathField.setText(""); 97 _classpathField.requestFocusInWindow(); 98 } 99 } 100 101 private class AcceptClasspathListener extends AbstractAcceptListener { 102 public AcceptClasspathListener(JTextField f) { 103 super(f, true); 104 } 105 106 public void actionPerformed(ActionEvent e) { 107 String cp = getText(); 108 if (Validator.isEmpty(cp)) { 109 signalViolation(Messages.getString("specifyClassPath")); 110 return; 111 } 112 DefaultListModel model = (DefaultListModel ) _classpathList.getModel(); 113 if (_classpathList.isSelectionEmpty()) { 114 model.addElement(cp); 115 clear(); 116 } else { 117 model.setElementAt(cp, _classpathList.getSelectedIndex()); 118 } 119 } 120 } 121 122 private class ClasspathSelectionListener implements ListSelectionListener { 123 public void valueChanged(ListSelectionEvent e) { 124 if (e.getValueIsAdjusting()) { 125 return; 126 } 127 if (_classpathList.isSelectionEmpty()) { 128 _classpathField.setText(""); 129 } else { 130 _classpathField.setText((String ) _classpathList.getSelectedValue()); 131 } 132 _classpathField.requestFocusInWindow(); 133 } 134 } 135 136 private class RemoveClasspathListener implements ActionListener { 137 public void actionPerformed(ActionEvent e) { 138 if (_classpathList.isSelectionEmpty() 139 || !MainFrame.getInstance().confirm( 140 Messages.getString("confirmClassPathRemoval"))) { 141 return; 142 } 143 DefaultListModel model = (DefaultListModel ) _classpathList.getModel(); 144 while (!_classpathList.isSelectionEmpty()) { 145 model.remove(_classpathList.getSelectedIndex()); 146 } 147 } 148 } 149 150 private class MoveUpListener implements ActionListener { 151 public void actionPerformed(ActionEvent e) { 152 int x = _classpathList.getSelectedIndex(); 153 if (x < 1) { 154 return; 155 } 156 DefaultListModel model = (DefaultListModel ) _classpathList.getModel(); 157 Object o = model.get(x - 1); 158 model.set(x - 1, model.get(x)); 159 model.set(x, o); 160 _classpathList.setSelectedIndex(x - 1); 161 } 162 } 163 164 private class MoveDownListener implements ActionListener { 165 public void actionPerformed(ActionEvent e) { 166 DefaultListModel model = (DefaultListModel ) _classpathList.getModel(); 167 int x = _classpathList.getSelectedIndex(); 168 if (x == -1 || x >= model.getSize() - 1) { 169 return; 170 } 171 Object o = model.get(x + 1); 172 model.set(x + 1, model.get(x)); 173 model.set(x, o); 174 _classpathList.setSelectedIndex(x + 1); 175 } 176 } 177 178 private class ImportClasspathListener implements ActionListener { 179 public void actionPerformed(ActionEvent e) { 180 try { 181 _fileChooser.setFileFilter(_filter); 182 _fileChooser.setSelectedFile(new File ("")); 183 if (_fileChooser.showOpenDialog(MainFrame.getInstance()) 184 == JFileChooser.APPROVE_OPTION) { 185 JarFile jar = new JarFile (_fileChooser.getSelectedFile()); 186 if (jar.getManifest() == null) { 187 jar.close(); 188 MainFrame.getInstance().info(Messages.getString("noManifest")); 189 return; 190 } 191 Attributes attr = jar.getManifest().getMainAttributes(); 192 String mainClass = (String ) attr.getValue("Main-Class"); 193 String classPath = (String ) attr.getValue("Class-Path"); 194 jar.close(); 195 _mainclassField.setText(mainClass != null ? mainClass : ""); 196 DefaultListModel model = new DefaultListModel (); 197 if (classPath != null) { 198 String [] paths = classPath.split(" "); 199 for (int i = 0; i < paths.length; i++) { 200 model.addElement(paths[i]); 201 } 202 } 203 _classpathList.setModel(model); 204 } 205 } catch (IOException ex) { 206 MainFrame.getInstance().warn(ex.getMessage()); 207 } 208 } 209 } 210 } 211 | Popular Tags |