/** Returns a file choosed by the user. */ private static File getFile(Frame frame) { File file = null; final JFileChooser fc = new JFileChooser(TMEV.getDataPath()); fc.addChoosableFileFilter(new ExpressionFileFilter()); fc.setFileView(new ExpressionFileView()); int ret = fc.showSaveDialog(frame); if (ret == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); } return file; }
/** Shows */ private static File showOpenDialog(Component parent, String title, File basedir) { JFileChooser jfc = new JFileChooser(); File fx; if (basedir != null && basedir.exists()) { fx = basedir; } else { fx = new File(ConfigFetch.getConfigFile()); fx = new File(fx.getParentFile(), "checklists"); if (!fx.exists()) { fx = new File(ConfigFetch.resolvePath(".")); if (!fx.exists()) { fx = new File("."); } } } jfc.setCurrentDirectory(fx); jfc.setAccessory(new ChecklistPreview(jfc)); jfc.setFileView(new NeptusFileView()); jfc.setFileFilter( new FileFilter() { public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = FileUtil.getFileExtension(f); if (extension != null) { if (FileUtil.FILE_TYPE_CHECKLIST.equalsIgnoreCase(extension) || FileUtil.FILE_TYPE_XML.equalsIgnoreCase(extension)) { // return ChecklistType.validate(f); return true; } else { return false; } } return false; } public String getDescription() { return I18n.text("Checklist files") + " ('" + FileUtil.FILE_TYPE_CHECKLIST + "', '" + FileUtil.FILE_TYPE_XML + "')"; } }); int result = jfc.showDialog((parent == null) ? new JFrame() : parent, title); if (result == JFileChooser.CANCEL_OPTION) return null; return jfc.getSelectedFile(); }
public File showOpenImageDialog() { JFileChooser jfc = new JFileChooser(); File fx; if (openedFile != null && openedFile.exists()) { fx = openedFile; } else { fx = new File(ConfigFetch.getConfigFile()); if (!fx.exists()) { fx = new File(ConfigFetch.resolvePath(".")); if (!fx.exists()) { fx = new File("."); } } } jfc.setCurrentDirectory(fx); // jfc.setCurrentDirectory(new File(ConfigFetch.getConfigFile())); jfc.setFileView(new NeptusFileView()); // jfc.setAccessory(new MissionPreview(jfc)); jfc.setFileFilter( new FileFilter() { public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = FileUtil.getFileExtension(f).toLowerCase(); if (extension != null) { if (extension.equals("j3d") || extension.equals("3ds") || extension.equals("x3d") || extension.equals("wrl")) { return true; } else { return false; } } return false; } public String getDescription() { return "3D model files ('j3d', '3ds', 'x3d', 'wrl')"; } }); int result = jfc.showDialog(Viewer3D.this, "Open 3D File"); if (result == JFileChooser.CANCEL_OPTION) return null; return jfc.getSelectedFile(); }
public File showSaveImageDialog() { // FIXME This is not a good idea. ConfigFetch is static!! // ConfigFetch.initialize(); JFileChooser jfc = new JFileChooser(); File fx; if (openedFile != null && openedFile.exists()) { fx = openedFile; } else { fx = new File(ConfigFetch.getConfigFile()); if (!fx.exists()) { fx = new File(ConfigFetch.resolvePath(".")); if (!fx.exists()) { fx = new File("."); } } } if (openedFile != null) { String fxS = FileUtil.replaceFileExtension(openedFile.getName(), "j3d"); jfc.setSelectedFile(new File(openedFile.getParentFile(), fxS)); } jfc.setCurrentDirectory(fx); jfc.setFileView(new NeptusFileView()); // jfc.setAccessory(new MissionPreview(jfc)); jfc.setFileFilter( new FileFilter() { public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = FileUtil.getFileExtension(f); if (extension != null) { if (extension.equals("j3d")) return true; else return false; } return false; } public String getDescription() { return "Java 3D files ('j3d')"; } }); int result = jfc.showDialog(Viewer3D.this, "Save File"); if (result == JFileChooser.CANCEL_OPTION) return null; return jfc.getSelectedFile(); }
public ImageViewerFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // set up menu bar JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("Open"); menu.add(openItem); openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { chooser.setCurrentDirectory(new File(".")); // show file chooser dialog int result = chooser.showOpenDialog(ImageViewerFrame.this); // if image file accepted, set it as icon of the label if (result == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); pack(); } } }); JMenuItem exitItem = new JMenuItem("Exit"); menu.add(exitItem); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); // use a label to display the images label = new JLabel(); add(label); // set up file chooser chooser = new JFileChooser(); // accept all image files ending with .jpg, .jpeg, .gif /* final ExtensionFileFilter filter = new ExtensionFileFilter(); filter.addExtension("jpg"); filter.addExtension("jpeg"); filter.addExtension("gif"); filter.setDescription("Image files"); */ FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "jpeg", "gif"); chooser.setFileFilter(filter); chooser.setAccessory(new ImagePreviewer(chooser)); chooser.setFileView(new FileIconView(filter, new ImageIcon("palette.gif"))); }