예제 #1
0
  private void changeDirectory(File dir) {
    JFileChooser fc = getFileChooser();
    // Traverse shortcuts on Windows
    if (dir != null && FilePane.usesShellFolder(fc)) {
      try {
        ShellFolder shellFolder = ShellFolder.getShellFolder(dir);

        if (shellFolder.isLink()) {
          File linkedTo = shellFolder.getLinkLocation();

          // If linkedTo is null we try to use dir
          if (linkedTo != null) {
            if (fc.isTraversable(linkedTo)) {
              dir = linkedTo;
            } else {
              return;
            }
          } else {
            dir = shellFolder;
          }
        }
      } catch (FileNotFoundException ex) {
        return;
      }
    }
    fc.setCurrentDirectory(dir);
    if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES
        && fc.getFileSystemView().isFileSystem(dir)) {

      setFileName(dir.getAbsolutePath());
    }
  }
예제 #2
0
 protected void uninstallDefaults(JFileChooser fc) {
   uninstallIcons(fc);
   uninstallStrings(fc);
   if (fc.getTransferHandler() instanceof UIResource) {
     fc.setTransferHandler(null);
   }
 }
예제 #3
0
 protected void uninstallListeners(JFileChooser fc) {
   if (propertyChangeListener != null) {
     fc.removePropertyChangeListener(propertyChangeListener);
   }
   fc.removePropertyChangeListener(getModel());
   SwingUtilities.replaceUIInputMap(fc, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
   SwingUtilities.replaceUIActionMap(fc, null);
 }
예제 #4
0
 protected void installDefaults(JFileChooser fc) {
   installIcons(fc);
   installStrings(fc);
   usesSingleFilePane = UIManager.getBoolean("FileChooser.usesSingleFilePane");
   readOnly = UIManager.getBoolean("FileChooser.readOnly");
   TransferHandler th = fc.getTransferHandler();
   if (th == null || th instanceof UIResource) {
     fc.setTransferHandler(defaultTransferHandler);
   }
   LookAndFeel.installProperty(fc, "opaque", Boolean.FALSE);
 }
예제 #5
0
 private void resetGlobFilter() {
   if (actualFileFilter != null) {
     JFileChooser chooser = getFileChooser();
     FileFilter currentFilter = chooser.getFileFilter();
     if (currentFilter != null && currentFilter.equals(globFilter)) {
       chooser.setFileFilter(actualFileFilter);
       chooser.removeChoosableFileFilter(globFilter);
     }
     actualFileFilter = null;
   }
 }
예제 #6
0
 public String getApproveButtonText(JFileChooser fc) {
   String buttonText = fc.getApproveButtonText();
   if (buttonText != null) {
     return buttonText;
   } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
     return openButtonText;
   } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
     return saveButtonText;
   } else {
     return null;
   }
 }
예제 #7
0
 /** Returns the title of this dialog */
 public String getDialogTitle(JFileChooser fc) {
   String dialogTitle = fc.getDialogTitle();
   if (dialogTitle != null) {
     return dialogTitle;
   } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
     return openDialogTitleText;
   } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
     return saveDialogTitleText;
   } else {
     return getApproveButtonText(fc);
   }
 }
예제 #8
0
 public int getApproveButtonMnemonic(JFileChooser fc) {
   int mnemonic = fc.getApproveButtonMnemonic();
   if (mnemonic > 0) {
     return mnemonic;
   } else if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
     return openButtonMnemonic;
   } else if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
     return saveButtonMnemonic;
   } else {
     return mnemonic;
   }
 }
