Java Code Examples for javax.swing.JFileChooser.getCurrentDirectory()

The following are Jave code examples for showing how to use getCurrentDirectory() of the javax.swing.JFileChooser class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: ComparePanel.java   Source Code and License Vote up 6 votes
private void btnNewFileBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewFileBrowseActionPerformed
    JFileChooser fc = new JFileChooser(wd);
    FileFilter filter = new FileFilter() {
        public boolean accept(File f) {
            return f.getName().endsWith(".xml");
        }

        public String getDescription() {
            return "XML files";
        }

    };
    fc.setFileFilter(filter);
    int result = fc.showOpenDialog(this);
    if(result == JFileChooser.APPROVE_OPTION) {
        txtNewFile.setText (fc.getSelectedFile().getPath());
        wd = fc.getCurrentDirectory();
    }
}
 
Example 2
Project: rapidminer   File: FileChooserUI.java   Source Code and License Vote up 6 votes
private void doDirectoryChanged(PropertyChangeEvent e) {
	JFileChooser fc = getFileChooser();
	FileSystemView fsv = fc.getFileSystemView();

	clearIconCache();

	File currentDirectory = fc.getCurrentDirectory();
	this.fileList.updatePath(currentDirectory);

	if (currentDirectory != null) {
		this.directoryComboBoxModel.addItem(currentDirectory);
		getNewFolderAction().setEnabled(currentDirectory.canWrite());
		getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory));
		getChangeToParentDirectoryAction().setEnabled(!fsv.isRoot(currentDirectory));
		getGoHomeAction().setEnabled(!userHomeDirectory.equals(currentDirectory));

		if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
			if (fsv.isFileSystem(currentDirectory)) {
				setFileName(currentDirectory.getPath());
			} else {
				setFileName(null);
			}
			setFileSelected();
		}
	}
}
 
Example 3
Project: cuttlefish   File: NetworkInitializer.java   Source Code and License Vote up 6 votes
public void initCxfNetwork(CxfNetwork cxfNetwork) {
	 JFileChooser fc = new FileChooser();		 		 
	 fc.setDialogTitle("Select a CXF file");
	 fc.setFileFilter(new FileNameExtensionFilter(".cxf files", "cxf"));
	 int returnVal = fc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
       	 ch.ethz.sg.cuttlefish.gui2.Cuttlefish.currentDirectory = fc.getCurrentDirectory();
       	 System.out.println("Current directory: " + fc.getCurrentDirectory());
            File file = fc.getSelectedFile();             
            cxfNetwork.load(file);
            cxfNetwork.setNetworkLoaded(true);
        } else {
            System.out.println("Input cancelled by user");
        }
}
 
Example 4
Project: incubator-netbeans   File: SpringCustomizerPanel.java   Source Code and License Vote up 5 votes
private void addFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFileButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(true);
        chooser.setDialogTitle(NbBundle.getMessage(SpringCustomizerPanel.class, "LBL_ChooseFile")); //NOI18N
        chooser.setCurrentDirectory(basedir);
        int option = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(groupFilesList));
        if (option == JFileChooser.APPROVE_OPTION) {
            boolean showDialog = false;
            List<File> newFiles = new LinkedList<File>();
            StringBuilder existing = new StringBuilder(
                    NbBundle.getMessage(SpringCustomizerPanel.class, "LBL_FileAlreadyAdded")).append("\n"); //NOI18N
            for (File file : chooser.getSelectedFiles()) {
                if (files.contains(file)) {
                    existing.append(file.getAbsolutePath()).append("\n"); //NOI18N
                    showDialog = true;
                } else {
                    newFiles.add(file);
                }
            }

            // remember last location
            basedir = chooser.getCurrentDirectory();
            addFiles(newFiles);
            if (showDialog) {
                DialogDisplayer.getDefault().notify(
                        new NotifyDescriptor.Message(existing.toString(), NotifyDescriptor.ERROR_MESSAGE));
            }
        }
}
 
Example 5
Project: incubator-netbeans   File: TextImporterUI.java   Source Code and License Vote up 5 votes
private File selectIconFile() {
    JFileChooser dlg = new JFileChooser( defaultFolder );
    dlg.setAcceptAllFileFilterUsed( true );
    dlg.setMultiSelectionEnabled( false );
    if( dlg.showOpenDialog(this) != JFileChooser.APPROVE_OPTION )
        return null;
    defaultFolder = dlg.getCurrentDirectory();
    return dlg.getSelectedFile();
}
 
