Search in sources :

Example 1 with Source

use of org.csstudio.trends.databrowser3.export.Source in project org.csstudio.display.builder by kasemir.

the class AddModelItemCommand method forPV.

/**
 * Create PV via undo-able AddModelItemCommand,
 *  displaying errors in dialog
 *  @param shell Shell used for error dialogs
 *  @param operations_manager OperationsManager where command will be reg'ed
 *  @param model Model were PV is to be added
 *  @param pv_name Name of new PV
 *  @param period scan period
 *  @param axis Axis
 *  @param archive Archive data source
 *  @return AddModelItemCommand or <code>null</code> on error
 */
public static Optional<AddModelItemCommand> forPV(final Shell shell, final UndoableActionManager operations_manager, final Model model, final String pv_name, final double period, final AxisConfig axis, final ArchiveDataSource archive) {
    // Create item
    final PVItem item;
    try {
        item = new PVItem(pv_name, period);
        if (archive != null)
            item.addArchiveDataSource(archive);
        else
            item.useDefaultArchiveDataSources();
        axis.setVisible(true);
        item.setAxis(axis);
    } catch (Exception ex) {
        MessageDialog.openError(shell, Messages.Error, NLS.bind(Messages.AddItemErrorFmt, pv_name, ex.getMessage()));
        return Optional.empty();
    }
    // Add to model via undo-able command
    return Optional.of(new AddModelItemCommand(shell, operations_manager, model, item));
}
Also used : PVItem(org.csstudio.trends.databrowser3.model.PVItem)

Example 2 with Source

use of org.csstudio.trends.databrowser3.export.Source in project org.csstudio.display.builder by kasemir.

the class Preferences method getArchives.

public static ArchiveDataSource[] getArchives() {
    final ArrayList<ArchiveDataSource> archives = new ArrayList<ArchiveDataSource>();
    final IPreferencesService prefs = Platform.getPreferencesService();
    final String urls = prefs.getString(Activator.PLUGIN_ID, ARCHIVES, "", null);
    // data source specs are separated by '*'
    final String[] specs = urls.split(ITEM_SEPARATOR_RE);
    for (String spec : specs) {
        // Each spec is "<name>|<key>|<url>"
        if (spec.length() <= 0)
            continue;
        try {
            final String[] segs = spec.split(COMPONENT_SEPARATOR_RE);
            final String name = segs[0];
            final int key = Integer.parseInt(segs[1]);
            final String url = segs[2];
            archives.add(new ArchiveDataSource(url, key, name));
        } catch (Throwable ex) {
            throw new Error("Error in archive preference '" + spec + "'");
        }
    }
    return archives.toArray(new ArchiveDataSource[archives.size()]);
}
Also used : ArrayList(java.util.ArrayList) ArchiveDataSource(org.csstudio.trends.databrowser3.model.ArchiveDataSource) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService)

Example 3 with Source

use of org.csstudio.trends.databrowser3.export.Source in project org.csstudio.display.builder by kasemir.

the class ExportJob method createValueIterator.

/**
 * @param item ModelItem
 *  @return ValueIterator for samples in the item
 *  @throws Exception on error
 */
protected ValueIterator createValueIterator(final ModelItem item) throws Exception {
    if (source == Source.PLOT || !(item instanceof PVItem))
        return new ModelSampleIterator(item, start, end);
    // Start ValueIterator for each sub-archive
    final ArchiveDataSource[] archives = ((PVItem) item).getArchiveDataSources();
    final List<ValueIterator> iters = new ArrayList<>();
    Exception error = null;
    for (ArchiveDataSource archive : archives) {
        // Create reader, remember to close it when done
        final ArchiveReader reader = ArchiveRepository.getInstance().getArchiveReader(archive.getUrl());
        archive_readers.add(reader);
        // Create ValueIterator
        try {
            ValueIterator iter;
            if (source == Source.OPTIMIZED_ARCHIVE && optimize_parameter > 1)
                iter = reader.getOptimizedValues(archive.getKey(), item.getName(), start, end, (int) optimize_parameter);
            else {
                iter = reader.getRawValues(archive.getKey(), item.getName(), start, end);
                if (source == Source.LINEAR_INTERPOLATION && optimize_parameter >= 1)
                    iter = new LinearValueIterator(iter, TimeDuration.ofSeconds(optimize_parameter));
            }
            iters.add(iter);
        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.FINE, "Export error for " + item.getName(), ex);
            if (error == null)
                error = ex;
        }
    }
    // If none of the iterators work out, report the first error that we found
    if (iters.isEmpty() && error != null)
        throw error;
    // Return a merging iterator
    return new MergingValueIterator(iters.toArray(new ValueIterator[iters.size()]));
}
Also used : ArchiveReader(org.csstudio.archive.reader.ArchiveReader) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArchiveDataSource(org.csstudio.trends.databrowser3.model.ArchiveDataSource) ValueIterator(org.csstudio.archive.reader.ValueIterator) MergingValueIterator(org.csstudio.archive.reader.MergingValueIterator) LinearValueIterator(org.csstudio.archive.reader.LinearValueIterator) MergingValueIterator(org.csstudio.archive.reader.MergingValueIterator) PVItem(org.csstudio.trends.databrowser3.model.PVItem) LinearValueIterator(org.csstudio.archive.reader.LinearValueIterator)

