Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
7246 | schaersvoo | 1 | /* |
2 | Sketch Elements: Chemistry molecular diagram drawing tool. |
||
3 | |||
4 | (c) 2008 Dr. Alex M. Clark |
||
5 | |||
6 | Released as GNUware, under the Gnu Public License (GPL) |
||
7 | |||
8 | See www.gnu.org for details. |
||
9 | */ |
||
10 | |||
11 | package WIMSchem; |
||
12 | |||
13 | import java.io.*; |
||
14 | import java.awt.*; |
||
15 | import java.util.*; |
||
16 | import java.awt.image.*; |
||
17 | import java.awt.event.*; |
||
18 | import java.awt.datatransfer.*; |
||
19 | import javax.swing.*; |
||
20 | |||
21 | public class DraggableMolecule extends JLabel implements MouseListener,MouseMotionListener |
||
22 | { |
||
23 | EditorPane src; |
||
24 | TransferMoleculeSource trans; |
||
25 | |||
26 | public DraggableMolecule(EditorPane src) |
||
27 | { |
||
28 | this.src=src; |
||
29 | setIcon(new ImageIcon(getClass().getResource("/images/SmallIcon.png"))); |
||
30 | |||
31 | addMouseListener(this); |
||
32 | addMouseMotionListener(this); |
||
33 | |||
34 | trans=new TransferMoleculeSource(src); |
||
35 | setTransferHandler(trans); |
||
36 | } |
||
37 | |||
38 | public void mouseEntered(MouseEvent e) {} |
||
39 | public void mouseExited(MouseEvent e) {} |
||
40 | public void mouseClicked(MouseEvent e) {} |
||
41 | public void mousePressed(MouseEvent e) |
||
42 | { |
||
43 | if (src.molData().numAtoms()>0) trans.exportAsDrag(this,e,TransferHandler.COPY); |
||
44 | } |
||
45 | public void mouseReleased(MouseEvent e) {} |
||
46 | public void mouseMoved(MouseEvent e) {} |
||
47 | public void mouseDragged(MouseEvent e) {} |
||
48 | |||
49 | // Class for describing the content of that which is dragged from this control. |
||
50 | |||
51 | class TransferMoleculeSource extends TransferHandler |
||
52 | { |
||
53 | EditorPane src; |
||
54 | |||
55 | public TransferMoleculeSource(EditorPane src) {this.src=src;} |
||
56 | public boolean canImport(TransferHandler.TransferSupport info) {return false;} |
||
57 | public int getSourceActions(JComponent c) {return COPY;} |
||
58 | public void exportAsDrag(JComponent source,InputEvent e,int action) |
||
59 | { |
||
60 | super.exportAsDrag(source,e,action); |
||
61 | } |
||
62 | protected void exportDone(JComponent source,Transferable data,int action) |
||
63 | { |
||
64 | super.exportDone(source,data,action); |
||
65 | } |
||
66 | protected Transferable createTransferable(JComponent c) |
||
67 | { |
||
68 | Molecule mol=/*src.molData()*/ src.selectedSubgraph(); |
||
69 | try |
||
70 | { |
||
71 | StringWriter sw=new StringWriter(); |
||
72 | BufferedWriter bw=new BufferedWriter(sw); |
||
73 | MoleculeStream.writeMDLMOL(bw,mol); |
||
74 | MoleculeStream.writeNative(bw,mol); |
||
75 | return new StringSelection(sw.toString()); |
||
76 | } |
||
77 | catch (IOException ex) {return null;} |
||
78 | } |
||
79 | } |
||
80 | } |