public static JDialog createDialog(Component c, String title,
			boolean modal, final JColorChooser chooserPane,
			ActionListener okListener, ActionListener cancelListener)
			throws HeadlessException {

		final Color initialColor = chooserPane.getColor();

		Window window = getWindowForComponent(c);
		final JDialog ret;
		if (window instanceof Frame) {
			ret = new JDialog((Frame) window, title, modal);
		} else {
			ret = new JDialog((Dialog) window, title, modal);
		}

		ret.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		Locale locale = window.getLocale();
		String okString = UIManager.getString("ColorChooser.okText", locale);
		String cancelString = UIManager.getString("ColorChooser.cancelText",
				locale);
		String resetString = UIManager.getString("ColorChooser.resetText",
				locale);

		ret.setLayout(new BorderLayout());
		ret.add(chooserPane, BorderLayout.CENTER);

		JPanel buttons = new JPanel();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		ret.add(buttons, BorderLayout.SOUTH);

		JButton okB = new JButton(okString);
		okB.getAccessibleContext().setAccessibleDescription(okString);
		okB.setActionCommand("OK");
		okB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ret.setVisible(false);
			}
		});
		if (okListener != null) {
			okB.addActionListener(okListener);
		}
		buttons.add(okB);

		final JButton cancelB = new JButton(cancelString);
		cancelB.getAccessibleContext().setAccessibleDescription(cancelString);

		// The following few lines are used to register esc to close the dialog
		Action cancelKeyAction = new AbstractAction() {
			private static final long serialVersionUID = 6313146026842426365L;

			public void actionPerformed(ActionEvent e) {
				cancelB.doClick();
			}
		};
		KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,
				0);
		InputMap inputMap = cancelB
				.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = cancelB.getActionMap();
		if (inputMap != null && actionMap != null) {
			inputMap.put(cancelKeyStroke, "cancel");
			actionMap.put("cancel", cancelKeyAction);
		}
		// end esc handling

		cancelB.setActionCommand("cancel");
		cancelB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ret.setVisible(false);
			}
		});
		if (cancelListener != null) {
			cancelB.addActionListener(cancelListener);
		}
		buttons.add(cancelB);

		JButton resetB = new JButton(resetString);
		resetB.getAccessibleContext().setAccessibleDescription(resetString);
		resetB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chooserPane.setColor(initialColor);
			}
		});
		buttons.add(resetB);

		if (JDialog.isDefaultLookAndFeelDecorated()) {
			boolean supportsWindowDecorations = UIManager.getLookAndFeel()
					.getSupportsWindowDecorations();
			if (supportsWindowDecorations) {
				ret.getRootPane().setWindowDecorationStyle(
						JRootPane.COLOR_CHOOSER_DIALOG);
			}
		}
		ret.applyComponentOrientation(((c == null) ? ret.getRootPane() : c)
				.getComponentOrientation());

		ret.pack();
		ret.setLocationRelativeTo(c);
		ret.getRootPane().setDefaultButton(okB);
		ret.getAccessibleContext().setAccessibleDescription(title);

		return ret;
	}