KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > texteditor > DocViewUtils


1 package snow.texteditor;
2
3 import snow.utils.gui.Icons;
4 import snow.utils.StringUtils;
5 import snow.datatransfer.ClipboardUtils;
6 import java.awt.event.*;
7 import javax.swing.*;
8 /** class DocViewUtils.
9 */

10 public final class DocViewUtils
11 {
12    /** Constructor. */
13    public DocViewUtils()
14    {
15    }
16
17   /** adapted from tide, NOT A COPY. be careful.
18   * Copy, paste, search, used for the mail editor.
19   * Also adds the "paste" history from ClipboardUtils.
20   *
21   * BE CAREFUL With shortcus, they are "fake" and focus is not really precise.
22   */

23   public static void addStandardPopupDocumentActions(JPopupMenu popup, final SimpleDocument doc, final SearchPanel searchPanel, final JTextPane pane, final int clickedPos )
24   {
25      JMenuItem copy = new JMenuItem("Copy");
26      copy.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK, false));
27      popup.add(copy);
28      copy.addActionListener(new ActionListener()
29      {
30        public void actionPerformed(ActionEvent ae)
31        {
32           String JavaDoc sel = pane.getSelectedText();
33           if(sel!=null)
34           {
35             ClipboardUtils.copyToClipboard( sel );
36           }
37        }
38      });
39
40      if(pane.isEditable())
41      {
42         JMenuItem paste = new JMenuItem("Paste");
43         paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK, false));
44         popup.addSeparator();
45         popup.add(paste);
46         paste.addActionListener(new ActionListener()
47         {
48           public void actionPerformed(ActionEvent ae)
49           {
50             if(!pane.isEditable()) return;
51             String JavaDoc txt = ClipboardUtils.getClipboardStringContent();
52             if(txt!=null && txt.length()>0)
53             {
54               doc.insertString(txt, pane.getCaretPosition());
55             }
56           }
57         });
58
59         if(ClipboardUtils.getInstance().getHistory().size()>0)
60         {
61            JMenu pasteHistory = new JMenu("Paste history ");
62            popup.add(pasteHistory);
63            for(final String JavaDoc p : ClipboardUtils.getInstance().getHistory())
64            {
65              JMenuItem pasteH = new JMenuItem(""+StringUtils.shortFormForDisplay(p,70));
66              pasteHistory.add(pasteH);
67              pasteH.addActionListener(new ActionListener()
68              {
69                public void actionPerformed(ActionEvent ae)
70                {
71                   doc.insertString(p, pane.getCaretPosition());
72                }
73              });
74            }
75         }
76      }
77
78      final int start = pane.getSelectionStart();
79      final int end = pane.getSelectionEnd();
80
81      if(pane.isEditable())
82      {
83         JMenuItem delete = new JMenuItem(start>=end ? "Delete all text" : "Delete selected text",
84           Icons.CrossIcon.shared10);
85         popup.addSeparator();
86         popup.add(delete);
87         delete.addActionListener(new ActionListener()
88         {
89           public void actionPerformed(ActionEvent ae)
90           {
91             if(start>=end)
92             {
93                // delete all
94
doc.setText("");
95             }
96             else
97             {
98                // delete selected
99
try
100                {
101                  doc.remove(start, end-start);
102                } catch(Exception JavaDoc e) { e.printStackTrace(); }
103             }
104
105           }
106         });
107      }
108
109      String JavaDoc selectedText = "";
110      if((pane.getSelectionEnd()-pane.getSelectionStart())>0)
111      {
112        selectedText = pane.getSelectedText();
113      }
114      else
115      {
116        // detect the word at the cursot
117
selectedText = DocumentUtils.getWordAt(doc, clickedPos);
118      }
119
120      popup.addSeparator();
121      JMenuItem search = new JMenuItem("Search text", Icons.SearchIcon.shared18);
122      search.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK, false));
123      popup.add(search);
124      search.addActionListener(new ActionListener()
125      {
126        public void actionPerformed(ActionEvent ae)
127        {
128           searchPanel.setVisible(true);
129        }
130      });
131
132      if(selectedText!=null && selectedText.length()>0)
133      {
134         final String JavaDoc ft = selectedText;
135         final String JavaDoc shortText = StringUtils.shortFormForDisplay(selectedText, 70);
136         JMenuItem searchF = new JMenuItem("Search next \""+shortText+"\"", Icons.SearchIcon.shared18);
137         popup.add(searchF);
138         searchF.addActionListener(new ActionListener()
139         {
140           public void actionPerformed(ActionEvent ae)
141           {
142              searchPanel.searchNext_calledFromPopup( ft, clickedPos);
143           }
144         });
145
146         popup.addSeparator();
147 /*
148         JMenuItem searchFE = new JMenuItem("Search in editor for first \""+shortText+"\"", Icons.SearchIcon.shared18);
149         popup.add(searchFE);
150
151         searchFE.addActionListener(new ActionListener()
152         {
153           public void actionPerformed(ActionEvent ae)
154           {
155              //searchPanel.searchNext_calledFromPopup( ft, clickedPos);
156              //MainEditorFrame.instance.editorPanel.searchFirst(ft);
157           }
158         });
159
160         JMenuItem searchG = new JMenuItem("Global search for \""+shortText+"\"", Icons.SearchIcon.shared18);
161         searchG.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.CTRL_MASK, false));
162         popup.add(searchG);
163
164         searchG.addActionListener(new ActionListener()
165         {
166           public void actionPerformed(ActionEvent ae)
167           {
168             // MainEditorFrame.instance.globalProperties.setProperty("SearchTool_searchTF", ft);
169             // MainEditorFrame.instance.globalProperties.setBoolean("SearchTool_regex", false);
170
171             // new SearchTool(MainEditorFrame.instance.sourcesTreePanel.getTreeModel().getAllSourceFiles(false), false);
172           }
173         });
174         */

175      }
176
177   }
178
179 }
Popular Tags