Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / command / CommandStackDialog.java @ 24986

History | View | Annotate | Download (9.39 KB)

1
package com.iver.cit.gvsig.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.gui.beans.DefaultBean;
21

    
22
import com.iver.andami.PluginServices;
23
import com.iver.andami.ui.mdiManager.IWindowListener;
24
import com.iver.andami.ui.mdiManager.SingletonWindow;
25
import com.iver.andami.ui.mdiManager.WindowInfo;
26
import com.iver.cit.gvsig.AddLayer;
27
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
28
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand;
29
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener;
30
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
31
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand;
32

    
33
public class CommandStackDialog extends DefaultBean implements SingletonWindow, IWindowListener,CommandListener{
34

    
35
        private JTable jTable = null;
36
        private JPanel jPanel = null;
37
        private CommandRecord cr;
38
        private JSlider jSlider = null;
39
        //private int itemCount;
40
        private int lowLimit;
41
        private int currentValue=-1;
42
        private JPanel jPanel1 = null;
43
        protected boolean refreshing;
44
        private JPanel pCenter = null;
45
        private JScrollPane jScrollPane = null;
46
        private static final ImageIcon imodify=PluginServices.getIconTheme()
47
                .get("edition-modify-command");
48
        private static final ImageIcon iadd= PluginServices.getIconTheme()
49
                .get("edition-add-command");
50

    
51
        private static final ImageIcon idel = PluginServices.getIconTheme()
52
                .get("edition-del-command");
53
        
54
        /**
55
         * This is the default constructor
56
         */
57
        public CommandStackDialog() {
58
                super();
59
                //this.cr=cr;
60
                //cr.addCommandListener(this);
61
                initialize();
62
                //System.err.println("Identificaci�n del objeto en constructor = "+ cr.toString());
63
        }
64
        public void setModel(CommandRecord cr1){
65
//                System.err.println("Identificaci�n del objeto en setModel = "+ cr1.toString());
66
                if (this.cr!= null && this.cr.equals(cr1))
67
                        return;
68
                this.cr=cr1;
69
                this.cr.addCommandListener(this);
70
                initTable();
71
                initSlider();
72
                currentValue=cr.getCommandCount()-cr.getUndoCommands().length;
73
            refreshControls();
74
                //refreshSlider(cr.getCommandCount());
75
                refreshScroll();
76

    
77
                //
78
        }
79
        /**
80
         * This method initializes this
81
         *
82
         * @return void
83
         */
84
        private void initialize() {
85
                this.setLayout(new BorderLayout());
86
                this.setSize(328, 229);
87
                this.add(getJPanel(), java.awt.BorderLayout.NORTH);
88
                this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
89
                //refreshScroll();
90
        }
91

    
92
        /**
93
         * This method initializes jList
94
         *
95
         * @return javax.swing.JList
96
         */
97
        private JTable getTable() {
98
                if (jTable == null) {
99
                        jTable = new JTable();
100
                }
101
                return jTable;
102
        }
103
        private void initTable(){
104
                MyModel mymodel=new MyModel(cr);
105
                jTable.setModel(mymodel);
106
                jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
107
                jTable.setSelectionBackground(Color.orange);
108
                jTable.setSelectionForeground(Color.black);
109
                jTable.setShowGrid(false);
110
                jTable.getTableHeader().setBackground(Color.white);
111
                TableColumn tc = jTable.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
                                    if (value instanceof AddRowCommand){
124
                                            label.setIcon(iadd);
125
                                    }else if (value instanceof RemoveRowCommand){
126
                                            label.setIcon(idel);
127
                                    }else{
128
                                            label.setIcon(imodify);
129
                                    }
130
                                 if (CommandStackDialog.this.cr.getPos()<row){
131
                                         label.setBackground(Color.lightGray);
132
                                 }else {
133
                                        /* if (cr.getPos()==row){
134
                                                 label.setBackground(Color.orange);
135
                                         }else{*/
136
                                                 label.setBackground(Color.orange);
137
                                         //}
138
                                 }
139
                                    return label;
140
                              }
141
                        });
