gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / task / DefaultJTasksStatusList.java @ 2632
History | View | Annotate | Download (7.92 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 673 | cordinyana | *
|
4 | 2632 | jjdelcerro | * Copyright (C) 2007-2021 gvSIG Association.
|
5 | 802 | cordinyana | *
|
6 | 673 | cordinyana | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | 802 | cordinyana | *
|
11 | 673 | cordinyana | * This program is distributed in the hope that it will be useful,
|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | 802 | cordinyana | *
|
16 | 673 | cordinyana | * You should have received a copy of the GNU General Public License
|
17 | * along with this program; if not, write to the Free Software
|
||
18 | 802 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 673 | cordinyana | * MA 02110-1301, USA.
|
20 | 802 | cordinyana | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 673 | cordinyana | */
|
24 | 449 | jjdelcerro | package org.gvsig.tools.swing.impl.task; |
25 | |||
26 | import java.awt.BorderLayout; |
||
27 | 1719 | jjdelcerro | import java.awt.Component; |
28 | 449 | jjdelcerro | import java.awt.Dimension; |
29 | import java.util.ArrayList; |
||
30 | 2632 | jjdelcerro | import java.util.EventObject; |
31 | 449 | jjdelcerro | import java.util.List; |
32 | 2632 | jjdelcerro | import java.util.Objects; |
33 | 449 | jjdelcerro | |
34 | 2632 | jjdelcerro | import javax.swing.JLabel; |
35 | 449 | jjdelcerro | import javax.swing.JScrollPane; |
36 | 2632 | jjdelcerro | import javax.swing.JTable; |
37 | 1723 | jjdelcerro | import javax.swing.SwingUtilities; |
38 | 2632 | jjdelcerro | import javax.swing.event.CellEditorListener; |
39 | import javax.swing.table.AbstractTableModel; |
||
40 | import javax.swing.table.DefaultTableColumnModel; |
||
41 | import javax.swing.table.TableCellEditor; |
||
42 | import javax.swing.table.TableCellRenderer; |
||
43 | import javax.swing.table.TableColumn; |
||
44 | import javax.swing.table.TableModel; |
||
45 | 449 | jjdelcerro | |
46 | import org.gvsig.tools.observer.Observable; |
||
47 | import org.gvsig.tools.observer.Observer; |
||
48 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
49 | import org.gvsig.tools.swing.api.task.JTaskStatus; |
||
50 | import org.gvsig.tools.swing.api.task.JTasksStatusList; |
||
51 | import org.gvsig.tools.swing.api.task.TaskStatusSwingManager; |
||
52 | import org.gvsig.tools.task.TaskStatus; |
||
53 | import org.gvsig.tools.task.TaskStatusManager; |
||
54 | 2632 | jjdelcerro | import org.slf4j.Logger; |
55 | import org.slf4j.LoggerFactory; |
||
56 | 449 | jjdelcerro | |
57 | 673 | cordinyana | /**
|
58 | * Default implementation of the {@link JTasksStatusList}.
|
||
59 | *
|
||
60 | * @author gvSIG Team
|
||
61 | */
|
||
62 | public class DefaultJTasksStatusList extends JTasksStatusList implements |
||
63 | Observer {
|
||
64 | 449 | jjdelcerro | |
65 | 2632 | jjdelcerro | public static final Logger LOGGER = LoggerFactory.getLogger(DefaultJTasksStatusList.class); |
66 | |||
67 | 673 | cordinyana | private static final long serialVersionUID = 7740032506050392725L; |
68 | 449 | jjdelcerro | |
69 | 2632 | jjdelcerro | private final TaskStatusManager manager; |
70 | private JTable table; |
||
71 | 449 | jjdelcerro | |
72 | 2632 | jjdelcerro | @SuppressWarnings("LeakingThisInConstructor") |
73 | 673 | cordinyana | public DefaultJTasksStatusList(TaskStatusManager manager) {
|
74 | this.manager = manager;
|
||
75 | this.makeUI();
|
||
76 | this.manager.addObserver(this); |
||
77 | } |
||
78 | |||
79 | 1719 | jjdelcerro | @Override
|
80 | 673 | cordinyana | public TaskStatusManager getManager() {
|
81 | return this.manager; |
||
82 | } |
||
83 | |||
84 | 2632 | jjdelcerro | |
85 | 673 | cordinyana | private void makeUI() { |
86 | this.setLayout(new BorderLayout()); |
||
87 | 2632 | jjdelcerro | TaskStatusSwingManager swingManager = ToolsSwingLocator.getTaskStatusSwingManager(); |
88 | this.table = new JTable(); |
||
89 | DefaultTableColumnModel columnModel = new DefaultTableColumnModel(); |
||
90 | TableColumn tableColumn = new TableColumn( |
||
91 | 0,
|
||
92 | 300,
|
||
93 | new TableCellRenderer() { |
||
94 | JTaskStatus component = swingManager.createJTaskStatus(); |
||
95 | JLabel empty = new JLabel("???"); |
||
96 | @Override
|
||
97 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
||
98 | if( value instanceof TaskStatus ) { |
||
99 | this.component.bind((TaskStatus) value);
|
||
100 | return this.component; |
||
101 | } |
||
102 | this.empty.setText("??? "+Objects.toString(value)); |
||
103 | return this.empty; |
||
104 | } |
||
105 | }, |
||
106 | new TableCellEditor() { |
||
107 | JTaskStatus component = swingManager.createJTaskStatus(); |
||
108 | JLabel empty = new JLabel("???"); |
||
109 | @Override
|
||
110 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
||
111 | if( value instanceof TaskStatus ) { |
||
112 | this.component.bind((TaskStatus) value);
|
||
113 | return this.component; |
||
114 | } |
||
115 | this.empty.setText("??? "+Objects.toString(value)); |
||
116 | return this.empty; |
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | public Object getCellEditorValue() { |
||
121 | return this.component.getTaskStatus(); |
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | public boolean isCellEditable(EventObject anEvent) { |
||
126 | return true; |
||
127 | } |
||
128 | |||
129 | @Override
|
||
130 | public boolean shouldSelectCell(EventObject anEvent) { |
||
131 | return false; |
||
132 | } |
||
133 | |||
134 | @Override
|
||
135 | public boolean stopCellEditing() { |
||
136 | return true; |
||
137 | } |
||
138 | |||
139 | @Override
|
||
140 | public void cancelCellEditing() { |
||
141 | } |
||
142 | |||
143 | @Override
|
||
144 | public void addCellEditorListener(CellEditorListener l) { |
||
145 | } |
||
146 | |||
147 | @Override
|
||
148 | public void removeCellEditorListener(CellEditorListener l) { |
||
149 | } |
||
150 | } |
||
151 | ); |
||
152 | JTaskStatus taskStatusHeight = swingManager.createJTaskStatus(); |
||
153 | taskStatusHeight.setTitle("WWWMMMGGGgggqqqjjjttt");
|
||
154 | taskStatusHeight.message("WWWMMMGGGgggqqqjjjttt");
|
||
155 | columnModel.addColumn(tableColumn); |
||
156 | this.table.setColumnModel(columnModel);
|
||
157 | this.table.setRowHeight(taskStatusHeight.getPreferredSize().height);
|
||
158 | this.table.setAutoCreateColumnsFromModel(false); |
||
159 | this.table.setTableHeader(null); |
||
160 | this.table.setModel(this.createModel()); |
||
161 | JScrollPane scroller = new JScrollPane(table); |
||
162 | |||
163 | 1723 | jjdelcerro | scroller.setAlignmentY(Component.TOP_ALIGNMENT);
|
164 | 673 | cordinyana | scroller.setPreferredSize(new Dimension(300, 150)); |
165 | 449 | jjdelcerro | this.add(scroller, BorderLayout.CENTER); |
166 | 673 | cordinyana | } |
167 | 449 | jjdelcerro | |
168 | 2632 | jjdelcerro | private TableModel createModel() { |
169 | List<TaskStatus> tasks = new ArrayList<>(this.manager.getAll().values()); |
||
170 | return new AbstractTableModel() { |
||
171 | @Override
|
||
172 | public int getRowCount() { |
||
173 | return tasks.size();
|
||
174 | } |
||
175 | 673 | cordinyana | |
176 | 2632 | jjdelcerro | @Override
|
177 | public int getColumnCount() { |
||
178 | return 1; |
||
179 | } |
||
180 | 673 | cordinyana | |
181 | 2632 | jjdelcerro | @Override
|
182 | public String getColumnName(int columnIndex) { |
||
183 | return null; |
||
184 | 673 | cordinyana | } |
185 | 2632 | jjdelcerro | |
186 | @Override
|
||
187 | public Class<?> getColumnClass(int columnIndex) { |
||
188 | return TaskStatus.class;
|
||
189 | } |
||
190 | |||
191 | @Override
|
||
192 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
||
193 | return true; |
||
194 | } |
||
195 | |||
196 | @Override
|
||
197 | public Object getValueAt(int rowIndex, int columnIndex) { |
||
198 | return tasks.get(rowIndex);
|
||
199 | } |
||
200 | |||
201 | @Override
|
||
202 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
||
203 | } |
||
204 | }; |
||
205 | 673 | cordinyana | } |
206 | 2632 | jjdelcerro | |
207 | 1719 | jjdelcerro | @Override
|
208 | 1723 | jjdelcerro | public void update(final Observable observable, final Object notification) { |
209 | 673 | cordinyana | if (!(observable instanceof TaskStatusManager)) { |
210 | return;
|
||
211 | } |
||
212 | 1723 | jjdelcerro | if( !SwingUtilities.isEventDispatchThread() ) { |
213 | 2632 | jjdelcerro | SwingUtilities.invokeLater(() -> { update(observable, notification); });
|
214 | 1723 | jjdelcerro | return;
|
215 | } |
||
216 | 2632 | jjdelcerro | if (this.table.isEditing()) { |
217 | this.table.editingCanceled(null); |
||
218 | 673 | cordinyana | } |
219 | 2632 | jjdelcerro | this.table.setModel(this.createModel()); |
220 | 673 | cordinyana | } |
221 | 449 | jjdelcerro | } |