1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.awt.Font ; 23 import java.awt.KeyboardFocusManager ; 24 import java.awt.Window ; 25 import java.io.File ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 31 import javax.swing.JFileChooser ; 32 import javax.swing.DefaultListModel ; 33 import javax.swing.DefaultListSelectionModel ; 34 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 import org.openide.util.NbBundle; 38 import org.openide.util.Utilities; 39 import org.openide.windows.WindowManager; 40 41 class SelectModulesPanel extends javax.swing.JPanel { 42 43 private static final javax.swing.filechooser.FileFilter NBM_FILE_FILTER = new NbmFileFilter(); 44 45 46 private DefaultListModel modulesModel; 47 48 49 static File defaultDir = null; 50 51 private Wizard.Validator validator; 52 53 static final long serialVersionUID = 8279789280479374665L; 54 55 56 public SelectModulesPanel(Wizard.Validator validator) { 57 putClientProperty("WizardPanel_contentSelectedIndex", new Integer (1)); setName(getBundle("LAB_Select")); 59 initComponents(); 60 61 this.validator = validator; 62 63 getAccessibleContext().setAccessibleName(getBundle("LAB_Select")); 64 getAccessibleContext().setAccessibleDescription(getBundle("LAB_21") + getBundle("LAB_22")); 65 jTextArea.getAccessibleContext().setAccessibleDescription(getBundle("ACS_21")); 66 addButton.getAccessibleContext().setAccessibleDescription(getBundle("ACS_Add")); 67 removeButton.getAccessibleContext().setAccessibleDescription(getBundle("ACS_Remove")); 68 jList.getAccessibleContext().setAccessibleDescription(getBundle("ACS_InstallList")); 69 70 reset(); 71 } 72 73 74 public java.awt.Dimension getPreferredSize() { 75 return new java.awt.Dimension ( 350, super.getPreferredSize().height ); 76 } 77 78 83 private void initComponents() { 85 java.awt.GridBagConstraints gridBagConstraints; 86 87 jTextArea = new javax.swing.JTextArea (); 88 jScrollPane1 = new javax.swing.JScrollPane (); 89 jList = new javax.swing.JList (); 90 addButton = new javax.swing.JButton (); 91 removeButton = new javax.swing.JButton (); 92 nextLabel = new javax.swing.JLabel (); 93 listLabel = new javax.swing.JLabel (); 94 95 setLayout(new java.awt.GridBagLayout ()); 96 97 setNextFocusableComponent(addButton); 98 jTextArea.setBackground(nextLabel.getBackground()); 99 jTextArea.setFont(new Font ("Dialog", Font.PLAIN, jTextArea.getFont().getSize())); 100 jTextArea.setLineWrap(true); 101 jTextArea.setText(getBundle("LAB_21")); 102 jTextArea.setWrapStyleWord(true); 103 jTextArea.setDisabledTextColor(java.awt.Color.black); 104 jTextArea.setEnabled(false); 105 gridBagConstraints = new java.awt.GridBagConstraints (); 106 gridBagConstraints.gridx = 0; 107 gridBagConstraints.gridy = 0; 108 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 109 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 110 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 111 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 112 add(jTextArea, gridBagConstraints); 113 114 jList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 115 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 116 jListValueChanged(evt); 117 } 118 }); 119 120 jScrollPane1.setViewportView(jList); 121 122 gridBagConstraints = new java.awt.GridBagConstraints (); 123 gridBagConstraints.gridx = 0; 124 gridBagConstraints.gridy = 2; 125 gridBagConstraints.gridheight = 2; 126 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 127 gridBagConstraints.weightx = 1.0; 128 gridBagConstraints.weighty = 1.0; 129 add(jScrollPane1, gridBagConstraints); 130 131 org.openide.awt.Mnemonics.setLocalizedText(addButton, getBundle("BTN_Add")); 132 addButton.addActionListener(new java.awt.event.ActionListener () { 133 public void actionPerformed(java.awt.event.ActionEvent evt) { 134 addButtonActionPerformed(evt); 135 } 136 }); 137 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.gridx = 1; 140 gridBagConstraints.gridy = 2; 141 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 142 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 143 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 144 add(addButton, gridBagConstraints); 145 146 org.openide.awt.Mnemonics.setLocalizedText(removeButton, getBundle("BTN_Remove")); 147 removeButton.addActionListener(new java.awt.event.ActionListener () { 148 public void actionPerformed(java.awt.event.ActionEvent evt) { 149 removeButtonActionPerformed(evt); 150 } 151 }); 152 153 gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 1; 155 gridBagConstraints.gridy = 3; 156 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 158 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 159 add(removeButton, gridBagConstraints); 160 161 nextLabel.setLabelFor(this); 162 org.openide.awt.Mnemonics.setLocalizedText(nextLabel, getBundle("LAB_22")); 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridx = 0; 165 gridBagConstraints.gridy = 4; 166 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 167 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 168 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 169 add(nextLabel, gridBagConstraints); 170 171 listLabel.setLabelFor(jList); 172 org.openide.awt.Mnemonics.setLocalizedText(listLabel, getBundle("LAB_ModulesList")); 173 gridBagConstraints = new java.awt.GridBagConstraints (); 174 gridBagConstraints.gridx = 0; 175 gridBagConstraints.gridy = 1; 176 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 177 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 178 add(listLabel, gridBagConstraints); 179 180 } 181 183 private void jListValueChanged(javax.swing.event.ListSelectionEvent evt) { buttonsCheck(); 185 } 187 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { Object [] sel = jList.getSelectedValues(); 189 for (int i=0;i<sel.length;i++) 190 modulesModel.removeElement(sel[i]); 191 jList.setSelectionModel(new DefaultListSelectionModel ()); 194 195 if ( modulesModel.getSize() == 0 ) 196 validator.setValid( false ); 197 buttonsCheck(); 198 } 200 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { List files = selectNbmFiles(); 202 203 if ( files != null ) { 204 Iterator it = files.iterator(); 205 for ( int i = 0; it.hasNext(); i++ ) { 206 if ( i == 0 ) 207 validator.setValid( true ); 208 209 File fn = (File )it.next(); 210 if ( !selectedFile( fn ) ) { 211 modulesModel.addElement( fn ); 212 } 213 } 214 } 215 216 buttonsCheck(); 217 } 219 private javax.swing.JButton addButton; 221 private javax.swing.JList jList; 222 private javax.swing.JScrollPane jScrollPane1; 223 private javax.swing.JTextArea jTextArea; 224 private javax.swing.JLabel listLabel; 225 private javax.swing.JLabel nextLabel; 226 private javax.swing.JButton removeButton; 227 229 private boolean selectedFile( File file ) { 230 for ( int i = 0; i < modulesModel.size(); i++ ) { 231 if ( ((File )modulesModel.elementAt(i)).compareTo( file ) == 0 ) 232 return true; 233 } 234 235 return false; 236 } 237 238 File [] getFiles() { 239 File [] files = new File [ modulesModel.size() ]; 240 241 for ( int i = 0; i < modulesModel.size(); i ++ ) { 242 files[i] = (File )modulesModel.elementAt( i ); 243 } 244 245 return files; 246 } 247 248 251 void reset() { 252 if ( modulesModel == null ) { 253 modulesModel = new DefaultListModel (); 254 jList.setModel( modulesModel ); 255 } 256 else { 257 modulesModel.removeAllElements(); 258 } 259 buttonsCheck(); 260 } 261 262 private void buttonsCheck() { 263 if ( modulesModel.getSize() > 0 && jList.getSelectedIndex() != -1 ) 264 removeButton.setEnabled(true); 265 else 266 removeButton.setEnabled(false); 267 } 268 269 private static class NbmFileFilter extends javax.swing.filechooser.FileFilter { 270 public boolean accept( File f ) { 271 return f.isDirectory() || f.getName().toLowerCase().endsWith( ".nbm" ); } 273 274 public String getDescription() { 275 return getBundle( "CTL_FileFilterDescription" ); 276 } 277 } 278 279 static File getDefaultDir() { 280 File defDir = new File ( System.getProperty("user.home") ); try { 282 if ( Utilities.isUnix() ) 283 return defDir; 284 else if ( Utilities.isWindows() ) { 285 do { 286 defDir = defDir.getParentFile(); 287 } while ( defDir != null && defDir.getParentFile() != null ); 288 } 289 } 290 catch (Exception ex) { 291 defDir = null; 292 } 293 return defDir; 294 } 295 296 static List selectNbmFiles() { 297 JFileChooser chooser = new JFileChooser (); 298 chooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); 299 chooser.addChoosableFileFilter( NBM_FILE_FILTER ); 300 chooser.setFileFilter(NBM_FILE_FILTER); 301 chooser.setMultiSelectionEnabled( true ); 302 chooser.setFileHidingEnabled( false ); 303 chooser.setDialogTitle(getBundle("CTL_FileChooser_Title")); 304 305 if (defaultDir == null) 306 defaultDir = getDefaultDir(); 307 308 if ( defaultDir != null) { 309 chooser.setCurrentDirectory( defaultDir ); 310 } 311 312 List files = null; 313 314 Window focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow (); 315 316 if (chooser.showDialog (KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow (), 317 getBundle("CTL_FileChooser_Approve_Button")) 318 != JFileChooser.APPROVE_OPTION) 319 return null; 320 321 defaultDir = chooser.getCurrentDirectory(); 322 323 if (focusOwner != null) { 325 focusOwner.toFront (); 326 } 327 328 return Arrays.asList(chooser.getSelectedFiles()); 329 } 330 331 private static String getBundle( String key ) { 332 return NbBundle.getMessage( SelectModulesPanel.class, key ); 333 } 334 } 335 | Popular Tags |