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 10 public final class DocViewUtils 11 { 12 13 public DocViewUtils() 14 { 15 } 16 17 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 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 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 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 doc.setText(""); 95 } 96 else 97 { 98 try 100 { 101 doc.remove(start, end-start); 102 } catch(Exception e) { e.printStackTrace(); } 103 } 104 105 } 106 }); 107 } 108 109 String selectedText = ""; 110 if((pane.getSelectionEnd()-pane.getSelectionStart())>0) 111 { 112 selectedText = pane.getSelectedText(); 113 } 114 else 115 { 116 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 ft = selectedText; 135 final String 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 175 } 176 177 } 178 179 } | Popular Tags |