Example usage for javax.swing.undo UndoManager canRedo

List of usage examples for javax.swing.undo UndoManager canRedo

Introduction

In this page you can find the example usage for javax.swing.undo UndoManager canRedo.

Prototype

public synchronized boolean canRedo() 

Source Link

Document

Returns true if edits may be redone.

Usage

From source file:org.pmedv.blackboard.commands.AddItemCommand.java

@Override
public void execute(ActionEvent e) {

    if (item == null)
        return;/*w  w w  .j a v  a2  s .  c  o m*/

    boolean split = false;

    removals.clear();
    addables.clear();

    ApplicationContext ctx = AppContext.getContext();
    BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    BoardEditorModel model = editor.getModel();
    UndoManager undoManager = editor.getUndoManager();

    final ShapePropertiesPanel shapesPanel = ctx.getBean(ShapePropertiesPanel.class);
    item.setLayer(model.getCurrentLayer().getIndex());

    Boolean useLayerColor = (Boolean) Preferences.values
            .get("org.pmedv.blackboard.BoardDesignerPerspective.useLayerColor");

    if (useLayerColor) {
        item.setColor(model.getCurrentLayer().getColor());
    } else {
        item.setColor(ctx.getBean(Palette.class).getCurrentColor());
    }

    int maxIndex = EditorUtils.getFreeIndex(model);
    item.setIndex(maxIndex);

    if (item instanceof Line) {

        Line line = (Line) item;

        // Line has to be larger than the current raster
        if (line.getLength() >= editor.getRaster()) {
            // we do not allow double connections
            if (model.getCurrentLayer().getItems().contains(line)) {
                ErrorUtils.showErrorDialog(
                        new IllegalStateException(resources.getResourceByKey("msg.connection.alreadyexists")));
                return;
            }
        }

        line.setStartType((LineEdgeType) shapesPanel.getStartLineCombo().getSelectedItem());
        line.setEndType((LineEdgeType) shapesPanel.getEndLineCombo().getSelectedItem());

        split = checkSplits(editor, line);

        line.setStroke((BasicStroke) shapesPanel.getThicknessCombo().getSelectedItem());

    } else if (item instanceof Shape) {
        Shape s = (Shape) item;
        s.setStroke((BasicStroke) shapesPanel.getThicknessCombo().getSelectedItem());
        s.setStyle((ShapeStyle) shapesPanel.getStyleCombo().getSelectedItem());
    }

    log.debug("Adding item to layer " + model.getCurrentLayer());

    model.getCurrentLayer().getItems().add(item);
    removePending(model);

    if (!split) {
        if (!undoManager.addEdit(new AddItemEdit(item))) {
            log.info("Could not add edit " + this.getClass());
        }
    }

    editor.setFileState(FileState.DIRTY);

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}

From source file:org.pmedv.blackboard.commands.AddLineCommand.java

@Override
public void execute(ActionEvent e) {

    if (line == null)
        return;/* w  w w  .j  a va 2  s  . c  om*/

    final ApplicationContext ctx = AppContext.getContext();
    final BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    final UndoManager undoManager = editor.getUndoManager();

    line.setLayer(model.getCurrentLayer().getIndex());

    if (line.getLength() >= editor.getRaster()) {
        if (model.getCurrentLayer().getItems().contains(line)) {
            JOptionPane.showMessageDialog(AppContext.getContext().getBean(ApplicationWindow.class),
                    resources.getResourceByKey("msg.connectionexists"));
        } else {

            final ShapePropertiesPanel shapesPanel = ctx.getBean(ShapePropertiesPanel.class);

            line.setStartType((LineEdgeType) shapesPanel.getStartLineCombo().getSelectedItem());
            line.setEndType((LineEdgeType) shapesPanel.getEndLineCombo().getSelectedItem());

            for (Layer layer : model.getLayers()) {

                for (Item item : layer.getItems()) {
                    if (item instanceof Line) {

                        Line l = (Line) item;

                        if (l.containsPoint(line.getStart(), 1)) {
                            line.setStartType(LineEdgeType.ROUND_DOT);
                        }
                        if (l.containsPoint(line.getEnd(), 1)) {
                            line.setEndType(LineEdgeType.ROUND_DOT);
                        }

                    }
                }

            }

            line.setStroke((BasicStroke) shapesPanel.getThicknessCombo().getSelectedItem());

            final Boolean useLayerColor = (Boolean) Preferences.values
                    .get("org.pmedv.blackboard.BoardDesignerPerspective.useLayerColor");

            if (useLayerColor) {
                line.setColor(model.getCurrentLayer().getColor());
            } else {
                line.setColor(ctx.getBean(Palette.class).getCurrentColor());
            }

            log.info("Adding line to layer " + model.getCurrentLayer());
            model.getCurrentLayer().getItems().add(line);

            if (!undoManager.addEdit(new AddLineEdit(line))) {
                log.debug("Could not add edit " + this.getClass());
            }
            editor.setFileState(FileState.DIRTY);
        }
    }

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}

