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.sql.framework.ui.view.validation;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Component;
045: import java.awt.FontMetrics;
046: import java.awt.Graphics;
047: import java.awt.event.MouseAdapter;
048: import java.awt.event.MouseEvent;
049: import java.net.URL;
050: import java.util.Collections;
051: import java.util.Iterator;
052: import java.util.List;
053: import javax.swing.ImageIcon;
054: import javax.swing.JLabel;
055: import javax.swing.JPanel;
056: import javax.swing.JScrollPane;
057: import javax.swing.JTable;
058: import javax.swing.event.ListSelectionEvent;
059: import javax.swing.event.ListSelectionListener;
060: import javax.swing.table.DefaultTableCellRenderer;
061: import javax.swing.table.TableColumn;
062: import net.java.hulp.i18n.Logger;
063: import org.netbeans.modules.etl.logger.Localizer;
064: import org.netbeans.modules.etl.logger.LogUtil;
065: import org.netbeans.modules.sql.framework.model.ValidationInfo;
066: import org.netbeans.modules.sql.framework.ui.graph.IGraphView;
067:
068: /**
069: * @author Ritesh Adval
070: */
071: public class ValidationTableView extends JPanel {
072:
073: private static URL warningImgUrl = ValidationTableView.class
074: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/Warning.png");
075: private static URL errorImgUrl = ValidationTableView.class
076: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/Error.png");
077: private static URL infoImgUrl = ValidationTableView.class
078: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/information.png");
079: private static ImageIcon errorImg;
080: private static ImageIcon warningImg;
081: private static ImageIcon infoImg;
082: private JTable table;
083: private IGraphView graphView;
084: private TableCellRenderer cellRenderer;
085: private String maxLengthStr = "THIS IS MAX LENGTH STRING";
086: private static transient final Logger mLogger = LogUtil
087: .getLogger(ValidationTableView.class.getName());
088: private static transient final Localizer mLoc = Localizer.get();
089: static {
090: errorImg = new ImageIcon(errorImgUrl);
091: warningImg = new ImageIcon(warningImgUrl);
092: infoImg = new ImageIcon(infoImgUrl);
093: }
094:
095: public ValidationTableView(IGraphView gView) {
096: this .graphView = gView;
097: initGui();
098: }
099:
100: private void initGui() {
101: this .setLayout(new BorderLayout());
102:
103: ValidationTableModel model = new ValidationTableModel(
104: Collections.EMPTY_LIST);
105:
106: table = new JTable(model);
107: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
108: table.getSelectionModel().addListSelectionListener(
109: new TableListSelectionListener());
110: table.getTableHeader().setReorderingAllowed(false);
111:
112: JScrollPane scrollPane = new JScrollPane();
113: scrollPane.setViewportView(table);
114:
115: //add scrollpane to this panel
116: this .add(scrollPane, BorderLayout.CENTER);
117:
118: //add table cell renderer
119: cellRenderer = new TableCellRenderer();
120: table.setDefaultRenderer(Integer.class, cellRenderer);
121: table.setDefaultRenderer(String.class, cellRenderer);
122:
123: //add mouse handler on table
124: table.addMouseListener(new TableMouseAdapter());
125: }
126:
127: public void setValidationInfos(List vInfos) {
128: ValidationTableModel model = new ValidationTableModel(vInfos);
129: table.setModel(model);
130:
131: // set icon column size
132: TableColumn column1 = table.getColumnModel().getColumn(0);
133: column1.setResizable(false);
134: column1.setMinWidth(30);
135: column1.setPreferredWidth(30);
136: column1.setMaxWidth(30);
137:
138: Iterator it = vInfos.iterator();
139:
140: int maxLength = 0;
141:
142: while (it.hasNext()) {
143: ValidationInfo vInfo = (ValidationInfo) it.next();
144: int newLength = vInfo.getDescription().length();
145:
146: if (newLength > maxLength) {
147: maxLength = newLength;
148: maxLengthStr = vInfo.getDescription();
149: }
150: }
151: }
152:
153: private void setDescriptionColumnWidth(int width) {
154: // set description column size
155: TableColumn column2 = table.getColumnModel().getColumn(1);
156: column2.setMinWidth(100);
157: column2.setPreferredWidth(width);
158: }
159:
160: public void clearView() {
161: ValidationTableModel model = new ValidationTableModel(
162: Collections.EMPTY_LIST);
163: table.setModel(model);
164: }
165:
166: class TableMouseAdapter extends MouseAdapter {
167:
168: /**
169: * Invoked when a mouse button has been pressed on a component.
170: */
171: @Override
172: public void mousePressed(MouseEvent e) {
173: int row = table.getSelectedRow();
174: ValidationTableModel model = (ValidationTableModel) table
175: .getModel();
176: ValidationInfo vInfo = model.getValidationInfo(row);
177: ValidationHandlerFactory factory = new ValidationHandlerFactory(
178: graphView);
179: ValidationHandler vHandler = factory
180: .getValidationHandler(vInfo);
181:
182: if (e.getClickCount() == 2) {
183: Object validatedObject = vInfo.getValidatedObject();
184: if (vHandler != null && validatedObject != null) {
185: vHandler.editValue(validatedObject);
186: }
187: }
188: }
189: }
190:
191: class TableCellRenderer extends DefaultTableCellRenderer {
192:
193: /**
194: * Returns the default table cell renderer.
195: *
196: * @param tbl the <code>JTable</code>
197: * @param value the value to assign to the cell at <code>[row, column]</code>
198: * @param isSelected true if cell is selected
199: * @param isFocus true if cell has focus
200: * @param row the row of the cell to render
201: * @param column the column of the cell to render
202: * @return the default table cell renderer
203: */
204: @Override
205: public Component getTableCellRendererComponent(JTable tbl,
206: Object value, boolean isSelected, boolean hasFocus,
207: int row, int column) {
208: JLabel label = null;
209:
210: if (value instanceof Integer) {
211: int type = ((Integer) value).intValue();
212: if (type == ValidationInfo.VALIDATION_WARNING) {
213: label = new JLabel(warningImg);
214: label.setDisplayedMnemonic('W');
215: String nbBundle1 = mLoc.t("PRSR001: Warning");
216: label.setToolTipText(Localizer.parse(nbBundle1));
217: label.getAccessibleContext().setAccessibleName(
218: Localizer.parse(nbBundle1));
219: } else if (type == ValidationInfo.VALIDATION_INFO) {
220: label = new JLabel(infoImg);
221: label.setDisplayedMnemonic('I');
222: String nbBundle2 = mLoc.t("PRSR001: Information");
223: label.getAccessibleContext().setAccessibleName(
224: Localizer.parse(nbBundle2));
225: label.setToolTipText(Localizer.parse(nbBundle2));
226: } else {
227: label = new JLabel(errorImg);
228: label.setDisplayedMnemonic('E');
229: String nbBundle3 = mLoc.t("PRSR001: Error");
230: label.setToolTipText(Localizer.parse(nbBundle3));
231: label.getAccessibleContext().setAccessibleName(
232: Localizer.parse(nbBundle3));
233: }
234: return label;
235: }
236: label = (JLabel) super .getTableCellRendererComponent(tbl,
237: value, isSelected, hasFocus, row, column);
238: label.setToolTipText(value.toString());
239: return label;
240: }
241:
242: @Override
243: public void paint(Graphics g) {
244: FontMetrics fm = g.getFontMetrics();
245: if (fm != null) {
246: setDescriptionColumnWidth(fm.stringWidth(maxLengthStr) + 10);
247: }
248:
249: super .paint(g);
250: }
251: }
252:
253: class TableListSelectionListener implements ListSelectionListener {
254:
255: /*
256: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
257: */
258: public void valueChanged(ListSelectionEvent e) {
259: int row = table.getSelectedRow();
260: if (row != -1) {
261: ValidationTableModel model = (ValidationTableModel) table
262: .getModel();
263: ValidationInfo vInfo = model.getValidationInfo(row);
264: ValidationHandlerFactory factory = new ValidationHandlerFactory(
265: graphView);
266: factory.higlightInvalidNode(vInfo);
267: }
268: }
269: }
270: }
|