Statistics
| Revision:

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

History | View | Annotate | Download (10.2 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.SwingConstants;
17
import javax.swing.table.DefaultTableCellRenderer;
18
import javax.swing.table.TableColumn;
19

    
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
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 commandTablePanel = null;
45
        private JScrollPane jScrollPane = null;
46
        
47
        private static final ImageIcon imodify = PluginServices.getIconTheme()
48
                .get("edition-modify-command");
49
        private static final ImageIcon iadd = PluginServices.getIconTheme()
50
                .get("edition-add-command");
51
        private static final ImageIcon idel = PluginServices.getIconTheme()
52
                .get("edition-del-command");
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
                refreshScroll();
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(getJPanel(), java.awt.BorderLayout.NORTH);
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 getJPanel() {
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
                // TODO Auto-generated method stub
182

    
183
        }
184

    
185
        /* (non-Javadoc)
186
         * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#executeCommand(com.iver.cit.gvsig.fmap.edition.commands.CommandEvent)
187
         */
188
        public void commandRepaint() {
189
                setValue(commandTableModel.getPos()+1,true);
190
                refreshControls();
191
                refreshScroll();
192
        }
193
        private void refreshScroll(){
194
                Dimension size=new Dimension(commandSlider.getPreferredSize().width,((commandTableModel.getRowCount())*getTable().getRowHeight()));
195
                JScrollBar verticalScrollBar=getJScrollPane().getVerticalScrollBar();//ove(size.width,size.height);
196
                verticalScrollBar.setValue(commandTableModel.getPos()*getTable().getRowHeight());
197
                commandSlider.setPreferredSize(size);
198
                commandSlider.setSize(size);
199
                validateTree();
200
        }
201
        
202
        /**
203
         * This method initializes jSlider
204
         *
205
         * @return javax.swing.JSlider
206
         */
207
        private JSlider getJSlider() {
208
                if (commandSlider == null) {
209
                        commandSlider = new JSlider();
210
                }
211
                return commandSlider;
212
        }
213
        
214
        private void initSlider(){
215
                commandSlider.setOrientation(SwingConstants.VERTICAL);
216
                commandSlider.setPreferredSize(new Dimension(commandSlider.getPreferredSize().width,((getTable().getRowCount())*getTable().getRowHeight())));
217
                commandSlider.addMouseListener(new java.awt.event.MouseAdapter() {
218
                public void mouseReleased(java.awt.event.MouseEvent e) {
219

    
220
                    }
221
            });
222
            commandSlider.addChangeListener(new javax.swing.event.ChangeListener() {
223
                                public void stateChanged(javax.swing.event.ChangeEvent e) {
224
                                        int value = (int) (getJSlider().getValue() * commandTableModel.getRowCount() * 0.01);
225
                           if (!refreshing) {
226
                                                commandTableModel.setPos(commandTableModel.getRowCount()-1-value);
227
                                                //System.out.println("setPos = "+(cr.getCommandCount()-1-value));
228
                                        }
229
                            }
230
                    });
231
            setValue(commandTableModel.getRowCount()-1-commandTableModel.getPos(),true);
232
        }
233
    public void setValue(int number, boolean fireEvent) {
234
        int rowCount=commandTableModel.getRowCount();
235
        if (number < lowLimit) {
236
                        number = lowLimit;
237
                }
238
        if (number > rowCount) {
239
                        number = rowCount;
240
                }
241
        if (number != currentValue) {
242
                currentValue = number;
243
                refreshControls();
244
                if (fireEvent) {
245
                                callValueChanged(new Integer(currentValue));
246
                        }
247
        }
248
        int selpos=rowCount-1-number;
249
        if (selpos>=0){
250
                        getTable().setRowSelectionInterval(selpos,selpos);
251
        } else {
252
                        getTable().clearSelection();
253
                }
254
    }
255
    /**
256
     * Refreshes all the mutable controls in this component.
257
     */
258
    private void refreshControls() {
259
            int normalizedValue = (int) (((commandTableModel.getRowCount()-currentValue) / (float) commandTableModel.getRowCount())*100);
260
                refreshSlider(normalizedValue);
261
                commandTable.repaint();
262
        }
263
    /**
264
         * Sets the slider to the correct (scaled) position.
265
     * @param normalizedValue
266
     */
267
    private void refreshSlider(int normalizedValue) {
268
            refreshing = true;
269
        getJSlider().setValue(normalizedValue);
270
        refreshing = false;
271
    }
272

    
273
        /**
274
         * This method initializes jPanel1
275
         *
276
         * @return javax.swing.JPanel
277
         */
278
        private JPanel getJPanel1() {
279
                if (sliderPanel == null) {
280
                        sliderPanel = new JPanel();
281
                        sliderPanel.add(getJSlider());
282
                }
283
                return sliderPanel;
284
        }
285

    
286

    
287

    
288
        /**
289
         * This method initializes pCenter
290
         *
291
         * @return javax.swing.JPanel
292
         */
293
        private JPanel getPCenter() {
294
                if (commandTablePanel == null) {
295
                        commandTablePanel = new JPanel();
296
                        commandTablePanel.setLayout(new BorderLayout());
297
                        commandTablePanel.add(getTable(), java.awt.BorderLayout.CENTER);
298
                        commandTablePanel.add(getJPanel1(), java.awt.BorderLayout.WEST);
299
                }
300
                return commandTablePanel;
301
        }
302

    
303
        /**
304
         * This method initializes jScrollPane
305
         *
306
         * @return javax.swing.JScrollPane
307
         */
308
        private JScrollPane getJScrollPane() {
309
                if (jScrollPane == null) {
310
                        jScrollPane = new JScrollPane();
311
                        jScrollPane.setViewportView(getPCenter());
312
                }
313
                return jScrollPane;
314
        }
315
        
316
        public void update(Observable observable, Object notification) {
317
                if (notification instanceof FeatureStoreNotification){
318
                        FeatureStoreNotification featureStoreNotification =
319
                                        (FeatureStoreNotification) notification;
320
                        //If the edition finish the command stack disappears
321
                        String type = featureStoreNotification.getType();
322
                        if (FeatureStoreNotification.AFTER_FINISHEDITING.equals(type) ||
323
                                        FeatureStoreNotification.AFTER_CANCELEDITING.equals(type)){                                                        
324
                                ApplicationLocator.getManager().getUIManager().closeWindow(this);                                
325
                                featureStoreNotification.getSource().deleteObserver(this);
326
                                return;
327
                        }
328
                }
329
            commandRepaint();
330
        }
331

    
332
        public Object getWindowProfile() {
333
                return WindowInfo.DIALOG_PROFILE;
334
        }
335

    
336
}  //  @jve:decl-index=0:visual-constraint="10,10"