Search in sources :

Example 1 with Proposal

use of org.yamcs.studio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class ContentProposalPopup method createDialogArea.

/*
     * Creates the content area for the proposal popup. This creates a table and places it inside the composite. The
     * table will contain a list of all the proposals.
     *
     * @param parent The parent composite to contain the dialog area; must not be <code>null</code>.
     */
@Override
protected final Control createDialogArea(Composite parent) {
    var wrapper = (Composite) super.createDialogArea(parent);
    wrapper.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    wrapper.setLayout(new GridLayout());
    // Use virtual where appropriate (see flag definition).
    if (USE_VIRTUAL) {
        proposalTable = new Table(wrapper, SWT.H_SCROLL | SWT.V_SCROLL | SWT.VIRTUAL | SWT.NO_FOCUS);
        proposalTable.addListener(SWT.SetData, this::handleSetData);
        proposalTable.addListener(SWTPaintItem, event -> {
            var item = (TableItem) event.item;
            var index = proposalTable.indexOf(item);
            if (textLayouts != null && index < textLayouts.length && textLayouts[index] != null) {
                textLayouts[index].handlePaintItemEvent(event, 20, 2);
            }
            var p = (Proposal) item.getData();
            var image = getImage(p, index == proposalTable.getSelectionIndex());
            if (image != null) {
                event.gc.drawImage(image, event.x, event.y + 2);
            }
        });
        proposalTable.addListener(SWTMeasureItem, event -> {
            var item = (TableItem) event.item;
            var index = proposalTable.indexOf(item);
            if (textLayouts != null && index < textLayouts.length && textLayouts[index] != null) {
                textLayouts[index].handleMeasureItemEvent(event);
            }
        });
    } else {
        proposalTable = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.NO_FOCUS);
    }
    footer = new Text(wrapper, SWT.READ_ONLY | SWT.WRAP | SWT.NO_FOCUS);
    var textGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
    textGridData.heightHint = FOOTER_MINIMUM_HEIGHT;
    textGridData.widthHint = 100;
    footer.setLayoutData(textGridData);
    // set the proposals to force population of the table.
    setProposals(proposalList);
    proposalTable.setHeaderVisible(false);
    proposalTable.addListener(SWT.KeyDown, e -> getTargetControlListener().handleEvent(e));
    proposalTable.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // popup. Otherwise close the popup.
            if (e.item == null) {
                if (infoPopup != null) {
                    infoPopup.close();
                }
            } else {
                var proposal = (Proposal) e.item.getData();
                if (proposal != null) {
                    showProposalDescription();
                    adapter.proposalSelected(proposal);
                } else {
                    if (infoPopup != null) {
                        infoPopup.close();
                    }
                    proposalTable.deselectAll();
                }
            }
        }

        // Default selection was made. Accept the current proposal.
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            var proposal = (Proposal) e.item.getData();
            if (proposal != null) {
                acceptCurrentProposal(true);
            } else {
                proposalTable.deselectAll();
            }
        }
    });
    // Added to solve a item resize bug on windows:
    new TableColumn(proposalTable, SWT.NONE | SWT.NO_FOCUS);
    proposalTable.addControlListener(new ControlAdapter() {

        @Override
        public void controlResized(ControlEvent event) {
            if (proposalTable.getColumnCount() > 0) {
                if (proposalTable.getClientArea().width > maxItemWidth) {
                    proposalTable.getColumn(0).setWidth(proposalTable.getClientArea().width);
                } else {
                    proposalTable.getColumn(0).setWidth(maxItemWidth);
                }
            }
        }
    });
    return proposalTable;
}
Also used : Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ControlAdapter(org.eclipse.swt.events.ControlAdapter) TableItem(org.eclipse.swt.widgets.TableItem) Text(org.eclipse.swt.widgets.Text) TableColumn(org.eclipse.swt.widgets.TableColumn) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ControlEvent(org.eclipse.swt.events.ControlEvent) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 2 with Proposal

