root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandStackDialog.java @ 38564
History | View | Annotate | Download (11.5 KB)
1 | 29596 | jpiera | package org.gvsig.app.gui.command; |
---|---|---|---|
2 | 4120 | caballero | |
3 | 37535 | jpiera | import java.awt.BorderLayout; |
4 | import java.awt.Color; |
||
5 | import java.awt.Component; |
||
6 | import java.awt.Dimension; |
||
7 | 4120 | caballero | |
8 | 37535 | jpiera | import javax.swing.ImageIcon; |
9 | import javax.swing.JLabel; |
||
10 | import javax.swing.JPanel; |
||
11 | import javax.swing.JScrollBar; |
||
12 | import javax.swing.JScrollPane; |
||
13 | import javax.swing.JSlider; |
||
14 | import javax.swing.JTable; |
||
15 | import javax.swing.ListSelectionModel; |
||
16 | 4120 | caballero | import javax.swing.table.DefaultTableCellRenderer; |
17 | import javax.swing.table.TableColumn; |
||
18 | |||
19 | 38564 | jjdelcerro | import org.gvsig.andami.IconThemeHelper; |
20 | 29596 | jpiera | import org.gvsig.andami.PluginServices; |
21 | import org.gvsig.andami.ui.mdiManager.IWindowListener; |
||
22 | import org.gvsig.andami.ui.mdiManager.SingletonWindow; |
||
23 | import org.gvsig.andami.ui.mdiManager.WindowInfo; |
||
24 | 37535 | jpiera | import org.gvsig.app.ApplicationLocator; |
25 | import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
||
26 | 4529 | ldiaz | import org.gvsig.gui.beans.DefaultBean; |
27 | 23075 | jmvivo | import org.gvsig.tools.observer.Observable; |
28 | import org.gvsig.tools.observer.Observer; |
||
29 | 25397 | cordinyana | import org.gvsig.tools.undo.UndoRedoInfo; |
30 | 25065 | vcaballero | import org.gvsig.tools.undo.UndoRedoStack; |
31 | 4529 | ldiaz | |
32 | 4120 | caballero | |
33 | 37597 | jpiera | @SuppressWarnings("serial") |
34 | public class CommandStackDialog extends DefaultBean implements SingletonWindow, IWindowListener, Observer{ |
||
35 | 4120 | caballero | |
36 | 37573 | jpiera | private JTable commandTable = null; |
37 | private JPanel topPanel = null; |
||
38 | 25065 | vcaballero | private UndoRedoStack undoRedoStack;
|
39 | 37573 | jpiera | private JSlider commandSlider = null; |
40 | |||
41 | 4120 | caballero | private int lowLimit; |
42 | 37573 | jpiera | private int currentValue = -1; |
43 | 37814 | jpiera | private int currentSliderValue = -1; |
44 | 37573 | jpiera | private JPanel sliderPanel = null; |
45 | 4120 | caballero | protected boolean refreshing; |
46 | 37605 | jpiera | private JPanel centerPanel = null; |
47 | 4120 | caballero | private JScrollPane jScrollPane = null; |
48 | 37605 | jpiera | private JPanel tablePanel = null; |
49 | 37573 | jpiera | |
50 | 38564 | jjdelcerro | private static final ImageIcon imodify = IconThemeHelper.getImageIcon("layout-tool-undo-redo-actions-modify"); |
51 | private static final ImageIcon iadd = IconThemeHelper.getImageIcon("layout-tool-undo-redo-actions-add"); |
||
52 | private static final ImageIcon idel = IconThemeHelper.getImageIcon("layout-tool-undo-redo-actions-delete"); |
||
53 | 21299 | vcaballero | |
54 | 37573 | jpiera | private CommandTableModel commandTableModel = null; |
55 | 4120 | caballero | /**
|
56 | * This is the default constructor
|
||
57 | */
|
||
58 | 4964 | caballero | public CommandStackDialog() {
|
59 | 4120 | caballero | super();
|
60 | initialize(); |
||
61 | } |
||
62 | 37573 | jpiera | |
63 | public void setModel(UndoRedoStack cr1) { |
||
64 | 26833 | cordinyana | if (this.undoRedoStack != null) { |
65 | if (this.undoRedoStack.equals(cr1)) { |
||
66 | return;
|
||
67 | } else {
|
||
68 | this.undoRedoStack.deleteObserver(this); |
||
69 | } |
||
70 | 23075 | jmvivo | } |
71 | 37573 | jpiera | this.undoRedoStack = cr1;
|
72 | 26833 | cordinyana | this.undoRedoStack.addObserver(this); |
73 | 4964 | caballero | initTable(); |
74 | initSlider(); |
||
75 | 37573 | jpiera | currentValue = commandTableModel.getPos(); |
76 | 4964 | caballero | refreshControls(); |
77 | 37577 | jpiera | refreshSliderSize(); |
78 | 4964 | caballero | } |
79 | 37573 | jpiera | |
80 | 4120 | caballero | /**
|
81 | * This method initializes this
|
||
82 | *
|
||
83 | * @return void
|
||
84 | */
|
||
85 | private void initialize() { |
||
86 | this.setLayout(new BorderLayout()); |
||
87 | 37605 | jpiera | this.setSize(328, 229); |
88 | 4120 | caballero | this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
89 | } |
||
90 | |||
91 | /**
|
||
92 | * This method initializes jList
|
||
93 | *
|
||
94 | * @return javax.swing.JList
|
||
95 | */
|
||
96 | private JTable getTable() { |
||
97 | 37573 | jpiera | if (commandTable == null) { |
98 | commandTable = new JTable(); |
||
99 | 4120 | caballero | } |
100 | 37573 | jpiera | return commandTable;
|
101 | 4120 | caballero | } |
102 | 37573 | jpiera | |
103 | 4964 | caballero | private void initTable(){ |
104 | 37573 | jpiera | commandTableModel = new CommandTableModel(undoRedoStack);
|
105 | commandTable.setModel(commandTableModel); |
||
106 | commandTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||
107 | commandTable.setSelectionBackground(Color.orange);
|
||
108 | commandTable.setSelectionForeground(Color.black);
|
||
109 | commandTable.setShowGrid(false);
|
||
110 | commandTable.getTableHeader().setBackground(Color.white);
|
||
111 | TableColumn tc = commandTable.getColumnModel().getColumn(0); |
||
112 | 4964 | caballero | tc.setCellRenderer(new DefaultTableCellRenderer() { |
113 | public Component getTableCellRendererComponent(JTable table, |
||
114 | Object value,
|
||
115 | boolean isSelected,
|
||
116 | boolean hasFocus,
|
||
117 | int row,
|
||
118 | int column)
|
||
119 | { |
||
120 | JLabel label = (JLabel) |
||
121 | super.getTableCellRendererComponent
|
||
122 | (table, value, isSelected, hasFocus, row, column); |
||
123 | 25397 | cordinyana | UndoRedoInfo info = (UndoRedoInfo) value; |
124 | switch (info.getType()) {
|
||
125 | case UndoRedoInfo.INSERT:
|
||
126 | label.setIcon(iadd); |
||
127 | break;
|
||
128 | case UndoRedoInfo.DELETE:
|
||
129 | label.setIcon(idel); |
||
130 | break;
|
||
131 | default:
|
||
132 | label.setIcon(imodify); |
||
133 | } |
||
134 | 37573 | jpiera | if (commandTableModel.getPos()<row){
|
135 | 4964 | caballero | label.setBackground(Color.lightGray);
|
136 | }else {
|
||
137 | 26949 | vcaballero | label.setBackground(Color.orange);
|
138 | 4964 | caballero | } |
139 | return label;
|
||
140 | } |
||
141 | }); |
||
142 | 4120 | caballero | |
143 | 37573 | jpiera | commandTable.addMouseListener(new java.awt.event.MouseAdapter() {
|
144 | 4964 | caballero | public void mousePressed(java.awt.event.MouseEvent e) { |
145 | 37814 | jpiera | int newpos = commandTable.getSelectedRow();
|
146 | if (newpos >= 0){ |
||
147 | commandTableModel.setPos(newpos); |
||
148 | PluginServices.getMainFrame().enableControls(); |
||
149 | } |
||
150 | 4964 | caballero | } |
151 | }); |
||
152 | } |
||
153 | 4120 | caballero | /**
|
154 | * This method initializes jPanel
|
||
155 | *
|
||
156 | * @return javax.swing.JPanel
|
||
157 | */
|
||
158 | 37605 | jpiera | private JPanel getTopPanel() { |
159 | 37573 | jpiera | if (topPanel == null) { |
160 | 37605 | jpiera | topPanel = new JPanel(); |
161 | 4120 | caballero | } |
162 | 37573 | jpiera | return topPanel;
|
163 | 4120 | caballero | } |
164 | |||
165 | 6880 | cesar | public WindowInfo getWindowInfo() {
|
166 | WindowInfo m_viewinfo = new WindowInfo(WindowInfo.ICONIFIABLE |
|
||
167 | WindowInfo.MODELESSDIALOG | WindowInfo.RESIZABLE | WindowInfo.PALETTE); |
||
168 | 4120 | caballero | m_viewinfo.setTitle(PluginServices.getText(this,
|
169 | "pila_de_comandos"));
|
||
170 | return m_viewinfo;
|
||
171 | } |
||
172 | |||
173 | 6880 | cesar | public Object getWindowModel() { |
174 | 37573 | jpiera | return commandTableModel.getClass().getName();
|
175 | 4120 | caballero | } |
176 | |||
177 | 6880 | cesar | public void windowActivated() { |
178 | 4120 | caballero | this.validateTree();
|
179 | } |
||
180 | |||
181 | 6880 | cesar | public void windowClosed() { |
182 | 37597 | jpiera | |
183 | 4120 | caballero | } |
184 | |||
185 | 4140 | caballero | public void commandRepaint() { |
186 | 37597 | jpiera | setValue(commandTableModel.getPos(), true);
|
187 | 37577 | jpiera | refreshSliderSize(); |
188 | 4120 | caballero | } |
189 | 37577 | jpiera | |
190 | /**
|
||
191 | * Refreshes all the mutable controls in this component.
|
||
192 | */
|
||
193 | private void refreshControls() { |
||
194 | 37597 | jpiera | int normalizedValue = (int) (((commandTableModel.getRowCount() - (currentValue + 1)) / (float)commandTableModel.getRowCount()) * 100); |
195 | //Adding the 50% od the interval
|
||
196 | if (commandTableModel.getRowCount() > 0){ |
||
197 | normalizedValue = normalizedValue + (100 / (commandTableModel.getRowCount() * 2)); |
||
198 | } |
||
199 | 37577 | jpiera | refreshSlider(normalizedValue); |
200 | commandTable.repaint(); |
||
201 | } |
||
202 | 37597 | jpiera | |
203 | 37577 | jpiera | /**
|
204 | * Sets the slider to the correct (scaled) position.
|
||
205 | * @param normalizedValue
|
||
206 | */
|
||
207 | private void refreshSlider(int normalizedValue) { |
||
208 | if (!refreshing){
|
||
209 | refreshing = true;
|
||
210 | getJSlider().setValue(normalizedValue); |
||
211 | 37597 | jpiera | refreshing = false;
|
212 | 37577 | jpiera | } |
213 | } |
||
214 | |||
215 | private void refreshSliderSize(){ |
||
216 | if (!refreshing){
|
||
217 | 37597 | jpiera | Dimension size = new Dimension(commandSlider.getPreferredSize().width, |
218 | 37605 | jpiera | ((commandTableModel.getRowCount() + 1) * getTable().getRowHeight()));
|
219 | 37597 | jpiera | JScrollBar verticalScrollBar = getJScrollPane().getVerticalScrollBar();
|
220 | 37577 | jpiera | verticalScrollBar.setValue(commandTableModel.getPos()*getTable().getRowHeight()); |
221 | commandSlider.setPreferredSize(size); |
||
222 | commandSlider.setSize(size); |
||
223 | validateTree(); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | 37573 | jpiera | |
228 | 4120 | caballero | /**
|
229 | * This method initializes jSlider
|
||
230 | *
|
||
231 | * @return javax.swing.JSlider
|
||
232 | */
|
||
233 | private JSlider getJSlider() { |
||
234 | 37573 | jpiera | if (commandSlider == null) { |
235 | 37605 | jpiera | commandSlider = new JSlider(JSlider.VERTICAL, 0, 100, 0); |
236 | 4964 | caballero | } |
237 | 37573 | jpiera | return commandSlider;
|
238 | 4964 | caballero | } |
239 | 37573 | jpiera | |
240 | 37597 | jpiera | private void initSlider(){ |
241 | commandSlider.setPreferredSize( |
||
242 | new Dimension(commandSlider.getPreferredSize().width, |
||
243 | ((getTable().getRowCount())*getTable().getRowHeight()))); |
||
244 | 4120 | caballero | |
245 | 37597 | jpiera | commandSlider.addChangeListener(new javax.swing.event.ChangeListener() {
|
246 | 37814 | jpiera | public synchronized void stateChanged(javax.swing.event.ChangeEvent e) { |
247 | 37597 | jpiera | if (!refreshing){
|
248 | 37814 | jpiera | int value = getTablePosFromSlider();
|
249 | //currentSliderValue controls the same event thrown by the
|
||
250 | //slider. currentValue controls the event thrown by the table
|
||
251 | if (currentSliderValue != value){
|
||
252 | refreshing = true;
|
||
253 | currentSliderValue = value; |
||
254 | 37597 | jpiera | commandTableModel.setPos(value); |
255 | refreshing = false;
|
||
256 | } |
||
257 | } |
||
258 | 21299 | vcaballero | } |
259 | 4964 | caballero | }); |
260 | 37597 | jpiera | setValue(commandTableModel.getRowCount() - 1 - commandTableModel.getPos(),true); |
261 | 4120 | caballero | } |
262 | 37597 | jpiera | |
263 | private int getTablePosFromSlider(){ |
||
264 | if (commandTableModel.getRowCount() == 0){ |
||
265 | return -1; |
||
266 | } |
||
267 | |||
268 | int value = getJSlider().getValue();
|
||
269 | value = (int) ((value * 0.01) * commandTableModel.getRowCount()); |
||
270 | 37605 | jpiera | |
271 | 37597 | jpiera | //The bottom part of the slider starts in 100: take the reverse value
|
272 | value = (commandTableModel.getRowCount() - 1) - value;
|
||
273 | |||
274 | 37814 | jpiera | if (value == -1){ |
275 | return 0; |
||
276 | } |
||
277 | 37597 | jpiera | return value;
|
278 | } |
||
279 | |||
280 | 4120 | caballero | public void setValue(int number, boolean fireEvent) { |
281 | 37597 | jpiera | int rowCount = commandTableModel.getRowCount();
|
282 | |||
283 | 26949 | vcaballero | if (number < lowLimit) {
|
284 | 23075 | jmvivo | number = lowLimit; |
285 | } |
||
286 | 37597 | jpiera | if (number > (rowCount - 1)) { |
287 | 25065 | vcaballero | number = rowCount; |
288 | 23075 | jmvivo | } |
289 | 4120 | caballero | if (number != currentValue) {
|
290 | currentValue = number; |
||
291 | refreshControls(); |
||
292 | 23075 | jmvivo | if (fireEvent) {
|
293 | callValueChanged(new Integer(currentValue)); |
||
294 | } |
||
295 | 37597 | jpiera | } |
296 | 4120 | caballero | } |
297 | |||
298 | /**
|
||
299 | * This method initializes jPanel1
|
||
300 | *
|
||
301 | * @return javax.swing.JPanel
|
||
302 | */
|
||
303 | 37605 | jpiera | private JPanel getSliderPanel() { |
304 | 37573 | jpiera | if (sliderPanel == null) { |
305 | sliderPanel = new JPanel(); |
||
306 | sliderPanel.add(getJSlider()); |
||
307 | 4120 | caballero | } |
308 | 37573 | jpiera | return sliderPanel;
|
309 | 4120 | caballero | } |
310 | |||
311 | /**
|
||
312 | * This method initializes pCenter
|
||
313 | *
|
||
314 | * @return javax.swing.JPanel
|
||
315 | */
|
||
316 | 37605 | jpiera | private JPanel getCenterPanel() { |
317 | if (centerPanel == null) { |
||
318 | centerPanel = new JPanel(); |
||
319 | centerPanel.setLayout(new BorderLayout()); |
||
320 | centerPanel.add(getTablePanel(), java.awt.BorderLayout.CENTER); |
||
321 | centerPanel.add(getSliderPanel(), java.awt.BorderLayout.WEST); |
||
322 | 4120 | caballero | } |
323 | 37605 | jpiera | return centerPanel;
|
324 | 4120 | caballero | } |
325 | 37605 | jpiera | |
326 | private JPanel getTablePanel() { |
||
327 | if (tablePanel == null) { |
||
328 | tablePanel = new JPanel(); |
||
329 | tablePanel.setLayout(new BorderLayout()); |
||
330 | tablePanel.add(getTable(), java.awt.BorderLayout.CENTER); |
||
331 | tablePanel.add(getTopPanel(), java.awt.BorderLayout.NORTH); |
||
332 | } |
||
333 | return tablePanel;
|
||
334 | } |
||
335 | 4120 | caballero | |
336 | /**
|
||
337 | * This method initializes jScrollPane
|
||
338 | *
|
||
339 | * @return javax.swing.JScrollPane
|
||
340 | */
|
||
341 | private JScrollPane getJScrollPane() { |
||
342 | if (jScrollPane == null) { |
||
343 | jScrollPane = new JScrollPane(); |
||
344 | 37605 | jpiera | jScrollPane.setViewportView(getCenterPanel()); |
345 | 4120 | caballero | } |
346 | return jScrollPane;
|
||
347 | } |
||
348 | 37535 | jpiera | |
349 | 24923 | vcaballero | public void update(Observable observable, Object notification) { |
350 | 37535 | jpiera | if (notification instanceof FeatureStoreNotification){ |
351 | FeatureStoreNotification featureStoreNotification = |
||
352 | (FeatureStoreNotification) notification; |
||
353 | //If the edition finish the command stack disappears
|
||
354 | String type = featureStoreNotification.getType();
|
||
355 | if (FeatureStoreNotification.AFTER_FINISHEDITING.equals(type) ||
|
||
356 | FeatureStoreNotification.AFTER_CANCELEDITING.equals(type)){ |
||
357 | ApplicationLocator.getManager().getUIManager().closeWindow(this);
|
||
358 | featureStoreNotification.getSource().deleteObserver(this);
|
||
359 | return;
|
||
360 | } |
||
361 | 37597 | jpiera | //Only repaint if the event is a selection event or an edition event
|
362 | if (FeatureStoreNotification.AFTER_INSERT.equals(type) ||
|
||
363 | FeatureStoreNotification.AFTER_DELETE.equals(type) || |
||
364 | FeatureStoreNotification.AFTER_UPDATE.equals(type) || |
||
365 | 37814 | jpiera | FeatureStoreNotification.SELECTION_CHANGE.equals(type) || |
366 | FeatureStoreNotification.AFTER_REDO.equals(type) || |
||
367 | FeatureStoreNotification.AFTER_UNDO.equals(type)) { |
||
368 | 37597 | jpiera | commandRepaint(); |
369 | } |
||
370 | 37535 | jpiera | } |
371 | 24782 | vcaballero | } |
372 | 4128 | caballero | |
373 | 26449 | jmvivo | public Object getWindowProfile() { |
374 | return WindowInfo.DIALOG_PROFILE;
|
||
375 | } |
||
376 | 37597 | jpiera | } |