Example 6
Project: incubator-netbeans   File: JAXBBindingInfoPnl.java   Source Code and License Vote up 5 votes
protected static String selectFileFromFileSystem(JPanel panel,
        File projDir,
        String type){
    File file = null;
    String ret = null;
    File lastBrowsed = getLastBrowsedDir(type);
    JFileChooser jfc = new JFileChooser();
    if (lastBrowsed != null){
        jfc.setCurrentDirectory(lastBrowsed);
    }

    jfc.setMultiSelectionEnabled(false);
    jfc.setFileSelectionMode( JFileChooser.FILES_ONLY  );
    int iRt = jfc.showOpenDialog(panel);
    if ( iRt == JFileChooser.APPROVE_OPTION ) {
        file = jfc.getSelectedFile();
        file = FileUtil.normalizeFile(file);
    }
    File currDir = jfc.getCurrentDirectory();
    if (currDir != null){
        setLastBrowsedDir(type, currDir);
    }
    
    if (file != null){
        String absPath = file.getAbsolutePath();
        ret = FileSysUtil.Absolute2RelativePathStr(projDir, 
                file.getAbsoluteFile());
        if (ret == null){
            //This can happen in Windows where Project and Files are in 
            // different drives.
            ret = absPath;
        }
    }        

    return ret;
}
 
Example 7
Project: Ultraino   File: FileUtils.java   Source Code and License Vote up 5 votes
public static String selectNonExistingFile(Component parent,String extensionWanted){
       String forReturn = null;
       final String endWith = extensionWanted;

       JFileChooser chooser = new JFileChooser(lastChooserPath);
       chooser.setFileFilter(new FileFilter(){
           @Override
			public boolean accept(File file) {
				String filename = file.getName();
				return (filename.endsWith(endWith)||file.isDirectory());
			}
           @Override
			public String getDescription() {
				return endWith;
			}
		});
int result = chooser.showSaveDialog(parent);
       if ( result == JFileChooser.APPROVE_OPTION){
            try{
               lastChooserPath = chooser.getCurrentDirectory();
               forReturn = chooser.getSelectedFile().getCanonicalPath();
           }catch(Exception e){e.printStackTrace();}
       }
       if(forReturn != null){
           if(!forReturn.endsWith(extensionWanted)){
               forReturn += extensionWanted;
           }
       }
       return forReturn;
   }
 
Example 8
Project: cuttlefish   File: NetworkInitializer.java   Source Code and License Vote up 5 votes
private InteractiveCxfNetwork initInteractiveCxfNetwork()
		throws FileNotFoundException {
	InteractiveCxfNetwork interactiveCxfNetwork = null;

	JFileChooser fc = new FileChooser();
	fc.setDialogTitle("Select a CXF file");
	fc.setFileFilter(new FileNameExtensionFilter(".cxf files", "cxf"));
	int returnVal = fc.showOpenDialog(null);

	if (returnVal == JFileChooser.APPROVE_OPTION) {
		ch.ethz.sg.cuttlefish.gui.Cuttlefish.currentDirectory = fc
				.getCurrentDirectory();
		System.out
				.println("Current directory: " + fc.getCurrentDirectory());
		networkFile = fc.getSelectedFile();
		interactiveCxfNetwork = new InteractiveCxfNetwork();
		interactiveCxfNetwork.load(networkFile);
		interactiveCxfNetwork.setNetworkLoaded(true);
	} else {
		System.out.println("Input cancelled by user");
		return null;
	}

	fc = new FileChooser();
	fc.setDialogTitle("Select a CEF file");
	fc.setFileFilter(new FileNameExtensionFilter(".cef files", "cef"));
	returnVal = fc.showOpenDialog(null);

	if (returnVal == JFileChooser.APPROVE_OPTION) {
		networkFile = fc.getSelectedFile();
		interactiveCxfNetwork.loadInstructions(networkFile);
	} else {
		System.out.println("Input cancelled by user");
	}

	return interactiveCxfNetwork;
}
 