From source file:org.pmedv.blackboard.commands.AddResistorCommand.java

@Override
public void execute(ActionEvent e) {

    final ApplicationContext ctx = AppContext.getContext();
    final String title = resources.getResourceByKey("AddResistorCommand.name");
    final String subTitle = resources.getResourceByKey("AddResistorCommand.dialog.subtitle");
    final ImageIcon icon = resources.getIcon("icon.dialog.resistor");

    final ResistorDialog dlg = new ResistorDialog(title, subTitle, icon, null);
    dlg.setVisible(true);/*from  w  w  w .ja  v  a  2  s. c  om*/

    if (dlg.getResult() == AbstractNiceDialog.OPTION_CANCEL)
        return;

    final BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    final Resistor resistor = dlg.getResistor();

    // now get a new index
    int max = 0;
    for (Layer layer : editor.getModel().getLayers()) {
        for (Item item : layer.getItems()) {
            if (item.getIndex() > max)
                max = item.getIndex();
        }
    }
    max++;
    resistor.setIndex(max);

    // Check if the default part layer exists

    boolean onPartLayer = false;

    for (Layer layer : editor.getModel().getLayers()) {
        if (layer.getIndex() == BoardEditorModel.PART_LAYER) {
            resistor.setLayer(BoardEditorModel.PART_LAYER);
            onPartLayer = true;
            break;
        }
    }

    if (!onPartLayer) {
        resistor.setLayer(editor.getModel().getCurrentLayer().getIndex());
    }

    editor.getModel().getLayer(resistor.getLayer()).getItems().add(resistor);

    final UndoManager undoManager = editor.getUndoManager();
    if (!undoManager.addEdit(new AddResistorEdit(resistor))) {
        log.error("could not add edit to undo manager");
    }

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

    editor.setFileState(FileState.DIRTY);
    editor.updateStatusBar();
    editor.refresh();
}

From source file:org.pmedv.blackboard.commands.AddTextCommand.java

@Override
public void execute(ActionEvent e) {

    final ApplicationContext ctx = AppContext.getContext();
    final BoardEditor editor = EditorUtils.getCurrentActiveEditor();

    final String title = resources.getResourceByKey("AddTextCommand.name");
    final String subTitle = resources.getResourceByKey("AddTextCommand.dialog.subtitle");
    final ImageIcon icon = resources.getIcon("icon.dialog.text");

    int x = editor.getModel().getWidth() / 2;
    int y = editor.getModel().getHeight() / 2;

    if (null != e && e.getSource().getClass().getName().startsWith("javax.swing.JPopupMenu")) {
        x = mouseX;/*from   ww w . j ava 2 s .co  m*/
        y = mouseY;
    }

    final TextPart p = new TextPart(resources.getResourceByKey("AddTextCommand.defaultText"), lastSelectedFont,
            Color.BLACK, x, y, 0);

    final TextPropertiesDialog dlg = new TextPropertiesDialog(title, subTitle, icon, p);
    dlg.setVisible(true);

    if (dlg.getResult() == AbstractNiceDialog.OPTION_CANCEL)
        return;

    // preserve font properties
    lastSelectedFont = p.getFont();
    lastFontSize = dlg.getFontSize();

    // now get a new index      
    int max = 0;

    for (Layer layer : editor.getModel().getLayers()) {
        for (Item item : layer.getItems()) {
            if (item.getIndex() > max)
                max = item.getIndex();
        }
    }

    max++;
    p.setIndex(max);

    final Layer currentLayer = (Layer) ctx.getBean(ShowLayersCommand.class).getLayerPanel()
            .getCurrentLayerCombo().getSelectedItem();
    editor.getModel().getLayer(currentLayer.getIndex()).getItems().add(p);

    final UndoManager undoManager = editor.getUndoManager();

    if (!undoManager.addEdit(new AddTextEdit(p))) {
        log.error("could not add edit to undo manager");
    }

    editor.setFileState(FileState.DIRTY);
    editor.updateStatusBar();

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}

From source file:org.pmedv.blackboard.commands.ConvertToSymbolCommand.java

