Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: /antelope/src/ise/antelope/common/gui/

antelope
Discussion topic

Back to topic list

CVS update: /antelope/src/ise/antelope/common/gui/

Author danson
Full name Dale Anson
Date 2004-02-27 21:03:36 PST
Message User: danson
Date: 04/02/27 21:03:36

Added:
 /antelope/src/ise/an​telope/common/gui/
  ListTransferHandler.java, StringTransferHandler.java, TreeTransferHandler.java

Log:
 dnd data handlers
 

File Changes:

Directory: /antelope/src/ise/an​telope/common/gui/
====================​====================​=========

File [added]: ListTransferHandler.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/common/gui/Li​stTransferHandler.ja​va?rev=1.1&conte​nt-type=text/vnd.vie​wcvs-markup
Added lines: 106
----------------
package ise.antelope.common.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;

/**
 * Borrowed from Sun's java tutorial.
 */
public class ListTransferHandler extends StringTransferHandler {
   private int[] indices = null;
   private int addIndex = -1; //Location where items were added
   private int addCount = 0; //Number of items added.

   //Bundle up the selected items in the list
   //as a single string, for export.
   protected String exportString( JComponent c ) {
      JList list = ( JList ) c;
      indices = list.getSelectedIndices();
      Object[] values = list.getSelectedValues();

      StringBuffer buff = new StringBuffer();

      for ( int i = 0; i < values.length; i++ ) {
         Object val = values[ i ];
         buff.append( val == null ? "" : val.toString() );
         if ( i != values.length - 1 ) {
            buff.append( "\n" );
         }
      }

      return buff.toString();
   }

   //Take the incoming string and wherever there is a
   //newline, break it into a separate item in the list.
   protected void importString( JComponent c, String str ) {
      JList target = ( JList ) c;
      DefaultListModel listModel = ( DefaultListModel ) target.getModel();
      int index = target.getSelectedIndex();

      //Prevent the user from dropping data back on itself.
      //For example, if the user is moving items #4,#5,#6 and #7 and
      //attempts to insert the items after item #5, this would
      //be problematic when removing the original items.
      //So this is not allowed.
      if ( indices != null && index >= indices[ 0 ] - 1 &&
              index <= indices[ indices.length - 1 ] ) {
         indices = null;
         return ;
      }

      int max = listModel.getSize();
      if ( index < 0 ) {
         index = max;
      }
      else {
         index++;
         if ( index > max ) {
            index = max;
         }
      }
      addIndex = index;
      /*
      String target_name = JOptionPane.showInputDialog( c, "Enter target name:", "Target Name", JOptionPane.QUESTION_MESSAGE );
      if ( target_name != null ) {
         TargetBuilder tb = new TargetBuilder( target_name );
         listModel.add( index++, new TargetBuilder( target_name ) );
      }
      */
      String[] values = str.split( "\n" );
      addCount = values.length;
      for ( int i = 0; i < values.length; i++ ) {
         //listModel.add( index++, values[ i ] );
         listModel.add(index++, str);
      }
   }

   //If the remove argument is true, the drop has been
   //successful and it's time to remove the selected items
   //from the list. If the remove argument is false, it
   //was a Copy operation and the original list is left
   //intact.
   protected void cleanup( JComponent c, boolean remove ) {
      if ( remove && indices != null ) {
         JList source = ( JList ) c;
         DefaultListModel model = ( DefaultListModel ) source.getModel();
         //If we are moving items around in the same list, we
         //need to adjust the indices accordingly, since those
         //after the insertion point have moved.
         if ( addCount > 0 ) {
            for ( int i = 0; i < indices.length; i++ ) {
               if ( indices[ i ] > addIndex ) {
                  indices[ i ] += addCount;
               }
            }
         }
         for ( int i = indices.length - 1; i >= 0; i-- ) {
            model.remove( indices[ i ] );
         }
      }
      indices = null;
      addCount = 0;
      addIndex = -1;
   }
}

File [added]: StringTransferHandler.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/common/gui/St​ringTransferHandler.​java?rev=1.1&con​tent-type=text/vnd.v​iewcvs-markup
Added lines: 50
---------------
package ise.antelope.common.gui;

import java.awt.datatransfer.*;
import javax.swing.*;
import java.io.IOException;

/**
 * Borrowed from Sun's Java Tutorial.
 */
public abstract class StringTransferHandler extends TransferHandler {

   protected abstract String exportString( JComponent c );
   protected abstract void importString( JComponent c, String str );
   protected abstract void cleanup( JComponent c, boolean remove );

   protected Transferable createTransferable( JComponent c ) {
      return new StringSelection( exportString( c ) );
   }

   public int getSourceActions( JComponent c ) {
      return COPY_OR_MOVE;
   }

   public boolean importData( JComponent c, Transferable t ) {
      if ( canImport( c, t.getTransferDataFlavors() ) ) {
         try {
            String str = ( String ) t.getTransferData( DataFlavor.stringFlavor );
            importString( c, str );
            return true;
         }
         catch ( UnsupportedFlavorException ufe ) {}
         catch ( IOException ioe ) {}
      }

      return false;
   }

   protected void exportDone( JComponent c, Transferable data, int action ) {
      cleanup( c, action == MOVE );
   }

   public boolean canImport( JComponent c, DataFlavor[] flavors ) {
      for ( int i = 0; i < flavors.length; i++ ) {
         if ( DataFlavor.stringFlavor.equals( flavors[ i ] ) ) {
            return true;
         }
      }
      return false;
   }
}

File [added]: TreeTransferHandler.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/common/gui/Tr​eeTransferHandler.ja​va?rev=1.1&conte​nt-type=text/vnd.vie​wcvs-markup
Added lines: 40
---------------
package ise.antelope.common.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import javax.swing.tree.*;

/**
 * Modeled after code from Sun's java tutorial.
 */
public class TreeTransferHandler extends StringTransferHandler {
   private int[] indices = null;
   private int addIndex = -1; //Location where items were added
   private int addCount = 0; //Number of items added.
   
   public boolean canImport(JComponent c, DataFlavor[] df) {
      return false;
   }

   protected String exportString( JComponent c ) {
      JTree tree = (JTree)c;
      TreePath tp = tree.getSelectionPath();
      if (tp == null)
         return null;
      return "node: " + tp.toString();
   }

   //Take the incoming string and wherever there is a
   //newline, break it into a separate item in the list.
   protected void importString( JComponent c, String str ) {
   }

   //If the remove argument is true, the drop has been
   //successful and it's time to remove the selected items
   //from the list. If the remove argument is false, it
   //was a Copy operation and the original list is left
   //intact.
   protected void cleanup( JComponent c, boolean remove ) {
   }
}




--------------------​--------------------​--------------------​---------
To unsubscribe, e-mail: cvs-unsubscribe@ante​lope.tigris.org
For additional commands, e-mail: cvs-help at antelope dot tigris dot org

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

CVS update: /antelope/src/ise/antelope/common/gui/ danson Dale Anson 2004-02-27 21:03:36 PST
Messages per page: