View Javadoc

1   package com.explosion.expfmodules.texteditor;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.awt.Component;
23  import java.awt.event.MouseEvent;
24  
25  import javax.swing.JTextField;
26  import javax.swing.event.DocumentEvent;
27  import javax.swing.event.DocumentListener;
28  import javax.swing.text.JTextComponent;
29  
30  import com.explosion.expf.Application;
31  import com.explosion.expf.ExpActionListener;
32  import com.explosion.expf.ExpFrame;
33  import com.explosion.expf.menusandtools.menu.MenuHelper;
34  import com.explosion.expf.menusandtools.menu.popup.ExpPopupIntercepter;
35  import com.explosion.expf.menusandtools.menu.popup.ExpPopupableMenu;
36  import com.explosion.expf.menusandtools.menu.segmented.ExpMenuSegment;
37  import com.explosion.expf.supportmodules.EditSupportModule;
38  import com.explosion.utilities.WorkingCaret;
39  import com.explosion.utilities.exception.ExceptionManagerFactory;
40  
41  /***
42   * Date created:@16-Oct-2003
43   * @author Stephen Cowx
44   */
45  
46  public class ExpfTextField extends JTextField implements Editable, ExpPopupIntercepter
47  {
48      /* General variables */
49    private boolean dirty = false;
50    private MenuAwareUndoRedoBuffer undoRedoBuffer = null;
51  
52    /* Local listener */
53    private TextComponentLocalListener localListener = null;
54    
55    private Component highLevelParent;
56    private ExpPopupableMenu menu;
57    
58    /***
59     * Initialises a new ExpfTextArea
60     */
61    public ExpfTextField(Component highLevelParent)
62    {
63      super();
64      this.highLevelParent = highLevelParent;
65      init();
66    }
67    
68    
69    /***
70     * This method initialises the gui
71     */
72    private void init() 
73    {
74      
75      /* Initialise MenuAwareUndoRedoBuffer */
76      undoRedoBuffer = new MenuAwareUndoRedoBuffer(highLevelParent,this);
77      
78      /* Initialise the local listener */
79      localListener = new TextComponentLocalListener(this, highLevelParent);
80  	
81      
82      setDoubleBuffered(true);
83      setCaret(new WorkingCaret());
84      setCaretPosition(0);
85      setDoubleBuffered(true);
86      
87      getDocument().addDocumentListener(new DocumentListener()
88      {
89         public void insertUpdate(DocumentEvent e) {}
90         public void removeUpdate(DocumentEvent e) {}	
91         public void changedUpdate(DocumentEvent e) {}	
92      }
93      );
94      
95      getDocument().addUndoableEditListener(undoRedoBuffer);
96    }
97    
98    public void select(int startSelection, int endSelection)
99    {
100   }
101   
102   /***
103 	 * Returns the JTextComponent associated with this object
104 	 * @return
105 	 */
106 	public JTextComponent getTextComponent()
107 	{
108 		return this;
109 	}
110   
111   public ExpActionListener getlocalListener()
112   {
113   	return localListener;
114   }
115   
116   public void applyPreferences()
117   {
118     undoRedoBuffer.setMaxSize(((Integer)TextEditorModuleManager.instance().getPreference(TextEditorConstants.UNDOLIST_SIZE).getValue()).intValue());
119   }
120   
121   /***
122    * Clear this tool
123    */
124   public void clear() throws Exception
125   {
126     setText("");
127   }
128 
129   /***
130    * Undo the last command executed by the user
131    */
132   public void undo() throws Exception
133   { 
134     undoRedoBuffer.undo();
135   }
136 
137   /***
138    * Undo the last n commands executed by the user
139    */
140   public void undo(int numberOfSteps) throws Exception
141   {
142     int i = 0;
143     while (i < numberOfSteps)
144     {
145       undoRedoBuffer.undo();
146       i++;
147     }
148   }
149 
150   public void redo() throws Exception
151   {
152     undoRedoBuffer.redo();
153   }
154 
155   
156   /***
157    * Pops up a popup menu
158    * @param event
159    */
160   public void popupEvent(MouseEvent event) {
161     try
162     {
163         if (menu == null)
164         {
165             menu = new ExpPopupableMenu();
166             MenuHelper menuHelper = new MenuHelper(((ExpFrame)Application.getApplicationFrame()).getListener());
167             
168             ExpMenuSegment redo = menu.createNewSegment(ExpMenuSegment.ALWAYS_FIRST_SEGMENT);
169             ExpMenuSegment cutcopy = menu.createNewSegment(ExpMenuSegment.ANY_POSITION);
170             ExpMenuSegment selectAndClear = menu.createNewSegment(ExpMenuSegment.ANY_POSITION);
171             
172             EditSupportModule.populateUndoRedoMenuSegment(menuHelper, redo);
173             EditSupportModule.populateCutCopyPasteMenuSegment(menuHelper, cutcopy);
174             EditSupportModule.populateSelectAndClearSegment(menuHelper, selectAndClear);
175             
176             menuHelper.checkEnabled();
177         }
178         
179         menu.show(event);
180     }
181     catch (Exception e)
182     {
183         ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while handling popupEvent");
184     }
185   }
186   
187 }
188