Ejemplo n.º 1
0
  /**
   * 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.
   */
  public boolean saveAs() throws IOException {
    // get new name for folder
    FileDialog fd = new FileDialog(editor, "Save file as...", FileDialog.SAVE);
    if (isReadOnly()) {
      // default to the sketchbook folder
      fd.setDirectory(Base.preferences.get("sketchbook.path", null));
    } else {
      // default to the parent folder of where this was
      fd.setDirectory(folder.getParent());
    }
    fd.setFile(folder.getName());

    fd.setVisible(true);
    String newParentDir = fd.getDirectory();
    String newName = fd.getFile();

    File newFolder = new File(newParentDir);
    // user cancelled selection
    if (newName == null) return false;

    if (!newName.endsWith(".gcode")) newName = newName + ".gcode";

    // grab the contents of the current tab before saving
    // first get the contents of the editor text area
    if (current.modified) {
      current.program = editor.getText();
    }

    // save the other tabs to their new location
    for (int i = 1; i < codeCount; i++) {
      File newFile = new File(newFolder, code[i].file.getName());
      code[i].saveAs(newFile);
    }

    // save the hidden code to its new location
    for (int i = 0; i < hiddenCount; i++) {
      File newFile = new File(newFolder, hidden[i].file.getName());
      hidden[i].saveAs(newFile);
    }

    // save the main tab with its new name
    File newFile = new File(newFolder, newName);
    code[0].saveAs(newFile);

    editor.handleOpenUnchecked(
        newFile.getPath(),
        currentIndex,
        editor.textarea.getSelectionStart(),
        editor.textarea.getSelectionEnd(),
        editor.textarea.getScrollPosition());

    // Name changed, rebuild the sketch menus
    // editor.sketchbook.rebuildMenusAsync();

    // let MainWindow know that the save was successful
    return true;
  }
Ejemplo n.º 2
0
 void openEffect() {
   FileDialog dialog = new FileDialog(editor, "Open Effect", FileDialog.LOAD);
   if (lastDir != null) dialog.setDirectory(lastDir);
   dialog.setVisible(true);
   final String file = dialog.getFile();
   final String dir = dialog.getDirectory();
   if (dir == null || file == null || file.trim().length() == 0) return;
   lastDir = dir;
   ParticleEffect effect = new ParticleEffect();
   try {
     effect.loadEmitters(Gdx.files.absolute(new File(dir, file).getAbsolutePath()));
     editor.effect = effect;
     emitterTableModel.getDataVector().removeAllElements();
     editor.particleData.clear();
   } catch (Exception ex) {
     System.out.println("Error loading effect: " + new File(dir, file).getAbsolutePath());
     ex.printStackTrace();
     JOptionPane.showMessageDialog(editor, "Error opening effect.");
     return;
   }
   for (ParticleEmitter emitter : effect.getEmitters()) {
     emitter.setPosition(
         editor.worldCamera.viewportWidth / 2, editor.worldCamera.viewportHeight / 2);
     emitterTableModel.addRow(new Object[] {emitter.getName(), true});
   }
   editIndex = 0;
   emitterTable.getSelectionModel().setSelectionInterval(editIndex, editIndex);
   editor.reloadRows();
 }
Ejemplo n.º 3
0
  private void getFluffImage() {
    // copied from structureTab
    FileDialog fDialog = new FileDialog(getParentFrame(), "Image Path", FileDialog.LOAD);
    fDialog.setDirectory(
        new File(ImageHelper.fluffPath).getAbsolutePath()
            + File.separatorChar
            + ImageHelper.imageMech
            + File.separatorChar);
    fDialog.setLocationRelativeTo(this);

    fDialog.setVisible(true);

    if (fDialog.getFile() != null) {
      String relativeFilePath =
          new File(fDialog.getDirectory() + fDialog.getFile()).getAbsolutePath();
      relativeFilePath =
          "."
              + File.separatorChar
              + relativeFilePath.substring(
                  new File(System.getProperty("user.dir").toString()).getAbsolutePath().length()
                      + 1);
      getAero().getFluff().setMMLImagePath(relativeFilePath);
    }
    refresh.refreshPreview();
    return;
  }
