@@ -89,44 +89,7 @@ private void init() {
8989 private void setupDragAndDrop () {
9090 this .levelsList .setDragEnabled (true );
9191 this .levelsList .setDropMode (DropMode .INSERT );
92- this .levelsList .setTransferHandler (new TransferHandler () {
93- private int index = -1 ;
94-
95- @ Override
96- public int getSourceActions (JComponent c ) {
97- return MOVE ;
98- }
99-
100- @ Override
101- protected Transferable createTransferable (JComponent c ) {
102- this .index = SortOptionsPage .this .levelsList .getSelectedIndex ();
103- return new StringSelection (SortOptionsPage .this .levelsList .getSelectedValue ());
104- }
105-
106- @ Override
107- public boolean canImport (TransferSupport support ) {
108- return support .isDataFlavorSupported (DataFlavor .stringFlavor );
109- }
110-
111- @ Override
112- public boolean importData (TransferSupport support ) {
113- if (!this .canImport (support )) return false ;
114- JList .DropLocation dl = (JList .DropLocation ) support .getDropLocation ();
115- int dropIndex = dl .getIndex ();
116- try {
117- String data = (String ) support .getTransferable ().getTransferData (DataFlavor .stringFlavor );
118- if (this .index != -1 ) {
119- SortOptionsPage .this .selectedLevelsModel .remove (this .index );
120- if (dropIndex > this .index ) dropIndex --;
121- }
122- SortOptionsPage .this .selectedLevelsModel .add (dropIndex , data );
123- SortOptionsPage .this .updatePreview ();
124- return true ;
125- } catch (Exception e ) {
126- return false ;
127- }
128- }
129- });
92+ this .levelsList .setTransferHandler (new SortTransferHandler ());
13093 }
13194
13295 private void updatePreview () {
@@ -150,7 +113,9 @@ private void updatePreview() {
150113 break ;
151114 }
152115 }
153- if (!matches ) return ;
116+ if (!matches ) {
117+ return ;
118+ }
154119 }
155120
156121 DefaultMutableTreeNode currentNode = this .rootNode ;
@@ -171,7 +136,9 @@ private void updatePreview() {
171136 private DefaultMutableTreeNode getOrCreateChild (DefaultMutableTreeNode parent , String name ) {
172137 for (int i = 0 ; i < parent .getChildCount (); i ++) {
173138 DefaultMutableTreeNode child = (DefaultMutableTreeNode ) parent .getChildAt (i );
174- if (child .getUserObject ().equals (name )) return child ;
139+ if (child .getUserObject ().equals (name )) {
140+ return child ;
141+ }
175142 }
176143 DefaultMutableTreeNode newNode = new DefaultMutableTreeNode (name );
177144 parent .add (newNode );
@@ -257,4 +224,48 @@ private void handleRevert() {
257224 JOptionPane .showMessageDialog (this , "Error: " + ex .getMessage ());
258225 }
259226 }
227+
228+ private class SortTransferHandler extends TransferHandler {
229+ private int index = -1 ;
230+
231+ @ Override
232+ public int getSourceActions (JComponent c ) {
233+ return MOVE ;
234+ }
235+
236+ @ Override
237+ protected Transferable createTransferable (JComponent c ) {
238+ this .index = SortOptionsPage .this .levelsList .getSelectedIndex ();
239+ return new StringSelection (SortOptionsPage .this .levelsList .getSelectedValue ());
240+ }
241+
242+ @ Override
243+ public boolean canImport (TransferSupport support ) {
244+ return support .isDataFlavorSupported (DataFlavor .stringFlavor );
245+ }
246+
247+ @ Override
248+ public boolean importData (TransferSupport support ) {
249+ if (!this .canImport (support )) {
250+ return false ;
251+ }
252+
253+ JList .DropLocation dl = (JList .DropLocation ) support .getDropLocation ();
254+ int dropIndex = dl .getIndex ();
255+ try {
256+ String data = (String ) support .getTransferable ().getTransferData (DataFlavor .stringFlavor );
257+ if (this .index != -1 ) {
258+ SortOptionsPage .this .selectedLevelsModel .remove (this .index );
259+ if (dropIndex > this .index ) {
260+ dropIndex --;
261+ }
262+ }
263+ SortOptionsPage .this .selectedLevelsModel .add (dropIndex , data );
264+ SortOptionsPage .this .updatePreview ();
265+ return true ;
266+ } catch (Exception e ) {
267+ return false ;
268+ }
269+ }
270+ }
260271}
0 commit comments