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 @ 8796
History | View | Annotate | Download (11.7 KB)
1 | 8682 | fdiaz | /* 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 {//, ActionListener { //, ListSelectionListener { |
||
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 | // private OperationList operationList;
|
||
63 | |||
64 | // FLayer layer;
|
||
65 | |||
66 | /**
|
||
67 | * @param factories
|
||
68 | */
|
||
69 | @SuppressWarnings("unchecked") |
||
70 | public OperationListPanelController() {
|
||
71 | translate(); |
||
72 | initializeComponents(); |
||
73 | } |
||
74 | |||
75 | private void initializeComponents() { |
||
76 | // this.btnAdd.addActionListener(this);
|
||
77 | this.btnAdd.setActionCommand(this.ADD_ACTION_COMMAND); |
||
78 | this.btnUpdate.setActionCommand(this.UPDATE_ACTION_COMMAND); |
||
79 | this.btnRemove.setActionCommand(this.REMOVE_ACTION_COMMAND); |
||
80 | this.btnRemove.addActionListener(new ActionListener() { |
||
81 | @Override
|
||
82 | public void actionPerformed(ActionEvent e) { |
||
83 | OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
||
84 | int selectedRow = tblOperationList.getSelectedRow();
|
||
85 | int index = selectedRow;
|
||
86 | model.removeElementAt(tblOperationList.getSelectedRow()); |
||
87 | if(index>=tblOperationList.getRowCount()){
|
||
88 | index = tblOperationList.getRowCount()-1;
|
||
89 | } |
||
90 | if(index>=0){ |
||
91 | tblOperationList.getSelectionModel().setSelectionInterval(index, index); |
||
92 | } |
||
93 | } |
||
94 | }); |
||
95 | |||
96 | this.btnUp.setActionCommand(this.UP_ACTION_COMMAND); |
||
97 | this.btnUp.addActionListener(new ActionListener() { |
||
98 | @Override
|
||
99 | public void actionPerformed(ActionEvent e) { |
||
100 | OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
||
101 | int selectedRow = tblOperationList.getSelectedRow();
|
||
102 | int index = selectedRow;
|
||
103 | if(selectedRow>0){ |
||
104 | index--; |
||
105 | } |
||
106 | model.up(selectedRow); |
||
107 | tblOperationList.getSelectionModel().setSelectionInterval(index, index); |
||
108 | } |
||
109 | }); |
||
110 | this.btnUp.setActionCommand(this.DOWN_ACTION_COMMAND); |
||
111 | this.btnDown.addActionListener(new ActionListener() { |
||
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 | |||
137 | tblOperationList.setRowSelectionAllowed(true);
|
||
138 | tblOperationList.setColumnSelectionAllowed(false);
|
||
139 | tblOperationList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||
140 | // tblOperationList.getSelectionModel().addListSelectionListener(this);
|
||
141 | OperationListTableModel model = new OperationListTableModel();
|
||
142 | tblOperationList.setModel(model); |
||
143 | |||
144 | tblOperationList.setColumnModel(new DefaultTableColumnModel()); |
||
145 | TableColumnModel columnModel = tblOperationList.getColumnModel();
|
||
146 | columnModel.setColumnSelectionAllowed(false);
|
||
147 | for (int i = 0; i < model.getColumnCount(); i++) { |
||
148 | TableColumn tableColumn = new TableColumn(i); |
||
149 | tableColumn.setIdentifier(model.getColumnName(i)); |
||
150 | tableColumn.setHeaderValue(model.getColumnName(i)); |
||
151 | columnModel.addColumn(tableColumn); |
||
152 | } |
||
153 | |||
154 | columnModel.getColumn(0).setCellRenderer(new OperationEntryNameCellRenderer()); |
||
155 | |||
156 | tblOperationList.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
||
157 | @Override
|
||
158 | public void valueChanged(ListSelectionEvent e) { |
||
159 | int selectedRow = tblOperationList.getSelectedRow();
|
||
160 | btnUp.setEnabled(selectedRow >= 0);
|
||
161 | btnDown.setEnabled(selectedRow >= 0);
|
||
162 | btnUpdate.setEnabled(selectedRow >= 0);
|
||
163 | btnRemove.setEnabled(selectedRow >= 0);
|
||
164 | } |
||
165 | }); |
||
166 | } |
||
167 | |||
168 | private void translate() { |
||
169 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
170 | btnAdd.setText(""); //i18nManager.getTranslation(btnAdd.getText())); |
||
171 | btnAdd.setToolTipText(i18nManager.getTranslation(btnAdd.getToolTipText())); |
||
172 | btnRemove.setText(""); //i18nManager.getTranslation(btnRemove.getText())); |
||
173 | btnRemove.setToolTipText(i18nManager.getTranslation(btnRemove.getToolTipText())); |
||
174 | btnUp.setText(""); //i18nManager.getTranslation(btnUp.getText())); |
||
175 | btnUp.setToolTipText(i18nManager.getTranslation(btnUp.getToolTipText())); |
||
176 | btnDown.setText(""); //i18nManager.getTranslation(btnDown.getText())); |
||
177 | btnDown.setToolTipText(i18nManager.getTranslation(btnDown.getToolTipText())); |
||
178 | btnUpdate.setText(""); //i18nManager.getTranslation(btnAdd.getText())); |
||
179 | btnUpdate.setToolTipText(i18nManager.getTranslation(btnAdd.getToolTipText())); |
||
180 | } |
||
181 | |||
182 | /**
|
||
183 | * @param locale
|
||
184 | *
|
||
185 | */
|
||
186 | public void setLocate(Locale locale) { |
||
187 | Locale l = super.getLocale(); |
||
188 | if (!l.equals(locale)) {
|
||
189 | translate(); |
||
190 | } |
||
191 | super.setLocale(locale);
|
||
192 | } |
||
193 | |||
194 | /**
|
||
195 | * @param operationList
|
||
196 | */
|
||
197 | @SuppressWarnings("unchecked") |
||
198 | public void set(OperationList operationList) { |
||
199 | // this.operationList = operationList;
|
||
200 | // RasterLegend legend = this.operationList.getLegend();
|
||
201 | // ColorInterpretation colorInterpretation = null;
|
||
202 | // if (legend != null) {
|
||
203 | // colorInterpretation = legend.getColorInterpretation();
|
||
204 | // }
|
||
205 | // RasterStore store = this.operationList.getRasterStore();
|
||
206 | addOperations(operationList); |
||
207 | } |
||
208 | |||
209 | /**
|
||
210 | * @param colorInterpretation
|
||
211 | * @param store
|
||
212 | * @param model
|
||
213 | */
|
||
214 | private void addOperations(OperationList operationList) { |
||
215 | OperationListTableModel model = ((OperationListTableModel) tblOperationList.getModel()); |
||
216 | for (Iterator iterator = operationList.iterator(); iterator.hasNext();) { |
||
217 | OperationListEntry operationListEntry = (OperationListEntry) iterator.next(); |
||
218 | model.add(operationListEntry); |
||
219 | model.fireTableDataChanged(); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /**
|
||
224 | * @param layer
|
||
225 | */
|
||
226 | public boolean fetch(OperationList operationList) { |
||
227 | OperationListTableModel model = (OperationListTableModel) tblOperationList.getModel(); |
||
228 | for(int i=0; i<model.getRowCount(); i++){ |
||
229 | OperationListEntry entry = model.getElementAt(i); |
||
230 | try {
|
||
231 | operationList.add((OperationListEntry)entry.clone()); |
||
232 | } catch (CloneNotSupportedException e) { |
||
233 | LOG.warn("Can't get the operation list.");
|
||
234 | return false; |
||
235 | } |
||
236 | } |
||
237 | return true; |
||
238 | } |
||
239 | |||
240 | @Override
|
||
241 | public JComponent asJComponent() { |
||
242 | return this; |
||
243 | } |
||
244 | |||
245 | @Override
|
||
246 | public void addListSelectionListener(ListSelectionListener listener) { |
||
247 | tblOperationList.getSelectionModel().addListSelectionListener(listener); |
||
248 | } |
||
249 | |||
250 | @Override
|
||
251 | public void addTableModelListener(TableModelListener listener) { |
||
252 | tblOperationList.getModel().addTableModelListener(listener); |
||
253 | } |
||
254 | |||
255 | @Override
|
||
256 | public OperationListEntry getSelected() {
|
||
257 | int selectedRow = tblOperationList.getSelectedRow();
|
||
258 | OperationListTableModel model = (OperationListTableModel)tblOperationList.getModel(); |
||
259 | if(selectedRow>=0 && selectedRow < model.getRowCount()){ |
||
260 | return model.getElementAt(selectedRow);
|
||
261 | } |
||
262 | return null; |
||
263 | } |
||
264 | |||
265 | @Override
|
||
266 | public void enableAdd(boolean enable) { |
||
267 | this.btnAdd.setEnabled(enable);
|
||
268 | } |
||
269 | |||
270 | @Override
|
||
271 | public void enableUpdate(boolean enable) { |
||
272 | this.btnUpdate.setEnabled(enable);
|
||
273 | } |
||
274 | |||
275 | @Override
|
||
276 | public void add(OperationListEntry operationListEntry) { |
||
277 | // int selectedRow = tblOperationList.getSelectedRow();
|
||
278 | OperationListTableModel operationListTableModel = (OperationListTableModel)tblOperationList.getModel(); |
||
279 | operationListTableModel.add(operationListEntry); |
||
280 | tblOperationList.getSelectionModel().setSelectionInterval(tblOperationList.getRowCount()-1, tblOperationList.getRowCount()-1); |
||
281 | // if (selectedRow >= 0) {
|
||
282 | // for (int i = operationListTableModel.getRowCount()-1; i>selectedRow; i--){
|
||
283 | // operationListTableModel.up(i);
|
||
284 | // enableAdd(false);
|
||
285 | // tblOperationList.getSelectionModel().setSelectionInterval(i-1, i-1);
|
||
286 | // }
|
||
287 | // }
|
||
288 | // operationListTableModel.fireTableDataChanged();
|
||
289 | } |
||
290 | |||
291 | @Override
|
||
292 | public void addActionListener(ActionListener listener) { |
||
293 | btnAdd.addActionListener(listener); |
||
294 | btnUpdate.addActionListener(listener); |
||
295 | btnUp.addActionListener(listener); |
||
296 | btnDown.addActionListener(listener); |
||
297 | btnRemove.addActionListener(listener); |
||
298 | } |
||
299 | |||
300 | @Override
|
||
301 | public void clearSelection() { |
||
302 | tblOperationList.clearSelection(); |
||
303 | } |
||
304 | |||
305 | |||
306 | } |