1 package jimm.datavision.gui; 2 import jimm.datavision.*; 3 import jimm.datavision.field.Field; 4 import jimm.datavision.gui.cmd.Command; 5 import jimm.datavision.gui.cmd.SectionResizeCommand; 6 import jimm.datavision.gui.cmd.SectionPageBreakCommand; 7 import java.util.*; 8 import java.awt.*; 9 import java.awt.event.*; 10 import javax.swing.*; 11 12 17 public class SectionWidget extends JPanel implements ActionListener { 18 19 public static final int LHS_WIDTH = 125; 20 21 protected static final Font DEFAULT_POPUP_FONT = 22 new Font("Serif", Font.PLAIN, 10); 23 24 static final Color NORMAL_COLOR = Color.white; 25 static final Color SUPPRESSED_COLOR = Color.lightGray; 26 27 protected String name; 28 protected String popupName; 29 protected Designer designer; 30 protected Section section; 31 protected SectionNameLabel label; 32 protected SectionFieldPanel fieldPanel; 33 protected JPopupMenu popup; 34 protected JMenuItem nameItem, editSuppress; 35 protected JCheckBoxMenuItem togglePageBreak; 36 protected JMenuItem deleteGroup, addGroup, deleteSection, insertSection; 37 38 41 class PopupListener extends MouseAdapter { 42 public void mousePressed(MouseEvent e) { maybeShowPopup(e); } 43 public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } 44 private void maybeShowPopup(MouseEvent e) { 45 if (e.isPopupTrigger()) 46 showPopup(e); 47 } 48 } 49 50 57 public SectionWidget(Designer win, Section sect, String name) { 58 super(); 59 60 designer = win; 61 section = sect; 62 this.name = name; 63 this.popupName = ""; 64 65 setLayout(new SectionLayout()); 66 setPreferredSize(new Dimension(getTotalWidth(), getHeight())); 67 68 buildPopupMenu(); 69 addMouseListener(new PopupListener()); 70 71 buildDisplayName(); 73 74 fieldPanel = new SectionFieldPanel(this); 76 fieldPanel.setLayout(null); 77 add(fieldPanel); 78 for (Iterator iter = section.fields(); iter.hasNext(); ) { 79 Field f = (Field)iter.next(); 80 FieldWidget fw = f.makeWidget(this); 81 fieldPanel.add(fw.getComponent(), 0); } 83 84 fieldPanel.setHidden(section.isHidden()); 87 88 fieldPanel.addMouseListener(new PopupListener() { 91 public void mouseClicked(MouseEvent e) { 92 deselectAll(); 93 if (designer.isPlacingNewTextField()) 94 createNewTextField(e); } 96 }); 97 98 add(new SectionResizer(this, designer.sectionContainer)); 100 } 101 102 105 protected void buildPopupMenu() { 106 popup = new JPopupMenu(popupName); 107 108 nameItem = 109 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_dummy_title", 110 DEFAULT_POPUP_FONT); 111 popup.addSeparator(); 112 editSuppress = 113 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_suppress", 114 DEFAULT_POPUP_FONT); 115 togglePageBreak = 116 MenuUtils.addCheckboxToMenu(this, popup, 117 "SectionWidget.popup_page_break", 118 DEFAULT_POPUP_FONT); 119 popup.addSeparator(); 120 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_shrink", 121 DEFAULT_POPUP_FONT); 122 popup.addSeparator(); 123 deleteGroup = 124 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_delete_group", 125 DEFAULT_POPUP_FONT); 126 addGroup = 127 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_add_group", 128 DEFAULT_POPUP_FONT); 129 popup.addSeparator(); 130 deleteSection = 131 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_delete_section", 132 DEFAULT_POPUP_FONT); 133 insertSection = 134 MenuUtils.addToMenu(this, popup, "SectionWidget.popup_insert_section", 135 DEFAULT_POPUP_FONT); 136 } 137 138 142 public void actionPerformed(ActionEvent e) { 143 String command = e.getActionCommand(); 144 if (command == null) return; 145 if ("suppress".equals(command)) 146 editSuppression(); 147 else if ("page_break".equals(command)) 148 togglePageBreak(); 149 else if ("delete_group".equals(command)) 150 designer.deleteGroupContaining(section); 151 else if ("add_group".equals(command)) 152 designer.openNewGroupWin(); 153 else if ("delete_section".equals(command)) 154 designer.deleteSection(section); 155 else if ("insert_section_below".equals(command)) 156 designer.insertSectionBelow(section); 157 else if ("shrink_to_fit".equals(command)) 158 shrinkToFit(); 159 } 160 161 164 protected void enablePopupMenuItems() { 165 nameItem.setText(popupName); 166 togglePageBreak.setSelected(section.hasPageBreak()); 167 deleteSection.setEnabled(!section.getReport().isOneOfAKind(section)); 168 deleteGroup.setEnabled(section.getReport().isInsideGroup(section)); 169 } 170 171 175 protected void buildDisplayName() { 176 label = new SectionNameLabel(name, this); 177 add(label); 178 } 179 180 185 public void setDisplayName(String name) { 186 this.name = name; 187 label.setText(name); 188 } 189 190 195 public void setPopupName(String popupName) { 196 this.popupName = popupName; 197 nameItem.setText(popupName); 198 } 199 200 205 public Report getReport() { return section.getReport(); } 206 207 212 public Section getSection() { return section; } 213 214 220 public SectionArea getSectionArea() { return section.getArea(); } 221 222 227 public Designer getDesigner() { return designer; } 228 229 235 public int getPaperWidth() { 236 return (int)section.getWidth(); 237 } 238 239 244 public int getTotalWidth() { 245 return LHS_WIDTH + getPaperWidth(); 246 } 247 248 253 public int getHeight() { 254 return (int)section.getMinHeight() + SectionResizer.HEIGHT; 255 } 256 257 262 public int getSectionHeight() { return (int)section.getMinHeight(); } 263 264 269 public int getMinSectionHeight() { 270 int minY = 0; 271 Component[] fieldWidgets = fieldPanel.getComponents(); 272 for (int i = 0; i < fieldWidgets.length; ++i) { 273 int y = fieldWidgets[i].getBounds().y 274 + fieldWidgets[i].getBounds().height; 275 if (y > minY) 276 minY = y; 277 } 278 return minY; 279 } 280 281 285 void paperSizeChanged() { 286 setPreferredSize(new Dimension(getTotalWidth(), getHeight())); 287 invalidate(); 288 } 289 290 293 void editSuppression() { 294 new SuppressionProcWin(designer, this); 295 } 296 297 300 void togglePageBreak() { 301 performCommand(new SectionPageBreakCommand(section)); 302 } 303 304 309 public void shrinkToFit() { 310 SectionResizeCommand cmd = new SectionResizeCommand(this); 311 growBy(getMinSectionHeight() - getSectionHeight()); 312 performCommand(cmd); 313 } 314 315 320 public void growToFit() { 321 int dy = getMinSectionHeight() - getSectionHeight(); 322 323 if (dy > 0) 324 growBy(dy); 325 } 326 327 333 public void growBy(int dy) { 334 if (dy == 0) return; 335 336 int origHeight = (int)section.getMinHeight(); 338 int newHeight = origHeight + dy; 339 int minHeight = getMinSectionHeight(); 340 if (newHeight < minHeight) { 341 newHeight = minHeight; 342 if (newHeight == origHeight) 343 return; 344 } 345 346 section.setMinHeight(newHeight); setPreferredSize(new Dimension(getTotalWidth(), getHeight())); revalidate(); 349 } 350 351 364 public void resizeBy(int dy, SectionResizeCommand cmd) { 365 growBy(dy); 366 performCommand(cmd); 367 } 368 369 375 public void performCommand(Command cmd) { 376 designer.performCommand(cmd); 377 } 378 379 383 public void setIgnoreKeys(boolean ignoreKeys) { 384 designer.setIgnoreKeys(ignoreKeys); 385 } 386 387 396 public FieldWidget addTitleField(int x, int width, String title) { 397 return designer.addTitleField(x, width, title); 398 } 399 400 408 void pickUp(java.awt.Point mouseScreenPos) { 409 designer.pickUp(mouseScreenPos); 410 } 411 412 423 void putDown(FieldWidget f, java.awt.Point origScreenPos, 424 java.awt.Point mouseScreenPos) 425 { 426 designer.putDown(f, origScreenPos, mouseScreenPos); 427 } 428 429 437 void startStretching(java.awt.Point mouseScreenPos) { 438 designer.startStretching(mouseScreenPos); 439 } 440 441 450 void stopStretching(FieldWidget f, jimm.datavision.field.Rectangle origBounds) 451 { 452 designer.stopStretching(f, origBounds); 453 } 454 455 464 void dragSelectedWidgets(int action, java.awt.Point mouseScreenPos) { 465 designer.dragSelectedWidgets(action, mouseScreenPos); 466 } 467 468 480 void select(FieldWidget fieldWidget, boolean makeSelected, 481 boolean deselectOthers) 482 { 483 designer.select(fieldWidget, makeSelected, deselectOthers); 484 } 485 486 492 void deselectAll() { 493 designer.deselectAll(); 494 } 495 496 502 public void addField(FieldWidget fw) { 503 fieldPanel.add(fw.getComponent(), 0); fw.sectionWidget = this; 505 fw.getComponent().setBackground(section.isHidden() 506 ? SUPPRESSED_COLOR : NORMAL_COLOR); 507 growToFit(); 508 } 509 510 520 public void removeField(FieldWidget fw) { 521 fieldPanel.remove(fw.getComponent()); 522 } 523 524 public SectionFieldPanel getFieldPanel() { return fieldPanel; } 525 526 531 void snapToGrid(jimm.datavision.field.Rectangle r) { 532 designer.snapToGrid(r); 533 } 534 535 542 void setFieldVisibility(boolean newVisiblity, FieldWidget fw) { 543 designer.setFieldVisibility(newVisiblity, fw); 544 } 545 546 551 void createNewTextField(MouseEvent e) { 552 designer.createNewTextField(this, e); 553 } 554 555 560 void showPopup(MouseEvent e) { 561 enablePopupMenuItems(); 562 popup.show(e.getComponent(), e.getX(), e.getY()); 563 } 564 565 } 566 | Popular Tags |