📄 contentproposaladapter.java
字号:
* the time in milliseconds that will pass before a popup is * automatically opened */ public void setAutoActivationDelay(int delay) { autoActivationDelay = delay; } /** * Get the integer style that indicates how an accepted proposal affects the * control's content. * * @return a constant indicating how an accepted proposal should affect the * control's content. Should be one of <code>PROPOSAL_INSERT</code>, * <code>PROPOSAL_REPLACE</code>, or <code>PROPOSAL_IGNORE</code>. * (Default is <code>PROPOSAL_INSERT</code>). */ public int getProposalAcceptanceStyle() { return proposalAcceptanceStyle; } /** * Set the integer style that indicates how an accepted proposal affects the * control's content. * * @param acceptance * a constant indicating how an accepted proposal should affect * the control's content. Should be one of * <code>PROPOSAL_INSERT</code>, <code>PROPOSAL_REPLACE</code>, * or <code>PROPOSAL_IGNORE</code> */ public void setProposalAcceptanceStyle(int acceptance) { proposalAcceptanceStyle = acceptance; } /** * Return the integer style that indicates how keystrokes affect the content * of the proposal popup while it is open. * * @return a constant indicating how keystrokes in the proposal popup affect * filtering of the proposals shown. <code>FILTER_NONE</code> * specifies that no filtering will occur in the content proposal * list as keys are typed. <code>FILTER_CUMULATIVE</code> * specifies that the content of the popup will be filtered by a * string containing all the characters typed since the popup has * been open. <code>FILTER_CHARACTER</code> specifies the content * of the popup will be filtered by the most recently typed * character. The default is <code>FILTER_NONE</code>. */ public int getFilterStyle() { return filterStyle; } /** * Set the integer style that indicates how keystrokes affect the content of * the proposal popup while it is open. * * @param filterStyle * a constant indicating how keystrokes in the proposal popup * affect filtering of the proposals shown. * <code>FILTER_NONE</code> specifies that no filtering will * occur in the content proposal list as keys are typed. * <code>FILTER_CUMULATIVE</code> specifies that the content of * the popup will be filtered by a string containing all the * characters typed since the popup has been open. * <code>FILTER_CHARACTER</code> specifies the content of the * popup will be filtered by the most recently typed character. */ public void setFilterStyle(int filterStyle) { this.filterStyle = filterStyle; } /** * Return the size, in pixels, of the content proposal popup. * * @return a Point specifying the last width and height, in pixels, of the * content proposal popup. */ public Point getPopupSize() { return popupSize; } /** * Set the size, in pixels, of the content proposal popup. This size will be * used the next time the content proposal popup is opened. * * @param size * a Point specifying the desired width and height, in pixels, of * the content proposal popup. */ public void setPopupSize(Point size) { popupSize = size; } /** * Get the boolean that indicates whether key events (including * auto-activation characters) received by the content proposal popup should * also be propagated to the adapted control when the proposal popup is * open. * * @return a boolean that indicates whether key events (including * auto-activation characters) should be propagated to the adapted * control when the proposal popup is open. Default value is * <code>true</code>. */ public boolean getPropagateKeys() { return propagateKeys; } /** * Set the boolean that indicates whether key events (including * auto-activation characters) received by the content proposal popup should * also be propagated to the adapted control when the proposal popup is * open. * * @param propagateKeys * a boolean that indicates whether key events (including * auto-activation characters) should be propagated to the * adapted control when the proposal popup is open. */ public void setPropagateKeys(boolean propagateKeys) { this.propagateKeys = propagateKeys; } /** * Return the content adapter that can get or retrieve the text contents * from the adapter's control. This method is used when a client, such as a * content proposal listener, needs to update the control's contents * manually. * * @return the {@link IControlContentAdapter} which can update the control * text. */ public IControlContentAdapter getControlContentAdapter() { return controlContentAdapter; } /** * Set the boolean flag that determines whether the adapter is enabled. * * @param enabled * <code>true</code> if the adapter is enabled and responding * to user input, <code>false</code> if it is ignoring user * input. * */ public void setEnabled(boolean enabled) { // If we are disabling it while it's proposing content, close the // content proposal popup. if (isEnabled && !enabled) { if (popup != null) { popup.close(); } } isEnabled = enabled; } /** * Add the specified listener to the list of content proposal listeners that * are notified when content proposals are chosen. * </p> * * @param listener * the IContentProposalListener to be added as a listener. Must * not be <code>null</code>. If an attempt is made to register * an instance which is already registered with this instance, * this method has no effect. * * @see org.eclipse.jface.fieldassist.IContentProposalListener */ public void addContentProposalListener(IContentProposalListener listener) { proposalListeners.add(listener); } /* * Add our listener to the control. Debug information to be left in until * this support is stable on all platforms. */ private void addControlListener(Control control) { if (DEBUG) { System.out .println("ContentProposalListener#installControlListener()"); //$NON-NLS-1$ } if (controlListener != null) { return; } controlListener = new Listener() { public void handleEvent(Event e) { if (!isEnabled) { return; } switch (e.type) { case SWT.Traverse: case SWT.KeyDown: if (DEBUG) { StringBuffer sb; if (e.type == SWT.Traverse) { sb = new StringBuffer("Traverse"); //$NON-NLS-1$ } else { sb = new StringBuffer("KeyDown"); //$NON-NLS-1$ } sb.append(" received by adapter"); //$NON-NLS-1$ dump(sb.toString(), e); } // If the popup is open, it gets first shot at the // keystroke and should set the doit flags appropriately. if (popup != null) { popup.getTargetControlListener().handleEvent(e); if (DEBUG) { StringBuffer sb; if (e.type == SWT.Traverse) { sb = new StringBuffer("Traverse"); //$NON-NLS-1$ } else { sb = new StringBuffer("KeyDown"); //$NON-NLS-1$ } sb.append(" after being handled by popup"); //$NON-NLS-1$ dump(sb.toString(), e); } return; } // We were only listening to traverse events for the popup if (e.type == SWT.Traverse) { return; } // The popup is not open. We are looking at keydown events // for a trigger to open the popup. if (triggerKeyStroke != null) { // Either there are no modifiers for the trigger and we // check the character field... if ((triggerKeyStroke.getModifierKeys() == KeyStroke.NO_KEY && triggerKeyStroke .getNaturalKey() == e.character) || // ...or there are modifiers, in which case the // keycode and state must match (triggerKeyStroke.getNaturalKey() == e.keyCode && ((triggerKeyStroke .getModifierKeys() & e.stateMask) == triggerKeyStroke .getModifierKeys()))) { // We never propagate the keystroke for an explicit // keystroke invocation of the popup e.doit = false; openProposalPopup(); return; } } /* * The triggering keystroke was not invoked. Check for * autoactivation characters. */ if (e.character != 0) { boolean autoActivated = false; // Auto-activation characters were specified. Check // them. if (autoActivateString != null) { if (autoActivateString.indexOf(e.character) >= 0) { autoActivated = true; } // Auto-activation characters were not specified. If // there was no key stroke specified, assume // activation for alphanumeric characters. } else if (triggerKeyStroke == null && Character.isLetterOrDigit(e.character)) { autoActivated = true; } /* * When autoactivating, we check the autoactivation * delay. */ if (autoActivated) { e.doit = propagateKeys; if (autoActivationDelay > 0) { Runnable runnable = new Runnable() { public void run() { receivedKeyDown = false; try { Thread.sleep(autoActivationDelay); } catch (InterruptedException e) { } if (!isValid() || receivedKeyDown) { return; } getControl().getDisplay().syncExec( new Runnable() { public void run() { openProposalPopup(); } }); } }; Thread t = new Thread(runnable); t.start(); } else { // Since we do not sleep, we must open the popup // in an async exec. This is necessary because // the cursor position and other important info // changes as a result of this event occurring. getControl().getDisplay().asyncExec( new Runnable() { public void run() { if (isValid()) { openProposalPopup(); } } }); } } else { // No autoactivation occurred, so record the key down // as a means to interrupt any autoactivation that is // pending. receivedKeyDown = true; } } break; default: break; } } /** * Dump the given events to "standard" output. * * @param who * who is dumping the event * @param e * the event */ private void dump(String who, Event e) { StringBuffer sb = new StringBuffer( "--- [ContentProposalAdapter]\n"); //$NON-NLS-1$ sb.append(who); sb.append(" - e: keyCode=" + e.keyCode + hex(e.keyCode)); //$NON-NLS-1$ sb.append("; character=" + e.character + hex(e.character)); //$NON-NLS-1$ sb.append("; stateMask=" + e.stateMask + hex(e.stateMask)); //$NON-NLS-1$ sb.append("; doit=" + e.doit); //$NON-NLS-1$ sb.append("; detail=" + e.detail + hex(e.detail)); //$NON-NLS-1$ sb.append("; widget=" + e.widget); //$NON-NLS-1$ System.out.println(sb); } private String hex(int i) { return "[0x" + Integer.toHexString(i) + ']'; //$NON-NLS-1$ } }; control.addListener(SWT.KeyDown, controlListener); control.addListener(SWT.Traverse, controlListener); if (DEBUG) { System.out .println("ContentProposalAdapter#installControlListener() - installed"); //$NON-NLS-1$ } } /** * Open the proposal popup and display the proposals provided by the * proposal provider. This method returns immediately. That is, it does not * wait for a proposal to be selected. */ protected void openProposalPopup() { if (isValid()) { if (popup == null) { recordCursorPosition(); popup = new ContentProposalPopup(null); popup.open(); popup.getShell().addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent event) { popup = null; } }); } } } /* * A content proposal has been accepted. Update the control contents * accordingly and notify any listeners. * * @param proposal the accepted proposal */ private void proposalAccepted(IContentProposal proposal) { switch (proposalAcceptanceStyle) { case (PROPOSAL_REPLACE): setControlContent(proposal.getContent(), proposal .getCursorPosition()); break; case (PROPOSAL_INSERT): insertControlContent(proposal.getContent(), proposal .getCursorPosition()); break; default: // do nothing. Typically a listener is installed to handle this in // a custom way. break; } // In all cases, notify listeners of an accepted proposal. final Object[] listenerArray = proposalListeners.getListeners(); for (int i = 0; i < listenerArray.length; i++) { ((IContentProposalListener) listenerArray[i]) .proposalAccepted(proposal); } } /* * Set the text content of the control to the specified text, setting the * cursorPosition at the desired location within the new contents. */ private void setControlContent(String text, int cursorPosition) { if (isValid()) { controlContentAdapter.setControlContents(control, text, cursorPosition); } } /* * Insert the specified text into the control content, setting the * cursorPosition at the desired location within the new contents. */ private void insertControlContent(String text, int cursorPosition) { if (isValid()) { // Not all controls preserve their selection index when they lose // focus, so we must set it explicitly here to what it was before // the popup opened. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=127108 if (insertionPos != -1) { controlContentAdapter.setCursorPosition(control, insertionPos); } controlContentAdapter.insertControlContents(control, text, cursorPosition); } } /* * Check that the control and content adapter are valid. */ private boolean isValid() { return control != null && !control.isDisposed() && controlContentAdapter != null; } /* * Record the control's cursor position. */ private void recordCursorPosition() { if (isValid()) { insertionPos = getControlContentAdapter() .getCursorPosition(control); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -