1 package jimm.datavision.gui; 2 import jimm.datavision.*; 3 import jimm.datavision.field.*; 4 import jimm.datavision.gui.cmd.FormatCommand; 5 import jimm.util.I18N; 6 import java.awt.*; 7 import java.awt.event.*; 8 import java.util.ArrayList ; 9 import javax.swing.*; 10 11 26 public class FormatWin extends EditWin implements FieldWalker { 27 28 32 static class EdgeWidgets { 33 34 protected BorderEdge edge; 35 protected String name; 36 protected JComboBox numberComboBox; 37 protected JComboBox styleComboBox; 38 protected JTextField thicknessText; 39 40 EdgeWidgets(BorderEdge e, String name) { 41 edge = e; 42 this.name = name; 43 } 44 45 String getName() { return name; } 46 47 } 48 50 54 protected static Integer [] SIZE_CHOICES; 55 56 protected static final int FORMAT_TEXT_FIELD_COLS = 20; 57 protected static final int THICKNESS_COLS = 8; 58 59 protected static final int TOP = 0; 60 protected static final int LEFT = 1; 61 protected static final int RIGHT = 2; 62 protected static final int BOTTOM = 3; 63 64 protected static String [] fontFamilyNames; 65 66 protected Field field; 67 protected Format format; 68 protected Border border; 69 protected Format origFormat; 70 protected Border origBorder; 71 protected boolean saveRevertInfo; 72 protected JComboBox fontFamily; 73 protected JComboBox size; 74 protected JCheckBox bold; 75 protected JCheckBox italic; 76 protected JCheckBox underline; 77 protected JCheckBox wrap; 78 protected JComboBox align; 79 protected JTextField formatText; 80 protected JLabel fieldColorLabel; 81 protected JLabel borderColorLabel; 82 protected EdgeWidgets[] edgeWidgets; 83 84 89 public static void loadFontChoices() { 90 if (fontFamilyNames == null) { 91 new Thread (new Runnable () { 92 public void run() { 93 fontFamilyNames = 94 GraphicsEnvironment.getLocalGraphicsEnvironment() 95 .getAvailableFontFamilyNames(); 96 } 97 }).start(); 98 } 99 } 100 101 108 public FormatWin(Designer designer, Field f, int whichTab) { 109 super(designer, 110 I18N.get("FormatWin.title") 111 + (f.designLabel().startsWith("{") ? " " : " (") 112 + f.designLabel() 113 + (designer.countSelectedFields() > 1 ? " +" : "") 114 + (f.designLabel().startsWith("{") ? " " : " )"), 115 "FormatCommand.name"); 116 117 field = f; 118 119 origFormat = field.getFormat(); 120 if (origFormat != null) origFormat = (Format)origFormat.clone(); 121 origBorder = field.getBorder(); 122 if (origBorder != null) origBorder = (Border)origBorder.clone(); 123 124 copyFormatAndBorder(field.getFormat(), field.getBorder()); 125 126 buildWindow(whichTab); 127 pack(); 128 setVisible(true); 129 } 130 131 141 protected void copyFormatAndBorder(Format origFormat, Border origBorder) { 142 format = (Format)origFormat.clone(); 143 144 if (origBorder == null) { 145 border = (Border)field.getReport().getDefaultField().getBorder() 146 .clone(); 147 border.setField(field); 148 } 149 else 150 border = (Border)origBorder.clone(); 151 152 if (border.getTop() == null) 153 border.setTop(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0)); 154 if (border.getBottom() == null) 155 border.setBottom(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0)); 156 if (border.getLeft() == null) 157 border.setLeft(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0)); 158 if (border.getRight() == null) 159 border.setRight(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0)); 160 } 161 162 167 protected void buildWindow(int whichTab) { 168 JTabbedPane tabbedPane = new JTabbedPane(); 169 tabbedPane.addTab(I18N.get("FormatWin.format_tab"), buildFormatTab()); 170 tabbedPane.addTab(I18N.get("FormatWin.border_tab"), buildBorderTab()); 171 tabbedPane.setSelectedIndex(whichTab); 172 173 JPanel buttonPanel = closeButtonPanel(); 175 176 getContentPane().add(tabbedPane, BorderLayout.CENTER); 178 getContentPane().add(buttonPanel, BorderLayout.SOUTH); 179 180 fillFormatTab(); 181 fillBorderTab(); 182 } 183 184 187 protected java.awt.Container buildFormatTab() { 188 EditFieldLayout efl = new EditFieldLayout(); 189 190 fontFamily = efl.addComboBox(I18N.get("FormatWin.font"), fontChoices(), 191 true); 192 size = efl.addComboBox(I18N.get("FormatWin.size"), sizeChoices(), true); 193 bold = efl.addCheckBox(I18N.get("FormatWin.bold"), KeyEvent.VK_B); 194 italic = efl.addCheckBox(I18N.get("FormatWin.italic"), KeyEvent.VK_I); 195 underline = efl.addCheckBox(I18N.get("FormatWin.underline"), 196 KeyEvent.VK_U); 197 wrap = efl.addCheckBox(I18N.get("FormatWin.wrap"), KeyEvent.VK_W); 198 align = efl.addComboBox(I18N.get("FormatWin.align"), alignChoices()); 199 formatText = efl.addTextField(I18N.get("FormatWin.format"), 200 FORMAT_TEXT_FIELD_COLS); 201 efl.add(I18N.get("FormatWin.color"), buildFieldColorWidgets()); 202 203 Box outerBox = Box.createVerticalBox(); 205 outerBox.add(Box.createGlue()); 206 outerBox.add(efl.getPanel()); 207 outerBox.add(Box.createGlue()); 208 209 return outerBox; 210 } 211 212 protected Box buildFieldColorWidgets() { 213 Box box = Box.createHorizontalBox(); 214 fieldColorLabel = new JLabel(I18N.get("FormatWin.sample_text")); 215 box.add(fieldColorLabel); 216 setFieldExampleColor(); 217 box.add(Box.createHorizontalStrut(16)); 218 box.add(createFieldColorChooserButton()); 219 return box; 220 } 221 222 protected Box buildBorderColorWidgets() { 223 Box box = Box.createHorizontalBox(); 224 borderColorLabel = new JLabel(I18N.get("FormatWin.sample_text")); 225 box.add(borderColorLabel); 226 setBorderExampleColor(); 227 box.add(Box.createHorizontalStrut(16)); 228 box.add(createBorderColorChooserButton()); 229 230 Box widgetBox = Box.createVerticalBox(); 231 widgetBox.add(Box.createVerticalGlue()); 232 widgetBox.add(new JLabel(I18N.get("FormatWin.color"))); 233 widgetBox.add(Box.createVerticalStrut(6)); 234 widgetBox.add(box); 235 widgetBox.add(Box.createVerticalGlue()); 236 237 return widgetBox; 238 } 239 240 241 protected void setFieldExampleColor() { 242 fieldColorLabel.setForeground(format.getColor()); 243 } 244 245 protected void setBorderExampleColor() { 246 borderColorLabel.setForeground(border.getColor()); 247 } 248 249 protected JButton createFieldColorChooserButton() { 250 JButton b = new JButton(I18N.get("FormatWin.choose")); 251 b.addActionListener(new ActionListener() { 252 public void actionPerformed(ActionEvent e) { 253 Color c = JColorChooser.showDialog(FormatWin.this, 254 I18N.get("FormatWin.field_color_title"), 255 format.getColor()); 256 if (c != null) { 257 format.setColor(c); 258 setFieldExampleColor(); 259 } 260 } 261 }); 262 return b; 263 } 264 265 protected JButton createBorderColorChooserButton() { 266 JButton b = new JButton(I18N.get("FormatWin.choose")); 267 b.addActionListener(new ActionListener() { 268 public void actionPerformed(ActionEvent e) { 269 Color c = JColorChooser.showDialog(FormatWin.this, 270 I18N.get("FormatWin.border_color_title"), 271 border.getColor()); 272 if (c != null) { 273 border.setColor(c); 274 setBorderExampleColor(); 275 } 276 } 277 }); 278 return b; 279 } 280 281 287 protected Integer [] sizeChoices() { 288 if (SIZE_CHOICES == null) { 289 ArrayList list = new ArrayList (); 290 291 for (int size = 6; size <= 12; ++size) 292 list.add(new Integer (size)); 293 for (int size = 14; size <= 36; size += 2) 294 list.add(new Integer (size)); 295 296 SIZE_CHOICES = new Integer [list.size()]; 297 list.toArray(SIZE_CHOICES); 298 } 299 return SIZE_CHOICES; 300 } 301 302 308 protected int sizeIndexOf(int size) { 309 Integer [] sizeChoices = sizeChoices(); 310 for (int i = 0; i < sizeChoices.length; ++i) 311 if (sizeChoices[i].intValue() == size) 312 return i; 313 return -1; 314 } 315 316 321 protected String [] fontChoices() { 322 return fontFamilyNames; 323 } 324 325 331 protected int fontIndexOf(String fontFamilyName) { 332 if (fontFamilyName == null || fontFamilyName.length() == 0) 333 return -1; 334 335 String [] names = fontChoices(); 336 for (int i = 0; i < names.length; ++i) 337 if (names[i].equals(fontFamilyName)) 338 return i; 339 340 return -1; 341 } 342 343 protected String [] alignChoices() { 344 String [] choices = new String [3]; 345 int i = 0; 346 choices[i++] = I18N.get("FormatWin.align_left"); 347 choices[i++] = I18N.get("FormatWin.align_center"); 348 choices[i++] = I18N.get("FormatWin.align_right"); 349 return choices; 350 } 351 352 protected String [] edgeCountChoices() { 353 String [] choices = new String [4]; 354 for (int i = 0; i < 4; ++i) 355 choices[i] = I18N.get("FormatWin.edge_count_" + i); 356 return choices; 357 } 358 359 protected String [] edgeStyleChoices() { 360 String [] choices = new String [3]; 361 int i = 0; 362 choices[i++] = I18N.get("FormatWin.edge_style_line"); 363 choices[i++] = I18N.get("FormatWin.edge_style_dashed"); 364 choices[i++] = I18N.get("FormatWin.edge_style_dotted"); 365 return choices; 366 } 367 368 371 protected Box buildBorderTab() { 372 edgeWidgets = new EdgeWidgets[4]; 373 374 Box vertBox = Box.createVerticalBox(); 375 376 Box horizBox = Box.createHorizontalBox(); 377 horizBox.add(Box.createHorizontalGlue()); 378 horizBox.add(buildBorderEdge(TOP, I18N.get("FormatWin.edge_top"), 379 border.getTop())); 380 horizBox.add(Box.createHorizontalGlue()); 381 vertBox.add(horizBox); 382 383 horizBox = Box.createHorizontalBox(); 384 horizBox.add(buildBorderEdge(LEFT, I18N.get("FormatWin.edge_left"), 385 border.getLeft())); 386 horizBox.add(buildBorderColorWidgets()); 387 horizBox.add(buildBorderEdge(RIGHT, I18N.get("FormatWin.edge_right"), 388 border.getRight())); 389 vertBox.add(horizBox); 390 391 horizBox = Box.createHorizontalBox(); 392 horizBox.add(Box.createHorizontalGlue()); 393 horizBox.add(buildBorderEdge(BOTTOM, I18N.get("FormatWin.edge_bottom"), 394 border.getBottom())); 395 horizBox.add(Box.createHorizontalGlue()); 396 vertBox.add(horizBox); 397 398 return vertBox; 399 } 400 401 415 protected Box buildBorderEdge(int edgeIndex, String edgeName, 416 final BorderEdge edge) 417 { 418 EdgeWidgets ew = new EdgeWidgets(edge, edgeName); 420 edgeWidgets[edgeIndex] = ew; 421 422 EditFieldLayout efl = new EditFieldLayout(); 423 ew.numberComboBox = efl.addComboBox(I18N.get("FormatWin.count"), 424 edgeCountChoices()); 425 ew.styleComboBox = efl.addComboBox(I18N.get("FormatWin.style"), 426 edgeStyleChoices()); 427 ew.thicknessText = efl.addTextField(I18N.get("FormatWin.thickness"), 428 THICKNESS_COLS); 429 430 final JComboBox edgeBox = ew.numberComboBox; 432 edgeBox.addActionListener(new ActionListener() { 433 public void actionPerformed(ActionEvent e) { 434 String countStr = (String )edgeBox.getSelectedItem(); 435 String [] choices = edgeCountChoices(); 436 for (int i = 0; i < choices.length; ++i) 437 if (choices[i].equals(countStr)) { 438 edge.setNumber(i); 439 break; 440 } 441 } 442 }); 443 444 final JComboBox styleBox = ew.styleComboBox; 446 styleBox.addActionListener(new ActionListener() { 447 public void actionPerformed(ActionEvent e) { 448 String countStr = (String )styleBox.getSelectedItem(); 449 String [] choices = edgeStyleChoices(); 450 for (int i = 0; i < choices.length; ++i) 451 if (choices[i].equals(countStr)) { 452 edge.setStyle(i); 453 break; 454 } 455 } 456 }); 457 458 Box box = Box.createVerticalBox(); 460 box.add(new JLabel(ew.getName())); 461 box.add(Box.createVerticalStrut(8)); 462 box.add(efl.getPanel()); 463 464 return box; 465 } 466 467 470 protected void fillFormatTab() { 471 String fontFamilyName = format.getFontFamilyName(); 472 if (fontFamilyName == null) fontFamilyName = ""; 473 int index = fontIndexOf(fontFamilyName); 474 if (index == -1) { 475 fontFamily.setSelectedItem(null); 476 fontFamily.configureEditor(fontFamily.getEditor(), fontFamilyName); 477 } 478 else 479 fontFamily.setSelectedIndex(index); 480 481 int sizeVal = (int)format.getSize(); 482 index = sizeIndexOf(sizeVal); 483 if (index == -1) { 484 size.setSelectedItem(null); 485 size.configureEditor(size.getEditor(), "" + sizeVal); 486 } 487 else 488 size.setSelectedIndex(index); 489 490 bold.setSelected(format.isBold()); 491 italic.setSelected(format.isItalic()); 492 underline.setSelected(format.isUnderline()); 493 wrap.setSelected(format.isWrap()); 494 495 align.setSelectedIndex(format.getAlign()); 497 498 formatText.setText(format.getFormat()); 499 } 500 501 504 protected void fillBorderTab() { 505 for (int i = 0; i < 4; ++i) { 506 EdgeWidgets ew = edgeWidgets[i]; 507 508 ew.numberComboBox.setSelectedIndex(ew.edge.getNumber()); 511 512 ew.styleComboBox.setSelectedIndex(ew.edge.getStyle()); 515 516 ew.thicknessText.setText("" + ew.edge.getThickness()); 518 } 519 } 520 521 protected void doSave() { 522 Object selectedFont = fontFamily.getSelectedItem(); 523 if (selectedFont == null || selectedFont.toString().length() == 0) 524 format.setFontFamilyName(null); 525 else 526 format.setFontFamilyName(selectedFont.toString()); 527 528 double fontSize = Double.parseDouble(size.getSelectedItem().toString()); 529 format.setSize(fontSize); 530 format.setBold(bold.isSelected()); 531 532 format.setItalic(italic.isSelected()); 533 format.setUnderline(underline.isSelected()); 534 format.setWrap(wrap.isSelected()); 535 536 String alignText = (String )align.getSelectedItem(); 537 if (alignText.equals(I18N.get("FormatWin.align_left"))) 538 format.setAlign(Format.ALIGN_LEFT); 539 else if (alignText.equals(I18N.get("FormatWin.align_center"))) 540 format.setAlign(Format.ALIGN_CENTER); 541 else format.setAlign(Format.ALIGN_RIGHT); 543 544 format.setFormat(formatText.getText()); 545 546 for (int i = 0; i < 4; ++i) { 549 EdgeWidgets ew = edgeWidgets[i]; 550 ew.edge.setThickness(Double.parseDouble(ew.thicknessText.getText())); 551 } 552 553 if (designer.countSelectedFields() == 0 554 || field == field.getReport().getDefaultField()) step(field); 556 else designer.withSelectedFieldsDo(this); 558 } 559 560 566 public void step(Field f) { 567 FormatCommand cmd = new FormatCommand(f, format, border); 568 cmd.perform(); 569 commands.add(cmd); 570 } 571 572 protected void doRevert() { 573 fillFormatTab(); 574 fillBorderTab(); 575 } 576 577 } 578 | Popular Tags |