gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.mapcontext.raster.swing / org.gvsig.fmap.mapcontext.raster.swing.impl / src / main / java / org / gvsig / fmap / mapcontext / raster / swing / impl / operations / OperationListPanelController.java @ 8799
History | View | Annotate | Download (11.2 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.mapcontext.raster.swing.impl.operations; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
import java.util.Iterator; |
28 |
import java.util.Locale; |
29 |
|
30 |
import javax.swing.JComponent; |
31 |
import javax.swing.ListSelectionModel; |
32 |
import javax.swing.event.ListSelectionEvent; |
33 |
import javax.swing.event.ListSelectionListener; |
34 |
import javax.swing.event.TableModelListener; |
35 |
import javax.swing.table.DefaultTableColumnModel; |
36 |
import javax.swing.table.TableColumn; |
37 |
import javax.swing.table.TableColumnModel; |
38 |
|
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
import org.gvsig.fmap.mapcontext.raster.swing.operations.OperationListPanel; |
43 |
import org.gvsig.raster.lib.buffer.api.operations.OperationList; |
44 |
import org.gvsig.raster.lib.buffer.api.operations.OperationListEntry; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.i18n.I18nManager; |
47 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
48 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
49 |
|
50 |
/**
|
51 |
* @author fdiaz
|
52 |
*
|
53 |
*/
|
54 |
public class OperationListPanelController extends OperationListPanelView implements OperationListPanel { |
55 |
|
56 |
/**
|
57 |
*
|
58 |
*/
|
59 |
private static final long serialVersionUID = 938628147696863910L; |
60 |
@SuppressWarnings("unused") |
61 |
private static final Logger LOG = LoggerFactory.getLogger(OperationListPanelController.class); |
62 |
|
63 |
/**
|
64 |
* @param factories
|
65 |
*/
|
66 |
@SuppressWarnings("unchecked") |
67 |
public OperationListPanelController() {
|
68 |
translate(); |
69 |
initializeComponents(); |
70 |
} |
71 |
|
72 |
private void initializeComponents() { |
73 |
// this.btnAdd.addActionListener(this);
|
74 |
this.btnAdd.setActionCommand(this.ADD_ACTION_COMMAND); |
75 |
this.btnUpdate.setActionCommand(this.UPDATE_ACTION_COMMAND); |
76 |
this.btnRemove.setActionCommand(this.REMOVE_ACTION_COMMAND); |
77 |
this.btnRemove.addActionListener(new ActionListener() { |
78 |
|
79 |
@Override
|
80 |
public void actionPerformed(ActionEvent e) { |
81 |
OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
82 |
int selectedRow = tblOperationList.getSelectedRow();
|
83 |
int index = selectedRow;
|
84 |
model.removeElementAt(tblOperationList.getSelectedRow()); |
85 |
if (index >= tblOperationList.getRowCount()) {
|
86 |
index = tblOperationList.getRowCount() - 1;
|
87 |
} |
88 |
if (index >= 0) { |
89 |
tblOperationList.getSelectionModel().setSelectionInterval(index, index); |
90 |
} |
91 |
} |
92 |
}); |
93 |
|
94 |
this.btnUp.setActionCommand(this.UP_ACTION_COMMAND); |
95 |
this.btnUp.addActionListener(new ActionListener() { |
96 |
|
97 |
@Override
|
98 |
public void actionPerformed(ActionEvent e) { |
99 |
OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
100 |
int selectedRow = tblOperationList.getSelectedRow();
|
101 |
int index = selectedRow;
|
102 |
if (selectedRow > 0) { |
103 |
index--; |
104 |
} |
105 |
model.up(selectedRow); |
106 |
tblOperationList.getSelectionModel().setSelectionInterval(index, index); |
107 |
} |
108 |
}); |
109 |
this.btnUp.setActionCommand(this.DOWN_ACTION_COMMAND); |
110 |
this.btnDown.addActionListener(new ActionListener() { |
111 |
|
112 |
@Override
|
113 |
public void actionPerformed(ActionEvent e) { |
114 |
OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
115 |
int selectedRow = tblOperationList.getSelectedRow();
|
116 |
int index = selectedRow;
|
117 |
if (selectedRow < tblOperationList.getRowCount()) {
|
118 |
index++; |
119 |
} |
120 |
model.down(selectedRow); |
121 |
tblOperationList.getSelectionModel().setSelectionInterval(index, index); |
122 |
} |
123 |
}); |
124 |
|
125 |
btnUp.setEnabled(false);
|
126 |
btnDown.setEnabled(false);
|
127 |
btnUpdate.setEnabled(false);
|
128 |
|
129 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
130 |
this.btnAdd.setIcon(iconTheme.get("add-operation-icon")); |
131 |
this.btnRemove.setIcon(iconTheme.get("remove-operation-icon")); |
132 |
this.btnUp.setIcon(iconTheme.get("up-operation-icon")); |
133 |
this.btnDown.setIcon(iconTheme.get("down-operation-icon")); |
134 |
this.btnUpdate.setIcon(iconTheme.get("update-operation-icon")); |
135 |
|
136 |
tblOperationList.setRowSelectionAllowed(true);
|
137 |
tblOperationList.setColumnSelectionAllowed(false);
|
138 |
tblOperationList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
139 |
OperationListTableModel model = new OperationListTableModel();
|
140 |
tblOperationList.setModel(model); |
141 |
|
142 |
tblOperationList.setColumnModel(new DefaultTableColumnModel()); |
143 |
TableColumnModel columnModel = tblOperationList.getColumnModel();
|
144 |
columnModel.setColumnSelectionAllowed(false);
|
145 |
for (int i = 0; i < model.getColumnCount(); i++) { |
146 |
TableColumn tableColumn = new TableColumn(i); |
147 |
tableColumn.setIdentifier(model.getColumnName(i)); |
148 |
tableColumn.setHeaderValue(model.getColumnName(i)); |
149 |
columnModel.addColumn(tableColumn); |
150 |
} |
151 |
|
152 |
columnModel.getColumn(0).setCellRenderer(new OperationEntryNameCellRenderer()); |
153 |
|
154 |
tblOperationList.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
155 |
|
156 |
@Override
|
157 |
public void valueChanged(ListSelectionEvent e) { |
158 |
int selectedRow = tblOperationList.getSelectedRow();
|
159 |
btnUp.setEnabled(selectedRow >= 0);
|
160 |
btnDown.setEnabled(selectedRow >= 0);
|
161 |
btnUpdate.setEnabled(selectedRow >= 0);
|
162 |
btnRemove.setEnabled(selectedRow >= 0);
|
163 |
} |
164 |
}); |
165 |
} |
166 |
|
167 |
private void translate() { |
168 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
169 |
btnAdd.setText(""); // i18nManager.getTranslation(btnAdd.getText())); |
170 |
btnAdd.setToolTipText(i18nManager.getTranslation(btnAdd.getToolTipText())); |
171 |
btnRemove.setText(""); // i18nManager.getTranslation(btnRemove.getText())); |
172 |
btnRemove.setToolTipText(i18nManager.getTranslation(btnRemove.getToolTipText())); |
173 |
btnUp.setText(""); // i18nManager.getTranslation(btnUp.getText())); |
174 |
btnUp.setToolTipText(i18nManager.getTranslation(btnUp.getToolTipText())); |
175 |
btnDown.setText(""); // i18nManager.getTranslation(btnDown.getText())); |
176 |
btnDown.setToolTipText(i18nManager.getTranslation(btnDown.getToolTipText())); |
177 |
btnUpdate.setText(""); // i18nManager.getTranslation(btnAdd.getText())); |
178 |
btnUpdate.setToolTipText(i18nManager.getTranslation(btnAdd.getToolTipText())); |
179 |
} |
180 |
|
181 |
/**
|
182 |
* @param locale
|
183 |
*
|
184 |
*/
|
185 |
public void setLocate(Locale locale) { |
186 |
Locale l = super.getLocale(); |
187 |
if (!l.equals(locale)) {
|
188 |
translate(); |
189 |
} |
190 |
super.setLocale(locale);
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* @param operationList
|
195 |
*/
|
196 |
@SuppressWarnings("unchecked") |
197 |
public void set(OperationList operationList) { |
198 |
addOperations(operationList); |
199 |
} |
200 |
|
201 |
/**
|
202 |
* @param colorInterpretation
|
203 |
* @param store
|
204 |
* @param model
|
205 |
*/
|
206 |
private void addOperations(OperationList operationList) { |
207 |
OperationListTableModel model = ((OperationListTableModel) tblOperationList.getModel()); |
208 |
for (Iterator iterator = operationList.iterator(); iterator.hasNext();) { |
209 |
OperationListEntry operationListEntry = (OperationListEntry) iterator.next(); |
210 |
try {
|
211 |
model.add((OperationListEntry) operationListEntry.clone()); |
212 |
} catch (CloneNotSupportedException e) { |
213 |
LOG.warn("Can't clone operation list entry", e);
|
214 |
} |
215 |
model.fireTableDataChanged(); |
216 |
} |
217 |
} |
218 |
|
219 |
/**
|
220 |
* @param layer
|
221 |
*/
|
222 |
public boolean fetch(OperationList operationList) { |
223 |
OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
224 |
for (int i = 0; i < model.getRowCount(); i++) { |
225 |
OperationListEntry entry = model.getElementAt(i); |
226 |
try {
|
227 |
operationList.add((OperationListEntry) entry.clone()); |
228 |
} catch (CloneNotSupportedException e) { |
229 |
LOG.warn("Can't get the operation list.");
|
230 |
return false; |
231 |
} |
232 |
} |
233 |
return true; |
234 |
} |
235 |
|
236 |
@Override
|
237 |
public JComponent asJComponent() { |
238 |
return this; |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void addListSelectionListener(ListSelectionListener listener) { |
243 |
tblOperationList.getSelectionModel().addListSelectionListener(listener); |
244 |
} |
245 |
|
246 |
@Override
|
247 |
public void addTableModelListener(TableModelListener listener) { |
248 |
tblOperationList.getModel().addTableModelListener(listener); |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public OperationListEntry getSelected() {
|
253 |
int selectedRow = tblOperationList.getSelectedRow();
|
254 |
OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
255 |
if (selectedRow >= 0 && selectedRow < model.getRowCount()) { |
256 |
return model.getElementAt(selectedRow);
|
257 |
} |
258 |
return null; |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public void enableAdd(boolean enable) { |
263 |
this.btnAdd.setEnabled(enable);
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public void enableUpdate(boolean enable) { |
268 |
this.btnUpdate.setEnabled(enable);
|
269 |
} |
270 |
|
271 |
@Override
|
272 |
public void add(OperationListEntry operationListEntry) { |
273 |
OperationListTableModel operationListTableModel = (OperationListTableModel) tblOperationList.getModel(); |
274 |
operationListTableModel.add(operationListEntry); |
275 |
tblOperationList.getSelectionModel().setSelectionInterval(tblOperationList.getRowCount() - 1,
|
276 |
tblOperationList.getRowCount() - 1);
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public void addActionListener(ActionListener listener) { |
281 |
btnAdd.addActionListener(listener); |
282 |
btnUpdate.addActionListener(listener); |
283 |
btnUp.addActionListener(listener); |
284 |
btnDown.addActionListener(listener); |
285 |
btnRemove.addActionListener(listener); |
286 |
} |
287 |
|
288 |
@Override
|
289 |
public void clearSelection() { |
290 |
tblOperationList.clearSelection(); |
291 |
} |
292 |
|
293 |
@Override
|
294 |
public void select(int row) { |
295 |
tblOperationList.getSelectionModel().setSelectionInterval(row, row); |
296 |
} |
297 |
|
298 |
@Override
|
299 |
public int getSelectedRow() { |
300 |
return tblOperationList.getSelectedRow();
|
301 |
} |
302 |
|
303 |
} |