Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandStackDialog.java @ 39341

History | View | Annotate | Download (11.7 KB)

1
package org.gvsig.app.gui.command;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Component;
6
import java.awt.Dimension;
7

    
8
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
import javax.swing.table.DefaultTableCellRenderer;
17
import javax.swing.table.TableColumn;
18

    
19
import org.gvsig.andami.IconThemeHelper;
20
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
import org.gvsig.app.ApplicationLocator;
25
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
26
import org.gvsig.gui.beans.DefaultBean;
27
import org.gvsig.tools.observer.Observable;
28
import org.gvsig.tools.observer.Observer;
29
import org.gvsig.tools.undo.UndoRedoInfo;
30
import org.gvsig.tools.undo.UndoRedoStack;
31

    
32

    
33
@SuppressWarnings("serial")
34
public class CommandStackDialog extends DefaultBean implements SingletonWindow, IWindowListener, Observer{
35

    
36
        private JTable commandTable = null;
37
        private JPanel topPanel = null;
38
        private UndoRedoStack undoRedoStack;
39
        private JSlider commandSlider = null;
40
        
41
        private int lowLimit;
42
        private int currentValue = -1;
43
        private int currentSliderValue = -1;
44
        private JPanel sliderPanel = null;
45
        protected boolean refreshing;
46
        private JPanel centerPanel = null;
47
        private JScrollPane jScrollPane = null;
48
        private JPanel tablePanel = null;
49
        
50
        private static final ImageIcon imodify = IconThemeHelper.getImageIcon("edit-undo-redo-actions-modify"); 
51
        private static final ImageIcon iadd = IconThemeHelper.getImageIcon("edit-undo-redo-actions-add");
52
        private static final ImageIcon idel = IconThemeHelper.getImageIcon("edit-undo-redo-actions-delete");
53

    
54
        private CommandTableModel commandTableModel = null;
55
        /**
56
         * This is the default constructor
57
         */
58
        public CommandStackDialog() {
59
                super();
60
                initialize();
61
        }
62
        
63
        public void setModel(UndoRedoStack cr1) {
64
        if (this.undoRedoStack != null) {
65
            if (this.undoRedoStack.equals(cr1)) {
66
                return;
67
            } else {
68
                this.undoRedoStack.deleteObserver(this);
69
            }
70
                }
71
                this.undoRedoStack = cr1;
72
                this.undoRedoStack.addObserver(this);
73
                initTable();
74
                initSlider();
75
                currentValue = commandTableModel.getPos();
76
            refreshControls();
77
                refreshSliderSize();
78
        }
79
        
80
        /**
81
         * This method initializes this
82
         *
83
         * @return void
84
         */
85
        private void initialize() {
86
                this.setLayout(new BorderLayout());
87
                this.setSize(328, 229);                
88
                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
                if (commandTable == null) {
98
                        commandTable = new JTable();
99
                }
100
                return commandTable;
101
        }
102
        
103
        private void initTable(){
104
                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
                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
                                    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
                                 if (commandTableModel.getPos()<row){
135
                                         label.setBackground(Color.lightGray);
136
                                 }else {
137
                                                label.setBackground(Color.orange);
138
                                 }
139
                                    return label;
140
                              }
141
                        });
142

    
143
                commandTable.addMouseListener(new java.awt.event.MouseAdapter() {
144
                        public void mousePressed(java.awt.event.MouseEvent e) {
145
                                int newpos = commandTable.getSelectedRow();        
146
                                if (newpos >= 0){
147
                                    commandTableModel.setPos(newpos);                                
148
                                    ApplicationLocator.getManager().refreshMenusAndToolBars();
149
                                }
150
                        }
151
                });
152
        }
153
        /**
154
         * This method initializes jPanel
155
         *
156
         * @return javax.swing.JPanel
157
         */
158
        private JPanel getTopPanel() {
159
                if (topPanel == null) {
160
                        topPanel = new JPanel();        
161
                }
162
                return topPanel;
163
        }
164

    
165
        public WindowInfo getWindowInfo() {
166
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.ICONIFIABLE |
167
                                WindowInfo.MODELESSDIALOG | WindowInfo.RESIZABLE | WindowInfo.PALETTE);
168
                m_viewinfo.setTitle(PluginServices.getText(this,
169
                                "pila_de_comandos"));
170
                return m_viewinfo;
171
        }
172

    
173
        public Object getWindowModel() {
174
                return commandTableModel.getClass().getName();
175
        }
176

    
177
        public void windowActivated() {
178
                this.validateTree();
179
        }
180

    
181
        public void windowClosed() {
182
                
183
        }
184

    
185
        public void commandRepaint() {
186
                setValue(commandTableModel.getPos(), true);
187
                refreshSliderSize();
188
        }
189

    
190
    /**
191
     * Refreshes all the mutable controls in this component.
192
     */
193
    private void refreshControls() {
194
        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
        refreshSlider(normalizedValue);
200
        commandTable.repaint();
201
    }
202
    
203
    /**
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
            refreshing = false; 
212
        }
213
    }    
214
    
215
    private void refreshSliderSize(){
216
        if (!refreshing){
217
            Dimension size = new Dimension(commandSlider.getPreferredSize().width,
218
                ((commandTableModel.getRowCount() + 1) * getTable().getRowHeight()));
219
            JScrollBar verticalScrollBar = getJScrollPane().getVerticalScrollBar();
220
            verticalScrollBar.setValue(commandTableModel.getPos()*getTable().getRowHeight());
221
            commandSlider.setPreferredSize(size);
222
            commandSlider.setSize(size);
223
            validateTree();
224
        }
225
    }
226
    
227
        
228
        /**
229
         * This method initializes jSlider
230
         *
231
         * @return javax.swing.JSlider
232
         */