예제 #9
0
  protected void installListeners(JFileChooser fc) {
    propertyChangeListener = createPropertyChangeListener(fc);
    if (propertyChangeListener != null) {
      fc.addPropertyChangeListener(propertyChangeListener);
    }
    fc.addPropertyChangeListener(getModel());

    InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    SwingUtilities.replaceUIInputMap(fc, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
    ActionMap actionMap = getActionMap();
    SwingUtilities.replaceUIActionMap(fc, actionMap);
  }
예제 #10
0
  public void installUI(JComponent c) {
    accessoryPanel = new JPanel(new BorderLayout());
    filechooser = (JFileChooser) c;

    createModel();

    clearIconCache();

    installDefaults(filechooser);
    installComponents(filechooser);
    installListeners(filechooser);
    filechooser.applyComponentOrientation(filechooser.getComponentOrientation());
  }
예제 #11
0
    public void actionPerformed(ActionEvent e) {
      if (readOnly) {
        return;
      }
      JFileChooser fc = getFileChooser();
      File currentDirectory = fc.getCurrentDirectory();

      if (!currentDirectory.exists()) {
        JOptionPane.showMessageDialog(
            fc,
            newFolderParentDoesntExistText,
            newFolderParentDoesntExistTitleText,
            JOptionPane.WARNING_MESSAGE);
        return;
      }

      File newFolder;
      try {
        newFolder = fc.getFileSystemView().createNewFolder(currentDirectory);
        if (fc.isMultiSelectionEnabled()) {
          fc.setSelectedFiles(new File[] {newFolder});
        } else {
          fc.setSelectedFile(newFolder);
        }
      } catch (IOException exc) {
        JOptionPane.showMessageDialog(
            fc,
            newFolderErrorText + newFolderErrorSeparator + exc,
            newFolderErrorText,
            JOptionPane.ERROR_MESSAGE);
        return;
      }

      fc.rescanCurrentDirectory();
    }
  private void jMenuItemLoadProjectActionPerformed(
      java.awt.event.ActionEvent evt) // GEN-FIRST:event_jMenuItemLoadProjectActionPerformed
      { // GEN-HEADEREND:event_jMenuItemLoadProjectActionPerformed
    JFileChooser jfc = new JFileChooser();
    if (lastPath != null) {
      jfc.setCurrentDirectory(lastPath);
    }
    int fileDialogReturnVal = jfc.showOpenDialog(this);

    if (fileDialogReturnVal == JFileChooser.APPROVE_OPTION) {
      try {
        File inputFile = jfc.getSelectedFile();
        FileInputStream fis = new FileInputStream(inputFile);
        ObjectInputStream ois = new ObjectInputStream(fis);

        this.theProject = (Project) ois.readObject();
        this.currentResults = (LSAResults) ois.readObject();

        lastPath = new File(jfc.getSelectedFile().getPath());
      } catch (IOException e) {
        if (this.theProject == null) {
          log.log(Log.ERROR, "Failed to load project");
        }
        if (this.currentResults == null) {
          log.log(Log.WARNING, "Failed to load results");
        }

        log.log(Log.WARNING, e.getMessage());
      } catch (ClassNotFoundException e) {
        log.log(Log.ERROR, "Class not found error, version mismatch");
      }
    }
    if (this.theProject != null) {
      jMenuItemViewDocuments.setEnabled(true);
      jMenuItemSaveProject.setEnabled(true);
      this.setTitle(theProject.getProjectName());
      log.log(Log.INFO, "Project Loaded");
    }
    if (this.currentResults != null) {
      log.log(Log.INFO, "Results loaded");
    }
  } // GEN-LAST:event_jMenuItemLoadProjectActionPerformed
  private void jMenuItemSaveLSAResultsActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jMenuItemSaveLSAResultsActionPerformed
    if (this.currentResults != null) {
      JFileChooser jfc = new JFileChooser();
      int fileDialogReturnVal = jfc.showSaveDialog(this);

      if (fileDialogReturnVal == JFileChooser.APPROVE_OPTION) {
        try {
          File outputFile = jfc.getSelectedFile();
          FileOutputStream fos = new FileOutputStream(outputFile);
          ObjectOutputStream oos = new ObjectOutputStream(fos);

          oos.writeObject(this.currentResults);
        } catch (IOException e) {
          System.out.println("IOexception");
          System.out.println(e.getMessage());
        }
      }
    }
  } // GEN-LAST:event_jMenuItemSaveLSAResultsActionPerformed
  private void jMenuItemLoadLSAResultsActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jMenuItemLoadLSAResultsActionPerformed
    JFileChooser jfc = new JFileChooser();
    int fileDialogReturnVal = jfc.showOpenDialog(this);

    if (fileDialogReturnVal == JFileChooser.APPROVE_OPTION) {
      try {
        File inputFile = jfc.getSelectedFile();
        FileInputStream fis = new FileInputStream(inputFile);
        ObjectInputStream ois = new ObjectInputStream(fis);

        this.currentResults = (LSAResults) ois.readObject();
      } catch (IOException e) {
        log.log(Log.ERROR, "Failed to load LSA results\n" + e.getMessage());
      } catch (ClassNotFoundException e) {
        log.log(Log.ERROR, "Class not found : Error loading LSA results due to version mismatch");
      }

      System.out.println(currentResults == null);
    }
  } // GEN-LAST:event_jMenuItemLoadLSAResultsActionPerformed
  private void jMenuItemImportActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jMenuItemImportActionPerformed
    JFileChooser jfc = new JFileChooser();
    jfc.setMultiSelectionEnabled(true);
    if (lastPath != null) {
      jfc.setCurrentDirectory(lastPath);
    }
    int fileDialogReturnVal = jfc.showOpenDialog(this);

    // now select the file
    if (fileDialogReturnVal == JFileChooser.APPROVE_OPTION) {
      // add code here to allow selection of a document class

      DocumentClassDialog d = DocumentClassDialog.showClassDialog(this, this.theProject);

      DocumentClass selectedClass = d.getSelectedDocumentClass();

      File[] selected = jfc.getSelectedFiles();
      for (int i = 0; i < selected.length; i++) {
        theProject.addNewDocument(selected[i], 1.0f, selected[i].toString(), selectedClass);
      }

      docFrameTableModel.fireTableDataChanged();

      lastPath = new File(jfc.getSelectedFile().getPath());
    }
  } // GEN-LAST:event_jMenuItemImportActionPerformed
  private void jMenuItemSaveProjectActionPerformed(
      java.awt.event.ActionEvent evt) // GEN-FIRST:event_jMenuItemSaveProjectActionPerformed
      { // GEN-HEADEREND:event_jMenuItemSaveProjectActionPerformed
    if (this.theProject != null) {
      JFileChooser jfc = new JFileChooser();
      int fileDialogReturnVal = jfc.showSaveDialog(this);

      if (fileDialogReturnVal == JFileChooser.APPROVE_OPTION) {
        try {
          File outputFile = jfc.getSelectedFile();
          FileOutputStream fos = new FileOutputStream(outputFile);
          ObjectOutputStream oos = new ObjectOutputStream(fos);

          oos.writeObject(this.theProject);
          if (this.currentResults != null) {
            oos.writeObject(this.currentResults);
          }
        } catch (IOException e) {
          log.log(Log.ERROR, "Failed to save file\n" + e.getMessage());
        }
      }
    }
  } // GEN-LAST:event_jMenuItemSaveProjectActionPerformed
