001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2001-2002 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.Font;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.MouseAdapter;
025: import java.awt.event.MouseEvent;
026: import java.util.Arrays;
027:
028: import javax.swing.JTextArea;
029:
030: import net.sourceforge.squirrel_sql.fw.gui.TextPopupMenu;
031: import net.sourceforge.squirrel_sql.fw.gui.action.BaseAction;
032: import net.sourceforge.squirrel_sql.fw.gui.action.MakeEditableCommand;
033: import net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.CellComponentFactory;
034: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
035: import net.sourceforge.squirrel_sql.fw.util.StringManager;
036:
037: //??RENAME to DataSetViewerTextDestination
038:
039: public class DataSetViewerTextPanel extends
040: BaseDataSetViewerDestination {
041:
042: private static final StringManager s_stringMgr = StringManagerFactory
043: .getStringManager(DataSetViewerTextPanel.class);
044:
045: private final static int COLUMN_PADDING = 2;
046:
047: private MyJTextArea _outText = null;
048: private int _rowCount;
049:
050: public DataSetViewerTextPanel() {
051: super ();
052: _rowCount = 0;
053: }
054:
055: public void init(IDataSetUpdateableModel updateableObject) {
056: _outText = new MyJTextArea(updateableObject);
057: }
058:
059: public void clear() {
060: _outText.setText("");
061: _rowCount = 0;
062: }
063:
064: public void setColumnDefinitions(ColumnDisplayDefinition[] colDefs) {
065: super .setColumnDefinitions(colDefs);
066: colDefs = getColumnDefinitions(); // in case superclass modifies them.
067: if (getShowHeadings()) {
068: StringBuffer buf = new StringBuffer();
069: for (int i = 0; i < colDefs.length; ++i) {
070: buf.append(format(colDefs[i].getLabel(), colDefs[i]
071: .getDisplayWidth(), ' '));
072: }
073: addLine(buf.toString());
074: buf = new StringBuffer();
075: for (int i = 0; i < colDefs.length; ++i) {
076: buf
077: .append(format("",
078: colDefs[i].getDisplayWidth(), '-'));
079: }
080: addLine(buf.toString());
081: }
082: }
083:
084: protected void addRow(Object[] row) {
085: _rowCount++;
086: ColumnDisplayDefinition[] colDefs = getColumnDefinitions();
087: StringBuffer buf = new StringBuffer();
088: for (int i = 0; i < row.length; ++i) {
089: String cellValue = CellComponentFactory.renderObject(
090: row[i], colDefs[i]);
091: buf.append(format(cellValue, colDefs[i].getDisplayWidth(),
092: ' '));
093: }
094: addLine(buf.toString());
095: }
096:
097: public void moveToTop() {
098: _outText.select(0, 0);
099: }
100:
101: /*
102: * @see BaseDataSetViewerDestination#allRowsAdded()
103: */
104: protected void allRowsAdded() {
105: }
106:
107: /**
108: * Get the component for this viewer.
109: *
110: * @return The component for this viewer.
111: */
112: public Component getComponent() {
113: return _outText;
114: }
115:
116: /*
117: * @see IDataSetViewer#getRowCount()
118: */
119: public int getRowCount() {
120: return _rowCount;
121: }
122:
123: protected void addLine(String line) {
124: _outText.append(line);
125: _outText.append("\n");
126: }
127:
128: protected String format(String data, int displaySize, char fillChar) {
129: data = data.replace('\n', ' ');
130: data = data.replace('\r', ' ');
131: StringBuffer output = new StringBuffer(data);
132: if (displaySize > MAX_COLUMN_WIDTH) {
133: displaySize = MAX_COLUMN_WIDTH;
134: }
135:
136: if (output.length() > displaySize) {
137: output.setLength(displaySize);
138: }
139:
140: displaySize += COLUMN_PADDING;
141:
142: int extraPadding = displaySize - output.length();
143: if (extraPadding > 0) {
144: char[] padData = new char[extraPadding];
145: Arrays.fill(padData, fillChar);
146: output.append(padData);
147: }
148:
149: return output.toString();
150: }
151:
152: private final class MyJTextArea extends JTextArea {
153: private TextPopupMenu _textPopupMenu;
154:
155: MyJTextArea(IDataSetUpdateableModel updateableObject) {
156: super ();
157: boolean allowUpdate = false;
158: if (updateableObject != null)
159: allowUpdate = true;
160: createUserInterface(allowUpdate, updateableObject);
161: }
162:
163: protected void createUserInterface(boolean allowUpdate,
164: IDataSetUpdateableModel updateableObject) {
165: setEditable(false);
166: setLineWrap(false);
167: setFont(new Font("Monospaced", Font.PLAIN, 12));
168:
169: _textPopupMenu = new MyJTextAreaPopupMenu(allowUpdate,
170: updateableObject);
171: _textPopupMenu.setTextComponent(this );
172:
173: addMouseListener(new MouseAdapter() {
174: public void mousePressed(MouseEvent evt) {
175: if (evt.isPopupTrigger()) {
176: MyJTextArea.this .displayPopupMenu(evt);
177: }
178: }
179:
180: public void mouseReleased(MouseEvent evt) {
181: if (evt.isPopupTrigger()) {
182: MyJTextArea.this .displayPopupMenu(evt);
183: }
184: }
185: });
186:
187: }
188:
189: void displayPopupMenu(MouseEvent evt) {
190: _textPopupMenu.show(evt.getComponent(), evt.getX(), evt
191: .getY());
192: }
193: }
194:
195: private static class MyJTextAreaPopupMenu extends TextPopupMenu {
196: private MakeEditableAction _makeEditable = new MakeEditableAction();
197:
198: // The following pointer is needed to allow the "Make Editable button
199: // to tell the application to set up an editable display panel
200: private IDataSetUpdateableModel _updateableModel = null;
201:
202: MyJTextAreaPopupMenu(boolean allowUpdate,
203: IDataSetUpdateableModel updateableObject) {
204: super ();
205: // save the pointer needed to enable editing of data on-demand
206: _updateableModel = updateableObject;
207:
208: if (allowUpdate) {
209: addSeparator();
210: add(_makeEditable);
211: addSeparator();
212: }
213: }
214:
215: private class MakeEditableAction extends BaseAction {
216: MakeEditableAction() {
217: // i18n[dataSetViewerTablePanel.makeEditable=Make Editable]
218: super (
219: s_stringMgr
220: .getString("dataSetViewerTablePanel.makeEditable"));
221: }
222:
223: public void actionPerformed(ActionEvent evt) {
224: if (_updateableModel != null) {
225: new MakeEditableCommand(_updateableModel).execute();
226: }
227: }
228: }
229: }
230:
231: }
|