1 23 24 package org.gjt.sp.jedit.gui; 25 26 import javax.swing.border.*; 28 import javax.swing.text.Segment ; 29 import javax.swing.*; 30 import java.awt.event.*; 31 import java.awt.font.*; 32 import java.awt.geom.*; 33 import java.awt.*; 34 import java.text.*; 35 import java.util.Date ; 36 import org.gjt.sp.jedit.io.*; 37 import org.gjt.sp.jedit.textarea.*; 38 import org.gjt.sp.jedit.*; 39 import org.gjt.sp.util.*; 40 42 59 public class StatusBar extends JPanel implements WorkThreadProgressListener 60 { 61 public StatusBar(View view) 63 { 64 super(new BorderLayout()); 65 setBorder(new CompoundBorder(new EmptyBorder(4,0,0, 66 (OperatingSystem.isMacOS() ? 18 : 0)), 67 UIManager.getBorder("TextField.border"))); 68 69 this.view = view; 70 71 panel = new JPanel(new BorderLayout()); 72 box = new Box(BoxLayout.X_AXIS); 73 panel.add(BorderLayout.EAST,box); 74 add(BorderLayout.CENTER,panel); 75 76 MouseHandler mouseHandler = new MouseHandler(); 77 78 caretStatus = new ToolTipLabel(); 79 caretStatus.setToolTipText(jEdit.getProperty("view.status.caret-tooltip")); 80 caretStatus.addMouseListener(mouseHandler); 81 82 message = new JLabel(" "); 83 setMessageComponent(message); 84 85 mode = new ToolTipLabel(); 86 mode.setToolTipText(jEdit.getProperty("view.status.mode-tooltip")); 87 mode.addMouseListener(mouseHandler); 88 89 wrap = new ToolTipLabel(); 90 wrap.setHorizontalAlignment(SwingConstants.CENTER); 91 wrap.setToolTipText(jEdit.getProperty("view.status.wrap-tooltip")); 92 wrap.addMouseListener(mouseHandler); 93 94 multiSelect = new ToolTipLabel(); 95 multiSelect.setHorizontalAlignment(SwingConstants.CENTER); 96 multiSelect.setToolTipText(jEdit.getProperty("view.status.multi-tooltip")); 97 multiSelect.addMouseListener(mouseHandler); 98 99 rectSelect = new ToolTipLabel(); 100 rectSelect.setHorizontalAlignment(SwingConstants.CENTER); 101 rectSelect.setToolTipText(jEdit.getProperty("view.status.rect-tooltip")); 102 rectSelect.addMouseListener(mouseHandler); 103 104 overwrite = new ToolTipLabel(); 105 overwrite.setHorizontalAlignment(SwingConstants.CENTER); 106 overwrite.setToolTipText(jEdit.getProperty("view.status.overwrite-tooltip")); 107 overwrite.addMouseListener(mouseHandler); 108 109 lineSep = new ToolTipLabel(); 110 lineSep.setHorizontalAlignment(SwingConstants.CENTER); 111 lineSep.setToolTipText(jEdit.getProperty("view.status.linesep-tooltip")); 112 lineSep.addMouseListener(mouseHandler); 113 } 115 public void propertiesChanged() 117 { 118 Color fg = jEdit.getColorProperty("view.status.foreground"); 119 Color bg = jEdit.getColorProperty("view.status.background"); 120 121 showCaretStatus = jEdit.getBooleanProperty("view.status.show-caret-status"); 122 showEditMode = jEdit.getBooleanProperty("view.status.show-edit-mode"); 123 showFoldMode = jEdit.getBooleanProperty("view.status.show-fold-mode"); 124 showEncoding = jEdit.getBooleanProperty("view.status.show-encoding"); 125 showWrap = jEdit.getBooleanProperty("view.status.show-wrap"); 126 showMultiSelect = jEdit.getBooleanProperty("view.status.show-multi-select"); 127 showRectSelect = jEdit.getBooleanProperty("view.status.show-rect-select"); 128 showOverwrite = jEdit.getBooleanProperty("view.status.show-overwrite"); 129 showLineSeperator = jEdit.getBooleanProperty("view.status.show-line-seperator"); 130 boolean showMemory = jEdit.getBooleanProperty("view.status.show-memory"); 131 boolean showClock = jEdit.getBooleanProperty("view.status.show-clock"); 132 133 panel.setBackground(bg); 134 panel.setForeground(fg); 135 caretStatus.setBackground(bg); 136 caretStatus.setForeground(fg); 137 message.setBackground(bg); 138 message.setForeground(fg); 139 mode.setBackground(bg); 140 mode.setForeground(fg); 141 wrap.setBackground(bg); 142 wrap.setForeground(fg); 143 multiSelect.setBackground(bg); 144 multiSelect.setForeground(fg); 145 rectSelect.setBackground(bg); 146 rectSelect.setForeground(fg); 147 overwrite.setBackground(bg); 148 overwrite.setForeground(fg); 149 lineSep.setBackground(bg); 150 lineSep.setForeground(fg); 151 152 Font font = new JLabel().getFont(); 154 FontMetrics fm = getFontMetrics(font); 156 Dimension dim; 157 158 if (showCaretStatus) 159 { 160 panel.add(BorderLayout.WEST,caretStatus); 161 162 caretStatus.setFont(font); 163 164 dim = new Dimension(fm.stringWidth(caretTestStr), 165 fm.getHeight()); 166 caretStatus.setPreferredSize(dim); 167 } 168 else 169 panel.remove(caretStatus); 170 171 box.removeAll(); 172 173 if (showEncoding || showEditMode || showFoldMode) 174 box.add(mode); 175 176 if (showWrap) 177 { 178 dim = new Dimension(Math.max( 179 Math.max(fm.charWidth('-'),fm.charWidth('H')), 180 fm.charWidth('S')) + 1,fm.getHeight()); 181 wrap.setPreferredSize(dim); 182 wrap.setMaximumSize(dim); 183 box.add(wrap); 184 } 185 186 if (showMultiSelect) 187 { 188 dim = new Dimension( 189 Math.max(fm.charWidth('-'),fm.charWidth('M')) + 1, 190 fm.getHeight()); 191 multiSelect.setPreferredSize(dim); 192 multiSelect.setMaximumSize(dim); 193 box.add(multiSelect); 194 } 195 196 if (showRectSelect) 197 { 198 dim = new Dimension( 199 Math.max(fm.charWidth('-'),fm.charWidth('R')) + 1, 200 fm.getHeight()); 201 rectSelect.setPreferredSize(dim); 202 rectSelect.setMaximumSize(dim); 203 box.add(rectSelect); 204 } 205 206 if (showOverwrite) 207 { 208 dim = new Dimension( 209 Math.max(fm.charWidth('-'),fm.charWidth('O')) + 1, 210 fm.getHeight()); 211 overwrite.setPreferredSize(dim); 212 overwrite.setMaximumSize(dim); 213 box.add(overwrite); 214 } 215 216 if (showLineSeperator) 217 { 218 dim = new Dimension(Math.max( 219 Math.max(fm.charWidth('U'), 220 fm.charWidth('W')), 221 fm.charWidth('M')) + 1, 222 fm.getHeight()); 223 lineSep.setPreferredSize(dim); 224 lineSep.setMaximumSize(dim); 225 box.add(lineSep); 226 } 227 228 if (showMemory) 229 box.add(new MemoryStatus()); 230 231 if (showClock) 232 box.add(new Clock()); 233 234 updateBufferStatus(); 235 updateMiscStatus(); 236 } 238 public void addNotify() 240 { 241 super.addNotify(); 242 VFSManager.getIOThreadPool().addProgressListener(this); 243 } 245 public void removeNotify() 247 { 248 super.removeNotify(); 249 VFSManager.getIOThreadPool().removeProgressListener(this); 250 } 252 254 public void statusUpdate(final WorkThreadPool threadPool, int threadIndex) 256 { 257 SwingUtilities.invokeLater(new Runnable () 258 { 259 public void run() 260 { 261 if(message != null && !"".equals(message.getText().trim()) 263 && !currentMessageIsIO) 264 return; 265 266 int requestCount = threadPool.getRequestCount(); 267 if(requestCount == 0) 268 { 269 setMessageAndClear(jEdit.getProperty( 270 "view.status.io.done")); 271 currentMessageIsIO = true; 272 } 273 else if(requestCount == 1) 274 { 275 setMessage(jEdit.getProperty( 276 "view.status.io-1")); 277 currentMessageIsIO = true; 278 } 279 else 280 { 281 Object [] args = {Integer.valueOf(requestCount)}; 282 setMessage(jEdit.getProperty( 283 "view.status.io",args)); 284 currentMessageIsIO = true; 285 } 286 } 287 }); 288 } 290 public void progressUpdate(WorkThreadPool threadPool, int threadIndex) 292 { 293 } 295 297 303 public void setMessageAndClear(String message) 304 { 305 setMessage(message); 306 307 tempTimer = new Timer(0,new ActionListener() 308 { 309 public void actionPerformed(ActionEvent evt) 310 { 311 if(isShowing()) 313 setMessage(null); 314 } 315 }); 316 317 tempTimer.setInitialDelay(10000); 318 tempTimer.setRepeats(false); 319 tempTimer.start(); 320 } 322 326 public void setMessage(String message) 327 { 328 if(tempTimer != null) 329 { 330 tempTimer.stop(); 331 tempTimer = null; 332 } 333 334 setMessageComponent(this.message); 335 336 if(message == null) 337 { 338 if(view.getMacroRecorder() != null) 339 this.message.setText(jEdit.getProperty("view.status.recording")); 340 else 341 this.message.setText(" "); 342 } 343 else 344 this.message.setText(message); 345 } 347 public void setMessageComponent(Component comp) 349 { 350 currentMessageIsIO = false; 351 352 if (comp == null || messageComp == comp) 353 { 354 return; 355 } 356 357 messageComp = comp; 358 panel.add(BorderLayout.CENTER, messageComp); 359 } 361 public void updateCaretStatus() 363 { 364 367 if (showCaretStatus) 368 { 369 Buffer buffer = view.getBuffer(); 370 371 if(!buffer.isLoaded() || 372 373 buffer != view.getTextArea().getBuffer()) 374 { 375 caretStatus.setText(" "); 376 return; 377 } 378 379 JEditTextArea textArea = view.getTextArea(); 380 381 int currLine = textArea.getCaretLine(); 382 383 if(currLine >= buffer.getLineCount()) 389 return; 391 int start = textArea.getLineStartOffset(currLine); 392 int dot = textArea.getCaretPosition() - start; 393 394 if(dot < 0) 396 return; 397 398 buffer.getText(start,dot,seg); 399 int virtualPosition = StandardUtilities.getVirtualWidth(seg, 400 buffer.getTabSize()); 401 402 buf.setLength(0); 403 buf.append(Integer.toString(currLine + 1)); 404 buf.append(','); 405 buf.append(Integer.toString(dot + 1)); 406 407 if (virtualPosition != dot) 408 { 409 buf.append('-'); 410 buf.append(Integer.toString(virtualPosition + 1)); 411 } 412 413 buf.append(' '); 414 415 int firstLine = textArea.getFirstLine(); 416 int visible = textArea.getVisibleLines(); 417 int lineCount = textArea.getDisplayManager().getScrollLineCount(); 418 419 if (visible >= lineCount) 420 { 421 buf.append("All"); 422 } 423 else if (firstLine == 0) 424 { 425 buf.append("Top"); 426 } 427 else if (firstLine + visible >= lineCount) 428 { 429 buf.append("Bot"); 430 } 431 else 432 { 433 float percent = (float)firstLine / (float)lineCount 434 * 100.0f; 435 buf.append(Integer.toString((int)percent)); 436 buf.append('%'); 437 } 438 439 caretStatus.setText(buf.toString()); 440 } 441 } 443 public void updateBufferStatus() 445 { 446 449 Buffer buffer = view.getBuffer(); 450 451 if (showWrap) 452 { 453 String wrap = buffer.getStringProperty("wrap"); 454 if(wrap.equals("none")) 455 this.wrap.setText("-"); 456 else if(wrap.equals("hard")) 457 this.wrap.setText("H"); 458 else if(wrap.equals("soft")) 459 this.wrap.setText("S"); 460 } 461 462 if (showLineSeperator) 463 { 464 String lineSep = buffer.getStringProperty("lineSeparator"); 465 if("\n".equals(lineSep)) 466 this.lineSep.setText("U"); 467 else if("\r\n".equals(lineSep)) 468 this.lineSep.setText("W"); 469 else if("\r".equals(lineSep)) 470 this.lineSep.setText("M"); 471 } 472 473 if (showEditMode || showFoldMode || showEncoding) 474 { 475 479 buf.setLength(0); 480 481 if (buffer.isLoaded()) 482 { 483 if (showEditMode) 484 buf.append(buffer.getMode().getName()); 485 if (showFoldMode) 486 { 487 if (showEditMode) 488 buf.append(','); 489 buf.append((String )view.getBuffer().getProperty("folding")); 490 } 491 if (showEncoding) 492 { 493 if (showEditMode || showFoldMode) 494 buf.append(','); 495 buf.append(buffer.getStringProperty("encoding")); 496 } 497 } 498 499 mode.setText('(' + buf.toString() + ')'); 500 } 501 } 503 public void updateMiscStatus() 505 { 506 509 JEditTextArea textArea = view.getTextArea(); 510 511 if (showMultiSelect) 512 multiSelect.setText(textArea.isMultipleSelectionEnabled() 513 ? "M" : "-"); 514 515 if (showRectSelect) 516 rectSelect.setText(textArea.isRectangularSelectionEnabled() 517 ? "R" : "-"); 518 519 if (showOverwrite) 520 overwrite.setText(textArea.isOverwriteEnabled() 521 ? "O" : "-"); 522 } 524 private View view; 526 private JPanel panel; 527 private Box box; 528 private ToolTipLabel caretStatus; 529 private Component messageComp; 530 private JLabel message; 531 private JLabel mode; 532 private JLabel wrap; 533 private JLabel multiSelect; 534 private JLabel rectSelect; 535 private JLabel overwrite; 536 private JLabel lineSep; 537 StringBuilder buf = new StringBuilder (); 538 private Timer tempTimer; 539 private boolean currentMessageIsIO; 540 541 private Segment seg = new Segment (); 542 543 private boolean showCaretStatus; 544 private boolean showEditMode; 545 private boolean showFoldMode; 546 private boolean showEncoding; 547 private boolean showWrap; 548 private boolean showMultiSelect; 549 private boolean showRectSelect; 550 private boolean showOverwrite; 551 private boolean showLineSeperator; 552 554 static final String caretTestStr = "9999,999-999 99%"; 555 556 class MouseHandler extends MouseAdapter 558 { 559 public void mouseClicked(MouseEvent evt) 560 { 561 Buffer buffer = view.getBuffer(); 562 563 Object source = evt.getSource(); 564 if(source == caretStatus) 565 { 566 if(evt.getClickCount() == 2) 567 view.getTextArea().showGoToLineDialog(); 568 } 569 else if(source == mode) 570 { 571 if(evt.getClickCount() == 2) 572 new BufferOptions(view,view.getBuffer()); 573 } 574 else if(source == wrap) 575 buffer.toggleWordWrap(view); 576 else if(source == multiSelect) 577 view.getTextArea().toggleMultipleSelectionEnabled(); 578 else if(source == rectSelect) 579 view.getTextArea().toggleRectangularSelectionEnabled(); 580 else if(source == overwrite) 581 view.getTextArea().toggleOverwriteEnabled(); 582 else if(source == lineSep) 583 buffer.toggleLineSeparator(view); 584 } 585 } 587 static class ToolTipLabel extends JLabel 589 { 590 public Point getToolTipLocation(MouseEvent event) 592 { 593 return new Point(event.getX(),-20); 594 } } 597 class MemoryStatus extends JComponent implements ActionListener 599 { 600 MemoryStatus() 602 { 603 Font font = new JLabel().getFont(); 605 MemoryStatus.this.setFont(font); 607 608 FontRenderContext frc = new FontRenderContext( 609 null,false,false); 610 Rectangle2D bounds = font.getStringBounds( 611 memoryTestStr,frc); 612 Dimension dim = new Dimension((int)bounds.getWidth(), 613 (int)bounds.getHeight()); 614 setPreferredSize(dim); 615 setMaximumSize(dim); 616 lm = font.getLineMetrics(memoryTestStr,frc); 617 618 setForeground(jEdit.getColorProperty("view.status.foreground")); 619 setBackground(jEdit.getColorProperty("view.status.background")); 620 621 progressForeground = jEdit.getColorProperty( 622 "view.status.memory.foreground"); 623 progressBackground = jEdit.getColorProperty( 624 "view.status.memory.background"); 625 626 addMouseListener(new MouseHandler()); 627 } 629 public void addNotify() 631 { 632 super.addNotify(); 633 timer = new Timer(2000,this); 634 timer.start(); 635 ToolTipManager.sharedInstance().registerComponent(this); 636 } 638 public void removeNotify() 640 { 641 timer.stop(); 642 ToolTipManager.sharedInstance().unregisterComponent(this); 643 super.removeNotify(); 644 } 646 public String getToolTipText() 648 { 649 Runtime runtime = Runtime.getRuntime(); 650 int freeMemory = (int)(runtime.freeMemory() >> 10); 651 int totalMemory = (int)(runtime.totalMemory() >> 10); 652 int usedMemory = totalMemory - freeMemory; 653 args[0] = new Integer (usedMemory); 654 args[1] = new Integer (totalMemory); 655 return jEdit.getProperty("view.status.memory-tooltip",args); 656 } 658 public Point getToolTipLocation(MouseEvent event) 660 { 661 return new Point(event.getX(),-20); 662 } 664 public void actionPerformed(ActionEvent evt) 666 { 667 MemoryStatus.this.repaint(); 668 } 670 public void paintComponent(Graphics g) 672 { 673 Insets insets = new Insets(0,0,0,0); 675 Runtime runtime = Runtime.getRuntime(); 676 int freeMemory = (int)(runtime.freeMemory() >> 10); 677 int totalMemory = (int)(runtime.totalMemory() >> 10); 678 int usedMemory = totalMemory - freeMemory; 679 680 int width = MemoryStatus.this.getWidth() 681 - insets.left - insets.right; 682 int height = MemoryStatus.this.getHeight() 683 - insets.top - insets.bottom - 1; 684 685 float fraction = ((float)usedMemory) / totalMemory; 686 687 g.setColor(progressBackground); 688 689 g.fillRect(insets.left,insets.top, 690 (int)(width * fraction), 691 height); 692 693 String str = (usedMemory >> 10) + "/" 694 + (totalMemory >> 10) + "Mb"; 695 696 FontRenderContext frc = new FontRenderContext(null,false,false); 697 698 Rectangle2D bounds = g.getFont().getStringBounds(str,frc); 699 700 Graphics g2 = g.create(); 701 g2.setClip(insets.left,insets.top, 702 (int)(width * fraction), 703 height); 704 705 g2.setColor(progressForeground); 706 707 g2.drawString(str, 708 insets.left + ((int) (width - bounds.getWidth()) >> 1), 709 (int)(insets.top + lm.getAscent())); 710 711 g2.dispose(); 712 713 g2 = g.create(); 714 715 g2.setClip(insets.left + (int)(width * fraction), 716 insets.top,MemoryStatus.this.getWidth() 717 - insets.left - (int)(width * fraction), 718 height); 719 720 g2.setColor(MemoryStatus.this.getForeground()); 721 722 g2.drawString(str, 723 insets.left + ((int) (width - bounds.getWidth()) >> 1), 724 (int)(insets.top + lm.getAscent())); 725 726 g2.dispose(); 727 } 729 private static final String memoryTestStr = "999/999Mb"; 731 732 private final LineMetrics lm; 733 private final Color progressForeground; 734 private final Color progressBackground; 735 736 private final Integer [] args = new Integer [2]; 737 738 739 private Timer timer; 740 742 class MouseHandler extends MouseAdapter 744 { 745 public void mousePressed(MouseEvent evt) 746 { 747 if(evt.getClickCount() == 2) 748 { 749 jEdit.showMemoryDialog(view); 750 repaint(); 751 } 752 } 753 } } 756 static class Clock extends JLabel implements ActionListener 758 { 759 Clock() 761 { 762 770 771 setForeground(jEdit.getColorProperty("view.status.foreground")); 772 setBackground(jEdit.getColorProperty("view.status.background")); 773 } 775 public void addNotify() 777 { 778 super.addNotify(); 779 update(); 780 781 int millisecondsPerMinute = 1000 * 60; 782 783 timer = new Timer(millisecondsPerMinute,this); 784 timer.setInitialDelay((int)( 785 millisecondsPerMinute 786 - System.currentTimeMillis() 787 % millisecondsPerMinute) + 500); 788 timer.start(); 789 ToolTipManager.sharedInstance().registerComponent(this); 790 } 792 public void removeNotify() 794 { 795 timer.stop(); 796 ToolTipManager.sharedInstance().unregisterComponent(this); 797 super.removeNotify(); 798 } 800 public String getToolTipText() 802 { 803 return new Date ().toString(); 804 } 806 public Point getToolTipLocation(MouseEvent event) 808 { 809 return new Point(event.getX(),-20); 810 } 812 public void actionPerformed(ActionEvent evt) 814 { 815 update(); 816 } 818 private Timer timer; 820 821 private static String getTime() 823 { 824 return DateFormat.getTimeInstance( 825 DateFormat.SHORT).format(new Date ()); 826 } 828 private void update() 830 { 831 setText(getTime()); 832 } 834 } } 837 | Popular Tags |