Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / command / CommandStackDialog.java @ 38562

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