SWT Undo Redo : Undo Redo « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » SWT JFace Eclipse » Undo RedoScreenshots 
SWT Undo Redo

/*
SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic

ISBN: 1932394273

Publisher: Manning
*/


import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ExtendedModifyEvent;
import org.eclipse.swt.custom.ExtendedModifyListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class Ch5Undoable extends Composite {
  private static final int MAX_STACK_SIZE = 25;

  private List undoStack;

  private List redoStack;

  private StyledText styledText;

  public Ch5Undoable(Composite parent) {
    super(parent, SWT.NONE);
    undoStack = new LinkedList();
    redoStack = new LinkedList();
    buildControls();
  }

  private void buildControls() {
    this.setLayout(new FillLayout());
    styledText = new StyledText(this, SWT.MULTI | SWT.V_SCROLL);

    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
      public void modifyText(ExtendedModifyEvent event) {
        String currText = styledText.getText();
        String newText = currText.substring(event.start, event.start
            + event.length);
        if (newText != null && newText.length() 0) {
          if (undoStack.size() == MAX_STACK_SIZE) {
            undoStack.remove(undoStack.size() 1);
          }
          undoStack.add(0, newText);
        }
      }
    });

    styledText.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        switch (e.keyCode) {
        case SWT.F1:
          undo();
          break;
        case SWT.F2:
          redo();
          break;
        default:
        //ignore everything else
        }
      }
    });
  }

  private void undo() {
    if (undoStack.size() 0) {
      String lastEdit = (StringundoStack.remove(0);
      int editLength = lastEdit.length();
      String currText = styledText.getText();
      int startReplaceIndex = currText.length() - editLength;
      styledText.replaceTextRange(startReplaceIndex, editLength, "");
      redoStack.add(0, lastEdit);
    }
  }

  private void redo() {
    if (redoStack.size() 0) {
      String text = (StringredoStack.remove(0);
      moveCursorToEnd();
      styledText.append(text);
      moveCursorToEnd();
    }
  }

  private void moveCursorToEnd() {
    styledText.setCaretOffset(styledText.getText().length());
  }
}

           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.