001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mashup.db.ui;
042:
043: import java.awt.FlowLayout;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.net.URL;
049: import java.sql.Connection;
050: import java.sql.ResultSet;
051: import java.sql.SQLException;
052: import java.sql.Statement;
053:
054: import javax.swing.BorderFactory;
055: import javax.swing.BoxLayout;
056: import javax.swing.Icon;
057: import javax.swing.ImageIcon;
058: import javax.swing.JButton;
059: import javax.swing.JLabel;
060: import javax.swing.JPanel;
061: import javax.swing.JTextField;
062:
063: import org.netbeans.modules.mashup.db.model.FlatfileDefinition;
064: import org.netbeans.modules.mashup.db.ui.model.FlatfileTable;
065: import org.netbeans.modules.sql.framework.ui.SwingWorker;
066: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
067: import org.netbeans.modules.sql.framework.ui.output.dataview.ResultSetTablePanel;
068: import org.openide.DialogDisplayer;
069:
070: import org.openide.NotifyDescriptor;
071: import org.openide.NotifyDescriptor.Message;
072: import org.openide.util.NbBundle;
073: import org.openide.util.Utilities;
074: import net.java.hulp.i18n.Logger;
075: import org.netbeans.modules.etl.logger.Localizer;
076: import org.netbeans.modules.etl.logger.LogUtil;
077:
078: /**
079: * @author Ahimanikya Satapathy
080: * @version $Revision$
081: */
082: public class FlatfileResulSetPanel extends JPanel implements
083: ActionListener, PropertyChangeListener {
084:
085: private static transient final Logger mLogger = LogUtil
086: .getLogger(FlatfileResulSetPanel.class.getName());
087: private static transient final Localizer mLoc = Localizer.get();
088:
089: private class FlatfileTableQuery {
090:
091: public FlatfileTableQuery() {
092: }
093:
094: public void generateResult(final FlatfileNode node) {
095: String title = "Loading flat file table";
096: String msg = "Loading data from flat file table: "
097: + node.getName();
098: UIUtil.startProgressDialog(title, msg);
099: generateData(node);
100: }
101:
102: private void generateData(FlatfileNode node) {
103: showDataBtn.setEnabled(false);
104: recordCount.setEnabled(false);
105: DataViewWorkerThread queryThread = new DataViewWorkerThread(
106: node);
107: queryThread.start();
108: }
109: }
110:
111: private class DataViewWorkerThread extends SwingWorker {
112:
113: FlatfileNode node;
114: private ResultSet cntRs;
115: private Connection conn;
116: private Throwable ex;
117: private ResultSet rs;
118: private Statement stmt;
119:
120: public DataViewWorkerThread(FlatfileNode newNode) {
121: node = newNode;
122: }
123:
124: public Object construct() {
125:
126: int ct = 25;
127:
128: try {
129: ct = Integer.parseInt(recordCount.getText());
130: } catch (NumberFormatException nfe) {
131: recordCount.setText(String.valueOf(25));
132: }
133:
134: try {
135: Object obj = node.getUserObject();
136: if (obj instanceof FlatfileTable) {
137: FlatfileTable table = (FlatfileTable) obj;
138: conn = db.getJDBCConnection();
139: stmt = conn.createStatement();
140: String selectSQL = table.getSelectStatementSQL(ct);
141: mLogger
142: .infoNoloc(mLoc
143: .t(
144: "PRSR084: FlatfileResulSetPanel.class.getName(){0}",
145: selectSQL));
146: rs = stmt.executeQuery(selectSQL);
147: recordViewer.clearView();
148: recordViewer.setResultSet(rs);
149:
150: // get the count of all rows
151: String countSql = "Select count(*) From "
152: + table.getName();
153: mLogger
154: .infoNoloc(mLoc
155: .t(
156: "PRSR085: Select count(*) statement used for total rows: {0}",
157: countSql));
158: stmt = conn.createStatement();
159: cntRs = stmt.executeQuery(countSql);
160:
161: // set the count
162: if (cntRs == null) {
163: totalRowsLabel.setText("");
164: } else {
165: if (cntRs.next()) {
166: int count = cntRs.getInt(1);
167: totalRowsLabel.setText(String
168: .valueOf(count));
169: }
170: }
171:
172: } else {
173: totalRowsLabel.setText("");
174: recordViewer.clearView();
175: }
176:
177: } catch (Exception e) {
178: this .ex = e;
179: mLogger.errorNoloc(mLoc.t(
180: "PRSR086: Can't get contents for table:{0}",
181: FlatfileResulSetPanel.class.getName()), e);
182: recordViewer.clearView();
183: totalRowsLabel.setText("0");
184: }
185:
186: return "";
187: }
188:
189: // Runs on the event-dispatching thread.
190: public void finished() {
191: try {
192: if (this .ex != null) {
193: String nbBundle3 = mLoc
194: .t(
195: "PRSR001: Error fetching data for tableCause: {0}",
196: this .ex.getMessage());
197: String errorMsg = Localizer.parse(nbBundle3); // NOI18N
198: DialogDisplayer.getDefault().notify(
199: new Message(errorMsg,
200: NotifyDescriptor.ERROR_MESSAGE));
201: }
202:
203: showDataBtn.setEnabled(true);
204: recordCount.setEnabled(true);
205:
206: if (stmt != null) {
207: stmt.close();
208: }
209:
210: } catch (SQLException sqle) {
211: mLogger
212: .errorNoloc(
213: mLoc
214: .t(
215: "PRSR087: Could not close statement after retrieving table contents {0}",
216: FlatfileResulSetPanel.class
217: .getName()),
218: sqle);
219: } finally {
220: if (conn != null) {
221: try {
222: conn.close();
223: } catch (SQLException e) {
224: }
225: conn = null;
226: }
227: UIUtil.stopProgressDialog();
228: }
229: }
230: }
231:
232: private static final String CMD_SHOW_DATA = "Show Data"; // NOI18N
233:
234: public static Icon getDbIcon() {
235: Icon icon = null;
236: try {
237: icon = new ImageIcon(
238: Utilities
239: .loadImage("org/netbeans/modules/mashup/db/ui/resource/images/root.png"));
240: } catch (Exception ex) {
241: // Log exception
242: }
243: return icon;
244: }
245:
246: private FlatfileDefinition db = null;
247: private JTextField recordCount;
248: private ResultSetTablePanel recordViewer;
249: private JButton showDataBtn;
250: private JLabel totalRowsLabel = null;
251: private FlatfileTreeTableView treeView = null;
252:
253: public FlatfileResulSetPanel(FlatfileDefinition dbInstance) {
254: super ();
255: db = dbInstance;
256:
257: this .setBorder(BorderFactory
258: .createTitledBorder("Selected Table Content"));
259: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
260: this .add(createControlPanel());
261:
262: recordViewer = new ResultSetTablePanel();
263: this .add(recordViewer);
264: }
265:
266: /**
267: * Invoked when an action occurs.
268: *
269: * @param e ActionEvent to handle
270: */
271: public void actionPerformed(ActionEvent e) {
272: Object src = e.getSource();
273:
274: if (src == recordCount) {
275: showDataBtn.requestFocusInWindow();
276: showDataBtn.doClick();
277: } else if (src == showDataBtn) {
278: new FlatfileTableQuery()
279: .generateResult((FlatfileNode) treeView
280: .getCurrentNode());
281: }
282: }
283:
284: public void propertyChange(PropertyChangeEvent event) {
285: if (event.getSource() == treeView) {
286: new FlatfileTableQuery()
287: .generateResult((FlatfileNode) event.getNewValue());
288: }
289: }
290:
291: public void addActionListener(ActionListener listener) {
292: showDataBtn.addActionListener(listener);
293: recordCount.addActionListener(listener);
294: }
295:
296: public void generateResult(FlatfileNode node) {
297: new FlatfileTableQuery().generateResult(node);
298: }
299:
300: public void setTreeView(FlatfileTreeTableView view) {
301: this .treeView = view;
302: }
303:
304: /*
305: * Creates show data button and row count text field to control display of selected
306: * table.
307: */
308: private JPanel createControlPanel() {
309: JPanel controlPanel = new JPanel(new FlowLayout(
310: FlowLayout.LEADING));
311:
312: // add refresh button
313: URL url = getClass()
314: .getResource(
315: "/org/netbeans/modules/sql/framework/ui/resources/images/refresh16.png");
316: showDataBtn = new JButton(new ImageIcon(url));
317: String nbBundle30 = mLoc
318: .t("PRSR001: Show data for selected flat file table node");
319: showDataBtn.getAccessibleContext().setAccessibleName(
320: Localizer.parse(nbBundle30));
321: showDataBtn.setToolTipText(Localizer.parse(nbBundle30));
322: showDataBtn.setMnemonic(Localizer.parse(nbBundle30).charAt(0));
323: showDataBtn.setActionCommand(CMD_SHOW_DATA);
324:
325: JPanel recordCountPanel = new JPanel();
326: recordCountPanel.setBorder(BorderFactory.createEmptyBorder(0,
327: 10, 0, 0));
328:
329: String nbBundle31 = mLoc.t("PRSR001: Limit number of rows");
330: JLabel lbl = new JLabel(Localizer.parse(nbBundle31));
331: lbl.getAccessibleContext().setAccessibleName(
332: Localizer.parse(nbBundle31));
333: lbl.setDisplayedMnemonic(Localizer.parse(nbBundle31).charAt(0));
334:
335: recordCountPanel.add(lbl);
336: recordCount = new JTextField("25", 5);
337: recordCountPanel.add(recordCount);
338: lbl.setLabelFor(recordCount);
339:
340: // add total row count label
341: JPanel totalRowsPanel = new JPanel();
342: FlowLayout fl = new FlowLayout();
343: fl.setAlignment(FlowLayout.LEFT);
344: totalRowsPanel.setLayout(fl);
345:
346: String nbBundle51 = mLoc.t("PRSR001: Total rows:");
347: JLabel totalRowsNameLabel = new JLabel(Localizer
348: .parse(nbBundle51));
349: totalRowsNameLabel.getAccessibleContext().setAccessibleName(
350: Localizer.parse(nbBundle51));
351: totalRowsNameLabel.setBorder(BorderFactory.createEmptyBorder(0,
352: 100, 0, 8));
353: totalRowsPanel.add(totalRowsNameLabel);
354:
355: totalRowsLabel = new JLabel();
356: totalRowsPanel.add(totalRowsLabel);
357:
358: controlPanel.add(showDataBtn);
359: controlPanel.add(recordCountPanel);
360: controlPanel.add(totalRowsPanel);
361:
362: this.addActionListener(this);
363:
364: return controlPanel;
365: }
366: }
|