Ejemplo n.º 4
0
 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();
   }
 }
Ejemplo n.º 5
0
  //    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();
    }
  }
Ejemplo n.º 6
0
 public File showDirOpenDialog(Component parent, File defaultDir) {
   if (Platform.isMacOS()) {
     System.setProperty("apple.awt.fileDialogForDirectories", "true");
     FileDialog dialog = null;
     if (parent instanceof JFrame == false && parent instanceof JDialog == false) {
       parent = ((JComponent) parent).getTopLevelAncestor();
     }
     if (parent instanceof JFrame) dialog = new FileDialog((JFrame) parent);
     else dialog = new FileDialog((JDialog) parent);
     if (defaultDir != null) dialog.setDirectory(defaultDir.getAbsolutePath());
     dialog.setVisible(true);
     if (dialog.getDirectory() == null || dialog.getFile() == null) return null;
     File file = new File(dialog.getDirectory(), dialog.getFile());
     System.setProperty("apple.awt.fileDialogForDirectories", "false");
     return file;
   } else {
     JFileChooser chooser = new JFileChooser();
     if (defaultDir != null) chooser.setCurrentDirectory(defaultDir);
     chooser.setDialogType(JFileChooser.OPEN_DIALOG);
     chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     chooser.showOpenDialog(parent);
     File file = chooser.getSelectedFile();
     return file;
   }
 }
Ejemplo n.º 7
0
  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();
  }
Ejemplo n.º 8
0
 public File showFileSaveDialog(Component parent, File defaultFile) {
   FileDialog fileDialog = new FileDialog(getProjectExplorer());
   if (defaultFile != null) {
     fileDialog.setDirectory(defaultFile.getParent());
     fileDialog.setFile(defaultFile.getName());
   }
   fileDialog.setMode(FileDialog.SAVE);
   fileDialog.setVisible(true);
   String filename = fileDialog.getFile();
   return filename != null ? new File(fileDialog.getDirectory(), filename) : null;
 }
Ejemplo n.º 9
0
 /** Opens the load MIB dialog. */
 protected void loadMib() {
   FileDialog dialog = new FileDialog(this, "Select MIB File");
   dialog.setDirectory(currentDir.getAbsolutePath());
   dialog.setVisible(true);
   String file = dialog.getFile();
   if (file != null) {
     File[] files = new File[] {new File(dialog.getDirectory(), file)};
     currentDir = files[0].getParentFile();
     descriptionArea.setText("");
     new Loader(files).start();
   }
 }
Ejemplo n.º 10
0
 public File showFileOpenDialog(
     Component parent, File defaultDir, File defaultFile, FilenameFilter filter) {
   FileDialog fileDialog = new FileDialog(getProjectExplorer());
   if (defaultFile != null) {
     if (defaultDir != null) fileDialog.setDirectory(defaultDir.getPath());
     fileDialog.setFile(defaultFile.getName());
   }
   if (filter != null) fileDialog.setFilenameFilter(filter);
   fileDialog.setMode(FileDialog.LOAD);
   fileDialog.setVisible(true);
   String filename = fileDialog.getFile();
   return filename != null ? new File(fileDialog.getDirectory(), filename) : null;
 }
Ejemplo n.º 11
0
 public PathwayChooser(
     String taskName, int dialogType, Preference dirPreference, Set<? extends PathwayIO> set) {
   fileDialog = new FileDialog(new Frame(), taskName + " pathway", dialogType);
   fileDialog.setDirectory(
       PreferenceManager.getCurrent().getFile(dirPreference).getAbsolutePath());
   jfc = new JFileChooser();
   this.taskName = taskName;
   this.dirPreference = dirPreference;
   createFileFilters(set);
   jfc.setDialogTitle(taskName + " pathway");
   jfc.setDialogType(dialogType);
   jfc.setCurrentDirectory(PreferenceManager.getCurrent().getFile(dirPreference));
 }
Ejemplo n.º 12
0
  /**
   * Handles 'Save As' for a build.
   *
   * <p>This basically just duplicates the current build 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.
   */
  public boolean saveAs() throws IOException {
    // get new name for folder
    FileDialog fd = new FileDialog(new Frame(), "Save file as...", FileDialog.SAVE);
    // default to the folder that this file is in
    fd.setDirectory(folder.getCanonicalPath());
    fd.setFile(mainFilename);

    fd.setVisible(true);
    String parentDir = fd.getDirectory();
    String newName = fd.getFile();
    // user cancelled selection
    if (newName == null) return false;

    File folder = new File(parentDir);

    // Find base name
    if (newName.toLowerCase().endsWith(".gcode"))
      newName = newName.substring(0, newName.length() - 6);
    if (newName.toLowerCase().endsWith(".ngc"))
      newName = newName.substring(0, newName.length() - 4);
    if (newName.toLowerCase().endsWith(".stl"))
      newName = newName.substring(0, newName.length() - 4);
    if (newName.toLowerCase().endsWith(".obj"))
      newName = newName.substring(0, newName.length() - 4);
    if (newName.toLowerCase().endsWith(".dae"))
      newName = newName.substring(0, newName.length() - 4);

    BuildCode code = getCode();
    if (code != null) {
      // grab the contents of the current tab before saving
      // first get the contents of the editor text area
      if (hasMainWindow) {
        if (code.isModified()) {
          code.program = editor.getText();
        }
      }
      File newFile = new File(folder, newName + ".gcode");
      code.saveAs(newFile);
    }

    BuildModel model = getModel();
    if (model != null) {
      File newFile = new File(folder, newName + ".stl");
      model.saveAs(newFile);
    }

    this.name = newName;
    this.mainFilename = fd.getFile();
    this.folder = folder;
    return true;
  }
Ejemplo n.º 13
0
  /**
   * Opens a dialog asking the user to choose a file which is is used for saving to.
   *
   * @param parent the parent component the dialog is centered on
   * @param titleKey the key for the locale-specific string to use for the file dialog title
   * @param suggestedFile the suggested file for saving
   * @param the filter to use for what's shown.
   * @return the file or <code>null</code> when the user cancelled the dialog
   */
  public static File getSaveAsFile(
      Component parent, String titleKey, File suggestedFile, final FileFilter filter) {
    if (OSUtils.isAnyMac()) {
      FileDialog dialog =
          new FileDialog(GUIMediator.getAppFrame(), I18n.tr(titleKey), FileDialog.SAVE);
      dialog.setDirectory(suggestedFile.getParent());
      dialog.setFile(suggestedFile.getName());
      if (filter != null) {
        FilenameFilter f =
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return filter.accept(new File(dir, name));
              }
            };
        dialog.setFilenameFilter(f);
      }

      dialog.setVisible(true);
      String dir = dialog.getDirectory();
      setLastInputDirectory(new File(dir));
      String file = dialog.getFile();
      if (dir != null && file != null) {

        if (suggestedFile != null) {
          String suggestedFileExtension = FileUtils.getFileExtension(suggestedFile);

          String newFileExtension = FileUtils.getFileExtension(file);

          if (newFileExtension == null && suggestedFileExtension != null) {
            file = file + "." + suggestedFileExtension;
          }
        }

        File f = new File(dir, file);
        if (filter != null && !filter.accept(f)) return null;
        else return f;
      } else {
        return null;
      }
    } else {
      JFileChooser chooser =
          getDirectoryChooser(titleKey, null, null, JFileChooser.FILES_ONLY, filter);
      chooser.setSelectedFile(suggestedFile);
      int ret = chooser.showSaveDialog(parent);
      File file = chooser.getSelectedFile();
      setLastInputDirectory(file);
      return ret != JFileChooser.APPROVE_OPTION ? null : file;
    }
  }
