Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandStackDialog.java @ 37605

History | View | Annotate | Download (11 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.PluginServices;
20
import org.gvsig.andami.ui.mdiManager.IWindowListener;
21
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
22
import org.gvsig.andami.ui.mdiManager.WindowInfo;
23
import org.gvsig.app.ApplicationLocator;
24
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
25
import org.gvsig.gui.beans.DefaultBean;
26
import org.gvsig.tools.observer.Observable;
27
import org.gvsig.tools.observer.Observer;
28
import org.gvsig.tools.undo.UndoRedoInfo;
29
import org.gvsig.tools.undo.UndoRedoStack;
30

    
31

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

    
35
        private JTable commandTable = null;
36
        private JPanel topPanel = null;
37
        private UndoRedoStack undoRedoStack;
38
        private JSlider commandSlider = null;
39
        
40
        private int lowLimit;
41
        private int currentValue = -1;
42
        private JPanel sliderPanel = null;
43
        protected boolean refreshing;
44
        private JPanel centerPanel = null;
45
        private JScrollPane jScrollPane = null;
46
        private JPanel tablePanel = null;
47
        
48
        private static final ImageIcon imodify = PluginServices.getIconTheme()
49
                .get("edition-modify-command");
50
        private static final ImageIcon iadd = PluginServices.getIconTheme()
51
                .get("edition-add-command");
52
        private static final ImageIcon idel = PluginServices.getIconTheme()
53
                .get("edition-del-command");
54

    
55
        private CommandTableModel commandTableModel = null;
56
        /**
57
         * This is the default constructor
58
         */
59
        public CommandStackDialog() {
60
                super();
61
                initialize();
62
        }
63
        
64
        public void setModel(UndoRedoStack cr1) {
65
        if (this.undoRedoStack != null) {
66
            if (this.undoRedoStack.equals(cr1)) {
67
                return;
68
            } else {
69
                this.undoRedoStack.deleteObserver(this);
70
            }
71
                }
72
                this.undoRedoStack = cr1;
73
                this.undoRedoStack.addObserver(this);
74
                initTable();
75
                initSlider();
76
                currentValue = commandTableModel.getPos();
77
            refreshControls();
78
                refreshSliderSize();
79
        }
80
        
81
        /**
82
         * This method initializes this
83
         *
84
         * @return void
85
         */
86
        private void initialize() {
87
                this.setLayout(new BorderLayout());
88
                this.setSize(328, 229);                
89
                this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
90
        }
91

    
92
        /**
93
         * This method initializes jList
94
         *
95
         * @return javax.swing.JList
96
         */
97
        private JTable getTable() {
98
                if (commandTable == null) {
99
                        commandTable = new JTable();
100
                }
101
                return commandTable;
102
        }
103
        
104
        private void initTable(){
105
                commandTableModel = new CommandTableModel(undoRedoStack);
106
                commandTable.setModel(commandTableModel);
107
                commandTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
108
                commandTable.setSelectionBackground(Color.orange);
109
                commandTable.setSelectionForeground(Color.black);
110
                commandTable.setShowGrid(false);
111
                commandTable.getTableHeader().setBackground(Color.white);
112
                TableColumn tc = commandTable.getColumnModel().getColumn(0);
113
                tc.setCellRenderer(new DefaultTableCellRenderer() {
114
                           public Component getTableCellRendererComponent(JTable table,
115
                                                                       Object value,
116
                                                                       boolean isSelected,
117
                                                                       boolean hasFocus,
118
                                                                       int row,
119
                                                                       int column)
120
                              {
121
                                 JLabel label = (JLabel)
122
                                    super.getTableCellRendererComponent
123
                                       (table, value, isSelected, hasFocus, row, column);
124
                                    UndoRedoInfo info = (UndoRedoInfo) value;
125
                                    switch (info.getType()) {
126
                            case UndoRedoInfo.INSERT:
127
                                label.setIcon(iadd);
128
                                break;
129
                            case UndoRedoInfo.DELETE:
130
                                label.setIcon(idel);
131
                                break;
132
                            default:
133
                                label.setIcon(imodify);
134
                        }
135
                                 if (commandTableModel.getPos()<row){
136
                                         label.setBackground(Color.lightGray);
137
                                 }else {
138
                                                label.setBackground(Color.orange);
139
                                 }
140
                                    return label;
141
                              }
142
                        });
143

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

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

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

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

    
180
        public void windowClosed() {
181
                
182
        }
183

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

    
189
    /**
190
     * Refreshes all the mutable controls in this component.
191
     */
192
    private void refreshControls() {
193
        int normalizedValue = (int) (((commandTableModel.getRowCount() - (currentValue + 1)) / (float)commandTableModel.getRowCount()) * 100);
194
        //Adding the 50% od the interval
195
        if (commandTableModel.getRowCount() > 0){
196
            normalizedValue = normalizedValue + (100 / (commandTableModel.getRowCount() * 2));
197
        }
198
        refreshSlider(normalizedValue);
199
        commandTable.repaint();
200
    }
201
    
202
    /**
203
     * Sets the slider to the correct (scaled) position.
204
     * @param normalizedValue
205
     */
206
    private void refreshSlider(int normalizedValue) {
207
        if (!refreshing){
208
            refreshing = true;
209
            getJSlider().setValue(normalizedValue);
210
            refreshing = false; 
211
        }
212
    }    
213
    
214
    private void refreshSliderSize(){
215
        if (!refreshing){
216
            Dimension size = new Dimension(commandSlider.getPreferredSize().width,
217
                ((commandTableModel.getRowCount() + 1) * getTable().getRowHeight()));
218
            JScrollBar verticalScrollBar = getJScrollPane().getVerticalScrollBar();
219
            verticalScrollBar.setValue(commandTableModel.getPos()*getTable().getRowHeight());
220
            commandSlider.setPreferredSize(size);
221
            commandSlider.setSize(size);
222
            validateTree();
223
        }
224
    }
225
    
226
        
227
        /**
228
         * This method initializes jSlider
229
         *
230
         * @return javax.swing.JSlider
231
         */
232
        private JSlider getJSlider() {
233
                if (commandSlider == null) {
234
                        commandSlider = new JSlider(JSlider.VERTICAL, 0, 100, 0);        
235
                }
236
                return commandSlider;
237
        }
238
        
239
        private void initSlider(){                
240
                commandSlider.setPreferredSize(
241
                    new Dimension(commandSlider.getPreferredSize().width,
242
                        ((getTable().getRowCount())*getTable().getRowHeight())));
243

    
244
                commandSlider.addChangeListener(new javax.swing.event.ChangeListener() {
245
                                public void stateChanged(javax.swing.event.ChangeEvent e) {
246
                                    if (!refreshing){
247
                                        int value = getTablePosFromSlider();                    
248
                                        if (currentValue != value){
249
                                            refreshing = true;
250
                                            commandTableModel.setPos(value);
251
                                            refreshing = false;    
252
                                        }
253
                                    }
254
                            }
255
                    });
256
            setValue(commandTableModel.getRowCount() - 1 - commandTableModel.getPos(),true);
257
        }
258
        
259
        private int getTablePosFromSlider(){          
260
            if (commandTableModel.getRowCount() == 0){
261
                return -1;
262
            }
263
            
264
            int value = getJSlider().getValue();
265
            value = (int) ((value * 0.01) * commandTableModel.getRowCount());
266

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

    
291
        /**
292
         * This method initializes jPanel1
293
         *
294
         * @return javax.swing.JPanel
295
         */
296
        private JPanel getSliderPanel() {
297
                if (sliderPanel == null) {
298
                        sliderPanel = new JPanel();
299
                        sliderPanel.add(getJSlider());
300
                }
301
                return sliderPanel;
302
        }
303

    
304
        /**
305
         * This method initializes pCenter
306
         *
307
         * @return javax.swing.JPanel
308
         */
309
        private JPanel getCenterPanel() {
310
                if (centerPanel == null) {
311
                        centerPanel = new JPanel();
312
                        centerPanel.setLayout(new BorderLayout());                        
313
                        centerPanel.add(getTablePanel(), java.awt.BorderLayout.CENTER);
314
                        centerPanel.add(getSliderPanel(), java.awt.BorderLayout.WEST);
315
                }
316
                return centerPanel;
317
        }
318
        
319
        private JPanel getTablePanel() {
320
            if (tablePanel == null) {
321
                tablePanel = new JPanel();
322
                tablePanel.setLayout(new BorderLayout());
323
                tablePanel.add(getTable(), java.awt.BorderLayout.CENTER);                
324
                tablePanel.add(getTopPanel(), java.awt.BorderLayout.NORTH);
325
            }
326
            return tablePanel;
327
        }
328

    
329
        /**
330
         * This method initializes jScrollPane
331
         *
332
         * @return javax.swing.JScrollPane
333
         */
334
        private JScrollPane getJScrollPane() {
335
                if (jScrollPane == null) {
336
                        jScrollPane = new JScrollPane();
337
                        jScrollPane.setViewportView(getCenterPanel());
338
                }
339
                return jScrollPane;
340
        }
341
        
342
        public void update(Observable observable, Object notification) {
343
                if (notification instanceof FeatureStoreNotification){
344
                        FeatureStoreNotification featureStoreNotification =
345
                                        (FeatureStoreNotification) notification;
346
                        //If the edition finish the command stack disappears
347
                        String type = featureStoreNotification.getType();
348
                        if (FeatureStoreNotification.AFTER_FINISHEDITING.equals(type) ||
349
                                        FeatureStoreNotification.AFTER_CANCELEDITING.equals(type)){                                                        
350
                                ApplicationLocator.getManager().getUIManager().closeWindow(this);                                
351
                                featureStoreNotification.getSource().deleteObserver(this);
352
                                return;
353
                        }
354
                        //Only repaint if the event is a selection event or an edition event
355
                        if (FeatureStoreNotification.AFTER_INSERT.equals(type) ||
356
                FeatureStoreNotification.AFTER_DELETE.equals(type) ||
357
                FeatureStoreNotification.AFTER_UPDATE.equals(type) ||
358
                FeatureStoreNotification.SELECTION_CHANGE.equals(type)) {                   
359
                            commandRepaint();
360
                        }                        
361
                }
362
        }
363

    
364
        public Object getWindowProfile() {
365
                return WindowInfo.DIALOG_PROFILE;
366
        }
367
}