Example 4 with Source

use of org.csstudio.trends.databrowser3.export.Source in project org.csstudio.display.builder by kasemir.

the class ExportView method startExportJob.

/**
 * Start export job with parameters from GUI
 */
private void startExportJob() {
    if (model == null)
        return;
    // Determine start/end time
    final Instant start_time, end_time;
    if (use_plot_times.getSelection()) {
        start_time = model.getStartTime();
        end_time = model.getEndTime();
    } else {
        try {
            final StartEndTimeParser times = new StartEndTimeParser(start.getText(), end.getText());
            start_time = times.getStart().toInstant();
            end_time = times.getEnd().toInstant();
        } catch (final Exception ex) {
            handleExportError(ex);
            return;
        }
    }
    // Determine source: Plot, archive, ...
    final Source source;
    int optimize_parameter = -1;
    if (source_raw.getSelection())
        source = Source.RAW_ARCHIVE;
    else if (source_opt.getSelection()) {
        source = Source.OPTIMIZED_ARCHIVE;
        try {
            optimize_parameter = Integer.parseInt(optimize.getText());
        } catch (Exception ex) {
            MessageDialog.openError(optimize.getShell(), Messages.Error, Messages.ExportOptimizeCountError);
            return;
        }
    } else if (source_lin.getSelection()) {
        source = Source.LINEAR_INTERPOLATION;
        try {
            optimize_parameter = (int) (SecondsParser.parseSeconds(linear.getText()) + 0.5);
            if (optimize_parameter < 1)
                optimize_parameter = 1;
        } catch (Exception ex) {
            MessageDialog.openError(linear.getShell(), Messages.Error, Messages.ExportLinearIntervalError);
            return;
        }
    } else
        source = Source.PLOT;
    // Get remaining export parameters
    final String filename = this.filename.getText().trim();
    if (filename.equalsIgnoreCase(Messages.ExportDefaultFilename)) {
        MessageDialog.openConfirm(optimize.getShell(), Messages.Error, Messages.ExportEnterFilenameError);
        this.filename.setFocus();
        return;
    }
    if (new File(filename).exists()) {
        if (!MessageDialog.openConfirm(optimize.getShell(), Messages.ExportFileExists, NLS.bind(Messages.ExportFileExistsFmt, filename)))
            return;
    }
    // Construct appropriate export job
    final Job export;
    if (type_matlab.getSelection()) {
        if (// $NON-NLS-1$
        filename.endsWith(".m"))
            export = new MatlabScriptExportJob(model, start_time, end_time, source, optimize_parameter, filename, this);
        else if (// $NON-NLS-1$
        filename.endsWith(".mat"))
            export = new MatlabFileExportJob(model, start_time, end_time, source, optimize_parameter, filename, this);
        else {
            MessageDialog.openError(type_matlab.getShell(), Messages.Error, Messages.ExportMatlabFilenameError);
            return;
        }
    } else {
        // Spreadsheet file export
        final Style style;
        if (format_decimal.getSelection())
            style = Style.Decimal;
        else if (format_expo.getSelection())
            style = Style.Exponential;
        else
            style = Style.Default;
        final int precision;
        if (style == Style.Default)
            // Not used
            precision = 0;
        else {
            try {
                precision = Integer.parseInt(format_digits.getText().trim());
            } catch (Exception ex) {
                MessageDialog.openError(optimize.getShell(), Messages.Error, Messages.ExportDigitsError);
                return;
            }
        }
        final ValueFormatter formatter;
        if (sev_stat.getSelection())
            formatter = new ValueWithInfoFormatter(style, precision);
        else
            formatter = new ValueFormatter(style, precision);
        formatter.useMinMaxColumn(minMaxAllowed() && min_max_col.getSelection());
        if (tabular.getSelection())
            export = new SpreadsheetExportJob(model, start_time, end_time, source, optimize_parameter, formatter, filename, this);
        else
            export = new PlainExportJob(model, start_time, end_time, source, optimize_parameter, formatter, filename, this);
    }
    export.schedule();
}
Also used : Instant(java.time.Instant) MatlabScriptExportJob(org.csstudio.trends.databrowser3.export.MatlabScriptExportJob) Source(org.csstudio.trends.databrowser3.export.Source) StartEndTimeParser(org.csstudio.apputil.time.StartEndTimeParser) SpreadsheetExportJob(org.csstudio.trends.databrowser3.export.SpreadsheetExportJob) MatlabFileExportJob(org.csstudio.trends.databrowser3.export.MatlabFileExportJob) Style(org.csstudio.archive.vtype.Style) ValueWithInfoFormatter(org.csstudio.trends.databrowser3.export.ValueWithInfoFormatter) PlainExportJob(org.csstudio.trends.databrowser3.export.PlainExportJob) SpreadsheetExportJob(org.csstudio.trends.databrowser3.export.SpreadsheetExportJob) MatlabScriptExportJob(org.csstudio.trends.databrowser3.export.MatlabScriptExportJob) Job(org.eclipse.core.runtime.jobs.Job) MatlabFileExportJob(org.csstudio.trends.databrowser3.export.MatlabFileExportJob) ValueFormatter(org.csstudio.trends.databrowser3.export.ValueFormatter) File(java.io.File) PlainExportJob(org.csstudio.trends.databrowser3.export.PlainExportJob)