Ejemplo n.º 14
0
 // click open
 private void FileOpen() {
   // create Open File dialog
   //		JFileChooser GraphFileDialog = new JFileChooser(FilePath);
   FileDialog dialog = new FileDialog(this, "File Open", FileDialog.LOAD);
   dialog.setDirectory(FilePath);
   dialog.setFile("*.graph");
   dialog.setVisible(true);
   String path = dialog.getDirectory();
   String file = dialog.getFile();
   if (file != null) {
     File graphFile = new File(path + file);
     // System.out.println(GraphFileDialog.getName());
     // get paramter from *.graph
     loadGraphFile(graphFile);
     // store latest directory
     FilePath = graphFile.getParent();
   }
 }
Ejemplo n.º 15
0
 void saveEffect() {
   FileDialog dialog = new FileDialog(editor, "Save Effect", FileDialog.SAVE);
   if (lastDir != null) dialog.setDirectory(lastDir);
   dialog.setVisible(true);
   String file = dialog.getFile();
   String dir = dialog.getDirectory();
   if (dir == null || file == null || file.trim().length() == 0) return;
   lastDir = dir;
   int index = 0;
   for (ParticleEmitter emitter : editor.effect.getEmitters())
     emitter.setName((String) emitterTableModel.getValueAt(index++, 0));
   try {
     editor.effect.save(new File(dir, file));
   } catch (Exception ex) {
     System.out.println("Error saving effect: " + new File(dir, file).getAbsolutePath());
     ex.printStackTrace();
     JOptionPane.showMessageDialog(editor, "Error saving effect.");
   }
 }
Ejemplo n.º 16
0
  public static File getSaveAsDir(
      Component parent, String titleKey, File suggestedFile, final FileFilter filter) {
    if (OSUtils.isAnyMac()) {
      FileDialog dialog =
          new FileDialog(GUIMediator.getAppFrame(), I18n.tr(titleKey), FileDialog.SAVE);
      dialog.setDirectory(suggestedFile.getParent());
      dialog.setFile(suggestedFile.getName());

      if (filter != null) {
        FilenameFilter f =
            new FilenameFilter() {
              public boolean accept(File dir, String name) {
                return filter.accept(new File(dir, name));
              }
            };
        dialog.setFilenameFilter(f);
      }

      dialog.setVisible(true);
      String dir = dialog.getDirectory();
      setLastInputDirectory(new File(dir));

      System.setProperty("apple.awt.fileDialogForDirectories", "false");
      if (dir != null) {
        File f = new File(dir);
        if (filter != null && !filter.accept(f)) {
          return null;
        } else {
          return f;
        }
      } else {
        return null;
      }
    } else {
      JFileChooser chooser =
          getDirectoryChooser(titleKey, null, null, JFileChooser.DIRECTORIES_ONLY, filter);
      chooser.setSelectedFile(suggestedFile);
      int ret = chooser.showSaveDialog(parent);
      File file = chooser.getSelectedFile();
      setLastInputDirectory(file);
      return ret != JFileChooser.APPROVE_OPTION ? null : file;
    }
  }
