// public static final String showElementTreeAction = "showElementTree"; // ------------------------------------------------------------- public void openFile(String currDirStr, String currFileStr) { if (fileDialog == null) { fileDialog = new FileDialog(this); } fileDialog.setMode(FileDialog.LOAD); if (!(currDirStr.equals(""))) { fileDialog.setDirectory(currDirStr); } if (!(currFileStr.equals(""))) { fileDialog.setFile(currFileStr); } fileDialog.show(); String file = fileDialog.getFile(); // cancel pushed if (file == null) { return; } String directory = fileDialog.getDirectory(); File f = new File(directory, file); if (f.exists()) { Document oldDoc = getEditor().getDocument(); if (oldDoc != null) // oldDoc.removeUndoableEditListener(undoHandler); /* if (elementTreePanel != null) { elementTreePanel.setEditor(null); } */ getEditor().setDocument(new PlainDocument()); fileDialog.setTitle(file); Thread loader = new FileLoader(f, editor1.getDocument()); loader.start(); } }
public void loadROM() { FileDialog fileDialog = new FileDialog(this); fileDialog.setMode(FileDialog.LOAD); fileDialog.setTitle("Select a ROM to load"); // should open last folder used, and if that doesn't exist, the folder it's running in final String path = PrefsSingleton.get().get("filePath", System.getProperty("user.dir", "")); final File startDirectory = new File(path); if (startDirectory.isDirectory()) { fileDialog.setDirectory(path); } // and if the last path used doesn't exist don't set the directory at all // and hopefully the jFileChooser will open somewhere usable // on Windows it does - on Mac probably not. fileDialog.setFilenameFilter(new NESFileFilter()); boolean wasInFullScreen = false; if (inFullScreen) { wasInFullScreen = true; // load dialog won't show if we are in full screen, so this fixes for now. toggleFullScreen(); } fileDialog.setVisible(true); if (fileDialog.getFile() != null) { PrefsSingleton.get().put("filePath", fileDialog.getDirectory()); loadROM(fileDialog.getDirectory() + fileDialog.getFile()); } if (wasInFullScreen) { toggleFullScreen(); } }
protected void showFileChooser() { File p; FileDialog fDlg; String fDir, fFile; // , fPath; // int i; Component win; for (win = this; !(win instanceof Frame); ) { win = SwingUtilities.getWindowAncestor(win); if (win == null) return; } p = getPath(); switch (type & PathField.TYPE_BASICMASK) { case PathField.TYPE_INPUTFILE: fDlg = new FileDialog((Frame) win, dlgTxt, FileDialog.LOAD); break; case PathField.TYPE_OUTPUTFILE: fDlg = new FileDialog((Frame) win, dlgTxt, FileDialog.SAVE); break; case PathField.TYPE_FOLDER: fDlg = new FileDialog((Frame) win, dlgTxt, FileDialog.SAVE); // fDlg = new FolderDialog( (Frame) win, dlgTxt ); break; default: fDlg = null; assert false : (type & PathField.TYPE_BASICMASK); break; } if (p != null) { fDlg.setFile(p.getName()); fDlg.setDirectory(p.getParent()); } if (filter != null) { fDlg.setFilenameFilter(filter); } showDialog(fDlg); fDir = fDlg.getDirectory(); fFile = fDlg.getFile(); if (((type & PathField.TYPE_BASICMASK) != PathField.TYPE_FOLDER) && (fDir == null)) { fDir = ""; } if ((fFile != null) && (fDir != null)) { if ((type & PathField.TYPE_BASICMASK) == PathField.TYPE_FOLDER) { p = new File(fDir); } else { p = new File(fDir + fFile); } setPathAndDispatchEvent(p); } fDlg.dispose(); }
/** * Checks whether the given window is either a FileDialog, or contains a JFileChooser component. * If so, its current directory is stored in the given properties. * * @param aNamespace the name space to use; * @param aProperties the properties to store the found directory in; * @param aWindow the window to check for. */ private static void loadFileDialogState(final Preferences aProperties, final Window aWindow) { final String propKey = "lastDirectory"; if (aWindow instanceof FileDialog) { final String dir = aProperties.get(propKey, null); if (dir != null) { ((FileDialog) aWindow).setDirectory(dir); } } else if (aWindow instanceof JDialog) { final Container contentPane = ((JDialog) aWindow).getContentPane(); final JFileChooser fileChooser = (JFileChooser) findComponent(contentPane, JFileChooser.class); if (fileChooser != null) { final String dir = aProperties.get(propKey, null); if (dir != null) { fileChooser.setCurrentDirectory(new File(dir)); } } } }
/** * Shows a file-open selection dialog for the given working directory. * * @param aOwner the owning window to show the dialog in; * @param aCurrentDirectory the working directory to start the dialog in, can be <code>null</code> * . * @return the selected file, or <code>null</code> if the user aborted the dialog. */ public static final File showFileOpenDialog( final Window aOwner, final String aCurrentDirectory, final javax.swing.filechooser.FileFilter... aFileFilters) { if (HostUtils.isMacOS()) { final FileDialog dialog; if (aOwner instanceof Dialog) { dialog = new FileDialog((Dialog) aOwner, "Open file", FileDialog.LOAD); } else { dialog = new FileDialog((Frame) aOwner, "Open file", FileDialog.LOAD); } dialog.setDirectory(aCurrentDirectory); if ((aFileFilters != null) && (aFileFilters.length > 0)) { dialog.setFilenameFilter(new FilenameFilterAdapter(aFileFilters)); } try { dialog.setVisible(true); final String selectedFile = dialog.getFile(); return selectedFile == null ? null : new File(dialog.getDirectory(), selectedFile); } finally { dialog.dispose(); } } else { final JFileChooser dialog = new JFileChooser(); dialog.setCurrentDirectory((aCurrentDirectory == null) ? null : new File(aCurrentDirectory)); for (javax.swing.filechooser.FileFilter filter : aFileFilters) { dialog.addChoosableFileFilter(filter); } File result = null; if (dialog.showOpenDialog(aOwner) == JFileChooser.APPROVE_OPTION) { result = dialog.getSelectedFile(); } return result; } }
/** * Handles 'Save As' for a sketch. * * <p>This basically just duplicates the current sketch folder to a new location, and then calls * 'Save'. (needs to take the current state of the open files and save them to the new folder.. * but not save over the old versions for the old sketch..) * * <p>Also removes the previously-generated .class and .jar files, because they can cause trouble. */ protected boolean saveAs() throws IOException { // get new name for folder FileDialog fd = new FileDialog(editor, tr("Save sketch folder as..."), FileDialog.SAVE); if (isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath()) || isUntitled()) { // default to the sketchbook folder fd.setDirectory(BaseNoGui.getSketchbookFolder().getAbsolutePath()); } else { // default to the parent folder of where this was // on macs a .getParentFile() method is required fd.setDirectory(sketch.getFolder().getParentFile().getAbsolutePath()); } String oldName = sketch.getName(); fd.setFile(oldName); fd.setVisible(true); String newParentDir = fd.getDirectory(); String newName = fd.getFile(); // user canceled selection if (newName == null) return false; newName = SketchController.checkName(newName); File newFolder = new File(newParentDir, newName); // check if the paths are identical if (newFolder.equals(sketch.getFolder())) { // just use "save" here instead, because the user will have received a // message (from the operating system) about "do you want to replace?" return save(); } // check to see if the user is trying to save this sketch inside itself try { String newPath = newFolder.getCanonicalPath() + File.separator; String oldPath = sketch.getFolder().getCanonicalPath() + File.separator; if (newPath.indexOf(oldPath) == 0) { Base.showWarning( tr("How very Borges of you"), tr( "You cannot save the sketch into a folder\n" + "inside itself. This would go on forever."), null); return false; } } catch (IOException e) { // ignore } // if the new folder already exists, then need to remove // its contents before copying everything over // (user will have already been warned) if (newFolder.exists()) { FileUtils.recursiveDelete(newFolder); } // in fact, you can't do this on windows because the file dialog // will instead put you inside the folder, but it happens on osx a lot. try { sketch.saveAs(newFolder); } catch (IOException e) { // This does not pass on e, to prevent showing a backtrace for "normal" // errors. Base.showWarning(tr("Error"), e.getMessage(), null); } // Name changed, rebuild the sketch menus // editor.sketchbook.rebuildMenusAsync(); editor.base.rebuildSketchbookMenus(); editor.header.rebuild(); // Make sure that it's not an untitled sketch setUntitled(false); // let Editor know that the save was successful return true; }