Example 5 with Source

use of org.csstudio.trends.databrowser3.export.Source in project org.csstudio.display.builder by kasemir.

the class DataBrowserEditor method hookDragAndDrop.

/**
 * Allow 'drop' of PV name, archive, ..
 *  @param canvas FXCanvas
 */
private void hookDragAndDrop(final Control canvas) {
    // CS-Studio uses the ControlSystemDragSource
    // and ControlSystemDropTarget to transfer its own
    // data types, using the SWT drag-and-drop API
    // plus some class loader hacking to de-serialize
    // types from other plugins.
    // 
    // The JavaFX plot is inside an FXCanvas.
    // The FXCanvas establishes an SWT DropTarget
    // to forward drops from SWT to the hosted JavaFX scene.
    // 
    // Issues:
    // The FXCanvas only reliably forwards standard transfers
    // (string, ..), and not custom data types.
    // SWT generally doesn't allow multiple drop targets.
    // 
    // Option 1)
    // Use the JavaFX drop API on the plot, and parse the received strings.
    // -> Don't want to parse possibly arbitrary strings.
    // 
    // Option 2)
    // Change the ControlSystemDropTarget to use
    // getSite().getService(IDragAndDropService.class).addMergedDropTarget(..)
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=162192
    // This would allow 'adding' another DropTarget,
    // but requires changes to the ControlSystemDropTarget,
    // would only work in RCP, not plain SWT.
    // Who then receives the drop events would depend on the order
    // in which drop targets are added and which transfers they use.
    // 
    // Option 3)
    // Hack around the SWT limitation by fetching
    // the FXCanva's DropTarget via
    // control.getData(DND.DROP_TARGET_KEY);
    // and disposing it.
    // Checking the source code for the SWT DropTarget
    // on Windows, Mac and Linux for SWT 3.104,
    // this detaches the DropTarget and allows us
    // to add our own.
    final DropTarget old = (DropTarget) canvas.getData(DND.DROP_TARGET_KEY);
    if (old != null)
        old.dispose();
    if (canvas.getData(DND.DROP_TARGET_KEY) != null)
        throw new IllegalStateException("Cannot remove FXCanvas's DropTarget");
    // Allow dropped arrays
    new ControlSystemDropTarget(canvas, ChannelInfo[].class, ProcessVariable[].class, ArchiveDataSource[].class, File.class, String.class) {

        @Override
        public void handleDrop(final Object item) {
            final PlotListener lst = plot.getListener();
            if (lst == null)
                return;
            if (item instanceof ChannelInfo[]) {
                final ChannelInfo[] channels = (ChannelInfo[]) item;
                final int N = channels.length;
                final ProcessVariable[] pvs = new ProcessVariable[N];
                final ArchiveDataSource[] archives = new ArchiveDataSource[N];
                for (int i = 0; i < N; ++i) {
                    pvs[i] = channels[i].getProcessVariable();
                    archives[i] = channels[i].getArchiveDataSource();
                }
                lst.droppedPVNames(pvs, archives);
            } else if (item instanceof ProcessVariable[]) {
                final ProcessVariable[] pvs = (ProcessVariable[]) item;
                lst.droppedPVNames(pvs, null);
            } else if (item instanceof ArchiveDataSource[]) {
                final ArchiveDataSource[] archives = (ArchiveDataSource[]) item;
                lst.droppedPVNames(null, archives);
            } else if (item instanceof String) {
                final List<String> pvs = new ArrayList<>();
                // Allow passing in many names, assuming that white space separates them
                // $NON-NLS-1$
                final String[] names = ((String) item).split("[\\r\\n\\t ]+");
                for (String one_name : names) {
                    // Might also have received "[pv1, pv2, pv2]", turn that into "pv1", "pv2", "pv3"
                    String suggestion = one_name;
                    if (suggestion.startsWith("["))
                        suggestion = suggestion.substring(1);
                    if (suggestion.endsWith("]") && !suggestion.contains("["))
                        suggestion = suggestion.substring(0, suggestion.length() - 1);
                    if (suggestion.endsWith(","))
                        suggestion = suggestion.substring(0, suggestion.length() - 1);
                    pvs.add(suggestion);
                }
                if (pvs.size() > 0)
                    lst.droppedNames(pvs.toArray(new String[pvs.size()]));
            } else if (item instanceof String[]) {
                // File names arrive as String[]...
                final String[] files = (String[]) item;
                try {
                    for (String filename : files) lst.droppedFilename(filename);
                } catch (Exception ex) {
                    ExceptionDetailsErrorDialog.openError(canvas.getShell(), Messages.Error, ex);
                }
            }
        }
    };
}
Also used : PlotListener(org.csstudio.trends.databrowser3.ui.PlotListener) ProcessVariable(org.csstudio.csdata.ProcessVariable) ArchiveDataSource(org.csstudio.trends.databrowser3.model.ArchiveDataSource) ChannelInfo(org.csstudio.trends.databrowser3.model.ChannelInfo) ControlSystemDropTarget(org.csstudio.ui.util.dnd.ControlSystemDropTarget) PartInitException(org.eclipse.ui.PartInitException) List(java.util.List) ArrayList(java.util.ArrayList) ControlSystemDropTarget(org.csstudio.ui.util.dnd.ControlSystemDropTarget) DropTarget(org.eclipse.swt.dnd.DropTarget)