예제 #17
0
  protected void installStrings(JFileChooser fc) {

    Locale l = fc.getLocale();
    newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText", l);
    newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator", l);

    newFolderParentDoesntExistTitleText =
        UIManager.getString("FileChooser.newFolderParentDoesntExistTitleText", l);
    newFolderParentDoesntExistText =
        UIManager.getString("FileChooser.newFolderParentDoesntExistText", l);

    fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText", l);
    directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText", l);

    saveButtonText = UIManager.getString("FileChooser.saveButtonText", l);
    openButtonText = UIManager.getString("FileChooser.openButtonText", l);
    saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText", l);
    openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText", l);
    cancelButtonText = UIManager.getString("FileChooser.cancelButtonText", l);
    updateButtonText = UIManager.getString("FileChooser.updateButtonText", l);
    helpButtonText = UIManager.getString("FileChooser.helpButtonText", l);
    directoryOpenButtonText = UIManager.getString("FileChooser.directoryOpenButtonText", l);

    saveButtonMnemonic = getMnemonic("FileChooser.saveButtonMnemonic", l);
    openButtonMnemonic = getMnemonic("FileChooser.openButtonMnemonic", l);
    cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic", l);
    updateButtonMnemonic = getMnemonic("FileChooser.updateButtonMnemonic", l);
    helpButtonMnemonic = getMnemonic("FileChooser.helpButtonMnemonic", l);
    directoryOpenButtonMnemonic = getMnemonic("FileChooser.directoryOpenButtonMnemonic", l);

    saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText", l);
    openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText", l);
    cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText", l);
    updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText", l);
    helpButtonToolTipText = UIManager.getString("FileChooser.helpButtonToolTipText", l);
    directoryOpenButtonToolTipText =
        UIManager.getString("FileChooser.directoryOpenButtonToolTipText", l);
  }
