Search in sources :

Example 16 with Proposal

use of org.csstudio.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(final Composite parent) {
    Composite 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);
        Listener listener = new Listener() {

            @Override
            public void handleEvent(Event event) {
                handleSetData(event);
            }
        };
        proposalTable.addListener(SWT.SetData, listener);
        proposalTable.addListener(SWTPaintItem, new Listener() {

            @Override
            public void handleEvent(Event event) {
                TableItem item = (TableItem) event.item;
                int index = proposalTable.indexOf(item);
                if (textLayouts != null && index < textLayouts.length && textLayouts[index] != null) {
                    textLayouts[index].handlePaintItemEvent(event, 20, 2);
                }
                Proposal p = (Proposal) item.getData();
                Image image = getImage(p, index == proposalTable.getSelectionIndex());
                if (image != null)
                    event.gc.drawImage(image, event.x, event.y + 2);
            }
        });
        proposalTable.addListener(SWTMeasureItem, new Listener() {

            @Override
            public void handleEvent(Event event) {
                TableItem item = (TableItem) event.item;
                int 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);
    GridData 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, new Listener() {

        @Override
        public void handleEvent(Event 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 {
                Proposal 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) {
            Proposal 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) DisposeListener(org.eclipse.swt.events.DisposeListener) Listener(org.eclipse.swt.widgets.Listener) SelectionListener(org.eclipse.swt.events.SelectionListener) Composite(org.eclipse.swt.widgets.Composite) ControlAdapter(org.eclipse.swt.events.ControlAdapter) TableItem(org.eclipse.swt.widgets.TableItem) Text(org.eclipse.swt.widgets.Text) Image(org.eclipse.swt.graphics.Image) TableColumn(org.eclipse.swt.widgets.TableColumn) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) FocusEvent(org.eclipse.swt.events.FocusEvent) Event(org.eclipse.swt.widgets.Event) ControlEvent(org.eclipse.swt.events.ControlEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ControlEvent(org.eclipse.swt.events.ControlEvent) Proposal(org.csstudio.autocomplete.proposals.Proposal) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 17 with Proposal

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

the class DataSourceProvider method listResult.

@Override
public AutoCompleteResult listResult(final ContentDescriptor desc, final int limit) {
    AutoCompleteResult result = new AutoCompleteResult();
    for (String ds : dataSources) {
        if (ds.startsWith(desc.getValue())) {
            Proposal proposal = new Proposal(ds, true);
            proposal.addStyle(ProposalStyle.getDefault(0, desc.getValue().length() - 1));
            proposal.setInsertionPos(desc.getStartIndex());
            result.addTopProposal(proposal);
        }
    }
    return result;
}
Also used : AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Example 18 with Proposal

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

the class LocalContentProvider method listResult.

@Override
public AutoCompleteResult listResult(final ContentDescriptor desc, final int limit) {
    AutoCompleteResult 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
    int count = 0;
    if (locDesc.isCompletingVType() && locDesc.getvType() != null) {
        String type = locDesc.getvType();
        Proposal topProposal = null;
        String closestMatchingType = null;
        for (String vType : LocalContentDescriptor.listVTypes()) {
            if (vType.startsWith(type)) {
                String prefix = locDesc.getPvName() + LocalContentParser.VTYPE_START;
                if (desc.getDefaultDataSource() != LocalContentParser.LOCAL_SOURCE)
                    prefix = LocalContentParser.LOCAL_SOURCE + prefix;
                int offset = prefix.length();
                Proposal 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();
        // $NON-NLS-1$
        td.value = "pvname";
        String vType = locDesc.getvType();
        if (vType != null) {
            td.value += LocalContentParser.VTYPE_START + locDesc.getvType() + LocalContentParser.VTYPE_END;
        }
        td.value += LocalContentParser.INITIAL_VALUE_START;
        int start = td.value.length();
        td.value += locDesc.getInitialValueTooltip();
        int 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();
        // $NON-NLS-1$
        td.value = "pvname<type>";
        td.styles = new ProposalStyle[1];
        td.styles[0] = ProposalStyle.getDefault(6, 12);
        result.addTooltipData(td);
        td = new TooltipData();
        // $NON-NLS-1$
        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();
            // $NON-NLS-1$
            td.value = "pvname";
            td.styles = new ProposalStyle[1];
            td.styles[0] = ProposalStyle.getDefault(from, to);
            result.addTooltipData(td);
        }
        td = new TooltipData();
        // $NON-NLS-1$
        td.value = "pvname<type>";
        td.styles = new ProposalStyle[1];
        td.styles[0] = ProposalStyle.getDefault(from, to);
        result.addTooltipData(td);
        td = new TooltipData();
        // $NON-NLS-1$
        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.csstudio.autocomplete.AutoCompleteResult) ProposalStyle(org.csstudio.autocomplete.proposals.ProposalStyle) TooltipData(org.csstudio.autocomplete.tooltips.TooltipData) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Example 19 with Proposal

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

the class SysContentProvider method provideSystemProperties.

private AutoCompleteResult provideSystemProperties(final SysContentDescriptor sysDesc, final int limit) {
    AutoCompleteResult result = new AutoCompleteResult();
    int count = 0;
    int dotIndex = sysDesc.getValue().indexOf(SYSTEM_SEPARATOR);
    String propValue = sysDesc.getValue().substring(dotIndex + 1);
    String 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<String>();
    Properties systemProperties = System.getProperties();
    Enumeration<?> enuProp = systemProperties.propertyNames();
    int offset = SysContentParser.SYS_SOURCE.length() + 7;
    while (enuProp.hasMoreElements()) {
        String propertyName = (String) enuProp.nextElement();
        String propertyValue = systemProperties.getProperty(propertyName);
        Matcher m = valuePattern.matcher(propertyName);
        if (m.find()) {
            String propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + propertyName;
            if (sysDesc.getDefaultDataSource() != SysContentParser.SYS_SOURCE)
                propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
            Proposal 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
    TopProposalFinder tpf = new TopProposalFinder(SYSTEM_SEPARATOR);
    for (Proposal tp : tpf.getTopProposals(propValue, matchingProperties)) {
        String propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + tp.getValue();
        if (sysDesc.getDefaultDataSource() != SysContentParser.SYS_SOURCE)
            propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
        Proposal proposal = new Proposal(propDisplay, tp.isPartial());
        String propertyValue = systemProperties.getProperty(tp.getValue());
        proposal.setDescription(propertyValue);
        ProposalStyle 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.csstudio.autocomplete.proposals.TopProposalFinder) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) ProposalStyle(org.csstudio.autocomplete.proposals.ProposalStyle) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Aggregations

Proposal (org.csstudio.autocomplete.proposals.Proposal)19 AutoCompleteResult (org.csstudio.autocomplete.AutoCompleteResult)9 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 Point (org.eclipse.swt.graphics.Point)5 ArrayList (java.util.ArrayList)3 ProposalStyle (org.csstudio.autocomplete.proposals.ProposalStyle)3 Display (org.eclipse.swt.widgets.Display)3 TableItem (org.eclipse.swt.widgets.TableItem)3 FunctionDescriptor (org.csstudio.autocomplete.parser.FunctionDescriptor)2 TopProposalFinder (org.csstudio.autocomplete.proposals.TopProposalFinder)2 ControlEvent (org.eclipse.swt.events.ControlEvent)2 DisposeEvent (org.eclipse.swt.events.DisposeEvent)2 DisposeListener (org.eclipse.swt.events.DisposeListener)2 FocusEvent (org.eclipse.swt.events.FocusEvent)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 SelectionListener (org.eclipse.swt.events.SelectionListener)2 Color (org.eclipse.swt.graphics.Color)2 Font (org.eclipse.swt.graphics.Font)2 FontData (org.eclipse.swt.graphics.FontData)2