KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > thickclient > application > swt > TextModificationSelectionAdapter


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: TextModificationSelectionAdapter.java,v 1.3 2007/01/07 06:14:15 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.thickclient.application.swt;
23
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.widgets.Text;
26 import org.opensubsystems.patterns.thickclient.application.ModificationListener;
27 import org.opensubsystems.patterns.thickclient.application.TextProcessor;
28
29 /**
30  * Adapter used to process selection events that result in text modification.
31  *
32  * @version $Id: TextModificationSelectionAdapter.java,v 1.3 2007/01/07 06:14:15 bastafidli Exp $
33  * @author Miro Halas
34  * @code.reviewer Miro Halas
35  * @code.reviewed 1.1 2006/02/16 14:41:03 bastafidli
36  */

37 public class TextModificationSelectionAdapter extends DelegatingSelectionAdapter
38 {
39    // Constants ////////////////////////////////////////////////////////////////
40

41    /**
42     * Clear last character from the text.
43     */

44    public static final String JavaDoc FUNCTION_CLEAR = "clear";
45    
46    /**
47     * Clear all characters from the text.
48     */

49    public static final String JavaDoc FUNCTION_CLEAR_ALL = "clearall";
50    
51    // Attributes ///////////////////////////////////////////////////////////////
52

53    /**
54     * Text processor, which should be utilized to preprocess text when the text
55     * is modified.
56     */

57    protected TextProcessor m_processor;
58
59    /**
60     * Listener, which will be notified when the content is changed.
61     */

62    protected ModificationListener m_listener;
63    
64    /**
65     * What function to perform, either a character to add or one
66     * of the FUNCTION_XXX controls.
67     */

68    protected String JavaDoc m_strFunction;
69    
70    /**
71     * Text control being monitored and possibly modified by this adapter.
72     */

73    protected Text m_destination;
74
75    // Constructors /////////////////////////////////////////////////////////////
76

77    /**
78     * Construct new adapter processing events from the control.
79     *
80     * @param processor - text processor, which should be utilized to preprocess
81     * text when the text is modified.
82     * @param listener - listener to call when some modification has happened
83     * @param strFunction - what function to perform, either a character
84     * to add or one of the FUNCTION_XXX controls.
85     * @param destination - text control being monitored and possibly modified
86     * by this adapter.
87     */

88    public TextModificationSelectionAdapter(
89       TextProcessor processor,
90       ModificationListener listener,
91       String JavaDoc strFunction,
92       Text destination
93    )
94    {
95       m_processor = processor;
96       m_listener = listener;
97       m_strFunction = strFunction;
98       m_destination = destination;
99    }
100
101    // Public methods ///////////////////////////////////////////////////////////
102

103    /**
104     * {@inheritDoc}
105     */

106    public void widgetSelected(
107       SelectionEvent event
108    )
109    {
110       if (m_destination != null)
111       {
112          String JavaDoc strCurrent;
113          
114          if (FUNCTION_CLEAR.equals(m_strFunction))
115          {
116             strCurrent = m_destination.getText();
117             if (strCurrent.length() > 0)
118             {
119                 m_destination.setText(strCurrent.substring(0, strCurrent.length() - 1));
120                 m_destination.setSelection(m_destination.getText().length(),
121                                            m_destination.getText().length());
122             }
123          }
124          else if (FUNCTION_CLEAR_ALL.equals(m_strFunction))
125          {
126             m_destination.setText("");
127          }
128          else
129          {
130             if (m_processor != null)
131             {
132                m_strFunction = m_processor.processText(m_strFunction);
133             }
134             m_destination.append(m_strFunction);
135          }
136    
137          m_destination.setFocus();
138       }
139       
140       if (m_listener != null)
141       {
142          m_listener.modified();
143       }
144    }
145 }
146
Popular Tags