Statistics
| Revision:

root / tags / v2_0_0_Build_2050 / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandStackDialog.java @ 38653

History | View | Annotate | Download (11.5 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("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

    
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
                                    PluginServices.getMainFrame().enableControls();
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
                                            refreshing = false;    
256
                                        }
257
                                    }
258
                            }
259
                    });
260
            setValue(commandTableModel.getRowCount() - 1 - commandTableModel.getPos(),true);
261
        }
262
        
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

    
271
            //The bottom part of the slider starts in 100: take the reverse value
272
            value = (commandTableModel.getRowCount() - 1) - value;
273
            
274
            if (value == -1){
275
                return 0;
276
            }
277
            return value;
278
        }
279
        
280
    public void setValue(int number, boolean fireEvent) {
281
        int rowCount = commandTableModel.getRowCount();
282
               
283
        if (number < lowLimit) {
284
                        number = lowLimit;
285
                }
286
        if (number > (rowCount - 1)) {
287
                        number = rowCount;
288
                }
289
        if (number != currentValue) {
290
                currentValue = number;
291
                refreshControls();
292
                if (fireEvent) {
293
                                callValueChanged(new Integer(currentValue));
294
                        }
295
        }     
296
    }
297

    
298
        /**
299
         * This method initializes jPanel1
300
         *
301
         * @return javax.swing.JPanel
302
         */
303
        private JPanel getSliderPanel() {
304
                if (sliderPanel == null) {
305
                        sliderPanel = new JPanel();
306
                        sliderPanel.add(getJSlider());
307
                }
308
                return sliderPanel;
309
        }
310

    
311
        /**
312
         * This method initializes pCenter
313
         *
314
         * @return javax.swing.JPanel
315
         */
316
        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
                }
323
                return centerPanel;
324
        }
325
        
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

    
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
                        jScrollPane.setViewportView(getCenterPanel());
345
                }
346
                return jScrollPane;
347
        }
348
        
349
        public void update(Observable observable, Object notification) {
350
                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
                        //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
                FeatureStoreNotification.SELECTION_CHANGE.equals(type) ||
366
                FeatureStoreNotification.AFTER_REDO.equals(type) ||
367
                FeatureStoreNotification.AFTER_UNDO.equals(type)) {                   
368
                            commandRepaint();
369
                        }                        
370
                }
371
        }
372

    
373
        public Object getWindowProfile() {
374
                return WindowInfo.DIALOG_PROFILE;
375
        }
376
}