Aggregations

ArrayList (java.util.ArrayList)4 ArchiveDataSource (org.csstudio.trends.databrowser3.model.ArchiveDataSource)4 PVItem (org.csstudio.trends.databrowser3.model.PVItem)4 File (java.io.File)1 Instant (java.time.Instant)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 StartEndTimeParser (org.csstudio.apputil.time.StartEndTimeParser)1 ArchiveReader (org.csstudio.archive.reader.ArchiveReader)1 LinearValueIterator (org.csstudio.archive.reader.LinearValueIterator)1 MergingValueIterator (org.csstudio.archive.reader.MergingValueIterator)1 ValueIterator (org.csstudio.archive.reader.ValueIterator)1 Style (org.csstudio.archive.vtype.Style)1 ProcessVariable (org.csstudio.csdata.ProcessVariable)1 ArchiveFetchJob (org.csstudio.trends.databrowser3.archive.ArchiveFetchJob)1 MatlabFileExportJob (org.csstudio.trends.databrowser3.export.MatlabFileExportJob)1 MatlabScriptExportJob (org.csstudio.trends.databrowser3.export.MatlabScriptExportJob)1 PlainExportJob (org.csstudio.trends.databrowser3.export.PlainExportJob)1 Source (org.csstudio.trends.databrowser3.export.Source)1 SpreadsheetExportJob (org.csstudio.trends.databrowser3.export.SpreadsheetExportJob)1