All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rapiddweller.common.ui.swing.SwingFileChooser Maven / Gradle / Ivy

Go to download

'rapiddweller Common' is an open source Java library forked from Databene Commons by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 2.0.1-jdk-11
Show newest version
/*
 * Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rapiddweller.common.ui.swing;

import com.rapiddweller.common.Assert;
import com.rapiddweller.common.ui.FileChooser;
import com.rapiddweller.common.ui.FileOperation;
import com.rapiddweller.common.ui.FileTypeSupport;

import javax.swing.JFileChooser;
import java.awt.Component;
import java.io.File;

/**
 * Swing implementation of the {@link FileChooser} interface.
 * Created at 14.12.2008 14:34:50
 *
 * @author Volker Bergmann
 * @since 0.5.13
 */
public class SwingFileChooser extends JFileChooser implements FileChooser {

  private static final long serialVersionUID = 3258145358496737942L;

  private final FileOperation operation;

  /**
   * Instantiates a new Swing file chooser.
   *
   * @param supportedTypes the supported types
   * @param operation      the operation
   */
  public SwingFileChooser(FileTypeSupport supportedTypes, FileOperation operation) {
    Assert.notNull(supportedTypes, "supportedTypes");
    Assert.notNull(operation, "operation");
    this.operation = operation;
    super.setFileSelectionMode(fileSelectionMode(supportedTypes));
    super.setDialogType(dialogType(operation));
  }

  @Override
  public void setTitle(String title) {
    setDialogTitle(title);
  }

  @Override
  public File chooseFile(Component component) {
    int approval;
    switch (operation) {
      case OPEN:
        approval = showOpenDialog(component);
        break;
      case SAVE:
        approval = showSaveDialog(component);
        break;
      default:
        approval = showDialog(component, "Choose");
        break;
    }
    return (approval == APPROVE_OPTION ? super.getSelectedFile() : null);
  }

  // private helpers -------------------------------------------------------------------------------------------------

  private static int dialogType(FileOperation operation) {
    switch (operation) {
      case OPEN:
        return JFileChooser.OPEN_DIALOG;
      case SAVE:
        return JFileChooser.SAVE_DIALOG;
      default:
        return JFileChooser.CUSTOM_DIALOG;
    }
  }

  private static int fileSelectionMode(FileTypeSupport supportedTypes) {
    switch (supportedTypes) {
      case directoriesOnly:
        return JFileChooser.DIRECTORIES_ONLY;
      case filesOnly:
        return JFileChooser.FILES_ONLY;
      case filesAndDirectories:
        return JFileChooser.FILES_AND_DIRECTORIES;
      default:
        throw new IllegalArgumentException("Illegal option: " + supportedTypes);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy