Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package uk.ac.starlink.connect;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.filechooser.FileView;
/**
* JComboBox which allows selection of {@link Branch} objects.
* For any branch in the box's model, all its ancestors are automatically
* in the model too (this behaviour is inspired by widgets like
* {@link javax.swing.JFileChooser}).
*
* @author Mark Taylor (Starlink)
* @since 18 Feb 2005
*/
public class BranchComboBox extends JComboBox {
private BranchComboBoxModel model_;
private static FileView fileView_;
/**
* Constructor.
*/
public BranchComboBox() {
super( new BranchComboBoxModel() );
assert model_ instanceof BranchComboBoxModel;
setRenderer( new BranchCellRenderer() );
}
/**
* Sets this combo box's model. Will only accept a suitable model
* (one acquired from another BranchComboBox).
*
* @param model model
* @throws ClassCastException if it's the wrong type
*/
public void setModel( ComboBoxModel model ) {
model_ = (BranchComboBoxModel) model;
super.setModel( model );
Object selected = model.getSelectedItem();
int state = selected == null ? ItemEvent.DESELECTED
: ItemEvent.SELECTED;
fireItemStateChanged( new ItemEvent( this, 0, selected, state ) );
}
/**
* Sets the selected branch. This may or may not have the effect of
* adding or subtracting branches from the box's model.
*
* @param branch branch to form the current selection
*/
public void setSelectedBranch( Branch branch ) {
model_.setSelectedBranch( branch );
}
/**
* Returns the currently selected branch.
*
* @return current branch
*/
public Branch getSelectedBranch() {
return model_.getSelectedBranch();
}
/**
* Adds a new normal branch to the model.
*
* @param branch branch to add
*/
public void addBranch( Branch branch ) {
model_.addBranch( branch );
}
/**
* Adds a new branch to the model which represents a
* Connector.
*
* @param connAct connector action to be represented by a new branch
*/
public void addConnection( ConnectorAction connAct ) {
model_.addBranch( new ConnectorBranch( connAct ) );
}
/**
* Returns any connector action which is assocated with the currently
* selected branch. This will return null unless the current branch
* represents a Connector.
*
* @return connector action associated with current selection, if any
* @see #addConnection
*/
public ConnectorAction getConnectorAction() {
return model_.getConnectorAction();
}
/**
* Returns a FileView object which can be used to interpret files on
* this platform.
*
* @return FileView
*/
public static FileView getFileView() {
if ( fileView_ == null ) {
JFileChooser fc = new JFileChooser();
fileView_ = fc.getUI().getFileView( fc );
}
return fileView_;
}
/**
* Returns the connector, if any, associated with a branch.
* It will be null unless the branch is a ConnectorBranch.
*/
private static Connector getConnector( Branch branch ) {
return branch instanceof ConnectorBranch
? ((ConnectorBranch) branch).getConnectorAction().getConnector()
: null;
}
/**
* Model for the combo box.
*/
private static class BranchComboBoxModel extends AbstractListModel
implements ComboBoxModel {
/* The basic data structure in which the model's data is held is
* an array of N BranchHolder objects.
* However this corresponds to >= N items, since the model
* is considered to hold all the ancestors of each branch.
* The selected_ member points to the currently selected
* BranchHolder; the currently selected branch is the terminal
* node of the selected branch. */
private BranchHolder[] holders_ = new BranchHolder[ 0 ];
private int selected_ = -1;
private static final FileView fView_ = getFileView();
public int getSize() {
int size = 0;
for ( int i = 0; i < holders_.length; i++ ) {
size += holders_[ i ].getDepth();
}
return size;
}
public Branch getElementAt( int index ) {
for ( int i = 0; i < holders_.length; i++ ) {
BranchHolder holder = holders_[ i ];
int depth = holder.getDepth();
if ( index < depth ) {
return holder.getAncestor( index );
}
else {
index -= depth;
}
}
return null;
}
public Object getSelectedItem() {
return getSelectedBranch();
}
public void setSelectedItem( Object branch ) {
setSelectedBranch( (Branch) branch );
}
public Branch getSelectedBranch() {
return selected_ >= 0 ? holders_[ selected_ ].getBranch()
: null;
}
/**
* Sets the selected branch to a given value.
* If the given branch has the same root as one of the
* existing holders, that holder's branch is set to the one
* given and it is marked selected. If the given branch is
* not rooted at the same place as any of the existing ones,
* a new holder is added containing it.
*
* @param branch new selection
*/
public void setSelectedBranch( Branch branch ) {
Branch root = new BranchHolder( branch ).getRoot();
for ( int i = 0; i < holders_.length; i++ ) {
if ( root.equals( holders_[ i ].getRoot() ) ) {
holders_[ i ].setBranch( branch );
selected_ = i;
fireContentsChanged( this, -1, -1 );
return;
}
}
addBranch( branch );
selected_ = holders_.length - 1;
fireContentsChanged( this, -1, -1 );
}
public void addBranch( Branch branch ) {
int oldSize = getSize();
final BranchHolder holder = new BranchHolder( branch );
List hlist =
new ArrayList( Arrays.asList( holders_ ) );
hlist.add( holder );
holders_ = hlist.toArray( new BranchHolder[ 0 ] );
fireIntervalAdded( this,
Math.max( 0, oldSize - 1 ), getSize() - 1 );
/* If it's a connector branch, do some additional work.
* Make sure that this model reacts properly to
* the connection going up or down. */
if ( branch instanceof ConnectorBranch ) {
final ConnectorBranch connBranch = (ConnectorBranch) branch;
connBranch.getConnectorAction()
.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange( PropertyChangeEvent evt ) {
if ( evt.getPropertyName()
.equals( ConnectorAction
.CONNECTION_PROPERTY ) ) {
Connection conn = (Connection)
evt.getNewValue();
int oldSize = getSize();
if ( conn == null ) {
holder.setBranch( connBranch );
}
else {
holder.setBranch( conn.getRoot() );
}
int maxSize = Math.max( oldSize, getSize() );
fireContentsChanged( this, 0, maxSize );
}
}
} );
}
}
public ConnectorAction getConnectorAction() {
return selected_ >= 0 ? holders_[ selected_ ].getConnectorAction()
: null;
}
Icon getCustomIcon( Branch branch ) {
if ( branch instanceof FileBranch && fView_ != null ) {
return fView_.getIcon( ((FileBranch) branch).getFile() );
}
Branch root = new BranchHolder( branch ).getRoot();
for ( int i = 0; i < holders_.length; i++ ) {
BranchHolder holder = holders_[ i ];
if ( root.equals( holder.getRoot() ) ) {
ConnectorAction connAct = holder.getConnectorAction();
if ( connAct != null ) {
return connAct.getConnector().getIcon();
}
}
}
return null;
}
}
/**
* Renderer used for a BranchComboBox.
*/
private static class BranchCellRenderer
implements ListCellRenderer