com.dua3.meja.ui.swing.MejaSwingHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of meja-swing Show documentation
Show all versions of meja-swing Show documentation
Meja spreadsheet library - swing tools
/*
* Copyright 2015 Axel Howind ([email protected]).
*
* 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.dua3.meja.ui.swing;
import com.dua3.meja.model.Sheet;
import com.dua3.meja.model.Workbook;
import com.dua3.meja.util.TableOptions;
import com.dua3.utility.io.FileType;
import com.dua3.utility.io.IoUtil;
import com.dua3.utility.io.OpenMode;
import com.dua3.utility.options.Arguments;
import com.dua3.utility.options.Option;
import com.dua3.utility.swing.SwingFileFilter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableModel;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
/**
* Helper class.
*
* @author Axel Howind ([email protected])
*/
public final class MejaSwingHelper {
private static final Logger LOG = LogManager.getLogger(MejaSwingHelper.class);
/**
* Create a TableModel to be used with JTable.
*
* @param sheet the sheet to create a model for
* @param options the options to use
* @return table model instance of {@code JTableModel} for the sheet
*/
public static TableModel getTableModel(final Sheet sheet, TableOptions... options) {
return new SheetTableModel(sheet, options);
}
/**
* Show a file open dialog and load the selected workbook.
*
* @param parent the parent component to use for the dialog
* @param uri the directory to set in the open dialog or the default path
* @return the workbook the user chose or null if dialog was canceled
* @throws IOException if a workbook was selected but could not be loaded
*/
public static Optional showDialogAndOpenWorkbook(Component parent, URI uri) throws IOException {
JFileChooser jfc = createJFileChooser(uri);
for (FileFilter filter : SwingFileFilter.getFilters(OpenMode.READ, Workbook.class)) {
jfc.addChoosableFileFilter(filter);
}
int rc = jfc.showOpenDialog(parent);
if (rc != JFileChooser.APPROVE_OPTION) {
return Optional.empty();
}
return openWorkbook(parent, jfc.getSelectedFile().toURI());
}
private static JFileChooser createJFileChooser(URI uri) {
Path path = IoUtil.toPath(uri);
boolean defaultFS = path.getFileSystem().equals(FileSystems.getDefault());
File file = defaultFS ? path.toFile() : new File(".");
return new JFileChooser(file.isDirectory() ? file : file.getParentFile());
}
/**
* Open a workbook from a given URI. A dialog to sepcify options is shown if required.
*
* @param parent the parent component to use
* @param uri the URI of the workbook to open
* @return an Optional containing the opened workbook, or an empty Optional if the dialog was canceled
* @throws IOException if the selected workbook could not be loaded
*/
public static Optional openWorkbook(Component parent, URI uri) throws IOException {
FileType fileType = FileType.forUri(uri, Workbook.class)
.orElseThrow(() -> new IllegalArgumentException("unknown filetype: " + uri));
// load
return Optional.of(fileType.read(uri, t -> showOptionsDialog(parent, t)));
}
private static Arguments showOptionsDialog(Component parent, FileType> fileType) {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy