Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SourcePathDialog |
|
| 2.8333333333333335;2.833 | ||||
SourcePathDialog$SelectionListener |
|
| 2.8333333333333335;2.833 |
1 | /* $Id: SourcePathDialog.java 17881 2010-01-12 21:09:28Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.ui; | |
40 | ||
41 | import java.awt.event.ActionEvent; | |
42 | import java.awt.event.ActionListener; | |
43 | import java.util.LinkedList; | |
44 | ||
45 | import javax.swing.JButton; | |
46 | import javax.swing.JOptionPane; | |
47 | import javax.swing.JScrollPane; | |
48 | import javax.swing.JTable; | |
49 | import javax.swing.ListSelectionModel; | |
50 | import javax.swing.event.ListSelectionListener; | |
51 | import javax.swing.table.TableColumn; | |
52 | ||
53 | import org.argouml.i18n.Translator; | |
54 | import org.argouml.util.ArgoDialog; | |
55 | ||
56 | /** | |
57 | * This dialog appears when selecting | |
58 | * <code>Generation -> Settings for Generate for Project...</code> | |
59 | * in the menu.<p> | |
60 | * | |
61 | * Provides support for setting a source path tagged value used in Java | |
62 | * round trip engineering. | |
63 | */ | |
64 | 0 | public class SourcePathDialog extends ArgoDialog implements ActionListener { |
65 | ||
66 | 0 | private SourcePathController srcPathCtrl = new SourcePathControllerImpl(); |
67 | ||
68 | 0 | private SourcePathTableModel srcPathTableModel = |
69 | srcPathCtrl.getSourcePathSettings(); | |
70 | ||
71 | private JTable srcPathTable; | |
72 | ||
73 | private JButton delButton; | |
74 | ||
75 | private ListSelectionModel rowSM; | |
76 | ||
77 | /** | |
78 | * The constructor. | |
79 | * | |
80 | */ | |
81 | public SourcePathDialog() { | |
82 | 0 | super( |
83 | Translator.localize("action.generate-code-for-project"), | |
84 | ArgoDialog.OK_CANCEL_OPTION, | |
85 | true); | |
86 | ||
87 | 0 | srcPathTable = new JTable(); |
88 | 0 | srcPathTable.setModel(srcPathTableModel); |
89 | 0 | srcPathTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); |
90 | // Hack: don't show first column, where the model element object is | |
91 | // placed. | |
92 | 0 | TableColumn elemCol = srcPathTable.getColumnModel().getColumn(0); |
93 | 0 | elemCol.setMinWidth(0); |
94 | 0 | elemCol.setMaxWidth(0); |
95 | ||
96 | 0 | delButton = new JButton(Translator.localize("button.delete")); |
97 | 0 | delButton.setEnabled(false); |
98 | 0 | addButton(delButton, 0); |
99 | ||
100 | 0 | rowSM = srcPathTable.getSelectionModel(); |
101 | 0 | rowSM.addListSelectionListener(new SelectionListener()); |
102 | 0 | delButton.addActionListener(this); |
103 | ||
104 | 0 | setContent(new JScrollPane(srcPathTable)); |
105 | 0 | } |
106 | ||
107 | //////////////////////////////////////////////////////////////// | |
108 | // event handlers | |
109 | ||
110 | /* | |
111 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) | |
112 | */ | |
113 | public void actionPerformed(ActionEvent e) { | |
114 | 0 | super.actionPerformed(e); |
115 | ||
116 | // OK Button ------------------------------------------ | |
117 | 0 | if (e.getSource() == getOkButton()) { |
118 | 0 | buttonOkActionPerformed(); |
119 | } | |
120 | // Delete Button | |
121 | 0 | if (e.getSource() == delButton) { |
122 | 0 | deleteSelectedSettings(); |
123 | } | |
124 | 0 | } |
125 | ||
126 | /** | |
127 | * The OK button is pressed. | |
128 | */ | |
129 | private void buttonOkActionPerformed() { | |
130 | 0 | srcPathCtrl.setSourcePath(srcPathTableModel); |
131 | 0 | } |
132 | ||
133 | /** | |
134 | * Retrieve the selected rows indexes. | |
135 | */ | |
136 | private int[] getSelectedIndexes() { | |
137 | 0 | int firstSelectedRow = rowSM.getMinSelectionIndex(); |
138 | 0 | int lastSelectedRow = rowSM.getMaxSelectionIndex(); |
139 | 0 | LinkedList selectedIndexesList = new LinkedList(); |
140 | 0 | int numSelectedRows = 0; |
141 | 0 | for (int i = firstSelectedRow; i <= lastSelectedRow; i++) { |
142 | 0 | if (rowSM.isSelectedIndex(i)) { |
143 | 0 | numSelectedRows++; |
144 | 0 | selectedIndexesList.add(Integer.valueOf(i)); |
145 | } | |
146 | } | |
147 | 0 | int[] indexes = new int[selectedIndexesList.size()]; |
148 | 0 | java.util.Iterator it = selectedIndexesList.iterator(); |
149 | 0 | for (int i = 0; i < indexes.length && it.hasNext(); i++) { |
150 | 0 | indexes[i] = ((Integer) it.next()).intValue(); |
151 | } | |
152 | 0 | return indexes; |
153 | } | |
154 | ||
155 | /** | |
156 | * Delete the source path settings of the selected table rows. | |
157 | */ | |
158 | private void deleteSelectedSettings() { | |
159 | // find selected rows and make a list of the model elements | |
160 | // that are selected | |
161 | 0 | int[] selectedIndexes = getSelectedIndexes(); |
162 | ||
163 | // confirm with the user that he wants to delete, presenting the | |
164 | // list of settings to delete | |
165 | 0 | StringBuffer msg = new StringBuffer(); |
166 | 0 | msg.append(Translator.localize("dialog.source-path-del.question")); |
167 | 0 | for (int i = 0; i < selectedIndexes.length; i++) { |
168 | 0 | msg.append("\n"); |
169 | 0 | msg.append(srcPathTableModel.getMEName(selectedIndexes[i])); |
170 | 0 | msg.append(" ("); |
171 | 0 | msg.append(srcPathTableModel.getMEType(selectedIndexes[i])); |
172 | 0 | msg.append(")"); |
173 | } | |
174 | ||
175 | 0 | int res = JOptionPane.showConfirmDialog(this, |
176 | msg.toString(), | |
177 | Translator.localize("dialog.title.source-path-del"), | |
178 | JOptionPane.OK_CANCEL_OPTION); | |
179 | ||
180 | 0 | if (res == JOptionPane.OK_OPTION) { |
181 | // procede with the deletion in the model | |
182 | 0 | int firstSel = rowSM.getMinSelectionIndex(); |
183 | 0 | for (int i = 0; i < selectedIndexes.length && firstSel != -1; i++) { |
184 | 0 | srcPathCtrl.deleteSourcePath(srcPathTableModel |
185 | .getModelElement(firstSel)); | |
186 | 0 | srcPathTableModel.removeRow(firstSel); |
187 | 0 | firstSel = rowSM.getMinSelectionIndex(); |
188 | } | |
189 | // disable the button since no row will be selected now | |
190 | 0 | delButton.setEnabled(false); |
191 | } | |
192 | 0 | } |
193 | ||
194 | /** | |
195 | * Class that listens to selection events. | |
196 | */ | |
197 | 0 | class SelectionListener implements ListSelectionListener { |
198 | public void valueChanged(javax.swing.event.ListSelectionEvent e) { | |
199 | 0 | if (!delButton.isEnabled()) { |
200 | 0 | delButton.setEnabled(true); |
201 | } | |
202 | 0 | } |
203 | } | |
204 | } /* end class SourcePathDialog */ |