001: /*
002: * PlainEditor.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Color;
016: import java.awt.FlowLayout;
017: import java.awt.Window;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import javax.swing.Box;
021: import javax.swing.JCheckBox;
022: import javax.swing.JLabel;
023: import javax.swing.JPanel;
024: import javax.swing.JScrollPane;
025: import javax.swing.JTextArea;
026: import javax.swing.text.Document;
027: import javax.swing.text.PlainDocument;
028: import workbench.gui.editor.SearchAndReplace;
029: import workbench.interfaces.Restoreable;
030: import workbench.interfaces.TextContainer;
031: import workbench.resource.ResourceMgr;
032: import workbench.resource.Settings;
033:
034: /**
035: * A simple text editor based on a JTextArea.
036: * The panel displays also a checkbox to turn word wrapping on and off
037: * and optionally an information label.
038: *
039: * @author support@sql-workbench.net
040: */
041: public class PlainEditor extends JPanel implements ActionListener,
042: TextContainer, Restoreable {
043: private JTextArea editor;
044: private JCheckBox wordWrap;
045: private SearchAndReplace replacer;
046: private Color enabledBackground;
047: private JLabel infoText;
048: private JPanel toolPanel;
049:
050: public PlainEditor(Window parent) {
051: editor = new JTextArea();
052: this .enabledBackground = editor.getBackground();
053: editor.putClientProperty("JTextArea.infoBackground",
054: Boolean.TRUE);
055: TextComponentMouseListener l = new TextComponentMouseListener(
056: this .editor);
057:
058: JScrollPane scroll = new JScrollPane(editor);
059: editor.setLineWrap(true);
060: editor.setWrapStyleWord(true);
061: editor.setFont(Settings.getInstance().getEditorFont());
062: this .setLayout(new BorderLayout());
063: toolPanel = new JPanel();
064: toolPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
065: wordWrap = new JCheckBox(ResourceMgr.getString("LblWordWrap"));
066: wordWrap.addActionListener(this );
067: toolPanel.add(wordWrap);
068:
069: this .add(toolPanel, BorderLayout.NORTH);
070: this .add(scroll, BorderLayout.CENTER);
071: this .setFocusable(false);
072: Document d = editor.getDocument();
073: if (d != null) {
074: int tabSize = Settings.getInstance().getEditorTabWidth();
075: d.putProperty(PlainDocument.tabSizeAttribute, new Integer(
076: tabSize));
077: }
078: replacer = new SearchAndReplace(this , this );
079: l.addAction(replacer.getFindAction());
080: l.addAction(replacer.getFindAgainAction());
081: l.addAction(replacer.getReplaceAction());
082: }
083:
084: public int getCaretPosition() {
085: return this .editor.getCaretPosition();
086: }
087:
088: public int getSelectionEnd() {
089: return this .editor.getSelectionEnd();
090: }
091:
092: public int getSelectionStart() {
093: return this .editor.getSelectionStart();
094: }
095:
096: public void select(int start, int end) {
097: this .editor.select(start, end);
098: }
099:
100: public void setInfoText(String text) {
101: if (this .infoText == null) {
102: this .infoText = new JLabel();
103: this .toolPanel.add(Box.createHorizontalStrut(10));
104: this .toolPanel.add(infoText);
105: }
106: this .infoText.setText(text);
107: }
108:
109: public void requestFocus() {
110: this .editor.requestFocus();
111: }
112:
113: public boolean requestFocusInWindow() {
114: return this .editor.requestFocusInWindow();
115: }
116:
117: public void restoreSettings() {
118: boolean wrap = Settings.getInstance().getPlainEditorWordWrap();
119: wordWrap.setSelected(wrap);
120: this .editor.setLineWrap(wrap);
121: }
122:
123: public void saveSettings() {
124: Settings.getInstance().setPlainEditorWordWrap(
125: wordWrap.isSelected());
126: }
127:
128: public void setSelectedText(String aText) {
129: this .editor.replaceSelection(aText);
130: }
131:
132: public String getText() {
133: return this .editor.getText();
134: }
135:
136: public String getSelectedText() {
137: return this .editor.getSelectedText();
138: }
139:
140: public void setText(String aText) {
141: this .editor.setText(aText);
142: }
143:
144: public void setCaretPosition(int pos) {
145: this .editor.setCaretPosition(pos);
146: }
147:
148: public void actionPerformed(ActionEvent e) {
149: this .editor.setLineWrap(this .wordWrap.isSelected());
150: }
151:
152: public void setEditable(boolean flag) {
153: this.editor.setEditable(flag);
154: this.editor.setBackground(enabledBackground);
155: }
156:
157: }
|