예제 #18
0
    public void actionPerformed(ActionEvent e) {
      if (isDirectorySelected()) {
        File dir = getDirectory();
        if (dir != null) {
          try {
            // Strip trailing ".."
            dir = ShellFolder.getNormalizedFile(dir);
          } catch (IOException ex) {
            // Ok, use f as is
          }
          changeDirectory(dir);
          return;
        }
      }

      JFileChooser chooser = getFileChooser();

      String filename = getFileName();
      FileSystemView fs = chooser.getFileSystemView();
      File dir = chooser.getCurrentDirectory();

      if (filename != null) {
        // Remove whitespaces from end of filename
        int i = filename.length() - 1;

        while (i >= 0 && filename.charAt(i) <= ' ') {
          i--;
        }

        filename = filename.substring(0, i + 1);
      }

      if (filename == null || filename.length() == 0) {
        // no file selected, multiple selection off, therefore cancel the approve action
        resetGlobFilter();
        return;
      }

      File selectedFile = null;
      File[] selectedFiles = null;

      // Unix: Resolve '~' to user's home directory
      if (File.separatorChar == '/') {
        if (filename.startsWith("~/")) {
          filename = System.getProperty("user.home") + filename.substring(1);
        } else if (filename.equals("~")) {
          filename = System.getProperty("user.home");
        }
      }

      if (chooser.isMultiSelectionEnabled()
          && filename.length() > 1
          && filename.charAt(0) == '"'
          && filename.charAt(filename.length() - 1) == '"') {
        List<File> fList = new ArrayList<File>();

        String[] files = filename.substring(1, filename.length() - 1).split("\" \"");
        // Optimize searching files by names in "children" array
        Arrays.sort(files);

        File[] children = null;
        int childIndex = 0;

        for (String str : files) {
          File file = fs.createFileObject(str);
          if (!file.isAbsolute()) {
            if (children == null) {
              children = fs.getFiles(dir, false);
              Arrays.sort(children);
            }
            for (int k = 0; k < children.length; k++) {
              int l = (childIndex + k) % children.length;
              if (children[l].getName().equals(str)) {
                file = children[l];
                childIndex = l + 1;
                break;
              }
            }
          }
          fList.add(file);
        }

        if (!fList.isEmpty()) {
          selectedFiles = fList.toArray(new File[fList.size()]);
        }
        resetGlobFilter();
      } else {
        selectedFile = fs.createFileObject(filename);
        if (!selectedFile.isAbsolute()) {
          selectedFile = fs.getChild(dir, filename);
        }
        // check for wildcard pattern
        FileFilter currentFilter = chooser.getFileFilter();
        if (!selectedFile.exists() && isGlobPattern(filename)) {
          changeDirectory(selectedFile.getParentFile());
          if (globFilter == null) {
            globFilter = new GlobFilter();
          }
          try {
            globFilter.setPattern(selectedFile.getName());
            if (!(currentFilter instanceof GlobFilter)) {
              actualFileFilter = currentFilter;
            }
            chooser.setFileFilter(null);
            chooser.setFileFilter(globFilter);
            return;
          } catch (PatternSyntaxException pse) {
            // Not a valid glob pattern. Abandon filter.
          }
        }

        resetGlobFilter();

        // Check for directory change action
        boolean isDir = (selectedFile != null && selectedFile.isDirectory());
        boolean isTrav = (selectedFile != null && chooser.isTraversable(selectedFile));
        boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled();
        boolean isFileSelEnabled = chooser.isFileSelectionEnabled();
        boolean isCtrl =
            (e != null
                && (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0);

        if (isDir && isTrav && (isCtrl || !isDirSelEnabled)) {
          changeDirectory(selectedFile);
          return;
        } else if ((isDir || !isFileSelEnabled)
            && (!isDir || !isDirSelEnabled)
            && (!isDirSelEnabled || selectedFile.exists())) {
          selectedFile = null;
        }
      }

      if (selectedFiles != null || selectedFile != null) {
        if (selectedFiles != null || chooser.isMultiSelectionEnabled()) {
          if (selectedFiles == null) {
            selectedFiles = new File[] {selectedFile};
          }
          chooser.setSelectedFiles(selectedFiles);
          // Do it again. This is a fix for bug 4949273 to force the
          // selected value in case the ListSelectionModel clears it
          // for non-existing file names.
          chooser.setSelectedFiles(selectedFiles);
        } else {
          chooser.setSelectedFile(selectedFile);
        }
        chooser.approveSelection();
      } else {
        if (chooser.isMultiSelectionEnabled()) {
          chooser.setSelectedFiles(null);
        } else {
          chooser.setSelectedFile(null);
        }
        chooser.cancelSelection();
      }
    }
예제 #19
0
 public void actionPerformed(ActionEvent e) {
   JFileChooser fc = getFileChooser();
   changeDirectory(fc.getFileSystemView().getHomeDirectory());
 }
예제 #20
0
    public void valueChanged(ListSelectionEvent evt) {
      if (!evt.getValueIsAdjusting()) {
        JFileChooser chooser = getFileChooser();
        FileSystemView fsv = chooser.getFileSystemView();
        JList list = (JList) evt.getSource();

        int fsm = chooser.getFileSelectionMode();
        boolean useSetDirectory = usesSingleFilePane && (fsm == JFileChooser.FILES_ONLY);

        if (chooser.isMultiSelectionEnabled()) {
          File[] files = null;
          Object[] objects = list.getSelectedValues();
          if (objects != null) {
            if (objects.length == 1
                && ((File) objects[0]).isDirectory()
                && chooser.isTraversable(((File) objects[0]))
                && (useSetDirectory || !fsv.isFileSystem(((File) objects[0])))) {
              setDirectorySelected(true);
              setDirectory(((File) objects[0]));
            } else {
              ArrayList<File> fList = new ArrayList<File>(objects.length);
              for (Object object : objects) {
                File f = (File) object;
                boolean isDir = f.isDirectory();
                if ((chooser.isFileSelectionEnabled() && !isDir)
                    || (chooser.isDirectorySelectionEnabled() && fsv.isFileSystem(f) && isDir)) {
                  fList.add(f);
                }
              }
              if (fList.size() > 0) {
                files = fList.toArray(new File[fList.size()]);
              }
              setDirectorySelected(false);
            }
          }
          chooser.setSelectedFiles(files);
        } else {
          File file = (File) list.getSelectedValue();
          if (file != null
              && file.isDirectory()
              && chooser.isTraversable(file)
              && (useSetDirectory || !fsv.isFileSystem(file))) {

            setDirectorySelected(true);
            setDirectory(file);
            if (usesSingleFilePane) {
              chooser.setSelectedFile(null);
            }
          } else {
            setDirectorySelected(false);
            if (file != null) {
              chooser.setSelectedFile(file);
            }
          }
        }
      }
    }
예제 #21
0
 public void actionPerformed(ActionEvent e) {
   JFileChooser fc = getFileChooser();
   fc.setCurrentDirectory(fc.getFileSystemView().createFileObject(getDirectoryName()));
   fc.rescanCurrentDirectory();
 }