Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30526
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Jeff Higgins <jeff@invalid.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: JEditorPane question |
| Date | Mon, 08 Dec 2014 23:11:38 -0500 |
| Organization | A noiseless patient Spider |
| Lines | 186 |
| Message-ID | <m65sr8$tdq$1@dont-email.me> (permalink) |
| References | <m64a5v$d3j$1@dont-email.me> <m659sr$5vl$1@dont-email.me> <m65car$d52$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 9 Dec 2014 04:12:24 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="c50a9e5d554cfaba13eda1c71e805135"; logging-data="30138"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184ww3fMXsJzW1QvwAL/BCtrlkpCFRdKdg=" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.2.0 |
| In-Reply-To | <m65car$d52$1@dont-email.me> |
| Cancel-Lock | sha1:g7zo5o7EnKlreySnJl2RGaiB3XA= |
| Xref | csiph.com comp.lang.java.programmer:30526 |
Show key headers only | View raw
This one with a TransferHandler allows
me to consistently copy/paste with crlf.
Modified from Java Tutorial.
That ignorant comment kinda pissed me off.
package bluesg.sandbox;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JEditorPane;
import javax.swing.TransferHandler;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;
public class JEditorPaneTest {
JFrame frame = null;
public static void main(String[] a) throws FileNotFoundException,
UnsupportedEncodingException {
(new JEditorPaneTest()).test();
}
private void test() throws FileNotFoundException,
UnsupportedEncodingException {
TransferHandler th = new TextTransferHandler();
frame = new JFrame("JEditorPane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setJMenuBar(createMenuBar());
JEditorPane pane = new JEditorPane();
pane.setTransferHandler(th);
pane.setContentType("text/plain");
pane.setText("Lorem ipsum dolor sit amet, consectetur adipiscing
elit.\n\n"
+ "Nunc pellentesque quam et justo fringilla, eu gravida nunc
tempus.");
String test = pane.getText();
System.out.print(test);
new PrintWriter("LoremIpsum", "UTF-8").printf(test);
frame.setContentPane(pane);
frame.setVisible(true);
}
public JMenuBar createMenuBar() {
JMenuItem menuItem = null;
JMenuBar menuBar = new JMenuBar();
JMenu mainMenu = new JMenu("Edit");
mainMenu.setMnemonic(KeyEvent.VK_E);
menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
menuItem.setText("Cut");
menuItem.setMnemonic(KeyEvent.VK_T);
mainMenu.add(menuItem);
menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
menuItem.setText("Copy");
menuItem.setMnemonic(KeyEvent.VK_C);
mainMenu.add(menuItem);
menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
menuItem.setText("Paste");
menuItem.setMnemonic(KeyEvent.VK_P);
mainMenu.add(menuItem);
menuBar.add(mainMenu);
return menuBar;
}
class TextTransferHandler extends TransferHandler {
// Start and end position in the source text.
// We need this information when performing a MOVE
// in order to remove the dragged text from the source.
Position p0 = null, p1 = null;
/**
* Perform the actual import.
* This method supports both drag and drop and
* cut/copy/paste.
*/
public boolean importData(TransferHandler.TransferSupport support) {
// If we can't handle the import, bail now.
if (!canImport(support)) {
return false;
}
// Fetch the data -- bail if this fails
String data;
try {
data = (String) support.getTransferable().getTransferData(
DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
return false;
} catch (java.io.IOException e) {
return false;
}
JEditorPane tc = (JEditorPane) support.getComponent();
tc.replaceSelection(data);
return true;
}
/**
* Bundle up the data for export.
*/
protected Transferable createTransferable(JComponent c) {
JEditorPane source = (JEditorPane) c;
int start = source.getSelectionStart();
int end = source.getSelectionEnd();
Document doc = source.getDocument();
doc.putProperty(DefaultEditorKit.EndOfLineStringProperty, "\r\n");
if (start == end) {
return null;
}
try {
p0 = doc.createPosition(start);
p1 = doc.createPosition(end);
} catch (BadLocationException e) {
System.out
.println("Can't create position - unable to remove text
from source.");
}
String data = source.getText().substring(start, end);
return new StringSelection(data);
}
/**
* These text fields handle both copy and move actions.
*/
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
/**
* When the export is complete, remove the old text if the action was a
* move.
*/
protected void exportDone(JComponent c, Transferable data, int
action) {
if (action != MOVE) {
return;
}
if ((p0 != null) && (p1 != null) && (p0.getOffset() !=
p1.getOffset())) {
try {
JTextComponent tc = (JTextComponent) c;
tc.getDocument().remove(p0.getOffset(),
p1.getOffset() - p0.getOffset());
} catch (BadLocationException e) {
System.out.println("Can't remove text from source.");
}
}
}
/**
* We only support importing strings.
*/
public boolean canImport(TransferHandler.TransferSupport support) {
// we only import Strings
if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
return true;
}
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-08 13:47 +0000
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 17:48 -0500
Re: JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-08 23:30 +0000
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 19:14 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 19:49 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 20:48 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 21:21 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 21:37 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 23:11 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-08 23:28 -0500
Re: JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-11 18:31 +0000
Re: JEditorPane question Martin Gregorie <martin@address-in-sig.invalid> - 2014-12-09 09:41 +0000
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-09 22:41 -0500
Re: JEditorPane question Jeff Higgins <jeff@invalid.invalid> - 2014-12-10 00:46 -0500
csiph-web