233
        private JSlider getJSlider() {
234
                if (commandSlider == null) {
235
                        commandSlider = new JSlider(JSlider.VERTICAL, 0, 100, 0);        
236
                }
237
                return commandSlider;
238
        }
239
        
240
        private void initSlider(){                
241
                commandSlider.setPreferredSize(
242
                    new Dimension(commandSlider.getPreferredSize().width,
243
                        ((getTable().getRowCount())*getTable().getRowHeight())));
244

    
245
                commandSlider.addChangeListener(new javax.swing.event.ChangeListener() {
246
                                public synchronized void stateChanged(javax.swing.event.ChangeEvent e) {
247
                                    if (!refreshing){
248
                                        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
                                            commandTableModel.setPos(value);
255
                                            ApplicationLocator.getManager().refreshMenusAndToolBars();
256
                                            refreshing = false;    
257
                                        }
258
                                    }
259
                            }
260
                    });
261
            setValue(commandTableModel.getRowCount() - 1 - commandTableModel.getPos(),true);
262
        }
263
        
264
        private int getTablePosFromSlider(){          
265
            if (commandTableModel.getRowCount() == 0){
266
                return -1;
267
            }
268
            
269
            int value = getJSlider().getValue();
270
            value = (int) ((value * 0.01) * commandTableModel.getRowCount());
271

    
272
            //The bottom part of the slider starts in 100: take the reverse value
273
            value = (commandTableModel.getRowCount() - 1) - value;
274
            
275
            if (value == -1){
276
                return 0;
277
            }
278
            return value;
279
        }
280
        
281
    public void setValue(int number, boolean fireEvent) {
282
        int rowCount = commandTableModel.getRowCount();
283
               
284
        if (number < lowLimit) {
285
                        number = lowLimit;
286
                }
287
        if (number > (rowCount - 1)) {
288
                        number = rowCount;
289
                }
290
        if (number != currentValue) {
291
                currentValue = number;
292
                if (fireEvent) {
293
                                callValueChanged(new Integer(currentValue));
294
                        }
295
        }
296
        /*
297
         * This needs to be refreshed also when same number is set
298
         * Example: select one feature and click 'delete' key
299
         */
300
        refreshControls();
301
    }
302

    
303
        /**
304
         * This method initializes jPanel1
305
         *
306
         * @return javax.swing.JPanel
307
         */
308
        private JPanel getSliderPanel() {
309
                if (sliderPanel == null) {
310
                        sliderPanel = new JPanel();
311
                        sliderPanel.add(getJSlider());
312
                }
313
                return sliderPanel;
314
        }
315

    
316
        /**
317
         * This method initializes pCenter
318
         *
319
         * @return javax.swing.JPanel
320
         */
321
        private JPanel getCenterPanel() {
322
                if (centerPanel == null) {
323
                        centerPanel = new JPanel();
324
                        centerPanel.setLayout(new BorderLayout());                        
325
                        centerPanel.add(getTablePanel(), java.awt.BorderLayout.CENTER);
326
                        centerPanel.add(getSliderPanel(), java.awt.BorderLayout.WEST);
327
                }
328
                return centerPanel;
329
        }
330
        
331
        private JPanel getTablePanel() {
332
            if (tablePanel == null) {
333
                tablePanel = new JPanel();
334
                tablePanel.setLayout(new BorderLayout());
335
                tablePanel.add(getTable(), java.awt.BorderLayout.CENTER);                
336
                tablePanel.add(getTopPanel(), java.awt.BorderLayout.NORTH);
337
            }
338
            return tablePanel;
339
        }
340

    
341
        /**
342
         * This method initializes jScrollPane
343
         *
344
         * @return javax.swing.JScrollPane
345
         */
346
        private JScrollPane getJScrollPane() {
347
                if (jScrollPane == null) {
348
                        jScrollPane = new JScrollPane();
349
                        jScrollPane.setViewportView(getCenterPanel());
350
                }
351
                return jScrollPane;
352
        }
353
        
354
        public void update(Observable observable, Object notification) {
355
                if (notification instanceof FeatureStoreNotification){
356
                        FeatureStoreNotification featureStoreNotification =
357
                                        (FeatureStoreNotification) notification;
358
                        //If the edition finish the command stack disappears
359
                        String type = featureStoreNotification.getType();
360
                        if (FeatureStoreNotification.AFTER_FINISHEDITING.equals(type) ||
361
                                        FeatureStoreNotification.AFTER_CANCELEDITING.equals(type)){                                                        
362
                                ApplicationLocator.getManager().getUIManager().closeWindow(this);                                
363
                                featureStoreNotification.getSource().deleteObserver(this);
364
                                return;
365
                        }
366
                        //Only repaint if the event is a selection event or an edition event
367
                        if (FeatureStoreNotification.AFTER_INSERT.equals(type) ||
368
                FeatureStoreNotification.AFTER_DELETE.equals(type) ||
369
                FeatureStoreNotification.AFTER_UPDATE.equals(type) ||
370
                FeatureStoreNotification.SELECTION_CHANGE.equals(type) ||
371
                FeatureStoreNotification.AFTER_REDO.equals(type) ||
372
                FeatureStoreNotification.AFTER_UNDO.equals(type)) {                   
373
                            commandRepaint();
374
                        }                        
375
                }
376
        }
377

    
378
        public Object getWindowProfile() {
379
                return WindowInfo.DIALOG_PROFILE;
380
        }
381
}