1 31 32 package org.antlr.xjlib.appkit.utils; 33 34 import org.antlr.xjlib.appkit.app.XJApplication; 35 import org.antlr.xjlib.foundation.XJSystem; 36 37 import javax.swing.*; 38 import java.awt.*; 39 40 public class XJAlert { 41 42 public static final int YES = JOptionPane.YES_OPTION; 43 public static final int NO = JOptionPane.NO_OPTION; 44 public static final int CANCEL = JOptionPane.CANCEL_OPTION; 45 46 public static void display(Component parent, String title, String message) { 47 JOptionPane.showMessageDialog(parent==null?XJApplication.getActiveContainer():parent, 48 autoAdjustMessage(message), title, JOptionPane.INFORMATION_MESSAGE, null); 49 } 50 51 public static int displayAlertYESNO(Component parent, String title, String message) { 52 return JOptionPane.showConfirmDialog(parent==null?XJApplication.getActiveContainer():parent, 53 autoAdjustMessage(message), 54 title, 55 JOptionPane.YES_NO_OPTION, 56 JOptionPane.QUESTION_MESSAGE); 57 } 58 59 public static int displayAlertYESNOCANCEL(Component parent, String title, String message) { 60 return JOptionPane.showConfirmDialog(parent==null?XJApplication.getActiveContainer():parent, 61 autoAdjustMessage(message), 62 title, 63 JOptionPane.YES_NO_CANCEL_OPTION, 64 JOptionPane.QUESTION_MESSAGE); 65 } 66 67 public static int displayAlert(Component parent, String title, String message, String b1, String b2, int def) { 68 return displayCustomAlert(parent, title, message, new String [] { b1, b2}, def); 69 } 70 71 public static int displayAlert(Component parent, String title, String message, String b1, String b2, String b3, int def) { 72 return displayCustomAlert(parent, title, message, new String [] { b1, b2, b3 }, def); 73 } 74 75 public static int displayCustomAlert(Component parent, String title, String message, String [] buttons, int def) { 76 if(XJSystem.isMacOS()) { 77 String [] reverse = new String [buttons.length]; 78 for(int i=0; i<buttons.length; i++) { 79 reverse[i] = buttons[buttons.length-i-1]; 80 } 81 buttons = reverse; 82 def = buttons.length-def-1; 83 } 84 int result = JOptionPane.showOptionDialog(parent==null?XJApplication.getActiveContainer():parent, 85 autoAdjustMessage(message), title, 0, JOptionPane.INFORMATION_MESSAGE, null, buttons, buttons[def]); 86 if(XJSystem.isMacOS()) { 87 return buttons.length-result-1; 88 } else 89 return result; 90 } 91 92 public static final int AUTO_ADJUST_LENGTH = 100; 93 94 public static String autoAdjustMessage(String message) { 95 if(message.length() <= AUTO_ADJUST_LENGTH) 96 return message; 97 98 101 102 StringBuffer sb = new StringBuffer (message); 103 int i = 0; 104 int k = 0; 105 while(i < sb.length()) { 106 if(sb.charAt(i) == '\n') 108 k = 0; 109 110 if(k == AUTO_ADJUST_LENGTH) { 114 autoAdjustMessageAround(sb, i); 115 i++; 118 119 k = 0; 121 } 122 123 i++; 125 k++; 126 } 127 return sb.toString(); 128 } 129 130 public static void autoAdjustMessageAround(StringBuffer sb, int k) { 131 for(int i = k; i > k-AUTO_ADJUST_LENGTH + 1; i--) { 134 char c = sb.charAt(i); 135 switch(c) { 136 case ' ': 137 case ',': 138 case '.': 139 case ';': 140 case ':': 141 case '/': 142 case '!': 143 case '?': 144 sb.insert(i+1, "\n"); 145 return; 146 } 147 } 148 sb.insert(k, "\n"); 149 } 150 } 151 | Popular Tags |