01: package net.sourceforge.squirrel_sql.client.gui.session;
02:
03: import java.util.Collections;
04: import java.util.Comparator;
05: import java.util.Vector;
06:
07: import javax.swing.Action;
08:
09: import net.sourceforge.squirrel_sql.fw.completion.CompletionCandidates;
10: import net.sourceforge.squirrel_sql.fw.completion.ICompletorModel;
11:
12: public class ToolsPopupCompletorModel implements ICompletorModel {
13:
14: Vector<ToolsPopupCompletionInfo> _toolsPopupCompletionInfos = new Vector<ToolsPopupCompletionInfo>();
15:
16: public CompletionCandidates getCompletionCandidates(
17: String selectionStringBegin) {
18:
19: Vector<ToolsPopupCompletionInfo> ret = new Vector<ToolsPopupCompletionInfo>();
20: int maxNameLen = 0;
21: for (int i = 0; i < _toolsPopupCompletionInfos.size(); i++) {
22: ToolsPopupCompletionInfo tpci = _toolsPopupCompletionInfos
23: .get(i);
24: if (tpci.getSelectionString().startsWith(
25: selectionStringBegin)) {
26: ret.add(tpci);
27: maxNameLen = Math.max(maxNameLen, tpci
28: .getSelectionString().length());
29: }
30: }
31:
32: ToolsPopupCompletionInfo[] candidates = ret
33: .toArray(new ToolsPopupCompletionInfo[ret.size()]);
34:
35: for (int i = 0; i < candidates.length; i++) {
36: candidates[i]
37: .setMaxCandidateSelectionStringName(maxNameLen);
38: }
39:
40: return new CompletionCandidates(candidates);
41: }
42:
43: public void addAction(String selectionString, Action action) {
44: _toolsPopupCompletionInfos.add(new ToolsPopupCompletionInfo(
45: selectionString, action));
46:
47: Collections.sort(_toolsPopupCompletionInfos,
48: new ToolsPopupCompletionInfoComparator());
49:
50: }
51:
52: private static class ToolsPopupCompletionInfoComparator implements
53: Comparator<ToolsPopupCompletionInfo> {
54:
55: /**
56: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
57: */
58: public int compare(ToolsPopupCompletionInfo arg0,
59: ToolsPopupCompletionInfo arg1) {
60: return arg0.getSelectionString().compareTo(
61: arg1.getSelectionString());
62: }
63:
64: }
65:
66: }
|