142

    
143
                jTable.addMouseListener(new java.awt.event.MouseAdapter() {
144
                        public void mousePressed(java.awt.event.MouseEvent e) {
145
                                int newpos=jTable.getSelectedRow();
146
                                try {
147
                                        CommandStackDialog.this.cr.setPos(newpos);
148
                                        PluginServices.getMainFrame().enableControls();
149
                                } catch (EditionCommandException e1) {
150
                                        e1.printStackTrace();
151
                                }
152
                        }
153
                });
154
        }
155
        /**
156
         * This method initializes jPanel
157
         *
158
         * @return javax.swing.JPanel
159
         */
160
        private JPanel getJPanel() {
161
                if (jPanel == null) {
162
                        jPanel = new JPanel();
163
                }
164
                return jPanel;
165
        }
166

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

    
175
        public Object getWindowModel() {
176
                return "CommandStack";
177
        }
178

    
179
        public void windowActivated() {
180
                this.validateTree();
181
        }
182

    
183
        public void windowClosed() {
184
                // TODO Auto-generated method stub
185

    
186
        }
187

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

    
220
                    }
221
            });
222
            jSlider.addChangeListener(new javax.swing.event.ChangeListener() {
223

    
224

    
225
                                public void stateChanged(javax.swing.event.ChangeEvent e) {
226
                                        int value = (int) (getJSlider().getValue() * cr.getCommandCount() * 0.01);
227
                        try {
228
                            if (!refreshing)
229
                            cr.setPos(cr.getCommandCount()-1-value);
230
                            //System.out.println("setPos = "+(cr.getCommandCount()-1-value));
231
                                        } catch (EditionCommandException e1) {
232
                                                e1.printStackTrace();
233
                                        }
234

    
235
                }
236
                    });
237
            setValue(cr.getCommandCount()-1-cr.getPos(),true);
238
        }
239
    public void setValue(int number, boolean fireEvent) {
240
        if (number < lowLimit)
241
            number = lowLimit;
242
        if (number > cr.getCommandCount())
243
            number = cr.getCommandCount();
244
        if (number != currentValue) {
245
                currentValue = number;
246
                refreshControls();
247
                if (fireEvent)
248
                        callValueChanged(new Integer(currentValue));
249
        }
250
        int selpos=cr.getCommandCount()-1-number;
251
        if (selpos>=0){
252
                getTable().setRowSelectionInterval(selpos,selpos);
253
        } else
254
                        getTable().clearSelection();
255
    }
256
    /**
257
     * Refreshes all the mutable controls in this component.
258
     */
259
    private void refreshControls() {
260
            int normalizedValue = (int) ((currentValue / (float) cr.getCommandCount())*100);
261
                refreshSlider(normalizedValue);
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 (jPanel1 == null) {
280
                        jPanel1 = new JPanel();
281
                        jPanel1.add(getJSlider());
282
                }
283
                return jPanel1;
284
        }
285

    
286

    
287

    
288
        /**
289
         * This method initializes pCenter
290
         *
291
         * @return javax.swing.JPanel
292
         */
293
        private JPanel getPCenter() {
294
                if (pCenter == null) {
295
                        pCenter = new JPanel();
296
                        pCenter.setLayout(new BorderLayout());
297
                        pCenter.add(getTable(), java.awt.BorderLayout.CENTER);
298
                        pCenter.add(getJPanel1(), java.awt.BorderLayout.WEST);
299
                }
300
                return pCenter;
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 commandRefresh() {
317
                commandRepaint();
318

    
319
        }
320
        public Object getWindowProfile() {
321
                return WindowInfo.DIALOG_PROFILE;
322
        }
323
}  //  @jve:decl-index=0:visual-constraint="10,10"