Example 9
Project: Ultraino   File: FileUtils.java   Source Code and License Vote up 5 votes
public static String selectFile(Component component, String name, String extension, File pathToUse){
       JFileChooser chooser = new JFileChooser();
       if(extension != null){        
           chooser.setFileFilter(new ExtensionFilter(extension));
       }
       
       boolean useGivenPath = pathToUse != null;
       if (useGivenPath && lastIndicatedPath == null){
           lastIndicatedPath = pathToUse;
       }
       chooser.setCurrentDirectory(useGivenPath?lastIndicatedPath:lastChooserPath);
int result = chooser.showDialog(component, name);
       if ( result == JFileChooser.APPROVE_OPTION){
           try{
               if(useGivenPath){
                   lastIndicatedPath = chooser.getCurrentDirectory();
               }else{
                   lastChooserPath = chooser.getCurrentDirectory();
               }
               String p = chooser.getSelectedFile().getAbsolutePath();
               if (extension.startsWith(".")){
                   if (p.endsWith(extension)){
                       return p;
                   }else{
                       return p + extension;
                   }
               }else{
                   if (p.endsWith(extension)){
                       return p;
                   }else{
                       return p + "." + extension;
                   }
               }
              
           }catch(Exception e){e.printStackTrace();}
       }

       return null;
   }
 
Example 10
Project: rapidminer   File: FileChooserUI.java   Source Code and License Vote up 5 votes
@Override
public void actionPerformed(ActionEvent e) {
	if (UIManager.getBoolean("FileChooser.readOnly")) {
		return;
	}
	JFileChooser fc = getFileChooser();
	File currentDirectory = fc.getCurrentDirectory();
	FileSystemView fsv = fc.getFileSystemView();
	File newFolder = null;

	String name = SwingTools.showInputDialog("file_chooser.new_folder", "");

	try {
		if (name != null && !"".equals(name)) {
			newFolder = fsv.createNewFolder(currentDirectory);
			if (newFolder.renameTo(fsv.createFileObject(fsv.getParentDirectory(newFolder), name))) {
				newFolder = fsv.createFileObject(fsv.getParentDirectory(newFolder), name);
			} else {
				SwingTools.showVerySimpleErrorMessage("file_chooser.new_folder.rename", name);
			}
		}
	} catch (IOException exc) {
		SwingTools.showVerySimpleErrorMessage("file_chooser.new_folder.create", name);
		return;
	} catch (Exception exp) {
		// do nothing
	}

	if (fc.isMultiSelectionEnabled()) {
		fc.setSelectedFiles(new File[] { newFolder });
	} else {
		fc.setSelectedFile(newFolder);
	}

	fc.rescanCurrentDirectory();
}
 
Example 11
Project: rapidminer   File: FileChooserUI.java   Source Code and License Vote up 5 votes
private void doFileSelectionModeChanged(PropertyChangeEvent e) {
	doFilterChanged(e);

	JFileChooser fc = getFileChooser();
	File currentDirectory = fc.getCurrentDirectory();
	if (currentDirectory != null && fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()
			&& fc.getFileSystemView().isFileSystem(currentDirectory)) {
		setFileName(currentDirectory.getPath());
	} else {
		setFileName(null);
	}
	setFileSelected();
}
 
Example 12
Project: OpenDA   File: PlotFrame.java   Source Code and License Vote up 5 votes
/** Query the user for a filename and save the plot to that file.
 */
protected void _saveAs() {

    JFileChooser fileDialog = new JFileChooser();
    fileDialog.addChoosableFileFilter(new PLTOrXMLFileFilter());
    fileDialog.setDialogTitle("Save plot as...");
    if (_directory != null) {
        fileDialog.setCurrentDirectory(_directory);
    } else {
        // The default on Windows is to open at user.home, which is
        // typically an absurd directory inside the O/S installation.
        // So we use the current directory instead.
        String cwd = StringUtilities.getProperty("user.dir");
        if (cwd != null) {
            fileDialog.setCurrentDirectory(new File(cwd));
        }
    }

    fileDialog.setSelectedFile(new File(fileDialog.getCurrentDirectory(),
            "plot.xml"));

    int returnVal = fileDialog.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        _file = fileDialog.getSelectedFile();
        setTitle(_file.getName());
        _directory = fileDialog.getCurrentDirectory();
        _save();
    }
}
 
