Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/maddyhome/idea/vim/ui/ExEditorKit.java


1   package com.maddyhome.idea.vim.ui;
2   
3   /*
4   * IdeaVim - A Vim emulator plugin for IntelliJ Idea
5   * Copyright (C) 2003 Rick Maddy
6   *
7   * This program is free software; you can redistribute it and/or
8   * modify it under the terms of the GNU General Public License
9   * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
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  import com.maddyhome.idea.vim.helper.SearchHelper;
23  import java.awt.event.ActionEvent;
24  import javax.swing.Action;
25  import javax.swing.JTextField;
26  import javax.swing.text.BadLocationException;
27  import javax.swing.text.Caret;
28  import javax.swing.text.DefaultEditorKit;
29  import javax.swing.text.Document;
30  import javax.swing.text.JTextComponent;
31  import javax.swing.text.TextAction;
32  
33  /**
34   *
35   */
36  public class ExEditorKit extends DefaultEditorKit
37  {
38      public static ExEditorKit getInstance()
39      {
40          if (instance == null)
41          {
42              instance = new ExEditorKit();
43          }
44  
45          return instance;
46      }
47  
48      /**
49       * Gets the MIME type of the data that this
50       * kit represents support for.
51       *
52       * @return the type
53       */
54      public String getContentType()
55      {
56          return "text/plain";
57      }
58  
59      /**
60       * Fetches the set of commands that can be used
61       * on a text component that is using a model and
62       * view produced by this kit.
63       *
64       * @return the set of actions
65       */
66      public Action[] getActions()
67      {
68          return TextAction.augmentList(super.getActions(), this.exActions);
69      }
70  
71      /**
72       * Creates an uninitialized text storage model
73       * that is appropriate for this type of editor.
74       *
75       * @return the model
76       */
77      public Document createDefaultDocument()
78      {
79          return new ExDocument();
80      }
81  
82      public static final String DeletePreviousChar = "delete-prev-char";
83      public static final String DeletePreviousWord = "delete-prev-word";
84      public static final String DeleteToCursor = "delete-to-cursor";
85      public static final String ToggleInsertReplace = "toggle-insert";
86      public static final String InsertRegister = "insert-register";
87      public static final String InsertWord = "insert-word";
88      public static final String InsertWORD = "insert-WORD";
89      public static final String HistoryRecent = "history-recent";
90      public static final String HistoryOld = "history-old";
91      public static final String HistoryRecentFilter = "history-recent-filter";
92      public static final String HistoryOldFilter = "history-old-filter";
93  
94      //TODO - add rest of actions
95      protected Action[] exActions = new Action[] {
96          new DeletePreviousCharAction(),
97          new DeletePreviousWordAction(),
98          new DeleteToCursorAction(),
99          new ToggleInsertReplaceAction()
100     };
101 
102     // TODO - how do I get the argument (register name)?
103     public static class InsertRegisterAction extends TextAction
104     {
105         public InsertRegisterAction()
106         {
107             super(InsertRegister);
108         }
109 
110         /**
111          * Invoked when an action occurs.
112          */
113         public void actionPerformed(ActionEvent e)
114         {
115             JTextComponent target = getTextComponent(e);
116             if (target != null && target.isEditable())
117             {
118             }
119         }
120 
121     }
122 
123     public static class DeletePreviousCharAction extends TextAction
124     {
125         public DeletePreviousCharAction()
126         {
127             super(DeletePreviousChar);
128         }
129 
130         /**
131          * Invoked when an action occurs.
132          */
133         public void actionPerformed(ActionEvent e)
134         {
135             JTextField target = (JTextField)getTextComponent(e);
136             if ((target != null) && (target.isEditable()))
137             {
138                 try
139                 {
140                     Document doc = target.getDocument();
141                     Caret caret = target.getCaret();
142                     int dot = caret.getDot();
143                     int mark = caret.getMark();
144                     if (dot != mark)
145                     {
146                         doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
147                     }
148                     else if (dot > 0)
149                     {
150                         int delChars = 1;
151 
152                         if (dot > 1)
153                         {
154                             String dotChars = doc.getText(dot - 2, 2);
155                             char c0 = dotChars.charAt(0);
156                             char c1 = dotChars.charAt(1);
157 
158                             if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
159                                 c1 >= '\uDC00' && c1 <= '\uDFFF')
160                             {
161                                 delChars = 2;
162                             }
163                         }
164 
165                         doc.remove(dot - delChars, delChars);
166                     }
167                 }
168                 catch (BadLocationException bl)
169                 {
170                 }
171             }
172         }
173     }
174 
175     public static class DeletePreviousWordAction extends TextAction
176     {
177         public DeletePreviousWordAction()
178         {
179             super(DeletePreviousWord);
180         }
181 
182         /**
183          * Invoked when an action occurs.
184          */
185         public void actionPerformed(ActionEvent e)
186         {
187             JTextComponent target = getTextComponent(e);
188             if (target != null && target.isEditable())
189             {
190                 Document doc = target.getDocument();
191                 Caret caret = target.getCaret();
192                 int offset = SearchHelper.findNextWord(target.getText().toCharArray(), caret.getDot(), doc.getLength(),
193                     -1, false);
194                 try
195                 {
196                     doc.remove(offset, caret.getDot());
197                 }
198                 catch (BadLocationException ex)
199                 {
200                 }
201             }
202         }
203     }
204 
205     public static class DeleteToCursorAction extends TextAction
206     {
207         public DeleteToCursorAction()
208         {
209             super(DeleteToCursor);
210         }
211 
212         /**
213          * Invoked when an action occurs.
214          */
215         public void actionPerformed(ActionEvent e)
216         {
217             JTextComponent target = getTextComponent(e);
218             if (target != null && target.isEditable())
219             {
220                 Document doc = target.getDocument();
221                 Caret caret = target.getCaret();
222                 try
223                 {
224                     doc.remove(0, caret.getDot());
225                 }
226                 catch (BadLocationException ex)
227                 {
228                 }
229             }
230         }
231     }
232 
233     public static class ToggleInsertReplaceAction extends TextAction
234     {
235         public ToggleInsertReplaceAction()
236         {
237             super(ToggleInsertReplace);
238         }
239 
240         /**
241          * Invoked when an action occurs.
242          */
243         public void actionPerformed(ActionEvent e)
244         {
245             JTextComponent target = getTextComponent(e);
246             ExDocument doc = (ExDocument)target.getDocument();
247             doc.toggleInsertReplace();
248         }
249     }
250 
251     private static ExEditorKit instance;
252 }