Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / command / CommandStackDialog.java @ 10626

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 = new ImageIcon(AddLayer.class.getClassLoader()
47
                        .getResource("images/ModifyCommand.png"));
48
        private static final ImageIcon iadd = new ImageIcon(AddLayer.class.getClassLoader()
49
                        .getResource("images/AddCommand.png"));
50

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

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

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

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

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

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

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

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

    
185
        }
186

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

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

    
223

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

    
234
                }
235
                    });
236
            setValue(cr.getCommandCount()-1-cr.getPos(),true);
237
        }
238
    public void setValue(int number, boolean fireEvent) {
239
        if (number < lowLimit)
240
            number = lowLimit;
241
        if (number > cr.getCommandCount())
242
            number = cr.getCommandCount();
243
        if (number != currentValue) {
244
                currentValue = number;
245
                refreshControls();
246
                if (fireEvent)
247
                        callValueChanged(new Integer(currentValue));
248
        }
249
        int selpos=cr.getCommandCount()-1-number;
250
        if (selpos>=0){
251
                getTable().setRowSelectionInterval(selpos,selpos);
252
        } else
253
                        getTable().clearSelection();
254
    }
255
    /**
256
     * Refreshes all the mutable controls in this component.
257
     */
258
    private void refreshControls() {
259
            int normalizedValue = (int) ((currentValue / (float) cr.getCommandCount())*100);
260
                refreshSlider(normalizedValue);
261
        }
262
    /**
263
         * Sets the slider to the correct (scaled) position.
264
     * @param normalizedValue
265
     */
266
    private void refreshSlider(int normalizedValue) {
267
            refreshing = true;
268
        getJSlider().setValue(normalizedValue);
269
        refreshing = false;
270
    }
271

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

    
285

    
286

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

    
302
        /**
303
         * This method initializes jScrollPane
304
         *
305
         * @return javax.swing.JScrollPane
306
         */
307
        private JScrollPane getJScrollPane() {
308
                if (jScrollPane == null) {
309
                        jScrollPane = new JScrollPane();
310
                        jScrollPane.setViewportView(getPCenter());
311
                }
312
                return jScrollPane;
313
        }
314

    
315
        public void commandRefresh() {
316
                commandRepaint();
317

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