Ejemplo n.º 17
0
        /**
        * get the filename from the user using a file requester
        */
        private String getFileName(int mode) {
                String prompt;
                String filename;
                String pathname;
                File   file;

                // depending on load or save set the prompt                
                if (mode == FileDialog.LOAD) {
                        prompt = "Open File ";
                } else {
                        prompt = "Save File As ";
                }

                // create a file requester
                FileDialog d = new FileDialog(getJPE().getEditor(), prompt, mode);

                // set the file filter (not working on epoc it seems)
                d.setFilenameFilter(new JPEFilenameFilter());

                // set the directory to open in (not working?)
                //d.setDirectory("c:\\JPE");
                d.setDirectory(dir);

                // show requester (blocks until user is done)
                d.show();

                // optain the filename
                filename = d.getFile();

                // complete the file name by adding its path 
                pathname = d.getDirectory() + filename; 

                if (filename != null) {
                        dir = d.getDirectory();
                        getJPE().putProperty("setup","currentdir",dir);
                        // if we have a filename return it
                        return(pathname);
                } else {
                        // return null to signal a problem
                        return(null);
                }
        }
Ejemplo n.º 18
0
  /**
   * 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));
        }
      }
    }
  }
Ejemplo n.º 19
0
  protected boolean browseFile() {
    File currentFile = new File(fnameField.getText());

    FileDialog fd = new FileDialog(this, "Save next session as...", FileDialog.SAVE);
    fd.setDirectory(currentFile.getParent());
    fd.setVisible(true);
    if (fd.getFile() != null) {
      String newDir = fd.getDirectory();
      String sep = System.getProperty("file.separator");
      if (newDir.length() > 0) {
        if (!sep.equals(newDir.substring(newDir.length() - sep.length()))) newDir += sep;
      }
      String newFname = newDir + fd.getFile();
      if (newFname.equals(fnameField.getText())) {
        fnameField.setText(newFname);
        return true;
      }
    }
    return false;
  }
Ejemplo n.º 20
0
 private void FileSave() {
   // create File dialog for save
   FileDialog dialog = new FileDialog(this, "File Open", FileDialog.SAVE);
   dialog.setDirectory(FilePath);
   dialog.setFile("*.graph");
   dialog.setVisible(true);
   String file = dialog.getDirectory() + dialog.getFile();
   int point = file.lastIndexOf(".");
   if (point > 0) {
     file = file.substring(0, point) + ".graph";
   } else {
     file = file + ".graph";
   }
   if (file != null) {
     File graphFile = new File(file);
     // write parameter
     writeGraphFile(graphFile);
     // store latest directory
     FilePath = graphFile.getParent();
   }
 }
Ejemplo n.º 21
0
  /**
   * 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;
    }
  }
Ejemplo n.º 22
0
  private void getFluffImage() {
    // copied from structureTab
    FileDialog fDialog = new FileDialog(getParentFrame(), "Image Path", FileDialog.LOAD);
    fDialog.setDirectory(
        new File(ImageHelper.fluffPath).getAbsolutePath()
            + File.separatorChar
            + ImageHelper.imageMech
            + File.separatorChar);
    /*
     //This does not seem to be working
    if (getMech().getFluff().getMMLImagePath().trim().length() > 0) {
        String fullPath = new File(getMech().getFluff().getMMLImagePath()).getAbsolutePath();
        String imageName = fullPath.substring(fullPath.lastIndexOf(File.separatorChar) + 1);
        fullPath = fullPath.substring(0, fullPath.lastIndexOf(File.separatorChar) + 1);
        fDialog.setDirectory(fullPath);
        fDialog.setFile(imageName);
    } else {
        fDialog.setDirectory(new File(ImageHelper.fluffPath).getAbsolutePath() + File.separatorChar + ImageHelper.imageMech + File.separatorChar);
        fDialog.setFile(getMech().getChassis() + " " + getMech().getModel() + ".png");
    }
    */
    fDialog.setLocationRelativeTo(this);

    fDialog.setVisible(true);

    if (fDialog.getFile() != null) {
      String relativeFilePath =
          new File(fDialog.getDirectory() + fDialog.getFile()).getAbsolutePath();
      relativeFilePath =
          "."
              + File.separatorChar
              + relativeFilePath.substring(
                  new File(System.getProperty("user.dir").toString()).getAbsolutePath().length()
                      + 1);
      getMech().getFluff().setMMLImagePath(relativeFilePath);
    }
    refresh.refreshPreview();
    return;
  }
