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 Fayçal SOUGRATI, Vincent Pautret, Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.ihm.main;
025:
026: import java.awt.BorderLayout;
027: import java.awt.FlowLayout;
028: import java.awt.FontMetrics;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.util.ArrayList;
032: import java.util.Hashtable;
033: import java.util.Vector;
034:
035: import javax.swing.BorderFactory;
036: import javax.swing.JButton;
037: import javax.swing.JPanel;
038: import javax.swing.JScrollPane;
039: import javax.swing.JTable;
040: import javax.swing.ListSelectionModel;
041: import javax.swing.event.ListSelectionEvent;
042: import javax.swing.event.ListSelectionListener;
043: import javax.swing.event.TreeSelectionEvent;
044: import javax.swing.event.TreeSelectionListener;
045: import javax.swing.table.DefaultTableModel;
046: import javax.swing.tree.DefaultMutableTreeNode;
047:
048: import org.objectweb.salome_tmf.api.Util;
049: import org.objectweb.salome_tmf.api.data.UserWrapper;
050: import org.objectweb.salome_tmf.data.Campaign;
051: import org.objectweb.salome_tmf.data.Project;
052: import org.objectweb.salome_tmf.data.Test;
053: import org.objectweb.salome_tmf.data.User;
054: import org.objectweb.salome_tmf.ihm.languages.Language;
055: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
056: import org.objectweb.salome_tmf.ihm.models.DynamicTree;
057: import org.objectweb.salome_tmf.ihm.models.TableSorter;
058: import org.objectweb.salome_tmf.plugins.UICompCst;
059:
060: public class AssignedCamPanel extends JPanel implements
061: IAssignedCampAction, ListSelectionListener,
062: TreeSelectionListener, ActionListener {
063:
064: JTable assigendTable;
065: TableSorter sorter;
066: DynamicTree campaignDynamicTree;
067: Campaign pCamp = null;
068: Vector ligneModel = new Vector();
069: SimpleTableModel model;
070: JButton assignButton;
071: Hashtable userByID = null;
072: Vector userList = null;
073:
074: public AssignedCamPanel() {
075: super (new BorderLayout());
076: //initData();
077: initComponent();
078: }
079:
080: void initData() {
081: userByID = new Hashtable();
082: userList = new Vector();
083: Project current_project = DataModel.getCurrentProject();
084: try {
085: Vector users_wrapper = current_project.getAllUsersWrapper();
086: for (int i = 0; i < users_wrapper.size(); i++) {
087: User user = new User((UserWrapper) users_wrapper
088: .elementAt(i));
089: userList.add(user);
090: //System.out.println("-----------> Add user " + user.getNameFromModel() + " ID = " +user.getIdBdd() + " <------------------");
091: userByID.put(new Integer(user.getIdBdd()), user);
092: }
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096: }
097:
098: void initComponent() {
099: model = new SimpleTableModel();
100: model.addColumn(Language.getInstance().getText("Utilisateurs"));
101: model.addColumn(Language.getInstance().getText("Familles"));
102: model.addColumn(Language.getInstance().getText("Suites"));
103: model.addColumn(Language.getInstance().getText("Tests"));
104: sorter = new TableSorter(model);
105:
106: assigendTable = new JTable(sorter);
107:
108: ListSelectionModel rowSM = assigendTable.getSelectionModel();
109: rowSM.addListSelectionListener(this );
110: setColumnSize(assigendTable);
111: sorter.setTableHeader(assigendTable.getTableHeader());
112:
113: assigendTable
114: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
115: JScrollPane tablePane = new JScrollPane(assigendTable);
116: tablePane.setBorder(BorderFactory.createRaisedBevelBorder());
117:
118: add(tablePane, BorderLayout.CENTER);
119:
120: JPanel panelButton = new JPanel(new FlowLayout());
121: assignButton = new JButton(Language.getInstance().getText(
122: "Assign_to"));
123: assignButton.setEnabled(false);
124: assignButton.addActionListener(this );
125: panelButton.add(assignButton);
126: add(panelButton, BorderLayout.SOUTH);
127:
128: campaignDynamicTree = (DynamicTree) SalomeTMFContext
129: .getInstance().getUIComponent(
130: UICompCst.CAMPAIGN_DYNAMIC_TREE);
131: campaignDynamicTree.getTree().addTreeSelectionListener(this );
132: DataModel.setAssignedCampAction(this );
133: }
134:
135: public void updateData(Campaign pCamp) {
136: try {
137: boolean isSorting = sorter.isSorting();
138: int statusSort_col0 = -1;
139: int statusSort_col1 = -1;
140: int statusSort_col2 = -1;
141: int statusSort_col3 = -1;
142: if (isSorting) {
143: statusSort_col0 = sorter.getSortingStatus(0);
144: statusSort_col1 = sorter.getSortingStatus(1);
145: statusSort_col2 = sorter.getSortingStatus(2);
146: statusSort_col3 = sorter.getSortingStatus(3);
147: }
148: model.clearTable();
149: ligneModel.clear();
150: ArrayList testList = pCamp.getTestListFromModel();
151: for (int i = 0; i < testList.size(); i++) {
152: Test pTest = (Test) testList.get(i);
153: ligneModel.add(pTest);
154: Vector data = new Vector();
155: int assigendID = pCamp.getAssignedUserID(pTest);
156: User pUser = (User) userByID
157: .get(new Integer(assigendID));
158: System.out.println("User assigned is = " + pUser
159: + " , assigendID = " + assigendID);
160: data.add(((User) userByID.get(new Integer(assigendID)))
161: .getLoginFromModel()); // TO CHANGE
162: data.add(pTest.getTestListFromModel()
163: .getFamilyFromModel().getNameFromModel());
164: data.add(pTest.getTestListFromModel()
165: .getNameFromModel());
166: data.add(pTest.getNameFromModel());
167:
168: model.insertRow(i, data);
169: }
170: setColumnSize(assigendTable);
171: if (isSorting) {
172: sorter.setSortingStatus(0, statusSort_col0);
173: sorter.setSortingStatus(1, statusSort_col1);
174: sorter.setSortingStatus(2, statusSort_col2);
175: sorter.setSortingStatus(3, statusSort_col3);
176: }
177: } catch (Exception e) {
178: Util
179: .log("[AssignedCamPanel->updateData] error on loading assigned tester (Data come from old version of Salome ?)");
180: e.printStackTrace();
181: }
182: }
183:
184: /**
185: * redefine size of table columns
186: */
187: public void setColumnSize(JTable table) {
188: FontMetrics fm = table.getFontMetrics(table.getFont());
189: for (int i = 0; i < table.getColumnCount(); i++) {
190: int max = 0;
191: for (int j = 0; j < table.getRowCount(); j++) {
192: int taille = fm.stringWidth((String) table.getValueAt(
193: j, i));
194: if (taille > max)
195: max = taille;
196: }
197: String nom = (String) table.getColumnModel().getColumn(i)
198: .getIdentifier();
199: int taille = fm.stringWidth(nom);
200: if (taille > max)
201: max = taille;
202: table.getColumnModel().getColumn(i).setPreferredWidth(
203: max + 10);
204: }
205: }
206:
207: /*********************** Interfaces ***************************/
208: /********************** ListSelectionListener ***************************/
209: public void valueChanged(ListSelectionEvent e) {
210: if (e.getSource().equals(assigendTable.getSelectionModel())) {
211: assigendTableValueChanged(e);
212: }
213: }
214:
215: void assigendTableValueChanged(ListSelectionEvent e) {
216:
217: if (e.getValueIsAdjusting())
218: return;
219:
220: int nbOfSelectedRows = assigendTable.getSelectedRowCount();
221: int selectedRow = assigendTable.getSelectedRow();
222: if (selectedRow != -1) {
223: assignButton.setEnabled(true);
224: if (nbOfSelectedRows != 1) {
225: /* Multi Selection */
226:
227: } else {
228: /* Mono Selection */
229:
230: }
231: } else {
232: /* Pas de Selection */
233: assignButton.setEnabled(false);
234: }
235:
236: }
237:
238: /********************** TreeSelectionListener ***************************/
239: public void valueChanged(TreeSelectionEvent e) {
240: if (userByID == null) {
241: initData();
242: }
243: DefaultMutableTreeNode node = (DefaultMutableTreeNode) campaignDynamicTree
244: .getTree().getLastSelectedPathComponent();
245: Campaign pCampTmp = getSelectedCamp(node);
246: if (pCampTmp == null) {
247: return;
248: }
249: if (pCamp == null) {
250: pCamp = pCampTmp;
251: updateData(pCamp);
252: } else if (pCamp.getIdBdd() != pCampTmp.getIdBdd()) {
253: pCamp = pCampTmp;
254: updateData(pCamp);
255: }
256: }
257:
258: Campaign getSelectedCamp(DefaultMutableTreeNode node) {
259: Campaign pCamp = null;
260:
261: if (node == null) {
262: return null;
263: }
264: if (node.getUserObject() instanceof Campaign) {
265: pCamp = (Campaign) node.getUserObject();
266: }
267: return pCamp;
268: }
269:
270: /********************************** Implements ActionListener *********************************/
271:
272: public void actionPerformed(ActionEvent e) {
273: if (e.getSource().equals(assignButton)) {
274: assignPerformed();
275: }
276: }
277:
278: void assignPerformed() {
279: AskChooseAssignedUser pAskChooseAssignedUser = new AskChooseAssignedUser(
280: userList, false);
281: pAskChooseAssignedUser.setVisible(true);
282: User selectedUser = pAskChooseAssignedUser.getSelectedUser();
283: System.out.println("Selected User is " + selectedUser);
284: if (selectedUser != null) {
285: assign(selectedUser);
286: }
287: }
288:
289: void assign(User selectedUser) {
290: int[] selectedRows = assigendTable.getSelectedRows();
291: boolean doUpdateData = true;
292: for (int i = 0; i < selectedRows.length; i++) {
293: try {
294: int modeIndex = sorter.modelIndex(selectedRows[i]);
295: Test pTest = (Test) ligneModel.elementAt(modeIndex);
296: System.out.println("Change assignation for "
297: + pTest.getNameFromModel() + " with user "
298: + selectedUser + " in camp "
299: + pCamp.getNameFromModel());
300: pCamp.updateTestAssignationInDBAndModel(pTest
301: .getIdBdd(), selectedUser.getIdBdd());
302: //assigendTable.setValueAt(selectedUser.getLoginFromModel(),modeIndex ,0);
303: if (selectedRows.length == 1) {
304: sorter.setValueAt(selectedUser.getLoginFromModel(),
305: selectedRows[i], 0);
306: doUpdateData = false;
307: }
308: } catch (Exception e) {
309: e.printStackTrace();
310: }
311: }
312: if (doUpdateData) {
313: updateData(pCamp);
314: }
315:
316: }
317:
318: class SimpleTableModel extends DefaultTableModel {
319: public boolean isCellEditable(int row, int col) {
320: return false;
321: } // Fin de la m?thode isCellEditable/2
322:
323: public void clearTable() {
324: int nbRow = getRowCount();
325: for (int i = 0; i < nbRow; i++) {
326: removeRow(0);
327: }
328: fireTableStructureChanged();
329: }
330: }
331:
332: }
|