📄 contentproposaladapter.java
字号:
Thread.sleep(POPUP_DELAY); } catch (InterruptedException e) { } if (!isValid()) { return; } getShell().getDisplay().syncExec(new Runnable() { public void run() { // Query the current selection since we have // been delayed IContentProposal p = getSelectedProposal(); if (p != null) { if (infoPopup == null) { infoPopup = new InfoPopupDialog( getShell()); infoPopup.open(); infoPopup.getShell() .addDisposeListener( new DisposeListener() { public void widgetDisposed( DisposeEvent event) { infoPopup = null; } }); } infoPopup.setContents(p.getDescription()); pendingDescriptionUpdate = false; } } }); } }; Thread t = new Thread(runnable); t.start(); } } /* * Accept the current proposal. */ private void acceptCurrentProposal() { // Close before accepting the proposal. // This is important so that the cursor position can be // properly restored at acceptance, which does not work without // focus on some controls. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=127108 IContentProposal proposal = getSelectedProposal(); close(); proposalAccepted(proposal); } /* * Request the proposals from the proposal provider, and recompute any * caches. Repopulate the popup if it is open. */ private void recomputeProposals(String filterText) { setProposals(getProposals(filterText)); } /* * In an async block, request the proposals. This is used when clients * are in the middle of processing an event that affects the widget * content. By using an async, we ensure that the widget content is up * to date with the event. */ private void asyncRecomputeProposals(final String filterText) { if (isValid()) { control.getDisplay().asyncExec(new Runnable() { public void run() { recordCursorPosition(); recomputeProposals(filterText); } }); } else { recomputeProposals(filterText); } } /* * Filter the provided list of content proposals according to the filter * text. */ private IContentProposal[] filterProposals( IContentProposal[] proposals, String filterString) { if (filterString.length() == 0) { return proposals; } // Check each string for a match. Use the string displayed to the // user, not the proposal content. ArrayList list = new ArrayList(); for (int i = 0; i < proposals.length; i++) { String string = getString(proposals[i]); if (string.length() >= filterString.length() && string.substring(0, filterString.length()) .equalsIgnoreCase(filterString)) { list.add(proposals[i]); } } return (IContentProposal[]) list.toArray(new IContentProposal[list .size()]); } Listener getTargetControlListener() { if (targetControlListener == null) { targetControlListener = new TargetControlListener(); } return targetControlListener; } } /** * Flag that controls the printing of debug info. */ public static final boolean DEBUG = false; /** * Indicates that a chosen proposal should be inserted into the field. */ public static final int PROPOSAL_INSERT = 1; /** * Indicates that a chosen proposal should replace the entire contents of * the field. */ public static final int PROPOSAL_REPLACE = 2; /** * Indicates that the contents of the control should not be modified when a * proposal is chosen. This is typically used when a client needs more * specialized behavior when a proposal is chosen. In this case, clients * typically register an IContentProposalListener so that they are notified * when a proposal is chosen. */ public static final int PROPOSAL_IGNORE = 3; /** * Indicates that there should be no filter applied as keys are typed in the * popup. */ public static final int FILTER_NONE = 1; /** * Indicates that a single character filter applies as keys are typed in the * popup. */ public static final int FILTER_CHARACTER = 2; /** * Indicates that a cumulative filter applies as keys are typed in the * popup. That is, each character typed will be added to the filter. */ public static final int FILTER_CUMULATIVE = 3; /* * Set to <code>true</code> to use a Table with SWT.VIRTUAL. This is a * workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=98585#c40 * The corresponding SWT bug is * https://bugs.eclipse.org/bugs/show_bug.cgi?id=90321 */ private static final boolean USE_VIRTUAL = !"motif".equals(SWT.getPlatform()); //$NON-NLS-1$ /* * The delay before showing a secondary popup. */ private static final int POPUP_DELAY = 750; /* * The character height hint for the popup. May be overridden by using * setInitialPopupSize. */ private static final int POPUP_CHAR_HEIGHT = 10; /* * The minimum pixel width for the popup. May be overridden by using * setInitialPopupSize. */ private static final int POPUP_MINIMUM_WIDTH = 300; /* * The pixel offset of the popup from the bottom corner of the control. */ private static final int POPUP_OFFSET = 3; /* * Empty string. */ private static final String EMPTY = ""; //$NON-NLS-1$ /* * The object that provides content proposals. */ private IContentProposalProvider proposalProvider; /* * A label provider used to display proposals in the popup, and to extract * Strings from non-String proposals. */ private ILabelProvider labelProvider; /* * The control for which content proposals are provided. */ private Control control; /* * The adapter used to extract the String contents from an arbitrary * control. */ private IControlContentAdapter controlContentAdapter; /* * The popup used to show proposals. */ private ContentProposalPopup popup; /* * The keystroke that signifies content proposals should be shown. */ private KeyStroke triggerKeyStroke; /* * The String containing characters that auto-activate the popup. */ private String autoActivateString; /* * Integer that indicates how an accepted proposal should affect the * control. One of PROPOSAL_IGNORE, PROPOSAL_INSERT, or PROPOSAL_REPLACE. * Default value is PROPOSAL_INSERT. */ private int proposalAcceptanceStyle = PROPOSAL_INSERT; /* * A boolean that indicates whether key events received while the proposal * popup is open should also be propagated to the control. Default value is * true. */ private boolean propagateKeys = true; /* * Integer that indicates the filtering style. One of FILTER_CHARACTER, * FILTER_CUMULATIVE, FILTER_NONE. */ private int filterStyle = FILTER_NONE; /* * The listener we install on the control. */ private Listener controlListener; /* * The list of listeners who wish to be notified when something significant * happens with the proposals. */ private ListenerList proposalListeners = new ListenerList(); /* * Flag that indicates whether the adapter is enabled. In some cases, * adapters may be installed but depend upon outside state. */ private boolean isEnabled = true; /* * The delay in milliseconds used when autoactivating the popup. */ private int autoActivationDelay = 0; /* * A boolean indicating whether a keystroke has been received. * Used to see if an autoactivation delay was interrupted by * a keystroke. */ private boolean receivedKeyDown; /* * The desired size in pixels of the proposal popup. */ private Point popupSize; /* * The remembered position of the insertion position. Not all controls will * restore the insertion position if the proposal popup gets focus, so we * need to remember it. */ private int insertionPos = -1; /** * Construct a content proposal adapter that can assist the user with * choosing content for the field. * * @param control * the control for which the adapter is providing content assist. * May not be <code>null</code>. * @param controlContentAdapter * the <code>IControlContentAdapter</code> used to obtain and * update the control's contents as proposals are accepted. May * not be <code>null</code>. * @param proposalProvider * the <code>IContentProposalProvider</code> used to obtain * content proposals for this control, or <code>null</code> if * no content proposal is available. * @param keyStroke * the keystroke that will invoke the content proposal popup. If * this value is <code>null</code>, then proposals will be * activated automatically when any of the auto activation * characters are typed. * @param autoActivationCharacters * An array of characters that trigger auto-activation of content * proposal. If specified, these characters will trigger * auto-activation of the proposal popup, regardless of whether * an explicit invocation keyStroke was specified. If this * parameter is <code>null</code>, then only a specified * keyStroke will invoke content proposal. If this parameter is * <code>null</code> and the keyStroke parameter is * <code>null</code>, then all alphanumeric characters will * auto-activate content proposal. */ public ContentProposalAdapter(Control control, IControlContentAdapter controlContentAdapter, IContentProposalProvider proposalProvider, KeyStroke keyStroke, char[] autoActivationCharacters) { super(); // We always assume the control and content adapter are valid. Assert.isNotNull(control); Assert.isNotNull(controlContentAdapter); this.control = control; this.controlContentAdapter = controlContentAdapter; // The rest of these may be null this.proposalProvider = proposalProvider; this.triggerKeyStroke = keyStroke; if (autoActivationCharacters != null) { this.autoActivateString = new String(autoActivationCharacters); } addControlListener(control); } /** * Get the control on which the content proposal adapter is installed. * * @return the control on which the proposal adapter is installed. */ public Control getControl() { return control; } /** * Get the label provider that is used to show proposals. * * @return the {@link ILabelProvider} used to show proposals, or * <code>null</code> if one has not been installed. */ public ILabelProvider getLabelProvider() { return labelProvider; } /** * Return a boolean indicating whether the receiver is enabled. * * @return <code>true</code> if the adapter is enabled, and * <code>false</code> if it is not. */ public boolean isEnabled() { return isEnabled; } /** * Set the label provider that is used to show proposals. The lifecycle of * the specified label provider is not managed by this adapter. Clients must * dispose the label provider when it is no longer needed. * * @param labelProvider * the (@link ILabelProvider} used to show proposals. */ public void setLabelProvider(ILabelProvider labelProvider) { this.labelProvider = labelProvider; } /** * Return the proposal provider that provides content proposals given the * current content of the field. A value of <code>null</code> indicates * that there are no content proposals available for the field. * * @return the {@link IContentProposalProvider} used to show proposals. May * be <code>null</code>. */ public IContentProposalProvider getContentProposalProvider() { return proposalProvider; } /** * Set the content proposal provider that is used to show proposals. * * @param proposalProvider * the {@link IContentProposalProvider} used to show proposals */ public void setContentProposalProvider( IContentProposalProvider proposalProvider) { this.proposalProvider = proposalProvider; } /** * Return the array of characters on which the popup is autoactivated. * * @return An array of characters that trigger auto-activation of content * proposal. If specified, these characters will trigger * auto-activation of the proposal popup, regardless of whether an * explicit invocation keyStroke was specified. If this parameter is * <code>null</code>, then only a specified keyStroke will invoke * content proposal. If this value is <code>null</code> and the * keyStroke value is <code>null</code>, then all alphanumeric * characters will auto-activate content proposal. */ public char[] getAutoActivationCharacters() { if (autoActivateString == null) { return null; } return autoActivateString.toCharArray(); } /** * Set the array of characters that will trigger autoactivation of the * popup. * * @param autoActivationCharacters * An array of characters that trigger auto-activation of content * proposal. If specified, these characters will trigger * auto-activation of the proposal popup, regardless of whether * an explicit invocation keyStroke was specified. If this * parameter is <code>null</code>, then only a specified * keyStroke will invoke content proposal. If this parameter is * <code>null</code> and the keyStroke value is * <code>null</code>, then all alphanumeric characters will * auto-activate content proposal. * */ public void setAutoActivationCharacters(char[] autoActivationCharacters) { this.autoActivateString = new String(autoActivationCharacters); } /** * Set the delay, in milliseconds, used before any autoactivation is * triggered. * * @return the time in milliseconds that will pass before a popup is * automatically opened */ public int getAutoActivationDelay() { return autoActivationDelay; } /** * Set the delay, in milliseconds, used before autoactivation is triggered. * * @param delay
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -