Code Search for Developers
 
 
  

AgentProgressIndicator.java from GridBlocks at Krugle


Show AgentProgressIndicator.java syntax highlighted

package fi.hip.gb.client.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Progressbar container that can contain multiple progress bars with 
 * addition information and stand-alone component like JButton.
 * <p>
 * Indicator is used in following way:
 * <ul><li>Call {@link AgentProgressIndicator#insertProgress(String, BoundedRangeModel, String, JComponent)}
 * as many times as you want progressbars to the indicator.
 * <li>Insert the AgentProgressIndicator into your UI
 * </ul>
 * <p>
 * When the status of BoundedRangeModel changes, it is automatically
 * reflected to the GUI component. This also means that values of the 
 * BoundedRangeModel need to be changed using setter -methods instead of 
 * replacing it.
 * 
 * @author Juho Karppinen
 **/
public class AgentProgressIndicator extends JPanel {
    private static final long serialVersionUID = 187637372610017973L;
    
    private GridBagConstraints con;
	private Hashtable<Container,Container> items = new Hashtable<Container,Container>();
	
	/**
	 * Creates a new empty AgentProgressIndicator object.
	 **/
	public AgentProgressIndicator() {
		super();
		
		setLayout(new GridBagLayout());
		con = new GridBagConstraints();

		// define distance between items
		con.insets = new Insets(0, 2, 0, 2);
		
		// default values
		con.gridy = -1;
		con.weighty = 0.0f;
		con.gridwidth = 1; con.gridheight = 1;
	}

	/**
	 * Create title around progress bars
	 * @param title title
	 */
	public void insertTitle(String title) {
		setBorder(BorderFactory.createTitledBorder(title));
	}

	/**
	 * Inserts two progressbars into same line
	 * 
	 * @param title text to be inserted before progressbars, can contain html
	 * expressions
	 * @param model1 model containing status informations. This object is
	 * monitored and changed values are automatically updated into progress bar
	 * @param regexp1 regular expression. Following values are updated with the
	 * values from the model:
	 * <ul><li>%value
	 * <li>%value_kb
	 * <li>%maximum
	 * <li>%maximum_kb
	 * </ul>
	 * @param model2 model for second progressbar
	 * @param regexp2 regular expression for second progressbar
	 */	
	public void insertProgress(String title, int[] model1,
		String regexp1, int[] model2, String regexp2) {
			Container item2 = new Container(model2, regexp2, null);
			insertProgress(title, model1, regexp1, item2, 1.0d);
	}

	/**
	 * Inserts new progress bar
	 * 
	 * @param title text to be inserted before progressbar, can contain html
	 * expressions
	 * @param model model containing status informations. This object is
	 * monitored and changed values are automatically updated into progress
	 * bar
	 * @param regexp regular expression. Following values are updated with the
	 * values from the model:
	 * <ul><li>%value
	 * <li>%value_kb
	 * <li>%maximum
	 * <li>%maximum_kb
	 * </ul>
	 * @param cmp component to be added after progress bar
	 */	
	public void insertProgress(String title, int[] model,
			String regexp, JComponent cmp) {
		insertProgress(title, model, regexp, cmp, 0.0d);
	}

	
	/**
	 * Inserts new progress bar
	 * 
	 * @param title text to be inserted before progressbar, can contain html
	 * expressions
	 * @param model model containing status informations. This object is
	 * monitored and changed values are automatically updated into progress
	 * bar
	 * @param regexp regular expression. Following values are updated with the
	 * values from the model:
	 * <ul><li>%value
	 * <li>%value_kb
	 * <li>%maximum
	 * <li>%maximum_kb
	 * </ul>
	 * @param cmp component to be added after progress bar
	 * @param weightx weight for the extra component
	 */	
	public void insertProgress(String title, int[] model,
		String regexp, JComponent cmp, double weightx) {

		con.gridy++;
		con.gridx = -1;
		Container item = new Container(model, regexp, cmp);

		if (title != null) {
			JLabel preLabel = new EnabledLabel(item, "<html>" + title + "</html>");
			preLabel.setHorizontalTextPosition(SwingConstants.LEFT);
			con.gridx++;
			con.weightx = 0.0f;
			con.anchor = GridBagConstraints.WEST;
			con.fill = GridBagConstraints.NONE;
			add(preLabel, con);
		}

		con.gridx++;
		con.weightx = 1.0f;
		con.anchor = GridBagConstraints.CENTER;
		con.fill = GridBagConstraints.HORIZONTAL;
		add(item, con);

		if (cmp != null) {
			con.gridx++;
			con.weightx = weightx;
			add(cmp, con);
		}

		items.put(item, item);
	}

	/**
	 * Container for one progressbar element. Can autonomously update
	 * text inside progressbar when state changes.
	 */
	private class Container extends JProgressBar {
        private static final long serialVersionUID = 328002775781423174L;

        /**
		 * Creates new container element
		 * @param model model where status is stored
		 * @param regexp regular expression
		 * @param cmp component to be added after progress bar
		 */
		public Container(int[] model, String regexp, JComponent cmp) {
			_regexp = regexp;
			_cmp = cmp;

			if (model != null)
				setModel(new DefaultBoundedRangeModel(model[0], 0, 0, model[1]));

			setBorderPainted(true);
			setPreferredSize(new Dimension(100, 18));
			setFont(getFont().deriveFont(10.0f));

			addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					updateState();
				}
			});

			// call first update manually, otherwise texts doesn't show up
			updateState();
			setStringPainted(true);
		}

		/**
		 * Gets the regexp string
		 * @return regexp for this item
		 */
		public String getRegexp() {
			return _regexp;
		}

		/**
		 * Gets the component bound to this item
		 * @return JComponent
		 */
		public JComponent getCmp() {
			return _cmp;
		}

		/**
		 * Updates progressbar and its state.
		 */
		private void updateState() {
			if (_regexp != null) {
				// replace all values with proper filesize string

				String filesizeValue = Integer.toString(getValue());
				String filesizeMax = getMaximum() + " b";

				// If size is over 1000 bytes, change scale to kilobytes
				if (getMaximum() > 1000) {
					filesizeValue = Math.round(getValue() / 100) / 10.f + "";
					if (filesizeValue.equals("0.0"))
						filesizeValue = "0";
					filesizeMax = Math.round(getMaximum() / 100) / 10.f + " KB";
				}

				//  replace current or maximum values in the place of following regexps
				String content = _regexp.replaceFirst("%value_kb", filesizeValue);
				content = content.replaceFirst("%value", Integer.toString(getValue()));
				content = content.replaceFirst("%maximum_kb", filesizeMax);
				content = content.replaceFirst("%maximum", Integer.toString(getMaximum()));

				setString(content);
			}
		}

		/** component which is inserted after progress bar */
		private JComponent _cmp;
		
		/** regular expression replaced with updated values straight from progressbar */
		private String _regexp;
	}
}




See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  myproxy/
    MyProxyGUI.java
    MyProxyOptions.java
    PasswordDialog.java
    package.html
  treetable/
    AbstractTreeTableModel.java
    DynamicTreeTableModel.java
    JTreeTable.java
    QuestionCellEditorModel.java
    RightClickAdapter.java
    TreeTableCellRenderer.java
    TreeTableModel.java
    TreeTableModelAdapter.java
    package.html
  AgentProgressIndicator.java
  EnabledLabel.java
  JobListItem.java
  SoundObserver.java
  TextObserver.java
  Utils.java
  Window.java
  package.html