Ejemplo n.º 23
0
  /**
   * 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;
  }
Ejemplo n.º 24
0
  public static File show(
      Window parent,
      String title,
      File startFile,
      boolean selectDirectories,
      boolean load,
      final String fileDescription,
      final String[] fileSuffixes,
      boolean suggestFileName) {
    File selectedFile = null;
    GeneralFileFilter filter = new GeneralFileFilter(fileDescription, fileSuffixes);

    if (JavaUtils.isMac()) {
      if (selectDirectories) {
        // For Mac we only select directories, unfortunately!
        System.setProperty("apple.awt.fileDialogForDirectories", "true");
      }
      int mode = load ? FileDialog.LOAD : FileDialog.SAVE;
      FileDialog fd;
      if (parent instanceof Dialog) {
        fd = new FileDialog((Dialog) parent, title, mode);
      } else if (parent instanceof Frame) {
        fd = new FileDialog((Frame) parent, title, mode);
      } else {
        fd = new FileDialog((Dialog) null, title, mode);
      }
      if (startFile != null) {
        if (startFile.isDirectory()) {
          fd.setDirectory(startFile.getAbsolutePath());
        } else {
          fd.setDirectory(startFile.getParent());
        }
        if (!load && suggestFileName) {
          fd.setFile(startFile.getName());
        }
      }
      if (fileSuffixes != null) {
        fd.setFilenameFilter(filter);
      }
      fd.setVisible(true);
      if (selectDirectories) {
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
      }
      if (fd.getFile() != null) {
        selectedFile = new File(fd.getDirectory() + "/" + fd.getFile());
        if (mode == FileDialog.SAVE) {
          if (!filter.accept(selectedFile)) {
            selectedFile = new File(selectedFile.getAbsolutePath() + "." + fileSuffixes[0]);
          }
        }
      }
      fd.dispose();

    } else {
      JFileChooser fc = new JFileChooser();
      if (startFile != null) {
        if ((!load && suggestFileName) || startFile.isDirectory()) {
          fc.setSelectedFile(startFile);
        } else {
          fc.setSelectedFile(startFile.getParentFile());
        }
      }
      fc.setDialogTitle(title);
      if (selectDirectories) {
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      }
      if (fileSuffixes != null) {
        fc.setFileFilter(filter);
      }
      int returnVal;
      if (load) {
        returnVal = fc.showOpenDialog(parent);
      } else {
        returnVal = fc.showSaveDialog(parent);
      }
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        selectedFile = fc.getSelectedFile();
      }
    }
    return selectedFile;
  }
Ejemplo n.º 25
0
  public File browse(
      Component parent,
      BrowseType browseType,
      FileType fileType,
      PrefType prefType,
      boolean dirsOnly,
      String startingDir) {
    init(parent);

    try {
      prefs.sync();
    } catch (BackingStoreException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    File selFile = null;
    if (startingDir != null) {
      File f = new File(startingDir);

      if (f.isFile()) //
      startingDir = f.getParent();
      else {
        if (!f.exists()) f.mkdir();

        if (dirsOnly) { // make parent dir the starting dir, if it exists
          startingDir = f.getParent();
          selFile = f;
        }
      }
    }

    String lastDir =
        startingDir != null && startingDir.length() != 0
            ? startingDir
            : prefs.get("file." + browseType.name() + "." + prefType.name(), null);
    if (lastDir == null) {
      if (prefType == PrefType.FontLoad) {
        lastDir = getBestFontPath();
      } else {
        lastDir = System.getProperty("user.home");
      }
    }

    File lastDirFile = new File(lastDir);
    String lastDirFileName = "";
    if (lastDirFile.exists() && lastDirFile.isFile()) lastDirFileName = lastDirFile.getName();

    File file = null;

    if (useJFileChooser) {
      jFileChooser.setFileFilter(fileType.swingFilter);

      jFileChooser.setCurrentDirectory(new File(lastDir));

      if (selFile != null) jFileChooser.setSelectedFile(selFile);

      jFileChooser.setFileSelectionMode(
          dirsOnly ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY);
      int a =
          browseType == BrowseType.Open
              ? jFileChooser.showOpenDialog(parent)
              : jFileChooser.showSaveDialog(parent);
      if (a == JFileChooser.APPROVE_OPTION) {
        prefs.put(
            "file." + browseType.name(), jFileChooser.getCurrentDirectory().getAbsolutePath());
        file = jFileChooser.getSelectedFile();
      }
    } else {
      System.setProperty("apple.awt.fileDialogForDirectories", Boolean.toString(dirsOnly));

      fileDialog.setAlwaysOnTop(true);
      fileDialog.setFilenameFilter(fileType.awtFilter);
      fileDialog.setDirectory(lastDir);
      fileDialog.setFile(lastDirFileName);
      fileDialog.setMode(browseType == BrowseType.Open ? FileDialog.LOAD : FileDialog.SAVE);
      fileDialog.setVisible(true);
      String res = fileDialog.getFile();
      if (res != null) {
        File ret = new File(fileDialog.getDirectory(), res);
        File dir = ret;
        if (dir.getParentFile() != null && !dir.isDirectory()) dir = dir.getParentFile();
        prefs.put("file." + browseType.name(), dir.getAbsolutePath());
        file = ret;
      }
    }

    if (file != null) {
      try {
        prefs.flush();
      } catch (BackingStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    return file;
  }
  /**
   * Method declaration
   *
   * @param ev
   */
  public void actionPerformed(ActionEvent ev) {

    String s = ev.getActionCommand();

    if (s == null) {
      if (ev.getSource() instanceof MenuItem) {
        MenuItem i;

        s = ((MenuItem) ev.getSource()).getLabel();
      }
    }

    if (s.equals("Execute")) {
      execute();
    } else if (s.equals("Exit")) {
      windowClosing(null);
    } else if (s.equals("Transfer")) {
      Transfer.work(null);
    } else if (s.equals("Dump")) {
      Transfer.work(new String[] {"-d"});

      /* NB - 26052002 Restore is not implemented yet in the transfer tool */
      /*
              } else if (s.equals("Restore")) {
                  Transfer.work(new String[]{"-r"});
      */
    } else if (s.equals("Logging on")) {
      jdbcSystem.setLogToSystem(true);
    } else if (s.equals("Logging off")) {
      jdbcSystem.setLogToSystem(false);
    } else if (s.equals("Refresh Tree")) {
      refreshTree();
    } else if (s.startsWith("#")) {
      int i = Integer.parseInt(s.substring(1));

      txtCommand.setText(sRecent[i]);
    } else if (s.equals("Connect...")) {
      connect(ConnectionDialog.createConnection(fMain, "Connect"));
      refreshTree();
    } else if (s.equals("Results in Grid")) {
      iResult = 0;

      pResult.removeAll();
      pResult.add("Center", gResult);
      pResult.doLayout();
    } else if (s.equals("Open Script...")) {
      FileDialog f = new FileDialog(fMain, "Open Script", FileDialog.LOAD);

      // (ulrivo): set default directory if set from command line
      if (defDirectory != null) {
        f.setDirectory(defDirectory);
      }

      f.show();

      String file = f.getFile();

      if (file != null) {
        txtCommand.setText(DatabaseManagerCommon.readFile(f.getDirectory() + file));
      }
    } else if (s.equals("Save Script...")) {
      FileDialog f = new FileDialog(fMain, "Save Script", FileDialog.SAVE);

      // (ulrivo): set default directory if set from command line
      if (defDirectory != null) {
        f.setDirectory(defDirectory);
      }

      f.show();

      String file = f.getFile();

      if (file != null) {
        DatabaseManagerCommon.writeFile(f.getDirectory() + file, txtCommand.getText());
      }
    } else if (s.equals("Save Result...")) {
      FileDialog f = new FileDialog(fMain, "Save Result", FileDialog.SAVE);

      // (ulrivo): set default directory if set from command line
      if (defDirectory != null) {
        f.setDirectory(defDirectory);
      }

      f.show();

      String file = f.getFile();

      if (file != null) {
        showResultInText();
        DatabaseManagerCommon.writeFile(f.getDirectory() + file, txtResult.getText());
      }
    } else if (s.equals("Results in Text")) {
      iResult = 1;

      pResult.removeAll();
      pResult.add("Center", txtResult);
      pResult.doLayout();
      showResultInText();
    } else if (s.equals("AutoCommit on")) {
      try {
        cConn.setAutoCommit(true);
      } catch (SQLException e) {
      }
    } else if (s.equals("AutoCommit off")) {
      try {
        cConn.setAutoCommit(false);
      } catch (SQLException e) {
      }
    } else if (s.equals("Enlarge Tree")) {
      Dimension d = tTree.getMinimumSize();

      d.width += 20;

      tTree.setMinimumSize(d);
      fMain.pack();
    } else if (s.equals("Shrink Tree")) {
      Dimension d = tTree.getMinimumSize();

      d.width -= 20;

      if (d.width >= 0) {
        tTree.setMinimumSize(d);
      }

      fMain.pack();
    } else if (s.equals("Enlarge Command")) {
      txtCommand.setRows(txtCommand.getRows() + 1);
      fMain.pack();
    } else if (s.equals("Shrink Command")) {
      int i = txtCommand.getRows() - 1;

      txtCommand.setRows(i < 1 ? 1 : i);
      fMain.pack();
    } else if (s.equals("Commit")) {
      try {
        cConn.commit();
      } catch (SQLException e) {
      }
    } else if (s.equals("Insert test data")) {
      insertTestData();
    } else if (s.equals("Rollback")) {
      try {
        cConn.rollback();
      } catch (SQLException e) {
      }
    } else if (s.equals("Disable MaxRows")) {
      try {
        sStatement.setMaxRows(0);
      } catch (SQLException e) {
      }
    } else if (s.equals("Set MaxRows to 100")) {
      try {
        sStatement.setMaxRows(100);
      } catch (SQLException e) {
      }
    } else if (s.equals("SELECT")) {
      showHelp(DatabaseManagerCommon.selectHelp);
    } else if (s.equals("INSERT")) {
      showHelp(DatabaseManagerCommon.insertHelp);
    } else if (s.equals("UPDATE")) {
      showHelp(DatabaseManagerCommon.updateHelp);
    } else if (s.equals("DELETE")) {
      showHelp(DatabaseManagerCommon.deleteHelp);
    } else if (s.equals("CREATE TABLE")) {
      showHelp(DatabaseManagerCommon.createTableHelp);
    } else if (s.equals("DROP TABLE")) {
      showHelp(DatabaseManagerCommon.dropTableHelp);
    } else if (s.equals("CREATE INDEX")) {
      showHelp(DatabaseManagerCommon.createIndexHelp);
    } else if (s.equals("DROP INDEX")) {
      showHelp(DatabaseManagerCommon.dropIndexHelp);
    } else if (s.equals("CHECKPOINT")) {
      showHelp(DatabaseManagerCommon.checkpointHelp);
    } else if (s.equals("SCRIPT")) {
      showHelp(DatabaseManagerCommon.scriptHelp);
    } else if (s.equals("SHUTDOWN")) {
      showHelp(DatabaseManagerCommon.shutdownHelp);
    } else if (s.equals("SET")) {
      showHelp(DatabaseManagerCommon.setHelp);
    } else if (s.equals("Test Script")) {
      showHelp(DatabaseManagerCommon.testHelp);
    }
  }