/*
* Created on 23.06.2004
*/
package forum;
import java.util.TreeSet;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Editor {
public static void main( String[] args ){
JList list = new JList( new String[]{ "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"} );
list.setSelectionModel( new Selection() );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( new JScrollPane( list ) );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
class Selection implements ListSelectionModel{
private TreeSet selected = new TreeSet();
private Vector listener = new Vector();
private boolean adjusting = false;
private int anchor = -1, lead = -1;
public boolean isSelectable( int index ){
return index % 2 == 0;
}
public void setSelectionInterval(int index0, int index1) {
boolean old = adjusting;
adjusting = true;
clearSelection();
adjusting = old;
setAnchorAndLead( index0, index1 );
addSelectionInterval( index0, index1 );
}
public void addSelectionInterval(int index0, int index1) {
int a = Math.min( index0, index1 );
int b = Math.max( index0, index1 );
for( int i = a; i <= b; i++ )
if( isSelectable( i ))
selected.add( new Integer( i ));
setAnchorAndLead( index0, index1 );
fireValueChanged( new ListSelectionEvent( this, index0, index1, false ) );
}
public void removeSelectionInterval(int index0, int index1) {
int a = Math.min( index0, index1 );
int b = Math.max( index0, index1 );
for( int i = a; i <= b; i++ )
selected.remove( new Integer( i ));
setAnchorAndLead( index0, index1 );
fireValueChanged( new ListSelectionEvent( this, index0, index1, false ) );
}
public int getMinSelectionIndex() {
if( isSelectionEmpty() )
return -1;
else
return ((Integer)selected.first()).intValue();
}
public int getMaxSelectionIndex() {
if( isSelectionEmpty() )
return -1;
else
return ((Integer)selected.last()).intValue();
}
public boolean isSelectedIndex(int index) {
return selected.contains( new Integer( index ) );
}
public int getAnchorSelectionIndex() {
return anchor;
}
private void setAnchorAndLead( int anchor, int lead ){
boolean old = adjusting;
adjusting = true;
setAnchorSelectionIndex( anchor );
setLeadSelectionIndex( lead );
adjusting = old;
}
public void setAnchorSelectionIndex(int index) {
if( anchor != index ){
anchor = index;
fireValueChanged( new ListSelectionEvent( this, Math.min( anchor, index )-1,
Math.max( anchor, index )+1, adjusting ) );
}
}
public int getLeadSelectionIndex() {
return lead;
}
public void setLeadSelectionIndex(int index) {
if( lead != index ){
lead = index;
fireValueChanged( new ListSelectionEvent( this, Math.min( lead, index )-1,
Math.max( lead, index )+1, adjusting ) );
}
}
public void clearSelection() {
if( !isSelectionEmpty() ){
int max = getMaxSelectionIndex();
int min = getMinSelectionIndex();
selected.clear();
setAnchorAndLead( -1, -1 );
fireValueChanged( new ListSelectionEvent( this, min, max, adjusting ) );
}
}
public boolean isSelectionEmpty() {
return selected.isEmpty();
}
public void insertIndexInterval(int index, int length, boolean before) {
if( before )
addSelectionInterval( index-1, index-2+length );
else
addSelectionInterval( index+1, index+length );
}
public void removeIndexInterval(int index0, int index1) {
int a = Math.min( index0, index1 );
int b = Math.max( index0, index1 );
for( int i = a; i <= b; i++ )
selected.remove( new Integer( i ) );
setAnchorAndLead( index0, index1 );
fireValueChanged( new ListSelectionEvent( this, index0, index1, false ));
}
public void setValueIsAdjusting(boolean valueIsAdjusting) {
adjusting = valueIsAdjusting;
}
public boolean getValueIsAdjusting() {
return adjusting;
}
public void setSelectionMode(int selectionMode) {
}
public int getSelectionMode() {
return MULTIPLE_INTERVAL_SELECTION;
}
protected void fireValueChanged( ListSelectionEvent event ){
for( int i = 0, n = listener.size(); i<n; i++ )
((ListSelectionListener)listener.get(i)).valueChanged( event );
}
public void addListSelectionListener(ListSelectionListener x) {
listener.add( x );
}
public void removeListSelectionListener(ListSelectionListener x) {
listener.remove( x );
}
}
/*
* Created on 23.06.2004
*/
package forum;
import java.util.TreeSet;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Editor {
public static void main( String[] args ){
JList list = new JList( new String[]{ "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"} );
list.setSelectionModel( new Selection() );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( new JScrollPane( list ) );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
class Interval{
private int lower, upper;
public Interval(){
}
public Interval( int lower, int upper ){
setLower( lower );
setUpper( upper );
}
public int getLower() {
return lower;
}
public void setLower(int lower) {
this.lower = lower;
}
public int getUpper() {
return upper;
}
public void setUpper(int upper) {
this.upper = upper;
}
}
class Selection implements ListSelectionModel{
private TreeSet selected = new TreeSet();
private Vector listener = new Vector();
private boolean adjusting = false;
private int anchor = -1, lead = -1;
public boolean isSelectable( int index ){
return index % 2 == 0;
}
public void setSelectionInterval(int index0, int index1) {
boolean old = adjusting;
adjusting = true;
clearSelection();
adjusting = old;
setAnchorAndLead( index0, index1 );
addSelectionInterval( index0, index1 );
}
public void addSelectionInterval(int index0, int index1) {
int a = Math.min( index0, index1 );
int b = Math.max( index0, index1 );
for( int i = a; i <= b; i++ )
if( isSelectable( i ))
selected.add( new Integer( i ));
setAnchorAndLead( index0, index1 );
fireValueChanged( new ListSelectionEvent( this, index0, index1, false ) );
}
public void removeSelectionInterval(int index0, int index1) {
int a = Math.min( index0, index1 );
int b = Math.max( index0, index1 );
for( int i = a; i <= b; i++ )
selected.remove( new Integer( i ));
setAnchorAndLead( index0, index1 );
fireValueChanged( new ListSelectionEvent( this, index0, index1, false ) );
}
public int getMinSelectionIndex() {
if( isSelectionEmpty() )
return -1;
else
return ((Integer)selected.first()).intValue();
}
public int getMaxSelectionIndex() {
if( isSelectionEmpty() )
return -1;
else
return ((Integer)selected.last()).intValue();
}
public boolean isSelectedIndex(int index) {
return selected.contains( new Integer( index ) );
}
public int getAnchorSelectionIndex() {
return anchor;
}
private void setAnchorAndLead( int anchor, int lead ){
boolean old = adjusting;
adjusting = true;
setAnchorSelectionIndex( anchor );
setLeadSelectionIndex( lead );
adjusting = old;
}
public void setAnchorSelectionIndex(int index) {
if( anchor != index ){
anchor = index;
fireValueChanged( new ListSelectionEvent( this, Math.min( anchor, index )-1,
Math.max( anchor, index )+1, adjusting ) );
}
}
public int getLeadSelectionIndex() {
return lead;
}
public void setLeadSelectionIndex(int index) {
if( lead != index ){
lead = index;
fireValueChanged( new ListSelectionEvent( this, Math.min( lead, index )-1,
Math.max( lead, index )+1, adjusting ) );
}
}
public void clearSelection() {
if( !isSelectionEmpty() ){
int max = getMaxSelectionIndex();
int min = getMinSelectionIndex();
selected.clear();
setAnchorAndLead( -1, -1 );
fireValueChanged( new ListSelectionEvent( this, min, max, adjusting ) );
}
}
public boolean isSelectionEmpty() {
return selected.isEmpty();
}
public void insertIndexInterval(int index, int length, boolean before) {
if( before )
addSelectionInterval( index-1, index-2+length );
else
addSelectionInterval( index+1, index+length );
}
public void removeIndexInterval(int index0, int index1) {
int a = Math.min( index0, index1 );
int b = Math.max( index0, index1 );
for( int i = a; i <= b; i++ )
selected.remove( new Integer( i ) );
setAnchorAndLead( index0, index1 );
fireValueChanged( new ListSelectionEvent( this, index0, index1, false ));
}
public void setValueIsAdjusting(boolean valueIsAdjusting) {
adjusting = valueIsAdjusting;
}
public boolean getValueIsAdjusting() {
return adjusting;
}
public void setSelectionMode(int selectionMode) {
}
public int getSelectionMode() {
return MULTIPLE_INTERVAL_SELECTION;
}
protected void fireValueChanged( ListSelectionEvent event ){
for( int i = 0, n = listener.size(); i<n; i++ )
((ListSelectionListener)listener.get(i)).valueChanged( event );
}
public void addListSelectionListener(ListSelectionListener x) {
listener.add( x );
}
public void removeListSelectionListener(ListSelectionListener x) {
listener.remove( x );
}
}