123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.input;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Locale;
-
- import com.dmdirc.Config;
-
- /**
- * The tab completer handles a user's request to tab complete some word.
- * @author chris
- */
- public final class TabCompleter {
-
- /**
- * The parent TabCompleter. Results from parents are merged with results
- * from this completer.
- */
- private TabCompleter parent;
- /**
- * The entries in this completer.
- */
- private List<String> entries = new ArrayList<String>();
-
- /** Creates a new instance of TabCompleter. */
- public TabCompleter() {
- //Do nothing.
- }
-
- /**
- * Creates a new instance of TabCompleter, with a designated parent.
- * @param newParent The parent TabCompleter, which is checked for matches if
- * local ones fail
- */
- public TabCompleter(final TabCompleter newParent) {
- this.parent = newParent;
- }
-
- /**
- * Attempts to complete the partial string.
- * @param partial The string to tab complete
- * @return A TabCompleterResult containing any matches found
- */
- public TabCompleterResult complete(final String partial) {
- final TabCompleterResult result = new TabCompleterResult();
-
- for (String entry : entries) {
- if (Config.getOptionBool("tabcompletion", "casesensitive")) {
- if (entry.startsWith(partial)) {
- result.addResult(entry);
- }
- } else {
- if (entry.toLowerCase(Locale.getDefault())
- .startsWith(partial.toLowerCase(Locale.getDefault()))) {
- result.addResult(entry);
- }
- }
- }
-
- if (parent != null) {
- result.merge(parent.complete(partial));
- }
- return result;
- }
-
- /**
- * Adds a new entry to this tab completer's list.
- * @param entry The new entry to be added
- */
- public void addEntry(final String entry) {
- entries.add(entry);
- }
-
- /**
- * Adds multiple new entries to this tab completer's list.
- * @param newEntries Entries to be added
- */
- public void addEntries(final List<String> newEntries) {
- if (newEntries == null) {
- return;
- }
-
- for (String entry : newEntries) {
- addEntry(entry);
- }
- }
-
- /**
- * Removes a specified entry from this tab completer's list.
- * @param entry The entry to be removed
- */
- public void removeEntry(final String entry) {
- entries.remove(entry);
- }
-
- /**
- * Replaces the current entries with the new list.
- * @param newEntries the new entries to use
- */
- public void replaceEntries(final List<String> newEntries) {
- entries = newEntries;
- }
-
- }
|