Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  /*
   * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
   * subject to license terms.
   */ 
  
  package examples;
  
 import java.awt.Color;
A demo of the @Action block options for background task. It's an example of three of the Action.Block types:
 @Action(block = Task.BlockingScope.ACTION)  
 public Task blockAction() { ... }
 
 @Action(block = Task.BlockingScope.COMPONENT) 
 public Task blockComponent() { ... }
 
 @Action(block = Task.BlockingScope.WINDOW) 
 public Task blockWindow() { ... }
 
 @Action(block = Task.BlockingScope.APPLICATION)
 public Task blockApplication() { ... }
 
The first BlockingScope.ACTION @Action disables the corresponding Action while blockAction method runs. When you press the blockAction button or toolbar-button or menu item you'll observe that all of the components are disabled. The BlockingScope.COMPONENT version only disables the component that triggered the action. The Block.WINDOW method uses a custom inputBlocker to temporarily block input to the by making the window's glass pane visible. And the Task.BlockingScope.APPLICATION version pops up a modal dialog for the action's duration. The blocking dialog's title/message/icon are defined by resources from the ResourceBundle named BlockingExample1:
 BlockingDialog.title = Blocking Application 
 BlockingDialog.message = Please wait patiently ...
 Action.BlockingDialog.icon = wait.png
 

All of the actions in this example just sleep for about 2 seconds, while periodically updating their Task's message/progress properties.

This class loads resources from the ResourceBundle called BlockingExample1. It depends on the example StatusBar class.

 
 
public class BlockingExample1 extends SingleFrameApplication {
    private static Logger logger = Logger.getLogger(LocalStorage.class.getName());
    private JFrame mainFrame = null;
    private StatusBar statusBar = null;
    private BusyIndicator busyIndicator = null;
    @Override protected void startup() {
         = new BusyIndicator();
        show();
    }
    private ActionMap actionMap() {
    }
    private JMenu createMenu(String menuNameString[] actionNames) {
	JMenu menu = new JMenu();
	menu.setName(menuName);
	for (String actionName : actionNames) {
	    JMenuItem menuItem = new JMenuItem();
	    menuItem.setAction(actionMap().get(actionName));
	    menu.add(menuItem);
	}
	return menu;
    }
    private JMenuBar createMenuBar() {
	String[] demoMenuActionNames = {
	    "blockAction",
	    "blockComponent",
	    "blockApplication",
	    "blockWindow",
	    "quit"
	};
	JMenuBar menuBar = new JMenuBar();
	menuBar.add(createMenu("demoMenu"demoMenuActionNames));
	return menuBar;
    }
    private JComponent createToolBar() {
	String[] toolbarActionNames = {
	    "blockAction",
	    "blockComponent",
	    "blockApplication",
	    "blockWindow",
	};
	JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);
	for (String actionName : toolbarActionNames) {
	    JButton button = new JButton();
	    button.setRequestFocusEnabled(false);
	    button.setAction(actionMap().get(actionName));
	    button.setName(actionName + "ToolBarButton");
	    toolBar.add(button);
	}
	return toolBar;
    }
    private JComponent createMainPanel() {
	JButton actionButton = new JButton(actionMap().get("blockAction"));
	JButton componentButton = new JButton(actionMap().get("blockComponent"));
	JButton applicationButton = new JButton(actionMap().get("blockApplication"));
	JButton windowButton = new JButton(actionMap().get("blockWindow"));
	JPanel panel1 = new JPanel(new FlowLayout(., 10, 50));
	panel1.add(actionButton);
	panel1.add(componentButton);
	panel1.add(applicationButton);
	panel1.add(windowButton);
	JPanel panel2 = new JPanel(new BorderLayout());
	panel2.add(new JSeparator(), .);
	panel2.add(panel1.);
	panel2.setBorder(new EmptyBorder(0, 2, 0, 2)); // top, left, bottom, right
	return panel2;
    }
    /* Progress is interdeterminate for the first 150ms, then
     * run for another 7500ms, marking progress every 150ms.
     */
    private class DoNothingTask extends Task<VoidVoid> {
        DoNothingTask() {
            super(BlockingExample1.this);
            setUserCanCancel(true);
        }
	    for(int i = 0; i < 50; i++) {
		setMessage("Working... [" + i + "]");
		Thread.sleep(150L);
		setProgress(i, 0, 49);
	    }
	    Thread.sleep(150L);
            return null;
	}
	@Override protected void succeeded(Void ignored) {
	    setMessage("Done");
	}
	@Override protected void cancelled() {
	    setMessage("Canceled");
	}
    }
    @Action(block = .)
    public Task blockAction() {
	return new DoNothingTask();
    }
    @Action(block = .)
    public Task blockComponent() {
	return new DoNothingTask();
    }
    @Action(block = .)
    public Task blockWindow() {
        return new DoNothingTask();
    }
    @Action(block = .)
    public Task blockApplication() {
	Task task = new DoNothingTask();
        task.setInputBlocker(new BusyIndicatorInputBlocker(task));
        return task;
    }
    public static void main(String[] args) {
        Application.launch(BlockingExample1.classargs);
    }
    /* This component is intended to be used as a GlassPane.  It's
     * start method makes this component visible, consumes mouse
     * and keyboard input, and displays a spinning activity indicator 
     * animation.  The stop method makes the component not visible.
     * The code for rendering the animation was lifted from 
     * org.jdesktop.swingx.painter.BusyPainter.  I've made some
     * simplifications to keep the example small.
     */
    private static class BusyIndicator extends JComponent implements ActionListener {
        private int frame = -1;  // animation frame index
        private final int nBars = 8;  
        private final float barWidth = 6;
        private final float outerRadius = 28;  
        private final float innerRadius = 12; 
        private final int trailLength = 4;
        private final float barGray = 200f;  // shade of gray, 0-255
        private final Timer timer = new Timer(65, this); // 65ms = animation rate
        BusyIndicator() {
            setCursor(Cursor.getPredefinedCursor(.));
            MouseInputListener blockMouseEvents = new MouseInputAdapter() {};
            addMouseMotionListener(blockMouseEvents);
            addMouseListener(blockMouseEvents);
            InputVerifier retainFocusWhileVisible = new InputVerifier() { 
                public boolean verify(JComponent c) { 
                    return !c.isVisible(); 
                } 
            };
            setInputVerifier(retainFocusWhileVisible);
        }
        public void actionPerformed(ActionEvent ignored) {
             += 1;
            repaint();
        }
        void start() {
            setVisible(true);
            requestFocusInWindow();
            .start(); 
        }
        void stop() {
            setVisible(false);
            .stop(); 
        }
        @Override protected void paintComponent(Graphics g) {
            RoundRectangle2D bar = new RoundRectangle2D.Float(
                , -/2, );
                // x,         y,          width,       height,   arc width,arc height
            double angle = . * 2.0 / (double)// between bars
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(getWidth() / 2, getHeight() / 2);
            g2d.setRenderingHint(
            for (int i = 0; i < i++) {
                // compute bar i's color based on the frame index
                Color barColor = new Color((int), (int), (int));
                if ( != -1) {
                    for(int t = 0; t < t++) {
                        if (i == (( - t + ) % )) {
                            float tlf = (float);
                            float pct = 1.0f - ((tlf - t) / tlf);
                            int gray = (int)(( - (pct * )) + 0.5f);
                            barColor = new Color(graygraygray);
                        }
                    }
                }
                // draw the bar
                g2d.setColor(barColor);
                g2d.fill(bar);
                g2d.rotate(angle);
            }
        }
    }
    private class BusyIndicatorInputBlocker extends InputBlocker {
        BusyIndicatorInputBlocker(Task task) {
            super(task..);
        }
        @Override protected void block() {
            .start();
        }
        @Override protected void unblock() {
            .stop();
        }
    }
New to GrepCode? Check out our FAQ X