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

Quick Search    Search Deep

Source code: com/memoire/dnd/DndManageText.java


1   /**
2    * @modification $Date: 2003/01/02 18:32:03 $
3    * @statut       unstable
4    * @file         DndManageText.java
5    * @version      0.36
6    * @author       Guillaume Desnoix
7    * @email        guillaume@desnoix.com
8    * @license      GNU General Public License 2 (GPL2)
9    * @copyright    1998-2001 Guillaume Desnoix
10   */
11  
12  package com.memoire.dnd;
13  import com.memoire.dnd.*;
14  
15  
16  import java.awt.*;
17  import javax.swing.*;
18  import javax.swing.text.*;
19  
20  public class DndManageText
21      implements DndManageData, DndModes
22  {
23      public static final DndManageText SINGLETON_REPLACE_ALL=
24    new DndManageText(REPLACE_ALL);
25  
26      public static final DndManageText SINGLETON_INSERT_AT_CURSOR=
27    new DndManageText(INSERT_AT_CURSOR);
28  
29      private int mode_;
30  
31      public DndManageText(int _mode)
32      {
33    mode_=_mode;
34      }
35  
36      public boolean manage
37    (Object _data,JComponent _component,Point _location)
38      {
39    boolean r=false;
40  
41    try
42    {
43        String         str =_data.toString();
44        JTextComponent text=(JTextComponent)_component;
45        Document       doc =text.getDocument();
46        int            pos =-1;
47  
48        synchronized(text)
49        {
50        synchronized(doc)
51        {
52  
53        switch(mode_)
54        {
55        case INSERT_AT_BEGIN:
56            pos=0;//doc.getStartPosition().getOffset();
57      break;
58        case INSERT_AT_END:
59            pos=doc.getLength();//doc.getEndPosition().getOffset();
60      break;
61        case INSERT_AT_CURSOR:
62      Point p=_location;
63      pos=text.viewToModel(p);
64      break;
65        case INSERT_AT_CARET:
66      pos=text.getCaretPosition();
67      break;
68        case INSERT_BEFORE_SELECTION:
69      pos=text.getSelectionStart();
70      break;
71        case INSERT_AFTER_SELECTION:
72      pos=text.getSelectionEnd();
73      break;
74        case REPLACE_SELECTION:
75      pos=text.getSelectionStart();
76      doc.remove(pos,text.getSelectionEnd()-pos);
77      break;
78        case REPLACE_ALL:
79      int start=doc.getStartPosition().getOffset();
80      doc.remove(start,doc.getLength());
81      pos=start;
82      break;
83        }
84  
85        text.setCaretPosition(pos);
86        doc.insertString(pos,str,null);
87        r=true;
88  
89        }
90        }
91    }
92    catch(BadLocationException ex) {}
93    return r;
94      }
95  }