View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui.completion;
21  
22  import javax.swing.text.JTextComponent;
23  
24  /***
25   * Abstract convenience class which provides some wrapping function to ease
26   * the implementation of a {@link CompletionMode}.
27   * 
28   * @author Felix Berger
29   */
30  public abstract class AbstractCompletionMode implements CompletionMode
31  {
32  	
33  	private Completion completion;
34  	
35  	/*
36  	 * @see org.xnap.commons.gui.completion.CompletionMode#enable(org.xnap.commons.gui.completion.Completion)
37  	 */
38  	public void enable(Completion completion)
39  	{
40  		this.completion = completion;
41  		enable();
42  	}
43  
44  	/***
45  	 * Has to be implemented by subclasses which don't have to worry about
46  	 * setting the completion object and simply use the accessors provided here. 
47  	 */
48  	protected abstract void enable();
49  	
50  	/*
51  	 * @see org.xnap.commons.gui.completion.CompletionMode#disable()
52  	 */
53  	public abstract void disable();
54  
55  	/***
56  	 * Convenience accessor, returns the completion model
57  	 */
58  	protected CompletionModel getModel()
59  	{
60  		return completion.getModel();
61  	}
62  
63  	/***
64  	 * Convenience accessor, returns the text component.
65  	 */
66  	protected JTextComponent getTextComponent()
67  	{
68  		return completion.getTextComponent();
69  	}
70  
71  	/***
72  	 * Returns the completion object that uses this completion mode.
73  	 */
74  	protected Completion getCompletion()
75  	{
76  		return completion;
77  	}
78  	
79  	/***
80  	 * Convenience accessor, returns the text which should be completed.
81  	 * @return the whole text up to the current caret position, if 
82  	 * {@link Completion#isWholeTextCompletion()} is true, otherwise the
83  	 * previous word up to the current care position
84  	 */
85  	protected String getText()
86  	{
87  		return completion.getText();
88  	}
89  	
90  	/***
91  	 * Convenience setter, forwards to 
92  	 * {@link Completion#setText(String, int, int)}.
93  	 */
94  	protected void setText(String text, int selectionStart,
95  						   int selectionEnd)
96  	{
97  		completion.setText(text, selectionStart, selectionEnd);
98  	}
99  
100 	/***
101 	 * Convenience setter, forwards to {@link Completion#setText(String)}.
102 	 */
103 	protected void setText(String text)
104 	{
105 		completion.setText(text);
106 	}
107 }