@Override
public void execute(ActionEvent e) {

    // which editor do we have?

    final BoardEditor editor = EditorUtils.getCurrentActiveEditor();

    final List<Item> items = editor.getSelectedItems();
    final List<Item> preserveItems = new ArrayList<Item>();

    for (Item item : items) {
        if (item instanceof Symbol || item instanceof Resistor || item instanceof Diode) {
            ErrorUtils/* w  w  w . j  a  va2  s .  c  o  m*/
                    .showErrorDialog(new IllegalStateException(resources.getResourceByKey("error.onlyshapes")));
            return;
        }
    }

    // first we need to check all items and determine min_x, min_y, max_x, max_y as well as height and width

    int min_x = Integer.MAX_VALUE;
    int min_y = Integer.MAX_VALUE;
    int max_x = Integer.MIN_VALUE;
    int max_y = Integer.MIN_VALUE;

    for (Item item : items) {

        if (item instanceof Line) {

            final Line line = (Line) item;
            final Rectangle boundingBox = line.getBoundingBox();

            int x1 = (int) boundingBox.getLocation().getX();
            int y1 = (int) boundingBox.getLocation().getY();
            int x2 = x1 + (int) boundingBox.getWidth();
            int y2 = y1 + (int) boundingBox.getHeight();

            if (x1 < min_x)
                min_x = x1;
            if (y1 < min_y)
                min_y = y1;
            if (x2 > max_x)
                max_x = x2;
            if (y2 > max_y)
                max_y = y2;
        } else {
            if (item.getXLoc() + item.getWidth() > max_x)
                max_x = item.getXLoc() + item.getWidth();
            if (item.getYLoc() + item.getHeight() > max_y)
                max_y = item.getYLoc() + item.getHeight();
            if (item.getXLoc() < min_x)
                min_x = item.getXLoc();
            if (item.getYLoc() < min_y)
                min_y = item.getYLoc();
        }

    }

    int width = max_x - min_x;
    int height = max_y - min_y;

    final Symbol symbol = new Symbol();
    symbol.setWidth(width);
    symbol.setHeight(height);

    // sort items by z-index
    Collections.sort(items);

    symbol.setLayer(items.get(0).getLayer());

    for (final Item item : items) {

        // preserve old position and set new position and render, since we need to convert the coordinates
        // of the item to the newly calculated drawing area

        if (item instanceof Line) {

            final Line line = (Line) item;

            line.getOldstart().setLocation(line.getStart().getX(), line.getStart().getY());
            line.getOldEnd().setLocation(line.getEnd().getX(), line.getEnd().getY());

            line.getStart().setLocation(line.getStart().getX() - min_x, line.getStart().getY() - min_y);
            line.getEnd().setLocation(line.getEnd().getX() - min_x, line.getEnd().getY() - min_y);
        } else {

            item.setOldXLoc(item.getXLoc());
            item.setOldYLoc(item.getYLoc());

            item.setXLoc(item.getXLoc() - min_x);
            item.setYLoc(item.getYLoc() - min_y);
        }

        item.setOpacity(item.getOpacity());

        // restore position
        if (item instanceof Line) {

            final Line line = (Line) item;

            line.getStart().setLocation(line.getOldstart().getX(), line.getOldstart().getY());
            line.getEnd().setLocation(line.getOldEnd().getX(), line.getOldEnd().getY());
        } else {
            item.setXLoc(item.getOldXLoc());
            item.setYLoc(item.getOldYLoc());
        }

        symbol.getItems().add(item);

    }

    // now get a new index      
    int max = 0;

    for (final Layer layer : editor.getModel().getLayers()) {
        for (Item item : layer.getItems()) {
            if (item.getIndex() > max)
                max = item.getIndex();
        }
    }

    max++;
    symbol.setIndex(max);

    // remove original items and replace with created symbol 

    if (editor.getSelectedItems().size() > 1) {
        editor.clearSelectionBorder();

        preserveItems.addAll(editor.getSelectedItems());

        for (Item item : editor.getSelectedItems()) {
            for (Layer layer : editor.getModel().getLayers())
                layer.getItems().remove(item);
        }

        editor.getSelectedItems().clear();

        symbol.setXLoc(min_x);
        symbol.setYLoc(min_y);

        editor.getModel().getCurrentLayer().getItems().add(symbol);
        editor.setSelectedItem(symbol);

        UndoManager undoManager = editor.getUndoManager();
        if (!undoManager.addEdit(new ConvertToSymbolEdit(preserveItems, symbol))) {
            log.info("Could not add edit " + this.getClass());
        }
        editor.setFileState(FileState.DIRTY);

        AppContext.getContext().getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
        AppContext.getContext().getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

        editor.refresh();
    }

}

