Revision 37597

View differences:

branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/app/gui/command/CommandStackDialog.java
13 13
import javax.swing.JSlider;
14 14
import javax.swing.JTable;
15 15
import javax.swing.ListSelectionModel;
16
import javax.swing.SwingConstants;
17 16
import javax.swing.table.DefaultTableCellRenderer;
18 17
import javax.swing.table.TableColumn;
19 18

  
......
30 29
import org.gvsig.tools.undo.UndoRedoStack;
31 30

  
32 31

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

  
35 35
	private JTable commandTable = null;
36 36
	private JPanel topPanel = null;
......
143 143

  
144 144
		commandTable.addMouseListener(new java.awt.event.MouseAdapter() {
145 145
			public void mousePressed(java.awt.event.MouseEvent e) {
146
				int newpos=commandTable.getSelectedRow();
147
				commandTableModel.setPos(newpos);
146
				int newpos = commandTable.getSelectedRow();				
147
				commandTableModel.setPos(newpos);				
148 148
				PluginServices.getMainFrame().enableControls();
149 149
			}
150 150
		});
......
178 178
	}
179 179

  
180 180
	public void windowClosed() {
181
		// TODO Auto-generated method stub
182

  
181
		
183 182
	}
184 183

  
185
	/* (non-Javadoc)
186
	 * @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#executeCommand(com.iver.cit.gvsig.fmap.edition.commands.CommandEvent)
187
	 */
188 184
	public void commandRepaint() {
189
		setValue(commandTableModel.getPos()+1,true);
190
		refreshControls();
185
		setValue(commandTableModel.getPos(), true);
191 186
		refreshSliderSize();
192 187
	}
193 188

  
......
195 190
     * Refreshes all the mutable controls in this component.
196 191
     */
197 192
    private void refreshControls() {
198
        int normalizedValue = (int) (((commandTableModel.getRowCount()-currentValue) / (float) commandTableModel.getRowCount())*100);
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
        }
199 198
        refreshSlider(normalizedValue);
200 199
        commandTable.repaint();
201 200
    }
201
    
202 202
    /**
203 203
     * Sets the slider to the correct (scaled) position.
204 204
     * @param normalizedValue
......
207 207
        if (!refreshing){
208 208
            refreshing = true;
209 209
            getJSlider().setValue(normalizedValue);
210
            refreshing = false;
210
            refreshing = false; 
211 211
        }
212 212
    }    
213 213
    
214 214
    private void refreshSliderSize(){
215 215
        if (!refreshing){
216
            Dimension size=new Dimension(commandSlider.getPreferredSize().width,((commandTableModel.getRowCount())*getTable().getRowHeight()));
217
            JScrollBar verticalScrollBar=getJScrollPane().getVerticalScrollBar();//ove(size.width,size.height);
216
            Dimension size = new Dimension(commandSlider.getPreferredSize().width,
217
                ((commandTableModel.getRowCount())*getTable().getRowHeight()));
218
            JScrollBar verticalScrollBar = getJScrollPane().getVerticalScrollBar();
218 219
            verticalScrollBar.setValue(commandTableModel.getPos()*getTable().getRowHeight());
219 220
            commandSlider.setPreferredSize(size);
220 221
            commandSlider.setSize(size);
......
230 231
	 */
231 232
	private JSlider getJSlider() {
232 233
		if (commandSlider == null) {
233
			commandSlider = new JSlider();
234
			commandSlider = new JSlider(JSlider.VERTICAL, 0, 100, 0);			
234 235
		}
235 236
		return commandSlider;
236 237
	}
237 238
	
