Skip to content
Snippets Groups Projects
Commit 59664a35 authored by Raphael GIRARDOT's avatar Raphael GIRARDOT
Browse files

- refresh current directory (EXPDATA-642)

- some code refactoring
- automatically grow AdaptableTextArea in case of big texts
parent 0e69cfda
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,9 @@
******************************************************************************/
package org.cdma.gui.databrowser.impl;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
......@@ -21,23 +21,37 @@ import org.cdma.gui.databrowser.interfaces.IDataSourceSeeker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.soleil.lib.project.ObjectUtils;
public abstract class AbstractFileDataSourceSeeker implements IDataSourceSeeker {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFileDataSourceSeeker.class);
private static final String SELECTED_DIRECTORY = "selectedDirectory={}";
private static final String DIRECTORY_IS_NOT_READABLE = "{} directory is not readable ";
private static final String PERMISSION_DENIED_ON = "Permission denied on ";
private static final String DIRECTORTY_PERMISSION = "Directorty permission";
protected static final long DIRECTORY_SCAN_PERIOD = 1000;// 1s
protected Timer timer;
protected IDataSourceBrowser dataSourceBrowser;
protected JFileChooser fileChooser;
protected String directory;
private File currentDirectory = null;
protected final String timerName;
protected File currentDirectory;
protected volatile long lastModified;
protected volatile long lastDirectoryUpdateDate;
public AbstractFileDataSourceSeeker(final IDataSourceBrowser browser) {
dataSourceBrowser = browser;
timerName = getClass().getSimpleName() + ": Refresh current directory";
}
@Override
public void setDirectory(final String path) {
directory = path;
if ((fileChooser != null) && (directory != null)) {
fileChooser.setCurrentDirectory(new File(directory));
}
......@@ -72,27 +86,28 @@ public abstract class AbstractFileDataSourceSeeker implements IDataSourceSeeker
}
}
protected JFileChooser getFileChooser() {
if ((fileChooser == null) && (dataSourceBrowser != null)) {
protected void initFileChooser() {
fileChooser = new JFileChooser();
fileChooser.setOpaque(true);
fileChooser.setBackground(UIManager.getColor("Panel.background"));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setControlButtonsAreShown(false);
fileChooser.addPropertyChangeListener(JFileChooser.DIRECTORY_CHANGED_PROPERTY,
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
fileChooser.addPropertyChangeListener(JFileChooser.DIRECTORY_CHANGED_PROPERTY, (evt) -> {
Object newValue = evt.getNewValue();
if (newValue instanceof File) {
File newFile = (File) newValue;
if (!ObjectUtils.sameObject(newFile, currentDirectory)) {
currentDirectory = (File) newValue;
LOGGER.trace("selectedDirectory={}", currentDirectory.getAbsolutePath());
if (!currentDirectory.canRead()) {
LOGGER.error("{} directory is not readable ", currentDirectory.getAbsolutePath());
LOGGER.trace(SELECTED_DIRECTORY, currentDirectory.getAbsolutePath());
if (currentDirectory.canRead()) {
lastDirectoryUpdateDate = System.currentTimeMillis();
lastModified = currentDirectory.lastModified();
} else {
LOGGER.error(DIRECTORY_IS_NOT_READABLE, currentDirectory.getAbsolutePath());
JOptionPane.showMessageDialog(fileChooser,
"Permission denied on " + currentDirectory.getAbsolutePath(),
"Directorty permission", JOptionPane.ERROR_MESSAGE);
PERMISSION_DENIED_ON + currentDirectory.getAbsolutePath(), DIRECTORTY_PERMISSION,
JOptionPane.ERROR_MESSAGE);
}
}
}
......@@ -105,12 +120,11 @@ public abstract class AbstractFileDataSourceSeeker implements IDataSourceSeeker
public boolean accept(final File f) {
boolean result = false;
if ((f != null) && ((currentDirectory == null) || currentDirectory.canRead())) {
// always accept directories
if (f.isDirectory()) {
// always accept directories
result = true;
}
} else if (f.canRead()) {
// For file check the read property before call plugin method
else if (f.canRead()) {
result = acceptFile(f);
}
}
......@@ -119,25 +133,58 @@ public abstract class AbstractFileDataSourceSeeker implements IDataSourceSeeker
@Override
public String getDescription() {
String description = null;
if (dataSourceBrowser != null) {
description = getFileDescription();
}
return description;
return getFileDescription();
}
});
}
protected JFileChooser getFileChooser() {
if ((fileChooser == null) && (dataSourceBrowser != null)) {
initFileChooser();
}
return fileChooser;
}
@Override
public void startSeeking() {
// nop
// EXPDATA-642: Refresh directory at opening and then regularly
if (fileChooser != null) {
lastDirectoryUpdateDate = System.currentTimeMillis();
lastModified = fileChooser.getCurrentDirectory().lastModified();
fileChooser.rescanCurrentDirectory();
}
timer = new Timer(timerName);
timer.scheduleAtFixedRate(new RefreshDirectoryTask(), DIRECTORY_SCAN_PERIOD, DIRECTORY_SCAN_PERIOD);
}
@Override
public void stopSeeking() {
// nop
// EXPDATA-642: Stop directory refreshing when file choosing dialog is not visible
if (timer != null) {
timer.cancel();
timer = null;
}
}
// ///////////// //
// Inner classes //
// ///////////// //
/**
* {@link TimerTask} that refreshes {@link JFileChooser}'s current directory if necessary.
*/
protected class RefreshDirectoryTask extends TimerTask {
@Override
public void run() {
if ((fileChooser != null) && fileChooser.isShowing()) {
long now = System.currentTimeMillis();
if (now - lastDirectoryUpdateDate >= DIRECTORY_SCAN_PERIOD) {
lastDirectoryUpdateDate = now;
if (fileChooser.getCurrentDirectory().lastModified() != lastModified) {
fileChooser.rescanCurrentDirectory();
}
}
}
}
}
}
......@@ -17,9 +17,13 @@ import fr.soleil.lib.project.awt.ColorUtils;
*/
public class DynamicForegroundAdaptableTextArea extends AdaptableTextArea {
private static final long serialVersionUID = 2029329228563021900L;
private static final long serialVersionUID = -6601535463699390179L;
protected static final String NEW_LINE = "\n";
protected static final int MAX_LINES = 10;
protected Color fg;
protected boolean textSet;
public DynamicForegroundAdaptableTextArea() {
super();
......@@ -46,7 +50,7 @@ public class DynamicForegroundAdaptableTextArea extends AdaptableTextArea {
if (getTextArea() != null) {
CometeColor cfg;
if (fg == null) {
if (((UpdateForegroundTextArea) getTextArea()).getTextArea().isOpaque()) {
if (((UpdateForegroundTextArea) textArea).getTextArea().isOpaque()) {
cfg = ColorTool
.getCometeColor(ColorUtils.getBestForeground(ColorTool.getColor(getCometeBackground())));
} else {
......@@ -64,6 +68,21 @@ public class DynamicForegroundAdaptableTextArea extends AdaptableTextArea {
return new UpdateForegroundTextArea();
}
@Override
public void setText(String text) {
super.setText(text);
if (!textSet) {
if (text != null) {
int count = text.split(NEW_LINE).length;
if (count > 1) {
rows = Math.min(count, MAX_LINES);
updateRows();
}
}
textSet = true;
}
}
// ///////////// //
// Inner classes //
// ///////////// //
......
......@@ -32,7 +32,7 @@ import fr.soleil.data.service.IKey;
public class SeekerDialog extends JDialog implements ActionListener {
private static final long serialVersionUID = 4737688088036348063L;
private static final long serialVersionUID = 8035789792888636358L;
private JButton okButton = null;
private JButton cancelButton = null;
......@@ -68,8 +68,8 @@ public class SeekerDialog extends JDialog implements ActionListener {
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 5));
SequentialGroup hGroup = buttonLayout.createSequentialGroup();
hGroup.addGap(0, 0, Short.MAX_VALUE).addComponent(okButton)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(cancelButton);
hGroup.addGap(0, 0, Short.MAX_VALUE).addComponent(okButton).addPreferredGap(ComponentPlacement.RELATED)
.addComponent(cancelButton);
ParallelGroup vGroup = buttonLayout.createParallelGroup(Alignment.BASELINE);
vGroup.addComponent(okButton).addComponent(cancelButton);
......@@ -98,8 +98,7 @@ public class SeekerDialog extends JDialog implements ActionListener {
if (result != null && !result.isEmpty()) {
setVisible(false);
}
}
else if (e.getSource() == cancelButton) {
} else if (e.getSource() == cancelButton) {
setVisible(false);
}
}
......@@ -110,6 +109,7 @@ public class SeekerDialog extends JDialog implements ActionListener {
@Override
public void setVisible(final boolean visible) {
if (visible != isVisible()) {
if (seeker != null) {
if (visible) {
seeker.startSeeking();
......@@ -117,8 +117,8 @@ public class SeekerDialog extends JDialog implements ActionListener {
seeker.stopSeeking();
}
}
super.setVisible(visible);
}
}
}
......@@ -37,25 +37,24 @@ public abstract class AbstractCDMADataSourceSeeker extends AbstractFileDataSourc
@Override
protected String getFileDescription() {
return getCDMADataSourceBrowser().getFileFilterDescription();
CDMADataSourceBrowser dataSourceBrowser = getCDMADataSourceBrowser();
return dataSourceBrowser == null ? null : dataSourceBrowser.getFileFilterDescription();
}
@Override
public boolean acceptFile(final File file) {
boolean result = false;
if (dataSourceBrowser != null) {
result = getCDMADataSourceBrowser().canRead(file);
}
return result;
CDMADataSourceBrowser dataSourceBrowser = getCDMADataSourceBrowser();
return dataSourceBrowser == null ? false : dataSourceBrowser.canRead(file);
}
protected List<IKey> getFileChooserResult() {
List<IKey> result = null;
CDMADataSourceBrowser dataSourceBrowser = getCDMADataSourceBrowser();
if ((fileChooser != null) && (dataSourceBrowser != null)) {
File[] selectedFiles = fileChooser.getSelectedFiles();
result = new ArrayList<IKey>();
result = new ArrayList<>();
for (File selectedFile : selectedFiles) {
IKey key = getCDMADataSourceBrowser().getKeyFactory(CDMADataSourceBrowser.PHYSICAL_VIEW)
IKey key = dataSourceBrowser.getKeyFactory(CDMADataSourceBrowser.PHYSICAL_VIEW)
.generateKeySourceTitle(selectedFile.toURI());
result.add(key);
}
......
......@@ -80,7 +80,7 @@ public class CDMADataSourceSeeker extends AbstractCDMADataSourceSeeker implement
@Override
public List<IKey> getResult() {
List<IKey> result = new ArrayList<IKey>();
List<IKey> result = new ArrayList<>();
// get the logical results
if ((dataSourceBrowser != null) && getCDMADataSourceBrowser().isLogical()) {
......@@ -103,7 +103,7 @@ public class CDMADataSourceSeeker extends AbstractCDMADataSourceSeeker implement
}
protected List<IKey> getLogicalResult() {
List<IKey> result = new ArrayList<IKey>();
List<IKey> result = new ArrayList<>();
if ((logicalPanel != null) && (logicalPanel.getSelectedView() != null)) {
CDMAKeyFactory keyFactory = getCDMADataSourceBrowser().getKeyFactory(logicalPanel.getSelectedView());
List<URI> selectedURIs = logicalPanel.getSelectedURIs();
......
......@@ -13,35 +13,30 @@ import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import org.cdma.gui.databrowser.interfaces.IDataSourceSeeker;
import org.cdma.gui.databrowser.impl.AbstractFileDataSourceSeeker;
import fr.soleil.comete.tango.data.service.TangoDataSourceFactory;
import fr.soleil.comete.tango.data.service.TangoKey;
import fr.soleil.comete.tango.data.service.TangoKeyTool;
import fr.soleil.data.service.BasicKey;
import fr.soleil.data.service.IKey;
import fr.soleil.lib.project.swing.file.MultiExtFileFilter;
import fr.soleil.lib.project.file.FileUtils;
public class TangoDataSourceSeeker implements IDataSourceSeeker {
public class TangoDataSourceSeeker extends AbstractFileDataSourceSeeker {
public static final ResourceBundle MESSAGES = ResourceBundle
.getBundle("org.cdma.gui.databrowser.impl.tango.messages");
private static final String[] SUPPORTED_EXTENSIONS = new String[] { "txt" };
private static final FileFilter TANGO_FILE_FILTER = new MultiExtFileFilter(
MESSAGES.getString("ATKTuningFiles.filterDescription"), null, SUPPORTED_EXTENSIONS);
private static final String SUPPORTED_EXTENSION = "txt";
private static final String FILE_DESCRIPTION = MESSAGES.getString("ATKTuningFiles.filterDescription");
private JTabbedPane tabbedPane;
private JFileChooser fileChooser;
private DevicePanel devicePanel;
private String directory;
public TangoDataSourceSeeker() {
super(null);
}
@Override
......@@ -51,16 +46,7 @@ public class TangoDataSourceSeeker implements IDataSourceSeeker {
private void initPanel() {
// File Chooser creation
fileChooser = new JFileChooser();
fileChooser.setOpaque(true);
fileChooser.setBackground(UIManager.getColor("Panel.background"));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setFileFilter(TANGO_FILE_FILTER);
fileChooser.setControlButtonsAreShown(false);
if (directory != null) {
fileChooser.setCurrentDirectory(new File(directory));
}
initFileChooser();
// Device panel creation
devicePanel = new DevicePanel();
......@@ -70,31 +56,11 @@ public class TangoDataSourceSeeker implements IDataSourceSeeker {
tabbedPane.add(MESSAGES.getString("ATKTuningFiles.TabName"), fileChooser);
}
@Override
public void setDirectory(final String path) {
directory = path;
if ((fileChooser != null) && (directory != null)) {
fileChooser.setCurrentDirectory(new File(directory));
}
}
@Override
public String getDirectory() {
if (fileChooser != null) {
File file = fileChooser.getCurrentDirectory();
if (file != null) {
directory = file.getAbsolutePath();
}
}
return directory;
}
@Override
public void cleanComponent() {
if (tabbedPane != null) {
devicePanel.clearPanel();
fileChooser.setSelectedFiles(null);
cleanFileChooser();
}
}
......@@ -108,7 +74,7 @@ public class TangoDataSourceSeeker implements IDataSourceSeeker {
@Override
public List<IKey> getResult() {
List<IKey> result = new ArrayList<IKey>();
List<IKey> result = new ArrayList<>();
if (tabbedPane != null) {
File[] selectedFiles = fileChooser.getSelectedFiles();
......@@ -140,25 +106,25 @@ public class TangoDataSourceSeeker implements IDataSourceSeeker {
return result;
}
@Override
public void startSeeking() {
// nop
}
@Override
public void stopSeeking() {
// nop
}
public static IKey createKeyFromFile(final File file) {
BasicKey key = new BasicKey(TangoDataSourceFactory.SOURCE_PRODUCER_ID);
key.setInformationKey(file.getAbsolutePath());
// System.out.println("key " + key.getInformationKey() + "=>" + key.hashCode());
return key;
}
public static boolean canRead(final File file) {
return TANGO_FILE_FILTER.accept(file);
return (file != null) && file.canRead()
&& (file.isDirectory() || SUPPORTED_EXTENSION.equalsIgnoreCase(FileUtils.getExtension(file)));
}
@Override
public boolean acceptFile(File file) {
return canRead(file);
}
@Override
protected String getFileDescription() {
return FILE_DESCRIPTION;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment