Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2009-2011, EzWare
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * Redistributions of source code must retain the above copyright notice,
  * this list of conditions and the following disclaimer.Redistributions
  * in binary form must reproduce the above copyright notice, this list of
  * conditions and the following disclaimer in the documentation and/or
  * other materials provided with the distribution.Neither the name of the
  * EzWare nor the names of its contributors may be used to endorse or
  * promote products derived from this software without specific prior
  * written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
  */
 
 package org.oxbow.swingbits.dialog.task;
 
 import java.awt.Color;
 import java.util.List;
 
 
 
A set of methods to simplify creation of task dialogs

Author(s):
Eugene Ryzhikov
 
 public final class TaskDialogs {
 
 	private TaskDialogs() {}
 
 	public static final class TaskDialogBuilder {
 
 		private Window parent = null;
 		private String title = null;
 		private Icon icon = null;
 		private String instruction = null;
 		private String text = null;
 		private Integer inputColumns = null;
 
 		private TaskDialogBuilder() {}

Sets dialog parent

Parameters:
title
Returns:
 
 		public TaskDialogBuilder parentWindow parent ) {
 			this. = parent;
 			return TaskDialogBuilder.this;
 		}


Sets dialog title

Parameters:
title
Returns:
		public TaskDialogBuilder titleString title ) {
			this. = title;
			return TaskDialogBuilder.this;
		}

Sets dialog icon

Parameters:
icon
Returns:
		public TaskDialogBuilder iconIcon icon ) {
			this. = icon;
			return TaskDialogBuilder.this;
		}

Sets instruction test

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
Returns:
		public TaskDialogBuilder instructionString instruction ) {
			this. = instruction;
			return TaskDialogBuilder.this;
		}

Sets dialog text

Parameters:
text
Returns:
		public TaskDialogBuilder textString text ) {
			this. = text;
			return TaskDialogBuilder.this;
		}

Sets number of columns in the text field for input dialog

Parameters:
columns
Returns:
		public TaskDialogBuilder inputColumnsint columns ) {
			this. = columns;
			return TaskDialogBuilder.this;
		}
		private String getTitleString defaultTitle ) {
			return  == nulldefaultTitle;
		}
		private Icon getIconIcon defaultIcon ) {
			return  == nulldefaultIcon;
		}


Shows simple message using previously set title, icon and instruction
		public void message() {
		}


Shows simple information message
		public void inform() {
					getTitle( TaskDialog.makeKey("Information")),
		}


Shows simple error message
		public void error() {
				getTitle( TaskDialog.makeKey("Error")),
		}

Shows simple question

Returns:
		public boolean ask() {
					getTitle( TaskDialog.makeKey("Question")),
		}

Shows simple warning message

Deprecated:
use isConfirmed instead
Returns:
		public boolean warn() {
					getTitle( TaskDialog.makeKey("Warning")),
		}


Shows simple warning message

Returns:
true if accepted
		public boolean isConfirmed() {
					getTitle( TaskDialog.makeKey("Warning")),
		}


Show exception dialog

Parameters:
ex
		public void showExceptionThrowable ex ) {
			TaskDialog dlg = new TaskDialoggetTitle(TaskDialog.makeKey("Exception")));
			String msg = ex.getMessage();
			String className = ex.getClass().getName();
			boolean noMessage = Strings.isEmpty(msg);
	        dlg.setInstructionnoMessageclassNamemsg );
	        dlg.setTextnoMessage""className );
	        dlg.setIcongetIcon.. ));
	        dlg.setCommands..derive(TaskDialog.makeKey("Close")));
			JTextArea text = new JTextArea();
			text.setEditable(false);
			text.setFont( UIManager.getFont("Label.font"));
			text.setText(Strings.stackStraceAsString(ex));
			JScrollPane scroller = new JScrollPanetext );
			scroller.setPreferredSize(new Dimension(400,200));
			dlg.getDetails().setExpanded(noMessage);
			dlg.setResizable(true);
			// Issue 22: Exception can be printed by user if required
			// ex.printStackTrace();
			dlg.setVisible(true);
		}

Simplifies the presentation of choice based on radio buttons

Parameters:
defaultChoice initial choice selection
choices collection of available choices
Returns:
selection index or -1 if nothing is selected
		public int radioChoiceint defaultChoiceList<Stringchoices ) {
			TaskDialog dlg = questionDialoggetTitle( TaskDialog.makeKey("Choice")), null);
			ButtonGroup bGroup = new ButtonGroup();
		    List<ButtonModelmodels = new ArrayList<ButtonModel>();
		    JRadioButton btn;
			JPanel p = new JPanelnew MigLayout(""));
			forString cchoices ) {
				btn = new JRadioButton(c);
				btn.setOpaque(false);
				models.addbtn.getModel());
				bGroup.add(btn);
				p.addbtn"dock north");
			}
			if ( defaultChoice >= 0 && defaultChoice < choices.size()) {
				bGroup.setSelected(models.get(defaultChoice), true);
			}
			p.setOpaque(false);
	        dlg.setCommands..derive(TaskDialog.makeKey("Select"), twi.getWaitInterval()),
	        		         . );
			return dlg.show().equals(.)? models.indexOfbGroup.getSelection()) : -1;
		}

Simplifies the presentation of choice based on radio buttons

Parameters:
defaultChoice initial choice selection
choices array of available choices
Returns:
selection index or -1 if nothing is selected
		public int radioChoiceint defaultChoiceString... choices ) {
			return radioChoicedefaultChoice, Arrays.asList(choices));
		}

Simplifies the presentation of choice based on check boxes Check boxes are wrapped into a scrollable check list if there are more than 7 of them

Parameters:
choices All available choices presented
defaultSelection choices checked by default
Returns:
collection of checked choices
		public <T> Collection<T> checkChoiceList<T> choicesCollection<T> defaultSelection ) {
			TaskDialog dlg = questionDialoggetTitle( TaskDialog.makeKey("Choice")), null);
            JList list = new JList();
            CheckList<T> checkList = new CheckList.Builder(list).build();
			checkList.setData(choices);
			checkList.setCheckedItems(defaultSelection);
			if ( choices.size() > 7 ) {
				dlg.setFixedComponentnew JScrollPane(list));	
else {
				// blend list color with dialog
				list.setBackground(listColor);
				list.setSelectionBackground(listColor);
			}
	        dlg.setCommands..derive(TaskDialog.makeKey("Select"), twi.getWaitInterval()),
	        		         . );
			return dlg.show().equals(.)? checkList.getCheckedItems() : null;
		}

Simplifies the presentation of choice based on command links

Parameters:
defaultChoice initial choice selection
choices collection of available command links
Returns:
selection index or -1 if nothing is selected
		public int choicefinal int defaultChoiceList<CommandLinkchoices ) {
			// NOTE: Task dialog has to be created first to initialize resources
			// Should resource initialization be done somewhere else (like design itself)?
					getTitle(TaskDialog.makeKey("Choice")), // localized title 
					getIcon(null), // null by default, according to MS ux guidlines 
		    final List<ButtonModelmodels = new ArrayList<ButtonModel>();
		    final List<CommandLinkButtonbuttons = new ArrayList<CommandLinkButton>();
		    CommandLinkButton btn;
			JPanel p = new JPanelnew MigLayout(""));
			p.setOpaque(false);
			forCommandLink linkchoices ) {
				btn = new CommandLinkButton(link, TaskDialog.getDesign().getCommandLinkPainter() );
				models.addbtn.getModel());
				buttons.addbtn );
				bGroup.add(btn);
				p.addbtn"dock north, gapbottom 8");
			}
			if ( defaultChoice >= 0 && defaultChoice < choices.size()) {
				bGroup.setSelected(models.get(defaultChoice), true);
				// make sure that selected button is focused
					public void ancestorAdded(AncestorEvent event) {
						buttons.get(defaultChoice).requestFocusInWindow();
					}
				});
			}
			return choices.indexOfdlg.show() );
		}

Simplifies the presentation of choice based on command links

Parameters:
defaultChoice initial choice selection
choices array of available command links
Returns:
selection index or -1 if nothing is selected
		public int choiceint defaultChoiceCommandLink... choices ) {
			return choicedefaultChoice, Arrays.asList(choices));
		}

Shows simple input dialog with one JFormattedTextField.

Parameters:
<T>
defaultValue
Returns:
edited value or null if dialog was canceled
		@SuppressWarnings("unchecked")
		public <T> T input( T defaultValue ) {
			TaskDialog dlg = questionDialoggetTitle( TaskDialog.makeKey("Input")), null);
			tfInput.setColumns == null? 25:  );
			tfInput.setValuedefaultValue );
			dlg.setFixedComponenttfInput );
			return (T) (dlg.show().equals(.)? tfInput.getValue(): null);
		}
	}


Creates task dialog builder

Returns:
	public static TaskDialogBuilder build() {
		return new TaskDialogBuilder();
	}

Creates task dialog builder

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
Returns:
	public static TaskDialogBuilder buildWindow parentString instructionString text) {
//		TaskDialogBuilder builder = new TaskDialogBuilder();
//		builder.parent( parent );
//		builder.instruction(instruction);
//		builder.text(text);
//		return builder;
		return build().parent(parent).instruction(instruction).text(text);
	}

Shows simple information message

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
	public static void informWindow parentString instructionString text ) {
		buildparentinstructiontext ).inform();
	}

Shows simple error message

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
	public static void errorWindow parentString instructionString text ) {
		buildparentinstructiontext ).error();
	}

Shows simple question

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
Returns:
	public static boolean askWindow parentString instructionString text ) {
		return buildparentinstructiontext ).ask();
	}

Shows simple warning message

Deprecated:
use isConfirmed instead
Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
Returns:
	public static boolean warnWindow parentString instructionString text ) {
		return buildparentinstructiontext ).warn();
	}

Shows simple warning message

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
Returns:
true if accepted
	public static boolean isConfirmedWindow parentString instructionString text ) {
		return buildparentinstructiontext ).isConfirmed();
	}

Shows exception with stack trace as expandable component.

Parameters:
ex
	public static void showExceptionThrowable ex ) {
	}

Simplifies the presentation of choice based on radio buttons

Parameters:
instruction instruction text. Wait interval can be added at the end. i.e. "@@10" will wait for 10 sec
text
defaultChoice initial choice selection
choices collection of available choices
Returns:
selection index or -1 if nothing is selected
	public static final int radioChoiceWindow parentString instructionString textint defaultChoiceList<Stringchoices ) {
		return build(parentinstructiontext).radioChoice(defaultChoicechoices);
	}



Simplifies the presentation of choice based on radio buttons

Parameters:
instruction
text
defaultChoice
choices
Returns:
	public static final int radioChoiceWindow parentString instructionString textint defaultChoiceString... choices ) {
		return buildparentinstructiontext).radioChoice(defaultChoicechoices);
	}

Produces choice dialog based on command links. Task dialog commands are suppressed.

Parameters:
instruction
text
defaultChoice command link index used as default choice. -1 is none is required
choices collection of command links
Returns:
	public static final int choiceWindow parentString instructionString textint defaultChoiceList<CommandLinkchoices ) {
		return build(parentinstruction,text).choicedefaultChoicechoices );
	}

Produces choice dialog based on command links. Task dialog commands are suppressed.

Parameters:
instruction
text
defaultChoice command link index used as default choice. -1 is none is required
choices array of command links
Returns:
	public static final int choiceWindow parentString instructionString textint defaultChoiceCommandLink... choices ) {
		return build(parentinstruction,text).choicedefaultChoicechoices );
	}

Shows simple input dialog with one JFormattedTextField.

Parameters:
<T>
defaultValue
Returns:
edited value or null if dialog was canceled
	public static final <T> T inputWindow parentString instructionString text, T defaultValue ) {
		return build(parentinstruction,text).inputColumns(25).inputdefaultValue );
	}
 /*----------------------------------------------------------------------------------------------------------*/
	private static class TextWithWaitInterval {
		int waitInterval = 0;
		public TextWithWaitIntervalString text ) {
			this. = text;
			int prefixPos = text.indexOf(.);
			if ( prefixPos >= 0) {
				try {
					 = Integer.valueOftext.substringprefixPos + ..length()));
catchThrowable ex ) {
				}
				this. = text.substring(0, prefixPos);
			}
		}
		public String getText() {
			return ;
		}
		public int getWaitInterval() {
			return ;
		}
	}


Creates Task dialog using provided attributes and one "Close" button

Parameters:
title
icon
instruction
text
Returns:
	private static TaskDialog messageDialogWindow parentString titleIcon iconString instructionString text ) {
		TextWithWaitInterval twi = new TextWithWaitInterval(instruction);
		TaskDialog dlg = new TaskDialog(parenttitle);
        dlg.setInstructiontwi.getText() );
        dlg.setTexttext );
        dlg.setIconicon );
        dlg.setCommands..derive(TaskDialog.makeKey("Close"), twi.getWaitInterval()));
        return dlg;
	}

Creates Task dialog using provided attributes and "Yes" and "No" buttons

Parameters:
title
icon
instruction
text
Returns:
	private static TaskDialog questionDialogWindow parentString titleIcon iconString instructionString text ) {
		TextWithWaitInterval twi = new TextWithWaitInterval(instruction);
		TaskDialog dlg = new TaskDialog(parenttitle);
        dlg.setInstructiontwi.getText() );
        dlg.setTexttext );
        dlg.setIconicon );
        dlg.setCommands(
        	..derive(TaskDialog.makeKey("Yes"), twi.getWaitInterval()),
        	..derive(TaskDialog.makeKey("No")) );
        return dlg;
	}
New to GrepCode? Check out our FAQ X