Example 13
Project: geomapapp   File: PersTool.java   Source Code and License Vote up 5 votes
void save() {
	if( pi==null||pi.getImage()==null )return;
	JFileChooser chooser = haxby.map.MapApp.getFileChooser();
	String name = "3Dimage.jpg";
	File dir = chooser.getCurrentDirectory();
	chooser.setSelectedFile(new File(dir,name));
	File file = null;
	while( true ) {
		int ok = chooser.showSaveDialog(pi);
		if( ok==chooser.CANCEL_OPTION)return;
		file = chooser.getSelectedFile();
		if( file.exists() ) {
			ok=JOptionPane.showConfirmDialog(
					pi,
					"File exists, overwrite?");
			if( ok==JOptionPane.CANCEL_OPTION)return;
			if( ok==JOptionPane.YES_OPTION)break;
		} else {
			break;
		}
	}
	try {
		int sIndex = file.getName().lastIndexOf(".");
		String suffix = sIndex<0
			? "jpg"
			: file.getName().substring( sIndex+1 );
		if( !ImageIO.getImageWritersBySuffix(suffix).hasNext())suffix = "jpg";
		ImageIO.write( pi.getImage(), suffix, file);
	} catch(Exception ex) {
		
	}
}
 
Example 14
Project: cuttlefish   File: NetworkInitializer.java   Source Code and License Vote up 5 votes
public void initCxfNetwork(CxfNetwork cxfNetwork) {
	 JFileChooser fc = new FileChooser();		 		 
	 fc.setDialogTitle("Select a CXF file");
	 fc.setFileFilter(new FileNameExtensionFilter(".cxf files", "cxf"));
	 int returnVal = fc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
       	 ch.ethz.sg.cuttlefish.gui2.Cuttlefish.currentDirectory = fc.getCurrentDirectory();
       	 System.out.println("Current directory: " + fc.getCurrentDirectory());
            File file = fc.getSelectedFile();             
            cxfNetwork.load(file);
        } else {
            System.out.println("Input cancelled by user");
        }
}
 
Example 15
Project: Luyten4Forge   File: FileDialog.java   Source Code and License Vote up 5 votes
private void saveSaveDialogDir(JFileChooser fc) {
	try {
		File currentDir = fc.getCurrentDirectory();
		if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
			luytenPrefs.setFileSaveCurrentDirectory(currentDir.getAbsolutePath());
		}
	} catch (Exception e) {
		Luyten.showExceptionDialog("Exception!", e);
	}
}
 
Example 16
Project: incubator-netbeans   File: J2SEVolumeCustomizer.java   Source Code and License Vote up 4 votes
private void addResource(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addResource
    // TODO add your handling code here:
    JFileChooser chooser = new JFileChooser();
    FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
    chooser.setAcceptAllFileFilterUsed(false);
    if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_CLASSPATH)) {
        chooser.setMultiSelectionEnabled (true);
        chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenClasses"));
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter (new SimpleFileFilter(NbBundle.getMessage(
                J2SEVolumeCustomizer.class,"TXT_Classpath"),new String[] {"ZIP","JAR"}));   //NOI18N
        chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectCP"));
        chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectCP").charAt(0));
    }
    else if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_JAVADOC)) {
        chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenJavadoc"));
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter (new SimpleFileFilter(NbBundle.getMessage(
                J2SEVolumeCustomizer.class,"TXT_Javadoc"),new String[] {"ZIP","JAR"}));     //NOI18N
        chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectJD"));
        chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectJD").charAt(0));
    }
    else if (this.volumeType.equals(PersistenceLibrarySupport.VOLUME_TYPE_SRC)) {
        chooser.setDialogTitle(NbBundle.getMessage(J2SEVolumeCustomizer.class,"TXT_OpenSources"));
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter (new SimpleFileFilter(NbBundle.getMessage(
                J2SEVolumeCustomizer.class,"TXT_Sources"),new String[] {"ZIP","JAR"}));     //NOI18N
        chooser.setApproveButtonText(NbBundle.getMessage(J2SEVolumeCustomizer.class,"CTL_SelectSRC"));
        chooser.setApproveButtonMnemonic(NbBundle.getMessage(J2SEVolumeCustomizer.class,"MNE_SelectSRC").charAt(0));
    }
    if (lastFolder != null) {
        chooser.setCurrentDirectory (lastFolder);
    }
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try {
            lastFolder = chooser.getCurrentDirectory();
            if (chooser.isMultiSelectionEnabled()) {
                addFiles (chooser.getSelectedFiles());
            }
            else {
                final File selectedFile = chooser.getSelectedFile();                    
                addFiles (new File[] {selectedFile});
            }
        } catch (MalformedURLException mue) {
            Exceptions.printStackTrace(mue);
        }
    }
}
 
