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 Aurore PENAULT
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023: package salomeTMF_plug.docXML.common;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Dimension;
027: import java.awt.Rectangle;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.util.ArrayList;
031:
032: import javax.swing.BorderFactory;
033: import javax.swing.Box;
034: import javax.swing.BoxLayout;
035: import javax.swing.JButton;
036: import javax.swing.JDialog;
037: import javax.swing.JPanel;
038: import javax.swing.JScrollPane;
039: import javax.swing.JTree;
040: import javax.swing.tree.DefaultMutableTreeNode;
041: import javax.swing.tree.DefaultTreeModel;
042: import javax.swing.tree.TreePath;
043:
044: import org.objectweb.salome_tmf.ihm.models.TreeRenderer;
045: import org.objectweb.salome_tmf.ihm.tools.Tools;
046:
047: import salomeTMF_plug.docXML.languages.Language;
048:
049: /**
050: * Classe qui construit la fenêtre permettant de choisir les chapitres à insérer
051: * dans la documentation
052: * @author vapu8214
053: */
054: public class ChapterChooser extends JDialog {
055:
056: private DefaultMutableTreeNode chosenRootNode;
057: private DefaultMutableTreeNode chapterRootNode;
058: private JTree chosenTree;
059: private JTree chapterTree;
060: private DefaultTreeModel chosenTreeModel;
061:
062: private ArrayList<String> chosenNodes = new ArrayList<String>();
063: private ArrayList<String> allChaptersList;
064:
065: private String chapitres = Language.getInstance().getText(
066: "Chapitres");
067:
068: private GenDocDialog parentDialog;
069:
070: /**
071: * Permet de sélectionner les chapitres qui apparaîtront dans le rapport
072: * @param dialog
073: * @param initChapterSelection true si la liste des chapitres a déjà été initialisée
074: * @param allTitlesList liste des extensions dont on peut choisir de les ajouter ou de les retirer du rapport
075: */
076: public ChapterChooser(GenDocDialog dialog,
077: boolean initChapterSelection,
078: ArrayList<String> allTitlesList) {
079: super (dialog, true);
080:
081: parentDialog = dialog;
082: allChaptersList = allTitlesList;
083:
084: TreeRenderer chosenRenderer = new TreeRenderer();
085: TreeRenderer chapterRenderer = new TreeRenderer();
086:
087: chosenRootNode = new DefaultMutableTreeNode(Language
088: .getInstance().getText("Chapitres"));
089: if (initChapterSelection) {
090: chosenNodes = parentDialog.getChosenChaptersList();
091: ArrayList<String> newChosenNodes = new ArrayList<String>();
092: for (String chapterTitle : newChosenNodes) {
093: if (allChaptersList.contains(chapterTitle)) {
094: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
095: chapterTitle);
096: chosenRootNode.add(node);
097: newChosenNodes.add(chapterTitle);
098: }
099: }
100: chosenNodes = newChosenNodes;
101: } else {
102: for (String chapterTitle : allChaptersList) {
103: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
104: chapterTitle);
105: chosenRootNode.add(node);
106: chosenNodes.add(chapterTitle);
107: }
108: }
109: chapterRootNode = new DefaultMutableTreeNode(chapitres);
110: for (String chapterTitle : allChaptersList) {
111: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
112: chapterTitle);
113: chapterRootNode.add(node);
114: }
115:
116: chosenTree = new JTree();
117: chosenTree.setCellRenderer(chosenRenderer);
118: chosenTreeModel = new DefaultTreeModel(chosenRootNode);
119: chosenTree.setModel(chosenTreeModel);
120:
121: chapterTree = new JTree();
122: chapterTree.setCellRenderer(chapterRenderer);
123: chapterTree.setModel(new DefaultTreeModel(chapterRootNode));
124:
125: JButton addButton = new JButton(">");
126: addButton.setToolTipText(Language.getInstance().getText(
127: "Ajouter_à_la_sélection"));
128: addButton.addActionListener(new ActionListener() {
129: public void actionPerformed(ActionEvent e) {
130: TreePath[] pathTab = chapterTree.getSelectionPaths();
131: if (pathTab != null) {
132: for (int i = 0; i < pathTab.length; i++) {
133: String nodeValue = (String) ((DefaultMutableTreeNode) pathTab[i]
134: .getLastPathComponent())
135: .getUserObject();
136: if (!chosenNodes.contains(nodeValue)) {
137: int index = getIndexFor(nodeValue);
138: chosenNodes.add(index, nodeValue);
139: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
140: nodeValue);
141: chosenTreeModel.insertNodeInto(newNode,
142: chosenRootNode, index);
143: chosenTree
144: .scrollPathToVisible(new TreePath(
145: newNode.getPath()));
146: }
147: }
148: }
149: }
150: });
151:
152: JButton removeButton = new JButton("<");
153: removeButton.setToolTipText(Language.getInstance().getText(
154: "Retirer_de_la_sélection"));
155: removeButton.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent e) {
157: TreePath[] pathTab = chosenTree.getSelectionPaths();
158: if (pathTab != null) {
159: for (int i = 0; i < pathTab.length; i++) {
160: DefaultMutableTreeNode nodeToRemove = (DefaultMutableTreeNode) pathTab[i]
161: .getLastPathComponent();
162: String nodeValue = (String) nodeToRemove
163: .getUserObject();
164: chosenTreeModel
165: .removeNodeFromParent(nodeToRemove);
166: chosenNodes.remove(nodeValue);
167: }
168: }
169:
170: }
171: });
172:
173: JPanel buttonSet = new JPanel();
174: buttonSet.setLayout(new BoxLayout(buttonSet, BoxLayout.Y_AXIS));
175: buttonSet.add(addButton);
176: buttonSet.add(Box.createRigidArea(new Dimension(1, 25)));
177: buttonSet.add(removeButton);
178:
179: JScrollPane chosenScrollPane = new JScrollPane(chosenTree);
180: chosenScrollPane.setBorder(BorderFactory
181: .createTitledBorder(Language.getInstance().getText(
182: "Sélection")));
183: chosenScrollPane.setPreferredSize(new Dimension(300, 450));
184:
185: JScrollPane chapterScrollPane = new JScrollPane(chapterTree);
186: chapterScrollPane.setBorder(BorderFactory
187: .createTitledBorder(Language.getInstance().getText(
188: "Document")));
189: chapterScrollPane.setPreferredSize(new Dimension(300, 450));
190:
191: JPanel windowPanel = new JPanel();
192: windowPanel.setLayout(new BoxLayout(windowPanel,
193: BoxLayout.X_AXIS));
194: windowPanel.add(chapterScrollPane);
195: windowPanel.add(Box.createRigidArea(new Dimension(20, 50)));
196: windowPanel.add(buttonSet);
197: windowPanel.add(Box.createRigidArea(new Dimension(20, 50)));
198: windowPanel.add(chosenScrollPane);
199:
200: JButton validate = new JButton(Language.getInstance().getText(
201: "Valider"));
202: validate.setToolTipText(Language.getInstance().getText(
203: "Valider"));
204: validate.addActionListener(new ActionListener() {
205: public void actionPerformed(ActionEvent e) {
206: try {
207: parentDialog.setChosenChaptersList(chosenNodes);
208: parentDialog.setInitChapterSelection(true);
209: ChapterChooser.this .dispose();
210: } catch (Exception ex) {
211: Tools.ihmExceptionView(ex);
212: }
213: }
214: });
215:
216: JButton cancel = new JButton(Language.getInstance().getText(
217: "Annuler"));
218: cancel
219: .setToolTipText(Language.getInstance().getText(
220: "Annuler"));
221: cancel.addActionListener(new ActionListener() {
222: public void actionPerformed(ActionEvent e) {
223: try {
224: ChapterChooser.this .dispose();
225: } catch (Exception ex) {
226: Tools.ihmExceptionView(ex);
227: }
228: }
229: });
230:
231: JPanel secondButtonSet = new JPanel();
232: secondButtonSet.add(validate);
233: secondButtonSet.add(cancel);
234:
235: JPanel center = new JPanel();
236: center.add(windowPanel);
237:
238: JPanel page = new JPanel();
239: page.setLayout(new BoxLayout(page, BoxLayout.Y_AXIS));
240: page.add(center);
241: page.add(secondButtonSet);
242:
243: getContentPane().add(page, BorderLayout.CENTER);
244: setTitle(Language.getInstance().getText(
245: "Ajouter_des_chapitres_à_la_sélection"));
246: //setLocation(300,200);
247: centerScreen();
248: }
249:
250: void centerScreen() {
251: Dimension dim = getToolkit().getScreenSize();
252: this .pack();
253: Rectangle abounds = getBounds();
254: setLocation((dim.width - abounds.width) / 2,
255: (dim.height - abounds.height) / 2);
256: this .setVisible(true);
257: requestFocus();
258: }
259:
260: protected int getIndexFor(String nodeValue) {
261: if (chosenNodes.size() == 0) {
262: return 0;
263: } else {
264: int allChapterIndex = allChaptersList.indexOf(nodeValue);
265: int i = 0;
266: while (i < chosenNodes.size()) {
267: String currentNode = (String) chosenNodes.get(i);
268: int currentNodeChapterIndex = allChaptersList
269: .indexOf(currentNode);
270: if (allChapterIndex < currentNodeChapterIndex) {
271: return i;
272: }
273: if (i == chosenNodes.size() - 1)
274: return i + 1;
275: i++;
276: }
277: }
278: return 0;
279: }
280:
281: }
|