use of org.yamcs.studio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class LocalContentProvider method listResult.

@Override
public AutoCompleteResult listResult(ContentDescriptor desc, int limit) {
    var result = new AutoCompleteResult();
    LocalContentDescriptor locDesc = null;
    if (desc instanceof LocalContentDescriptor) {
        locDesc = (LocalContentDescriptor) desc;
    } else {
        // empty result
        return result;
    }
    if (locDesc.isComplete()) {
        // empty result
        return result;
    }
    // handle proposals
    var count = 0;
    if (locDesc.isCompletingVType() && locDesc.getvType() != null) {
        var type = locDesc.getvType();
        Proposal topProposal = null;
        String closestMatchingType = null;
        for (var vType : LocalContentDescriptor.listVTypes()) {
            if (vType.startsWith(type)) {
                var prefix = locDesc.getPvName() + LocalContentParser.VTYPE_START;
                prefix = LocalContentParser.LOCAL_SOURCE + prefix;
                var offset = prefix.length();
                var proposal = new Proposal(prefix + vType + LocalContentParser.VTYPE_END, false);
                proposal.setDescription(LocalContentDescriptor.getVTypeDescription(vType));
                proposal.addStyle(ProposalStyle.getDefault(0, offset + type.length() - 1));
                proposal.setInsertionPos(desc.getStartIndex());
                result.addProposal(proposal);
                count++;
                if (closestMatchingType == null || closestMatchingType.compareTo(vType) > 0) {
                    closestMatchingType = vType;
                    topProposal = proposal;
                }
            }
        }
        // handle top proposals
        if (closestMatchingType != null && !type.isEmpty()) {
            result.addTopProposal(topProposal);
        }
    }
    result.setCount(count);
    // handle tooltip
    TooltipData td = null;
    if (locDesc.isCompletingInitialValue()) {
        td = new TooltipData();
        td.value = "pvname";
        var vType = locDesc.getvType();
        if (vType != null) {
            td.value += LocalContentParser.VTYPE_START + locDesc.getvType() + LocalContentParser.VTYPE_END;
        }
        td.value += LocalContentParser.INITIAL_VALUE_START;
        var start = td.value.length();
        td.value += locDesc.getInitialValueTooltip();
        var end = td.value.length();
        td.value += LocalContentParser.INITIAL_VALUE_END;
        td.styles = new ProposalStyle[1];
        if (locDesc.checkParameters()) {
            td.styles[0] = ProposalStyle.getDefault(start, end);
        } else {
            td.styles[0] = ProposalStyle.getError(start, end);
        }
        result.addTooltipData(td);
    } else if (locDesc.isCompletingVType()) {
        td = new TooltipData();
        td.value = "pvname<type>";
        td.styles = new ProposalStyle[1];
        td.styles[0] = ProposalStyle.getDefault(6, 12);
        result.addTooltipData(td);
        td = new TooltipData();
        td.value = "pvname<type>(initialValue)";
        td.styles = new ProposalStyle[1];
        td.styles[0] = ProposalStyle.getDefault(6, 12);
        result.addTooltipData(td);
    } else {
        // bold <type>
        int from = 6, to = 12;
        if (locDesc.getvType() == null) {
            // bold pvname
            from = 0;
            to = 6;
            td = new TooltipData();
            td.value = "pvname";
            td.styles = new ProposalStyle[1];
            td.styles[0] = ProposalStyle.getDefault(from, to);
            result.addTooltipData(td);
        }
        td = new TooltipData();
        td.value = "pvname<type>";
        td.styles = new ProposalStyle[1];
        td.styles[0] = ProposalStyle.getDefault(from, to);
        result.addTooltipData(td);
        td = new TooltipData();
        td.value = "pvname<type>(initialValue)";
        td.styles = new ProposalStyle[1];
        td.styles[0] = ProposalStyle.getDefault(from, to);
        result.addTooltipData(td);
    }
    return result;
}
Also used : AutoCompleteResult(org.yamcs.studio.autocomplete.AutoCompleteResult) ProposalStyle(org.yamcs.studio.autocomplete.proposals.ProposalStyle) TooltipData(org.yamcs.studio.autocomplete.tooltips.TooltipData) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal)