Example 17
Project: incubator-netbeans   File: FileArrayEditor.java   Source Code and License Vote up 4 votes
/** Property change listaner attached to the JFileChooser chooser. */
public void propertyChange(PropertyChangeEvent e) {
    // Fix for IZ#36742 - FileArrayEditor fires each selection change twice
    if ( ANCESTOR.equals( e.getPropertyName()) ){
        myPropertyFired.set( null );
    }
    
    if (e.getSource() instanceof JFileChooser) {
        JFileChooser jfc = (JFileChooser) e.getSource();
        if (mode == jfc.DIRECTORIES_ONLY && jfc.DIRECTORY_CHANGED_PROPERTY.equals(e.getPropertyName())) {
            if (jfc.getSelectedFile() == null) {
                File dir = jfc.getCurrentDirectory();
                if (dir != null) {
                    setValue (new File[] {new File(dir.getAbsolutePath())});
                    return;
                }
            }
        }
    }
    
    if (( ! JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(e.getPropertyName())) && 
        ( ! JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName()))) {
            return;
    }
    if (! (e.getSource() instanceof JFileChooser)) {
        return;
    }
    JFileChooser chooser = (JFileChooser)e.getSource();
    File[] f = (File[])chooser.getSelectedFiles();
    if (f == null) {
        return;
    }
    
    // Fix for IZ#36742 - FileArrayEditor fires each selection change twice
    if ( isAlreadyHandled ( chooser , f , e.getPropertyName())){
        return;
    }
    
    if ((f.length == 0) && (chooser.getSelectedFile() != null)) {
        f = new File[] { chooser.getSelectedFile() };
    }
    
    for (int i = 0; i < f.length; i++) {
        if (!files && f[i].isFile ()) return;
        if (!directories && f[i].isDirectory ()) return;
    }

    if (baseDirectory != null) {
        for (int i = 0; i < f.length; i++) {
            String rel = FileEditor.getChildRelativePath(baseDirectory, f[i]);
            if (rel != null) {
                f[i] = new File(rel);
            }
        }
    }
    
    File[] nf = new File[f.length];
    for (int i = 0; i < f.length; i++) {
        // the next line is
        // workaround for JDK bug 4533419
        // it should be returned back to f[i] after the
        // mentioned bug is fixed in JDK.
        nf[i] = new File(f[i].getAbsolutePath());
    }
    setValue(nf);
    
    FileEditor.lastCurrentDir = chooser.getCurrentDirectory();
}
 
Example 18
Project: incubator-netbeans   File: ResultsManager.java   Source Code and License Vote up 4 votes
SelectedFile selectSnapshotTargetFile(final String defaultName, final boolean heapdump) {
    String targetName;
    FileObject targetDir;

    // 1. let the user choose file or directory
    JFileChooser chooser = new JFileChooser();

    if (exportDir != null) {
        chooser.setCurrentDirectory(exportDir);
    }

    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setMultiSelectionEnabled(false);
    chooser.setDialogTitle(Bundle.ResultsManager_SelectFileOrDirDialogCaption());
    chooser.setApproveButtonText(Bundle.ResultsManager_SaveButtonName());
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().endsWith("." + (heapdump ? HEAPDUMP_EXTENSION : SNAPSHOT_EXTENSION)); //NOI18N
            }

            public String getDescription() {
                if (heapdump) {
                    return Bundle.ResultsManager_ProfilerHeapdumpFileFilter(HEAPDUMP_EXTENSION);
                }
                return Bundle.ResultsManager_ProfilerSnapshotFileFilter(SNAPSHOT_EXTENSION);
            }
        });

    if (chooser.showSaveDialog(WindowManager.getDefault().getMainWindow()) != JFileChooser.APPROVE_OPTION) {
        return null; // cancelled by the user
    }

    // 2. process both cases and extract file name and extension to use
    File file = chooser.getSelectedFile();
    String targetExt = heapdump ? HEAPDUMP_EXTENSION : SNAPSHOT_EXTENSION;

    if (file.isDirectory()) { // save to selected directory under default name
        exportDir = chooser.getCurrentDirectory();
        targetDir = FileUtil.toFileObject(FileUtil.normalizeFile(file));
        targetName = defaultName;
    } else { // save to selected file
        exportDir = chooser.getCurrentDirectory();

        targetDir = FileUtil.toFileObject(FileUtil.normalizeFile(exportDir));

        String fName = file.getName();

        // divide the file name into name and extension
        int idx = fName.lastIndexOf('.'); // NOI18N

        if (idx == -1) { // no extension
            targetName = fName;

            // extension will be used from source file
        } else { // extension exists
            if (heapdump || fName.endsWith("." + targetExt)) { // NOI18N
                targetName = fName.substring(0, idx);
                targetExt = fName.substring(idx + 1);
            } else {
                targetName = fName;
            }
        }
    }

    // 3. return a newly created FileObject
    return new SelectedFile(targetDir, targetName, targetExt);
}
 
