File Source: MircStyle.java
/*
P/P * Method: com.dmdirc.addons.tabcompletion_mirc.MircStyle__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.tabcompletion_mirc;
24
25 import com.dmdirc.Channel;
26 import com.dmdirc.ui.input.AdditionalTabTargets;
27 import com.dmdirc.ui.input.TabCompleter;
28 import com.dmdirc.ui.input.TabCompleterResult;
29 import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
30 import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
31 import com.dmdirc.ui.interfaces.InputWindow;
32
33 import java.awt.Toolkit;
34 import java.util.List;
35
36 public class MircStyle implements TabCompletionStyle {
37
38 /** The last set of results we retrieved. */
39 private List<String> lastResult;
40
41 /** The last word that was tab completed. */
42 private String lastWord;
43
44 /** The tab completer that we use. */
45 protected final TabCompleter tabCompleter;
46
47 /** The input window that we use. */
48 protected final InputWindow window;
49
50 /**
51 * Creates a new mIRC-style tab completer.
52 *
53 * @param completer The tab completer this style is for
54 * @param window The window this tab style is for
55 */
/*
P/P * Method: void com.dmdirc.addons.tabcompletion_mirc.MircStyle(TabCompleter, InputWindow)
*
* Postconditions:
* this.tabCompleter == completer
* init'ed(this.tabCompleter)
* this.window == window
* init'ed(this.window)
*/
56 public MircStyle(final TabCompleter completer, final InputWindow window) {
57 this.tabCompleter = completer;
58 this.window = window;
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 public TabCompletionResult getResult(final String original, final int start,
64 final int end, final AdditionalTabTargets additional) {
65
/*
P/P * Method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
*
* Preconditions:
* init'ed(this.lastWord)
* original != null
* (soft) this.lastResult != null
* (soft) this.tabCompleter != null
* (soft) this.window != null
*
* Presumptions:
* com.dmdirc.Channel:getChannelInfo(...)@80 != null
* com.dmdirc.Channel:getChannelInfo(...)@83 != null
* com.dmdirc.parser.irc.ChannelInfo:getName(...)@80 != null
* com.dmdirc.parser.irc.ChannelInfo:getName(...)@83 != null
* com.dmdirc.ui.input.TabCompleter:complete(...)@74 != null
* ...
*
* Postconditions:
* return_value in Addr_Set{null,&new TabCompletionResult(getResult#1)}
* init'ed(this.lastResult)
* init'ed(this.lastWord)
* new TabCompletionResult(getResult#1) num objects <= 1
*
* Test Vectors:
* com.dmdirc.ui.input.TabCompleterResult:getResultCount(...)@76: {-231..-1, 1..232-1}, {0}
* java.lang.String:equals(...)@69: {0}, {1}
* java.lang.String:length(...)@80: {0}, {1..232-1}
* java.lang.String:startsWith(...)@80: {0}, {1}
*/
66 final String word = original.substring(start, end);
67 String target = "";
68
69 if (word.equals(lastWord)) {
70 // We're continuing to tab through
71 target = lastResult.get((lastResult.indexOf(lastWord) + 1) % lastResult.size());
72 } else {
73 // New tab target
74 final TabCompleterResult res = tabCompleter.complete(word, additional);
75
76 if (res.getResultCount() == 0) {
77 Toolkit.getDefaultToolkit().beep();
78 return null;
79 } else {
80 if (word.length() > 0 && window.getContainer() instanceof Channel
81 && ((Channel) window.getContainer())
82 .getChannelInfo().getName().startsWith(word)) {
83 target = ((Channel) window.getContainer()).getChannelInfo().getName();
84 } else {
85 target = res.getResults().get(0);
86 }
87 lastResult = res.getResults();
88 }
89 }
90
91 lastWord = target;
92
93 return new TabCompletionResult(original.substring(0, start) + target
94 + original.substring(end), start + target.length());
95 }
96
97 }
SofCheck Inspector Build Version : 2.17854
| MircStyle.java |
2009-Jun-25 01:54:24 |
| MircStyle.class |
2009-Sep-02 17:04:15 |