Example 3 with Proposal

use of org.yamcs.studio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class AutoCompleteProposalProvider method getProposals.

@Override
public void getProposals(String contents, IContentProposalSearchHandler handler) {
    currentId = System.currentTimeMillis();
    synchronized (currentList) {
        currentList.clear();
        currentList.setOriginalValue(contents);
    }
    var cns = AutoCompleteService.getInstance();
    var expected = cns.get(currentId, AutoCompleteType.valueOf(type), contents, (uniqueId, index, result) -> {
        if (uniqueId == currentId) {
            synchronized (currentList) {
                currentList.responseReceived();
            }
            if (result == null) {
                return;
            }
            List<Proposal> contentProposals = new ArrayList<>();
            if (result.getProposals() != null) {
                contentProposals.addAll(result.getProposals());
            }
            var contentProposalsArray = contentProposals.toArray(new Proposal[contentProposals.size()]);
            List<Proposal> topContentProposals = new ArrayList<>();
            if (result.getTopProposals() != null) {
                topContentProposals.addAll(result.getTopProposals());
            }
            ContentProposalList cpl = null;
            synchronized (currentList) {
                if (result.getProvider() != null) {
                    currentList.addProposals(result.getProvider(), contentProposalsArray, result.getCount(), index);
                }
                currentList.addTopProposals(topContentProposals);
                cpl = currentList.clone();
            }
            handler.handleResult(cpl);
            handler.handleTooltips(result.getTooltips());
        // System.out.println("PROCESSED: " + uniqueId + ", " + index);
        }
    });
    currentList.setExpected(expected);
}
Also used : ContentProposalList(org.yamcs.studio.autocomplete.ui.content.ContentProposalList) ArrayList(java.util.ArrayList) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal)

Example 4 with Proposal

use of org.yamcs.studio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class StateContentProvider method provideFunctions.

private AutoCompleteResult provideFunctions(StateContentDescriptor stateDesc, int limit) {
    var result = new AutoCompleteResult();
    var count = 0;
    var regex = stateDesc.getValue();
    regex = regex.replaceAll("\\*", ".*");
    regex = regex.replaceAll("\\?", ".");
    Pattern valuePattern = null;
    try {
        // start with !
        valuePattern = Pattern.compile("^" + regex);
    } catch (Exception e) {
        // empty result
        return result;
    }
    Proposal topProposal = null;
    String closestMatchingFunction = null;
    var offset = StateContentParser.STATE_SOURCE.length();
    for (var function : StateContentDescriptor.listFunctions()) {
        var m = valuePattern.matcher(function);
        if (m.find()) {
            var fctDisplay = function;
            fctDisplay = StateContentParser.STATE_SOURCE + function;
            var proposal = new Proposal(fctDisplay, false);
            proposal.setDescription(StateContentDescriptor.getDescription(function));
            proposal.addStyle(ProposalStyle.getDefault(0, offset + m.end() - 1));
            proposal.setInsertionPos(stateDesc.getStartIndex());
            if (count <= limit) {
                result.addProposal(proposal);
            }
            count++;
            if (closestMatchingFunction == null || closestMatchingFunction.compareTo(function) > 0) {
                closestMatchingFunction = function;
                topProposal = proposal;
            }
        }
    }
    // handle top proposals
    if (closestMatchingFunction != null) {
        result.addTopProposal(topProposal);
    }
    result.setCount(count);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) AutoCompleteResult(org.yamcs.studio.autocomplete.AutoCompleteResult) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal)

Example 5 with Proposal

use of org.yamcs.studio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class SysContentProvider method provideSystemProperties.

private AutoCompleteResult provideSystemProperties(SysContentDescriptor sysDesc, int limit) {
    var result = new AutoCompleteResult();
    var count = 0;
    var dotIndex = sysDesc.getValue().indexOf(SYSTEM_SEPARATOR);
    var propValue = sysDesc.getValue().substring(dotIndex + 1);
    var regex = propValue.replaceAll("\\.", "\\\\.");
    regex = regex.replaceAll("\\*", ".*");
    regex = regex.replaceAll("\\?", ".");
    Pattern valuePattern = null;
    try {
        // start with !
        valuePattern = Pattern.compile("^" + regex);
    } catch (Exception e) {
        // empty result
        return result;
    }
    List<String> matchingProperties = new ArrayList<>();
    var systemProperties = System.getProperties();
    Enumeration<?> enuProp = systemProperties.propertyNames();
    var offset = SysContentParser.SYS_SOURCE.length() + 7;
    while (enuProp.hasMoreElements()) {
        var propertyName = (String) enuProp.nextElement();
        var propertyValue = systemProperties.getProperty(propertyName);
        var m = valuePattern.matcher(propertyName);
        if (m.find()) {
            var propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + propertyName;
            propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
            var proposal = new Proposal(propDisplay, false);
            proposal.setDescription(propertyValue);
            proposal.addStyle(ProposalStyle.getDefault(0, offset + m.end() - 1));
            proposal.setInsertionPos(sysDesc.getStartIndex());
            if (count <= limit) {
                result.addProposal(proposal);
            }
            matchingProperties.add(propertyName);
            count++;
        }
    }
    // handle top proposals
    var tpf = new TopProposalFinder(SYSTEM_SEPARATOR);
    for (var tp : tpf.getTopProposals(propValue, matchingProperties)) {
        var propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + tp.getValue();
        propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
        var proposal = new Proposal(propDisplay, tp.isPartial());
        var propertyValue = systemProperties.getProperty(tp.getValue());
        proposal.setDescription(propertyValue);
        var tpStyle = tp.getStyles().get(0);
        proposal.addStyle(ProposalStyle.getDefault(tpStyle.from, (offset + tpStyle.to)));
        proposal.setInsertionPos(sysDesc.getStartIndex());
        result.addTopProposal(proposal);
    }
    result.setCount(count);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) TopProposalFinder(org.yamcs.studio.autocomplete.proposals.TopProposalFinder) AutoCompleteResult(org.yamcs.studio.autocomplete.AutoCompleteResult) ArrayList(java.util.ArrayList) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal)

Aggregations

Proposal (org.yamcs.studio.autocomplete.proposals.Proposal)9 AutoCompleteResult (org.yamcs.studio.autocomplete.AutoCompleteResult)7 Pattern (java.util.regex.Pattern)4 ArrayList (java.util.ArrayList)3 FunctionDescriptor (org.yamcs.studio.autocomplete.parser.FunctionDescriptor)2 ControlAdapter (org.eclipse.swt.events.ControlAdapter)1 ControlEvent (org.eclipse.swt.events.ControlEvent)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 SelectionListener (org.eclipse.swt.events.SelectionListener)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Composite (org.eclipse.swt.widgets.Composite)1 Table (org.eclipse.swt.widgets.Table)1 TableColumn (org.eclipse.swt.widgets.TableColumn)1 TableItem (org.eclipse.swt.widgets.TableItem)1 Text (org.eclipse.swt.widgets.Text)1 ProposalStyle (org.yamcs.studio.autocomplete.proposals.ProposalStyle)1 TopProposalFinder (org.yamcs.studio.autocomplete.proposals.TopProposalFinder)1 TooltipData (org.yamcs.studio.autocomplete.tooltips.TooltipData)1 ContentProposalList (org.yamcs.studio.autocomplete.ui.content.ContentProposalList)1