Example 19
Project: incubator-netbeans   File: NbClassPathCustomEditor.java   Source Code and License Vote up 4 votes
private void addJarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJarButtonActionPerformed

        JFileChooser chooser = FileEditor.createHackedFileChooser();
        chooser.setFileHidingEnabled(false);
        setHelpToChooser( chooser );
        
        chooser.setFileFilter(new FileFilter() {
                                  public boolean accept(File f) {
                                      return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); // NOI18N
                                  }
                                  public String getDescription() {
                                      return getString("CTL_JarArchivesMask");
                                  }
                              });

        if (lastJarFolder != null) {
            chooser.setCurrentDirectory(lastJarFolder);
        }

        chooser.setDialogTitle(getString("CTL_FileSystemPanel.Jar_Dialog_Title"));
        chooser.setMultiSelectionEnabled( true );
        if (chooser.showDialog(this,
                               getString("CTL_Approve_Button_Title"))
                == JFileChooser.APPROVE_OPTION) {
            File[] files = chooser.getSelectedFiles();
            boolean found = false;
            for (int i=0; i<files.length; i++) {
                if ((files[i] != null) && (files[i].isFile())) {
                    found = true;
                    String path = files[i].getAbsolutePath();
                    if (! listModel.contains (path)) {
                        listModel.addElement (path);
                    }
                }
            }
            if ( found ) {
                lastJarFolder = chooser.getCurrentDirectory();
                fireValueChanged();
            }
            pathList.setSelectedIndex(listModel.size() - 1);
        }
  }
 
Example 20
Project: incubator-netbeans   File: OpenFileAction.java   Source Code and License Vote up 4 votes
/**
 * {@inheritDoc} Displays a file chooser dialog
 * and opens the selected files.
 */
@Override
public void actionPerformed(ActionEvent e) {
    if (running) {
        return;
    }
    try {
        running = true;
        JFileChooser chooser = prepareFileChooser();
        File[] files;
        try {
            if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N
                String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                System.setProperty("apple.awt.fileDialogForDirectories", "false"); //NOI18N
                FileDialog fileDialog = new FileDialog(WindowManager.getDefault().getMainWindow());
                fileDialog.setMode(FileDialog.LOAD);
                fileDialog.setDirectory(getCurrentDirectory().getAbsolutePath());
                fileDialog.setTitle(chooser.getDialogTitle());
                fileDialog.setVisible(true);
                if( null != oldFileDialogProp ) {
                    System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
                } else {
                    System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                }

                if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
                    String selFile = fileDialog.getFile();
                    File dir = new File( fileDialog.getDirectory() );
                    files = new File[] { new File( dir, selFile ) };
                    currentDirectory = dir;
                } else {
                    throw new UserCancelException();
                }
            } else {
                files = chooseFilesToOpen(chooser);
                currentDirectory = chooser.getCurrentDirectory();
                if (chooser.getFileFilter() != null) { // #227187
                    currentFileFilter =
                            chooser.getFileFilter().getDescription();
                }
            }
        } catch (UserCancelException ex) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            OpenFile.openFile(files[i], -1);
        }
    } finally {
        running = false;
    }
}