/*
* Copyright 2013 Serdar.
*
* 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 de.fub.maps.project.datasource.spi.actions;
import de.fub.maps.project.datasource.GPXDatasourceNode;
import de.fub.maps.project.datasource.MapsDatasourceNodeFactory;
import de.fub.utilsmodule.icons.IconRegister;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.util.WeakListeners;
/**
*
* @author Serdar
*/
public class ExportDialog extends javax.swing.JPanel implements ActionListener, DocumentListener {
private static final long serialVersionUID = 1L;
private List<FileObject> folders = null;
private ImageIcon icon;
/**
* Creates new form ExportDialog
*/
public ExportDialog() {
initComponents();
folderName.addActionListener(WeakListeners.create(ActionListener.class, ExportDialog.this, folderName));
fileName.getDocument().addDocumentListener(WeakListeners.document(ExportDialog.this, fileName.getDocument()));
GPXDatasourceNode datasourceNode = MapsDatasourceNodeFactory.gpxDatasourceNode;
if (datasourceNode != null) {
DataObject dataObject = datasourceNode.getLookup().lookup(DataObject.class);
if (dataObject != null) {
folders = Arrays.asList(dataObject.getPrimaryFile().getChildren());
for (FileObject file : folders) {
folderName.addItem(file.getName());
}
if (!folders.isEmpty()) {
folderName.setSelectedIndex(0);
}
}
}
}
public JTextField getFileName() {
return fileName;
}
public JComboBox getFolderName() {
return folderName;
}
public JCheckBox getOpenAfterSave() {
return openAfterSave;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public JButton getCancelbutton() {
return cancelbutton;
}
public JButton getSaveButton() {
return saveButton;
}
public Object[] getOptions() {
return new Object[]{saveButton, cancelbutton};
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
saveButton = new javax.swing.JButton();
cancelbutton = new javax.swing.JButton();
folderName = new javax.swing.JComboBox<String>();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
fileName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
openAfterSave = new javax.swing.JCheckBox();
message = new javax.swing.JLabel();
org.openide.awt.Mnemonics.setLocalizedText(saveButton, org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.saveButton.text")); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(cancelbutton, org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.cancelbutton.text")); // NOI18N
folderName.setEditable(true);
jScrollPane1.setBorder(null);
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
jTextArea1.setLineWrap(true);
jTextArea1.setRows(2);
jTextArea1.setText(org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.jTextArea1.text")); // NOI18N
jTextArea1.setWrapStyleWord(true);
jTextArea1.setOpaque(false);
jScrollPane1.setViewportView(jTextArea1);
org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.jLabel1.text")); // NOI18N
fileName.setText(org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.fileName.text")); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.jLabel2.text")); // NOI18N
openAfterSave.setSelected(true);
org.openide.awt.Mnemonics.setLocalizedText(openAfterSave, org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.openAfterSave.text")); // NOI18N
openAfterSave.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
openAfterSave.setIconTextGap(10);
openAfterSave.setMargin(new java.awt.Insets(2, -2, 2, 2));
org.openide.awt.Mnemonics.setLocalizedText(message, org.openide.util.NbBundle.getMessage(ExportDialog.class, "ExportDialog.message.text")); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(openAfterSave)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(message, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(folderName, 0, 394, Short.MAX_VALUE)
.addComponent(fileName))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(folderName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(openAfterSave)
.addGap(18, 18, 18)
.addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cancelbutton;
private javax.swing.JTextField fileName;
private javax.swing.JComboBox<String> folderName;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JLabel message;
private javax.swing.JCheckBox openAfterSave;
private javax.swing.JButton saveButton;
// End of variables declaration//GEN-END:variables
@Override
public void actionPerformed(ActionEvent e) {
checkFileExist();
}
private FileObject getTestFileObject() {
FileObject folderFileObject = null;
GPXDatasourceNode datasourceNode = MapsDatasourceNodeFactory.gpxDatasourceNode;
Object selectedItem = folderName.getSelectedItem();
if (datasourceNode != null && selectedItem instanceof String) {
String foldName = (String) selectedItem;
DataObject dataObject = datasourceNode.getLookup().lookup(DataObject.class);
if (dataObject != null) {
FileObject datasourceFileObject = dataObject.getPrimaryFile();
folderFileObject = datasourceFileObject.getFileObject(foldName);
}
}
return folderFileObject;
}
@Override
public void insertUpdate(DocumentEvent e) {
checkFileExist();
}
@Override
public void removeUpdate(DocumentEvent e) {
checkFileExist();
}
@Override
public void changedUpdate(DocumentEvent e) {
checkFileExist();
}
private void checkFileExist() {
normalState();
FileObject testFileObject = getTestFileObject();
if (testFileObject != null && fileName.getText() != null) {
if (fileName.getText().trim().length() == 0) {
errorState(ErrorState.ERROR_FILE_NAME_REQUIRED);
} else {
String fileString = fileName.getText();
if (!fileString.endsWith(".gpx")) {
fileString = MessageFormat.format("{0}.gpx", fileString);
}
FileObject fileObject = testFileObject.getFileObject(fileString);
if (fileObject != null) {
errorState(ErrorState.ERROR_FILE_ALREADY_EXISTS);
}
}
}
}
private Icon getErrorIcon() {
icon = null;
if (icon == null) {
Image image = IconRegister.findRegisteredIcon("errorIcon.png");
if (image != null) {
icon = new ImageIcon(image);
}
}
return icon;
}
private void errorState(ErrorState error) {
switch (error) {
case ERROR_FILE_ALREADY_EXISTS:
message.setText("There already exists a file with the specifed name. Please enter another name.");
break;
case ERROR_FILE_NAME_REQUIRED:
message.setText("You have to specifiy a file name which has atleast one character!");
break;
default:
throw new AssertionError();
}
message.setIcon(getErrorIcon());
saveButton.setEnabled(false);
}
private void normalState() {
message.setText(null);
message.setIcon(null);
saveButton.setEnabled(true);
}
private enum ErrorState {
ERROR_FILE_ALREADY_EXISTS,
ERROR_FILE_NAME_REQUIRED;
}
}