238
	private void initSlider(){
239
		commandSlider.setOrientation(SwingConstants.VERTICAL);
240
		commandSlider.setPreferredSize(new Dimension(commandSlider.getPreferredSize().width,((getTable().getRowCount())*getTable().getRowHeight())));
241
		commandSlider.addMouseListener(new java.awt.event.MouseAdapter() {
242
                public void mouseReleased(java.awt.event.MouseEvent e) {
239
	private void initSlider(){		
240
		commandSlider.setPreferredSize(
241
		    new Dimension(commandSlider.getPreferredSize().width,
242
		        ((getTable().getRowCount())*getTable().getRowHeight())));
243 243

  
244
            	}
245
            });
246
    	commandSlider.addChangeListener(new javax.swing.event.ChangeListener() {
244
		commandSlider.addChangeListener(new javax.swing.event.ChangeListener() {
247 245
				public void stateChanged(javax.swing.event.ChangeEvent e) {
248
					int value = (int) (getJSlider().getValue() * commandTableModel.getRowCount() * 0.01);
249
                   	if (!refreshing) {
250
                   	    refreshing = true;
251
						commandTableModel.setPos(commandTableModel.getRowCount()-1-value);
252
						refreshing = false;						
253
					}
246
				    if (!refreshing){
247
				        int value = getTablePosFromSlider();                    
248
				        if (currentValue != value){
249
				            refreshing = true;
250
				            commandTableModel.setPos(value);
251
				            refreshing = false;    
252
				        }
253
				    }
254 254
			    }
255 255
    		});
256
    	setValue(commandTableModel.getRowCount()-1-commandTableModel.getPos(),true);
256
    	setValue(commandTableModel.getRowCount() - 1 - commandTableModel.getPos(),true);
257 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
	    //If the slider in in the 100% the previous row has to be selected
267
	    if (value == commandTableModel.getRowCount()){
268
	        value--;
269
	    }
270
	    //The bottom part of the slider starts in 100: take the reverse value
271
	    value = (commandTableModel.getRowCount() - 1) - value;
272
	    
273
	    return value;
274
	}
275
	
258 276
    public void setValue(int number, boolean fireEvent) {
259
        int rowCount=commandTableModel.getRowCount();
277
        int rowCount = commandTableModel.getRowCount();
278
               
260 279
        if (number < lowLimit) {
261 280
			number = lowLimit;
262 281
		}
263
        if (number > rowCount) {
282
        if (number > (rowCount - 1)) {
264 283
			number = rowCount;
265 284
		}
266 285
        if (number != currentValue) {
......
269 288
        	if (fireEvent) {
270 289
				callValueChanged(new Integer(currentValue));
271 290
			}
272
        }
273
        int selpos=rowCount-1-number;
274
        if (selpos>=0){
275
       	 	getTable().setRowSelectionInterval(selpos,selpos);
276
        } else {
277
			getTable().clearSelection();
278
		}
291
        }     
279 292
    }
280 293

  
281 294
	/**
......
331 344
				featureStoreNotification.getSource().deleteObserver(this);
332 345
				return;
333 346
			}
347
			//Only repaint if the event is a selection event or an edition event
348
			if (FeatureStoreNotification.AFTER_INSERT.equals(type) ||
349
                FeatureStoreNotification.AFTER_DELETE.equals(type) ||
350
                FeatureStoreNotification.AFTER_UPDATE.equals(type) ||
351
                FeatureStoreNotification.SELECTION_CHANGE.equals(type)) {                   
352
			    commandRepaint();
353
			}			
334 354
		}
335
		commandRepaint();
336 355
	}
337 356

  
338 357
	public Object getWindowProfile() {
339 358
		return WindowInfo.DIALOG_PROFILE;
340 359
	}
341

  
342
}  //  @jve:decl-index=0:visual-constraint="10,10"
360
}  
branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/app/gui/command/CommandTableModel.java
2 2

  
3 3
import javax.swing.table.AbstractTableModel;
4 4

  
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7

  
5 8
import org.gvsig.tools.undo.RedoException;
6 9
import org.gvsig.tools.undo.UndoException;
7 10
import org.gvsig.tools.undo.UndoRedoStack;
8 11
import org.gvsig.tools.undo.command.Command;
9 12

  
10 13

  
14
@SuppressWarnings("serial")
11 15
public class CommandTableModel extends AbstractTableModel{
12
private UndoRedoStack cr;
13
public CommandTableModel(UndoRedoStack cr) {
14
	this.cr=cr;
16
    private static final Logger LOG = 
17
        LoggerFactory.getLogger(CommandTableModel.class);
18
    private UndoRedoStack cr;
19
    
20
    public CommandTableModel(UndoRedoStack cr) {
21
        this.cr = cr;
15 22
	}
23

  
16 24
	public int getPos() {
17
		return cr.getUndoInfos().size()-1;
25
		return cr.getUndoInfos().size() - 1;
18 26
	}
27
	
19 28
	public int getColumnCount() {
20 29
		return 1;
21 30
	}
31
	
22 32
	public int getRowCount() {
23
		return cr.getRedoInfos().size()+cr.getUndoInfos().size();
33
		return cr.getRedoInfos().size() + cr.getUndoInfos().size();
24 34
	}
25
	public Object getValueAt(int i, int j) {
35
	
36
	@SuppressWarnings("unchecked")
37
    public Object getValueAt(int i, int j) {
26 38
		Command[] undos=(Command[])cr.getUndoInfos().toArray(new Command[0]);
27 39
		Command[] redos=(Command[])cr.getRedoInfos().toArray(new Command[0]);
28 40
		if (i<undos.length){
......
33 45
			return redos[i-undos.length];
34 46
		}
35 47
	}
48
	
36 49
	public void setPos(int newpos) {
37 50
		try {
38
			if (newpos>getPos()){
39
				cr.redo(newpos-getPos());
40

  
41
			}else{
42
				cr.undo(getPos()-newpos);
51
			if (newpos > getPos()) {
52
				cr.redo(newpos - getPos());
53
			}else if (newpos < getPos()) {
54
				cr.undo(getPos() - newpos);
43 55
			}
44 56
		} catch (RedoException e) {
45
			e.printStackTrace();
57
			LOG.error("Error executing the command", e);
46 58
		} catch (UndoException e) {
47
			e.printStackTrace();
59
		    LOG.error("Error executing the command", e);
48 60
		}
49 61
	}
50 62
}

Also available in: Unified diff