Debian Codegraph @

src/com/jidesoft/plaf/basic/BasicJideOptionPaneUI.java

raw
1 /*
2 * @(#)BasicJideOptionPaneUI.java 3/27/2006
3 *
4 * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.plaf.basic;
8
9 import com.jidesoft.dialog.ButtonNames;
10 import com.jidesoft.dialog.ButtonPanel;
11 import com.jidesoft.dialog.ButtonResources;
12 import com.jidesoft.dialog.JideOptionPane;
13 import com.jidesoft.plaf.UIDefaultsLookup;
14 import com.jidesoft.swing.JideBoxLayout;
15 import com.jidesoft.swing.JideSwingUtilities;
16 import com.jidesoft.swing.NullPanel;
17 import com.jidesoft.swing.PaintPanel;
18
19 import javax.swing.*;
20 import javax.swing.border.Border;
21 import javax.swing.event.AncestorEvent;
22 import javax.swing.event.AncestorListener;
23 import javax.swing.plaf.ComponentUI;
24 import javax.swing.plaf.basic.BasicOptionPaneUI;
25 import java.awt.*;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.beans.PropertyChangeEvent;
29 import java.beans.PropertyChangeListener;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Locale;
33 import java.util.ResourceBundle;
34
35 public class BasicJideOptionPaneUI extends BasicOptionPaneUI {
36 private Container _detailsArea;
37 private JButton _detailsButton;
38 private Container _buttonArea;
39 private Container _bannerArea;
40 private Component _titleComponent;
41
42 private ThemePainter _painter;
43 private PropertyChangeListener _propertyListener;
44
45 // keep track of detail component width.
46 private int _detailsPreferredWidth = -1;
47
48 /**
49 * Creates a new BasicOptionPaneUI instance.
50 *
51 * @param x the component to create UI
52 * @return the UI instance.
53 */
54 public static ComponentUI createUI(JComponent x) {
55 return new BasicJideOptionPaneUI();
56 }
57
58 @Override
59 protected LayoutManager createLayoutManager() {
60 return new JideBoxLayout(optionPane, JideBoxLayout.Y_AXIS);
61 }
62
63 // use a static to keep track of detail area visibility during the session
64 private boolean _detailsVisible = false;
65
66 /**
67 * Is details area visible initially.
68 *
69 * @return true if details area is visible.
70 */
71 public boolean isDetailsVisible() {
72 return _detailsVisible;
73 }
74
75 /**
76 * Sets if details area is visible initially.
77 *
78 * @param detailsVisible the flag
79 */
80 public void setDetailsVisible(boolean detailsVisible) {
81 if (_detailsVisible != detailsVisible) {
82 _detailsVisible = detailsVisible;
83 toggleDetailsArea();
84 }
85 }
86
87 @Override
88 protected void installComponents() {
89 boolean showBanner = UIDefaultsLookup.get("OptionPane.showBanner") == null || UIDefaultsLookup.getBoolean("OptionPane.showBanner");
90 if (showBanner) {
91 optionPane.add(_bannerArea = createBannerArea(), JideBoxLayout.FIX);
92 }
93
94 Container messageArea = createMessageArea();
95 if (!showBanner) {
96 addIcon(messageArea);
97 }
98 LookAndFeel.installBorder((JComponent) messageArea, "OptionPane.border");
99 optionPane.add(messageArea);
100
101 Container separator = createSeparator();
102 if (separator != null) {
103 optionPane.add(separator);
104 }
105
106 optionPane.add(_buttonArea = createButtonArea(), JideBoxLayout.FIX);
107 optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
108
109 if (shouldDetailsButtonVisible()) {
110 updateDetailsComponent();
111 }
112 }
113
114 @Override
115 protected void installListeners() {
116 super.installListeners();
117 _propertyListener = new PropertyChangeListener() {
118 public void propertyChange(PropertyChangeEvent evt) {
119 if (JideOptionPane.DETAILS_PROPERTY.equals(evt.getPropertyName())) {
120 updateDetailsComponent();
121 if (_buttonArea instanceof ButtonPanel) {
122 Component detailsButton = ((ButtonPanel) _buttonArea).getButtonByName(ButtonNames.DETAILS);
123 if (detailsButton != null) {
124 detailsButton.setVisible(evt.getNewValue() != null);
125 }
126 }
127 }
128 else if (JideOptionPane.TITLE_PROPERTY.equals(evt.getPropertyName())) {
129 updateTitleComponent(_bannerArea);
130 }
131 }
132 };
133 optionPane.addPropertyChangeListener(_propertyListener);
134 }
135
136 @Override
137 protected void uninstallListeners() {
138 super.uninstallListeners();
139 optionPane.removePropertyChangeListener(_propertyListener);
140 }
141
142 protected void updateDetailsComponent() {
143 if (_detailsArea != null) {
144 optionPane.remove(_detailsArea);
145 _detailsArea = null;
146 }
147 _detailsArea = createDetailsComponent();
148 if (_detailsArea != null) {
149 optionPane.add(_detailsArea, JideBoxLayout.VARY);
150 _detailsArea.setVisible(isDetailsVisible());
151 }
152 }
153
154 @Override
155 protected Container createMessageArea() {
156 JPanel top = new JPanel();
157 Border topBorder = (Border) UIDefaultsLookup.get("OptionPane.messageAreaBorder");
158 if (topBorder != null) {
159 top.setBorder(topBorder);
160 }
161 top.setLayout(new BorderLayout());
162
163 /* Fill the body. */
164 Container body = new JPanel(new GridBagLayout());
165 Container realBody = new JPanel(new BorderLayout());
166
167 body.setName("OptionPane.body");
168 realBody.setName("OptionPane.realBody");
169
170 if (getIcon() != null) {
171 JPanel sep = new JPanel();
172 sep.setName("OptionPane.separator");
173 sep.setPreferredSize(new Dimension(15, 1));
174 realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);
175 }
176 realBody.add(body, BorderLayout.CENTER);
177
178 GridBagConstraints cons = new GridBagConstraints();
179 cons.gridx = cons.gridy = 0;
180 cons.gridwidth = GridBagConstraints.REMAINDER;
181 cons.gridheight = 1;
182 int anchor = UIDefaultsLookup.getInt("OptionPane.messageAnchor");
183 cons.anchor = anchor == 0 ? GridBagConstraints.CENTER : anchor;
184 cons.insets = new Insets(0, 0, 3, 0);
185
186 Object message = getMessage();
187 // if (message instanceof String
188 // && !((String) message).toLowerCase().startsWith("<html>")) {
189 // addMessageComponents(body, cons, null, getMaxCharactersPerLineCount(), false);
190 // }
191 // else {
192 addMessageComponents(body, cons, message, getMaxCharactersPerLineCount(), false);
193 // }
194 top.add(realBody, BorderLayout.CENTER);
195
196 return top;
197 }
198
199 @Override
200 protected Container createSeparator() {
201 return new JSeparator();
202 }
203
204 @Override
205 protected void installDefaults() {
206 super.installDefaults();
207 optionPane.setBorder(BorderFactory.createEmptyBorder());
208 _painter = (ThemePainter) UIDefaultsLookup.get("Theme.painter");
209 }
210
211 @Override
212 protected void uninstallDefaults() {
213 super.uninstallDefaults();
214 _painter = null;
215 }
216
217 protected Container createDetailsComponent() {
218 if (!(optionPane instanceof JideOptionPane)) {
219 return null;
220 }
221 JideOptionPane jideOptionPane = (JideOptionPane) optionPane;
222 Object details = jideOptionPane.getDetails();
223 if (details instanceof Container) {
224 _detailsPreferredWidth = ((Container) details).getPreferredSize().width;
225 return (Container) details;
226 }
227 else if (details instanceof Component) {
228 JPanel panel = new JPanel(new BorderLayout());
229 panel.add((Component) details);
230 _detailsPreferredWidth = panel.getPreferredSize().width;
231 return panel;
232 }
233 else if (details instanceof String) {
234 JTextArea area = new JTextArea((String) details);
235 area.setEditable(false);
236 area.setRows(20);
237 area.setColumns(60);
238 JPanel panel = new JPanel(new BorderLayout());
239 panel.add(new JScrollPane(area));
240 panel.setBorder(BorderFactory.createEmptyBorder(10, 6, 10, 6));
241 _detailsPreferredWidth = panel.getPreferredSize().width;
242 return panel;
243 }
244 else {
245 return null;
246 }
247 }
248
249 @Override
250 protected Container createButtonArea() {
251 int orientation = UIDefaultsLookup.getInt("OptionPane.buttonOrientation");
252 orientation = orientation == 0 ? SwingConstants.CENTER : orientation;
253 ButtonPanel buttonPanel = new ButtonPanel(orientation);
254 Border border = (Border) UIDefaultsLookup.get("OptionPane.buttonAreaBorder");
255 buttonPanel.setName("OptionPane.buttonArea");
256 if (border != null) {
257 buttonPanel.setBorder(border);
258 }
259 boolean sameSize = UIDefaultsLookup.getBoolean("OptionPane.sameSizeButtons");
260 buttonPanel.setSizeConstraint(sameSize ? ButtonPanel.SAME_SIZE : ButtonPanel.NO_LESS_THAN);
261 int padding = UIDefaultsLookup.getInt("OptionPane.buttonPadding");
262 padding = padding == 0 ? 6 : padding;
263 buttonPanel.setButtonGap(padding);
264 addButtonComponents(buttonPanel, getButtons(), getInitialValueIndex());
265 return buttonPanel;
266 }
267
268 @Override
269 protected void addButtonComponents(Container container, Object[] buttons,
270 int initialIndex) {
271 if (buttons != null && buttons.length > 0) {
272 int numButtons = buttons.length;
273 for (int counter = 0; counter < numButtons; counter++) {
274 Object button = buttons[counter];
275 Component newComponent;
276
277 if (button instanceof Component) {
278 newComponent = (Component) button;
279 container.add(newComponent, ButtonPanel.OTHER_BUTTON);
280 hasCustomComponents = true;
281 }
282 else {
283 JButton aButton;
284
285 if (button instanceof ButtonFactory) {
286 aButton = ((ButtonFactory) button).createButton();
287 }
288 else if (button instanceof Icon)
289 aButton = new JButton((Icon) button);
290 else
291 aButton = new JButton(button.toString());
292
293 aButton.setMultiClickThreshhold(UIDefaultsLookup.getInt("OptionPane.buttonClickThreshhold"));
294 configureButton(aButton);
295
296 if (ButtonNames.YES.equals(aButton.getName())
297 || ButtonNames.NO.equals(aButton.getName())
298 || ButtonNames.OK.equals(aButton.getName())
299 || ButtonNames.CLOSE.equals(aButton.getName())
300 || ButtonNames.FINISH.equals(aButton.getName())) {
301 container.add(aButton, ButtonPanel.AFFIRMATIVE_BUTTON);
302 }
303 else if (ButtonNames.CANCEL.equals(aButton.getName())) {
304 container.add(aButton, ButtonPanel.CANCEL_BUTTON);
305 }
306 else if (ButtonNames.HELP.equals(aButton.getName())) {
307 container.add(aButton, ButtonPanel.HELP_BUTTON);
308 }
309 else {
310 container.add(aButton, ButtonPanel.OTHER_BUTTON);
311 }
312
313 if (ButtonNames.DETAILS.equals(aButton.getName())) {
314 aButton.addActionListener(new AbstractAction() {
315 private static final long serialVersionUID = -6121325145433592319L;
316
317 public void actionPerformed(ActionEvent e) {
318 JButton defaultButton = (JButton) e.getSource();
319 toggleDetailsArea();
320 Container top = defaultButton.getTopLevelAncestor();
321 if (top instanceof Window) {
322 ((Window) top).pack();
323 }
324 }
325 });
326 aButton.setVisible(shouldDetailsButtonVisible());
327 _detailsButton = aButton;
328 }
329 else {
330 ActionListener buttonListener = createButtonActionListener(counter);
331 if (buttonListener != null) {
332 aButton.addActionListener(buttonListener);
333 }
334 }
335 newComponent = aButton;
336 }
337
338 if (counter == initialIndex) {
339 initialFocusComponent = newComponent;
340 if (initialFocusComponent instanceof JButton) {
341 JButton defaultB = (JButton) initialFocusComponent;
342 defaultB.addAncestorListener(new AncestorListener() {
343 public void ancestorAdded(AncestorEvent e) {
344 JButton defaultButton = (JButton) e.getComponent();
345 JRootPane root = SwingUtilities.getRootPane(defaultButton);
346 if (root != null) {
347 root.setDefaultButton(defaultButton);
348 }
349 }
350
351 public void ancestorRemoved(AncestorEvent event) {
352 }
353
354 public void ancestorMoved(AncestorEvent event) {
355 }
356 });
357 }
358 }
359 }
360 }
361 }
362
363 private void toggleDetailsArea() {
364 final ResourceBundle resourceBundle = ButtonResources.getResourceBundle(optionPane.getLocale());
365 if (_detailsArea.isVisible()) {
366 setDetailsVisible(false);
367 _detailsArea.setVisible(false);
368 _detailsButton.setText(optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.showDetails") : resourceBundle.getString("Button.showDetails"));
369 _detailsButton.setMnemonic(optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.showDetails.mnemonic").charAt(0) : resourceBundle.getString("Button.showDetails.mnemonic").charAt(0));
370 }
371 else {
372 setDetailsVisible(true);
373 _detailsArea.setVisible(true);
374 _detailsButton.setText(optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.hideDetails") : resourceBundle.getString("Button.hideDetails"));
375 _detailsButton.setMnemonic(optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.hideDetails.mnemonic").charAt(0) : resourceBundle.getString("Button.hideDetails.mnemonic").charAt(0));
376 }
377 }
378
379 /**
380 * Returns the buttons to display from the JOptionPane the receiver is providing the look and feel for. If the
381 * JOptionPane has options set, they will be provided, otherwise if the optionType is YES_NO_OPTION, yesNoOptions is
382 * returned, if the type is YES_NO_CANCEL_OPTION yesNoCancelOptions is returned, otherwise defaultButtons are
383 * returned.
384 */
385 @Override
386 protected Object[] getButtons() {
387 if (optionPane != null) {
388 Object[] suppliedOptions = optionPane.getOptions();
389
390 if (suppliedOptions == null) {
391 Object[] defaultOptions;
392 int type = optionPane.getOptionType();
393 Locale l = optionPane.getLocale();
394 if (type == JOptionPane.YES_NO_OPTION) {
395 defaultOptions = new ButtonFactory[2];
396 defaultOptions[0] = new ButtonFactory(
397 ButtonNames.YES,
398 UIDefaultsLookup.getString("OptionPane.yesButtonText", l),
399 getMnemonic("OptionPane.yesButtonMnemonic", l),
400 (Icon) UIDefaultsLookup.get("OptionPane.yesIcon"));
401 defaultOptions[1] = new ButtonFactory(
402 ButtonNames.NO,
403 UIDefaultsLookup.getString("OptionPane.noButtonText", l),
404 getMnemonic("OptionPane.noButtonMnemonic", l),
405 (Icon) UIDefaultsLookup.get("OptionPane.noIcon"));
406 }
407 else if (type == JOptionPane.YES_NO_CANCEL_OPTION) {
408 defaultOptions = new ButtonFactory[3];
409 defaultOptions[0] = new ButtonFactory(
410 ButtonNames.YES,
411 UIDefaultsLookup.getString("OptionPane.yesButtonText", l),
412 getMnemonic("OptionPane.yesButtonMnemonic", l),
413 (Icon) UIDefaultsLookup.get("OptionPane.yesIcon"));
414 defaultOptions[1] = new ButtonFactory(
415 ButtonNames.NO,
416 UIDefaultsLookup.getString("OptionPane.noButtonText", l),
417 getMnemonic("OptionPane.noButtonMnemonic", l),
418 (Icon) UIDefaultsLookup.get("OptionPane.noIcon"));
419 defaultOptions[2] = new ButtonFactory(
420 ButtonNames.CANCEL,
421 UIDefaultsLookup.getString("OptionPane.cancelButtonText", l),
422 getMnemonic("OptionPane.cancelButtonMnemonic", l),
423 (Icon) UIDefaultsLookup.get("OptionPane.cancelIcon"));
424 }
425 else if (type == JOptionPane.OK_CANCEL_OPTION) {
426 defaultOptions = new ButtonFactory[2];
427 defaultOptions[0] = new ButtonFactory(
428 ButtonNames.OK,
429 JideSwingUtilities.getOKString(l),
430 getMnemonic("OptionPane.okButtonMnemonic", l),
431 (Icon) UIDefaultsLookup.get("OptionPane.okIcon"));
432 defaultOptions[1] = new ButtonFactory(
433 ButtonNames.CANCEL,
434 JideSwingUtilities.getCancelString(l),
435 getMnemonic("OptionPane.cancelButtonMnemonic", l),
436 (Icon) UIDefaultsLookup.get("OptionPane.cancelIcon"));
437 }
438 else if (type == JideOptionPane.CLOSE_OPTION) {
439 defaultOptions = new ButtonFactory[1];
440 final ResourceBundle resourceBundle = ButtonResources.getResourceBundle(optionPane.getLocale());
441 defaultOptions[0] = new ButtonFactory(
442 ButtonNames.CLOSE,
443 optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.close") : resourceBundle.getString("Button.close"),
444 optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.close.mnemonic").charAt(0) : resourceBundle.getString("Button.close.mnemonic").charAt(0),
445 null);
446 }
447 else {
448 defaultOptions = new ButtonFactory[1];
449 defaultOptions[0] = new ButtonFactory(
450 ButtonNames.OK,
451 JideSwingUtilities.getOKString(l),
452 getMnemonic("OptionPane.okButtonMnemonic", l),
453 (Icon) UIDefaultsLookup.get("OptionPane.okIcon"));
454 }
455
456 return addDetailsButton(defaultOptions, true);
457
458 }
459 return addDetailsButton(suppliedOptions, true);
460 }
461 return null;
462 }
463
464 protected Object[] addDetailsButton(Object[] options, boolean showDetails) {
465 if (showDetails) {
466 Object[] newOptions = new Object[options.length + 1];
467 System.arraycopy(options, 0, newOptions, 0, options.length);
468 final ResourceBundle resourceBundle = ButtonResources.getResourceBundle(optionPane.getLocale());
469 if (isDetailsVisible()) {
470 newOptions[newOptions.length - 1] = new ButtonFactory(
471 ButtonNames.DETAILS,
472 optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.hideDetails") : resourceBundle.getString("Button.hideDetails"),
473 optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.hideDetails.mnemonic").charAt(0) : resourceBundle.getString("Button.hideDetails.mnemonic").charAt(0), null);
474 }
475 else {
476 newOptions[newOptions.length - 1] = new ButtonFactory(
477 ButtonNames.DETAILS,
478 optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.showDetails") : resourceBundle.getString("Button.showDetails"),
479 optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getResourceString("Button.showDetails.mnemonic").charAt(0) : resourceBundle.getString("Button.showDetails.mnemonic").charAt(0), null);
480 }
481 return newOptions;
482 }
483 else {
484 return options;
485 }
486
487 }
488
489 private boolean shouldDetailsButtonVisible() {
490 return optionPane instanceof JideOptionPane && ((JideOptionPane) optionPane).getDetails() != null;
491 }
492
493 /**
494 * Configures any necessary colors/fonts for the specified button used representing the button portion of the
495 * OptionPane.
496 *
497 * @param button the button to configure
498 */
499 protected void configureButton(JButton button) {
500 Font buttonFont = (Font) UIDefaultsLookup.get("OptionPane.buttonFont");
501 if (buttonFont != null) {
502 button.setFont(buttonFont);
503 }
504 }
505
506 protected int getMnemonic(String key, Locale l) {
507 String value = (String) UIDefaultsLookup.get(key, l);
508
509 if (value == null) {
510 return 0;
511 }
512 try {
513 return Integer.parseInt(value);
514 }
515 catch (NumberFormatException nfe) {
516 // null
517 }
518 return 0;
519 }
520
521 /**
522 * This class is used to create the default buttons. This indirection is used so that addButtonComponents can tell
523 * which Buttons were created by us vs subclasses or from the JOptionPane itself.
524 */
525 protected static class ButtonFactory {
526 private String name;
527 private String text;
528 private int mnemonic;
529 private Icon icon;
530
531 ButtonFactory(String name, String text, int mnemonic, Icon icon) {
532 this.name = name;
533 this.text = text;
534 this.mnemonic = mnemonic;
535 this.icon = icon;
536 }
537
538 JButton createButton() {
539 JButton button = new JButton(text);
540 if (name != null) {
541 button.setName(name);
542 }
543 if (icon != null) {
544 button.setIcon(icon);
545 }
546 if (mnemonic != 0) {
547 button.setMnemonic(mnemonic);
548 }
549 return button;
550 }
551 }
552
553 @Override
554 protected void addIcon(Container top) {
555 Icon sideIcon = getIcon();
556 if (sideIcon != null) {
557 JLabel iconLabel = new JLabel(sideIcon);
558 iconLabel.setName("OptionPane.iconLabel");
559 top.add(iconLabel, BorderLayout.BEFORE_LINE_BEGINS);
560 }
561 }
562
563 protected Container createBannerArea() {
564 PaintPanel bannerPanel = new PaintPanel() {
565 @Override
566 public Dimension getPreferredSize() {
567 Dimension preferredSize = super.getPreferredSize();
568 if (preferredSize.width < _detailsPreferredWidth) {
569 preferredSize.width = _detailsPreferredWidth;
570 }
571 return preferredSize;
572 }
573 };
574 customizeBannerArea(bannerPanel);
575 bannerPanel.setLayout(new BorderLayout(10, 10));
576 addIcon(bannerPanel);
577 updateTitleComponent(bannerPanel);
578 bannerPanel.add(new JLabel(UIDefaultsLookup.getIcon("OptionPane.bannerIcon")), BorderLayout.AFTER_LINE_ENDS);
579 return bannerPanel;
580 }
581
582 protected void customizeBannerArea(PaintPanel bannerPanel) {
583 Paint paint = (Paint) UIDefaultsLookup.get("OptionPane.bannerBackgroundPaint");
584
585 if (paint != null) {
586 bannerPanel.setBackgroundPaint(paint);
587 }
588 else {
589 Color dk = UIDefaultsLookup.getColor("OptionPane.bannerBackgroundDk");
590 Color lt = UIDefaultsLookup.getColor("OptionPane.bannerBackgroundLt");
591 boolean direction = UIDefaultsLookup.get("OptionPane.bannerBackgroundDirection") == null
592 || UIDefaultsLookup.getBoolean("OptionPane.bannerBackgroundDirection");
593 if (dk == null && lt != null) {
594 dk = lt;
595 bannerPanel.setGradientPaint(
596 dk != null ? dk : getPainter().getOptionPaneBannerLt(),
597 lt != null ? lt : getPainter().getOptionPaneBannerDk(),
598 direction);
599 }
600 else if (dk != null && lt == null) {
601 lt = dk;
602 bannerPanel.setGradientPaint(
603 dk != null ? dk : getPainter().getOptionPaneBannerLt(),
604 lt != null ? lt : getPainter().getOptionPaneBannerDk(),
605 direction);
606 }
607 else if (dk != null && lt != null) {
608 bannerPanel.setGradientPaint(
609 dk != null ? dk : getPainter().getOptionPaneBannerLt(),
610 lt != null ? lt : getPainter().getOptionPaneBannerDk(),
611 direction);
612 }
613 }
614
615 Border border = UIDefaultsLookup.getBorder("OptionPane.bannerBorder");
616 bannerPanel.setBorder(border != null ? border : BorderFactory.createEmptyBorder(0, 10, 0, 0));
617 }
618
619 private void updateTitleComponent(Container bannerArea) {
620 if (bannerArea == null) {
621 return;
622 }
623
624 if (_titleComponent != null) {
625 bannerArea.remove(_titleComponent);
626 _titleComponent = null;
627 }
628
629 Object title = optionPane instanceof JideOptionPane ? ((JideOptionPane) optionPane).getTitle() : null;
630 if (title instanceof String) {
631 if (((String) title).startsWith("<html>") || ((String) title).startsWith("<HTML>")) {
632 _titleComponent = new JLabel(((String) title));
633 }
634 else {
635 String[] titles = fitInWidth((String) title, UIDefaultsLookup.getInt("OptionPane.bannerMaxCharsPerLine"));
636 JPanel titlePanel = new NullPanel();
637 titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
638 titlePanel.setOpaque(false);
639 titlePanel.add(Box.createGlue());
640 for (String s : titles) {
641 JLabel label = new JLabel(s);
642 label.setFont(label.getFont().deriveFont(UIDefaultsLookup.getInt("OptionPane.bannerFontStyle"), UIDefaultsLookup.getInt("OptionPane.bannerFontSize")));
643 Color color = UIDefaultsLookup.getColor("OptionPane.bannerForeground");
644 label.setForeground(color != null ? color : getPainter().getOptionPaneBannerForeground());
645 titlePanel.add(label);
646 }
647 titlePanel.add(Box.createGlue());
648 _titleComponent = titlePanel;
649 }
650 }
651 else if (title instanceof Component) {
652 _titleComponent = (Component) title;
653 }
654 if (_titleComponent != null) {
655 bannerArea.add(_titleComponent, BorderLayout.CENTER);
656 }
657 }
658
659 // static methods to split a string into multiple lines.
660
661 /**
662 * 1. Do not break a word/number. If the character is letter/digit, break at the most recent non- one; 2. Expand
663 * "\n" to a blank line 3. Expand "\t" to four " " 4. Trim leading empty spaces
664 *
665 * @param str the string
666 * @param width the target width
667 * @return An array of Strings with length of "width"
668 */
669 private static String[] fitInWidth(String str, int width) {
670 if (str == null) str = "";
671
672 String BLANK_STR = blankString(width, (byte) 32);
673 str = replaceOccurrences(str, "\n", BLANK_STR);
674 str = replaceOccurrences(str, "\t", " ");
675
676 ArrayList<String> strArray = new ArrayList<String>();
677 str = str.trim();
678 while (str.length() > width) {
679 int breakPos = width;
680 if (Character.isLetterOrDigit(str.charAt(width))) {
681 breakPos--;
682 char breakChar = str.charAt(breakPos);
683 while (Character.isLetterOrDigit(breakChar) && breakPos > 0) {
684 breakPos--;
685 breakChar = str.charAt(breakPos);
686 }
687 if (breakPos == 0 && Character.isLetterOrDigit(breakChar)) {
688 breakPos = width;
689 }
690 else {
691 breakPos++;
692 }
693 }
694 String subStr = str.substring(0, breakPos);
695 if (subStr.length() < width) {
696 subStr = subStr + blankString(width - subStr.length(), (byte) 32);
697 }
698 strArray.add(subStr);
699 str = str.substring(breakPos).trim();
700 }
701 if (str.length() < width) {
702 str = str + blankString(width - str.length(), (byte) 32);
703 }
704 strArray.add(str);
705 if (strArray.size() == 1) {
706 return new String[]{strArray.get(0).trim()};
707 }
708 return strArray.toArray(new String[strArray.size()]);
709 }
710
711 private static String blankString(int width, byte b) {
712 byte[] bytes = new byte[width];
713 Arrays.fill(bytes, b);
714 return new String(bytes);
715 }
716
717 /**
718 * Replaces all occurrences of <code>target</code> in <code>string</code> with <code>dest</code>.
719 *
720 * @param string string in which to target occurrences
721 * @param target string to target
722 * @param dest replacement string
723 * @return <code>string</code> with <code>dest</code> substituted for <code>target</code>
724 */
725 private static String replaceOccurrences(String string, String target, String dest) {
726 StringBuffer b = new StringBuffer(string);
727 int lastIndex = 0;
728 while (true) {
729 int index = indexOf(b, target, lastIndex);
730 if (index < 0) {
731 break;
732 }
733 b.replace(index, index + target.length(), dest);
734 lastIndex = index + dest.length();
735 }
736 return b.toString();
737 }
738
739 /**
740 * to do searches in character arrays. The source is the character array being searched, and the target is the
741 * string being searched for.
742 *
743 * @param source the characters being searched.
744 * @param sourceOffset offset of the source string.
745 * @param sourceCount count of the source string.
746 * @param target the characters being searched for.
747 * @param targetOffset offset of the target string.
748 * @param targetCount count of the target string.
749 * @param fromIndex the index to begin searching from.
750 * @return If the string argument occurs as a substring within this object, then the index of the first character of
751 * the first such substring is returned; if it does not occur as a substring, -1 is returned.
752 */
753 private static int indexOf(char[] source, int sourceOffset, int sourceCount,
754 char[] target, int targetOffset, int targetCount,
755 int fromIndex) {
756 if (fromIndex >= sourceCount) {
757 return (targetCount == 0 ? sourceCount : -1);
758 }
759 if (fromIndex < 0) {
760 fromIndex = 0;
761 }
762 if (targetCount == 0) {
763 return fromIndex;
764 }
765
766 char first = target[targetOffset];
767 int pos = sourceOffset + fromIndex;
768 int max = sourceOffset + (sourceCount - targetCount);
769
770 searchForNextChar:
771 while (true) {
772 // find first character
773
774 // not first char increment position
775 while (pos <= max && source[pos] != first) {
776 pos++;
777 }
778
779 // at end of char buffer didn't find
780 if (pos > max) {
781 return -1;
782 }
783
784 // found first character - see if the rest of matches up
785 int secondPos = pos + 1;
786 int end = secondPos + targetCount - 1;
787 int targetPos = targetOffset + 1;
788 while (secondPos < end) {
789 if (source[secondPos++] != target[targetPos++]) {
790 pos++;
791 // string doesn't match exit out of this loop and go back and
792 // continue looking at the next char
793 continue searchForNextChar;
794 }
795 }
796 // Found an entire whole string.
797 return pos - sourceOffset;
798 }
799 }
800
801 /**
802 * Returns the index within this string of the first occurrence of the specified substring, starting at the
803 * specified index. If the string argument occurs as a substring within this object, then the index of the first
804 * character of the first such substring is returned; if it does not occur as a substring, -1 is returned. This
805 * replaces the functionality for java 1.4 StringBuffer.indexOf.
806 *
807 * @param buf - buffer to search for str
808 * @param findStr - string to located
809 * @param fromIndex - index to search
810 * @return If the string argument occurs as a substring within this object, then the index of the first character of
811 * the first such substring is returned; if it does not occur as a substring, -1 is returned.
812 */
813 private static int indexOf(StringBuffer buf, String findStr, int fromIndex) {
814 // function in Java's StringBuffer version 1.4 is synchronized
815 // get character buffer from string buffer
816 synchronized (buf) {
817 int bufLen = buf.length();
818 char[] charArray = new char[bufLen];
819 buf.getChars(0, bufLen, charArray, 0);
820
821 // send into char indexOf function
822 return indexOf(charArray, 0, bufLen,
823 findStr.toCharArray(), 0, findStr.length(), fromIndex);
824 }
825 }
826
827 public ThemePainter getPainter() {
828 return _painter;
829 }
830 }