001: /*
002: * SQLStateCodesDockedPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.gui.sqlstates;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Font;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.ArrayList;
029: import java.util.List;
030: import javax.swing.BorderFactory;
031: import javax.swing.JScrollPane;
032: import javax.swing.JTable;
033: import javax.swing.table.AbstractTableModel;
034: import javax.swing.table.TableColumnModel;
035: import org.executequery.Constants;
036: import org.executequery.GUIUtilities;
037: import org.executequery.gui.AbstractDockedTabActionPanel;
038: import org.executequery.gui.DefaultTable;
039:
040: /**
041: * Docked SQL State Codes panel.
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.3 $
045: * @date $Date: 2006/07/15 13:14:12 $
046: */
047: public class SQLStateCodesDockedPanel extends
048: AbstractDockedTabActionPanel {
049:
050: public static final String TITLE = "SQL State Codes";
051:
052: /** sql keywords */
053: private List<SQLStateCode> codes;
054:
055: /** the table display */
056: //private JTable table;
057: /** Creates a new instance of SQLStateCodesDockedPanel */
058: public SQLStateCodesDockedPanel() {
059: super (new BorderLayout());
060: init();
061: }
062:
063: private void init() {
064: loadStateCodes();
065:
066: JTable table = new DefaultTable(new StateCodesModel());
067: table.setFont(new Font("Dialog", Font.PLAIN,
068: Constants.DEFAULT_FONT_SIZE));
069: //table.setRowHeight(20);
070: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
071:
072: // init the cell renderer component
073: TableColumnModel tcm = table.getColumnModel();
074: for (int i = 0, n = tcm.getColumnCount(); i < n; i++) {
075: tcm.getColumn(i).setCellRenderer(
076: new SQLStateCodesCellRenderer());
077: }
078:
079: // size the columns
080: tcm.getColumn(0).setPreferredWidth(40);
081: tcm.getColumn(1).setPreferredWidth(55);
082: tcm.getColumn(2).setPreferredWidth(160);
083:
084: add(new JScrollPane(table));
085: setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
086: }
087:
088: private void loadStateCodes() {
089: String path = "org/executequery/sql-state.codes";
090: codes = new ArrayList<SQLStateCode>();
091:
092: InputStream input = null;
093: try {
094: ClassLoader cl = getClass().getClassLoader();
095:
096: if (cl != null) {
097: input = cl.getResourceAsStream(path);
098: } else {
099: input = ClassLoader.getSystemResourceAsStream(path);
100: }
101:
102: int i = 0;
103: StringBuffer buf = new StringBuffer();
104:
105: char PIPE = '|';
106: char NEW_LINE = '\n';
107:
108: int count = 0;
109:
110: String stateClass = null;
111: String stateSubclass = null;
112: String description = null;
113:
114: try {
115: while ((i = input.read()) != -1) {
116: char _char = (char) i;
117: if (_char == PIPE) {
118:
119: // only picking first 2 values -
120: // description picked up on line-feed
121: switch (count) {
122: case 0:
123: stateClass = buf.toString();
124: break;
125: case 1:
126: stateSubclass = buf.toString();
127: break;
128: }
129: count++;
130: buf.setLength(0);
131: } else if (_char == NEW_LINE) {
132: count = 0;
133: description = buf.toString();
134: codes.add(new SQLStateCode(stateClass,
135: stateSubclass, description));
136: buf.setLength(0);
137: } else {
138: buf.append(_char);
139: }
140: }
141: } catch (IOException e) {
142: GUIUtilities.displayExceptionErrorDialog(
143: "Error loading SQL State Codes:\n"
144: + e.getMessage(), e);
145: }
146:
147: } finally {
148: if (input != null) {
149: try {
150: input.close();
151: } catch (IOException e) {
152: }
153: }
154: }
155: }
156:
157: // ----------------------------------------
158: // DockedTabView Implementation
159: // ----------------------------------------
160:
161: public static final String MENU_ITEM_KEY = "viewSqlStateCodes";
162:
163: public static final String PROPERTY_KEY = "system.display.state-codes";
164:
165: /**
166: * Returns the display title for this view.
167: *
168: * @return the title displayed for this view
169: */
170: public String getTitle() {
171: return TITLE;
172: }
173:
174: /**
175: * Returns the name defining the property name for this docked tab view.
176: *
177: * @return the key
178: */
179: public String getPropertyKey() {
180: return PROPERTY_KEY;
181: }
182:
183: /**
184: * Returns the name defining the menu cache property
185: * for this docked tab view.
186: *
187: * @return the preferences key
188: */
189: public String getMenuItemKey() {
190: return MENU_ITEM_KEY;
191: }
192:
193: /**
194: * Indicates the panel is being removed from the pane
195: */
196: public boolean tabViewClosing() {
197: return true;
198: }
199:
200: /**
201: * Indicates the panel is being selected in the pane
202: */
203: public boolean tabViewSelected() {
204: return true;
205: }
206:
207: /**
208: * Indicates the panel is being selected in the pane
209: */
210: public boolean tabViewDeselected() {
211: return true;
212: }
213:
214: public String toString() {
215: return TITLE;
216: }
217:
218: /**
219: * State codes table model.
220: */
221: private class StateCodesModel extends AbstractTableModel {
222:
223: private String[] columnNames = { "Class", "Subclass",
224: "Description" };
225:
226: public StateCodesModel() {
227: }
228:
229: public int getColumnCount() {
230: return columnNames.length;
231: }
232:
233: public String getColumnName(int column) {
234: return columnNames[column];
235: }
236:
237: public int getRowCount() {
238: if (codes == null) {
239: return 0;
240: }
241: return codes.size();
242: }
243:
244: public Object getValueAt(int row, int col) {
245: return codes.get(row);
246: }
247:
248: public boolean isCellEditable(int row, int col) {
249: return false;
250: }
251:
252: public Class getColumnClass(int col) {
253: return String.class;
254: }
255:
256: } // class StateCodesModel
257:
258: }
|