From source file:org.pmedv.blackboard.commands.DeleteCommand.java

@Override
public void execute(ActionEvent e) {

    ArrayList<Item> deletedItems = new ArrayList<Item>();

    ApplicationContext ctx = AppContext.getContext();
    BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    UndoManager undoManager = editor.getUndoManager();

    try {//from w w  w. j  a v a 2s  . c  om

        if (editor.getSelectedItems().size() >= 1) {
            editor.clearSelectionBorder();
            for (Item item : editor.getSelectedItems()) {
                deletedItems.add(item);
                for (Layer layer : editor.getModel().getLayers())
                    layer.getItems().remove(item);
            }
            editor.getSelectedItems().clear();
            editor.refresh();
        } else {
            if (editor.getSelectedItem() == null)
                return;
            editor.clearSelectionBorder();
            deletedItems.add(editor.getSelectedItem());
            for (Layer layer : editor.getModel().getLayers())
                layer.getItems().remove(editor.getSelectedItem());
            editor.setSelectedItem(null);
            editor.refresh();
        }

        editor.setFileState(FileState.DIRTY);

        log.info("Deleting " + deletedItems.size() + " items.");

        DeleteEdit de = new DeleteEdit(deletedItems);

        if (!undoManager.addEdit(de)) {
            log.error("could not add edit to undo manager");
        }

    } catch (Exception e1) {
        ErrorUtils.showErrorDialog(e1);
    }

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}

From source file:org.pmedv.blackboard.commands.PasteCommand.java

@Override
public void execute(ActionEvent e) {

    BoardEditor editor = EditorUtils.getCurrentActiveEditor();

    List<Item> items = AppContext.getClipboard().paste();

    editor.clearSelectionBorder();//from  ww  w  .j a v  a  2 s.  co  m
    editor.getSelectedItems().clear();
    editor.setSelectedItem(null);

    ArrayList<Item> pastedItems = new ArrayList<Item>();

    for (Item item : items) {

        try {
            Item i = (Item) item.clone();

            int freeIndex = EditorUtils.getFreeIndex(editor.getModel());
            i.setIndex(freeIndex);

            editor.getSelectedItems().add(i);

            if (i instanceof Line) {
                Line line = (Line) i;
                line.setStart(new Point((int) line.getStart().getX() + 32, (int) line.getStart().getY() + 32));
                line.setEnd(new Point((int) line.getEnd().getX() + 32, (int) line.getEnd().getY() + 32));
                line.setOldstart(new Point(line.getStart()));
                line.setOldEnd(new Point(line.getEnd()));
            } else {
                i.setXLoc(i.getXLoc() + 32);
                i.setYLoc(i.getYLoc() + 32);
                i.setOldXLoc(i.getXLoc());
                i.setOldYLoc(i.getYLoc());
                i.setOldWidth(i.getWidth());
                i.setOldHeight(i.getHeight());
            }

            editor.getModel().getLayer(i.getLayer()).getItems().add(i);

            if (i instanceof Resistor) {
                String designator = BoardUtil.getNextFreeDesignator(editor.getModel(), "R");
                i.setName(designator);
            } else if (i instanceof Part && !(i instanceof Symbol)) {
                Part part = (Part) i;
                String designator = BoardUtil.getNextFreeDesignator(editor.getModel(), part.getDesignator());
                i.setName(designator);
            }

            if (i instanceof Symbol) {

                Symbol symbol = (Symbol) i;

                String designator = "X";

                if (null != symbol.getName() && symbol.getName().length() > 1) {

                    designator = String.valueOf(symbol.getName().charAt(0));

                    if (!symbol.getName().equalsIgnoreCase("gnd")) {
                        String name = BoardUtil.getNextFreeDesignator(editor.getModel(), designator);
                        symbol.setName(name);
                    }
                }

                for (Item subItem : symbol.getItems()) {

                    if (subItem instanceof Line) {
                        Line line = (Line) subItem;
                        line.setStart(new Point((int) line.getStart().getX() + 32,
                                (int) line.getStart().getY() + 32));
                        line.setEnd(
                                new Point((int) line.getEnd().getX() + 32, (int) line.getEnd().getY() + 32));
                        line.setOldstart(new Point(line.getStart()));
                        line.setOldEnd(new Point(line.getEnd()));
                    } else {
                        subItem.setXLoc(subItem.getXLoc() + 32);
                        subItem.setYLoc(subItem.getYLoc() + 32);
                        subItem.setOldXLoc(subItem.getXLoc());
                        subItem.setOldYLoc(subItem.getYLoc());
                        subItem.setOldWidth(subItem.getWidth());
                        subItem.setOldHeight(subItem.getHeight());
                    }

                    if (subItem instanceof TextPart) {

                        TextPart text = (TextPart) subItem;

                        if (text.getType() != null) {

                            if (null != symbol.getName() && text.getType().equals(TextType.NAME)) {
                                text.setText(symbol.getName());
                            }
                            if (null != symbol.getValue() && text.getType().equals(TextType.VALUE)) {
                                text.setText(symbol.getValue());
                            }

                        }

                    }

                }

            }
            pastedItems.add(i);
        } catch (CloneNotSupportedException e1) {
            ErrorUtils.showErrorDialog(e1);
        }

    }

    // Switch to select after paste
    AppContext.getContext().getBean(SetSelectModeCommand.class).execute(null);

    UndoManager undoManager = editor.getUndoManager();
    if (!undoManager.addEdit(new PasteEdit(pastedItems))) {
        log.info("Could not add edit " + this.getClass());
    }
    editor.setFileState(FileState.DIRTY);

    AppContext.getContext().getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    AppContext.getContext().getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

    if (pastedItems.size() == 1) {
        editor.getSelectedItems().clear();
        editor.setSelectedItem(pastedItems.get(0));
    }

    editor.refresh();
}

