001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Mikael MARCHE, Fayçal SOUGRATI, Vincent PAUTRET
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.ihm.models;
025:
026: import java.awt.Frame;
027: import java.awt.event.KeyEvent;
028: import java.awt.event.KeyListener;
029: import java.util.ArrayList;
030:
031: import javax.swing.JOptionPane;
032: import javax.swing.JScrollPane;
033: import javax.swing.JTable;
034: import javax.swing.ListSelectionModel;
035: import javax.swing.event.ListSelectionEvent;
036: import javax.swing.event.ListSelectionListener;
037: import javax.swing.event.TableModelEvent;
038: import javax.swing.event.TableModelListener;
039:
040: import org.objectweb.salome_tmf.ihm.languages.Language;
041:
042: /**
043: * @author teaml039
044: * @version : 0.1
045: */
046: public class ParameterJTableScrollPane extends JScrollPane {
047:
048: /**
049: * Le mod?le de liste d'ex?cutions
050: */
051: ParameterTableModel parametersTableModel;
052:
053: JTable parametersTable;
054:
055: ListSelectionModel rowSelectionModel;
056:
057: ListSelectionModel colSelectionModel;
058:
059: boolean addNewLine = false;
060:
061: boolean addNewParam = false;
062:
063: boolean wrongName = false;
064:
065: boolean otherKey = false;
066:
067: /**
068: *
069: * @param table
070: */
071: public ParameterJTableScrollPane(JTable table) {
072: super (table);
073: parametersTableModel = new ParameterTableModel();
074: parametersTable = table;
075: parametersTableModel.addColumnNameAndColumn(Language
076: .getInstance().getText("Nom"));
077: parametersTableModel.addColumnNameAndColumn(Language
078: .getInstance().getText("Valeur"));
079: parametersTableModel.addColumnNameAndColumn(Language
080: .getInstance().getText("Description"));
081:
082: parametersTable.setModel(parametersTableModel);
083: parametersTable
084: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
085:
086: parametersTable.setCellSelectionEnabled(true);
087: parametersTable.setColumnSelectionAllowed(true);
088: parametersTable.setRowSelectionAllowed(true);
089:
090: rowSelectionModel = parametersTable.getSelectionModel();
091: rowSelectionModel
092: .addListSelectionListener(new ListSelectionListener() {
093: public void valueChanged(ListSelectionEvent e) {
094: int rowIndex = e.getLastIndex();
095:
096: if (addNewParam) {
097: rowSelectionModel.setSelectionInterval(
098: rowIndex, rowIndex);
099: addNewParam = false;
100: }
101: if (wrongName) {
102: rowSelectionModel.setSelectionInterval(
103: rowIndex, rowIndex);
104: colSelectionModel
105: .setSelectionInterval(0, 0);
106: wrongName = false;
107: }
108:
109: }
110: });
111:
112: colSelectionModel = parametersTable.getColumnModel()
113: .getSelectionModel();
114: colSelectionModel
115: .addListSelectionListener(new ListSelectionListener() {
116: public void valueChanged(ListSelectionEvent e) {
117: if (addNewLine) {
118: colSelectionModel
119: .setSelectionInterval(0, 0);
120: addNewLine = false;
121: }
122: }
123: });
124: parametersTable.addKeyListener(new KeyListener() {
125: public void keyTyped(KeyEvent e) {
126:
127: }
128:
129: public void keyPressed(KeyEvent e) {
130:
131: if (e.getKeyCode() == KeyEvent.VK_ENTER
132: || e.getKeyCode() == KeyEvent.VK_TAB) {
133: if (parametersTable.getSelectedColumn() == 0
134: && parametersTableModel
135: .getValueAt(
136: parametersTable
137: .getSelectedRow(),
138: parametersTable
139: .getSelectedColumn()) == null
140: && !otherKey) {
141: wrongName = true;
142: otherKey = false;
143: }
144:
145: if (parametersTable.getSelectedColumn() == 1
146: && parametersTableModel
147: .getValueAt(
148: parametersTable
149: .getSelectedRow(),
150: parametersTable
151: .getSelectedColumn()) == null
152: && !otherKey
153: && parametersTableModel
154: .getValueAt(parametersTable
155: .getSelectedRow(), 0) != null
156: && !parametersTableModel
157: .getValueAt(
158: parametersTable
159: .getSelectedRow(),
160: 0).equals("")) {
161: parametersTableModel.setValueAt("",
162: parametersTable.getSelectedRow(),
163: parametersTable.getSelectedColumn());
164: otherKey = false;
165: }
166: if (parametersTable.getSelectedColumn() == 2
167: && parametersTableModel
168: .getValueAt(
169: parametersTable
170: .getSelectedRow(),
171: parametersTable
172: .getSelectedColumn()) == null
173: && !otherKey
174: && parametersTableModel
175: .getValueAt(parametersTable
176: .getSelectedRow(), 0) != null
177: && !parametersTableModel
178: .getValueAt(
179: parametersTable
180: .getSelectedRow(),
181: 0).equals("")) {
182: parametersTableModel.setValueAt("",
183: parametersTable.getSelectedRow(),
184: parametersTable.getSelectedColumn());
185: otherKey = false;
186: }
187: } else {
188: otherKey = true;
189: }
190: }
191:
192: public void keyReleased(KeyEvent e) {
193: }
194:
195: });
196:
197: parametersTableModel
198: .addTableModelListener(new TableModelListener() {
199: public void tableChanged(TableModelEvent e) {
200:
201: int row = e.getFirstRow();
202: int column = e.getColumn();
203: if (row != -1 && column != -1) {
204:
205: Object data = parametersTableModel
206: .getValueAt(row, column);
207: if (column == 0
208: && data instanceof String
209: && containsParamName((String) data,
210: row)) {
211: JOptionPane
212: .showMessageDialog(
213: new Frame(),
214: Language
215: .getInstance()
216: .getText(
217: "Ce_nom_de_paramètre_existe_déjà_!"),
218: Language
219: .getInstance()
220: .getText(
221: "Erreur_!"),
222: JOptionPane.ERROR_MESSAGE);
223: parametersTableModel.setValueAt(null,
224: row, column);
225: wrongName = true;
226: } else if (parametersTableModel.getValueAt(
227: row, 0) != null
228: && !parametersTableModel
229: .getValueAt(row, 0).equals(
230: "")) {
231: addNewParam = true;
232: otherKey = false;
233:
234: }
235:
236: if (column == 2
237: && parametersTableModel.getValueAt(
238: row, 0) != null
239: && !parametersTableModel
240: .getValueAt(row, 0).equals(
241: "")
242: && row == parametersTableModel
243: .getRowCount() - 1) {
244: newEntry();
245: addNewLine = true;
246: }
247: }
248: }
249: });
250: }
251:
252: /**
253: *
254: * @param paramName
255: * @param selectedRow
256: * @return
257: */
258: private boolean containsParamName(String paramName, int selectedRow) {
259: for (int i = 0; i < parametersTableModel.getRowCount(); i++) {
260: if (i != selectedRow
261: && parametersTableModel.getValueAt(i, 0) != null
262: && parametersTableModel.getValueAt(i, 0).equals(
263: paramName)) {
264: return true;
265: }
266: }
267: return false;
268: } // Fin de la m?thode containsParamName/2
269:
270: /**
271: *
272: *
273: */
274: public void newEntry() {
275: ArrayList dataList = new ArrayList();
276: dataList.add(null);
277: dataList.add(null);
278: dataList.add(null);
279: parametersTableModel.addRow(dataList);
280: } // Fin de la m?thode newEntry/0
281:
282: } // Fin de la classe ParameterJTable
|