1 51 package org.apache.fop.viewer; 52 53 import java.io.*; 54 import java.awt.*; 55 import javax.swing.*; 56 57 66 public class MessagesDialog extends JOptionPane { 67 68 static Translator res; 69 70 public static void setTranslator(Translator aRes) { 71 res = aRes; 72 iniConstants(); 73 } 74 75 76 static String DETAIL_OPTION; 77 static String YES_OPTION; 78 static String NO_OPTION; 79 static String CANCEL_OPTION; 80 static String OK_OPTION; 81 82 static String [] defaultDetailOption; 83 static String [] yesNoDetailOption; 84 static String [] yesNoCancelDetailOption; 85 static String [] okCancelDetailOption; 86 87 static String [] defaultOption; 88 static String [] yesNoOption; 89 static String [] yesNoCancelOption; 90 static String [] okCancelOption; 91 92 93 94 private static void iniConstants() { 95 DETAIL_OPTION = res.getString("Details"); 96 YES_OPTION = res.getString("Yes"); 97 NO_OPTION = res.getString("No"); 98 CANCEL_OPTION = res.getString("Cancel"); 99 OK_OPTION = res.getString("Ok"); 100 101 defaultDetailOption = new String [] { 102 OK_OPTION, DETAIL_OPTION 103 }; 104 yesNoDetailOption = new String [] { 105 YES_OPTION, NO_OPTION, DETAIL_OPTION 106 }; 107 yesNoCancelDetailOption = new String [] { 108 YES_OPTION, NO_OPTION, CANCEL_OPTION, DETAIL_OPTION 109 }; 110 okCancelDetailOption = new String [] { 111 OK_OPTION, CANCEL_OPTION, DETAIL_OPTION 112 }; 113 114 defaultOption = new String [] { 115 OK_OPTION 116 }; 117 yesNoOption = new String [] { 118 YES_OPTION, NO_OPTION 119 }; 120 yesNoCancelOption = new String [] { 121 YES_OPTION, NO_OPTION, CANCEL_OPTION 122 }; 123 okCancelOption = new String [] { 124 OK_OPTION, CANCEL_OPTION 125 }; 126 } 127 128 129 protected String detailInformation = null; 130 protected JDialog dialog = null; 131 protected boolean showsDetails = false; 132 133 136 public MessagesDialog(Object message, int messageType, int optionType, 137 Icon icon, Object [] options, Object initialValue) { 138 super(message, messageType, optionType, icon, options, initialValue); 139 setMinimumSize(new Dimension(240, 96)); 140 } 141 142 public static int showConfirmDialog(Component parentComponent, 143 Object message, String title, 144 int optionType, int messageType) { 145 Object [] options; 146 147 switch (optionType) { 148 case JOptionPane.YES_NO_OPTION: 149 options = yesNoOption; 150 break; 151 case JOptionPane.YES_NO_CANCEL_OPTION: 152 options = yesNoCancelOption; 153 break; 154 case JOptionPane.OK_CANCEL_OPTION: 155 options = okCancelOption; 156 break; 157 default: 158 options = defaultOption; 159 } 160 161 MessagesDialog pane = new MessagesDialog(message, messageType, 162 JOptionPane.DEFAULT_OPTION, 163 null, options, options[0]); 164 165 pane.setInitialValue(options[0]); 166 167 JDialog dialog = pane.createDialog(parentComponent, title); 168 169 pane.setDialog(dialog); 170 pane.selectInitialValue(); 171 172 dialog.show(); 173 174 Object selectedValue = pane.getValue(); 175 176 if (selectedValue == null) 177 return CLOSED_OPTION; 178 179 if (selectedValue.equals(OK_OPTION)) 180 return JOptionPane.OK_OPTION; 181 if (selectedValue.equals(CANCEL_OPTION)) 182 return JOptionPane.CANCEL_OPTION; 183 if (selectedValue.equals(YES_OPTION)) 184 return JOptionPane.YES_OPTION; 185 if (selectedValue.equals(NO_OPTION)) 186 return JOptionPane.NO_OPTION; 187 188 return CLOSED_OPTION; 189 } 190 191 196 public static int showDetailDialog(Component parentComponent, 197 Object message, String title, 198 int optionType, int messageType, 199 Icon icon, 200 String newDetailInformation) { 201 Object [] options; 202 203 switch (optionType) { 204 case JOptionPane.YES_NO_OPTION: 205 options = yesNoDetailOption; 206 break; 207 case JOptionPane.YES_NO_CANCEL_OPTION: 208 options = yesNoCancelDetailOption; 209 break; 210 case JOptionPane.OK_CANCEL_OPTION: 211 options = okCancelDetailOption; 212 break; 213 default: 214 options = defaultDetailOption; 215 } 216 217 MessagesDialog pane = new MessagesDialog(message, messageType, 218 JOptionPane.DEFAULT_OPTION, 219 icon, options, options[0]); 220 221 pane.setDetailInformation(newDetailInformation); 222 pane.setInitialValue(options[0]); 223 224 JDialog dialog = pane.createDialog(parentComponent, title); 225 226 pane.setDialog(dialog); 227 pane.selectInitialValue(); 228 229 dialog.show(); 230 231 Object selectedValue = pane.getValue(); 232 233 if (selectedValue == null) 234 return CLOSED_OPTION; 235 236 if (((String )selectedValue).equals(DETAIL_OPTION)) 237 return CLOSED_OPTION; 238 239 if (selectedValue.equals(OK_OPTION)) 240 return JOptionPane.OK_OPTION; 241 if (selectedValue.equals(CANCEL_OPTION)) 242 return JOptionPane.CANCEL_OPTION; 243 if (selectedValue.equals(YES_OPTION)) 244 return JOptionPane.YES_OPTION; 245 if (selectedValue.equals(NO_OPTION)) 246 return JOptionPane.NO_OPTION; 247 248 return CLOSED_OPTION; 249 } 250 251 257 protected void displayDetails(JDialog dialog) { 258 if (getDetailInformation() != null && dialog != null 259 && showsDetails == false) { 260 showsDetails = true; 261 JScrollPane aScrollPane = new JScrollPane(); 262 JTextArea aTextArea = new JTextArea(); 263 StringWriter aStringWriter = new StringWriter(); 264 265 aTextArea.setText(getDetailInformation()); 266 aTextArea.setEditable(false); 267 268 aScrollPane.getViewport().add(aTextArea, null); 269 dialog.getContentPane().add(aScrollPane, BorderLayout.SOUTH); 270 aScrollPane.setPreferredSize(new Dimension(320, 240)); 271 dialog.pack(); 272 } 273 } 274 275 277 public void setValue(Object aValue) { 278 if (aValue != null && DETAIL_OPTION.equals(aValue)) 279 displayDetails(getDialog()); 280 else 281 super.setValue(aValue); 282 } 283 284 public String getDetailInformation() { 285 return detailInformation; 286 } 287 288 public void setDetailInformation(String aValue) { 289 detailInformation = aValue; 290 } 291 292 public JDialog getDialog() { 293 return dialog; 294 } 295 296 public void setDialog(JDialog aValue) { 297 dialog = aValue; 298 } 299 300 } 301 | Popular Tags |