From source file:org.pmedv.blackboard.commands.RedoCommand.java

@Override
public void execute(ActionEvent e) {

    ApplicationContext ctx = AppContext.getContext();
    BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    UndoManager undoManager = editor.getUndoManager();

    try {/*from w  w  w . j av a  2 s  .  com*/
        undoManager.redo();
    } catch (CannotRedoException r) {
        log.warn("Cannot redo.");
    }

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}

From source file:org.pmedv.blackboard.commands.RotateCCWCommand.java

@Override
public void execute(ActionEvent e) {

    ApplicationContext ctx = AppContext.getContext();
    BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    UndoManager undoManager = editor.getUndoManager();

    if (editor.getSelectedItem() != null) {
        if (editor.getSelectedItem() instanceof Rotateable) {
            Rotateable part = (Rotateable) editor.getSelectedItem();
            part.rotateCCW();/*from www.j ava2 s . c o  m*/
            if (!undoManager.addEdit(new RotateCCWEdit(part))) {
                log.info("Could not add edit : " + this.getClass());
            }
            editor.refresh();
        }
    }

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}

From source file:org.pmedv.blackboard.commands.RotateCWCommand.java

@Override
public void execute(ActionEvent evt) {

    ApplicationContext ctx = AppContext.getContext();
    BoardEditor editor = EditorUtils.getCurrentActiveEditor();
    UndoManager undoManager = editor.getUndoManager();

    if (editor.getSelectedItem() != null) {
        if (editor.getSelectedItem() instanceof Rotateable) {

            if (editor.getSelectedItem() instanceof Part) {
                //               BoardUtil.findConnectionsToItem(editor.getSelectedItem(), editor.getModel());
                //               Part part = (Part)editor.getSelectedItem();            
                //               if (editor.isMagnetic()) {
                //                  
                //                  for (WireConnection wc : part.getWireConnections()) {
                //                     
                //                     if (wc.getType().equals(WireConnectionType.START)) {                     
                //                        Point s = BoardUtil.rotatePoint(wc.getLine().getStart().getX(), wc.getLine().getStart().getY(), part.getXLoc(), part.getYLoc(), -90);
                //                        wc.getLine().getStart().setLocation(BoardUtil.snap(s.x, editor.getRaster()),BoardUtil.snap(s.y, editor.getRaster()));                     
                //                     }
                //                     else if (wc.getType().equals(WireConnectionType.END)) {
                //                        Point e = BoardUtil.rotatePoint(wc.getLine().getEnd().getX(), wc.getLine().getEnd().getY(), part.getXLoc(), part.getYLoc(), -90);
                //                        wc.getLine().getEnd().setLocation(BoardUtil.snap(e.x, editor.getRaster()),BoardUtil.snap(e.y, editor.getRaster()));                                          
                //                     }
                //                     
                //                  }
                //                  
                //               }
                //               
            }/*w w w  . java  2s  .c o  m*/

            Rotateable r = (Rotateable) editor.getSelectedItem();
            r.rotateCW();

            if (!undoManager.addEdit(new RotateCWEdit(r))) {
                log.info("Could not add edit " + this.getClass());
            }
            editor.refresh();
        }
    }

    ctx.getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
    ctx.getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

}