001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.components;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Dimension;
018:
019: import javax.swing.BorderFactory;
020: import javax.swing.JComponent;
021: import javax.swing.JLabel;
022: import javax.swing.JPanel;
023: import javax.swing.event.CaretEvent;
024: import javax.swing.event.CaretListener;
025:
026: import com.eviware.soapui.SoapUI;
027: import com.jgoodies.forms.builder.ButtonBarBuilder;
028: import com.jgoodies.forms.layout.Sizes;
029:
030: /**
031: * A simple status bar for editors
032: *
033: * @author Ole.Matzura
034: */
035:
036: public class JEditorStatusBar extends JPanel implements CaretListener {
037: private JLabel caretLabel;
038: private JLabel infoLabel;
039: private JEditorStatusBarTarget target;
040: private JPanel statusPanel;
041:
042: public JEditorStatusBar() {
043: this (null);
044: }
045:
046: public JEditorStatusBar(JEditorStatusBarTarget target) {
047: this .target = target;
048:
049: caretLabel = new JLabel();
050: caretLabel.setPreferredSize(new Dimension(60, 16));
051:
052: infoLabel = new JLabel();
053: infoLabel.setVisible(false);
054:
055: caretLabel.setBorder(BorderFactory.createCompoundBorder(
056: BorderFactory.createMatteBorder(0, 1, 0, 0,
057: Color.LIGHT_GRAY), BorderFactory
058: .createMatteBorder(0, 1, 0, 0, Color.WHITE)));
059:
060: ButtonBarBuilder builder = new ButtonBarBuilder(this );
061: builder.addGriddedGrowing(infoLabel);
062: builder.addStrut(Sizes.pixel(2));
063:
064: statusPanel = new JPanel(new BorderLayout());
065: statusPanel.setPreferredSize(new Dimension(60, 16));
066:
067: statusPanel.setBorder(BorderFactory.createCompoundBorder(
068: BorderFactory.createMatteBorder(0, 1, 0, 0,
069: Color.LIGHT_GRAY), BorderFactory
070: .createMatteBorder(0, 1, 0, 0, Color.WHITE)));
071:
072: builder.addFixed(statusPanel);
073: builder.addFixed(caretLabel);
074: builder.getPanel();
075: }
076:
077: public void addNotify() {
078: super .addNotify();
079:
080: if (target != null)
081: target.addCaretListener(this );
082: }
083:
084: public void removeNotify() {
085: super .removeNotify();
086:
087: if (target != null)
088: target.removeCaretListener(this );
089: }
090:
091: public void caretUpdate(CaretEvent e) {
092: try {
093: if (target == null)
094: caretLabel.setText("");
095:
096: int offset = target.getCaretPosition();
097: int line = target.getLineOfOffset(offset);
098: int column = offset - target.getLineStartOffset(line);
099:
100: caretLabel.setText(" " + (line + 1) + " : " + (column + 1));
101: } catch (Exception e1) {
102: SoapUI.logError(e1);
103: }
104: }
105:
106: public void setTarget(JEditorStatusBarTarget target) {
107: if (this .target != null)
108: this .target.removeCaretListener(this );
109:
110: this .target = target;
111: this .target.addCaretListener(this );
112:
113: caretUpdate(null);
114: }
115:
116: public void setInfo(String txt) {
117: infoLabel.setText(txt);
118: infoLabel.setVisible(txt != null);
119: }
120:
121: public void setStatusComponent(JComponent statusComponent) {
122: statusPanel.removeAll();
123: statusPanel.add(statusComponent, BorderLayout.CENTER);
124: statusPanel.revalidate();
125: }
126:
127: /**
128: * Target for caret-status
129: *
130: * @author Ole.Matzura
131: */
132:
133: public interface JEditorStatusBarTarget {
134: void addCaretListener(CaretListener listener);
135:
136: int getCaretPosition();
137:
138: void removeCaretListener(CaretListener listener);
139:
140: int getLineStartOffset(int line) throws Exception;
141:
142: int getLineOfOffset(int offset) throws Exception;;
143: }
144: }
|